Skip to content

Commit

Permalink
improve transaction gas price handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Feb 28, 2024
1 parent 2271ad3 commit 21c7a9e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
7 changes: 4 additions & 3 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,9 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
if (tx.type == Transaction::Type::legacy)
{
// rlp [nonce, gas_price, gas_limit, to, value, data, v, r, s];
return rlp::encode_tuple(tx.nonce, tx.max_gas_price, static_cast<uint64_t>(tx.gas_limit),
tx.to.has_value() ? tx.to.value() : bytes_view(), tx.value, tx.data, tx.v, tx.r, tx.s);
return rlp::encode_tuple(tx.nonce, tx.get_legacy_gas_price(),
static_cast<uint64_t>(tx.gas_limit), tx.to.has_value() ? tx.to.value() : bytes_view(),
tx.value, tx.data, tx.v, tx.r, tx.s);
}
else if (tx.type == Transaction::Type::access_list)
{
Expand All @@ -530,7 +531,7 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
// tx_type +
// rlp [nonce, gas_price, gas_limit, to, value, data, access_list, v, r, s];
return bytes{0x01} + // Transaction type (eip2930 type == 1)
rlp::encode_tuple(tx.chain_id, tx.nonce, tx.max_gas_price,
rlp::encode_tuple(tx.chain_id, tx.nonce, tx.get_legacy_gas_price(),
static_cast<uint64_t>(tx.gas_limit),
tx.to.has_value() ? tx.to.value() : bytes_view(), tx.value, tx.data,
tx.access_list, static_cast<bool>(tx.v), tx.r, tx.s);
Expand Down
15 changes: 14 additions & 1 deletion test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,25 @@ struct Transaction
};

/// Returns amount of blob gas used by this transaction
[[nodiscard]] int64_t blob_gas_used() const
[[nodiscard]] int64_t blob_gas_used() const noexcept
{
static constexpr auto GAS_PER_BLOB = 0x20000;
return GAS_PER_BLOB * static_cast<int64_t>(blob_hashes.size());
}

/// FIXME: add docs
void set_legacy_gas_price(const intx::uint256 gas_price) noexcept
{
max_gas_price = gas_price;
max_priority_gas_price = gas_price;
}

[[nodiscard]] const intx::uint256& get_legacy_gas_price() const noexcept
{
assert(max_gas_price == max_priority_gas_price);
return max_gas_price;
}

Type type = Type::legacy;
bytes data;
int64_t gas_limit;
Expand Down
3 changes: 1 addition & 2 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ static void from_json_tx_common(const json::json& j, state::Transaction& o)
if (const auto gas_price_it = j.find("gasPrice"); gas_price_it != j.end())
{
o.type = state::Transaction::Type::legacy;
o.max_gas_price = from_json<intx::uint256>(*gas_price_it);
o.max_priority_gas_price = o.max_gas_price;
o.set_legacy_gas_price(from_json<intx::uint256>(*gas_price_it));
if (j.contains("maxFeePerGas") || j.contains("maxPriorityFeePerGas"))
{
throw std::invalid_argument(
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/state_transition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ void state_transition::export_state_test(const TransactionReceipt& receipt, cons
jtx["nonce"] = hex0x(tx.nonce);
if (rev < EVMC_LONDON)
{
assert(tx.max_gas_price == tx.max_priority_gas_price);
jtx["gasPrice"] = hex0x(tx.max_gas_price);
// Export as legacy transaction.
jtx["gasPrice"] = hex0x(tx.get_legacy_gas_price());
}
else
{
Expand Down
16 changes: 9 additions & 7 deletions test/unittests/state_transition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ class state_transition : public ExportableFixture
.coinbase = Coinbase,
.base_fee = 999,
};
Transaction tx{
.gas_limit = block.gas_limit,
.max_gas_price = block.base_fee + 1,
.max_priority_gas_price = block.base_fee + 1,
.sender = Sender,
.nonce = 1,
};
Transaction tx = [this]() {
Transaction t{
.gas_limit = block.gas_limit,
.sender = Sender,
.nonce = 1,
};
t.set_legacy_gas_price(block.base_fee + 1);
return t;
}();
State pre;
Expectation expect;

Expand Down

0 comments on commit 21c7a9e

Please sign in to comment.