accounts/abi,common/math: much test driven development

Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
RJ Catalano 2017-05-22 16:38:38 -05:00
parent 80f7c6c299
commit 5ef1e97b7c
No known key found for this signature in database
GPG key ID: D4AB109D9B5D6386
4 changed files with 24 additions and 2 deletions

View file

@ -34,7 +34,8 @@ import (
type ABI struct { type ABI struct {
Constructor Method Constructor Method
Methods map[string]Method Methods map[string]Method
Events map[string]Event // todo: make events loggable from an interface that specifies very abstract functions.
Events map[string]Event
} }
// JSON returns a parsed ABI interface and error if it failed. // JSON returns a parsed ABI interface and error if it failed.

View file

@ -370,6 +370,7 @@ func TestPack(t *testing.T) {
{"address[]", []common.Address{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))}, {"address[]", []common.Address{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))},
{"bytes32[]", []common.Hash{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))}, {"bytes32[]", []common.Hash{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))},
{"function", [24]byte{1}, pad([]byte{1}, 32, false)}, {"function", [24]byte{1}, pad([]byte{1}, 32, false)},
{"bool[]", []bool{false, false}, formatSliceOutput(pad([]byte{0}, 32, true), pad([]byte{0}, 32, true))},
} { } {
typ, err := NewType(test.typ) typ, err := NewType(test.typ)
if err != nil { if err != nil {
@ -471,7 +472,8 @@ const jsondata2 = `
{ "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] }, { "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
{ "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] }, { "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
{ "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] }, { "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
{ "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] }, { "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "", "type" : "uint16" } ] },
{ "type" : "function", "name" : "boolMultiPack", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" }, { "name" : "", "type" : "bool" } ] },
{ "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] }, { "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
{ "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }, { "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
{ "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] }, { "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
@ -626,6 +628,22 @@ func TestMultiPack(t *testing.T) {
if !bytes.Equal(packed, sig) { if !bytes.Equal(packed, sig) {
t.Errorf("expected %x got %x", sig, packed) t.Errorf("expected %x got %x", sig, packed)
} }
sig = crypto.Keccak256([]byte("boolMultiPack(bool,bool)"))[:4]
sig = append(sig, make([]byte, 64)...)
sig[35] = 0
sig[67] = 0
packed, err = abi.Pack("boolMultiPack", []interface{}{false, false}...)
if err != nil {
t.Error(err)
t.FailNow()
}
if !bytes.Equal(packed, sig) {
t.Errorf("expected %x got %x", sig, packed)
}
} }
func ExampleJSON() { func ExampleJSON() {

View file

@ -17,6 +17,7 @@
package abi package abi
import ( import (
"fmt"
"reflect" "reflect"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -48,6 +49,7 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
if reflectValue.Bool() { if reflectValue.Bool() {
return math.PaddedBigBytes(common.Big1, 32) return math.PaddedBigBytes(common.Big1, 32)
} else { } else {
fmt.Printf("Got this for value: %v\n", math.PaddedBigBytes(common.Big0, 32))
return math.PaddedBigBytes(common.Big0, 32) return math.PaddedBigBytes(common.Big0, 32)
} }
case BytesTy: case BytesTy:

View file

@ -125,6 +125,7 @@ func TestPaddedBigBytes(t *testing.T) {
{num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}}, {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
{num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}}, {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
{num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}}, {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
{num: big.NewInt(0), n: 32, result: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
} }
for _, test := range tests { for _, test := range tests {
if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) { if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {