|
|
@ -18,6 +18,7 @@ package abi |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
|
|
|
|
"math" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
@ -34,21 +35,38 @@ func TestNumberTypes(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestPackNumber(t *testing.T) { |
|
|
|
func TestPackNumber(t *testing.T) { |
|
|
|
ubytes := make([]byte, 32) |
|
|
|
tests := []struct { |
|
|
|
ubytes[31] = 1 |
|
|
|
value reflect.Value |
|
|
|
maxunsigned := []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} |
|
|
|
packed []byte |
|
|
|
|
|
|
|
}{ |
|
|
|
|
|
|
|
// Protocol limits
|
|
|
|
|
|
|
|
{reflect.ValueOf(0), []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}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(1), []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, 1}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(-1), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Type corner cases
|
|
|
|
|
|
|
|
{reflect.ValueOf(uint8(math.MaxUint8)), []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, 255}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(uint16(math.MaxUint16)), []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, 255, 255}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(uint32(math.MaxUint32)), []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, 255, 255, 255, 255}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(uint64(math.MaxUint64)), []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, 255, 255, 255, 255, 255, 255, 255, 255}}, |
|
|
|
|
|
|
|
|
|
|
|
packed := packNum(reflect.ValueOf(1)) |
|
|
|
{reflect.ValueOf(int8(math.MaxInt8)), []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, 127}}, |
|
|
|
if !bytes.Equal(packed, ubytes) { |
|
|
|
{reflect.ValueOf(int16(math.MaxInt16)), []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, 127, 255}}, |
|
|
|
t.Errorf("expected %x got %x", ubytes, packed) |
|
|
|
{reflect.ValueOf(int32(math.MaxInt32)), []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, 127, 255, 255, 255}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(int64(math.MaxInt64)), []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, 127, 255, 255, 255, 255, 255, 255, 255}}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{reflect.ValueOf(int8(math.MinInt8)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(int16(math.MinInt16)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(int32(math.MinInt32)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0}}, |
|
|
|
|
|
|
|
{reflect.ValueOf(int64(math.MinInt64)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0, 0, 0, 0, 0}}, |
|
|
|
} |
|
|
|
} |
|
|
|
packed = packNum(reflect.ValueOf(-1)) |
|
|
|
for i, tt := range tests { |
|
|
|
if !bytes.Equal(packed, maxunsigned) { |
|
|
|
packed := packNum(tt.value) |
|
|
|
t.Errorf("expected %x got %x", maxunsigned, packed) |
|
|
|
if !bytes.Equal(packed, tt.packed) { |
|
|
|
|
|
|
|
t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
packed = packNum(reflect.ValueOf("string")) |
|
|
|
if packed := packNum(reflect.ValueOf("string")); packed != nil { |
|
|
|
if packed != nil { |
|
|
|
|
|
|
|
t.Errorf("expected 'string' to pack to nil. got %x instead", packed) |
|
|
|
t.Errorf("expected 'string' to pack to nil. got %x instead", packed) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|