diff --git a/core/types/block.internal.libevm_test.go b/core/types/block.internal.libevm_test.go new file mode 100644 index 00000000000..bbcd2ad7658 --- /dev/null +++ b/core/types/block.internal.libevm_test.go @@ -0,0 +1,40 @@ +package types + +import ( + "reflect" + "slices" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBody_hasAnyOptionalFieldSet(t *testing.T) { + t.Parallel() + + var body Body + + assert.False(t, body.hasAnyOptionalFieldSet(), "body has no optional field set") + + v := reflect.ValueOf(&body).Elem() + for i := 0; i < v.NumField(); i++ { + fieldType := v.Type().Field(i) + tag := fieldType.Tag.Get("rlp") + if !slices.Contains(strings.Split(tag, ","), "optional") { + continue + } + + field := v.Field(i) + before := field.Interface() + switch field.Kind() { + case reflect.Slice: + slice := reflect.MakeSlice(field.Type(), 1, 1) + field.Set(slice) + default: + t.Errorf("unexpected field kind %q for field %q", field.Kind(), fieldType.Name) + } + + assert.Truef(t, body.hasAnyOptionalFieldSet(), "body has the optional field %q set", fieldType.Name) + field.Set(reflect.ValueOf(before)) // reset the field + } +} diff --git a/core/types/block.libevm.go b/core/types/block.libevm.go index 094613ee1e3..7cbb797fc39 100644 --- a/core/types/block.libevm.go +++ b/core/types/block.libevm.go @@ -137,7 +137,7 @@ func (b *Body) EncodeRLP(dst io.Writer) error { return err } - hasLaterOptionalField := b.Withdrawals != nil + hasLaterOptionalField := b.hasAnyOptionalFieldSet() if err := b.hooks().AppendRLPFields(w, hasLaterOptionalField); err != nil { return err } @@ -148,6 +148,10 @@ func (b *Body) EncodeRLP(dst io.Writer) error { }) } +func (b *Body) hasAnyOptionalFieldSet() bool { + return b.Withdrawals != nil +} + // DecodeRLP implements the [rlp.Decoder] interface. func (b *Body) DecodeRLP(s *rlp.Stream) error { return s.FromList(func() error {