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

evm: refactor bailout option handling #2668

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 11 additions & 7 deletions silkworm/core/execution/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ CallResult ExecutionProcessor::call(const Transaction& txn, const std::vector<st

SILKWORM_ASSERT(protocol::validate_call_precheck(txn, evm_) == ValidationResult::kOk);

if (!evm().bailout) {
SILKWORM_ASSERT(protocol::validate_call_funds(txn, evm_, state_.get_balance(*txn.sender())) == ValidationResult::kOk);
}

const BlockHeader& header{evm_.block().header};
const intx::uint256 base_fee_per_gas{header.base_fee_per_gas.value_or(0)};

Expand All @@ -284,10 +280,18 @@ CallResult ExecutionProcessor::call(const Transaction& txn, const std::vector<st
state_.set_nonce(*sender, state_.get_nonce(*txn.sender()) + 1);
}

if (!evm().bailout) {
const intx::uint256 required_funds = protocol::compute_call_cost(txn, effective_gas_price, evm_);
state_.subtract_from_balance(*txn.sender(), required_funds);
intx::uint256 required_funds{0};
if (evm().bailout) {
required_funds = protocol::compute_call_required_funds(txn, effective_gas_price, evm_);
// If the bailout option is on, add the required funds to the sender's balance
// so that after the transaction costs are deducted, the sender's balance is unchanged.
state_.add_to_balance(*txn.sender(), required_funds);
} else {
required_funds = protocol::compute_call_cost(txn, effective_gas_price, evm_);
}

SILKWORM_ASSERT(protocol::validate_call_funds(txn, evm_, state_.get_balance(*txn.sender())) == ValidationResult::kOk);
state_.subtract_from_balance(*txn.sender(), required_funds);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand why we are subtracting different values depending on the bailout on/off. This still feels quite a hack.

const intx::uint128 g0{protocol::intrinsic_gas(txn, evm_.revision())};
const auto result = evm_.execute(txn, txn.gas_limit - static_cast<uint64_t>(g0));

Expand Down
19 changes: 13 additions & 6 deletions silkworm/core/protocol/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,8 @@
const intx::uint256 effective_gas_price{txn.max_fee_per_gas >= evm.block().header.base_fee_per_gas ? txn.effective_gas_price(base_fee)
: txn.max_priority_fee_per_gas};

const auto required_funds = compute_call_cost(txn, effective_gas_price, evm);
intx::uint512 maximum_cost = required_funds;
if (txn.type != TransactionType::kLegacy && txn.type != TransactionType::kAccessList) {
maximum_cost = txn.maximum_gas_cost();
}
if (owned_funds < maximum_cost + txn.value) {
const auto required_funds = compute_call_required_funds(txn, effective_gas_price, evm);
if (owned_funds < required_funds) {
return ValidationResult::kInsufficientFunds;
}
return ValidationResult::kOk;
Expand All @@ -258,6 +254,17 @@
return required_funds;
}

intx::uint256 compute_call_required_funds(const Transaction& txn, const intx::uint256& effective_gas_price, const EVM& evm) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we at least need good documentation what this function is for and why it is different from compute_call_cost().

auto required_funds = compute_call_cost(txn, effective_gas_price, evm);

intx::uint512 maximum_cost = required_funds;
if (txn.type != TransactionType::kLegacy && txn.type != TransactionType::kAccessList) {
maximum_cost = txn.maximum_gas_cost();
}

Check warning on line 263 in silkworm/core/protocol/validation.cpp

View check run for this annotation

Codecov / codecov/patch

silkworm/core/protocol/validation.cpp#L262-L263

Added lines #L262 - L263 were not covered by tests

return static_cast<intx::uint256>(maximum_cost) + txn.value;
}

intx::uint256 expected_base_fee_per_gas(const BlockHeader& parent) {
if (!parent.base_fee_per_gas) {
return kInitialBaseFee;
Expand Down
2 changes: 2 additions & 0 deletions silkworm/core/protocol/validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ namespace protocol {

intx::uint256 compute_call_cost(const Transaction& txn, const intx::uint256& effective_gas_price, const EVM& evm);

intx::uint256 compute_call_required_funds(const Transaction& txn, const intx::uint256& effective_gas_price, const EVM& evm);

//! \see EIP-1559: Fee market change for ETH 1.0 chain
intx::uint256 expected_base_fee_per_gas(const BlockHeader& parent);

Expand Down
Loading