Skip to content

Commit

Permalink
fix: state during the tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
kstdl committed Nov 27, 2024
1 parent b1c4ab2 commit 5c8c0f4
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<state_api::EVMTransaction> trx, EthBlockNumber blk_n,
std::optional<state_api::Tracing> params = {}) const;
std::string trace(std::vector<state_api::EVMTransaction> state_trxs, std::vector<state_api::EVMTransaction> trxs,
EthBlockNumber blk_n, std::optional<state_api::Tracing> params = {}) const;

/**
* @brief total count of eligible votes are in DPOS precompiled contract
Expand Down Expand Up @@ -271,9 +271,10 @@ class FinalChain {
std::vector<h256>&& finalized_dag_blk_hashes,
std::shared_ptr<DagBlock>&& anchor);

const SharedTransactions getTransactions(std::optional<EthBlockNumber> n = {}) const;

private:
std::shared_ptr<TransactionHashes> getTransactionHashes(std::optional<EthBlockNumber> n = {}) const;
const SharedTransactions getTransactions(std::optional<EthBlockNumber> n = {}) const;
std::shared_ptr<const BlockHeader> getBlockHeader(EthBlockNumber n) const;
std::optional<h256> getBlockHash(EthBlockNumber n) const;
EthBlockNumber lastIfAbsent(const std::optional<EthBlockNumber>& client_blk_n) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<EVMTransaction> trx,
std::optional<Tracing> params = {}) const;
bytes trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector<EVMTransaction>& state_trxs,
const std::vector<EVMTransaction>& trxs, std::optional<Tracing> params = {}) const;
StateDescriptor get_last_committed_state_descriptor() const;

const TransactionsExecutionResult& execute_transactions(const EVMBlock& block,
Expand Down
6 changes: 4 additions & 2 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -446,7 +447,8 @@ state_api::ExecutionResult FinalChain::call(const state_api::EVMTransaction& trx
trx);
}

std::string FinalChain::trace(std::vector<state_api::EVMTransaction> trxs, EthBlockNumber blk_n,
std::string FinalChain::trace(std::vector<state_api::EVMTransaction> state_trxs,
std::vector<state_api::EVMTransaction> trxs, EthBlockNumber blk_n,
std::optional<state_api::Tracing> params) const {
const auto blk_header = blockHeader(lastIfAbsent(blk_n));
if (!blk_header) {
Expand All @@ -459,7 +461,7 @@ std::string FinalChain::trace(std::vector<state_api::EVMTransaction> trxs, EthBl
blk_header->timestamp,
BlockHeader::difficulty(),
},
trxs, params));
state_trxs, trxs, params));
}

uint64_t FinalChain::dposEligibleTotalVoteCount(EthBlockNumber blk_num) const {
Expand Down
8 changes: 4 additions & 4 deletions libraries/core_libs/consensus/src/final_chain/state_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<EVMTransaction> trxs,
std::optional<Tracing> params) const {
return c_method_args_rlp<bytes, from_rlp, taraxa_evm_state_api_trace_transactions>(this_c_, blk_num, blk, trxs,
params);
bytes StateAPI::trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector<EVMTransaction>& state_trxs,
const std::vector<EVMTransaction>& trxs, std::optional<Tracing> params) const {
return c_method_args_rlp<bytes, from_rlp, taraxa_evm_state_api_trace_transactions>(this_c_, blk_num, blk, state_trxs,
trxs, params);
}

StateDescriptor StateAPI::get_last_committed_state_descriptor() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ bool PillarVotes::addVerifiedVote(const std::shared_ptr<PillarVote>& vote, uint6

void PillarVotes::initializePeriodData(PbftPeriod period, uint64_t threshold) {
std::scoped_lock<std::shared_mutex> lock(mutex_);
votes_.insert({period, PeriodVotes{.threshold = threshold}});
votes_.insert({period, PeriodVotes{{}, {}, threshold}});
}

void PillarVotes::eraseVotes(PbftPeriod min_period) {
Expand Down
75 changes: 41 additions & 34 deletions libraries/core_libs/network/rpc/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -43,21 +32,44 @@ 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<std::vector<state_api::EVMTransaction>, 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;
}

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;
}
Expand All @@ -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<state_api::EVMTransaction> 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<state_api::EVMTransaction> trxs = to_eth_trxs(*transactions);
return util::readJsonFromString(node->getFinalChain()->trace({}, std::move(trxs), block, std::move(params)));
}
return res;
}
Expand Down Expand Up @@ -253,6 +262,13 @@ state_api::Tracing Debug::parse_tracking_parms(const Json::Value& json) const {
return ret;
}

std::vector<state_api::EVMTransaction> Debug::to_eth_trxs(const std::vector<std::shared_ptr<Transaction>>& trxs) {
std::vector<state_api::EVMTransaction> 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<Transaction> t) const {
return state_api::EVMTransaction{
t->getSender(), t->getGasPrice(), t->getReceiver(), t->getNonce(), t->getValue(), t->getGas(), t->getData(),
Expand Down Expand Up @@ -329,13 +345,4 @@ Address Debug::to_address(const string& s) const {
throw InvalidAddress();
}

std::pair<std::shared_ptr<Transaction>, std::optional<final_chain::TransactionLocation>>
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
5 changes: 3 additions & 2 deletions libraries/core_libs/network/rpc/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ class Debug : public DebugFace {
private:
state_api::EVMTransaction to_eth_trx(std::shared_ptr<Transaction> t) const;
state_api::EVMTransaction to_eth_trx(const Json::Value& json, EthBlockNumber blk_num);
std::vector<state_api::EVMTransaction> to_eth_trxs(const std::vector<std::shared_ptr<Transaction>>& 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::shared_ptr<Transaction>, std::optional<final_chain::TransactionLocation>>
get_transaction_with_location(const std::string& transaction_hash) const;
std::tuple<std::vector<state_api::EVMTransaction>, state_api::EVMTransaction, uint64_t> get_transaction_with_state(
const std::string& transaction_hash);

std::weak_ptr<taraxa::FullNode> full_node_;
const uint64_t kGasLimit = ((uint64_t)1 << 53) - 1;
Expand Down
2 changes: 1 addition & 1 deletion submodules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5c8c0f4

Please sign in to comment.