Skip to content

Commit

Permalink
WIP: support tx type 4 as setcode in RLP and statetest_loader
Browse files Browse the repository at this point in the history
  • Loading branch information
pdobacz authored and gumb0 committed Aug 8, 2024
1 parent 0cf3c95 commit a919431
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
21 changes: 19 additions & 2 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const

[[nodiscard]] bytes rlp_encode(const Transaction& tx)
{
assert(tx.type <= Transaction::Type::blob);
assert(tx.type <= Transaction::Type::set_code);

// TODO: Refactor this function. For all type of transactions most of the code is similar.
if (tx.type == Transaction::Type::legacy)
Expand Down Expand Up @@ -658,7 +658,7 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
tx.to.has_value() ? tx.to.value() : bytes_view(), tx.value, tx.data,
tx.access_list, tx.v, tx.r, tx.s);
}
else // Transaction::Type::blob
else if (tx.type == Transaction::Type::blob)
{
// tx_type +
// rlp [chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value,
Expand All @@ -669,6 +669,17 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
tx.to.has_value() ? tx.to.value() : bytes_view(), tx.value, tx.data,
tx.access_list, tx.max_blob_gas_price, tx.blob_hashes, tx.v, tx.r, tx.s);
}
else // Transaction::Type::set_code
{
// tx_type +
// rlp [chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to, value,
// data, access_list, authorization_list, sig_parity, r, s];
return bytes{0x04} + // Transaction type (set_code type == 4)
rlp::encode_tuple(tx.chain_id, tx.nonce, tx.max_priority_gas_price, 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.access_list, tx.authorization_list, tx.v, tx.r, tx.s);

Check warning on line 681 in test/state/state.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/state.cpp#L677-L681

Added lines #L677 - L681 were not covered by tests
}
}

[[nodiscard]] bytes rlp_encode(const TransactionReceipt& receipt)
Expand Down Expand Up @@ -699,6 +710,12 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
withdrawal.amount_in_gwei);
}

[[nodiscard]] bytes rlp_encode(const Authorization& authorization)

Check warning on line 713 in test/state/state.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/state.cpp#L713

Added line #L713 was not covered by tests
{
return rlp::encode_tuple(authorization.chain_id, authorization.addr, authorization.nonce,
authorization.v, authorization.r, authorization.s);

Check warning on line 716 in test/state/state.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/state.cpp#L715-L716

Added lines #L715 - L716 were not covered by tests
}

[[nodiscard]] std::string get_tests_invalid_tx_message(ErrorCode errc) noexcept
{
switch (errc)
Expand Down
9 changes: 7 additions & 2 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ struct Authorization
address signer;
intx::uint256 r;
intx::uint256 s;
bool y_parity = false;
uint8_t v = 0;
};

using AuthorizationList = std::vector<Authorization>;

struct Transaction
{
/// The type of the transaction.
Expand Down Expand Up @@ -239,7 +241,7 @@ struct Transaction
intx::uint256 r;
intx::uint256 s;
uint8_t v = 0;
std::vector<Authorization> authorization_list;
AuthorizationList authorization_list;
std::vector<bytes> initcodes;
};

Expand Down Expand Up @@ -313,6 +315,9 @@ void system_call(State& state, const BlockInfo& block, evmc_revision rev, evmc::
/// Defines how to RLP-encode a Withdrawal.
[[nodiscard]] bytes rlp_encode(const Withdrawal& withdrawal);

/// Defnies how to RLP-encode an Authorization (EIP-7702).
[[nodiscard]] bytes rlp_encode(const Authorization& authorization);

[[nodiscard]] std::string get_tests_invalid_tx_message(ErrorCode errc) noexcept;

} // namespace evmone::state
27 changes: 27 additions & 0 deletions test/statetest/statetest_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ state::AccessList from_json<state::AccessList>(const json::json& j)
return o;
}

template <>
state::AuthorizationList from_json<state::AuthorizationList>(const json::json& j)
{
state::AuthorizationList o;
for (const auto& a : j)
{
state::Authorization authorization{};
authorization.chain_id = from_json<uint64_t>(a.at("chainId"));
authorization.addr = from_json<address>(a.at("address"));
// TODO: this is current spec, but EEST still has nonce as a list, let's pick the first item
// authorization.nonce = from_json<uint64_t>(a.at("nonce"));
authorization.nonce = from_json<uint64_t>(a.at("nonce")[0]);
authorization.signer = from_json<address>(a.at("signer"));
authorization.r = from_json<intx::uint256>(a.at("r"));
authorization.s = from_json<intx::uint256>(a.at("s"));
authorization.v = from_json<uint8_t>(a.at("v"));
o.emplace_back(authorization);
}
return o;
}

// Based on calculateEIP1559BaseFee from ethereum/retesteth
inline uint64_t calculate_current_base_fee_eip1559(
uint64_t parent_gas_used, uint64_t parent_gas_limit, uint64_t parent_base_fee)
Expand Down Expand Up @@ -337,6 +358,12 @@ static void from_json_tx_common(const json::json& j, state::Transaction& o)
for (const auto& initcode : *it_initcodes)
o.initcodes.push_back(from_json<bytes>(initcode));
}
else if (const auto au_it = j.find("authorizationList"); au_it != j.end())
{
o.authorization_list = from_json<state::AuthorizationList>(*au_it);
if (o.type <= state::Transaction::Type::eip1559)
o.type = state::Transaction::Type::set_code;
}
}

template <>
Expand Down

0 comments on commit a919431

Please sign in to comment.