accounts/abi: change unpacking of abi fields w/ underscores (#16513)

This commit is contained in:
Daniel Liu 2024-12-12 16:00:43 +08:00
parent c0f547ca78
commit e58441b317
4 changed files with 61 additions and 49 deletions

View file

@ -278,21 +278,20 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
return ret, nil return ret, nil
} }
// capitalise makes the first character of a string upper case, also removing any // ToCamelCase converts an under-score string to a camel-case string
// prefixing underscores from the variable names. func ToCamelCase(input string) string {
func capitalise(input string) string { parts := strings.Split(input, "_")
for len(input) > 0 && input[0] == '_' { for i, s := range parts {
input = input[1:] if len(s) > 0 {
parts[i] = strings.ToUpper(s[:1]) + s[1:]
}
} }
if len(input) == 0 { return strings.Join(parts, "")
return ""
}
return strings.ToUpper(input[:1]) + input[1:]
} }
// unpackStruct extracts each argument into its corresponding struct field // unpackStruct extracts each argument into its corresponding struct field
func unpackStruct(value, reflectValue reflect.Value, arg Argument) error { func unpackStruct(value, reflectValue reflect.Value, arg Argument) error {
name := capitalise(arg.Name) name := ToCamelCase(arg.Name)
typ := value.Type() typ := value.Type()
for j := 0; j < typ.NumField(); j++ { for j := 0; j < typ.NumField(); j++ {
// TODO read tags: `abi:"fieldName"` // TODO read tags: `abi:"fieldName"`

View file

@ -327,48 +327,14 @@ var methodNormalizer = map[Lang]func(string) string{
// capitalise makes a camel-case string which starts with an upper case character. // capitalise makes a camel-case string which starts with an upper case character.
func capitalise(input string) string { func capitalise(input string) string {
for len(input) > 0 && input[0] == '_' { return abi.ToCamelCase(input)
input = input[1:]
}
if len(input) == 0 {
return ""
}
return toCamelCase(strings.ToUpper(input[:1]) + input[1:])
} }
// decapitalise makes a camel-case string which starts with a lower case character. // decapitalise makes a camel-case string which starts with a lower case character.
func decapitalise(input string) string { func decapitalise(input string) string {
for len(input) > 0 && input[0] == '_' { // NOTE: This is the current behavior, it doesn't match the comment
input = input[1:] // above and needs to be investigated.
} return abi.ToCamelCase(input)
if len(input) == 0 {
return ""
}
return toCamelCase(strings.ToLower(input[:1]) + input[1:])
}
// toCamelCase converts an under-score string to a camel-case string
func toCamelCase(input string) string {
toupper := false
result := ""
for k, v := range input {
switch {
case k == 0:
result = strings.ToUpper(string(input[0]))
case toupper:
result += strings.ToUpper(string(v))
toupper = false
case v == '_':
toupper = true
default:
result += string(v)
}
}
return result
} }
// structured checks whether a list of ABI data types has enough information to // structured checks whether a list of ABI data types has enough information to

View file

@ -116,7 +116,7 @@ func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind,
func requireUniqueStructFieldNames(args Arguments) error { func requireUniqueStructFieldNames(args Arguments) error {
exists := make(map[string]bool) exists := make(map[string]bool)
for _, arg := range args { for _, arg := range args {
field := capitalise(arg.Name) field := ToCamelCase(arg.Name)
if field == "" { if field == "" {
return errors.New("abi: purely underscored output cannot unpack to struct") return errors.New("abi: purely underscored output cannot unpack to struct")
} }

View file

@ -273,6 +273,53 @@ var unpackTests = []unpackTest{
Int2 *big.Int Int2 *big.Int
}{big.NewInt(1), big.NewInt(2)}, }{big.NewInt(1), big.NewInt(2)},
}, },
{
def: `[{"name":"int_one","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
IntOne *big.Int
}{big.NewInt(1)},
},
{
def: `[{"name":"int__one","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
IntOne *big.Int
}{big.NewInt(1)},
},
{
def: `[{"name":"int_one_","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
IntOne *big.Int
}{big.NewInt(1)},
},
{
def: `[{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
IntOne *big.Int
Intone *big.Int
}{big.NewInt(1), big.NewInt(2)},
},
{
def: `[{"name":"___","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
IntOne *big.Int
Intone *big.Int
}{},
err: "abi: purely underscored output cannot unpack to struct",
},
{
def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
Int1 *big.Int
Int2 *big.Int
}{},
err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
},
{ {
def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`, def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",