Skip to content

Commit 594328c

Browse files
karalabeobscuren
authored andcommitted
[release/1.4.4] accounts/abi/bind: fix multi-value anonymous unmarshalling
(cherry picked from commit cc21706)
1 parent 2e6b9c1 commit 594328c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

accounts/abi/bind/bind_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,44 @@ var bindTests = []struct {
194194
}
195195
`,
196196
},
197+
// Tests that plain values can be properly returned and deserialized
198+
{
199+
`Getter`,
200+
`
201+
contract Getter {
202+
function getter() constant returns (string, int, bytes32) {
203+
return ("Hi", 1, sha3(""));
204+
}
205+
}
206+
`,
207+
`606060405260dc8060106000396000f3606060405260e060020a6000350463993a04b78114601a575b005b600060605260c0604052600260809081527f486900000000000000000000000000000000000000000000000000000000000060a05260017fc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a47060e0829052610100819052606060c0908152600261012081905281906101409060a09080838184600060046012f1505081517fffff000000000000000000000000000000000000000000000000000000000000169091525050604051610160819003945092505050f3`,
208+
`[{"constant":true,"inputs":[],"name":"getter","outputs":[{"name":"","type":"string"},{"name":"","type":"int256"},{"name":"","type":"bytes32"}],"type":"function"}]`,
209+
`
210+
// Generate a new random account and a funded simulator
211+
key, _ := crypto.GenerateKey()
212+
auth := bind.NewKeyedTransactor(key)
213+
sim := backends.NewSimulatedBackend(core.GenesisAccount{Address: auth.From, Balance: big.NewInt(10000000000)})
214+
215+
// Deploy a tuple tester contract and execute a structured call on it
216+
_, _, getter, err := DeployGetter(auth, sim)
217+
if err != nil {
218+
t.Fatalf("Failed to deploy getter contract: %v", err)
219+
}
220+
sim.Commit()
221+
222+
if str, num, _, err := getter.Getter(nil); err != nil {
223+
t.Fatalf("Failed to call anonymous field retriever: %v", err)
224+
} else if str != "Hi" || num.Cmp(big.NewInt(1)) != 0 {
225+
t.Fatalf("Retrieved value mismatch: have %v/%v, want %v/%v", str, num, "Hi", 1)
226+
}
227+
`,
228+
},
197229
// Tests that tuples can be properly returned and deserialized
198230
{
199231
`Tupler`,
200232
`
201233
contract Tupler {
202-
function tuple() returns (string a, int b, bytes32 c) {
234+
function tuple() constant returns (string a, int b, bytes32 c) {
203235
return ("Hi", 1, sha3(""));
204236
}
205237
}
@@ -219,8 +251,10 @@ var bindTests = []struct {
219251
}
220252
sim.Commit()
221253
222-
if _, err := tupler.Tuple(nil); err != nil {
254+
if res, err := tupler.Tuple(nil); err != nil {
223255
t.Fatalf("Failed to call structure retriever: %v", err)
256+
} else if res.A != "Hi" || res.B.Cmp(big.NewInt(1)) != 0 {
257+
t.Fatalf("Retrieved value mismatch: have %v/%v, want %v/%v", res.A, res.B, "Hi", 1)
224258
}
225259
`,
226260
},

accounts/abi/bind/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ package {{.Package}}
211211
{{range $i, $_ := .Normalized.Outputs}}ret{{$i}} = new({{bindtype .Type}})
212212
{{end}}
213213
){{end}}
214-
out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}[]interface{}{
214+
out := {{if .Structured}}ret{{else}}{{if eq (len .Normalized.Outputs) 1}}ret0{{else}}&[]interface{}{
215215
{{range $i, $_ := .Normalized.Outputs}}ret{{$i}},
216216
{{end}}
217217
}{{end}}{{end}}

0 commit comments

Comments
 (0)