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

fix(l1): fork_id validation #1729

Merged
merged 19 commits into from
Jan 21, 2025
Merged

fix(l1): fork_id validation #1729

merged 19 commits into from
Jan 21, 2025

Conversation

avilagaston9
Copy link
Contributor

@avilagaston9 avilagaston9 commented Jan 16, 2025

Motivation

We are currently checking whether the received fork ID is exactly the same as ours. Instead, we should follow the validation rules specified in https://eips.ethereum.org/EIPS/eip-2124.

Description

This PR adds a proper validation to the received fork id.

Closes #1685

@avilagaston9 avilagaston9 requested a review from a team as a code owner January 16, 2025 16:19
@avilagaston9 avilagaston9 self-assigned this Jan 16, 2025
@avilagaston9 avilagaston9 marked this pull request as draft January 16, 2025 16:20
@avilagaston9 avilagaston9 marked this pull request as ready for review January 17, 2025 01:03
Copy link

github-actions bot commented Jan 17, 2025

| File                                                                      | Lines | Diff |
+---------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/common/types/fork_id.rs            | 413   | +303 |
+---------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/common/types/genesis.rs            | 361   | +6   |
+---------------------------------------------------------------------------+-------+------+
| /home/runner/work/ethrex/ethrex/crates/networking/p2p/rlpx/eth/backend.rs | 108   | +9   |
+---------------------------------------------------------------------------+-------+------+

Total lines added: +318
Total lines removed: 0
Total lines changed: 318

// decide if our head is block or timestamp based.
let mut head = head_timestamp;
if let Some(last_block_number_based_fork) = block_number_based_forks.last() {
if *last_block_number_based_fork > latest_block_number {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this condition. The idea is that if we are on a fork that is block-number based we should use the block number, but this condition does not ensure we are on a block-based fork.
For example if we were on genesis on a homestead block with block number 0 and homestead block 0 with no other forks we would be on a block-based fork but the condition would be false. I think we could check that there are no timestamp-based forks <= to our timestamp instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in c7f4ef8!

Copy link
Collaborator

@Arkenan Arkenan left a comment

Choose a reason for hiding this comment

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

I made comments but they're mostly nitty details. The only thing that I think is important is what Fede already commented on the head being timestamp vs block based.

// See https://eips.ethereum.org/EIPS/eip-2124#validation-rules.
pub fn is_valid(
&self,
incoming: Self,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe we can call this one remote instead so it matches the eip.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in d8f81cb.

let mut hasher = Hasher::new();
hasher.update(genesis_hash.as_bytes());
for activation in forks {
if activation != last_activation {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: A comment here with "skip forks with repeated block numbers or timestamps" would help.

Optionally, de-nesting into the following might help too, but that one might be personal taste instead of good reason:

if activation == last_activation {
  continue;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed! Addressed in d8f81cb.

}
}

fn get_all_fork_id_combinations(forks: Vec<u64>, genesis_hash: BlockHash) -> Vec<(H32, u64)> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Definitely not for this PR, but we should probably pre-compute/cache this and have it available in all connections without recomputing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #1761!

crates/common/types/fork_id.rs Outdated Show resolved Hide resolved
crates/common/types/fork_id.rs Outdated Show resolved Hide resolved
crates/common/types/fork_id.rs Outdated Show resolved Hide resolved
];

assert_test_cases(test_cases, genesis.config, genesis_hash);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add some non-happy test cases? We have, for instance, the cases in the spec. We can create some arbitrary A,B and C fork hashes and do the combinations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Added in e037d4a!

@avilagaston9 avilagaston9 requested a review from Arkenan January 21, 2025 00:37
@avilagaston9 avilagaston9 requested a review from fmoletta January 21, 2025 00:37
@Arkenan Arkenan added this pull request to the merge queue Jan 21, 2025
Merged via the queue into main with commit fc50771 Jan 21, 2025
19 checks passed
@Arkenan Arkenan deleted the l1/fix/validate_fork_id branch January 21, 2025 15:00
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.

bug(l1): wrong logic when validating peer ForkID
3 participants