2016-04-14 11:18:24 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-27 01:38:53 -06:00
|
|
|
package abi
|
|
|
|
|
|
|
|
import (
|
2017-12-20 08:09:23 -06:00
|
|
|
"bytes"
|
2017-11-09 19:28:17 -06:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
2017-12-01 15:32:04 -06:00
|
|
|
"reflect"
|
2016-01-27 01:38:53 -06:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-11-09 19:28:17 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-12-20 08:09:23 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-27 01:38:53 -06:00
|
|
|
)
|
|
|
|
|
2017-11-09 19:28:17 -06:00
|
|
|
var jsonEventTransfer = []byte(`{
|
|
|
|
"anonymous": false,
|
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"indexed": true, "name": "from", "type": "address"
|
|
|
|
}, {
|
|
|
|
"indexed": true, "name": "to", "type": "address"
|
|
|
|
}, {
|
|
|
|
"indexed": false, "name": "value", "type": "uint256"
|
|
|
|
}],
|
|
|
|
"name": "Transfer",
|
|
|
|
"type": "event"
|
|
|
|
}`)
|
|
|
|
|
|
|
|
var jsonEventPledge = []byte(`{
|
|
|
|
"anonymous": false,
|
|
|
|
"inputs": [{
|
|
|
|
"indexed": false, "name": "who", "type": "address"
|
|
|
|
}, {
|
|
|
|
"indexed": false, "name": "wad", "type": "uint128"
|
|
|
|
}, {
|
|
|
|
"indexed": false, "name": "currency", "type": "bytes3"
|
|
|
|
}],
|
|
|
|
"name": "Pledge",
|
|
|
|
"type": "event"
|
|
|
|
}`)
|
|
|
|
|
2018-05-14 07:47:31 -05:00
|
|
|
var jsonEventMixedCase = []byte(`{
|
|
|
|
"anonymous": false,
|
|
|
|
"inputs": [{
|
|
|
|
"indexed": false, "name": "value", "type": "uint256"
|
|
|
|
}, {
|
|
|
|
"indexed": false, "name": "_value", "type": "uint256"
|
|
|
|
}, {
|
|
|
|
"indexed": false, "name": "Value", "type": "uint256"
|
|
|
|
}],
|
|
|
|
"name": "MixedCase",
|
|
|
|
"type": "event"
|
|
|
|
}`)
|
|
|
|
|
2017-11-09 19:28:17 -06:00
|
|
|
// 1000000
|
|
|
|
var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240"
|
|
|
|
|
|
|
|
// "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd"
|
|
|
|
var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000"
|
|
|
|
|
2018-05-14 07:47:31 -05:00
|
|
|
// 1000000,2218516807680,1000001
|
|
|
|
var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241"
|
|
|
|
|
2016-01-27 01:38:53 -06:00
|
|
|
func TestEventId(t *testing.T) {
|
|
|
|
var table = []struct {
|
|
|
|
definition string
|
|
|
|
expectations map[string]common.Hash
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
definition: `[
|
2018-12-13 08:12:19 -06:00
|
|
|
{ "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
|
|
|
|
{ "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
|
2016-01-27 01:38:53 -06:00
|
|
|
]`,
|
|
|
|
expectations: map[string]common.Hash{
|
2018-12-13 08:12:19 -06:00
|
|
|
"Balance": crypto.Keccak256Hash([]byte("Balance(uint256)")),
|
|
|
|
"Check": crypto.Keccak256Hash([]byte("Check(address,uint256)")),
|
2016-01-27 01:38:53 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range table {
|
|
|
|
abi, err := JSON(strings.NewReader(test.definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, event := range abi.Events {
|
2020-04-20 02:01:04 -05:00
|
|
|
if event.ID != test.expectations[name] {
|
|
|
|
t.Errorf("expected id to be %x, got %x", test.expectations[name], event.ID)
|
2016-01-27 01:38:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 08:09:23 -06:00
|
|
|
|
2018-12-13 08:12:19 -06:00
|
|
|
func TestEventString(t *testing.T) {
|
|
|
|
var table = []struct {
|
|
|
|
definition string
|
|
|
|
expectations map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
definition: `[
|
|
|
|
{ "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
|
|
|
|
{ "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] },
|
|
|
|
{ "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] }
|
|
|
|
]`,
|
|
|
|
expectations: map[string]string{
|
|
|
|
"Balance": "event Balance(uint256 in)",
|
|
|
|
"Check": "event Check(address t, uint256 b)",
|
|
|
|
"Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range table {
|
|
|
|
abi, err := JSON(strings.NewReader(test.definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, event := range abi.Events {
|
|
|
|
if event.String() != test.expectations[name] {
|
|
|
|
t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 08:09:23 -06:00
|
|
|
// 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"}]}]`
|
|
|
|
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)))
|
|
|
|
}
|
2020-09-28 07:10:26 -05:00
|
|
|
unpacked, err := abi.Unpack("test", b.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, [2]uint8{1, 2}, unpacked[0])
|
|
|
|
require.Equal(t, uint8(3), unpacked[1])
|
2017-12-20 08:09:23 -06:00
|
|
|
}
|
2017-11-09 19:28:17 -06:00
|
|
|
|
|
|
|
func TestEventTupleUnpack(t *testing.T) {
|
|
|
|
|
|
|
|
type EventTransfer struct {
|
|
|
|
Value *big.Int
|
|
|
|
}
|
|
|
|
|
2018-05-14 07:47:31 -05:00
|
|
|
type EventTransferWithTag struct {
|
|
|
|
// this is valid because `value` is not exportable,
|
|
|
|
// so value is only unmarshalled into `Value1`.
|
2019-11-21 15:22:47 -06:00
|
|
|
value *big.Int //lint:ignore U1000 unused field is part of test
|
2018-05-14 07:47:31 -05:00
|
|
|
Value1 *big.Int `abi:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BadEventTransferWithSameFieldAndTag struct {
|
|
|
|
Value *big.Int
|
|
|
|
Value1 *big.Int `abi:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BadEventTransferWithDuplicatedTag struct {
|
|
|
|
Value1 *big.Int `abi:"value"`
|
|
|
|
Value2 *big.Int `abi:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BadEventTransferWithEmptyTag struct {
|
|
|
|
Value *big.Int `abi:""`
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:28:17 -06:00
|
|
|
type EventPledge struct {
|
|
|
|
Who common.Address
|
|
|
|
Wad *big.Int
|
|
|
|
Currency [3]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type BadEventPledge struct {
|
|
|
|
Who string
|
|
|
|
Wad int
|
|
|
|
Currency [3]byte
|
|
|
|
}
|
|
|
|
|
2018-05-14 07:47:31 -05:00
|
|
|
type EventMixedCase struct {
|
|
|
|
Value1 *big.Int `abi:"value"`
|
|
|
|
Value2 *big.Int `abi:"_value"`
|
|
|
|
Value3 *big.Int `abi:"Value"`
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:28:17 -06:00
|
|
|
bigint := new(big.Int)
|
|
|
|
bigintExpected := big.NewInt(1000000)
|
|
|
|
bigintExpected2 := big.NewInt(2218516807680)
|
2018-05-14 07:47:31 -05:00
|
|
|
bigintExpected3 := big.NewInt(1000001)
|
2017-11-09 19:28:17 -06:00
|
|
|
addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268")
|
|
|
|
var testCases = []struct {
|
|
|
|
data string
|
|
|
|
dest interface{}
|
|
|
|
expected interface{}
|
|
|
|
jsonLog []byte
|
|
|
|
error string
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
transferData1,
|
|
|
|
&EventTransfer{},
|
|
|
|
&EventTransfer{Value: bigintExpected},
|
|
|
|
jsonEventTransfer,
|
|
|
|
"",
|
|
|
|
"Can unpack ERC20 Transfer event into structure",
|
|
|
|
}, {
|
|
|
|
transferData1,
|
|
|
|
&[]interface{}{&bigint},
|
|
|
|
&[]interface{}{&bigintExpected},
|
|
|
|
jsonEventTransfer,
|
|
|
|
"",
|
|
|
|
"Can unpack ERC20 Transfer event into slice",
|
2018-05-14 07:47:31 -05:00
|
|
|
}, {
|
|
|
|
transferData1,
|
|
|
|
&EventTransferWithTag{},
|
|
|
|
&EventTransferWithTag{Value1: bigintExpected},
|
|
|
|
jsonEventTransfer,
|
|
|
|
"",
|
|
|
|
"Can unpack ERC20 Transfer event into structure with abi: tag",
|
|
|
|
}, {
|
|
|
|
transferData1,
|
|
|
|
&BadEventTransferWithDuplicatedTag{},
|
|
|
|
&BadEventTransferWithDuplicatedTag{},
|
|
|
|
jsonEventTransfer,
|
|
|
|
"struct: abi tag in 'Value2' already mapped",
|
|
|
|
"Can not unpack ERC20 Transfer event with duplicated abi tag",
|
|
|
|
}, {
|
|
|
|
transferData1,
|
|
|
|
&BadEventTransferWithSameFieldAndTag{},
|
|
|
|
&BadEventTransferWithSameFieldAndTag{},
|
|
|
|
jsonEventTransfer,
|
|
|
|
"abi: multiple variables maps to the same abi field 'value'",
|
|
|
|
"Can not unpack ERC20 Transfer event with a field and a tag mapping to the same abi variable",
|
|
|
|
}, {
|
|
|
|
transferData1,
|
|
|
|
&BadEventTransferWithEmptyTag{},
|
|
|
|
&BadEventTransferWithEmptyTag{},
|
|
|
|
jsonEventTransfer,
|
|
|
|
"struct: abi tag in 'Value' is empty",
|
|
|
|
"Can not unpack ERC20 Transfer event with an empty tag",
|
2017-11-09 19:28:17 -06:00
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
&EventPledge{},
|
|
|
|
&EventPledge{
|
|
|
|
addr,
|
|
|
|
bigintExpected2,
|
|
|
|
[3]byte{'u', 's', 'd'}},
|
|
|
|
jsonEventPledge,
|
|
|
|
"",
|
|
|
|
"Can unpack Pledge event into structure",
|
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
&[]interface{}{&common.Address{}, &bigint, &[3]byte{}},
|
|
|
|
&[]interface{}{
|
|
|
|
&addr,
|
|
|
|
&bigintExpected2,
|
|
|
|
&[3]byte{'u', 's', 'd'}},
|
|
|
|
jsonEventPledge,
|
|
|
|
"",
|
|
|
|
"Can unpack Pledge event into slice",
|
2017-12-01 15:32:04 -06:00
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
&[3]interface{}{&common.Address{}, &bigint, &[3]byte{}},
|
|
|
|
&[3]interface{}{
|
|
|
|
&addr,
|
|
|
|
&bigintExpected2,
|
|
|
|
&[3]byte{'u', 's', 'd'}},
|
|
|
|
jsonEventPledge,
|
|
|
|
"",
|
|
|
|
"Can unpack Pledge event into an array",
|
2017-11-09 19:28:17 -06:00
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
&[]interface{}{new(int), 0, 0},
|
|
|
|
&[]interface{}{},
|
|
|
|
jsonEventPledge,
|
|
|
|
"abi: cannot unmarshal common.Address in to int",
|
|
|
|
"Can not unpack Pledge event into slice with wrong types",
|
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
&BadEventPledge{},
|
|
|
|
&BadEventPledge{},
|
|
|
|
jsonEventPledge,
|
|
|
|
"abi: cannot unmarshal common.Address in to string",
|
|
|
|
"Can not unpack Pledge event into struct with wrong filed types",
|
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
&[]interface{}{common.Address{}, new(big.Int)},
|
|
|
|
&[]interface{}{},
|
|
|
|
jsonEventPledge,
|
2020-05-13 10:50:18 -05:00
|
|
|
"abi: insufficient number of arguments for unpack, want 3, got 2",
|
2017-11-09 19:28:17 -06:00
|
|
|
"Can not unpack Pledge event into too short slice",
|
|
|
|
}, {
|
|
|
|
pledgeData1,
|
|
|
|
new(map[string]interface{}),
|
|
|
|
&[]interface{}{},
|
|
|
|
jsonEventPledge,
|
2020-05-13 10:50:18 -05:00
|
|
|
"abi:[2] cannot unmarshal tuple in to map[string]interface {}",
|
2017-11-09 19:28:17 -06:00
|
|
|
"Can not unpack Pledge event into map",
|
2018-05-14 07:47:31 -05:00
|
|
|
}, {
|
|
|
|
mixedCaseData1,
|
|
|
|
&EventMixedCase{},
|
|
|
|
&EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3},
|
|
|
|
jsonEventMixedCase,
|
|
|
|
"",
|
|
|
|
"Can unpack abi variables with mixed case",
|
2017-11-09 19:28:17 -06:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
assert := assert.New(t)
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert)
|
|
|
|
if tc.error == "" {
|
|
|
|
assert.Nil(err, "Should be able to unpack event data.")
|
2017-12-21 09:18:37 -06:00
|
|
|
assert.Equal(tc.expected, tc.dest, tc.name)
|
2017-11-09 19:28:17 -06:00
|
|
|
} else {
|
2018-05-14 07:47:31 -05:00
|
|
|
assert.EqualError(err, tc.error, tc.name)
|
2017-11-09 19:28:17 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error {
|
|
|
|
data, err := hex.DecodeString(hexData)
|
|
|
|
assert.NoError(err, "Hex data should be a correct hex-string")
|
|
|
|
var e Event
|
|
|
|
assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI")
|
|
|
|
a := ABI{Events: map[string]Event{"e": e}}
|
2020-09-28 07:10:26 -05:00
|
|
|
return a.UnpackIntoInterface(dest, "e", data)
|
2017-11-09 19:28:17 -06:00
|
|
|
}
|
2017-12-21 03:26:30 -06:00
|
|
|
|
|
|
|
// 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 {
|
2020-09-28 07:10:26 -05:00
|
|
|
Value1 uint8 // indexed
|
2017-12-21 03:26:30 -06:00
|
|
|
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
|
2020-09-28 07:10:26 -05:00
|
|
|
require.NoError(t, abi.UnpackIntoInterface(&rst, "test", b.Bytes()))
|
2017-12-21 03:26:30 -06:00
|
|
|
require.Equal(t, uint8(0), rst.Value1)
|
|
|
|
require.Equal(t, uint8(8), rst.Value2)
|
|
|
|
}
|
|
|
|
|
2020-09-20 03:43:57 -05:00
|
|
|
// TestEventIndexedWithArrayUnpack verifies that decoder will not overflow when static array is indexed input.
|
2017-12-21 03:26:30 -06:00
|
|
|
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 {
|
2020-09-28 07:10:26 -05:00
|
|
|
Value1 [2]uint8 // indexed
|
2017-12-21 03:26:30 -06:00
|
|
|
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))
|
2017-12-21 09:18:37 -06:00
|
|
|
|
2017-12-21 03:26:30 -06:00
|
|
|
var rst testStruct
|
2020-09-28 07:10:26 -05:00
|
|
|
require.NoError(t, abi.UnpackIntoInterface(&rst, "test", b.Bytes()))
|
2017-12-21 03:26:30 -06:00
|
|
|
require.Equal(t, [2]uint8{0, 0}, rst.Value1)
|
|
|
|
require.Equal(t, stringOut, rst.Value2)
|
|
|
|
}
|