rlp: add support for optional struct fields#22832
Merged
fjl merged 9 commits intoethereum:masterfrom May 7, 2021
Merged
Conversation
Contributor
|
Suggestion: diff --git a/rlp/decode_test.go b/rlp/decode_test.go
index af4cd0acc4..c9191949fa 100644
--- a/rlp/decode_test.go
+++ b/rlp/decode_test.go
@@ -979,12 +979,17 @@ func TestInvalidOptionalField(t *testing.T) {
for _, test := range tests {
err := DecodeBytes(unhex("C20102"), test.v)
if err == nil {
- t.Errorf("no error for %T", test.v)
+ t.Errorf("decode: no error for %T", test.v)
} else if err.Error() != test.err {
- t.Errorf("wrong error for %T: %v", test.v, err.Error())
+ t.Errorf("decode: wrong error for %T: %v", test.v, err.Error())
+ }
+ _, err = EncodeToBytes(test.v)
+ if err == nil {
+ t.Errorf("encode: no error for %T", test.v)
+ } else if err.Error() != test.err {
+ t.Errorf("encode: wrong error for %T: %v", test.v, err.Error())
}
}
-
}
func ExampleDecode() { |
holiman
approved these changes
May 7, 2021
Contributor
holiman
left a comment
There was a problem hiding this comment.
Tested it a bit, it seems to work exactly as expected. I expected it to fail some of my tests, e..g
type x struct {
A uint64
B uint `rlp:"optional"`
C uint `rlp:"optional"`
}
When doing EncodeToBytes(&x{0xaa, 0, 0xcc}) it handles it fine (writes all data without complaining about optional), and with EncodeToBytes(&x{0xaa, 0, 0}) it ignores the two last elements. So that's good!
Contributor
Author
|
Regarding the suggestion, I think it's not necessary because the test is specifically for the struct tags validation in typecache.go. Both encoding and decoding call into this. |
atif-konasl
pushed a commit
to frozeman/pandora-execution-engine
that referenced
this pull request
Oct 15, 2021
This adds support for a new struct tag "optional". Using this tag, structs used for RLP encoding/decoding can be extended in a backwards-compatible way, by adding new fields at the end.
This was referenced Sep 23, 2022
19 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This adds support for a new struct tag "optional". Using this tag, structs used for RLP
encoding/decoding can be extended in a backwards-compatible way, by adding new fields at
the end.