Skip to content

Commit

Permalink
Merge pull request #296 from eosnetworkfoundation/GH-271-only-one
Browse files Browse the repository at this point in the history
Use ExecutionProcessor of silkworm directly instead of copy/pasting
  • Loading branch information
elmato committed Feb 8, 2023
2 parents 5d9a9cf + a2ce23b commit 85bdf2b
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 324 deletions.
2 changes: 2 additions & 0 deletions contract/include/evm_runtime/evm_contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ CONTRACT evm_contract : public contract {
check( _config.exists(), "contract not initialized" );
check( _config.get().version == 0u, "unsupported configuration singleton" );
}

void push_trx(eosio::name ram_payer, silkworm::Block& block, const bytes& rlptx);
};


Expand Down
69 changes: 0 additions & 69 deletions contract/include/evm_runtime/processor.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion contract/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ list(APPEND SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/state.cpp
${CMAKE_CURRENT_SOURCE_DIR}/utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/actions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/processor.cpp
)
if (WITH_TEST_ACTIONS)
add_compile_definitions(WITH_TEST_ACTIONS)
Expand Down Expand Up @@ -47,6 +46,7 @@ list(APPEND SOURCES
list(APPEND SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/core/silkworm/common/util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/core/silkworm/common/endian.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/core/silkworm/consensus/engine.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/core/silkworm/execution/evm.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/third_party/silkpre/lib/silkpre/precompile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/core/silkworm/execution/analysis_cache.cpp
Expand Down
89 changes: 59 additions & 30 deletions contract/src/actions.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#define NDEBUG 1 // make sure assert is no-op in processor.cpp

#include <eosio/system.hpp>
#include <eosio/transaction.hpp>
#include <evm_runtime/evm_contract.hpp>
#include <evm_runtime/tables.hpp>
#include <evm_runtime/processor.hpp>
#include <evm_runtime/state.hpp>
#include <evm_runtime/engine.hpp>
#include <evm_runtime/intrinsics.hpp>
#include <evm_runtime/eosio.token.hpp>

// included here so NDEBUG is defined to disable assert macro
#include <silkworm/execution/processor.cpp>

#ifdef WITH_TEST_ACTIONS
#include <evm_runtime/test/engine.hpp>
#endif
Expand All @@ -18,6 +22,13 @@
#define LOGTIME(MSG)
#endif

namespace silkworm {
// provide no-op bloom
Bloom logs_bloom(const std::vector<Log>& logs) {
return {};
}
}

static constexpr eosio::name token_account("eosio.token"_n);
static constexpr eosio::symbol token_symbol("EOS", 4u);

Expand All @@ -38,21 +49,31 @@ void evm_contract::init(const uint64_t chainid) {
}, get_self());
}

void evm_contract::pushtx( eosio::name ram_payer, const bytes& rlptx ) {
assert_inited();
void check_result( ValidationResult r, const Transaction& txn, const char* desc ) {
if( r == ValidationResult::kOk )
return;

LOGTIME("EVM START");
eosio::require_auth(ram_payer);
if( r == ValidationResult::kMissingSender ) {
eosio::print("txn.from.has_value is empty\n");
} else if ( r == ValidationResult::kSenderNoEOA ) {
eosio::print("get_code_hash is empty\n");
} else if ( r == ValidationResult::kWrongNonce ) {
eosio::print("invalid nonce:", txn.nonce, "\n");
} else if ( r == ValidationResult::kInsufficientFunds ) {
eosio::print("get_balance of from insufficient\n");
} else if ( r == ValidationResult::kBlockGasLimitExceeded ) {
eosio::print("available_gas\n");
}

eosio::print( "ERR: ", uint64_t(r), "\n" );
eosio::check( false, desc );
}

void evm_contract::push_trx( eosio::name ram_payer, Block& block, const bytes& rlptx ) {

std::optional<std::pair<const std::string, const ChainConfig*>> found_chain_config = lookup_known_chain(_config.get().chainid);
check( found_chain_config.has_value(), "failed to find expected chain config" );

Block block;
block.header.difficulty = 1;
block.header.gas_limit = 0x7ffffffffff;
block.header.timestamp = eosio::current_time_point().sec_since_epoch();
block.header.number = 1 + (block.header.timestamp - _config.get().genesis_time.sec_since_epoch()); // same logic with block_mapping in TrustEVM

Transaction tx;
ByteView bv{(const uint8_t*)rlptx.data(), rlptx.size()};
eosio::check(rlp::decode(bv,tx) == DecodingResult::kOk && bv.empty(), "unable to decode transaction");
Expand All @@ -65,14 +86,38 @@ void evm_contract::pushtx( eosio::name ram_payer, const bytes& rlptx ) {

evm_runtime::engine engine;
evm_runtime::state state{get_self(), ram_payer};
evm_runtime::ExecutionProcessor ep{block, engine, state, *found_chain_config->second};
silkworm::ExecutionProcessor ep{block, engine, state, *found_chain_config->second};

ValidationResult r = consensus::pre_validate_transaction(tx, ep.evm().block().header.number, ep.evm().config(),
ep.evm().block().header.base_fee_per_gas);
check_result( r, tx, "pre_validate_transaction error" );
r = ep.validate_transaction(tx);
check_result( r, tx, "validate_transaction error" );

Receipt receipt;
ep.execute_transaction(tx, receipt);


engine.finalize(ep.state(), ep.evm().block(), ep.evm().revision());
ep.state().write_to_db(ep.evm().block().header.number);

LOGTIME("EVM EXECUTE");
}

void evm_contract::pushtx( eosio::name ram_payer, const bytes& rlptx ) {
assert_inited();

LOGTIME("EVM START");
eosio::require_auth(ram_payer);

Block block;
block.header.difficulty = 1;
block.header.gas_limit = 0x7ffffffffff;
block.header.timestamp = eosio::current_time_point().sec_since_epoch();
block.header.number = 1 + (block.header.timestamp - _config.get().genesis_time.sec_since_epoch()); // same logic with block_mapping in TrustEVM

push_trx( ram_payer, block, rlptx );
}

void evm_contract::open(eosio::name owner, eosio::name ram_payer) {
assert_inited();
require_auth(ram_payer);
Expand Down Expand Up @@ -144,26 +189,10 @@ ACTION evm_contract::testtx( const bytes& rlptx, const evm_runtime::test::block_

eosio::require_auth(get_self());

std::optional<std::pair<const std::string, const ChainConfig*>> found_chain_config = lookup_known_chain(_config.get().chainid);
check( found_chain_config.has_value(), "failed to find expected chain config" );

Block block;
block.header = bi.get_block_header();

Transaction tx;
ByteView bv{(const uint8_t *)rlptx.data(), rlptx.size()};
eosio::check(rlp::decode(bv,tx) == DecodingResult::kOk && bv.empty(), "unable to decode transaction");

tx.from.reset();
tx.recover_sender();
eosio::check(tx.from.has_value(), "unable to recover sender");

evm_runtime::test::engine engine;
evm_runtime::state state{get_self(), get_self()};
evm_runtime::ExecutionProcessor ep{block, engine, state, *found_chain_config->second};

Receipt receipt;
ep.execute_transaction(tx, receipt);
push_trx( get_self(), block, rlptx );
}

ACTION evm_contract::dumpstorage(const bytes& addy) {
Expand Down
Loading

0 comments on commit 85bdf2b

Please sign in to comment.