Skip to content

Commit bb2568f

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

File tree

3 files changed

+71
-73
lines changed

3 files changed

+71
-73
lines changed

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)
@@ -755,74 +824,6 @@ bool is_eof_container(bytes_view container) noexcept
755824
return container.starts_with(EOF_MAGIC);
756825
}
757826

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

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;

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)