Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use base_fee_per_gas from when generating EVM blocks #214

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/block_conversion_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
} else {
eos_evm_version = eosevm::nonce_to_version(last_evm_block.header.nonce);
}
eosevm::prepare_block_header(new_block.header, bm.value(), evm_contract_name, last_evm_block.header.number+1, eos_evm_version);

std::optional<uint64_t> base_fee_per_gas;
if(last_evm_block.header.base_fee_per_gas.has_value()) {
base_fee_per_gas = static_cast<uint64_t>(last_evm_block.header.base_fee_per_gas.value());
}
eosevm::prepare_block_header(new_block.header, bm.value(), evm_contract_name, last_evm_block.header.number+1, eos_evm_version, base_fee_per_gas);

new_block.header.parent_hash = last_evm_block.header.hash();
new_block.header.transactions_root = silkworm::kEmptyRoot;
Expand Down Expand Up @@ -299,7 +304,7 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi

silkworm::ByteView bv = {(const uint8_t*)rlpx_ref.data(), rlpx_ref.size()};
silkworm::Transaction evm_tx;
if (!silkworm::rlp::decode(bv, evm_tx)) {
if (!silkworm::rlp::decode_transaction(bv, evm_tx, silkworm::rlp::Eip2718Wrapping::kBoth, silkworm::rlp::Leftover::kProhibit)) {
SILK_CRIT << "Failed to decode transaction in block: " << curr.header.number;
throw std::runtime_error("Failed to decode transaction");
}
Expand All @@ -318,6 +323,20 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
}
}

if(block_version >= 1) {
auto tx_base_fee = std::visit([](auto&& arg) -> auto { return arg.base_fee_per_gas; }, dtx);
if(!curr.header.base_fee_per_gas.has_value()) {
curr.header.base_fee_per_gas = tx_base_fee;
} else if (curr.header.base_fee_per_gas.value() != tx_base_fee) {
if(curr.transactions.empty()) {
curr.header.base_fee_per_gas = tx_base_fee;
} else {
SILK_CRIT << "curr.base_fee_per_gas != tx_base_fee";
throw std::runtime_error("curr.base_fee_per_gas != tx_base_fee");
}
}
}

curr.transactions.emplace_back(std::move(evm_tx));
});
set_upper_bound(curr, *new_block);
Expand Down Expand Up @@ -358,7 +377,7 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
if( na.name == pushtx_n ) {
pushtx tx;
eosio::convert_from_bin(tx, na.data);
return evmtx_type{evmtx_v0{0, std::move(tx.rlpx)}};
return evmtx_type{evmtx_v0{0, std::move(tx.rlpx), 0}};
}
evmtx_type evmtx;
eosio::convert_from_bin(evmtx, na.data);
Expand Down
3 changes: 2 additions & 1 deletion src/block_conversion_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ EOSIO_REFLECT(pushtx, miner, rlpx)
struct evmtx_v0 {
uint64_t eos_evm_version;
std::vector<uint8_t> rlpx;
uint64_t base_fee_per_gas;
};
using evmtx_type = std::variant<evmtx_v0>;
EOSIO_REFLECT(evmtx_v0, eos_evm_version, rlpx)
EOSIO_REFLECT(evmtx_v0, eos_evm_version, rlpx, base_fee_per_gas)

struct gas_parameter_type {
uint64_t gas_txnewaccount = 0;
Expand Down
37 changes: 28 additions & 9 deletions tests/nodeos_eos_evm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,13 @@ def get_block(num):
nonProdNode.transferFunds(cluster.eosioAccount, evmAcc, "111.0000 EOS", "0xB106D2C286183FFC3D1F0C4A6f0753bB20B407c2", waitForTransBlock=True)
time.sleep(2)

Utils.Print("Verify evm_version==1")
Utils.Print("Verify evm_version==1 and base_fe_per_gas")
# Verify header.nonce == 1 (evmversion=1)
# Verify header.baseFeePerGas == 10GWei (0x2540be400)
b = get_block("latest")

assert(b["nonce"] == "0x0000000000000001")
assert(b["baseFeePerGas"] == "0x2540be400")

Utils.Print("Transfer funds to trigger evmtx event on contract")
# Transfer funds (now using version=1)
Expand All @@ -883,14 +886,18 @@ def get_block(num):

b = get_block("latest")
Utils.Print("get_block_latest: " + json.dumps(b))
# 'consensusParameter': {'gasFeeParameters': {'gasCodedeposit': 118, 'gasNewaccount': 40946, 'gasSset': 43728, 'gasTxcreate': 71508, 'gasTxnewaccount': 40946}}
# "consensusParameter": {"gasFeeParameters": {"gasCodedeposit": 106, "gasNewaccount": 36782, "gasSset": 39576, "gasTxcreate": 64236, "gasTxnewaccount": 36782}

assert("consensusParameter" in b)
assert(b["consensusParameter"]["gasFeeParameters"]["gasCodedeposit"] == 118)
assert(b["consensusParameter"]["gasFeeParameters"]["gasNewaccount"] == 40946)
assert(b["consensusParameter"]["gasFeeParameters"]["gasSset"] == 43728)
assert(b["consensusParameter"]["gasFeeParameters"]["gasTxcreate"] == 71508)
assert(b["consensusParameter"]["gasFeeParameters"]["gasTxnewaccount"] == 40946)
assert(b["consensusParameter"]["gasFeeParameters"]["gasCodedeposit"] == 106)
assert(b["consensusParameter"]["gasFeeParameters"]["gasNewaccount"] == 36782)
assert(b["consensusParameter"]["gasFeeParameters"]["gasSset"] == 39576)
assert(b["consensusParameter"]["gasFeeParameters"]["gasTxcreate"] == 64236)
assert(b["consensusParameter"]["gasFeeParameters"]["gasTxnewaccount"] == 36782)

# Verify header.baseFeePerGas still 10GWei (0x2540be400) it will change in 3mins
b = get_block("latest")
assert(b["baseFeePerGas"] == "0x2540be400")

# EVM -> EOS
# 0x9E126C57330FA71556628e0aabd6B6B6783d99fA private key: 0xba8c9ff38e4179748925335a9891b969214b37dc3723a1754b8b849d3eea9ac0
Expand All @@ -917,8 +924,20 @@ def get_block(num):
Utils.Print("\taccount row4: ", row4)
bal2 = w3.eth.get_balance(Web3.to_checksum_address("0x9E126C57330FA71556628e0aabd6B6B6783d99fA"))

# balance different = 1.0 EOS (val) + 900(Gwei) (21000(base gas) + 40946 (gas for non-exist account) )
assert(bal1 == bal2 + 1000000000000000000 + 900000000000 * (21000 + 40946))
# balance different = 1.0 EOS (val) + 900(Gwei) (21000(base gas) + 36782 (gas for non-exist account) )
assert(bal1 == bal2 + 1000000000000000000 + 900000000000 * (21000 + 36782))

# Wait 3 mins
Utils.Print("Wait 3 mins")
time.sleep(180)

# Trigger change in base_fee_per_gas
nonProdNode.transferFunds(cluster.eosioAccount, evmAcc, "1.0000 EOS", "0xB106D2C286183FFC3D1F0C4A6f0753bB20B407c2", waitForTransBlock=True)
time.sleep(2)

# Verify header.baseFeePerGas is now 900GWei (0xd18c2e2800)
b = get_block("latest")
assert(b["baseFeePerGas"] == "0xd18c2e2800")

Utils.Print("Validate all balances (check evmtx event processing)")
# Validate all balances (check evmtx event)
Expand Down
Loading