rlp/rlpgen: RLP encoder code generator (#24251)
This change adds a code generator tool for creating EncodeRLP method implementations. The generated methods will behave identically to the reflect-based encoder, but run faster because there is no reflection overhead. Package rlp now provides the EncoderBuffer type for incremental encoding. This is used by generated code, but the new methods can also be useful for hand-written encoders. There is also experimental support for generating DecodeRLP, and some new methods have been added to the existing Stream type to support this. Creating decoders with rlpgen is not recommended at this time because the generated methods create very poor error reporting. More detail about package rlp changes: * rlp: externalize struct field processing / validation This adds a new package, rlp/internal/rlpstruct, in preparation for the RLP encoder generator. I think the struct field rules are subtle enough to warrant extracting this into their own package, even though it means that a bunch of adapter code is needed for converting to/from rlpstruct.Type. * rlp: add more decoder methods (for rlpgen) This adds new methods on rlp.Stream: - Uint64, Uint32, Uint16, Uint8, BigInt - ReadBytes for decoding into []byte - MoreDataInList - useful for optional list elements * rlp: expose encoder buffer (for rlpgen) This exposes the internal encoder buffer type for use in EncodeRLP implementations. The new EncoderBuffer type is a sort-of 'opaque handle' for a pointer to encBuffer. It is implemented this way to ensure the global encBuffer pool is handled correctly.
This commit is contained in:
@ -286,6 +286,47 @@ func TestStreamRaw(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamReadBytes(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
size int
|
||||
err string
|
||||
}{
|
||||
// kind List
|
||||
{input: "C0", size: 1, err: "rlp: expected String or Byte"},
|
||||
// kind Byte
|
||||
{input: "04", size: 0, err: "input value has wrong size 1, want 0"},
|
||||
{input: "04", size: 1},
|
||||
{input: "04", size: 2, err: "input value has wrong size 1, want 2"},
|
||||
// kind String
|
||||
{input: "820102", size: 0, err: "input value has wrong size 2, want 0"},
|
||||
{input: "820102", size: 1, err: "input value has wrong size 2, want 1"},
|
||||
{input: "820102", size: 2},
|
||||
{input: "820102", size: 3, err: "input value has wrong size 2, want 3"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
name := fmt.Sprintf("input_%s/size_%d", test.input, test.size)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
s := NewStream(bytes.NewReader(unhex(test.input)), 0)
|
||||
b := make([]byte, test.size)
|
||||
err := s.ReadBytes(b)
|
||||
if test.err == "" {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error %q", err)
|
||||
}
|
||||
} else {
|
||||
if err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
} else if err.Error() != test.err {
|
||||
t.Errorf("wrong error %q", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeErrors(t *testing.T) {
|
||||
r := bytes.NewReader(nil)
|
||||
|
||||
@ -990,7 +1031,7 @@ func TestInvalidOptionalField(t *testing.T) {
|
||||
v interface{}
|
||||
err string
|
||||
}{
|
||||
{v: new(invalid1), err: `rlp: struct field rlp.invalid1.B needs "optional" tag`},
|
||||
{v: new(invalid1), err: `rlp: invalid struct tag "" for rlp.invalid1.B (must be optional because preceding field "A" is optional)`},
|
||||
{v: new(invalid2), err: `rlp: invalid struct tag "optional" for rlp.invalid2.T (also has "tail" tag)`},
|
||||
{v: new(invalid3), err: `rlp: invalid struct tag "tail" for rlp.invalid3.T (also has "optional" tag)`},
|
||||
}
|
||||
|
Reference in New Issue
Block a user