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

rlp: add support for optional struct fields #22832

Merged
merged 9 commits into from
May 7, 2021
Merged

Conversation

fjl
Copy link
Contributor

@fjl fjl commented May 6, 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.

@fjl fjl added this to the 1.10.4 milestone May 6, 2021
@fjl fjl marked this pull request as draft May 6, 2021 12:51
@fjl fjl marked this pull request as ready for review May 7, 2021 11:06
@holiman
Copy link
Contributor

holiman commented May 7, 2021

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() {

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

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

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!

@fjl
Copy link
Contributor Author

fjl commented May 7, 2021

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.

@fjl fjl merged commit 700df14 into ethereum:master May 7, 2021
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants