github.com/luckypickle/go-ethereum-vet@v1.14.2/accounts/abi/abi_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package abi 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "fmt" 23 "log" 24 "math/big" 25 "strings" 26 "testing" 27 28 "reflect" 29 30 "github.com/luckypickle/go-ethereum-vet/common" 31 "github.com/luckypickle/go-ethereum-vet/crypto" 32 ) 33 34 const jsondata = ` 35 [ 36 { "type" : "function", "name" : "balance", "constant" : true }, 37 { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] } 38 ]` 39 40 const jsondata2 = ` 41 [ 42 { "type" : "function", "name" : "balance", "constant" : true }, 43 { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, 44 { "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] }, 45 { "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] }, 46 { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] }, 47 { "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] }, 48 { "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] }, 49 { "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] }, 50 { "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] }, 51 { "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] }, 52 { "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] }, 53 { "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }, 54 { "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[]" } ] } 56 ]` 57 58 func TestReader(t *testing.T) { 59 Uint256, _ := NewType("uint256") 60 exp := ABI{ 61 Methods: map[string]Method{ 62 "balance": { 63 "balance", true, nil, nil, 64 }, 65 "send": { 66 "send", false, []Argument{ 67 {"amount", Uint256, false}, 68 }, nil, 69 }, 70 }, 71 } 72 73 abi, err := JSON(strings.NewReader(jsondata)) 74 if err != nil { 75 t.Error(err) 76 } 77 78 // deep equal fails for some reason 79 for name, expM := range exp.Methods { 80 gotM, exist := abi.Methods[name] 81 if !exist { 82 t.Errorf("Missing expected method %v", name) 83 } 84 if !reflect.DeepEqual(gotM, expM) { 85 t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM) 86 } 87 } 88 89 for name, gotM := range abi.Methods { 90 expM, exist := exp.Methods[name] 91 if !exist { 92 t.Errorf("Found extra method %v", name) 93 } 94 if !reflect.DeepEqual(gotM, expM) { 95 t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM) 96 } 97 } 98 } 99 100 func TestTestNumbers(t *testing.T) { 101 abi, err := JSON(strings.NewReader(jsondata2)) 102 if err != nil { 103 t.Error(err) 104 t.FailNow() 105 } 106 107 if _, err := abi.Pack("balance"); err != nil { 108 t.Error(err) 109 } 110 111 if _, err := abi.Pack("balance", 1); err == nil { 112 t.Error("expected error for balance(1)") 113 } 114 115 if _, err := abi.Pack("doesntexist", nil); err == nil { 116 t.Errorf("doesntexist shouldn't exist") 117 } 118 119 if _, err := abi.Pack("doesntexist", 1); err == nil { 120 t.Errorf("doesntexist(1) shouldn't exist") 121 } 122 123 if _, err := abi.Pack("send", big.NewInt(1000)); err != nil { 124 t.Error(err) 125 } 126 127 i := new(int) 128 *i = 1000 129 if _, err := abi.Pack("send", i); err == nil { 130 t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int") 131 } 132 133 if _, err := abi.Pack("test", uint32(1000)); err != nil { 134 t.Error(err) 135 } 136 } 137 138 func TestTestString(t *testing.T) { 139 abi, err := JSON(strings.NewReader(jsondata2)) 140 if err != nil { 141 t.Error(err) 142 t.FailNow() 143 } 144 145 if _, err := abi.Pack("string", "hello world"); err != nil { 146 t.Error(err) 147 } 148 } 149 150 func TestTestBool(t *testing.T) { 151 abi, err := JSON(strings.NewReader(jsondata2)) 152 if err != nil { 153 t.Error(err) 154 t.FailNow() 155 } 156 157 if _, err := abi.Pack("bool", true); err != nil { 158 t.Error(err) 159 } 160 } 161 162 func TestTestSlice(t *testing.T) { 163 abi, err := JSON(strings.NewReader(jsondata2)) 164 if err != nil { 165 t.Error(err) 166 t.FailNow() 167 } 168 169 slice := make([]uint64, 2) 170 if _, err := abi.Pack("uint64[2]", slice); err != nil { 171 t.Error(err) 172 } 173 174 if _, err := abi.Pack("uint64[]", slice); err != nil { 175 t.Error(err) 176 } 177 } 178 179 func TestMethodSignature(t *testing.T) { 180 String, _ := NewType("string") 181 m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil} 182 exp := "foo(string,string)" 183 if m.Sig() != exp { 184 t.Error("signature mismatch", exp, "!=", m.Sig()) 185 } 186 187 idexp := crypto.Keccak256([]byte(exp))[:4] 188 if !bytes.Equal(m.Id(), idexp) { 189 t.Errorf("expected ids to match %x != %x", m.Id(), idexp) 190 } 191 192 uintt, _ := NewType("uint256") 193 m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil} 194 exp = "foo(uint256)" 195 if m.Sig() != exp { 196 t.Error("signature mismatch", exp, "!=", m.Sig()) 197 } 198 } 199 200 func TestMultiPack(t *testing.T) { 201 abi, err := JSON(strings.NewReader(jsondata2)) 202 if err != nil { 203 t.Error(err) 204 t.FailNow() 205 } 206 207 sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4] 208 sig = append(sig, make([]byte, 64)...) 209 sig[35] = 10 210 sig[67] = 11 211 212 packed, err := abi.Pack("bar", uint32(10), uint16(11)) 213 if err != nil { 214 t.Error(err) 215 t.FailNow() 216 } 217 218 if !bytes.Equal(packed, sig) { 219 t.Errorf("expected %x got %x", sig, packed) 220 } 221 } 222 223 func ExampleJSON() { 224 const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]` 225 226 abi, err := JSON(strings.NewReader(definition)) 227 if err != nil { 228 log.Fatalln(err) 229 } 230 out, err := abi.Pack("isBar", common.HexToAddress("01")) 231 if err != nil { 232 log.Fatalln(err) 233 } 234 235 fmt.Printf("%x\n", out) 236 // Output: 237 // 1f2c40920000000000000000000000000000000000000000000000000000000000000001 238 } 239 240 func TestInputVariableInputLength(t *testing.T) { 241 const definition = `[ 242 { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] }, 243 { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] }, 244 { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] } 245 ]` 246 247 abi, err := JSON(strings.NewReader(definition)) 248 if err != nil { 249 t.Fatal(err) 250 } 251 252 // test one string 253 strin := "hello world" 254 strpack, err := abi.Pack("strOne", strin) 255 if err != nil { 256 t.Error(err) 257 } 258 259 offset := make([]byte, 32) 260 offset[31] = 32 261 length := make([]byte, 32) 262 length[31] = byte(len(strin)) 263 value := common.RightPadBytes([]byte(strin), 32) 264 exp := append(offset, append(length, value...)...) 265 266 // ignore first 4 bytes of the output. This is the function identifier 267 strpack = strpack[4:] 268 if !bytes.Equal(strpack, exp) { 269 t.Errorf("expected %x, got %x\n", exp, strpack) 270 } 271 272 // test one bytes 273 btspack, err := abi.Pack("bytesOne", []byte(strin)) 274 if err != nil { 275 t.Error(err) 276 } 277 // ignore first 4 bytes of the output. This is the function identifier 278 btspack = btspack[4:] 279 if !bytes.Equal(btspack, exp) { 280 t.Errorf("expected %x, got %x\n", exp, btspack) 281 } 282 283 // test two strings 284 str1 := "hello" 285 str2 := "world" 286 str2pack, err := abi.Pack("strTwo", str1, str2) 287 if err != nil { 288 t.Error(err) 289 } 290 291 offset1 := make([]byte, 32) 292 offset1[31] = 64 293 length1 := make([]byte, 32) 294 length1[31] = byte(len(str1)) 295 value1 := common.RightPadBytes([]byte(str1), 32) 296 297 offset2 := make([]byte, 32) 298 offset2[31] = 128 299 length2 := make([]byte, 32) 300 length2[31] = byte(len(str2)) 301 value2 := common.RightPadBytes([]byte(str2), 32) 302 303 exp2 := append(offset1, offset2...) 304 exp2 = append(exp2, append(length1, value1...)...) 305 exp2 = append(exp2, append(length2, value2...)...) 306 307 // ignore first 4 bytes of the output. This is the function identifier 308 str2pack = str2pack[4:] 309 if !bytes.Equal(str2pack, exp2) { 310 t.Errorf("expected %x, got %x\n", exp, str2pack) 311 } 312 313 // test two strings, first > 32, second < 32 314 str1 = strings.Repeat("a", 33) 315 str2pack, err = abi.Pack("strTwo", str1, str2) 316 if err != nil { 317 t.Error(err) 318 } 319 320 offset1 = make([]byte, 32) 321 offset1[31] = 64 322 length1 = make([]byte, 32) 323 length1[31] = byte(len(str1)) 324 value1 = common.RightPadBytes([]byte(str1), 64) 325 offset2[31] = 160 326 327 exp2 = append(offset1, offset2...) 328 exp2 = append(exp2, append(length1, value1...)...) 329 exp2 = append(exp2, append(length2, value2...)...) 330 331 // ignore first 4 bytes of the output. This is the function identifier 332 str2pack = str2pack[4:] 333 if !bytes.Equal(str2pack, exp2) { 334 t.Errorf("expected %x, got %x\n", exp, str2pack) 335 } 336 337 // test two strings, first > 32, second >32 338 str1 = strings.Repeat("a", 33) 339 str2 = strings.Repeat("a", 33) 340 str2pack, err = abi.Pack("strTwo", str1, str2) 341 if err != nil { 342 t.Error(err) 343 } 344 345 offset1 = make([]byte, 32) 346 offset1[31] = 64 347 length1 = make([]byte, 32) 348 length1[31] = byte(len(str1)) 349 value1 = common.RightPadBytes([]byte(str1), 64) 350 351 offset2 = make([]byte, 32) 352 offset2[31] = 160 353 length2 = make([]byte, 32) 354 length2[31] = byte(len(str2)) 355 value2 = common.RightPadBytes([]byte(str2), 64) 356 357 exp2 = append(offset1, offset2...) 358 exp2 = append(exp2, append(length1, value1...)...) 359 exp2 = append(exp2, append(length2, value2...)...) 360 361 // ignore first 4 bytes of the output. This is the function identifier 362 str2pack = str2pack[4:] 363 if !bytes.Equal(str2pack, exp2) { 364 t.Errorf("expected %x, got %x\n", exp, str2pack) 365 } 366 } 367 368 func TestInputFixedArrayAndVariableInputLength(t *testing.T) { 369 const definition = `[ 370 { "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] }, 371 { "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] }, 372 { "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] }, 373 { "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] }, 374 { "type" : "function", "name" : "multipleMixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] } 375 ]` 376 377 abi, err := JSON(strings.NewReader(definition)) 378 if err != nil { 379 t.Error(err) 380 } 381 382 // test string, fixed array uint256[2] 383 strin := "hello world" 384 arrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)} 385 fixedArrStrPack, err := abi.Pack("fixedArrStr", strin, arrin) 386 if err != nil { 387 t.Error(err) 388 } 389 390 // generate expected output 391 offset := make([]byte, 32) 392 offset[31] = 96 393 length := make([]byte, 32) 394 length[31] = byte(len(strin)) 395 strvalue := common.RightPadBytes([]byte(strin), 32) 396 arrinvalue1 := common.LeftPadBytes(arrin[0].Bytes(), 32) 397 arrinvalue2 := common.LeftPadBytes(arrin[1].Bytes(), 32) 398 exp := append(offset, arrinvalue1...) 399 exp = append(exp, arrinvalue2...) 400 exp = append(exp, append(length, strvalue...)...) 401 402 // ignore first 4 bytes of the output. This is the function identifier 403 fixedArrStrPack = fixedArrStrPack[4:] 404 if !bytes.Equal(fixedArrStrPack, exp) { 405 t.Errorf("expected %x, got %x\n", exp, fixedArrStrPack) 406 } 407 408 // test byte array, fixed array uint256[2] 409 bytesin := []byte(strin) 410 arrin = [2]*big.Int{big.NewInt(1), big.NewInt(2)} 411 fixedArrBytesPack, err := abi.Pack("fixedArrBytes", bytesin, arrin) 412 if err != nil { 413 t.Error(err) 414 } 415 416 // generate expected output 417 offset = make([]byte, 32) 418 offset[31] = 96 419 length = make([]byte, 32) 420 length[31] = byte(len(strin)) 421 strvalue = common.RightPadBytes([]byte(strin), 32) 422 arrinvalue1 = common.LeftPadBytes(arrin[0].Bytes(), 32) 423 arrinvalue2 = common.LeftPadBytes(arrin[1].Bytes(), 32) 424 exp = append(offset, arrinvalue1...) 425 exp = append(exp, arrinvalue2...) 426 exp = append(exp, append(length, strvalue...)...) 427 428 // ignore first 4 bytes of the output. This is the function identifier 429 fixedArrBytesPack = fixedArrBytesPack[4:] 430 if !bytes.Equal(fixedArrBytesPack, exp) { 431 t.Errorf("expected %x, got %x\n", exp, fixedArrBytesPack) 432 } 433 434 // test string, fixed array uint256[2], dynamic array uint256[] 435 strin = "hello world" 436 fixedarrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)} 437 dynarrin := []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)} 438 mixedArrStrPack, err := abi.Pack("mixedArrStr", strin, fixedarrin, dynarrin) 439 if err != nil { 440 t.Error(err) 441 } 442 443 // generate expected output 444 stroffset := make([]byte, 32) 445 stroffset[31] = 128 446 strlength := make([]byte, 32) 447 strlength[31] = byte(len(strin)) 448 strvalue = common.RightPadBytes([]byte(strin), 32) 449 fixedarrinvalue1 := common.LeftPadBytes(fixedarrin[0].Bytes(), 32) 450 fixedarrinvalue2 := common.LeftPadBytes(fixedarrin[1].Bytes(), 32) 451 dynarroffset := make([]byte, 32) 452 dynarroffset[31] = byte(160 + ((len(strin)/32)+1)*32) 453 dynarrlength := make([]byte, 32) 454 dynarrlength[31] = byte(len(dynarrin)) 455 dynarrinvalue1 := common.LeftPadBytes(dynarrin[0].Bytes(), 32) 456 dynarrinvalue2 := common.LeftPadBytes(dynarrin[1].Bytes(), 32) 457 dynarrinvalue3 := common.LeftPadBytes(dynarrin[2].Bytes(), 32) 458 exp = append(stroffset, fixedarrinvalue1...) 459 exp = append(exp, fixedarrinvalue2...) 460 exp = append(exp, dynarroffset...) 461 exp = append(exp, append(strlength, strvalue...)...) 462 dynarrarg := append(dynarrlength, dynarrinvalue1...) 463 dynarrarg = append(dynarrarg, dynarrinvalue2...) 464 dynarrarg = append(dynarrarg, dynarrinvalue3...) 465 exp = append(exp, dynarrarg...) 466 467 // ignore first 4 bytes of the output. This is the function identifier 468 mixedArrStrPack = mixedArrStrPack[4:] 469 if !bytes.Equal(mixedArrStrPack, exp) { 470 t.Errorf("expected %x, got %x\n", exp, mixedArrStrPack) 471 } 472 473 // test string, fixed array uint256[2], fixed array uint256[3] 474 strin = "hello world" 475 fixedarrin1 := [2]*big.Int{big.NewInt(1), big.NewInt(2)} 476 fixedarrin2 := [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)} 477 doubleFixedArrStrPack, err := abi.Pack("doubleFixedArrStr", strin, fixedarrin1, fixedarrin2) 478 if err != nil { 479 t.Error(err) 480 } 481 482 // generate expected output 483 stroffset = make([]byte, 32) 484 stroffset[31] = 192 485 strlength = make([]byte, 32) 486 strlength[31] = byte(len(strin)) 487 strvalue = common.RightPadBytes([]byte(strin), 32) 488 fixedarrin1value1 := common.LeftPadBytes(fixedarrin1[0].Bytes(), 32) 489 fixedarrin1value2 := common.LeftPadBytes(fixedarrin1[1].Bytes(), 32) 490 fixedarrin2value1 := common.LeftPadBytes(fixedarrin2[0].Bytes(), 32) 491 fixedarrin2value2 := common.LeftPadBytes(fixedarrin2[1].Bytes(), 32) 492 fixedarrin2value3 := common.LeftPadBytes(fixedarrin2[2].Bytes(), 32) 493 exp = append(stroffset, fixedarrin1value1...) 494 exp = append(exp, fixedarrin1value2...) 495 exp = append(exp, fixedarrin2value1...) 496 exp = append(exp, fixedarrin2value2...) 497 exp = append(exp, fixedarrin2value3...) 498 exp = append(exp, append(strlength, strvalue...)...) 499 500 // ignore first 4 bytes of the output. This is the function identifier 501 doubleFixedArrStrPack = doubleFixedArrStrPack[4:] 502 if !bytes.Equal(doubleFixedArrStrPack, exp) { 503 t.Errorf("expected %x, got %x\n", exp, doubleFixedArrStrPack) 504 } 505 506 // test string, fixed array uint256[2], dynamic array uint256[], fixed array uint256[3] 507 strin = "hello world" 508 fixedarrin1 = [2]*big.Int{big.NewInt(1), big.NewInt(2)} 509 dynarrin = []*big.Int{big.NewInt(1), big.NewInt(2)} 510 fixedarrin2 = [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)} 511 multipleMixedArrStrPack, err := abi.Pack("multipleMixedArrStr", strin, fixedarrin1, dynarrin, fixedarrin2) 512 if err != nil { 513 t.Error(err) 514 } 515 516 // generate expected output 517 stroffset = make([]byte, 32) 518 stroffset[31] = 224 519 strlength = make([]byte, 32) 520 strlength[31] = byte(len(strin)) 521 strvalue = common.RightPadBytes([]byte(strin), 32) 522 fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32) 523 fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32) 524 dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32))) 525 dynarrlength = make([]byte, 32) 526 dynarrlength[31] = byte(len(dynarrin)) 527 dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32) 528 dynarrinvalue2 = common.LeftPadBytes(dynarrin[1].Bytes(), 32) 529 fixedarrin2value1 = common.LeftPadBytes(fixedarrin2[0].Bytes(), 32) 530 fixedarrin2value2 = common.LeftPadBytes(fixedarrin2[1].Bytes(), 32) 531 fixedarrin2value3 = common.LeftPadBytes(fixedarrin2[2].Bytes(), 32) 532 exp = append(stroffset, fixedarrin1value1...) 533 exp = append(exp, fixedarrin1value2...) 534 exp = append(exp, dynarroffset...) 535 exp = append(exp, fixedarrin2value1...) 536 exp = append(exp, fixedarrin2value2...) 537 exp = append(exp, fixedarrin2value3...) 538 exp = append(exp, append(strlength, strvalue...)...) 539 dynarrarg = append(dynarrlength, dynarrinvalue1...) 540 dynarrarg = append(dynarrarg, dynarrinvalue2...) 541 exp = append(exp, dynarrarg...) 542 543 // ignore first 4 bytes of the output. This is the function identifier 544 multipleMixedArrStrPack = multipleMixedArrStrPack[4:] 545 if !bytes.Equal(multipleMixedArrStrPack, exp) { 546 t.Errorf("expected %x, got %x\n", exp, multipleMixedArrStrPack) 547 } 548 } 549 550 func TestDefaultFunctionParsing(t *testing.T) { 551 const definition = `[{ "name" : "balance" }]` 552 553 abi, err := JSON(strings.NewReader(definition)) 554 if err != nil { 555 t.Fatal(err) 556 } 557 558 if _, ok := abi.Methods["balance"]; !ok { 559 t.Error("expected 'balance' to be present") 560 } 561 } 562 563 func TestBareEvents(t *testing.T) { 564 const definition = `[ 565 { "type" : "event", "name" : "balance" }, 566 { "type" : "event", "name" : "anon", "anonymous" : true}, 567 { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] } 568 ]` 569 570 arg0, _ := NewType("uint256") 571 arg1, _ := NewType("address") 572 573 expectedEvents := map[string]struct { 574 Anonymous bool 575 Args []Argument 576 }{ 577 "balance": {false, nil}, 578 "anon": {true, nil}, 579 "args": {false, []Argument{ 580 {Name: "arg0", Type: arg0, Indexed: false}, 581 {Name: "arg1", Type: arg1, Indexed: true}, 582 }}, 583 } 584 585 abi, err := JSON(strings.NewReader(definition)) 586 if err != nil { 587 t.Fatal(err) 588 } 589 590 if len(abi.Events) != len(expectedEvents) { 591 t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events)) 592 } 593 594 for name, exp := range expectedEvents { 595 got, ok := abi.Events[name] 596 if !ok { 597 t.Errorf("could not found event %s", name) 598 continue 599 } 600 if got.Anonymous != exp.Anonymous { 601 t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous) 602 } 603 if len(got.Inputs) != len(exp.Args) { 604 t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs)) 605 continue 606 } 607 for i, arg := range exp.Args { 608 if arg.Name != got.Inputs[i].Name { 609 t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name) 610 } 611 if arg.Indexed != got.Inputs[i].Indexed { 612 t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed) 613 } 614 if arg.Type.T != got.Inputs[i].Type.T { 615 t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T) 616 } 617 } 618 } 619 } 620 621 // TestUnpackEvent is based on this contract: 622 // 623 // contract T { 624 // event received(address sender, uint amount, bytes memo); 625 // event receivedAddr(address sender); 626 // function receive(bytes memo) external payable { 627 // received(msg.sender, msg.value, memo); 628 // receivedAddr(msg.sender); 629 // } 630 // } 631 // 632 // When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt: 633 // 634 // receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]} 635 func TestUnpackEvent(t *testing.T) { 636 const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]` 637 abi, err := JSON(strings.NewReader(abiJSON)) 638 if err != nil { 639 t.Fatal(err) 640 } 641 642 const hexdata = `000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158` 643 data, err := hex.DecodeString(hexdata) 644 if err != nil { 645 t.Fatal(err) 646 } 647 if len(data)%32 == 0 { 648 t.Errorf("len(data) is %d, want a non-multiple of 32", len(data)) 649 } 650 651 type ReceivedEvent struct { 652 Address common.Address 653 Amount *big.Int 654 Memo []byte 655 } 656 var ev ReceivedEvent 657 658 err = abi.Unpack(&ev, "received", data) 659 if err != nil { 660 t.Error(err) 661 } else { 662 t.Logf("len(data): %d; received event: %+v", len(data), ev) 663 } 664 665 type ReceivedAddrEvent struct { 666 Address common.Address 667 } 668 var receivedAddrEv ReceivedAddrEvent 669 err = abi.Unpack(&receivedAddrEv, "receivedAddr", data) 670 if err != nil { 671 t.Error(err) 672 } else { 673 t.Logf("len(data): %d; received event: %+v", len(data), receivedAddrEv) 674 } 675 } 676 677 func TestABI_MethodById(t *testing.T) { 678 const abiJSON = `[ 679 {"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"}, 680 {"type":"event","name":"received","anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}]}, 681 {"type":"function","name":"fixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"}]}, 682 {"type":"function","name":"fixedArrBytes","constant":true,"inputs":[{"name":"str","type":"bytes"},{"name":"fixedArr","type":"uint256[2]"}]}, 683 {"type":"function","name":"mixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"}]}, 684 {"type":"function","name":"doubleFixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"fixedArr2","type":"uint256[3]"}]}, 685 {"type":"function","name":"multipleMixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"},{"name":"fixedArr2","type":"uint256[3]"}]}, 686 {"type":"function","name":"balance","constant":true}, 687 {"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]}, 688 {"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]}, 689 {"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]}, 690 {"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]}, 691 {"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]}, 692 {"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]}, 693 {"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]}, 694 {"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]}, 695 {"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]}, 696 {"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]}, 697 {"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]}, 698 {"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]}, 699 {"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]} 700 ] 701 ` 702 abi, err := JSON(strings.NewReader(abiJSON)) 703 if err != nil { 704 t.Fatal(err) 705 } 706 for name, m := range abi.Methods { 707 a := fmt.Sprintf("%v", m) 708 m2, err := abi.MethodById(m.Id()) 709 if err != nil { 710 t.Fatalf("Failed to look up ABI method: %v", err) 711 } 712 b := fmt.Sprintf("%v", m2) 713 if a != b { 714 t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id())) 715 } 716 } 717 718 }