Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions core/types/block.internal.libevm_test.go
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs support for reflect.Pointer as the optional fields can be struct pointers.

Copy link
Author

Choose a reason for hiding this comment

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

We can just add them later on a add-when-needed basis. If it's not defined in the switch, the test would fail

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
}
}
6 changes: 5 additions & 1 deletion core/types/block.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
Loading