diff --git a/libethcore/Common.h b/libethcore/Common.h index 1d48803cb..466a6f070 100644 --- a/libethcore/Common.h +++ b/libethcore/Common.h @@ -77,6 +77,10 @@ using BlockNumber = unsigned; static const BlockNumber LatestBlock = (BlockNumber)-2; static const BlockNumber PendingBlock = (BlockNumber)-1; +static const h256 LatestBlockHash = h256(2); +static const h256 EarliestBlockHash = h256(1); +static const h256 PendingBlockHash = h256(0); + enum class RelativeBlock: BlockNumber { diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 86b745141..2c4e3e0bf 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -337,7 +337,7 @@ void Client::appendFromNewPending(TransactionReceipt const& _receipt, h256Hash& { Guard l(x_filtersWatches); for (pair& i: m_filters) - if (i.second.filter.envelops(RelativeBlock::Pending, m_bc.number() + 1)) + if (isInBlockHashRange(i.second.filter.earliest(), i.second.filter.latest(), PendingBlockHash)) { // acceptable number. auto m = i.second.filter.matches(_receipt); @@ -359,7 +359,7 @@ void Client::appendFromNewBlock(h256 const& _block, h256Hash& io_changed) Guard l(x_filtersWatches); for (pair& i: m_filters) - if (i.second.filter.envelops(RelativeBlock::Latest, d.number) && i.second.filter.matches(d.logBloom)) + if (isInBlockHashRange(i.second.filter.earliest(), i.second.filter.latest(), d.hash()) && i.second.filter.matches(d.logBloom)) // acceptable number & looks like block may contain a matching log entry. for (size_t j = 0; j < br.receipts.size(); j++) { diff --git a/libethereum/ClientBase.cpp b/libethereum/ClientBase.cpp index 8dc666bb5..12246daa2 100644 --- a/libethereum/ClientBase.cpp +++ b/libethereum/ClientBase.cpp @@ -171,8 +171,8 @@ LocalisedLogEntries ClientBase::logs(unsigned _watchId) const LocalisedLogEntries ClientBase::logs(LogFilter const& _f) const { LocalisedLogEntries ret; - unsigned begin = min(bc().number() + 1, (unsigned)_f.latest()); - unsigned end = min(bc().number(), min(begin, (unsigned)_f.earliest())); + unsigned begin = min(bc().number() + 1, (unsigned)numberFromHash(_f.latest())); + unsigned end = min(bc().number(), min(begin, (unsigned)numberFromHash(_f.earliest()))); // Handle pending transactions differently as they're not on the block chain. if (begin > bc().number()) @@ -442,6 +442,31 @@ h256 ClientBase::hashFromNumber(BlockNumber _number) const BlockNumber ClientBase::numberFromHash(h256 _blockHash) const { + if (_blockHash == PendingBlockHash) + _blockHash = hashFromNumber(PendingBlock); + else if (_blockHash == LatestBlockHash) + _blockHash = hashFromNumber(LatestBlock); + else if (_blockHash == EarliestBlockHash) + _blockHash = hashFromNumber(0); return bc().number(_blockHash); } +int ClientBase::compareBlockHashes(h256 _h1, h256 _h2) const +{ + BlockNumber n1 = numberFromHash(_h1); + BlockNumber n2 = numberFromHash(_h2); + + if (n1 > n2) { + return 1; + } else if (n1 == n2) { + return 0; + } + return -1; +} + +bool ClientBase::isInBlockHashRange(h256 _from, h256 _to, h256 _q) const +{ + int c1 = compareBlockHashes(_from, _q); + int c2 = compareBlockHashes(_q, _to); + return (c1 == 0 || c1 == -1) && (c2 == 0 || c2 == -1); +} diff --git a/libethereum/ClientBase.h b/libethereum/ClientBase.h index 2b271b1df..7b7b3cb76 100644 --- a/libethereum/ClientBase.h +++ b/libethereum/ClientBase.h @@ -117,6 +117,8 @@ public: virtual h256 hashFromNumber(BlockNumber _number) const override; virtual BlockNumber numberFromHash(h256 _blockHash) const override; + virtual int compareBlockHashes(h256 _h1, h256 _h2) const override; + virtual bool isInBlockHashRange(h256 _from, h256 _to, h256 _q) const override; virtual BlockInfo blockInfo(h256 _hash) const override; virtual BlockDetails blockDetails(h256 _hash) const override; virtual Transaction transaction(h256 _transactionHash) const override; diff --git a/libethereum/Interface.h b/libethereum/Interface.h index f7253ad29..05fa3873d 100644 --- a/libethereum/Interface.h +++ b/libethereum/Interface.h @@ -137,6 +137,8 @@ public: virtual std::pair transactionLocation(h256 const& _transactionHash) const = 0; virtual h256 hashFromNumber(BlockNumber _number) const = 0; virtual BlockNumber numberFromHash(h256 _blockHash) const = 0; + virtual int compareBlockHashes(h256 _h1, h256 _h2) const = 0; + virtual bool isInBlockHashRange(h256 _from, h256 _to, h256 _q) const = 0; virtual BlockInfo blockInfo(h256 _hash) const = 0; virtual BlockDetails blockDetails(h256 _hash) const = 0; diff --git a/libethereum/LogFilter.cpp b/libethereum/LogFilter.cpp index 21ba9d3ef..6e0fbd709 100644 --- a/libethereum/LogFilter.cpp +++ b/libethereum/LogFilter.cpp @@ -46,33 +46,6 @@ h256 LogFilter::sha3() const return dev::sha3(s.out()); } -static bool isNoLater(RelativeBlock _logBlockRelation, u256 _logBlockNumber, unsigned _latest) -{ - if (_latest == PendingBlock) - return true; - else if (_latest == LatestBlock) - return _logBlockRelation == RelativeBlock::Latest; - else - return _logBlockNumber <= _latest; -} - -static bool isNoEarlier(RelativeBlock _logBlockRelation, u256 _logBlockNumber, unsigned _earliest) -{ - if (_earliest == PendingBlock) - return _logBlockRelation == RelativeBlock::Pending; - else if (_earliest == LatestBlock) - return true; - else - return _logBlockNumber >= _earliest; -} - -bool LogFilter::envelops(RelativeBlock _logBlockRelation, u256 _logBlockNumber) const -{ - return - isNoLater(_logBlockRelation, _logBlockNumber, m_latest) && - isNoEarlier(_logBlockRelation, _logBlockNumber, m_earliest); -} - bool LogFilter::matches(LogBloom _bloom) const { if (m_addresses.size()) diff --git a/libethereum/LogFilter.h b/libethereum/LogFilter.h index 97ff5a3b1..ff33346f8 100644 --- a/libethereum/LogFilter.h +++ b/libethereum/LogFilter.h @@ -45,15 +45,14 @@ class State; class LogFilter { public: - LogFilter(unsigned _earliest = 0, unsigned _latest = PendingBlock): m_earliest(_earliest), m_latest(_latest) {} + LogFilter(h256 _earliest = EarliestBlockHash, h256 _latest = PendingBlockHash): m_earliest(_earliest), m_latest(_latest) {} void streamRLP(RLPStream& _s) const; h256 sha3() const; - unsigned earliest() const { return m_earliest; } - unsigned latest() const { return m_latest; } + h256 earliest() const { return m_earliest; } + h256 latest() const { return m_latest; } - bool envelops(RelativeBlock _logBlockRelation, u256 _logBlockNumber) const; std::vector bloomPossibilities() const; bool matches(LogBloom _bloom) const; bool matches(State const& _s, unsigned _i) const; @@ -61,16 +60,16 @@ public: LogFilter address(Address _a) { m_addresses.insert(_a); return *this; } LogFilter topic(unsigned _index, h256 const& _t) { if (_index < 4) m_topics[_index].insert(_t); return *this; } - LogFilter withEarliest(int _e) { m_earliest = _e; return *this; } - LogFilter withLatest(int _e) { m_latest = _e; return *this; } + LogFilter withEarliest(h256 _e) { m_earliest = _e; return *this; } + LogFilter withLatest(h256 _e) { m_latest = _e; return *this; } friend std::ostream& dev::eth::operator<<(std::ostream& _out, dev::eth::LogFilter const& _s); private: AddressHash m_addresses; std::array m_topics; - unsigned m_earliest = 0; - unsigned m_latest = LatestBlock; + h256 m_earliest = EarliestBlockHash; + h256 m_latest = PendingBlockHash; }; } diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index ff7b84dc4..0150bfb82 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -201,9 +201,9 @@ static dev::eth::LogFilter toLogFilter(Json::Value const& _json) // commented to // check only !empty. it should throw exceptions if input params are incorrect if (!_json["fromBlock"].empty()) - filter.withEarliest(jsToBlockNumber(_json["fromBlock"].asString())); + filter.withEarliest(jsToFixed<32>(_json["fromBlock"].asString())); if (!_json["toBlock"].empty()) - filter.withLatest(jsToBlockNumber(_json["toBlock"].asString())); + filter.withLatest(jsToFixed<32>(_json["toBlock"].asString())); if (!_json["address"].empty()) { if (_json["address"].isArray()) diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index aa469d8a0..c9974a666 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -251,7 +251,7 @@ void MixClient::executeTransaction(Transaction const& _t, State& _state, bool _c h256Set changed; Guard l(x_filtersWatches); for (std::pair& i: m_filters) - if ((unsigned)i.second.filter.latest() > bc().number()) + if (compareBlockHashes(i.second.filter.latest(), bc().currentHash()) > 0) { // acceptable number. auto m = i.second.filter.matches(_state.receipt(_state.pending().size() - 1));