Skip to content

Commit c0014e1

Browse files
committed
eof: Make validate_header() function private
No need to call from EOF Creation Transaction processing anymore.
1 parent 9e61fe1 commit c0014e1

File tree

3 files changed

+71
-73
lines changed

3 files changed

+71
-73
lines changed

Diff for: lib/evmone/eof.cpp

+69-68
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <numeric>
1717
#include <queue>
1818
#include <unordered_set>
19+
#include <variant>
1920
#include <vector>
2021

2122
namespace evmone
@@ -235,6 +236,74 @@ std::variant<EOFSectionHeaders, EOFValidationError> validate_section_headers(byt
235236
return section_headers;
236237
}
237238

239+
std::variant<EOF1Header, EOFValidationError> validate_header(
240+
evmc_revision rev, bytes_view container) noexcept
241+
{
242+
if (!is_eof_container(container))
243+
return EOFValidationError::invalid_prefix;
244+
245+
const auto version = get_eof_version(container);
246+
if (version != 1)
247+
return EOFValidationError::eof_version_unknown;
248+
249+
if (rev < EVMC_OSAKA)
250+
return EOFValidationError::eof_version_unknown;
251+
252+
// `offset` variable handled below is known to not be greater than the container size, as
253+
// checked in `validate_section_headers`. Combined with the requirement for the container
254+
// size to not exceed MAX_INITCODE_SIZE (checked before `validate-header` is called),
255+
// this allows us to cast `offset` to narrower integers.
256+
assert(container.size() <= MAX_INITCODE_SIZE);
257+
258+
auto section_headers_or_error = validate_section_headers(container);
259+
if (const auto* error = std::get_if<EOFValidationError>(&section_headers_or_error))
260+
return *error;
261+
262+
auto& section_headers = std::get<EOFSectionHeaders>(section_headers_or_error);
263+
264+
const auto header_size = eof_header_size(section_headers);
265+
266+
const auto type_section_offset = header_size;
267+
268+
if (section_headers.type_size !=
269+
section_headers.code_sizes.size() * EOF1Header::TYPE_ENTRY_SIZE)
270+
return EOFValidationError::invalid_type_section_size;
271+
272+
auto offset = header_size + section_headers.type_size;
273+
274+
std::vector<uint16_t> code_offsets;
275+
code_offsets.reserve(section_headers.code_sizes.size());
276+
for (const auto code_size : section_headers.code_sizes)
277+
{
278+
assert(offset <= std::numeric_limits<uint16_t>::max());
279+
code_offsets.emplace_back(static_cast<uint16_t>(offset));
280+
offset += code_size;
281+
}
282+
283+
std::vector<uint32_t> container_offsets;
284+
container_offsets.reserve(section_headers.container_sizes.size());
285+
for (const auto container_size : section_headers.container_sizes)
286+
{
287+
assert(offset <= std::numeric_limits<uint32_t>::max());
288+
container_offsets.emplace_back(static_cast<uint32_t>(offset));
289+
offset += container_size;
290+
}
291+
292+
assert(offset <= std::numeric_limits<uint32_t>::max());
293+
const auto data_offset = static_cast<uint32_t>(offset);
294+
295+
return EOF1Header{
296+
.version = container[2],
297+
.type_section_offset = type_section_offset,
298+
.code_sizes = std::move(section_headers.code_sizes),
299+
.code_offsets = std::move(code_offsets),
300+
.data_size = section_headers.data_size,
301+
.data_offset = data_offset,
302+
.container_sizes = std::move(section_headers.container_sizes),
303+
.container_offsets = std::move(container_offsets),
304+
};
305+
}
306+
238307
EOFValidationError validate_types(bytes_view container, const EOF1Header& header) noexcept
239308
{
240309
for (size_t i = 0; i < header.get_type_count(); ++i)
@@ -753,74 +822,6 @@ bool is_eof_container(bytes_view container) noexcept
753822
return container.starts_with(EOF_MAGIC);
754823
}
755824

