@@ -18,7 +18,6 @@ package abi
18
18
19
19
import (
20
20
"bytes"
21
- "errors"
22
21
"fmt"
23
22
"log"
24
23
"math/big"
@@ -53,52 +52,52 @@ func TestTypeCheck(t *testing.T) {
53
52
for i , test := range []struct {
54
53
typ string
55
54
input interface {}
56
- err error
55
+ err string
57
56
}{
58
- {"uint" , big .NewInt (1 ), nil },
59
- {"int" , big .NewInt (1 ), nil },
60
- {"uint30" , big .NewInt (1 ), nil },
61
- {"uint30" , uint8 (1 ), varErr ( reflect . Ptr , reflect . Uint8 ) },
62
- {"uint16" , uint16 (1 ), nil },
63
- {"uint16" , uint8 (1 ), varErr ( reflect . Uint16 , reflect . Uint8 ) },
64
- {"uint16[]" , []uint16 {1 , 2 , 3 }, nil },
65
- {"uint16[]" , [3 ]uint16 {1 , 2 , 3 }, nil },
66
- {"uint16[]" , []uint32 {1 , 2 , 3 }, typeErr ( formatSliceString ( reflect . Uint16 , - 1 ), formatSliceString ( reflect . Uint32 , - 1 )) },
67
- {"uint16[3]" , [3 ]uint32 {1 , 2 , 3 }, typeErr ( formatSliceString ( reflect . Uint16 , 3 ), formatSliceString ( reflect . Uint32 , 3 )) },
68
- {"uint16[3]" , [4 ]uint16 {1 , 2 , 3 }, typeErr ( formatSliceString ( reflect . Uint16 , 3 ), formatSliceString ( reflect . Uint16 , 4 )) },
69
- {"uint16[3]" , []uint16 {1 , 2 , 3 }, nil },
70
- {"uint16[3]" , []uint16 {1 , 2 , 3 , 4 }, typeErr ( formatSliceString ( reflect . Uint16 , 3 ), formatSliceString ( reflect . Uint16 , 4 )) },
71
- {"address[]" , []common.Address {common.Address {1 }}, nil },
72
- {"address[1]" , []common.Address {common.Address {1 }}, nil },
73
- {"address[1]" , [1 ]common.Address {common.Address {1 }}, nil },
74
- {"address[2]" , [1 ]common.Address {common.Address {1 }}, typeErr ( formatSliceString ( reflect . Array , 2 ), formatSliceString ( reflect . Array , 1 )) },
75
- {"bytes32" , [32 ]byte {}, nil },
76
- {"bytes32" , [33 ]byte {}, typeErr ( formatSliceString ( reflect . Uint8 , 32 ), formatSliceString ( reflect . Uint8 , 33 )) },
77
- {"bytes32" , common.Hash {1 }, nil },
78
- {"bytes31" , [31 ]byte {}, nil },
79
- {"bytes31" , [32 ]byte {}, typeErr ( formatSliceString ( reflect . Uint8 , 31 ), formatSliceString ( reflect . Uint8 , 32 )) },
80
- {"bytes" , []byte {0 , 1 }, nil },
81
- {"bytes" , [2 ]byte {0 , 1 }, nil },
82
- {"bytes" , common.Hash {1 }, nil },
83
- {"string" , "hello world" , nil },
84
- {"bytes32[]" , [][32 ]byte {[32 ]byte {}}, nil },
57
+ {"uint" , big .NewInt (1 ), "" },
58
+ {"int" , big .NewInt (1 ), "" },
59
+ {"uint30" , big .NewInt (1 ), "" },
60
+ {"uint30" , uint8 (1 ), "abi: cannot use uint8 as type ptr as argument" },
61
+ {"uint16" , uint16 (1 ), "" },
62
+ {"uint16" , uint8 (1 ), "abi: cannot use uint8 as type uint16 as argument" },
63
+ {"uint16[]" , []uint16 {1 , 2 , 3 }, "" },
64
+ {"uint16[]" , [3 ]uint16 {1 , 2 , 3 }, "" },
65
+ {"uint16[]" , []uint32 {1 , 2 , 3 }, "abi: cannot use []uint32 as type []uint16 as argument" },
66
+ {"uint16[3]" , [3 ]uint32 {1 , 2 , 3 }, "abi: cannot use [3]uint32 as type [3]uint16 as argument" },
67
+ {"uint16[3]" , [4 ]uint16 {1 , 2 , 3 }, "abi: cannot use [4]uint16 as type [3]uint16 as argument" },
68
+ {"uint16[3]" , []uint16 {1 , 2 , 3 }, "" },
69
+ {"uint16[3]" , []uint16 {1 , 2 , 3 , 4 }, "abi: cannot use [4]uint16 as type [3]uint16 as argument" },
70
+ {"address[]" , []common.Address {common.Address {1 }}, "" },
71
+ {"address[1]" , []common.Address {common.Address {1 }}, "" },
72
+ {"address[1]" , [1 ]common.Address {common.Address {1 }}, "" },
73
+ {"address[2]" , [1 ]common.Address {common.Address {1 }}, "abi: cannot use [1]array as type [2]array as argument" },
74
+ {"bytes32" , [32 ]byte {}, "" },
75
+ {"bytes32" , [33 ]byte {}, "abi: cannot use [33]uint8 as type [32]uint8 as argument" },
76
+ {"bytes32" , common.Hash {1 }, "" },
77
+ {"bytes31" , [31 ]byte {}, "" },
78
+ {"bytes31" , [32 ]byte {}, "abi: cannot use [32]uint8 as type [31]uint8 as argument" },
79
+ {"bytes" , []byte {0 , 1 }, "" },
80
+ {"bytes" , [2 ]byte {0 , 1 }, "" },
81
+ {"bytes" , common.Hash {1 }, "" },
82
+ {"string" , "hello world" , "" },
83
+ {"bytes32[]" , [][32 ]byte {[32 ]byte {}}, "" },
85
84
} {
86
85
typ , err := NewType (test .typ )
87
86
if err != nil {
88
87
t .Fatal ("unexpected parse error:" , err )
89
88
}
90
89
91
90
err = typeCheck (typ , reflect .ValueOf (test .input ))
92
- if err != nil && test .err == nil {
91
+ if err != nil && len ( test .err ) == 0 {
93
92
t .Errorf ("%d failed. Expected no err but got: %v" , i , err )
94
93
continue
95
94
}
96
- if err == nil && test .err != nil {
95
+ if err == nil && len ( test .err ) != 0 {
97
96
t .Errorf ("%d failed. Expected err: %v but got none" , i , test .err )
98
97
continue
99
98
}
100
99
101
- if err != nil && test .err != nil && err .Error () != test .err . Error () {
100
+ if err != nil && len ( test .err ) != 0 && err .Error () != test .err {
102
101
t .Errorf ("%d failed. Expected err: '%v' got err: '%v'" , i , test .err , err )
103
102
}
104
103
}
@@ -110,63 +109,93 @@ func TestSimpleMethodUnpack(t *testing.T) {
110
109
marshalledOutput []byte // evm return data
111
110
expectedOut interface {} // the expected output
112
111
outVar string // the output variable (e.g. uint32, *big.Int, etc)
113
- err error // nil or error if expected
112
+ err string // empty or error if expected
114
113
}{
115
114
{
116
115
`[ { "type": "uint32" } ]` ,
117
116
pad ([]byte {1 }, 32 , true ),
118
117
uint32 (1 ),
119
118
"uint32" ,
120
- nil ,
119
+ "" ,
121
120
},
122
121
{
123
122
`[ { "type": "uint32" } ]` ,
124
123
pad ([]byte {1 }, 32 , true ),
125
124
nil ,
126
125
"uint16" ,
127
- errors . New ( "abi: cannot unmarshal uint32 in to uint16" ) ,
126
+ "abi: cannot unmarshal uint32 in to uint16" ,
128
127
},
129
128
{
130
129
`[ { "type": "uint17" } ]` ,
131
130
pad ([]byte {1 }, 32 , true ),
132
131
nil ,
133
132
"uint16" ,
134
- errors . New ( "abi: cannot unmarshal *big.Int in to uint16" ) ,
133
+ "abi: cannot unmarshal *big.Int in to uint16" ,
135
134
},
136
135
{
137
136
`[ { "type": "uint17" } ]` ,
138
137
pad ([]byte {1 }, 32 , true ),
139
138
big .NewInt (1 ),
140
139
"*big.Int" ,
140
+ "" ,
141
+ },
142
+
143
+ {
144
+ `[ { "type": "int32" } ]` ,
145
+ pad ([]byte {1 }, 32 , true ),
146
+ int32 (1 ),
147
+ "int32" ,
148
+ "" ,
149
+ },
150
+ {
151
+ `[ { "type": "int32" } ]` ,
152
+ pad ([]byte {1 }, 32 , true ),
141
153
nil ,
154
+ "int16" ,
155
+ "abi: cannot unmarshal int32 in to int16" ,
156
+ },
157
+ {
158
+ `[ { "type": "int17" } ]` ,
159
+ pad ([]byte {1 }, 32 , true ),
160
+ nil ,
161
+ "int16" ,
162
+ "abi: cannot unmarshal *big.Int in to int16" ,
163
+ },
164
+ {
165
+ `[ { "type": "int17" } ]` ,
166
+ pad ([]byte {1 }, 32 , true ),
167
+ big .NewInt (1 ),
168
+ "*big.Int" ,
169
+ "" ,
142
170
},
171
+
143
172
{
144
173
`[ { "type": "address" } ]` ,
145
174
pad (pad ([]byte {1 }, 20 , false ), 32 , true ),
146
175
common.Address {1 },
147
176
"address" ,
148
- nil ,
177
+ "" ,
149
178
},
150
179
{
151
180
`[ { "type": "bytes32" } ]` ,
152
181
pad ([]byte {1 }, 32 , false ),
153
182
pad ([]byte {1 }, 32 , false ),
154
183
"bytes" ,
155
- nil ,
184
+ "" ,
156
185
},
157
186
{
158
187
`[ { "type": "bytes32" } ]` ,
159
188
pad ([]byte {1 }, 32 , false ),
160
189
pad ([]byte {1 }, 32 , false ),
161
190
"hash" ,
162
- nil ,
191
+ "" ,
163
192
},
164
193
{
165
194
`[ { "type": "bytes32" } ]` ,
166
195
pad ([]byte {1 }, 32 , false ),
167
196
pad ([]byte {1 }, 32 , false ),
168
197
"interface" ,
169
- nil ,
198
+ "" ,
170
199
},
171
200
} {
172
201
abiDefinition := fmt .Sprintf (`[{ "name" : "method", "outputs": %s}]` , test .def )
@@ -194,6 +223,22 @@ func TestSimpleMethodUnpack(t *testing.T) {
194
223
var v uint64
195
224
err = abi .Unpack (& v , "method" , test .marshalledOutput )
196
225
outvar = v
226
+ case "int8" :
227
+ var v int8
228
+ err = abi .Unpack (& v , "method" , test .marshalledOutput )
229
+ outvar = v
230
+ case "int16" :
231
+ var v int16
232
+ err = abi .Unpack (& v , "method" , test .marshalledOutput )
233
+ outvar = v
234
+ case "int32" :
235
+ var v int32
236
+ err = abi .Unpack (& v , "method" , test .marshalledOutput )
237
+ outvar = v
238
+ case "int64" :
239
+ var v int64
240
+ err = abi .Unpack (& v , "method" , test .marshalledOutput )
241
+ outvar = v
197
242
case "*big.Int" :
198
243
var v * big.Int
199
244
err = abi .Unpack (& v , "method" , test .marshalledOutput )
@@ -217,15 +262,15 @@ func TestSimpleMethodUnpack(t *testing.T) {
217
262
continue
218
263
}
219
264
220
- if err != nil && test .err == nil {
265
+ if err != nil && len ( test .err ) == 0 {
221
266
t .Errorf ("%d failed. Expected no err but got: %v" , i , err )
222
267
continue
223
268
}
224
- if err == nil && test .err != nil {
269
+ if err == nil && len ( test .err ) != 0 {
225
270
t .Errorf ("%d failed. Expected err: %v but got none" , i , test .err )
226
271
continue
227
272
}
228
- if err != nil && test .err != nil && err .Error () != test .err . Error () {
273
+ if err != nil && len ( test .err ) != 0 && err .Error () != test .err {
229
274
t .Errorf ("%d failed. Expected err: '%v' got err: '%v'" , i , test .err , err )
230
275
continue
231
276
}
@@ -253,6 +298,7 @@ func TestPack(t *testing.T) {
253
298
}{
254
299
{"uint16" , uint16 (2 ), pad ([]byte {2 }, 32 , true )},
255
300
{"uint16[]" , []uint16 {1 , 2 }, formatSliceOutput ([]byte {1 }, []byte {2 })},
301
+ {"bytes20" , [20 ]byte {1 }, pad ([]byte {1 }, 32 , false )},
256
302
{"uint256[]" , []* big.Int {big .NewInt (1 ), big .NewInt (2 )}, formatSliceOutput ([]byte {1 }, []byte {2 })},
257
303
{"address[]" , []common.Address {common.Address {1 }, common.Address {2 }}, formatSliceOutput (pad ([]byte {1 }, 20 , false ), pad ([]byte {2 }, 20 , false ))},
258
304
{"bytes32[]" , []common.Hash {common.Hash {1 }, common.Hash {2 }}, formatSliceOutput (pad ([]byte {1 }, 32 , false ), pad ([]byte {2 }, 32 , false ))},
@@ -346,26 +392,26 @@ func TestMethodPack(t *testing.T) {
346
392
347
393
const jsondata = `
348
394
[
349
- { "type" : "function", "name" : "balance", "const " : true },
350
- { "type" : "function", "name" : "send", "const " : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
395
+ { "type" : "function", "name" : "balance", "constant " : true },
396
+ { "type" : "function", "name" : "send", "constant " : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
351
397
]`
352
398
353
399
const jsondata2 = `
354
400
[
355
- { "type" : "function", "name" : "balance", "const " : true },
356
- { "type" : "function", "name" : "send", "const " : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
357
- { "type" : "function", "name" : "test", "const " : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
358
- { "type" : "function", "name" : "string", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
359
- { "type" : "function", "name" : "bool", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
360
- { "type" : "function", "name" : "address", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
361
- { "type" : "function", "name" : "uint64[2]", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
362
- { "type" : "function", "name" : "uint64[]", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
363
- { "type" : "function", "name" : "foo", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
364
- { "type" : "function", "name" : "bar", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
365
- { "type" : "function", "name" : "slice", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
366
- { "type" : "function", "name" : "slice256", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
367
- { "type" : "function", "name" : "sliceAddress", "const " : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
368
- { "type" : "function", "name" : "sliceMultiAddress", "const " : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
401
+ { "type" : "function", "name" : "balance", "constant " : true },
402
+ { "type" : "function", "name" : "send", "constant " : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
403
+ { "type" : "function", "name" : "test", "constant " : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
404
+ { "type" : "function", "name" : "string", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
405
+ { "type" : "function", "name" : "bool", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
406
+ { "type" : "function", "name" : "address", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
407
+ { "type" : "function", "name" : "uint64[2]", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
408
+ { "type" : "function", "name" : "uint64[]", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
409
+ { "type" : "function", "name" : "foo", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
410
+ { "type" : "function", "name" : "bar", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
411
+ { "type" : "function", "name" : "slice", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
412
+ { "type" : "function", "name" : "slice256", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
413
+ { "type" : "function", "name" : "sliceAddress", "constant " : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
414
+ { "type" : "function", "name" : "sliceMultiAddress", "constant " : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
369
415
]`
370
416
371
417
func TestReader (t * testing.T ) {
@@ -537,9 +583,9 @@ func ExampleJSON() {
537
583
538
584
func TestInputVariableInputLength (t * testing.T ) {
539
585
const definition = `[
540
- { "type" : "function", "name" : "strOne", "const " : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
541
- { "type" : "function", "name" : "bytesOne", "const " : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
542
- { "type" : "function", "name" : "strTwo", "const " : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
586
+ { "type" : "function", "name" : "strOne", "constant " : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
587
+ { "type" : "function", "name" : "bytesOne", "constant " : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
588
+ { "type" : "function", "name" : "strTwo", "constant " : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
543
589
]`
544
590
545
591
abi , err := JSON (strings .NewReader (definition ))
@@ -701,7 +747,7 @@ func TestBareEvents(t *testing.T) {
701
747
702
748
func TestMultiReturnWithStruct (t * testing.T ) {
703
749
const definition = `[
704
- { "name" : "multi", "const " : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
750
+ { "name" : "multi", "constant " : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
705
751
706
752
abi , err := JSON (strings .NewReader (definition ))
707
753
if err != nil {
@@ -754,7 +800,7 @@ func TestMultiReturnWithStruct(t *testing.T) {
754
800
755
801
func TestMultiReturnWithSlice (t * testing.T ) {
756
802
const definition = `[
757
- { "name" : "multi", "const " : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
803
+ { "name" : "multi", "constant " : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
758
804
759
805
abi , err := JSON (strings .NewReader (definition ))
760
806
if err != nil {
@@ -790,8 +836,8 @@ func TestMultiReturnWithSlice(t *testing.T) {
790
836
791
837
func TestMarshalArrays (t * testing.T ) {
792
838
const definition = `[
793
- { "name" : "bytes32", "const " : false, "outputs": [ { "type": "bytes32" } ] },
794
- { "name" : "bytes10", "const " : false, "outputs": [ { "type": "bytes10" } ] }
839
+ { "name" : "bytes32", "constant " : false, "outputs": [ { "type": "bytes32" } ] },
840
+ { "name" : "bytes10", "constant " : false, "outputs": [ { "type": "bytes10" } ] }
795
841
]`
796
842
797
843
abi , err := JSON (strings .NewReader (definition ))
@@ -849,14 +895,14 @@ func TestMarshalArrays(t *testing.T) {
849
895
850
896
func TestUnmarshal (t * testing.T ) {
851
897
const definition = `[
852
- { "name" : "int", "const " : false, "outputs": [ { "type": "uint256" } ] },
853
- { "name" : "bool", "const " : false, "outputs": [ { "type": "bool" } ] },
854
- { "name" : "bytes", "const " : false, "outputs": [ { "type": "bytes" } ] },
855
- { "name" : "fixed", "const " : false, "outputs": [ { "type": "bytes32" } ] },
856
- { "name" : "multi", "const " : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
857
- { "name" : "addressSliceSingle", "const " : false, "outputs": [ { "type": "address[]" } ] },
858
- { "name" : "addressSliceDouble", "const " : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
859
- { "name" : "mixedBytes", "const " : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
898
+ { "name" : "int", "constant " : false, "outputs": [ { "type": "uint256" } ] },
899
+ { "name" : "bool", "constant " : false, "outputs": [ { "type": "bool" } ] },
900
+ { "name" : "bytes", "constant " : false, "outputs": [ { "type": "bytes" } ] },
901
+ { "name" : "fixed", "constant " : false, "outputs": [ { "type": "bytes32" } ] },
902
+ { "name" : "multi", "constant " : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
903
+ { "name" : "addressSliceSingle", "constant " : false, "outputs": [ { "type": "address[]" } ] },
904
+ { "name" : "addressSliceDouble", "constant " : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
905
+ { "name" : "mixedBytes", "constant " : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
860
906
861
907
abi , err := JSON (strings .NewReader (definition ))
862
908
if err != nil {
0 commit comments