From 5c8c0f4a6fda6b91bc9da577ac29089a09b9268e Mon Sep 17 00:00:00 2001 From: kstdl Date: Fri, 22 Nov 2024 11:17:15 +0100 Subject: [PATCH] fix: state during the tracing --- .../include/final_chain/final_chain.hpp | 7 +- .../include/final_chain/state_api.hpp | 4 +- .../consensus/src/final_chain/final_chain.cpp | 6 +- .../consensus/src/final_chain/state_api.cpp | 8 +- .../src/pillar_chain/pillar_votes.cpp | 2 +- libraries/core_libs/network/rpc/Debug.cpp | 75 ++++++++++--------- libraries/core_libs/network/rpc/Debug.h | 5 +- submodules/CMakeLists.txt | 2 +- submodules/taraxa-evm | 2 +- 9 files changed, 61 insertions(+), 50 deletions(-) diff --git a/libraries/core_libs/consensus/include/final_chain/final_chain.hpp b/libraries/core_libs/consensus/include/final_chain/final_chain.hpp index 57d08d142d..a98c7c705f 100644 --- a/libraries/core_libs/consensus/include/final_chain/final_chain.hpp +++ b/libraries/core_libs/consensus/include/final_chain/final_chain.hpp @@ -186,8 +186,8 @@ class FinalChain { * @param blk_n EthBlockNumber number of block we are getting state from * @return std::string */ - std::string trace(std::vector trx, EthBlockNumber blk_n, - std::optional params = {}) const; + std::string trace(std::vector state_trxs, std::vector trxs, + EthBlockNumber blk_n, std::optional params = {}) const; /** * @brief total count of eligible votes are in DPOS precompiled contract @@ -271,9 +271,10 @@ class FinalChain { std::vector&& finalized_dag_blk_hashes, std::shared_ptr&& anchor); + const SharedTransactions getTransactions(std::optional n = {}) const; + private: std::shared_ptr getTransactionHashes(std::optional n = {}) const; - const SharedTransactions getTransactions(std::optional n = {}) const; std::shared_ptr getBlockHeader(EthBlockNumber n) const; std::optional getBlockHash(EthBlockNumber n) const; EthBlockNumber lastIfAbsent(const std::optional& client_blk_n) const; diff --git a/libraries/core_libs/consensus/include/final_chain/state_api.hpp b/libraries/core_libs/consensus/include/final_chain/state_api.hpp index cffffad3f0..291fce35af 100644 --- a/libraries/core_libs/consensus/include/final_chain/state_api.hpp +++ b/libraries/core_libs/consensus/include/final_chain/state_api.hpp @@ -42,8 +42,8 @@ class StateAPI { h256 get_account_storage(EthBlockNumber blk_num, const addr_t& addr, const u256& key) const; bytes get_code_by_address(EthBlockNumber blk_num, const addr_t& addr) const; ExecutionResult dry_run_transaction(EthBlockNumber blk_num, const EVMBlock& blk, const EVMTransaction& trx) const; - bytes trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector trx, - std::optional params = {}) const; + bytes trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector& state_trxs, + const std::vector& trxs, std::optional params = {}) const; StateDescriptor get_last_committed_state_descriptor() const; const TransactionsExecutionResult& execute_transactions(const EVMBlock& block, diff --git a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp index 19f5ea47a9..1c07801b9f 100644 --- a/libraries/core_libs/consensus/src/final_chain/final_chain.cpp +++ b/libraries/core_libs/consensus/src/final_chain/final_chain.cpp @@ -2,6 +2,7 @@ #include "common/encoding_solidity.hpp" #include "common/util.hpp" +#include "final_chain/state_api_data.hpp" #include "final_chain/trie_common.hpp" #include "pbft/pbft_block.hpp" #include "transaction/system_transaction.hpp" @@ -446,7 +447,8 @@ state_api::ExecutionResult FinalChain::call(const state_api::EVMTransaction& trx trx); } -std::string FinalChain::trace(std::vector trxs, EthBlockNumber blk_n, +std::string FinalChain::trace(std::vector state_trxs, + std::vector trxs, EthBlockNumber blk_n, std::optional params) const { const auto blk_header = blockHeader(lastIfAbsent(blk_n)); if (!blk_header) { @@ -459,7 +461,7 @@ std::string FinalChain::trace(std::vector trxs, EthBl blk_header->timestamp, BlockHeader::difficulty(), }, - trxs, params)); + state_trxs, trxs, params)); } uint64_t FinalChain::dposEligibleTotalVoteCount(EthBlockNumber blk_num) const { diff --git a/libraries/core_libs/consensus/src/final_chain/state_api.cpp b/libraries/core_libs/consensus/src/final_chain/state_api.cpp index 9366ae901b..1ed4ea86f4 100644 --- a/libraries/core_libs/consensus/src/final_chain/state_api.cpp +++ b/libraries/core_libs/consensus/src/final_chain/state_api.cpp @@ -164,10 +164,10 @@ ExecutionResult StateAPI::dry_run_transaction(EthBlockNumber blk_num, const EVMB trx); } -bytes StateAPI::trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector trxs, - std::optional params) const { - return c_method_args_rlp(this_c_, blk_num, blk, trxs, - params); +bytes StateAPI::trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector& state_trxs, + const std::vector& trxs, std::optional params) const { + return c_method_args_rlp(this_c_, blk_num, blk, state_trxs, + trxs, params); } StateDescriptor StateAPI::get_last_committed_state_descriptor() const { diff --git a/libraries/core_libs/consensus/src/pillar_chain/pillar_votes.cpp b/libraries/core_libs/consensus/src/pillar_chain/pillar_votes.cpp index a127eba448..4d2bed6a4b 100644 --- a/libraries/core_libs/consensus/src/pillar_chain/pillar_votes.cpp +++ b/libraries/core_libs/consensus/src/pillar_chain/pillar_votes.cpp @@ -132,7 +132,7 @@ bool PillarVotes::addVerifiedVote(const std::shared_ptr& vote, uint6 void PillarVotes::initializePeriodData(PbftPeriod period, uint64_t threshold) { std::scoped_lock lock(mutex_); - votes_.insert({period, PeriodVotes{.threshold = threshold}}); + votes_.insert({period, PeriodVotes{{}, {}, threshold}}); } void PillarVotes::eraseVotes(PbftPeriod min_period) { diff --git a/libraries/core_libs/network/rpc/Debug.cpp b/libraries/core_libs/network/rpc/Debug.cpp index 1f4c158f6e..e62990b409 100644 --- a/libraries/core_libs/network/rpc/Debug.cpp +++ b/libraries/core_libs/network/rpc/Debug.cpp @@ -6,6 +6,7 @@ #include "common/jsoncpp.hpp" #include "final_chain/state_api_data.hpp" #include "network/rpc/eth/data.hpp" +#include "transaction/transaction.hpp" using namespace std; using namespace dev; @@ -14,24 +15,12 @@ using namespace taraxa; namespace taraxa::net { -Json::Value Debug::debug_traceTransaction(const std::string& transaction_hash) { - Json::Value res; - auto [trx, loc] = get_transaction_with_location(transaction_hash); - if (!trx || !loc) { - throw std::runtime_error("Transaction not found"); - } - if (auto node = full_node_.lock()) { - return util::readJsonFromString(node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, loc->period)); - } - return res; -} - Json::Value Debug::debug_traceCall(const Json::Value& call_params, const std::string& blk_num) { Json::Value res; const auto block = parse_blk_num(blk_num); auto trx = to_eth_trx(call_params, block); if (auto node = full_node_.lock()) { - return util::readJsonFromString(node->getFinalChain()->trace({std::move(trx)}, block)); + return util::readJsonFromString(node->getFinalChain()->trace({}, {std::move(trx)}, block)); } return res; } @@ -43,7 +32,34 @@ Json::Value Debug::trace_call(const Json::Value& call_params, const Json::Value& auto params = parse_tracking_parms(trace_params); if (auto node = full_node_.lock()) { return util::readJsonFromString( - node->getFinalChain()->trace({to_eth_trx(call_params, block)}, block, std::move(params))); + node->getFinalChain()->trace({}, {to_eth_trx(call_params, block)}, block, std::move(params))); + } + return res; +} + +std::tuple, state_api::EVMTransaction, uint64_t> +Debug::get_transaction_with_state(const std::string& transaction_hash) { + auto node = full_node_.lock(); + if (!node) { + return {}; + } + const auto hash = jsToFixed<32>(transaction_hash); + + auto loc = node->getFinalChain()->transactionLocation(hash); + if (!loc) { + throw std::runtime_error("Transaction not found"); + } + auto block_transactions = node->getFinalChain()->getTransactions(loc->period); + + auto state_trxs = SharedTransactions(block_transactions.begin(), block_transactions.begin() + loc->position); + + return {to_eth_trxs(state_trxs), to_eth_trx(block_transactions[loc->position]), loc->period}; +} +Json::Value Debug::debug_traceTransaction(const std::string& transaction_hash) { + Json::Value res; + auto [state_trxs, trx, period] = get_transaction_with_state(transaction_hash); + if (auto node = full_node_.lock()) { + return util::readJsonFromString(node->getFinalChain()->trace({}, {trx}, period)); } return res; } @@ -51,13 +67,9 @@ Json::Value Debug::trace_call(const Json::Value& call_params, const Json::Value& Json::Value Debug::trace_replayTransaction(const std::string& transaction_hash, const Json::Value& trace_params) { Json::Value res; auto params = parse_tracking_parms(trace_params); - auto [trx, loc] = get_transaction_with_location(transaction_hash); - if (!trx || !loc) { - throw std::runtime_error("Transaction not found"); - } + auto [state_trxs, trx, period] = get_transaction_with_state(transaction_hash); if (auto node = full_node_.lock()) { - return util::readJsonFromString( - node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, loc->period, std::move(params))); + return util::readJsonFromString(node->getFinalChain()->trace(state_trxs, {trx}, period, params)); } return res; } @@ -71,11 +83,8 @@ Json::Value Debug::trace_replayBlockTransactions(const std::string& block_num, c if (!transactions.has_value() || transactions->empty()) { return Json::Value(Json::arrayValue); } - std::vector trxs; - trxs.reserve(transactions->size()); - std::transform(transactions->begin(), transactions->end(), std::back_inserter(trxs), - [this](auto t) { return to_eth_trx(std::move(t)); }); - return util::readJsonFromString(node->getFinalChain()->trace(std::move(trxs), block, std::move(params))); + std::vector trxs = to_eth_trxs(*transactions); + return util::readJsonFromString(node->getFinalChain()->trace({}, std::move(trxs), block, std::move(params))); } return res; } @@ -253,6 +262,13 @@ state_api::Tracing Debug::parse_tracking_parms(const Json::Value& json) const { return ret; } +std::vector Debug::to_eth_trxs(const std::vector>& trxs) { + std::vector eth_trxs; + eth_trxs.reserve(trxs.size()); + std::transform(trxs.begin(), trxs.end(), std::back_inserter(eth_trxs), + [this](auto t) { return to_eth_trx(std::move(t)); }); + return eth_trxs; +} state_api::EVMTransaction Debug::to_eth_trx(std::shared_ptr t) const { return state_api::EVMTransaction{ t->getSender(), t->getGasPrice(), t->getReceiver(), t->getNonce(), t->getValue(), t->getGas(), t->getData(), @@ -329,13 +345,4 @@ Address Debug::to_address(const string& s) const { throw InvalidAddress(); } -std::pair, std::optional> -Debug::get_transaction_with_location(const std::string& transaction_hash) const { - if (auto node = full_node_.lock()) { - const auto hash = jsToFixed<32>(transaction_hash); - return {node->getDB()->getTransaction(hash), node->getFinalChain()->transactionLocation(hash)}; - } - return {}; -} - } // namespace taraxa::net \ No newline at end of file diff --git a/libraries/core_libs/network/rpc/Debug.h b/libraries/core_libs/network/rpc/Debug.h index 7d897e7fe5..29cfc1d2ac 100644 --- a/libraries/core_libs/network/rpc/Debug.h +++ b/libraries/core_libs/network/rpc/Debug.h @@ -47,11 +47,12 @@ class Debug : public DebugFace { private: state_api::EVMTransaction to_eth_trx(std::shared_ptr t) const; state_api::EVMTransaction to_eth_trx(const Json::Value& json, EthBlockNumber blk_num); + std::vector to_eth_trxs(const std::vector>& trxs); EthBlockNumber parse_blk_num(const string& blk_num_str); state_api::Tracing parse_tracking_parms(const Json::Value& json) const; Address to_address(const string& s) const; - std::pair, std::optional> - get_transaction_with_location(const std::string& transaction_hash) const; + std::tuple, state_api::EVMTransaction, uint64_t> get_transaction_with_state( + const std::string& transaction_hash); std::weak_ptr full_node_; const uint64_t kGasLimit = ((uint64_t)1 << 53) - 1; diff --git a/submodules/CMakeLists.txt b/submodules/CMakeLists.txt index 4363497eb8..ad5b9bced0 100644 --- a/submodules/CMakeLists.txt +++ b/submodules/CMakeLists.txt @@ -117,7 +117,7 @@ set(EVM_AFTER_BUILD_COMMAND ${EVM_AFTER_BUILD_COMMAND} && mv ${EVM_BUILD_DIR}/li ## final command set(EVM_LIBRARY_COMMAND ${EVM_BUILD_COMMAND} && ${EVM_AFTER_BUILD_COMMAND}) -file(GLOB_RECURSE TARAXA_EVM_SOURCES "taraxa-evm/*.go" ) +file(GLOB_RECURSE TARAXA_EVM_SOURCES CONFIGURE_DEPENDS "taraxa-evm/*.go" ) list(APPEND TARAXA_EVM_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/taraxa-evm/taraxa/C/common.h ${CMAKE_CURRENT_SOURCE_DIR}/taraxa-evm/taraxa/C/state.h) diff --git a/submodules/taraxa-evm b/submodules/taraxa-evm index 870992b61c..4a41887d13 160000 --- a/submodules/taraxa-evm +++ b/submodules/taraxa-evm @@ -1 +1 @@ -Subproject commit 870992b61c7b183941622180880120c9662d9c14 +Subproject commit 4a41887d131a5841c165d981889348bcd6e96ac4