Skip to content

Commit b973176

Browse files
authored
accounts/abi: handle named ouputs prefixed with underscores (#15766)
* accounts/abi: handle named ouputs prefixed with underscores * accounts/abi: handle collinding outputs for struct unpacks * accounts: handle purely underscore output names
1 parent 36a1087 commit b973176

4 files changed

Lines changed: 153 additions & 4 deletions

File tree

‎accounts/abi/argument.go‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
9696
if err := requireUnpackKind(value, typ, kind, arguments); err != nil {
9797
return err
9898
}
99+
// If the output interface is a struct, make sure names don't collide
100+
if kind == reflect.Struct {
101+
exists := make(map[string]bool)
102+
for _, arg := range arguments {
103+
field := capitalise(arg.Name)
104+
if field == "" {
105+
return fmt.Errorf("abi: purely underscored output cannot unpack to struct")
106+
}
107+
if exists[field] {
108+
return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field)
109+
}
110+
exists[field] = true
111+
}
112+
}
99113
// `i` counts the nonindexed arguments.
100114
// `j` counts the number of complex types.
101115
// both `i` and `j` are used to to correctly compute `data` offset.
@@ -123,10 +137,10 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
123137

124138
switch kind {
125139
case reflect.Struct:
140+
name := capitalise(arg.Name)
126141
for j := 0; j < typ.NumField(); j++ {
127-
field := typ.Field(j)
128142
// TODO read tags: `abi:"fieldName"`
129-
if field.Name == strings.ToUpper(arg.Name[:1])+arg.Name[1:] {
143+
if typ.Field(j).Name == name {
130144
if err := set(value.Field(j), reflectValue, arg); err != nil {
131145
return err
132146
}
@@ -222,3 +236,15 @@ func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
222236

223237
return ret, nil
224238
}
239+
240+
// capitalise makes the first character of a string upper case, also removing any
241+
// prefixing underscores from the variable names.
242+
func capitalise(input string) string {
243+
for len(input) > 0 && input[0] == '_' {
244+
input = input[1:]
245+
}
246+
if len(input) == 0 {
247+
return ""
248+
}
249+
return strings.ToUpper(input[:1]) + input[1:]
250+
}

‎accounts/abi/bind/bind.go‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,15 @@ var methodNormalizer = map[Lang]func(string) string{
304304
LangJava: decapitalise,
305305
}
306306

307-
// capitalise makes the first character of a string upper case.
307+
// capitalise makes the first character of a string upper case, also removing any
308+
// prefixing underscores from the variable names.
308309
func capitalise(input string) string {
310+
for len(input) > 0 && input[0] == '_' {
311+
input = input[1:]
312+
}
313+
if len(input) == 0 {
314+
return ""
315+
}
309316
return strings.ToUpper(input[:1]) + input[1:]
310317
}
311318

@@ -315,15 +322,24 @@ func decapitalise(input string) string {
315322
}
316323

317324
// structured checks whether a method has enough information to return a proper
318-
// Go struct ot if flat returns are needed.
325+
// Go struct or if flat returns are needed.
319326
func structured(method abi.Method) bool {
320327
if len(method.Outputs) < 2 {
321328
return false
322329
}
330+
exists := make(map[string]bool)
323331
for _, out := range method.Outputs {
332+
// If the name is anonymous, we can't organize into a struct
324333
if out.Name == "" {
325334
return false
326335
}
336+
// If the field name is empty when normalized or collides (var, Var, _var, _Var),
337+
// we can't organize into a struct
338+
field := capitalise(out.Name)
339+
if field == "" || exists[field] {
340+
return false
341+
}
342+
exists[field] = true
327343
}
328344
return true
329345
}

‎accounts/abi/bind/bind_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ var bindTests = []struct {
126126
{"type":"function","name":"namedOutput","constant":true,"inputs":[],"outputs":[{"name":"str","type":"string"}]},
127127
{"type":"function","name":"anonOutput","constant":true,"inputs":[],"outputs":[{"name":"","type":"string"}]},
128128
{"type":"function","name":"namedOutputs","constant":true,"inputs":[],"outputs":[{"name":"str1","type":"string"},{"name":"str2","type":"string"}]},
129+
{"type":"function","name":"collidingOutputs","constant":true,"inputs":[],"outputs":[{"name":"str","type":"string"},{"name":"Str","type":"string"}]},
129130
{"type":"function","name":"anonOutputs","constant":true,"inputs":[],"outputs":[{"name":"","type":"string"},{"name":"","type":"string"}]},
130131
{"type":"function","name":"mixedOutputs","constant":true,"inputs":[],"outputs":[{"name":"","type":"string"},{"name":"str","type":"string"}]}
131132
]
@@ -140,6 +141,7 @@ var bindTests = []struct {
140141
str1, err = b.NamedOutput(nil)
141142
str1, err = b.AnonOutput(nil)
142143
res, _ := b.NamedOutputs(nil)
144+
str1, str2, err = b.CollidingOutputs(nil)
143145
str1, str2, err = b.AnonOutputs(nil)
144146
str1, str2, err = b.MixedOutputs(nil)
145147
@@ -447,6 +449,66 @@ var bindTests = []struct {
447449
}
448450
`,
449451
},
452+
{
453+
`Underscorer`,
454+
`
455+
contract Underscorer {
456+
function UnderscoredOutput() constant returns (int _int, string _string) {
457+
return (314, "pi");
458+
}
459+
function LowerLowerCollision() constant returns (int _res, int res) {
460+
return (1, 2);
461+
}
462+
function LowerUpperCollision() constant returns (int _res, int Res) {
463+
return (1, 2);
464+
}
465+
function UpperLowerCollision() constant returns (int _Res, int res) {
466+
return (1, 2);
467+
}
468+
function UpperUpperCollision() constant returns (int _Res, int Res) {
469+
return (1, 2);
470+
}
471+
function PurelyUnderscoredOutput() constant returns (int _, int res) {
472+
return (1, 2);
473+
}
474+
function AllPurelyUnderscoredOutput() constant returns (int _, int __) {
475+
return (1, 2);
476+
}
477+
}
478+
`, `6060604052341561000f57600080fd5b6103498061001e6000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806303a592131461008857806367e6633d146100b85780639df484851461014d578063af7486ab1461017d578063b564b34d146101ad578063e02ab24d146101dd578063e409ca451461020d575b600080fd5b341561009357600080fd5b61009b61023d565b604051808381526020018281526020019250505060405180910390f35b34156100c357600080fd5b6100cb610252565b6040518083815260200180602001828103825283818151815260200191508051906020019080838360005b838110156101115780820151818401526020810190506100f6565b50505050905090810190601f16801561013e5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b341561015857600080fd5b6101606102a0565b604051808381526020018281526020019250505060405180910390f35b341561018857600080fd5b6101906102b5565b604051808381526020018281526020019250505060405180910390f35b34156101b857600080fd5b6101c06102ca565b604051808381526020018281526020019250505060405180910390f35b34156101e857600080fd5b6101f06102df565b604051808381526020018281526020019250505060405180910390f35b341561021857600080fd5b6102206102f4565b604051808381526020018281526020019250505060405180910390f35b60008060016002819150809050915091509091565b600061025c610309565b61013a8090506040805190810160405280600281526020017f7069000000000000000000000000000000000000000000000000000000000000815250915091509091565b60008060016002819150809050915091509091565b60008060016002819150809050915091509091565b60008060016002819150809050915091509091565b60008060016002819150809050915091509091565b60008060016002819150809050915091509091565b6020604051908101604052806000815250905600a165627a7a72305820c11dcfa136fc7d182ee4d34f0b12d988496228f7e2d02d2b5376d996ca1743d00029`,
479+
`[{"constant":true,"inputs":[],"name":"LowerUpperCollision","outputs":[{"name":"_res","type":"int256"},{"name":"Res","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"UnderscoredOutput","outputs":[{"name":"_int","type":"int256"},{"name":"_string","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"PurelyUnderscoredOutput","outputs":[{"name":"_","type":"int256"},{"name":"res","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"UpperLowerCollision","outputs":[{"name":"_Res","type":"int256"},{"name":"res","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"AllPurelyUnderscoredOutput","outputs":[{"name":"_","type":"int256"},{"name":"__","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"UpperUpperCollision","outputs":[{"name":"_Res","type":"int256"},{"name":"Res","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"LowerLowerCollision","outputs":[{"name":"_res","type":"int256"},{"name":"res","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"}]`,
480+
`
481+
// Generate a new random account and a funded simulator
482+
key, _ := crypto.GenerateKey()
483+
auth := bind.NewKeyedTransactor(key)
484+
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}})
485+
486+
// Deploy a underscorer tester contract and execute a structured call on it
487+
_, _, underscorer, err := DeployUnderscorer(auth, sim)
488+
if err != nil {
489+
t.Fatalf("Failed to deploy underscorer contract: %v", err)
490+
}
491+
sim.Commit()
492+
493+
// Verify that underscored return values correctly parse into structs
494+
if res, err := underscorer.UnderscoredOutput(nil); err != nil {
495+
t.Errorf("Failed to call constant function: %v", err)
496+
} else if res.Int.Cmp(big.NewInt(314)) != 0 || res.String != "pi" {
497+
t.Errorf("Invalid result, want: {314, \"pi\"}, got: %+v", res)
498+
}
499+
// Verify that underscored and non-underscored name collisions force tuple outputs
500+
var a, b *big.Int
501+
502+
a, b, _ = underscorer.LowerLowerCollision(nil)
503+
a, b, _ = underscorer.LowerUpperCollision(nil)
504+
a, b, _ = underscorer.UpperLowerCollision(nil)
505+
a, b, _ = underscorer.UpperUpperCollision(nil)
506+
a, b, _ = underscorer.PurelyUnderscoredOutput(nil)
507+
a, b, _ = underscorer.AllPurelyUnderscoredOutput(nil)
508+
509+
fmt.Println(a, b, err)
510+
`,
511+
},
450512
}
451513

452514
// Tests that packages generated by the binder can be successfully compiled and

‎accounts/abi/unpack_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,51 @@ var unpackTests = []unpackTest{
259259
enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
260260
want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
261261
},
262+
// struct outputs
263+
{
264+
def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
265+
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
266+
want: struct {
267+
Int1 *big.Int
268+
Int2 *big.Int
269+
}{big.NewInt(1), big.NewInt(2)},
270+
},
271+
{
272+
def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
273+
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
274+
want: struct {
275+
Int1 *big.Int
276+
Int2 *big.Int
277+
}{},
278+
err: "abi: multiple outputs mapping to the same struct field 'Int'",
279+
},
280+
{
281+
def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
282+
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
283+
want: struct {
284+
Int1 *big.Int
285+
Int2 *big.Int
286+
}{},
287+
err: "abi: multiple outputs mapping to the same struct field 'Int'",
288+
},
289+
{
290+
def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
291+
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
292+
want: struct {
293+
Int1 *big.Int
294+
Int2 *big.Int
295+
}{},
296+
err: "abi: multiple outputs mapping to the same struct field 'Int'",
297+
},
298+
{
299+
def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
300+
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
301+
want: struct {
302+
Int1 *big.Int
303+
Int2 *big.Int
304+
}{},
305+
err: "abi: purely underscored output cannot unpack to struct",
306+
},
262307
}
263308

264309
func TestUnpack(t *testing.T) {

0 commit comments

Comments
 (0)