Browse Source

All of the JSONRPC APIs now work nicely with "latest" and "pending".

cl-refactor
Gav Wood 10 years ago
parent
commit
80e22a1386
  1. 28
      libdevcore/CommonData.h
  2. 17
      libethcore/CommonJS.cpp
  3. 3
      libethcore/CommonJS.h
  4. 8
      libethereum/BlockQueue.cpp
  5. 19
      libethereum/ClientBase.cpp
  6. 3
      libethereum/ClientBase.h
  7. 16
      libethereum/Interface.h
  8. 3
      libethereum/State.h
  9. 36
      libweb3jsonrpc/WebThreeStubServerBase.cpp

28
libdevcore/CommonData.h

@ -236,6 +236,34 @@ inline std::vector<_T>& operator+=(std::vector<typename std::enable_if<!std::is_
return _a; return _a;
} }
/// Insert the contents of a container into a set
template <class T, class U> std::set<T>& operator+=(std::set<T>& _a, U const& _b)
{
for (auto const& i: _b)
_a.insert(i);
return _a;
}
/// Concatenate the contents of a container onto a vector
template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U const& _b)
{
for (auto const& i: _b)
_a.push_back(i);
return _a;
}
/// Insert the contents of a container into a set
template <class T, class U> std::set<T> operator+(std::set<T> _a, U const& _b)
{
return _a += _b;
}
/// Concatenate the contents of a container onto a vector
template <class T, class U> std::vector<T> operator+(std::vector<T> _a, U const& _b)
{
return _a += _b;
}
/// Concatenate two vectors of elements. /// Concatenate two vectors of elements.
template <class _T> template <class _T>
inline std::vector<_T> operator+(std::vector<_T> const& _a, std::vector<_T> const& _b) inline std::vector<_T> operator+(std::vector<_T> const& _a, std::vector<_T> const& _b)

17
libethcore/CommonJS.cpp

@ -65,5 +65,22 @@ std::string prettyU256(u256 _n, bool _abridged)
return s.str(); return s.str();
} }
namespace eth
{
BlockNumber jsToBlockNumber(std::string const& _js)
{
if (_js == "latest")
return LatestBlock;
else if (_js == "earliest")
return 0;
else if (_js == "pending")
return PendingBlock;
else
return (unsigned)jsToInt(_js);
}
}
} }

3
libethcore/CommonJS.h

@ -67,5 +67,8 @@ struct TransactionSkeleton
u256 gasPrice; u256 gasPrice;
}; };
/// Convert to a block number, a bit like jsToInt, except that it correctly recognises "pending" and "latest".
BlockNumber jsToBlockNumber(std::string const& _js);
} }
} }

8
libethereum/BlockQueue.cpp

@ -107,14 +107,6 @@ ImportResult BlockQueue::import(bytesConstRef _block, BlockChain const& _bc)
} }
} }
namespace dev {
template <class T, class U> std::set<T>& operator+=(std::set<T>& _a, U const& _b)
{
for (auto const& i: _b)
_a.insert(i);
return _a;
} }
bool BlockQueue::doneDrain(h256s const& _bad) bool BlockQueue::doneDrain(h256s const& _bad)
{ {
WriteGuard l(m_lock); WriteGuard l(m_lock);

19
libethereum/ClientBase.cpp

@ -285,11 +285,6 @@ LocalisedLogEntries ClientBase::checkWatch(unsigned _watchId)
return ret; return ret;
} }
h256 ClientBase::hashFromNumber(unsigned _number) const
{
return bc().numberHash(_number);
}
BlockInfo ClientBase::blockInfo(h256 _hash) const BlockInfo ClientBase::blockInfo(h256 _hash) const
{ {
return BlockInfo(bc().block(_hash)); return BlockInfo(bc().block(_hash));
@ -369,6 +364,11 @@ Transactions ClientBase::pending() const
return postMine().pending(); return postMine().pending();
} }
h256s ClientBase::pendingHashes() const
{
return h256s() + postMine().pendingHashes();
}
StateDiff ClientBase::diff(unsigned _txi, h256 _block) const StateDiff ClientBase::diff(unsigned _txi, h256 _block) const
{ {
@ -399,3 +399,12 @@ Address ClientBase::address() const
{ {
return preMine().address(); return preMine().address();
} }
h256 ClientBase::hashFromNumber(BlockNumber _number) const
{
if (_number == PendingBlock)
return h256();
if (_number == LatestBlock)
return bc().currentHash();
return bc().numberHash(_number);
}

3
libethereum/ClientBase.h

@ -109,7 +109,7 @@ public:
virtual LocalisedLogEntries checkWatch(unsigned _watchId) override; virtual LocalisedLogEntries checkWatch(unsigned _watchId) override;
// TODO: switch all the _blockHash arguments to also accept _blockNumber // TODO: switch all the _blockHash arguments to also accept _blockNumber
virtual h256 hashFromNumber(unsigned _number) const override; virtual h256 hashFromNumber(BlockNumber _number) const override;
virtual eth::BlockInfo blockInfo(h256 _hash) const override; virtual eth::BlockInfo blockInfo(h256 _hash) const override;
virtual eth::BlockDetails blockDetails(h256 _hash) const override; virtual eth::BlockDetails blockDetails(h256 _hash) const override;
virtual eth::Transaction transaction(h256 _transactionHash) const override; virtual eth::Transaction transaction(h256 _transactionHash) const override;
@ -122,6 +122,7 @@ public:
virtual unsigned uncleCount(h256 _blockHash) const override; virtual unsigned uncleCount(h256 _blockHash) const override;
virtual unsigned number() const override; virtual unsigned number() const override;
virtual eth::Transactions pending() const override; virtual eth::Transactions pending() const override;
virtual h256s pendingHashes() const override;
using Interface::diff; using Interface::diff;
virtual StateDiff diff(unsigned _txi, h256 _block) const override; virtual StateDiff diff(unsigned _txi, h256 _block) const override;

16
libethereum/Interface.h

@ -112,10 +112,11 @@ public:
// [BLOCK QUERY API] // [BLOCK QUERY API]
virtual h256 hashFromNumber(unsigned _number) const = 0; virtual Transaction transaction(h256 _transactionHash) const = 0;
virtual h256 hashFromNumber(BlockNumber _number) const = 0;
virtual BlockInfo blockInfo(h256 _hash) const = 0; virtual BlockInfo blockInfo(h256 _hash) const = 0;
virtual BlockDetails blockDetails(h256 _hash) const = 0; virtual BlockDetails blockDetails(h256 _hash) const = 0;
virtual Transaction transaction(h256 _transactionHash) const = 0;
virtual Transaction transaction(h256 _blockHash, unsigned _i) const = 0; virtual Transaction transaction(h256 _blockHash, unsigned _i) const = 0;
virtual BlockInfo uncle(h256 _blockHash, unsigned _i) const = 0; virtual BlockInfo uncle(h256 _blockHash, unsigned _i) const = 0;
virtual UncleHashes uncleHashes(h256 _blockHash) const = 0; virtual UncleHashes uncleHashes(h256 _blockHash) const = 0;
@ -124,6 +125,16 @@ public:
virtual Transactions transactions(h256 _blockHash) const = 0; virtual Transactions transactions(h256 _blockHash) const = 0;
virtual TransactionHashes transactionHashes(h256 _blockHash) const = 0; virtual TransactionHashes transactionHashes(h256 _blockHash) const = 0;
BlockInfo blockInfo(BlockNumber _block) const { return blockInfo(hashFromNumber(_block)); }
BlockDetails blockDetails(BlockNumber _block) const { return blockDetails(hashFromNumber(_block)); }
Transaction transaction(BlockNumber _block, unsigned _i) const { if (_block == PendingBlock) { auto p = pending(); return _i < p.size() ? p[_i] : Transaction(); } return transaction(hashFromNumber(_block)); }
unsigned transactionCount(BlockNumber _block) const { if (_block == PendingBlock) { auto p = pending(); return p.size(); } return transactionCount(hashFromNumber(_block)); }
Transactions transactions(BlockNumber _block) const { if (_block == PendingBlock) return pending(); return transactions(hashFromNumber(_block)); }
TransactionHashes transactionHashes(BlockNumber _block) const { if (_block == PendingBlock) return pendingHashes(); return transactionHashes(hashFromNumber(_block)); }
BlockInfo uncle(BlockNumber _block, unsigned _i) const { return uncle(hashFromNumber(_block), _i); }
UncleHashes uncleHashes(BlockNumber _block) const { return uncleHashes(hashFromNumber(_block)); }
unsigned uncleCount(BlockNumber _block) const { return uncleCount(hashFromNumber(_block)); }
// [EXTRA API]: // [EXTRA API]:
/// @returns The height of the chain. /// @returns The height of the chain.
@ -132,6 +143,7 @@ public:
/// Get a map containing each of the pending transactions. /// Get a map containing each of the pending transactions.
/// @TODO: Remove in favour of transactions(). /// @TODO: Remove in favour of transactions().
virtual Transactions pending() const = 0; virtual Transactions pending() const = 0;
virtual h256s pendingHashes() const = 0;
/// Differences between transactions. /// Differences between transactions.
StateDiff diff(unsigned _txi) const { return diff(_txi, m_default); } StateDiff diff(unsigned _txi) const { return diff(_txi, m_default); }

3
libethereum/State.h

@ -274,6 +274,9 @@ public:
/// Get the list of pending transactions. /// Get the list of pending transactions.
Transactions const& pending() const { return m_transactions; } Transactions const& pending() const { return m_transactions; }
/// Get the list of hashes of pending transactions.
h256Set const& pendingHashes() const { return m_transactionSet; }
/// Get the transaction receipt for the transaction of the given index. /// Get the transaction receipt for the transaction of the given index.
TransactionReceipt const& receipt(unsigned _i) const { return m_receipts[_i]; } TransactionReceipt const& receipt(unsigned _i) const { return m_receipts[_i]; }

36
libweb3jsonrpc/WebThreeStubServerBase.cpp

@ -170,18 +170,6 @@ static Json::Value toJson(map<u256, u256> const& _storage)
return res; return res;
} }
static unsigned toBlockNumber(std::string const& _js)
{
if (_js == "latest")
return LatestBlock;
else if (_js == "earliest")
return 0;
else if (_js == "pending")
return PendingBlock;
else
return (unsigned)jsToInt(_js);
}
static dev::eth::LogFilter toLogFilter(Json::Value const& _json) // commented to avoid warning. Uncomment once in use @ PoC-7. static dev::eth::LogFilter toLogFilter(Json::Value const& _json) // commented to avoid warning. Uncomment once in use @ PoC-7.
{ {
dev::eth::LogFilter filter; dev::eth::LogFilter filter;
@ -190,9 +178,9 @@ static dev::eth::LogFilter toLogFilter(Json::Value const& _json) // commented to
// check only !empty. it should throw exceptions if input params are incorrect // check only !empty. it should throw exceptions if input params are incorrect
if (!_json["fromBlock"].empty()) if (!_json["fromBlock"].empty())
filter.withEarliest(toBlockNumber(_json["fromBlock"].asString())); filter.withEarliest(jsToBlockNumber(_json["fromBlock"].asString()));
if (!_json["toBlock"].empty()) if (!_json["toBlock"].empty())
filter.withLatest(toBlockNumber(_json["toBlock"].asString())); filter.withLatest(jsToBlockNumber(_json["toBlock"].asString()));
if (!_json["address"].empty()) if (!_json["address"].empty())
{ {
if (_json["address"].isArray()) if (_json["address"].isArray())
@ -336,7 +324,7 @@ string WebThreeStubServerBase::eth_getBalance(string const& _address, string con
{ {
try try
{ {
return toJS(client()->balanceAt(jsToAddress(_address), toBlockNumber(_blockNumber))); return toJS(client()->balanceAt(jsToAddress(_address), jsToBlockNumber(_blockNumber)));
} }
catch (...) catch (...)
{ {
@ -348,7 +336,7 @@ string WebThreeStubServerBase::eth_getStorageAt(string const& _address, string c
{ {
try try
{ {
return toJS(client()->stateAt(jsToAddress(_address), jsToU256(_position), toBlockNumber(_blockNumber))); return toJS(client()->stateAt(jsToAddress(_address), jsToU256(_position), jsToBlockNumber(_blockNumber)));
} }
catch (...) catch (...)
{ {
@ -360,7 +348,7 @@ string WebThreeStubServerBase::eth_getTransactionCount(string const& _address, s
{ {
try try
{ {
return toJS(client()->countAt(jsToAddress(_address), toBlockNumber(_blockNumber))); return toJS(client()->countAt(jsToAddress(_address), jsToBlockNumber(_blockNumber)));
} }
catch (...) catch (...)
{ {
@ -385,7 +373,7 @@ string WebThreeStubServerBase::eth_getBlockTransactionCountByNumber(string const
{ {
try try
{ {
return toJS(_blockNumber == "pending" ? client()->pending().size() : client()->transactionCount(client()->hashFromNumber(toBlockNumber(_blockNumber)))); return toJS(client()->transactionCount(jsToBlockNumber(_blockNumber)));
} }
catch (...) catch (...)
{ {
@ -409,7 +397,7 @@ string WebThreeStubServerBase::eth_getUncleCountByBlockNumber(string const& _blo
{ {
try try
{ {
return toJS(client()->uncleCount(client()->hashFromNumber(toBlockNumber(_blockNumber)))); return toJS(client()->uncleCount(jsToBlockNumber(_blockNumber)));
} }
catch (...) catch (...)
{ {
@ -421,7 +409,7 @@ string WebThreeStubServerBase::eth_getCode(string const& _address, string const&
{ {
try try
{ {
return toJS(client()->codeAt(jsToAddress(_address), toBlockNumber(_blockNumber))); return toJS(client()->codeAt(jsToAddress(_address), jsToBlockNumber(_blockNumber)));
} }
catch (...) catch (...)
{ {
@ -499,7 +487,7 @@ string WebThreeStubServerBase::eth_call(Json::Value const& _json, string const&
try try
{ {
t = toTransaction(_json); t = toTransaction(_json);
number = toBlockNumber(_blockNumber); number = jsToBlockNumber(_blockNumber);
} }
catch (...) catch (...)
{ {
@ -546,7 +534,7 @@ Json::Value WebThreeStubServerBase::eth_getBlockByNumber(string const& _blockNum
{ {
try try
{ {
auto h = client()->hashFromNumber(jsToInt(_blockNumber)); auto h = jsToBlockNumber(_blockNumber);
if (_includeTransactions) if (_includeTransactions)
return toJson(client()->blockInfo(h), client()->uncleHashes(h), client()->transactions(h)); return toJson(client()->blockInfo(h), client()->uncleHashes(h), client()->transactions(h));
else else
@ -586,7 +574,7 @@ Json::Value WebThreeStubServerBase::eth_getTransactionByBlockNumberAndIndex(stri
{ {
try try
{ {
return toJson(client()->transaction(client()->hashFromNumber(jsToInt(_blockNumber)), jsToInt(_transactionIndex))); return toJson(client()->transaction(jsToBlockNumber(_blockNumber), jsToInt(_transactionIndex)));
} }
catch (...) catch (...)
{ {
@ -610,7 +598,7 @@ Json::Value WebThreeStubServerBase::eth_getUncleByBlockNumberAndIndex(string con
{ {
try try
{ {
return toJson(client()->uncle(client()->hashFromNumber(toBlockNumber(_blockNumber)), jsToInt(_uncleIndex))); return toJson(client()->uncle(jsToBlockNumber(_blockNumber), jsToInt(_uncleIndex)));
} }
catch (...) catch (...)
{ {

Loading…
Cancel
Save