From f6719db60fba03f3b0626e76f98e3eec97f0e922 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 14 Jul 2015 00:32:04 +0200 Subject: [PATCH] commit eth_getFilterLogs returns reverted logs --- libethereum/Client.cpp | 7 ++--- libethereum/ClientBase.cpp | 58 ++++++++++++++++++-------------------- libethereum/ClientBase.h | 1 + 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 42b5bbcf4..b485f7def 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -412,7 +412,6 @@ void Client::appendFromNewPending(TransactionReceipt const& _receipt, h256Hash& void Client::appendFromBlock(h256 const& _block, BlockPolarity _polarity, h256Hash& io_changed) { // TODO: more precise check on whether the txs match. - auto d = m_bc.info(_block); auto receipts = m_bc.receipts(_block).receipts; Guard l(x_filtersWatches); @@ -421,18 +420,16 @@ void Client::appendFromBlock(h256 const& _block, BlockPolarity _polarity, h256Ha for (pair& i: m_filters) { // acceptable number & looks like block may contain a matching log entry. - unsigned logIndex = 0; for (size_t j = 0; j < receipts.size(); j++) { - logIndex++; auto tr = receipts[j]; auto m = i.second.filter.matches(tr); if (m.size()) { - auto transactionHash = transaction(d.hash(), j).sha3(); + auto transactionHash = transaction(_block, j).sha3(); // filter catches them for (LogEntry const& l: m) - i.second.changes.push_back(LocalisedLogEntry(l, d.hash(), (BlockNumber)d.number, transactionHash, j, logIndex, _polarity)); + i.second.changes.push_back(LocalisedLogEntry(l, _block, (BlockNumber)bc().number(_block), transactionHash, j, 0, _polarity)); io_changed.insert(i.first); } } diff --git a/libethereum/ClientBase.cpp b/libethereum/ClientBase.cpp index 375fd74f7..440443156 100644 --- a/libethereum/ClientBase.cpp +++ b/libethereum/ClientBase.cpp @@ -171,49 +171,47 @@ LocalisedLogEntries ClientBase::logs(LogFilter const& _f) const // Might have a transaction that contains a matching log. TransactionReceipt const& tr = temp.receipt(i); LogEntries le = _f.matches(tr); - if (le.size()) - for (unsigned j = 0; j < le.size(); ++j) - ret.insert(ret.begin(), LocalisedLogEntry(le[j])); + for (unsigned j = 0; j < le.size(); ++j) + ret.insert(ret.begin(), LocalisedLogEntry(le[j])); } begin = bc().number(); } - + + // Handle blocks from main chain set matchingBlocks; for (auto const& i: _f.bloomPossibilities()) for (auto u: bc().withBlockBloom(i, end, begin)) matchingBlocks.insert(u); - unsigned falsePos = 0; for (auto n: matchingBlocks) + appendLogsFromBlock(_f, bc().numberHash(n), BlockPolarity::Live, ret); + + // Handle reverted blocks + h256s blocks; + h256 ancestor; + unsigned ancestorIndex; + tie(blocks, ancestor, ancestorIndex) = bc().treeRoute(_f.earliest(), _f.latest(), false); + + for (size_t i = 0; i < ancestorIndex; i++) + appendLogsFromBlock(_f, blocks[i], BlockPolarity::Dead, ret); + + return ret; +} + +void ClientBase::appendLogsFromBlock(LogFilter const& _f, h256 const& _blockHash, BlockPolarity _polarity, LocalisedLogEntries& io_logs) const +{ + auto receipts = bc().receipts(_blockHash).receipts; + for (size_t i = 0; i < receipts.size(); i++) { - int total = 0; - auto h = bc().numberHash(n); - auto info = bc().info(h); - auto receipts = bc().receipts(h).receipts; - unsigned logIndex = 0; - for (size_t i = 0; i < receipts.size(); i++) + TransactionReceipt receipt = receipts[i]; + if (_f.matches(receipt.bloom())) { - logIndex++; - TransactionReceipt receipt = receipts[i]; - if (_f.matches(receipt.bloom())) - { - auto th = transaction(info.hash(), i).sha3(); - LogEntries le = _f.matches(receipt); - if (le.size()) - { - total += le.size(); - for (unsigned j = 0; j < le.size(); ++j) - ret.insert(ret.begin(), LocalisedLogEntry(le[j], info.hash(), (BlockNumber)info.number, th, i, logIndex, BlockPolarity::Live)); - } - } - - if (!total) - falsePos++; + auto th = transaction(_blockHash, i).sha3(); + LogEntries le = _f.matches(receipt); + for (unsigned j = 0; j < le.size(); ++j) + io_logs.insert(io_logs.begin(), LocalisedLogEntry(le[j], _blockHash, (BlockNumber)bc().number(_blockHash), th, i, 0, _polarity)); } } - - cdebug << matchingBlocks.size() << "searched from" << (end - begin) << "skipped; " << falsePos << "false +ves"; - return ret; } unsigned ClientBase::installWatch(LogFilter const& _f, Reaping _r) diff --git a/libethereum/ClientBase.h b/libethereum/ClientBase.h index 91a9daf29..b68fc540f 100644 --- a/libethereum/ClientBase.h +++ b/libethereum/ClientBase.h @@ -104,6 +104,7 @@ public: virtual LocalisedLogEntries logs(unsigned _watchId) const override; virtual LocalisedLogEntries logs(LogFilter const& _filter) const override; + virtual void appendLogsFromBlock(LogFilter const& _filter, h256 const& _blockHash, BlockPolarity _polarity, LocalisedLogEntries& io_logs) const; /// Install, uninstall and query watches. virtual unsigned installWatch(LogFilter const& _filter, Reaping _r = Reaping::Automatic) override;