Skip to content

Commit bc6c4a3

Browse files
committed
[release/1.4.7] accounts/abi: fix uint64 upper range encoding.
(cherry picked from commit 0f9539e)
1 parent f7fdfa4 commit bc6c4a3

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

accounts/abi/numbers.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,21 @@ var (
5656
big_ts = reflect.TypeOf([]*big.Int(nil))
5757
)
5858

59-
// U256 will ensure unsigned 256bit on big nums
59+
// U256 converts a big Int into a 256bit EVM number.
6060
func U256(n *big.Int) []byte {
6161
return common.LeftPadBytes(common.U256(n).Bytes(), 32)
6262
}
6363

64-
// S256 will ensure signed 256bit on big nums
65-
func U2U256(n uint64) []byte {
66-
return U256(big.NewInt(int64(n)))
67-
}
68-
6964
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
7065
func packNum(value reflect.Value) []byte {
7166
switch kind := value.Kind(); kind {
7267
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
73-
return U2U256(value.Uint())
68+
return U256(new(big.Int).SetUint64(value.Uint()))
7469
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
75-
return U2U256(uint64(value.Int()))
70+
return U256(big.NewInt(value.Int()))
7671
case reflect.Ptr:
7772
return U256(value.Interface().(*big.Int))
7873
}
79-
8074
return nil
8175
}
8276

accounts/abi/numbers_test.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package abi
1818

1919
import (
2020
"bytes"
21+
"math"
2122
"math/big"
2223
"reflect"
2324
"testing"
@@ -34,21 +35,38 @@ func TestNumberTypes(t *testing.T) {
3435
}
3536

3637
func TestPackNumber(t *testing.T) {
37-
ubytes := make([]byte, 32)
38-
ubytes[31] = 1
39-
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}
38+
tests := []struct {
39+
value reflect.Value
40+
packed []byte
41+
}{
42+
// Protocol limits
43+
{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}},
44+
{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}},
45+
{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}},
46+
47+
// Type corner cases
48+
{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}},
49+
{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}},
50+
{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}},
51+
{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}},
4052

41-
packed := packNum(reflect.ValueOf(1))
42-
if !bytes.Equal(packed, ubytes) {
43-
t.Errorf("expected %x got %x", ubytes, packed)
53+
{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}},
54+
{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}},
55+
{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}},
56+
{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}},
57+
58+
{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}},
59+
{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}},
60+
{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}},
61+
{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}},
4462
}
45-
packed = packNum(reflect.ValueOf(-1))
46-
if !bytes.Equal(packed, maxunsigned) {
47-
t.Errorf("expected %x got %x", maxunsigned, packed)
63+
for i, tt := range tests {
64+
packed := packNum(tt.value)
65+
if !bytes.Equal(packed, tt.packed) {
66+
t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
67+
}
4868
}
49-
50-
packed = packNum(reflect.ValueOf("string"))
51-
if packed != nil {
69+
if packed := packNum(reflect.ValueOf("string")); packed != nil {
5270
t.Errorf("expected 'string' to pack to nil. got %x instead", packed)
5371
}
5472
}

0 commit comments

Comments
 (0)