Skip to content

Commit 7ca4030

Browse files
rjl493456442gballet
authored andcommitted
accounts/abi: tuple support (#18406)
1 parent 6df3e4e commit 7ca4030

File tree

9 files changed

+1003
-393
lines changed

9 files changed

+1003
-393
lines changed

accounts/abi/abi.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,11 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
5858
return nil, err
5959
}
6060
return arguments, nil
61-
6261
}
6362
method, exist := abi.Methods[name]
6463
if !exist {
6564
return nil, fmt.Errorf("method '%s' not found", name)
6665
}
67-
6866
arguments, err := method.Inputs.Pack(args...)
6967
if err != nil {
7068
return nil, err

accounts/abi/abi_test.go

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ import (
2222
"fmt"
2323
"log"
2424
"math/big"
25+
"reflect"
2526
"strings"
2627
"testing"
2728

28-
"reflect"
29-
3029
"github.com/ethereum/go-ethereum/common"
3130
"github.com/ethereum/go-ethereum/crypto"
3231
)
@@ -52,11 +51,14 @@ const jsondata2 = `
5251
{ "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
5352
{ "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
5453
{ "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
55-
{ "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
54+
{ "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] },
55+
{ "type" : "function", "name" : "nestedArray", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint256[2][2]" }, { "name" : "b", "type" : "address[]" } ] },
56+
{ "type" : "function", "name" : "nestedArray2", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint8[][2]" } ] },
57+
{ "type" : "function", "name" : "nestedSlice", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint8[][]" } ] }
5658
]`
5759

5860
func TestReader(t *testing.T) {
59-
Uint256, _ := NewType("uint256")
61+
Uint256, _ := NewType("uint256", nil)
6062
exp := ABI{
6163
Methods: map[string]Method{
6264
"balance": {
@@ -177,7 +179,7 @@ func TestTestSlice(t *testing.T) {
177179
}
178180

179181
func TestMethodSignature(t *testing.T) {
180-
String, _ := NewType("string")
182+
String, _ := NewType("string", nil)
181183
m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
182184
exp := "foo(string,string)"
183185
if m.Sig() != exp {
@@ -189,12 +191,31 @@ func TestMethodSignature(t *testing.T) {
189191
t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
190192
}
191193

192-
uintt, _ := NewType("uint256")
194+
uintt, _ := NewType("uint256", nil)
193195
m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil}
194196
exp = "foo(uint256)"
195197
if m.Sig() != exp {
196198
t.Error("signature mismatch", exp, "!=", m.Sig())
197199
}
200+
201+
// Method with tuple arguments
202+
s, _ := NewType("tuple", []ArgumentMarshaling{
203+
{Name: "a", Type: "int256"},
204+
{Name: "b", Type: "int256[]"},
205+
{Name: "c", Type: "tuple[]", Components: []ArgumentMarshaling{
206+
{Name: "x", Type: "int256"},
207+
{Name: "y", Type: "int256"},
208+
}},
209+
{Name: "d", Type: "tuple[2]", Components: []ArgumentMarshaling{
210+
{Name: "x", Type: "int256"},
211+
{Name: "y", Type: "int256"},
212+
}},
213+
})
214+
m = Method{"foo", false, []Argument{{"s", s, false}, {"bar", String, false}}, nil}
215+
exp = "foo((int256,int256[],(int256,int256)[],(int256,int256)[2]),string)"
216+
if m.Sig() != exp {
217+
t.Error("signature mismatch", exp, "!=", m.Sig())
218+
}
198219
}
199220

200221
func TestMultiPack(t *testing.T) {
@@ -564,11 +585,13 @@ func TestBareEvents(t *testing.T) {
564585
const definition = `[
565586
{ "type" : "event", "name" : "balance" },
566587
{ "type" : "event", "name" : "anon", "anonymous" : true},
567-
{ "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
588+
{ "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] },
589+
{ "type" : "event", "name" : "tuple", "inputs" : [{ "indexed":false, "name":"t", "type":"tuple", "components":[{"name":"a", "type":"uint256"}] }, { "indexed":true, "name":"arg1", "type":"address" }] }
568590
]`
569591

570-
arg0, _ := NewType("uint256")
571-
arg1, _ := NewType("address")
592+
arg0, _ := NewType("uint256", nil)
593+
arg1, _ := NewType("address", nil)
594+
tuple, _ := NewType("tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}})
572595

573596
expectedEvents := map[string]struct {
574597
Anonymous bool
@@ -580,6 +603,10 @@ func TestBareEvents(t *testing.T) {
580603
{Name: "arg0", Type: arg0, Indexed: false},
581604
{Name: "arg1", Type: arg1, Indexed: true},
582605
}},
606+
"tuple": {false, []Argument{
607+
{Name: "t", Type: tuple, Indexed: false},
608+
{Name: "arg1", Type: arg1, Indexed: true},
609+
}},
583610
}
584611

585612
abi, err := JSON(strings.NewReader(definition))
@@ -646,28 +673,24 @@ func TestUnpackEvent(t *testing.T) {
646673
}
647674

648675
type ReceivedEvent struct {
649-
Address common.Address
650-
Amount *big.Int
651-
Memo []byte
676+
Sender common.Address
677+
Amount *big.Int
678+
Memo []byte
652679
}
653680
var ev ReceivedEvent
654681

655682
err = abi.Unpack(&ev, "received", data)
656683
if err != nil {
657684
t.Error(err)
658-
} else {
659-
t.Logf("len(data): %d; received event: %+v", len(data), ev)
660685
}
661686

662687
type ReceivedAddrEvent struct {
663-
Address common.Address
688+
Sender common.Address
664689
}
665690
var receivedAddrEv ReceivedAddrEvent
666691
err = abi.Unpack(&receivedAddrEv, "receivedAddr", data)
667692
if err != nil {
668693
t.Error(err)
669-
} else {
670-
t.Logf("len(data): %d; received event: %+v", len(data), receivedAddrEv)
671694
}
672695
}
673696

0 commit comments

Comments
 (0)