Skip to content

Commit

Permalink
feat: add support for tracking blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Apr 28, 2023
1 parent 3c7a8ec commit 33f5baa
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ class FinalChain {
std::optional<EthBlockNumber> blk_n = {}) const = 0;

/**
* @brief Trace execution of a new message call immediately without creating a transaction on the block chain. That
* @brief Trace execution of a new message call immediately without creating a transactions on the block chain. That
* means that state would be reverted and not saved anywhere
* @param trx state_api::EVMTransaction
* @param trxs std::vector<state_api::EVMTransaction> vector of transaction to trace
* @param blk_n EthBlockNumber number of block we are getting state from
* @return std::string
*/
virtual std::string trace_trx(const state_api::EVMTransaction& trx, EthBlockNumber blk_n,
std::optional<state_api::Tracing> params = {}) const = 0;
virtual std::string trace(std::vector<state_api::EVMTransaction> trx, EthBlockNumber blk_n,
std::optional<state_api::Tracing> params = {}) const = 0;

/**
* @brief total count of eligible votes are in DPOS precompiled contract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class StateAPI {
u256 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_transaction(EthBlockNumber blk_num, const EVMBlock& blk, const EVMTransaction& trx,
std::optional<Tracing> params = {}) const;
bytes trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector<EVMTransaction> trx,
std::optional<Tracing> params = {}) const;
StateDescriptor get_last_committed_state_descriptor() const;
const StateTransitionResult& transition_state(const EVMBlock& block,
const util::RangeView<EVMTransaction>& transactions,
Expand Down
20 changes: 10 additions & 10 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,17 @@ class FinalChainImpl final : public FinalChain {
trx);
}

std::string trace_trx(const state_api::EVMTransaction& trx, EthBlockNumber blk_n,
std::optional<state_api::Tracing> params = {}) const override {
std::string trace(std::vector<state_api::EVMTransaction> trxs, EthBlockNumber blk_n,
std::optional<state_api::Tracing> params = {}) const override {
const auto blk_header = block_header(last_if_absent(blk_n));
return dev::asString(state_api_.trace_transaction(blk_header->number,
{
blk_header->author,
blk_header->gas_limit,
blk_header->timestamp,
BlockHeader::difficulty(),
},
trx, params));
return dev::asString(state_api_.trace(blk_header->number,
{
blk_header->author,
blk_header->gas_limit,
blk_header->timestamp,
BlockHeader::difficulty(),
},
trxs, params));
}

uint64_t dpos_eligible_total_vote_count(EthBlockNumber blk_num) const override {
Expand Down
10 changes: 5 additions & 5 deletions libraries/core_libs/consensus/src/final_chain/state_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ ExecutionResult StateAPI::dry_run_transaction(EthBlockNumber blk_num, const EVMB
trx);
}

bytes StateAPI::trace_transaction(EthBlockNumber blk_num, const EVMBlock& blk, const EVMTransaction& trx,
std::optional<Tracing> params) const {
bytes StateAPI::trace(EthBlockNumber blk_num, const EVMBlock& blk, const std::vector<EVMTransaction> trxs,
std::optional<Tracing> params) const {
if (params) {
return c_method_args_rlp<bytes, from_rlp, taraxa_evm_state_api_trace_transaction>(this_c_, blk_num, blk, trx,
*params);
return c_method_args_rlp<bytes, from_rlp, taraxa_evm_state_api_trace_transactions>(this_c_, blk_num, blk, trxs,
*params);
} else {
return c_method_args_rlp<bytes, from_rlp, taraxa_evm_state_api_debug_transaction>(this_c_, blk_num, blk, trx);
return c_method_args_rlp<bytes, from_rlp, taraxa_evm_state_api_debug_transactions>(this_c_, blk_num, blk, trxs);
}
}

Expand Down
33 changes: 28 additions & 5 deletions libraries/core_libs/network/rpc/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Json::Value Debug::debug_traceTransaction(const std::string& transaction_hash) {
return res;
}
if (auto node = full_node_.lock()) {
return util::readJsonFromString(node->getFinalChain()->trace_trx(to_eth_trx(std::move(trx)), loc->blk_n - 1));
return util::readJsonFromString(node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, loc->blk_n - 1));
}
} catch (std::exception& e) {
res["status"] = e.what();
Expand All @@ -35,7 +35,7 @@ Json::Value Debug::debug_traceCall(const Json::Value& call_params, const std::st
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_trx(std::move(trx), block));
return util::readJsonFromString(node->getFinalChain()->trace({std::move(trx)}, block));
}
} catch (std::exception& e) {
res["status"] = e.what();
Expand All @@ -48,10 +48,10 @@ Json::Value Debug::trace_call(const Json::Value& call_params, const Json::Value&
Json::Value res;
try {
const auto block = parse_blk_num(blk_num);
auto trx = to_eth_trx(call_params, block);
auto params = parse_tracking_parms(trace_params);
if (auto node = full_node_.lock()) {
return util::readJsonFromString(node->getFinalChain()->trace_trx(std::move(trx), block, std::move(params)));
return util::readJsonFromString(
node->getFinalChain()->trace({to_eth_trx(call_params, block)}, block, std::move(params)));
}
} catch (std::exception& e) {
res["status"] = e.what();
Expand All @@ -70,7 +70,30 @@ Json::Value Debug::trace_replayTransaction(const std::string& transaction_hash,
}
if (auto node = full_node_.lock()) {
return util::readJsonFromString(
node->getFinalChain()->trace_trx(to_eth_trx(std::move(trx)), loc->blk_n - 1, std::move(params)));
node->getFinalChain()->trace({to_eth_trx(std::move(trx))}, loc->blk_n - 1, std::move(params)));
}
} catch (std::exception& e) {
res["status"] = e.what();
}
return res;
}

Json::Value Debug::trace_replayBlockTransactions(const std::string& block_num, const Json::Value& trace_params) {
Json::Value res;
try {
const auto block = parse_blk_num(block_num);
auto params = parse_tracking_parms(trace_params);
if (auto node = full_node_.lock()) {
auto transactions = node->getDB()->getPeriodTransactions(block);
if (!transactions.has_value() || transactions->empty()) {
res["status"] = "Block has no transactions";
return res;
}
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 - 1, std::move(params)));
}
} catch (std::exception& e) {
res["status"] = e.what();
Expand Down
1 change: 1 addition & 0 deletions libraries/core_libs/network/rpc/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Debug : public DebugFace {
virtual Json::Value trace_call(const Json::Value& param1, const Json::Value& param2,
const std::string& param3) override;
virtual Json::Value trace_replayTransaction(const std::string& param1, const Json::Value& param2) override;
virtual Json::Value trace_replayBlockTransactions(const std::string& param1, const Json::Value& param2) override;

private:
state_api::EVMTransaction to_eth_trx(std::shared_ptr<Transaction> t) const;
Expand Down
9 changes: 9 additions & 0 deletions libraries/core_libs/network/rpc/Debug.jsonrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@
],
"order": [],
"returns": {}
},
{
"name": "trace_replayBlockTransactions",
"params": [
"",
[]
],
"order": [],
"returns": {}
}
]
11 changes: 11 additions & 0 deletions libraries/core_libs/network/rpc/DebugClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ class DebugClient : public jsonrpc::Client {
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value trace_replayBlockTransactions(const std::string& param1,
const Json::Value& param2) throw(jsonrpc::JsonRpcException) {
Json::Value p;
p.append(param1);
p.append(param2);
Json::Value result = this->CallMethod("trace_replayBlockTransactions", p);
if (result.isObject())
return result;
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/DebugFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class DebugFace : public ServerInterface<DebugFace> {
jsonrpc::Procedure("trace_replayTransaction", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",
jsonrpc::JSON_STRING, "param2", jsonrpc::JSON_ARRAY, NULL),
&taraxa::net::DebugFace::trace_replayTransactionI);
this->bindAndAddMethod(
jsonrpc::Procedure("trace_replayBlockTransactions", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",
jsonrpc::JSON_STRING, "param2", jsonrpc::JSON_ARRAY, NULL),
&taraxa::net::DebugFace::trace_replayBlockTransactionsI);
}

inline virtual void debug_traceTransactionI(const Json::Value& request, Json::Value& response) {
Expand All @@ -40,10 +44,14 @@ class DebugFace : public ServerInterface<DebugFace> {
inline virtual void trace_replayTransactionI(const Json::Value& request, Json::Value& response) {
response = this->trace_replayTransaction(request[0u].asString(), request[1u]);
}
inline virtual void trace_replayBlockTransactionsI(const Json::Value& request, Json::Value& response) {
response = this->trace_replayBlockTransactions(request[0u].asString(), request[1u]);
}
virtual Json::Value debug_traceTransaction(const std::string& param1) = 0;
virtual Json::Value debug_traceCall(const Json::Value& param1, const std::string& param2) = 0;
virtual Json::Value trace_call(const Json::Value& param1, const Json::Value& param2, const std::string& param3) = 0;
virtual Json::Value trace_replayTransaction(const std::string& param1, const Json::Value& param2) = 0;
virtual Json::Value trace_replayBlockTransactions(const std::string& param1, const Json::Value& param2) = 0;
};

} // namespace net
Expand Down
2 changes: 1 addition & 1 deletion submodules/taraxa-evm

0 comments on commit 33f5baa

Please sign in to comment.