diff --git a/rlp/encode.go b/rlp/encode.go index ffb42b2997..0b311871fd 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -22,6 +22,7 @@ import ( "io" "math/big" "reflect" + "slices" "github.com/ethereum/go-ethereum/rlp/internal/rlpstruct" "github.com/holiman/uint256" @@ -351,7 +352,10 @@ func makeStructWriter(typ reflect.Type) (writer, error) { } var writer writer - firstOptionalField := firstOptionalField(fields) + firstOptionalField := slices.IndexFunc(fields, func(f field) bool { return f.optional }) + if firstOptionalField == -1 { + firstOptionalField = len(fields) + } if firstOptionalField == len(fields) { // This is the writer function for structs without any optional fields. writer = func(val reflect.Value, w *encBuffer) error { diff --git a/rlp/typecache.go b/rlp/typecache.go index 3e37c9d2fc..38cea32a42 100644 --- a/rlp/typecache.go +++ b/rlp/typecache.go @@ -159,16 +159,6 @@ func structFields(typ reflect.Type) (fields []field, err error) { return fields, nil } -// firstOptionalField returns the index of the first field with "optional" tag. -func firstOptionalField(fields []field) int { - for i, f := range fields { - if f.optional { - return i - } - } - return len(fields) -} - type structFieldError struct { typ reflect.Type field int