Skip to content

Commit

Permalink
Name unnamed return values Fi where i is the index of the returned pa…
Browse files Browse the repository at this point in the history
…rameter. If that name is taken, append _X until it's not taken (#13433)
  • Loading branch information
nolag authored Jun 5, 2024
1 parent 58420d6 commit 72c34c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
14 changes: 12 additions & 2 deletions core/services/relay/evm/types/codec_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,23 @@ func (entry *codecEntry) Init() (err error) {
if err != nil {
return err
}
allowRename := false
if len(arg.Name) == 0 {
return fmt.Errorf("%w: empty field names are not supported for multiple returns", commontypes.ErrInvalidType)
arg.Name = fmt.Sprintf("F%d", i)
allowRename = true
}

name := strings.ToUpper(arg.Name[:1]) + arg.Name[1:]
if seenNames[name] {
return fmt.Errorf("%w: duplicate field name %s, after ToCamelCase", commontypes.ErrInvalidConfig, name)
if !allowRename {
return fmt.Errorf("%w: duplicate field name %s, after ToCamelCase", commontypes.ErrInvalidConfig, name)
}
for {
name = name + "_X"
if !seenNames[name] {
break
}
}
}
seenNames[name] = true
native[i] = reflect.StructField{Name: name, Type: nativeArg}
Expand Down
37 changes: 33 additions & 4 deletions core/services/relay/evm/types/codec_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,40 @@ func TestCodecEntry(t *testing.T) {
assertHaveSameStructureAndNames(t, iNative.Type(), entry.CheckedType())
})

t.Run("Multiple unnamed parameters are not supported", func(t *testing.T) {
anyType, err := abi.NewType("int16[3]", "", []abi.ArgumentMarshaling{})
t.Run("Unnamed parameters are named after their locations", func(t *testing.T) {
// use different types to make sure that the right fields have the right types.
anyType1, err := abi.NewType("int64", "", []abi.ArgumentMarshaling{})
require.NoError(t, err)
anyType2, err := abi.NewType("int32", "", []abi.ArgumentMarshaling{})
require.NoError(t, err)
entry := NewCodecEntry(abi.Arguments{{Name: "", Type: anyType1}, {Name: "", Type: anyType2}}, nil, nil)
assert.NoError(t, entry.Init())
ct := entry.CheckedType()
require.Equal(t, 2, ct.NumField())
f0 := ct.Field(0)
assert.Equal(t, "F0", f0.Name)
assert.Equal(t, reflect.TypeOf((*int64)(nil)), f0.Type)
f1 := ct.Field(1)
assert.Equal(t, "F1", f1.Name)
assert.Equal(t, reflect.TypeOf((*int32)(nil)), f1.Type)
})

t.Run("Unnamed parameters adds _Xes at the end if their location name is taken", func(t *testing.T) {
// use different types to make sure that the right fields have the right types.
anyType1, err := abi.NewType("int64", "", []abi.ArgumentMarshaling{})
require.NoError(t, err)
anyType2, err := abi.NewType("int32", "", []abi.ArgumentMarshaling{})
require.NoError(t, err)
entry := NewCodecEntry(abi.Arguments{{Name: "", Type: anyType}, {Name: "", Type: anyType}}, nil, nil)
assert.True(t, errors.Is(entry.Init(), commontypes.ErrInvalidType))
entry := NewCodecEntry(abi.Arguments{{Name: "F1", Type: anyType1}, {Name: "", Type: anyType2}}, nil, nil)
assert.NoError(t, entry.Init())
ct := entry.CheckedType()
require.Equal(t, 2, ct.NumField())
f0 := ct.Field(0)
assert.Equal(t, "F1", f0.Name)
assert.Equal(t, reflect.TypeOf((*int64)(nil)), f0.Type)
f1 := ct.Field(1)
assert.Equal(t, "F1_X", f1.Name)
assert.Equal(t, reflect.TypeOf((*int32)(nil)), f1.Type)
})

t.Run("Multiple abi arguments with the same name returns an error", func(t *testing.T) {
Expand Down

0 comments on commit 72c34c0

Please sign in to comment.