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

Allow EOF creation transactions #878

Merged
merged 9 commits into from
May 31, 2024
138 changes: 70 additions & 68 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <span>
#include <stack>
#include <unordered_set>
#include <variant>
#include <vector>

namespace evmone
Expand Down Expand Up @@ -203,13 +202,10 @@ std::variant<EOFSectionHeaders, EOFValidationError> validate_section_headers(byt
std::accumulate(section_headers[CONTAINER_SECTION].begin(),
section_headers[CONTAINER_SECTION].end(), 0);
const auto remaining_container_size = container_end - it;
// Only data section may be truncated, so remaining_container size must be in
// [declared_size_without_data, declared_size_without_data + declared_data_size]
// Only data section may be truncated, so remaining_container size must be at least
// declared_size_without_data
if (remaining_container_size < section_bodies_without_data)
return EOFValidationError::invalid_section_bodies_size;
if (remaining_container_size >
section_bodies_without_data + section_headers[DATA_SECTION].front())
return EOFValidationError::invalid_section_bodies_size;

if (section_headers[TYPE_SECTION][0] != section_headers[CODE_SECTION].size() * 4)
return EOFValidationError::invalid_type_section_size;
Expand Down Expand Up @@ -250,68 +246,6 @@ std::variant<std::vector<EOFCodeType>, EOFValidationError> validate_types(
return types;
}

std::variant<EOF1Header, EOFValidationError> validate_header(
evmc_revision rev, bytes_view container) noexcept
{
if (!is_eof_container(container))
return EOFValidationError::invalid_prefix;

const auto version = get_eof_version(container);
if (version != 1)
return EOFValidationError::eof_version_unknown;

if (rev < EVMC_PRAGUE)
return EOFValidationError::eof_version_unknown;

const auto section_headers_or_error = validate_section_headers(container);
if (const auto* error = std::get_if<EOFValidationError>(&section_headers_or_error))
return *error;

const auto& section_headers = std::get<EOFSectionHeaders>(section_headers_or_error);
const auto& code_sizes = section_headers[CODE_SECTION];
const auto data_size = section_headers[DATA_SECTION][0];

const auto header_size = eof_header_size(section_headers);

const auto types_or_error =
validate_types(container, header_size, section_headers[TYPE_SECTION].front());
if (const auto* error = std::get_if<EOFValidationError>(&types_or_error))
return *error;
const auto& types = std::get<std::vector<EOFCodeType>>(types_or_error);

std::vector<uint16_t> code_offsets;
const auto type_section_size = section_headers[TYPE_SECTION][0];
auto offset = header_size + type_section_size;
for (const auto code_size : code_sizes)
{
assert(offset <= std::numeric_limits<uint16_t>::max());
code_offsets.emplace_back(static_cast<uint16_t>(offset));
offset += code_size;
}

const auto& container_sizes = section_headers[CONTAINER_SECTION];
std::vector<uint16_t> container_offsets;
for (const auto container_size : container_sizes)
{
container_offsets.emplace_back(static_cast<uint16_t>(offset));
offset += container_size;
}
// NOTE: assertion always satisfied only as long as initcode limits apply (48K).
assert(offset <= std::numeric_limits<uint16_t>::max());
const auto data_offset = static_cast<uint16_t>(offset);

return EOF1Header{
.version = container[2],
.code_sizes = code_sizes,
.code_offsets = code_offsets,
.data_size = data_size,
.data_offset = data_offset,
.container_sizes = container_sizes,
.container_offsets = container_offsets,
.types = types,
};
}

/// Result of validating instructions in a code section.
struct InstructionValidationResult
{
Expand Down Expand Up @@ -662,6 +596,9 @@ EOFValidationError validate_eof1(evmc_revision rev, bytes_view main_container) n

auto& header = std::get<EOF1Header>(error_or_header);

if (container.size() > static_cast<size_t>(header.data_offset) + header.data_size)
return EOFValidationError::invalid_section_bodies_size;

// Validate code sections
std::vector<bool> visited_code_sections(header.code_sizes.size());
std::queue<uint16_t> code_sections_queue({0});
Expand Down Expand Up @@ -753,6 +690,68 @@ bool is_eof_container(bytes_view container) noexcept
return container.size() > 1 && container[0] == MAGIC[0] && container[1] == MAGIC[1];
}

std::variant<EOF1Header, EOFValidationError> validate_header(
evmc_revision rev, bytes_view container) noexcept
{
if (!is_eof_container(container))
return EOFValidationError::invalid_prefix;

const auto version = get_eof_version(container);
if (version != 1)
return EOFValidationError::eof_version_unknown;

if (rev < EVMC_PRAGUE)
return EOFValidationError::eof_version_unknown;

const auto section_headers_or_error = validate_section_headers(container);
if (const auto* error = std::get_if<EOFValidationError>(&section_headers_or_error))
return *error;

const auto& section_headers = std::get<EOFSectionHeaders>(section_headers_or_error);
const auto& code_sizes = section_headers[CODE_SECTION];
const auto data_size = section_headers[DATA_SECTION][0];

const auto header_size = eof_header_size(section_headers);

const auto types_or_error =
validate_types(container, header_size, section_headers[TYPE_SECTION].front());
if (const auto* error = std::get_if<EOFValidationError>(&types_or_error))
return *error;
const auto& types = std::get<std::vector<EOFCodeType>>(types_or_error);

std::vector<uint16_t> code_offsets;
const auto type_section_size = section_headers[TYPE_SECTION][0];
auto offset = header_size + type_section_size;
for (const auto code_size : code_sizes)
{
assert(offset <= std::numeric_limits<uint16_t>::max());
code_offsets.emplace_back(static_cast<uint16_t>(offset));
offset += code_size;
}

const auto& container_sizes = section_headers[CONTAINER_SECTION];
std::vector<uint16_t> container_offsets;
for (const auto container_size : container_sizes)
{
container_offsets.emplace_back(static_cast<uint16_t>(offset));
offset += container_size;
}
// NOTE: assertion always satisfied only as long as initcode limits apply (48K).
assert(offset <= std::numeric_limits<uint16_t>::max());
const auto data_offset = static_cast<uint16_t>(offset);

return EOF1Header{
.version = container[2],
.code_sizes = code_sizes,
.code_offsets = code_offsets,
.data_size = data_size,
.data_offset = data_offset,
.container_sizes = container_sizes,
.container_offsets = container_offsets,
.types = types,
};
}

/// This function expects the prefix and version to be valid, as it ignores it.
EOF1Header read_valid_eof1_header(bytes_view container)
{
Expand Down Expand Up @@ -820,6 +819,9 @@ bool append_data_section(bytes& container, bytes_view aux_data)
{
const auto header = read_valid_eof1_header(container);

// Assert we don't need to trim off the bytes beyond the declared data section first.
assert(container.size() <= header.data_offset + header.data_size);

const auto new_data_size = container.size() - header.data_offset + aux_data.size();
if (new_data_size > std::numeric_limits<uint16_t>::max())
return false;
Expand Down
7 changes: 6 additions & 1 deletion lib/evmone/eof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstddef>
#include <cstdint>
#include <string>
#include <variant>
#include <vector>

namespace evmone
Expand Down Expand Up @@ -71,7 +72,7 @@ struct EOF1Header
[[nodiscard]] bool can_init(size_t container_size) const noexcept
{
// Containers with truncated data section cannot be initcontainers.
const auto truncated_data = static_cast<size_t>(data_offset + data_size) != container_size;
const auto truncated_data = static_cast<size_t>(data_offset + data_size) > container_size;
return !truncated_data;
}

Expand Down Expand Up @@ -145,6 +146,10 @@ enum class EOFValidationError
/// If the prefix is missing or invalid, 0 is returned meaning legacy code.
[[nodiscard]] uint8_t get_eof_version(bytes_view container) noexcept;

/// Validates the header and returns its representation if successful.
[[nodiscard]] EVMC_EXPORT std::variant<EOF1Header, EOFValidationError> validate_header(
evmc_revision rev, bytes_view container) noexcept;

/// Validates whether given container is a valid EOF according to the rules of given revision.
[[nodiscard]] EVMC_EXPORT EOFValidationError validate_eof(
evmc_revision rev, bytes_view container) noexcept;
Expand Down
3 changes: 0 additions & 3 deletions test/state/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ enum ErrorCode : int
EMPTY_BLOB_HASHES_LIST,
INVALID_BLOB_HASH_VERSION,
BLOB_GAS_LIMIT_EXCEEDED,
EOF_CREATION_TRANSACTION,
UNKNOWN_ERROR,
};

Expand Down Expand Up @@ -83,8 +82,6 @@ inline const std::error_category& evmone_category() noexcept
return "invalid blob hash version";
case BLOB_GAS_LIMIT_EXCEEDED:
return "blob gas limit exceeded";
case EOF_CREATION_TRANSACTION:
return "EOF initcode in creation transaction";
case UNKNOWN_ERROR:
return "Unknown error";
default:
Expand Down
45 changes: 41 additions & 4 deletions test/state/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,46 @@ std::optional<evmc_message> Host::prepare_message(evmc_message msg)
else
{
assert(msg.kind == EVMC_EOFCREATE);
const bytes_view initcontainer{msg.code, msg.code_size};
msg.recipient =
compute_eofcreate_address(msg.sender, msg.create2_salt, initcontainer);
const bytes_view input = {msg.input_data, msg.input_size};

// Indicator of an EOF creation tx - EVMC_EOFCREATE at depth 0.
pdobacz marked this conversation as resolved.
Show resolved Hide resolved
if (msg.depth == 0)
{
// Assert this is a legacy EOF creation tx.
assert(is_eof_container(input));

const auto header_or_error = validate_header(m_rev, input);
if (holds_alternative<EOFValidationError>(header_or_error))
return {}; // Light early exception.

const auto header = std::get<EOF1Header>(header_or_error);
pdobacz marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

@gumb0 gumb0 May 29, 2024

Choose a reason for hiding this comment

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

So std::get is actually throwing, I guess that's why the warning.

  1. The reference shouldn't affect this I think, so would be good to report to clang-tidy if it's possible to reproduce on some minimal example
  2. To work around we can use get_if
                   const auto* header = std::get_if<EOF1Header>(&header_or_error);
                    if (!header)
                        return {};  // Light early exception.
  1. I think it makes sense to add noexcept to prepare_message()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But in validate_eof1 a similar pattern is OK, which is what throws me off completely. Thanks for the suggestions, I'll poke around this


if (!header.can_init(msg.input_size))
return {}; // Light early exception.

const auto container_size =
static_cast<size_t>(header.data_offset + header.data_size);
// Follows from the header.can_init condition above.
assert(container_size <= msg.input_size);

msg.code = msg.input_data;
msg.code_size = container_size;
msg.input_data = msg.input_data + container_size;
msg.input_size = msg.input_size - container_size;

if (validate_eof(m_rev, {msg.code, msg.code_size}) !=
pdobacz marked this conversation as resolved.
Show resolved Hide resolved
EOFValidationError::success)
return {}; // Light early exception.

msg.recipient = compute_create_address(msg.sender, sender_nonce);
}
// EOFCREATE or TXCREATE
else
{
const bytes_view initcontainer{msg.code, msg.code_size};
msg.recipient =
compute_eofcreate_address(msg.sender, msg.create2_salt, initcontainer);
}
}

// By EIP-2929, the access to new created address is never reverted.
Expand Down Expand Up @@ -327,7 +364,7 @@ evmc::Result Host::create(const evmc_message& msg) noexcept
{
if (m_rev >= EVMC_PRAGUE)
{
// Only EOFCREATE/TXCREATE is allowed to deploy code starting with EF.
// Only EOFCREATE/TXCREATE/EOF-creation-tx is allowed to deploy code starting with EF.
// It must be valid EOF, which was validated before execution.
if (msg.kind != EVMC_EOFCREATE)
return evmc::Result{EVMC_CONTRACT_VALIDATION_FAILURE};
Expand Down
24 changes: 13 additions & 11 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,27 @@ int64_t compute_tx_intrinsic_cost(evmc_revision rev, const Transaction& tx) noex
static constexpr auto call_tx_cost = 21000;
static constexpr auto create_tx_cost = 53000;
static constexpr auto initcode_word_cost = 2;
const auto is_create = !tx.to.has_value();
const auto is_create = !tx.to.has_value(); // Covers also EOF creation txs.
const auto initcode_cost =
is_create && rev >= EVMC_SHANGHAI ? initcode_word_cost * num_words(tx.data.size()) : 0;
const auto tx_cost = is_create && rev >= EVMC_HOMESTEAD ? create_tx_cost : call_tx_cost;
return tx_cost + compute_tx_data_cost(rev, tx.data) + compute_access_list_cost(tx.access_list) +
compute_initcode_list_cost(rev, tx.initcodes) + initcode_cost;
}

evmc_message build_message(const Transaction& tx, int64_t execution_gas_limit) noexcept
evmc_message build_message(
const Transaction& tx, int64_t execution_gas_limit, evmc_revision rev) noexcept
{
const auto recipient = tx.to.has_value() ? *tx.to : evmc::address{};
return {tx.to.has_value() ? EVMC_CALL : EVMC_CREATE, 0, 0, execution_gas_limit, recipient,
tx.sender, tx.data.data(), tx.data.size(), intx::be::store<evmc::uint256be>(tx.value), {},
recipient, nullptr, 0};

const auto is_legacy_eof_create =
rev >= EVMC_PRAGUE && !tx.to.has_value() && is_eof_container(tx.data);

return {is_legacy_eof_create ? EVMC_EOFCREATE :
pdobacz marked this conversation as resolved.
Show resolved Hide resolved
tx.to.has_value() ? EVMC_CALL :
EVMC_CREATE,
0, 0, execution_gas_limit, recipient, tx.sender, tx.data.data(), tx.data.size(),
intx::be::store<evmc::uint256be>(tx.value), {}, recipient, nullptr, 0};
}
} // namespace

Expand Down Expand Up @@ -326,9 +333,6 @@ std::variant<int64_t, std::error_code> validate_transaction(const Account& sende
if (rev >= EVMC_SHANGHAI && !tx.to.has_value() && tx.data.size() > max_initcode_size)
return make_error_code(INIT_CODE_SIZE_LIMIT_EXCEEDED);

if (rev >= EVMC_PRAGUE && !tx.to.has_value() && is_eof_container(tx.data))
return make_error_code(EOF_CREATION_TRANSACTION);

// Compute and check if sender has enough balance for the theoretical maximum transaction cost.
// Note this is different from tx_max_cost computed with effective gas price later.
// The computation cannot overflow if done with 512-bit precision.
Expand Down Expand Up @@ -485,7 +489,7 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
if (rev >= EVMC_SHANGHAI)
host.access_account(block.coinbase);

const auto result = host.call(build_message(tx, execution_gas_limit));
const auto result = host.call(build_message(tx, execution_gas_limit, rev));

auto gas_used = tx.gas_limit - result.gas_left;

Expand Down Expand Up @@ -652,8 +656,6 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
return "TR_BLOBVERSION_INVALID";
case BLOB_GAS_LIMIT_EXCEEDED:
return "TR_BLOBLIST_OVERSIZE";
case EOF_CREATION_TRANSACTION:
return "TR_EOFCreationTransaction";
case UNKNOWN_ERROR:
return "Unknown error";
default:
Expand Down
40 changes: 40 additions & 0 deletions test/unittests/eof_example_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,46 @@ TEST_F(state_transition, eof_examples_callf)
expect.post[*tx.to].exists = true;
}

TEST_F(state_transition, eof_examples_creation_tx)
{
// # Example 4
//
// A creation transaction used to create a new EOF contract.

rev = EVMC_PRAGUE;

const auto initcontainer = bytecode(
//////////////////
// Initcontainer
// Code section: PUSH0 [aux data size], PUSH0 [aux data offset] and
// RETURNCONTRACT first subcontainer
// |
// Header: 1 code section 4 bytes long |
// | |
// version | Header terminator
// | |___________ | |________
"EF00 01 01 0004 02 0001 0004 03 0001 0014 04 0000 00 00 80 0002 5F5F EE00"
// |‾‾‾‾‾‾ |‾‾‾‾‾‾‾‾‾‾‾ |‾‾‾‾‾‾ |‾‾‾‾‾‾‾‾‾
// | | Header: data section 0 bytes long
// | | |
// Header: types section 4 bytes long Types section: first code section
// | 0 inputs, non-returning,
// | max stack height 2
// Header: 1 subcontainer 20 bytes long
//
//////////////////
// Deployed container (contract doing nothing, see Example 1)
"EF00 01 01 0004 02 0001 0001 04 0000 00 00 80 0000 00");

// Put the initcontainer in the `data` field of the transaction, appending some calldata.
tx.data = initcontainer + "ABCDEF";
// Empty `to` field.
tx.to = std::nullopt;

// Address of the newly created contract is calculated using the deployer's address and nonce.
expect.post[0x3442a1dec1e72f337007125aa67221498cdd759d_address].exists = true;
}

TEST_F(state_transition, eof_examples_eofcreate)
{
// # Example 5
Expand Down
Loading