mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
accounts/abi: count array fields after unpacking
In current codebase array fields are counted before unpacking output. So, if we have array with 2 fields: toGoType function will try to unpack values from 64 till 64 + 2*32 which is not correct in my opinion. I shifted counting to be done after processing array, so in case array is the first field, and it has 2 elements: we will read it from 0 till 2*32. Additionally array doesnt require length prefix, so field that follows array should be read from 64 byte. This is why i made additional substitution in this change.
This commit is contained in:
parent
e3bca640ca
commit
4fc2aa304a
2 changed files with 49 additions and 15 deletions
|
|
@ -73,14 +73,15 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error {
|
||||||
j--
|
j--
|
||||||
// can't read, continue
|
// can't read, continue
|
||||||
continue
|
continue
|
||||||
} else if input.Type.T == ArrayTy {
|
|
||||||
// need to move this up because they read sequentially
|
|
||||||
j += input.Type.Size
|
|
||||||
}
|
}
|
||||||
marshalledValue, err := toGoType((i+j)*32, input.Type, output)
|
marshalledValue, err := toGoType((i+j)*32, input.Type, output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if input.Type.T == ArrayTy {
|
||||||
|
// need to move this up because they read sequentially
|
||||||
|
j += input.Type.Size - 1
|
||||||
|
}
|
||||||
reflectValue := reflect.ValueOf(marshalledValue)
|
reflectValue := reflect.ValueOf(marshalledValue)
|
||||||
|
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,14 @@ package abi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/math"
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -60,59 +61,91 @@ func TestEventId(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type testResult struct {
|
type testResult struct {
|
||||||
|
Values [2]*big.Int
|
||||||
Value1 *big.Int
|
Value1 *big.Int
|
||||||
Value2 *big.Int
|
Value2 *big.Int
|
||||||
}
|
}
|
||||||
|
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
definition string
|
definition string
|
||||||
want testResult
|
want testResult
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc testCase) encoded() []byte {
|
func (tc testCase) encoded(intType, arrayType Type) []byte {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if tc.want.Value1 != nil {
|
if tc.want.Value1 != nil {
|
||||||
b.Write(math.PaddedBigBytes(math.U256(tc.want.Value1), 32))
|
val, _ := intType.pack(reflect.ValueOf(tc.want.Value1))
|
||||||
|
b.Write(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(tc.want.Values, [2]*big.Int{nil, nil}) {
|
||||||
|
val, _ := arrayType.pack(reflect.ValueOf(tc.want.Values))
|
||||||
|
b.Write(val)
|
||||||
}
|
}
|
||||||
if tc.want.Value2 != nil {
|
if tc.want.Value2 != nil {
|
||||||
b.Write(math.PaddedBigBytes(math.U256(tc.want.Value2), 32))
|
val, _ := intType.pack(reflect.ValueOf(tc.want.Value2))
|
||||||
|
b.Write(val)
|
||||||
}
|
}
|
||||||
return b.Bytes()
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEventUnpack(t *testing.T) {
|
func TestEventUnpack(t *testing.T) {
|
||||||
|
intType, _ := NewType("uint256")
|
||||||
|
arrayType, _ := NewType("uint256[2]")
|
||||||
|
definitionTemplate := `[{"anonymous":false,"inputs":[
|
||||||
|
{"indexed":%t,"name":"value1","type":"%s"},
|
||||||
|
{"indexed":%t,"name":"values","type":"%s"},
|
||||||
|
{"indexed":%t,"name":"value2","type":"%s"}],
|
||||||
|
"name":"test","type":"event"}]`
|
||||||
table := []testCase{
|
table := []testCase{
|
||||||
{
|
{
|
||||||
definition: `[{"anonymous":false,"inputs":[{"indexed":true,"name":"value1","type":"uint256"},{"indexed":false,"name":"value2","type":"uint256"}],"name":"transfer","type":"event"}]`,
|
// value1 is indexed
|
||||||
want: testResult{Value2: big.NewInt(10)},
|
definition: fmt.Sprintf(definitionTemplate, true, intType, false, arrayType, false, intType),
|
||||||
|
want: testResult{Value2: big.NewInt(10), Values: [2]*big.Int{big.NewInt(10), big.NewInt(11)}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
definition: `[{"anonymous":false,"inputs":[{"indexed":false,"name":"value1","type":"uint256"},{"indexed":false,"name":"value2","type":"uint256"}],"name":"transfer","type":"event"}]`,
|
// only values field (array) is indexed
|
||||||
|
definition: fmt.Sprintf(definitionTemplate, false, intType, true, arrayType, false, intType),
|
||||||
want: testResult{Value1: big.NewInt(100), Value2: big.NewInt(1)},
|
want: testResult{Value1: big.NewInt(100), Value2: big.NewInt(1)},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
definition: `[{"anonymous":false,"inputs":[{"indexed":false,"name":"value1","type":"uint256"},{"indexed":true,"name":"value2","type":"uint256"}],"name":"transfer","type":"event"}]`,
|
// values and value2 are indexed
|
||||||
|
definition: fmt.Sprintf(definitionTemplate, false, intType, true, arrayType, true, intType),
|
||||||
want: testResult{Value1: big.NewInt(100)},
|
want: testResult{Value1: big.NewInt(100)},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
// value1 and values are not indexed
|
||||||
|
definition: fmt.Sprintf(definitionTemplate, false, intType, false, arrayType, true, intType),
|
||||||
|
want: testResult{Value1: big.NewInt(10), Values: [2]*big.Int{big.NewInt(10), big.NewInt(11)}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for i, row := range table {
|
for i, row := range table {
|
||||||
t.Run(strconv.Itoa(i+1), func(t *testing.T) {
|
t.Run(strconv.Itoa(i+1), func(t *testing.T) {
|
||||||
t.Logf("unpacking %b with expected %v", row.encoded(), row.want)
|
encoded := row.encoded(intType, arrayType)
|
||||||
|
t.Logf("unpacking definition %s, expected %v", row.definition, row.want)
|
||||||
abi, err := JSON(strings.NewReader(row.definition))
|
abi, err := JSON(strings.NewReader(row.definition))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
var rst testResult
|
var rst testResult
|
||||||
if err := abi.Unpack(&rst, "transfer", row.encoded()); err != nil {
|
if err := abi.Unpack(&rst, "test", encoded); err != nil {
|
||||||
t.Fatalf("error unpacking %s: %v", row.definition, err)
|
t.Fatalf("error unpacking %s: %v", row.definition, err)
|
||||||
}
|
}
|
||||||
if row.want.Value1 != nil && rst.Value1.Cmp(row.want.Value1) != 0 {
|
if row.want.Value1 != nil && rst.Value1.Cmp(row.want.Value1) != 0 {
|
||||||
t.Errorf("result value1 %v is not equal to expected %v", rst.Value1, row.want.Value1)
|
t.Errorf("result value1 %v is not equal to expected %v", rst.Value1, row.want.Value1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if row.want.Value2 != nil && rst.Value2.Cmp(row.want.Value2) != 0 {
|
if row.want.Value2 != nil && rst.Value2.Cmp(row.want.Value2) != 0 {
|
||||||
t.Errorf("result value2 %v is not equal to expected %v", rst.Value2, row.want.Value2)
|
t.Errorf("result value2 %v is not equal to expected %v", rst.Value2, row.want.Value2)
|
||||||
}
|
}
|
||||||
|
if len(row.want.Values) != len(rst.Values) {
|
||||||
|
t.Errorf("result values %v are not equal to expected %v", rst.Values, row.want.Values)
|
||||||
|
} else {
|
||||||
|
for i, val := range rst.Values {
|
||||||
|
if exp := row.want.Values[i]; exp != nil && exp.Cmp(val) != 0 {
|
||||||
|
t.Errorf("value %d: %v is not equal to expected %v", i, val, exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue