diff options
author | Jeffrey Wilcke <geffobscura@gmail.com> | 2015-10-29 05:57:21 +0800 |
---|---|---|
committer | Jeffrey Wilcke <geffobscura@gmail.com> | 2015-10-30 04:40:18 +0800 |
commit | 1f72952f043e9df3df71eb27a6ed7c6bef93a3f2 (patch) | |
tree | 48753e3b5bb1d2e2105b6764b111fd5f2304a66b /accounts/abi/abi_test.go | |
parent | 6b5a42a15ca54749d41c0b29b4a26ebb3a1a53f0 (diff) | |
download | dexon-1f72952f043e9df3df71eb27a6ed7c6bef93a3f2.tar.gz dexon-1f72952f043e9df3df71eb27a6ed7c6bef93a3f2.tar.zst dexon-1f72952f043e9df3df71eb27a6ed7c6bef93a3f2.zip |
accounts/abi: ABI fixes & added types
Changed field `input` to new `inputs`. Addad Hash and Address as input
types.
Added bytes[N] and N validation
Diffstat (limited to 'accounts/abi/abi_test.go')
-rw-r--r-- | accounts/abi/abi_test.go | 75 |
1 files changed, 62 insertions, 13 deletions
diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index 7706de05d..96dd3ee62 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -18,35 +18,38 @@ package abi import ( "bytes" + "fmt" + "log" "math/big" "reflect" "strings" "testing" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) const jsondata = ` [ { "name" : "balance", "const" : true }, - { "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] } + { "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] } ]` const jsondata2 = ` [ { "name" : "balance", "const" : true }, - { "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] }, - { "name" : "test", "const" : false, "input" : [ { "name" : "number", "type" : "uint32" } ] }, - { "name" : "string", "const" : false, "input" : [ { "name" : "input", "type" : "string" } ] }, - { "name" : "bool", "const" : false, "input" : [ { "name" : "input", "type" : "bool" } ] }, - { "name" : "address", "const" : false, "input" : [ { "name" : "input", "type" : "address" } ] }, - { "name" : "string32", "const" : false, "input" : [ { "name" : "input", "type" : "string32" } ] }, - { "name" : "uint64[2]", "const" : false, "input" : [ { "name" : "input", "type" : "uint64[2]" } ] }, - { "name" : "uint64[]", "const" : false, "input" : [ { "name" : "input", "type" : "uint64[]" } ] }, - { "name" : "foo", "const" : false, "input" : [ { "name" : "input", "type" : "uint32" } ] }, - { "name" : "bar", "const" : false, "input" : [ { "name" : "input", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] }, - { "name" : "slice", "const" : false, "input" : [ { "name" : "input", "type" : "uint32[2]" } ] }, - { "name" : "slice256", "const" : false, "input" : [ { "name" : "input", "type" : "uint256[2]" } ] } + { "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, + { "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] }, + { "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] }, + { "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] }, + { "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] }, + { "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] }, + { "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] }, + { "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] }, + { "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] }, + { "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] }, + { "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] }, + { "name" : "slice256", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] } ]` func TestType(t *testing.T) { @@ -344,3 +347,49 @@ func TestPackSliceBig(t *testing.T) { t.Errorf("expected %x got %x", sig, packed) } } + +func ExampleJSON() { + const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]` + + abi, err := JSON(strings.NewReader(definition)) + if err != nil { + log.Fatalln(err) + } + out, err := abi.Pack("isBar", common.HexToAddress("01")) + if err != nil { + log.Fatalln(err) + } + + fmt.Printf("%x\n", out) + // Output: + // 1f2c40920000000000000000000000000000000000000000000000000000000000000001 +} + +func TestBytes(t *testing.T) { + const definition = `[ + { "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] }, + { "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] } +]` + + abi, err := JSON(strings.NewReader(definition)) + if err != nil { + t.Fatal(err) + } + ok := make([]byte, 20) + _, err = abi.Pack("balance", ok) + if err != nil { + t.Error(err) + } + + toosmall := make([]byte, 19) + _, err = abi.Pack("balance", toosmall) + if err != nil { + t.Error(err) + } + + toobig := make([]byte, 21) + _, err = abi.Pack("balance", toobig) + if err == nil { + t.Error("expected error") + } +} |