-
Notifications
You must be signed in to change notification settings - Fork 8
fix: change BlocksByRoot container de/serialization #63
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0844428
chore: bump leanSpec to use latest fixtures
MegaRedHand 9766f79
fix: change BlocksByRoot request de/serialization
MegaRedHand 448b1a2
refactor: clean up
MegaRedHand 6809715
Revert "chore: bump leanSpec to use latest fixtures"
MegaRedHand e029a2a
Merge branch 'main' into change-blocksbyroot-container
MegaRedHand 3975a1b
chore: fmt
MegaRedHand 163f315
chore: revert to old encoding for now
MegaRedHand a0bcbb7
Revert "chore: revert to old encoding for now"
MegaRedHand f79a17e
fix: support both SSZ formats
MegaRedHand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| use ethlambda_types::{block::SignedBlockWithAttestation, primitives::H256, state::Checkpoint}; | ||
| use ssz::Decode as SszDecode; | ||
| use ssz_derive::{Decode, Encode}; | ||
| use ssz_types::typenum; | ||
|
|
||
|
|
@@ -46,4 +47,61 @@ pub struct Status { | |
|
|
||
| type MaxRequestBlocks = typenum::U1024; | ||
|
|
||
| pub type BlocksByRootRequest = ssz_types::VariableList<H256, MaxRequestBlocks>; | ||
| pub type RequestedBlockRoots = ssz_types::VariableList<H256, MaxRequestBlocks>; | ||
|
|
||
| #[derive(Debug, Clone, Encode, Decode)] | ||
| pub struct BlocksByRootRequest { | ||
| pub roots: RequestedBlockRoots, | ||
| } | ||
|
|
||
| impl BlocksByRootRequest { | ||
| /// Decode from SSZ bytes with backward compatibility. | ||
| /// | ||
| /// Tries to decode as new format (container with `roots` field) first. | ||
| /// Falls back to old format (transparent - direct list of roots) if that fails. | ||
| pub fn from_ssz_bytes_compat(bytes: &[u8]) -> Result<Self, ssz::DecodeError> { | ||
| // Try new format (container) first | ||
| SszDecode::from_ssz_bytes(bytes).or_else(|_| { | ||
| // Fall back to old format (transparent/direct list) | ||
| SszDecode::from_ssz_bytes(bytes).map(|roots| Self { roots }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ssz::Encode as SszEncode; | ||
|
|
||
| #[test] | ||
| fn test_blocks_by_root_backward_compatibility() { | ||
| // Create some test roots | ||
| let root1 = H256::from_slice(&[1u8; 32]); | ||
| let root2 = H256::from_slice(&[2u8; 32]); | ||
| let roots_list = | ||
| RequestedBlockRoots::new(vec![root1, root2]).expect("Failed to create roots list"); | ||
|
|
||
| // Encode as old format (direct list, similar to transparent) | ||
| let old_format_bytes = roots_list.as_ssz_bytes(); | ||
|
|
||
| // Encode as new format (container) | ||
| let new_request = BlocksByRootRequest { | ||
| roots: roots_list.clone(), | ||
| }; | ||
| let new_format_bytes = new_request.as_ssz_bytes(); | ||
|
|
||
| // Both formats should decode successfully | ||
| let decoded_from_old = BlocksByRootRequest::from_ssz_bytes_compat(&old_format_bytes) | ||
| .expect("Failed to decode old format"); | ||
| let decoded_from_new = BlocksByRootRequest::from_ssz_bytes_compat(&new_format_bytes) | ||
| .expect("Failed to decode new format"); | ||
|
|
||
| // Both should have the same roots | ||
| assert_eq!(decoded_from_old.roots.len(), 2); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Correctness and Potential Bugs
Security Vulnerabilities
Performance Implications
Rust Best Practices and Idiomatic Patterns
Memory Safety and Proper Error Handling
Code Readability and Maintainability
Consensus Layer Considerations
Additional Recommendations
Overall, the code introduces a necessary compatibility feature but could be improved with better logging and more comprehensive test cases for malformed input. |
||
| assert_eq!(decoded_from_new.roots.len(), 2); | ||
| assert_eq!(decoded_from_old.roots[0], root1); | ||
| assert_eq!(decoded_from_old.roots[1], root2); | ||
| assert_eq!(decoded_from_new.roots[0], root1); | ||
| assert_eq!(decoded_from_new.roots[1], root2); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Comments
Code Correctness and Potential Bugs
from_ssz_bytestofrom_ssz_bytes_compat(lines 43-44) implies a modified function for deserialization. Ensure that this new function is validated against the SSZ standards. Verify thatfrom_ssz_bytes_compatis fully compatible with the expected data structure, especially if this data is involved in consensus-critical operations.Security Vulnerabilities
io::Error, it's crucial to ensure thatfrom_ssz_bytes_compatitself does not introduce any vulnerabilities or unchecked assumptions about the input. Consider running fuzz tests to ensure robustness against malformed inputs.Rust Best Practices and Idiomatic Patterns
map_errfor error handling is commendable.Memory Safety and Proper Error Handling
Code Readability and Maintainability
from_ssz_bytes_compatwould be beneficial for future maintainability.Consensus-Layer Considerations
from_ssz_bytes_compatif it affects block processing. Differences in serialization interpretation can potentially lead to consensus failures.Overall, while the change seems minimal, the implications on consensus and security require careful validation. Double-check the compatibility and testing of the
from_ssz_bytes_compatfunction before merging.