diff --git a/accounts/abi/event.go b/accounts/abi/event.go index 44ed7b8df256..403eb088b2b4 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -69,16 +69,20 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { for i := 0; i < len(e.Inputs); i++ { input := e.Inputs[i] if input.Indexed { + // indexed inputs are not available in log output + j-- // can't read, 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) if err != nil { return err } + if input.Type.T == ArrayTy { + // combined index ('i' + 'j') need to be adjusted only by size of array, thus + // we need to decrement 'j' because 'i' was incremented + j += input.Type.Size - 1 + } reflectValue := reflect.ValueOf(marshalledValue) switch value.Kind() { diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index 7e2f13f76320..d0d074a0d30d 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -17,11 +17,16 @@ package abi import ( + "bytes" + "fmt" + "math/big" + "reflect" "strings" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/require" ) func TestEventId(t *testing.T) { @@ -54,3 +59,91 @@ func TestEventId(t *testing.T) { } } } + +type testResult struct { + Values [2]*big.Int + Value1 *big.Int + Value2 *big.Int +} + +type testCase struct { + definition string + want testResult +} + +func (tc testCase) encoded(intType, arrayType Type) []byte { + var b bytes.Buffer + if tc.want.Value1 != nil { + 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 { + val, _ := intType.pack(reflect.ValueOf(tc.want.Value2)) + b.Write(val) + } + return b.Bytes() +} + +// TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder. +func TestEventUnpackIndexed(t *testing.T) { + definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` + type testStruct struct { + Value1 uint8 + Value2 uint8 + } + abi, err := JSON(strings.NewReader(definition)) + require.NoError(t, err) + var b bytes.Buffer + b.Write(packNum(reflect.ValueOf(uint8(8)))) + var rst testStruct + require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) + require.Equal(t, uint8(0), rst.Value1) + require.Equal(t, uint8(8), rst.Value2) +} + +// TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array. +func TestEventMultiValueWithArrayUnpack(t *testing.T) { + definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` + type testStruct struct { + Value1 [2]uint8 + Value2 uint8 + } + abi, err := JSON(strings.NewReader(definition)) + require.NoError(t, err) + var b bytes.Buffer + var i uint8 = 1 + for ; i <= 3; i++ { + b.Write(packNum(reflect.ValueOf(i))) + } + var rst testStruct + require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) + require.Equal(t, [2]uint8{1, 2}, rst.Value1) + require.Equal(t, uint8(3), rst.Value2) +} + +// TestEventIndexedWithArrayUnpack verifies that decoder will not overlow when static array is indexed input. +func TestEventIndexedWithArrayUnpack(t *testing.T) { + definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]` + type testStruct struct { + Value1 [2]uint8 + Value2 string + } + abi, err := JSON(strings.NewReader(definition)) + require.NoError(t, err) + var b bytes.Buffer + stringOut := "abc" + // number of fields that will be encoded * 32 + b.Write(packNum(reflect.ValueOf(32))) + b.Write(packNum(reflect.ValueOf(len(stringOut)))) + b.Write(common.RightPadBytes([]byte(stringOut), 32)) + fmt.Println(b.Bytes()) + var rst testStruct + require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) + require.Equal(t, [2]uint8{0, 0}, rst.Value1) + require.Equal(t, stringOut, rst.Value2) +}