mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
accounts/abi,common/math: much test driven development
Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
parent
80f7c6c299
commit
5ef1e97b7c
4 changed files with 24 additions and 2 deletions
|
|
@ -34,7 +34,8 @@ import (
|
|||
type ABI struct {
|
||||
Constructor 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.
|
||||
|
|
|
|||
|
|
@ -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))},
|
||||
{"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)},
|
||||
{"bool[]", []bool{false, false}, formatSliceOutput(pad([]byte{0}, 32, true), pad([]byte{0}, 32, true))},
|
||||
} {
|
||||
typ, err := NewType(test.typ)
|
||||
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[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
|
||||
{ "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" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
|
||||
{ "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) {
|
||||
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() {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package abi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
|
@ -48,6 +49,7 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
|
|||
if reflectValue.Bool() {
|
||||
return math.PaddedBigBytes(common.Big1, 32)
|
||||
} else {
|
||||
fmt.Printf("Got this for value: %v\n", math.PaddedBigBytes(common.Big0, 32))
|
||||
return math.PaddedBigBytes(common.Big0, 32)
|
||||
}
|
||||
case BytesTy:
|
||||
|
|
|
|||
|
|
@ -125,6 +125,7 @@ func TestPaddedBigBytes(t *testing.T) {
|
|||
{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: 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 {
|
||||
if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue