Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
state addresses ordered by hash
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed May 18, 2018
1 parent 1daf355 commit abcfc3f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
22 changes: 22 additions & 0 deletions libethereum/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,28 @@ unordered_map<Address, u256> State::addresses() const
#endif
}

std::pair<State::addressMap, h256> State::addresses(h256 const& _begin, size_t _maxResults) const
{
map<h256, Address> ret;
h256 nextKey;

for (auto it = m_state.hashedLowerBound(_begin); it != m_state.hashedEnd(); ++it)
{
if (ret.size() == _maxResults)
{
nextKey = h256((*it).first);
break;
}

h256 const hashedAddress((*it).first);
Address const address = Address(it.key());
// maybe here check in m_cache whether account is still alive
ret[hashedAddress] = address;
}

return {ret, nextKey};
}

void State::setRoot(h256 const& _r)
{
m_cache.clear();
Expand Down
5 changes: 5 additions & 0 deletions libethereum/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ class State
/// @throws InterfaceNotSupported if compiled without ETH_FATDB.
std::unordered_map<Address, u256> addresses() const;

/// @returns the map with maximum _maxResults elements containing hash->addresses and the next
/// address hash. This method faster then addresses() const;
typedef std::map<h256, Address> addressMap;
std::pair<addressMap, h256> addresses(h256 const& _begin, size_t _maxResults) const;

/// Execute a given transaction.
/// This will change the state accordingly.
std::pair<ExecutionResult, TransactionReceipt> execute(EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, Transaction const& _t, Permanence _p = Permanence::Committed, OnOpFunc const& _onOp = OnOpFunc());
Expand Down
63 changes: 34 additions & 29 deletions libweb3jsonrpc/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,41 @@ Json::Value Debug::debug_traceBlockByNumber(int _blockNumber, Json::Value const&
return ret;
}

Json::Value Debug::debug_accountRangeAt(string const& _blockHashOrNumber, int _txIndex, string const& _address, int _maxResults)
Json::Value Debug::debug_accountRangeAt(
string const& _blockHashOrNumber, int _txIndex, string const& _addressHash, int _maxResults)
{
Json::Value ret(Json::arrayValue);

if (_txIndex < 0)
throw jsonrpc::JsonRpcException("Negative index");
if (_maxResults <= 0)
throw jsonrpc::JsonRpcException("Nonpositive maxResults");

try
{
Block block = m_eth.block(blockHash(_blockHashOrNumber));

unsigned const i = ((unsigned)_txIndex < block.pending().size()) ? (unsigned)_txIndex : block.pending().size();
State state(State::Null);
createIntermediateState(state, block, i, m_eth.blockChain());
Address from(_address);
for (auto const& addr : state.addresses())
{
if (addr.first >= from)
ret.append(toHexPrefixed(addr.first));
}
}
catch (Exception const& _e)
{
cwarn << diagnostic_information(_e);
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS);
}

return ret;
Json::Value ret(Json::objectValue);

if (_txIndex < 0)
throw jsonrpc::JsonRpcException("Negative index");
if (_maxResults <= 0)
throw jsonrpc::JsonRpcException("Nonpositive maxResults");

try
{
Block block = m_eth.block(blockHash(_blockHashOrNumber));
size_t const i = std::min(static_cast<size_t>(_txIndex), block.pending().size());
State state(State::Null);
if (block.info().number() >= m_eth.chainParams().byzantiumForkBlock)
state = block.state();
else
createIntermediateState(state, block, i, m_eth.blockChain());
auto const addressMap = state.addresses(h256(_addressHash), _maxResults);

Json::Value addressList(Json::objectValue);
for (auto const& record : addressMap.first)
addressList[toString(record.first)] = toString(record.second);

ret["addressMap"] = addressList;
ret["nextKey"] = toString(addressMap.second);
}
catch (Exception const& _e)
{
cwarn << diagnostic_information(_e);
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_RPC_INVALID_PARAMS);
}

return ret;
}

Json::Value Debug::debug_storageRangeAt(string const& _blockHashOrNumber, int _txIndex, string const& _address, string const& _begin, int _maxResults)
Expand Down
5 changes: 3 additions & 2 deletions libweb3jsonrpc/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class Debug: public DebugFace
return RPCModules{RPCModule{"debug", "1.0"}};
}

virtual Json::Value debug_accountRangeAt(std::string const& _blockHashOrNumber, int _txIndex, std::string const& _address, int _maxResults) override;
virtual Json::Value debug_traceTransaction(std::string const& _txHash, Json::Value const& _json) override;
virtual Json::Value debug_accountRangeAt(std::string const& _blockHashOrNumber, int _txIndex,
std::string const& _addressHash, int _maxResults) override;
virtual Json::Value debug_traceTransaction(std::string const& _txHash, Json::Value const& _json) override;
virtual Json::Value debug_traceCall(Json::Value const& _call, std::string const& _blockNumber, Json::Value const& _options) override;
virtual Json::Value debug_traceBlockByNumber(int _blockNumber, Json::Value const& _json) override;
virtual Json::Value debug_traceBlockByHash(std::string const& _blockHash, Json::Value const& _json) override;
Expand Down

0 comments on commit abcfc3f

Please sign in to comment.