756-
std::variant<EOF1Header, EOFValidationError> validate_header(
757-
evmc_revision rev, bytes_view container) noexcept
758-
{
759-
if (!is_eof_container(container))
760-
return EOFValidationError::invalid_prefix;
761-
762-
const auto version = get_eof_version(container);
763-
if (version != 1)
764-
return EOFValidationError::eof_version_unknown;
765-
766-
if (rev < EVMC_OSAKA)
767-
return EOFValidationError::eof_version_unknown;
768-
769-
// `offset` variable handled below is known to not be greater than the container size, as
770-
// checked in `validate_section_headers`. Combined with the requirement for the container
771-
// size to not exceed MAX_INITCODE_SIZE (checked before `validate-header` is called),
772-
// this allows us to cast `offset` to narrower integers.
773-
assert(container.size() <= MAX_INITCODE_SIZE);
774-
775-
auto section_headers_or_error = validate_section_headers(container);
776-
if (const auto* error = std::get_if<EOFValidationError>(&section_headers_or_error))
777-
return *error;
778-
779-
auto& section_headers = std::get<EOFSectionHeaders>(section_headers_or_error);
780-
781-
const auto header_size = eof_header_size(section_headers);
782-
783-
const auto type_section_offset = header_size;
784-
785-
if (section_headers.type_size !=
786-
section_headers.code_sizes.size() * EOF1Header::TYPE_ENTRY_SIZE)
787-
return EOFValidationError::invalid_type_section_size;
788-
789-
auto offset = header_size + section_headers.type_size;
790-
791-
std::vector<uint16_t> code_offsets;
792-
code_offsets.reserve(section_headers.code_sizes.size());
793-
for (const auto code_size : section_headers.code_sizes)
794-
{
795-
assert(offset <= std::numeric_limits<uint16_t>::max());
796-
code_offsets.emplace_back(static_cast<uint16_t>(offset));
797-
offset += code_size;
798-
}
799-
800-
std::vector<uint32_t> container_offsets;
801-
container_offsets.reserve(section_headers.container_sizes.size());
802-
for (const auto container_size : section_headers.container_sizes)
803-
{
804-
assert(offset <= std::numeric_limits<uint32_t>::max());
805-
container_offsets.emplace_back(static_cast<uint32_t>(offset));
806-
offset += container_size;
807-
}
808-
809-
assert(offset <= std::numeric_limits<uint32_t>::max());
810-
const auto data_offset = static_cast<uint32_t>(offset);
811-
812-
return EOF1Header{
813-
.version = container[2],
814-
.type_section_offset = type_section_offset,
815-
.code_sizes = std::move(section_headers.code_sizes),
816-
.code_offsets = std::move(code_offsets),
817-
.data_size = section_headers.data_size,
818-
.data_offset = data_offset,
819-
.container_sizes = std::move(section_headers.container_sizes),
820-
.container_offsets = std::move(container_offsets),
821-
};
822-
}
823-
824825
/// This function expects the prefix and version to be valid, as it ignores it.
825826
EOF1Header read_valid_eof1_header(bytes_view container)
826827
{

Diff for: lib/evmone/eof.hpp

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <cstddef>
1111
#include <cstdint>
1212
#include <string_view>
13-
#include <variant>
1413
#include <vector>
1514

1615
namespace evmone
@@ -213,10 +212,6 @@ enum class ContainerKind : uint8_t
213212
/// If the prefix is missing or invalid, 0 is returned meaning legacy code.
214213
[[nodiscard]] uint8_t get_eof_version(bytes_view container) noexcept;
215214

216-
/// Validates the header and returns its representation if successful.
217-
[[nodiscard]] EVMC_EXPORT std::variant<EOF1Header, EOFValidationError> validate_header(
218-
evmc_revision rev, bytes_view container) noexcept;
219-
220215
/// Validates whether given container is a valid EOF according to the rules of given revision.
221216
[[nodiscard]] EVMC_EXPORT EOFValidationError validate_eof(
222217
evmc_revision rev, ContainerKind kind, bytes_view container) noexcept;

Diff for: lib/evmone/instructions_calls.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "eof.hpp"
77
#include "instructions.hpp"
88

9+
#include <variant>
10+
911
constexpr int64_t MIN_RETAINED_GAS = 5000;
1012
constexpr int64_t MIN_CALLEE_GAS = 2300;
1113
constexpr int64_t CALL_VALUE_COST = 9000;

0 commit comments

Comments
 (0)