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

Refactor subcontainer validation #888

Merged
merged 5 commits into from
May 14, 2024
Merged

Refactor subcontainer validation #888

merged 5 commits into from
May 14, 2024

Conversation

gumb0
Copy link
Member

@gumb0 gumb0 commented May 10, 2024

Refactoring pulled out of #876

Main idea:

  1. Instruction validation returns the list of references to subcontainers (instruction, subcontainer_idx)
    1.1. (Not directly related, but I moved returning accessed section list into returned InstructionValidationResult struct instead of non-const reference argument)
  2. The check for EOFCREATE with truncated data is moved out of instruction validation into being done once for a referenced container, after it is already validated.
  3. This way we don't need to split subcontainer header validation and instructions validation and the loop over containers gets simpler.

Then it gets handy for additional container kind validation in #876.

@gumb0 gumb0 added the EOF label May 10, 2024
@gumb0 gumb0 force-pushed the validate-instr-refactor branch from 80df8c9 to 0e48680 Compare May 10, 2024 15:20
Copy link

codecov bot commented May 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.50%. Comparing base (af17b74) to head (dc1d74c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #888      +/-   ##
==========================================
- Coverage   98.50%   98.50%   -0.01%     
==========================================
  Files         130      130              
  Lines       15621    15625       +4     
==========================================
+ Hits        15388    15391       +3     
- Misses        233      234       +1     
Flag Coverage Δ
ethereum-tests 27.96% <100.00%> (+<0.01%) ⬆️
ethereum-tests-silkpre 19.70% <0.00%> (+<0.01%) ⬆️
execution-spec-tests 19.06% <0.00%> (-0.01%) ⬇️
unittests 94.35% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
lib/evmone/eof.cpp 87.31% <100.00%> (-0.10%) ⬇️

/// Pairs of (container_index, opcode) of all opcodes referencing subcontainers in this section.
std::vector<std::pair<uint8_t, Opcode>> subcontainer_references;
/// Set of accessed code section indices.
std::unordered_set<uint16_t> accessed_code_sections;
Copy link
Member Author

Choose a reason for hiding this comment

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

this can be a vector, if we're cautios of sets

Copy link
Member

Choose a reason for hiding this comment

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

This may be a performance issue. But it is fine for now, just leave an issue or TODO comment.

@gumb0
Copy link
Member Author

gumb0 commented May 13, 2024

I think we could optimize out "list of references to subcontainers" if we rely more on non-const reference arguments to instruction validation, but I think this version looks cleaner, I would start with it at least.

@gumb0 gumb0 requested review from hugo-dc, chfast, pdobacz and rodiazet May 13, 2024 06:48
Copy link
Contributor

@pdobacz pdobacz left a comment

Choose a reason for hiding this comment

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

Looks great. I was wondering whether it impacts #878 but it seems it doesn't. And you're correct it supersedes #887

// Queue of (container, header) pairs left to process
std::queue<std::pair<bytes_view, EOF1Header>> container_queue;
container_queue.emplace(main_container, main_container_header);
struct Container
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a more specific name here, like "ValidatedContainer" or "ContainerValidationInfo"?

@gumb0 gumb0 mentioned this pull request May 13, 2024
2 tasks
@gumb0 gumb0 force-pushed the validate-instr-refactor branch from 0e48680 to e406e19 Compare May 13, 2024 12:56
/// Pairs of (container_index, opcode) of all opcodes referencing subcontainers in this section.
std::vector<std::pair<uint8_t, Opcode>> subcontainer_references;
/// Set of accessed code section indices.
std::unordered_set<uint16_t> accessed_code_sections;
Copy link
Member

Choose a reason for hiding this comment

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

This may be a performance issue. But it is fine for now, just leave an issue or TODO comment.

const auto& [accessed_code_sections] =
std::get<InstructionValidationResult>(instr_validation_result_or_error);
for (const auto section_id : accessed_code_sections)
code_sections_queue.push(section_id);
Copy link
Member

Choose a reason for hiding this comment

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

C++23 has .push_range(). You can add TODO(C++23) comment.

@@ -310,8 +310,12 @@ std::variant<EOF1Header, EOFValidationError> validate_header(
};
}

/// Result of validating instructions in a code section.
Copy link
Member

Choose a reason for hiding this comment

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

pedantic: should be in the first commit, but fine to leave it as it.

std::get<InstructionValidationResult>(instr_validation_result_or_error);

// Mark what instructions referenced which subcontainers.
for (const auto& [index, opcode] : subcontainer_references)
Copy link
Member

Choose a reason for hiding this comment

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

Can be std::ranges::any_of()?

Copy link
Member Author

@gumb0 gumb0 May 14, 2024

Choose a reason for hiding this comment

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

Not really, can be std::for_each() or std::ranges::for_each(), but maybe not much point.

bool referenced_by_eofcreate = false;
};
// Queue of containers left to process
std::queue<ContainerValidation> container_queue;
container_queue.emplace(main_container, main_container_header, false);
container_queue.push({main_container, false});
Copy link
Member

Choose a reason for hiding this comment

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

Why emplacepush is needed?

container_queue.emplace(std::move(subcontainer));
container_queue.emplace(subcontainer, std::move(subcont_header),
subcontainer_referenced_by_eofcreate[subcont_idx],
subcontainer_referenced_by_returncontract[subcont_idx]);
Copy link
Member

Choose a reason for hiding this comment

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

This fields doesn't exist in ContainerValidation.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@gumb0 gumb0 force-pushed the validate-instr-refactor branch from e406e19 to dc1d74c Compare May 14, 2024 08:55
@gumb0 gumb0 merged commit 7ad882c into master May 14, 2024
23 of 24 checks passed
@gumb0 gumb0 deleted the validate-instr-refactor branch May 14, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants