Skip to content

Commit

Permalink
feat: add method to get storage proof
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed Nov 27, 2023
1 parent 1323870 commit 4cff9b5
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 22 deletions.
5 changes: 0 additions & 5 deletions doc/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ will build out of the box without further effort:
cmake -DCONAN_PROFILE=clang -DCMAKE_BUILD_TYPE=RelWithDebInfo -DTARAXA_ENABLE_LTO=OFF -DTARAXA_STATIC_BUILD=OFF ../
make -j$(nproc)

And optional:

# optional
make install # defaults to /usr/local

## Building on MacOS

### Install taraxa-node dependencies:
Expand Down
2 changes: 1 addition & 1 deletion for_devs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aiohttp==3.8.5
aiohttp==3.7.4.post0
async-timeout==3.0.1
attrs==21.2.0
base58==2.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@
},
"initial_balances": {
"2cd4da7d3b345e022ca7e997c2bb3276a4d3d2e9": "0x1027e72f1f12813088000000",
"7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000"
"7e4aa664f71de4e9d0b4a6473d796372639bdcde": "0x1027e72f1f12813088000000",
"ee1326fbf7d9322e5ea02c6fe5eb63535fceccd1": "0x1027e72f1f12813088000000"
},
"gas_price": {
"blocks": 200,
"percentile": 60,
"minimum_price" : 1
"minimum_price": 1
},
"pbft": {
"committee_size": "0x3e8",
Expand Down Expand Up @@ -100,10 +101,10 @@
"hardforks": {
"fix_redelegate_block_num": 0,
"rewards_distribution_frequency": {
"0" : 100
"0": 100
},
"magnolia_hf" : {
"block_num" : 0,
"magnolia_hf": {
"block_num": 0,
"jail_time": 163459
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ class FinalChain {
virtual void prune(EthBlockNumber blk_n) = 0;

virtual std::vector<state_api::ValidatorStake> dpos_validators_total_stakes(EthBlockNumber blk_num) const = 0;
virtual state_api::ProofResponse get_proof(EthBlockNumber blk_n, const addr_t& addr,
const std::vector<h256>& keys) const = 0;

// TODO move out of here:

std::pair<val_t, bool> getBalance(addr_t const& addr) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,17 @@ class StateAPI {
void create_snapshot(PbftPeriod period);
void prune(const std::vector<dev::h256>& state_root_to_keep, EthBlockNumber blk_num);

ProofResponse get_proof(EthBlockNumber blk_num, const addr_t& addr, const h256& state_root,
const std::vector<h256>& keys) const;

// DPOS
uint64_t dpos_eligible_total_vote_count(EthBlockNumber blk_num) const;
uint64_t dpos_eligible_vote_count(EthBlockNumber blk_num, const addr_t& addr) const;
bool dpos_is_eligible(EthBlockNumber blk_num, const addr_t& addr) const;
u256 get_staking_balance(EthBlockNumber blk_num, const addr_t& addr) const;
vrf_wrapper::vrf_pk_t dpos_get_vrf_key(EthBlockNumber blk_num, const addr_t& addr) const;
std::vector<ValidatorStake> dpos_validators_total_stakes(EthBlockNumber blk_num) const;
/** @} */
};
/** @} */

} // namespace taraxa::state_api

namespace taraxa {
using state_api::StateAPI;
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,25 @@ struct ValidatorStake {

HAS_RLP_FIELDS
};

struct StorageProof {
h256 key;
bytes value;
std::vector<bytes> proof;

HAS_RLP_FIELDS
};

struct ProofResponse {
u256 balance;
h256 code_hash;
u256 nonce;
h256 storage_hash;
std::vector<bytes> account_proof;
std::vector<StorageProof> storage_proof;

HAS_RLP_FIELDS
};

/** @} */
} // namespace taraxa::state_api
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace taraxa::final_chain {
class FinalChainImpl final : public FinalChain {
std::shared_ptr<DB> db_;
const uint64_t kBlockGasLimit;
StateAPI state_api_;
state_api::StateAPI state_api_;
const bool kLightNode = false;
const uint64_t kLightNodeHistory = 0;
const uint32_t kMaxLevelsPerPeriod;
Expand Down Expand Up @@ -451,6 +451,12 @@ class FinalChainImpl final : public FinalChain {
return state_api_.dpos_validators_total_stakes(blk_num);
}

state_api::ProofResponse get_proof(EthBlockNumber blk_num, const addr_t& addr,
const std::vector<h256>& keys) const override {
auto h = get_block_header(blk_num);
return state_api_.get_proof(blk_num, addr, h->state_root, keys);
}

private:
std::shared_ptr<TransactionHashes> get_transaction_hashes(std::optional<EthBlockNumber> n = {}) const {
const auto& trxs = db_->getPeriodTransactions(last_if_absent(n));
Expand Down
6 changes: 6 additions & 0 deletions libraries/core_libs/consensus/src/final_chain/state_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ const RewardsDistributionResult& StateAPI::distribute_rewards(const std::vector<
return result_buf_rewards_distribution_;
}

ProofResponse StateAPI::get_proof(EthBlockNumber blk_num, const addr_t& addr, const h256& state_root,
const std::vector<h256>& keys) const {
return c_method_args_rlp<ProofResponse, from_rlp, taraxa_evm_state_api_get_proof>(this_c_, blk_num, addr, state_root,
keys);
}

void StateAPI::transition_state_commit() {
ErrorHandler err_h;
taraxa_evm_state_api_transition_state_commit(this_c_, err_h.cgo_part_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ RLP_FIELDS_DEFINE(Account, nonce, balance, storage_root_hash, code_hash, code_si
RLP_FIELDS_DEFINE(StateDescriptor, blk_num, state_root)
RLP_FIELDS_DEFINE(Tracing, vmTrace, trace, stateDiff)
RLP_FIELDS_DEFINE(ValidatorStake, addr, stake)
RLP_FIELDS_DEFINE(StorageProof, key, value, proof)
RLP_FIELDS_DEFINE(ProofResponse, balance, code_hash, nonce, storage_hash, account_proof, storage_proof)

} // namespace taraxa::state_api
} // namespace taraxa::state_api
3 changes: 3 additions & 0 deletions libraries/core_libs/consensus/src/final_chain/trie_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ h256 hash256(BytesMap const& _s) {
HexMap hexMap;
for (auto i = _s.rbegin(); i != _s.rend(); ++i) hexMap[asNibbles(bytesConstRef(&i->first))] = i->second;
RLPStream s;
for (const auto& [k, v] : hexMap) {
std::cout << toHex(k) << ": " << toHex(v) << std::endl;
}
hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s);
return sha3(s.out());
}
Expand Down
13 changes: 11 additions & 2 deletions libraries/core_libs/network/rpc/Eth.jsonrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@
"params": [],
"order": [],
"returns": {}
},
{
"name": "eth_getProof",
"params": [
"",
[],
""
],
"order": [],
"returns": {}
}
]

]
13 changes: 13 additions & 0 deletions libraries/core_libs/network/rpc/EthClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ class EthClient : public jsonrpc::Client {
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}

Json::Value eth_getProof(const std::string& param1, const Json::Value& param2,
const Json::Value& param3) throw(jsonrpc::JsonRpcException) {
Json::Value p;
p.append(param1);
p.append(param2);
p.append(param3);
Json::Value result = this->CallMethod("eth_getStorageAt", p);
if (result.isString())
return result.asString();
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
};

} // namespace net
Expand Down
8 changes: 8 additions & 0 deletions libraries/core_libs/network/rpc/EthFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ class EthFace : public ServerInterface<EthFace> {
&taraxa::net::EthFace::eth_estimateGasI);
this->bindAndAddMethod(jsonrpc::Procedure("eth_chainId", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, NULL),
&taraxa::net::EthFace::eth_chainIdI);
this->bindAndAddMethod(
jsonrpc::Procedure("eth_getProof", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",
jsonrpc::JSON_STRING, "param2", jsonrpc::JSON_ARRAY, "param3", JSON_ANY, NULL),
&taraxa::net::EthFace::eth_getProofI);
}

inline virtual void eth_protocolVersionI(const Json::Value &request, Json::Value &response) {
Expand Down Expand Up @@ -229,6 +233,9 @@ class EthFace : public ServerInterface<EthFace> {
(void)request;
response = this->eth_chainId();
}
inline virtual void eth_getProofI(const Json::Value &request, Json::Value &response) {
response = this->eth_getProof(request[0u].asString(), request[1u], request[2u]);
}
virtual std::string eth_protocolVersion() = 0;
virtual std::string eth_coinbase() = 0;
virtual std::string eth_gasPrice() = 0;
Expand Down Expand Up @@ -264,6 +271,7 @@ class EthFace : public ServerInterface<EthFace> {
virtual Json::Value eth_syncing() = 0;
virtual std::string eth_estimateGas(const Json::Value &param1) = 0;
virtual Json::Value eth_chainId() = 0;
virtual Json::Value eth_getProof(const std::string &param1, const Json::Value &param2, const Json::Value &param3) = 0;
};

} // namespace net
Expand Down
47 changes: 47 additions & 0 deletions libraries/core_libs/network/rpc/eth/Eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,41 @@ Json::Value toJson(const SyncStatus& obj) {
return res;
}

Json::Value toJson(const state_api::StorageProof& obj) {
Json::Value res(Json::objectValue);
res["key"] = toJS(obj.key);
Json::Value arr(Json::arrayValue);
for (const auto& v : obj.proof) {
arr.append(toJS(v));
}
res["proof"] = arr;
res["value"] = toJS(obj.value);
return res;
}

Json::Value toJson(const state_api::ProofResponse& obj) {
Json::Value res(Json::objectValue);
res["balance"] = toJS(obj.balance);
res["codeHash"] = toJS(obj.code_hash);
res["nonce"] = toJS(obj.nonce);
res["storageHash"] = toJS(obj.storage_hash);
{
Json::Value arr(Json::arrayValue);
for (const auto& v : obj.account_proof) {
arr.append(toJS(v));
}
res["accountProof"] = arr;
}
{
Json::Value arr(Json::arrayValue);
for (const auto& v : obj.storage_proof) {
arr.append(toJson(v));
}
res["storageProof"] = arr;
}
return res;
}

class EthImpl : public Eth, EthParams {
Watches watches_;

Expand Down Expand Up @@ -292,6 +327,18 @@ class EthImpl : public Eth, EthParams {

Json::Value eth_chainId() override { return chain_id ? Json::Value(toJS(chain_id)) : Json::Value(); }

Json::Value eth_getProof(const string& _address, const Json::Value& _keys, const Json::Value& _block) override {
const auto block_number = get_block_number_from_json(_block);
std::vector<h256> keys;
keys.reserve(_keys.size());
std::transform(_keys.begin(), _keys.end(), std::back_inserter(keys),
[](const auto& k) { return jsToFixed<32>(k.asString()); });
const auto proof = final_chain->get_proof(block_number, addr_t(_address), keys);
// std::cout << proof.storage_hash << std::endl;
// return to JS(final_chain->get_account_storage(toAddress(_address), jsToU256(_position), block_number));
return toJson(proof);
}

void note_block_executed(const BlockHeader& blk_header, const SharedTransactions& trxs,
const TransactionReceipts& receipts) override {
watches_.new_blocks_.process_update(blk_header.hash);
Expand Down
2 changes: 2 additions & 0 deletions libraries/core_libs/network/rpc/eth/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Json::Value toJson(const final_chain::BlockHeader& obj);
Json::Value toJson(const LocalisedLogEntry& lle);
Json::Value toJson(const LocalisedTransactionReceipt& ltr);
Json::Value toJson(const SyncStatus& obj);
Json::Value toJson(const state_api::StorageProof& obj);
Json::Value toJson(const state_api::ProofResponse& obj);

template <typename T>
Json::Value toJson(const T& t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Transaction {
explicit InvalidFormat(const std::string &msg) : InvalidTransaction("rlp format:\n" + msg) {}
};

private:
public:
trx_nonce_t nonce_ = 0;
val_t value_ = 0;
val_t gas_price_;
Expand Down Expand Up @@ -74,6 +74,8 @@ struct Transaction {

const bytes &rlp() const;

const bytes &sig_rlp() const;

Json::Value toJSON() const;
};

Expand Down
13 changes: 13 additions & 0 deletions libraries/types/transaction/src/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ const bytes &Transaction::rlp() const {
return cached_rlp_;
}

const bytes &Transaction::sig_rlp() const {
if (!cached_rlp_set_.load()) {
std::unique_lock l(cached_rlp_mu_);
if (!cached_rlp_set_.load()) {
dev::RLPStream s;
streamRLP<true>(s);
cached_rlp_ = s.invalidate();
cached_rlp_set_ = true;
}
}
return cached_rlp_;
}

trx_hash_t Transaction::hash_for_signature() const {
dev::RLPStream s;
streamRLP<true>(s);
Expand Down

0 comments on commit 4cff9b5

Please sign in to comment.