upstream to the following version ---- commit 0a3993c558616868e35f9730e92c704ac16ee437 Author: yumiel yoomee1313 <yumiel.ko@groundx.xyz> Date: Wed Dec 30 21:10:11 2020 +0900 accounts/abi/bind: fix erroneous test (#22053) closes #22049 Signed-off-by: Leo Chen <leo@harmony.one>pull/3493/head
parent
666033c3eb
commit
c63a27fdd1
@ -0,0 +1,990 @@ |
||||
// Copyright 2020 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/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
type packUnpackTest struct { |
||||
def string |
||||
unpacked interface{} |
||||
packed string |
||||
} |
||||
|
||||
var packUnpackTests = []packUnpackTest{ |
||||
// Booleans
|
||||
{ |
||||
def: `[{ "type": "bool" }]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: true, |
||||
}, |
||||
{ |
||||
def: `[{ "type": "bool" }]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: false, |
||||
}, |
||||
// Integers
|
||||
{ |
||||
def: `[{ "type": "uint8" }]`, |
||||
unpacked: uint8(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "uint8[]" }]`, |
||||
unpacked: []uint8{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "uint16" }]`, |
||||
unpacked: uint16(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "uint16[]" }]`, |
||||
unpacked: []uint16{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint17"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: big.NewInt(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: uint32(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[]"}]`, |
||||
unpacked: []uint32{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64"}]`, |
||||
unpacked: uint64(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64[]"}]`, |
||||
unpacked: []uint64{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256"}]`, |
||||
unpacked: big.NewInt(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[]"}]`, |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int8"}]`, |
||||
unpacked: int8(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int8[]"}]`, |
||||
unpacked: []int8{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16"}]`, |
||||
unpacked: int16(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16[]"}]`, |
||||
unpacked: []int16{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int17"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: big.NewInt(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32"}]`, |
||||
unpacked: int32(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: int32(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32[]"}]`, |
||||
unpacked: []int32{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64"}]`, |
||||
unpacked: int64(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64[]"}]`, |
||||
unpacked: []int64{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256"}]`, |
||||
unpacked: big.NewInt(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256"}]`, |
||||
packed: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
||||
unpacked: big.NewInt(-1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256[]"}]`, |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
// Address
|
||||
{ |
||||
def: `[{"type": "address"}]`, |
||||
packed: "0000000000000000000000000100000000000000000000000000000000000000", |
||||
unpacked: common.Address{1}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "address[]"}]`, |
||||
unpacked: []common.Address{{1}, {2}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000100000000000000000000000000000000000000" + |
||||
"0000000000000000000000000200000000000000000000000000000000000000", |
||||
}, |
||||
// Bytes
|
||||
{ |
||||
def: `[{"type": "bytes1"}]`, |
||||
unpacked: [1]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes2"}]`, |
||||
unpacked: [2]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes3"}]`, |
||||
unpacked: [3]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes4"}]`, |
||||
unpacked: [4]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes5"}]`, |
||||
unpacked: [5]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes6"}]`, |
||||
unpacked: [6]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes7"}]`, |
||||
unpacked: [7]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes8"}]`, |
||||
unpacked: [8]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes9"}]`, |
||||
unpacked: [9]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes10"}]`, |
||||
unpacked: [10]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes11"}]`, |
||||
unpacked: [11]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes12"}]`, |
||||
unpacked: [12]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes13"}]`, |
||||
unpacked: [13]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes14"}]`, |
||||
unpacked: [14]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes15"}]`, |
||||
unpacked: [15]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes16"}]`, |
||||
unpacked: [16]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes17"}]`, |
||||
unpacked: [17]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes18"}]`, |
||||
unpacked: [18]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes19"}]`, |
||||
unpacked: [19]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes20"}]`, |
||||
unpacked: [20]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes21"}]`, |
||||
unpacked: [21]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes22"}]`, |
||||
unpacked: [22]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes23"}]`, |
||||
unpacked: [23]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes24"}]`, |
||||
unpacked: [24]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes25"}]`, |
||||
unpacked: [25]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes26"}]`, |
||||
unpacked: [26]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes27"}]`, |
||||
unpacked: [27]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes28"}]`, |
||||
unpacked: [28]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes29"}]`, |
||||
unpacked: [29]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes30"}]`, |
||||
unpacked: [30]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes31"}]`, |
||||
unpacked: [31]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
unpacked: [32]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [32]byte{1, 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}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [32]byte{1, 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}, |
||||
}, |
||||
// Functions
|
||||
{ |
||||
def: `[{"type": "function"}]`, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [24]byte{1}, |
||||
}, |
||||
// Slice and Array
|
||||
{ |
||||
def: `[{"type": "uint8[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint8{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: []uint8{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: []*big.Int{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint8{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int8[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int8{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []int16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []int32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []int64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256[3]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003", |
||||
unpacked: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, |
||||
}, |
||||
// multi dimensional, if these pass, all types that don't require length prefix should pass
|
||||
{ |
||||
def: `[{"type": "uint8[][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [][]uint8{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [][]uint8{{1, 2}, {1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003", |
||||
unpacked: [][]uint8{{1, 2}, {1, 2, 3}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2][2]uint8{{1, 2}, {1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000060" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [2][]uint8{{}, {}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: [2][]uint8{{1}, {1}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [][2]uint8{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [][2]uint8{{1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [][2]uint8{{1, 2}, {1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint16[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint16[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[2][3][4]"}]`, |
||||
unpacked: [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"0000000000000000000000000000000000000000000000000000000000000004" + |
||||
"0000000000000000000000000000000000000000000000000000000000000005" + |
||||
"0000000000000000000000000000000000000000000000000000000000000006" + |
||||
"0000000000000000000000000000000000000000000000000000000000000007" + |
||||
"0000000000000000000000000000000000000000000000000000000000000008" + |
||||
"0000000000000000000000000000000000000000000000000000000000000009" + |
||||
"000000000000000000000000000000000000000000000000000000000000000a" + |
||||
"000000000000000000000000000000000000000000000000000000000000000b" + |
||||
"000000000000000000000000000000000000000000000000000000000000000c" + |
||||
"000000000000000000000000000000000000000000000000000000000000000d" + |
||||
"000000000000000000000000000000000000000000000000000000000000000e" + |
||||
"000000000000000000000000000000000000000000000000000000000000000f" + |
||||
"0000000000000000000000000000000000000000000000000000000000000010" + |
||||
"0000000000000000000000000000000000000000000000000000000000000011" + |
||||
"0000000000000000000000000000000000000000000000000000000000000012" + |
||||
"0000000000000000000000000000000000000000000000000000000000000013" + |
||||
"0000000000000000000000000000000000000000000000000000000000000014" + |
||||
"0000000000000000000000000000000000000000000000000000000000000015" + |
||||
"0000000000000000000000000000000000000000000000000000000000000016" + |
||||
"0000000000000000000000000000000000000000000000000000000000000017" + |
||||
"0000000000000000000000000000000000000000000000000000000000000018", |
||||
}, |
||||
|
||||
{ |
||||
def: `[{"type": "bytes32[]"}]`, |
||||
unpacked: [][32]byte{{1}, {2}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0100000000000000000000000000000000000000000000000000000000000000" + |
||||
"0200000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[3]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003", |
||||
unpacked: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "string[4]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"00000000000000000000000000000000000000000000000000000000000000c0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000100" + |
||||
"0000000000000000000000000000000000000000000000000000000000000140" + |
||||
"0000000000000000000000000000000000000000000000000000000000000005" + |
||||
"48656c6c6f000000000000000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000005" + |
||||
"576f726c64000000000000000000000000000000000000000000000000000000" + |
||||
"000000000000000000000000000000000000000000000000000000000000000b" + |
||||
"476f2d657468657265756d000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000008" + |
||||
"457468657265756d000000000000000000000000000000000000000000000000", |
||||
unpacked: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "string[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"0000000000000000000000000000000000000000000000000000000000000008" + |
||||
"457468657265756d000000000000000000000000000000000000000000000000" + |
||||
"000000000000000000000000000000000000000000000000000000000000000b" + |
||||
"676f2d657468657265756d000000000000000000000000000000000000000000", |
||||
unpacked: []string{"Ethereum", "go-ethereum"}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"f0f0f00000000000000000000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"f0f0f00000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [][]byte{{0xf0, 0xf0, 0xf0}, {0xf0, 0xf0, 0xf0}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[2][][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"00000000000000000000000000000000000000000000000000000000000000e0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000000c8" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000003e8" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000000c8" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000003e8", |
||||
unpacked: [][][2]*big.Int{{{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}, {{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}}, |
||||
}, |
||||
// struct outputs
|
||||
{ |
||||
def: `[{"components": [{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int_one","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int__one","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int_one_","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
Intone *big.Int |
||||
}{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "string"}]`, |
||||
unpacked: "foobar", |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000006" + |
||||
"666f6f6261720000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "string[]"}]`, |
||||
unpacked: []string{"hello", "foobar"}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
||||
"0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
|
||||
"666f6f6261720000000000000000000000000000000000000000000000000000", // str[1]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "string[2]"}]`, |
||||
unpacked: [2]string{"hello", "foobar"}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0
|
||||
"0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
|
||||
"666f6f6261720000000000000000000000000000000000000000000000000000", // str[1]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32[][]"}]`, |
||||
unpacked: [][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // array[1][2]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32[][2]"}]`, |
||||
unpacked: [2][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // array[1][2]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32[3][2]"}]`, |
||||
unpacked: [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // array[1][2]
|
||||
}, |
||||
{ |
||||
// static tuple
|
||||
def: `[{"components": [{"name":"a","type":"int64"},
|
||||
{"name":"b","type":"int256"},
|
||||
{"name":"c","type":"int256"},
|
||||
{"name":"d","type":"bool"}, |
||||
{"name":"e","type":"bytes32[3][2]"}], "type":"tuple"}]`, |
||||
unpacked: struct { |
||||
A int64 |
||||
B *big.Int |
||||
C *big.Int |
||||
D bool |
||||
E [2][3][32]byte |
||||
}{1, big.NewInt(1), big.NewInt(-1), true, [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + // struct[a]
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c]
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[d]
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][1]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][2]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // struct[e] array[1][2]
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"a","type":"string"},
|
||||
{"name":"b","type":"int64"},
|
||||
{"name":"c","type":"bytes"},
|
||||
{"name":"d","type":"string[]"}, |
||||
{"name":"e","type":"int256[]"}, |
||||
{"name":"f","type":"address[]"}], "type":"tuple"}]`, |
||||
unpacked: struct { |
||||
A string |
||||
B int64 |
||||
C []byte |
||||
D []string |
||||
E []*big.Int |
||||
F []common.Address |
||||
}{"foobar", 1, []byte{1}, []string{"foo", "bar"}, []*big.Int{big.NewInt(1), big.NewInt(-1)}, []common.Address{{1}, {2}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a
|
||||
"00000000000000000000000000000000000000000000000000000000000000c0" + // struct[a] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
|
||||
"0000000000000000000000000000000000000000000000000000000000000100" + // struct[c] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000140" + // struct[d] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000220" + // struct[e] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000280" + // struct[f] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" + // struct[a] length
|
||||
"666f6f6261720000000000000000000000000000000000000000000000000000" + // struct[a] "foobar"
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[c] length
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // []byte{1}
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[d] length
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // foo offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000080" + // bar offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // foo length
|
||||
"666f6f0000000000000000000000000000000000000000000000000000000000" + // foo
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // bar offset
|
||||
"6261720000000000000000000000000000000000000000000000000000000000" + // bar
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[e] length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // 1
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // -1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[f] length
|
||||
"0000000000000000000000000100000000000000000000000000000000000000" + // common.Address{1}
|
||||
"0000000000000000000000000200000000000000000000000000000000000000", // common.Address{2}
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{ "type": "tuple","components": [{"name": "a","type": "uint256"},
|
||||
{"name": "b","type": "uint256[]"}],
|
||||
"name": "a","type": "tuple"}, |
||||
{"name": "b","type": "uint256[]"}], "type": "tuple"}]`, |
||||
unpacked: struct { |
||||
A struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
} |
||||
B []*big.Int |
||||
}{ |
||||
A: struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
}{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(2)}}, |
||||
B: []*big.Int{big.NewInt(1), big.NewInt(2)}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // a offset
|
||||
"00000000000000000000000000000000000000000000000000000000000000e0" + // b offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // a.a value
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // a.b offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // a.b length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // a.b[0] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // a.b[1] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // b length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // b[0] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // b[1] value
|
||||
}, |
||||
|
||||
{ |
||||
def: `[{"components": [{"name": "a","type": "int256"},
|
||||
{"name": "b","type": "int256[]"}],
|
||||
"name": "a","type": "tuple[]"}]`, |
||||
unpacked: []struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
}{ |
||||
{big.NewInt(-1), []*big.Int{big.NewInt(1), big.NewInt(3)}}, |
||||
{big.NewInt(1), []*big.Int{big.NewInt(2), big.NewInt(-1)}}, |
||||
}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple length
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
|
||||
"00000000000000000000000000000000000000000000000000000000000000e0" + // tuple[1] offset
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0].B offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].B length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].B[0] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // tuple[0].B[1] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[1].B offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B length
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B[0] value
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // tuple[1].B[1] value
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name": "a","type": "int256"},
|
||||
{"name": "b","type": "int256"}],
|
||||
"name": "a","type": "tuple[2]"}]`, |
||||
unpacked: [2]struct { |
||||
A *big.Int |
||||
B *big.Int |
||||
}{ |
||||
{big.NewInt(-1), big.NewInt(1)}, |
||||
{big.NewInt(1), big.NewInt(-1)}, |
||||
}, |
||||
packed: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].a
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].b
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].a
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // tuple[1].b
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name": "a","type": "int256[]"}], |
||||
"name": "a","type": "tuple[2]"}]`, |
||||
unpacked: [2]struct { |
||||
A []*big.Int |
||||
}{ |
||||
{[]*big.Int{big.NewInt(-1), big.NewInt(1)}}, |
||||
{[]*big.Int{big.NewInt(1), big.NewInt(-1)}}, |
||||
}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
|
||||
"00000000000000000000000000000000000000000000000000000000000000c0" + // tuple[1] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[0].A offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].A length
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A[0]
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].A[1]
|
||||
"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[1].A offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].A length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A[0]
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // tuple[1].A[1]
|
||||
}, |
||||
} |
@ -0,0 +1,173 @@ |
||||
// Copyright 2018 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/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
"reflect" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// MakeTopics converts a filter query argument list into a filter topic set.
|
||||
func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) { |
||||
topics := make([][]common.Hash, len(query)) |
||||
for i, filter := range query { |
||||
for _, rule := range filter { |
||||
var topic common.Hash |
||||
|
||||
// Try to generate the topic based on simple types
|
||||
switch rule := rule.(type) { |
||||
case common.Hash: |
||||
copy(topic[:], rule[:]) |
||||
case common.Address: |
||||
copy(topic[common.HashLength-common.AddressLength:], rule[:]) |
||||
case *big.Int: |
||||
blob := rule.Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case bool: |
||||
if rule { |
||||
topic[common.HashLength-1] = 1 |
||||
} |
||||
case int8: |
||||
copy(topic[:], genIntType(int64(rule), 1)) |
||||
case int16: |
||||
copy(topic[:], genIntType(int64(rule), 2)) |
||||
case int32: |
||||
copy(topic[:], genIntType(int64(rule), 4)) |
||||
case int64: |
||||
copy(topic[:], genIntType(rule, 8)) |
||||
case uint8: |
||||
blob := new(big.Int).SetUint64(uint64(rule)).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case uint16: |
||||
blob := new(big.Int).SetUint64(uint64(rule)).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case uint32: |
||||
blob := new(big.Int).SetUint64(uint64(rule)).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case uint64: |
||||
blob := new(big.Int).SetUint64(rule).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case string: |
||||
hash := crypto.Keccak256Hash([]byte(rule)) |
||||
copy(topic[:], hash[:]) |
||||
case []byte: |
||||
hash := crypto.Keccak256Hash(rule) |
||||
copy(topic[:], hash[:]) |
||||
|
||||
default: |
||||
// todo(rjl493456442) according solidity documentation, indexed event
|
||||
// parameters that are not value types i.e. arrays and structs are not
|
||||
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||
//
|
||||
// We only convert stringS and bytes to hash, still need to deal with
|
||||
// array(both fixed-size and dynamic-size) and struct.
|
||||
|
||||
// Attempt to generate the topic from funky types
|
||||
val := reflect.ValueOf(rule) |
||||
switch { |
||||
// static byte array
|
||||
case val.Kind() == reflect.Array && reflect.TypeOf(rule).Elem().Kind() == reflect.Uint8: |
||||
reflect.Copy(reflect.ValueOf(topic[:val.Len()]), val) |
||||
default: |
||||
return nil, fmt.Errorf("unsupported indexed type: %T", rule) |
||||
} |
||||
} |
||||
topics[i] = append(topics[i], topic) |
||||
} |
||||
} |
||||
return topics, nil |
||||
} |
||||
|
||||
func genIntType(rule int64, size uint) []byte { |
||||
var topic [common.HashLength]byte |
||||
if rule < 0 { |
||||
// if a rule is negative, we need to put it into two's complement.
|
||||
// extended to common.HashLength bytes.
|
||||
topic = [common.HashLength]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} |
||||
} |
||||
for i := uint(0); i < size; i++ { |
||||
topic[common.HashLength-i-1] = byte(rule >> (i * 8)) |
||||
} |
||||
return topic[:] |
||||
} |
||||
|
||||
// ParseTopics converts the indexed topic fields into actual log field values.
|
||||
func ParseTopics(out interface{}, fields Arguments, topics []common.Hash) error { |
||||
return parseTopicWithSetter(fields, topics, |
||||
func(arg Argument, reconstr interface{}) { |
||||
field := reflect.ValueOf(out).Elem().FieldByName(ToCamelCase(arg.Name)) |
||||
field.Set(reflect.ValueOf(reconstr)) |
||||
}) |
||||
} |
||||
|
||||
// ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs.
|
||||
func ParseTopicsIntoMap(out map[string]interface{}, fields Arguments, topics []common.Hash) error { |
||||
return parseTopicWithSetter(fields, topics, |
||||
func(arg Argument, reconstr interface{}) { |
||||
out[arg.Name] = reconstr |
||||
}) |
||||
} |
||||
|
||||
// parseTopicWithSetter converts the indexed topic field-value pairs and stores them using the
|
||||
// provided set function.
|
||||
//
|
||||
// Note, dynamic types cannot be reconstructed since they get mapped to Keccak256
|
||||
// hashes as the topic value!
|
||||
func parseTopicWithSetter(fields Arguments, topics []common.Hash, setter func(Argument, interface{})) error { |
||||
// Sanity check that the fields and topics match up
|
||||
if len(fields) != len(topics) { |
||||
return errors.New("topic/field count mismatch") |
||||
} |
||||
// Iterate over all the fields and reconstruct them from topics
|
||||
for i, arg := range fields { |
||||
if !arg.Indexed { |
||||
return errors.New("non-indexed field in topic reconstruction") |
||||
} |
||||
var reconstr interface{} |
||||
switch arg.Type.T { |
||||
case TupleTy: |
||||
return errors.New("tuple type in topic reconstruction") |
||||
case StringTy, BytesTy, SliceTy, ArrayTy: |
||||
// Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash
|
||||
// whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash
|
||||
reconstr = topics[i] |
||||
case FunctionTy: |
||||
if garbage := binary.BigEndian.Uint64(topics[i][0:8]); garbage != 0 { |
||||
return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) |
||||
} |
||||
var tmp [24]byte |
||||
copy(tmp[:], topics[i][8:32]) |
||||
reconstr = tmp |
||||
default: |
||||
var err error |
||||
reconstr, err = toGoType(0, arg.Type, topics[i].Bytes()) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
// Use the setter function to store the value
|
||||
setter(arg, reconstr) |
||||
} |
||||
|
||||
return nil |
||||
} |
@ -0,0 +1,381 @@ |
||||
// Copyright 2019 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/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"math/big" |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
func TestMakeTopics(t *testing.T) { |
||||
type args struct { |
||||
query [][]interface{} |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want [][]common.Hash |
||||
wantErr bool |
||||
}{ |
||||
{ |
||||
"support fixed byte types, right padded to 32 bytes", |
||||
args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}}, |
||||
[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support common hash types in topics", |
||||
args{[][]interface{}{{common.Hash{1, 2, 3, 4, 5}}}}, |
||||
[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support address types in topics", |
||||
args{[][]interface{}{{common.Address{1, 2, 3, 4, 5}}}}, |
||||
[][]common.Hash{{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support *big.Int types in topics", |
||||
args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}}, |
||||
[][]common.Hash{{common.Hash{128}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support boolean types in topics", |
||||
args{[][]interface{}{ |
||||
{true}, |
||||
{false}, |
||||
}}, |
||||
[][]common.Hash{ |
||||
{common.Hash{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}}, |
||||
{common.Hash{0}}, |
||||
}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support int/uint(8/16/32/64) types in topics", |
||||
args{[][]interface{}{ |
||||
{int8(-2)}, |
||||
{int16(-3)}, |
||||
{int32(-4)}, |
||||
{int64(-5)}, |
||||
{int8(1)}, |
||||
{int16(256)}, |
||||
{int32(65536)}, |
||||
{int64(4294967296)}, |
||||
{uint8(1)}, |
||||
{uint16(256)}, |
||||
{uint32(65536)}, |
||||
{uint64(4294967296)}, |
||||
}}, |
||||
[][]common.Hash{ |
||||
{common.Hash{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, 254}}, |
||||
{common.Hash{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, 253}}, |
||||
{common.Hash{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, 252}}, |
||||
{common.Hash{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, 251}}, |
||||
{common.Hash{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}}, |
||||
{common.Hash{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, 0}}, |
||||
{common.Hash{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, 0, 0}}, |
||||
{common.Hash{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, 0, 0, 0, 0}}, |
||||
{common.Hash{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}}, |
||||
{common.Hash{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, 0}}, |
||||
{common.Hash{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, 0, 0}}, |
||||
{common.Hash{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, 0, 0, 0, 0}}, |
||||
}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support string types in topics", |
||||
args{[][]interface{}{{"hello world"}}}, |
||||
[][]common.Hash{{crypto.Keccak256Hash([]byte("hello world"))}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support byte slice types in topics", |
||||
args{[][]interface{}{{[]byte{1, 2, 3}}}}, |
||||
[][]common.Hash{{crypto.Keccak256Hash([]byte{1, 2, 3})}}, |
||||
false, |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
got, err := MakeTopics(tt.args.query...) |
||||
if (err != nil) != tt.wantErr { |
||||
t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr) |
||||
return |
||||
} |
||||
if !reflect.DeepEqual(got, tt.want) { |
||||
t.Errorf("makeTopics() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
type args struct { |
||||
createObj func() interface{} |
||||
resultObj func() interface{} |
||||
resultMap func() map[string]interface{} |
||||
fields Arguments |
||||
topics []common.Hash |
||||
} |
||||
|
||||
type bytesStruct struct { |
||||
StaticBytes [5]byte |
||||
} |
||||
type int8Struct struct { |
||||
Int8Value int8 |
||||
} |
||||
type int256Struct struct { |
||||
Int256Value *big.Int |
||||
} |
||||
|
||||
type hashStruct struct { |
||||
HashValue common.Hash |
||||
} |
||||
|
||||
type funcStruct struct { |
||||
FuncValue [24]byte |
||||
} |
||||
|
||||
type topicTest struct { |
||||
name string |
||||
args args |
||||
wantErr bool |
||||
} |
||||
|
||||
func setupTopicsTests() []topicTest { |
||||
bytesType, _ := NewType("bytes5", "", nil) |
||||
int8Type, _ := NewType("int8", "", nil) |
||||
int256Type, _ := NewType("int256", "", nil) |
||||
tupleType, _ := NewType("tuple(int256,int8)", "", nil) |
||||
stringType, _ := NewType("string", "", nil) |
||||
funcType, _ := NewType("function", "", nil) |
||||
|
||||
tests := []topicTest{ |
||||
{ |
||||
name: "support fixed byte types, right padded to 32 bytes", |
||||
args: args{ |
||||
createObj: func() interface{} { return &bytesStruct{} }, |
||||
resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"staticBytes": [5]byte{1, 2, 3, 4, 5}} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "staticBytes", |
||||
Type: bytesType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{1, 2, 3, 4, 5}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "int8 with negative value", |
||||
args: args{ |
||||
createObj: func() interface{} { return &int8Struct{} }, |
||||
resultObj: func() interface{} { return &int8Struct{Int8Value: -1} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"int8Value": int8(-1)} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "int8Value", |
||||
Type: int8Type, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{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}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "int256 with negative value", |
||||
args: args{ |
||||
createObj: func() interface{} { return &int256Struct{} }, |
||||
resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"int256Value": big.NewInt(-1)} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "int256Value", |
||||
Type: int256Type, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{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}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "hash type", |
||||
args: args{ |
||||
createObj: func() interface{} { return &hashStruct{} }, |
||||
resultObj: func() interface{} { return &hashStruct{crypto.Keccak256Hash([]byte("stringtopic"))} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"hashValue": crypto.Keccak256Hash([]byte("stringtopic"))} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "hashValue", |
||||
Type: stringType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("stringtopic")), |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "function type", |
||||
args: args{ |
||||
createObj: func() interface{} { return &funcStruct{} }, |
||||
resultObj: func() interface{} { |
||||
return &funcStruct{[24]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}} |
||||
}, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"funcValue": [24]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}} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "funcValue", |
||||
Type: funcType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "error on topic/field count mismatch", |
||||
args: args{ |
||||
createObj: func() interface{} { return nil }, |
||||
resultObj: func() interface{} { return nil }, |
||||
resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, |
||||
fields: Arguments{Argument{ |
||||
Name: "tupletype", |
||||
Type: tupleType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
{ |
||||
name: "error on unindexed arguments", |
||||
args: args{ |
||||
createObj: func() interface{} { return &int256Struct{} }, |
||||
resultObj: func() interface{} { return &int256Struct{} }, |
||||
resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, |
||||
fields: Arguments{Argument{ |
||||
Name: "int256Value", |
||||
Type: int256Type, |
||||
Indexed: false, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{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}, |
||||
}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
{ |
||||
name: "error on tuple in topic reconstruction", |
||||
args: args{ |
||||
createObj: func() interface{} { return &tupleType }, |
||||
resultObj: func() interface{} { return &tupleType }, |
||||
resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, |
||||
fields: Arguments{Argument{ |
||||
Name: "tupletype", |
||||
Type: tupleType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{{0}}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
{ |
||||
name: "error on improper encoded function", |
||||
args: args{ |
||||
createObj: func() interface{} { return &funcStruct{} }, |
||||
resultObj: func() interface{} { return &funcStruct{} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return make(map[string]interface{}) |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "funcValue", |
||||
Type: funcType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{0, 0, 0, 0, 0, 0, 0, 128, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
} |
||||
|
||||
return tests |
||||
} |
||||
|
||||
func TestParseTopics(t *testing.T) { |
||||
tests := setupTopicsTests() |
||||
|
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
createObj := tt.args.createObj() |
||||
if err := ParseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { |
||||
t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr) |
||||
} |
||||
resultObj := tt.args.resultObj() |
||||
if !reflect.DeepEqual(createObj, resultObj) { |
||||
t.Errorf("parseTopics() = %v, want %v", createObj, resultObj) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestParseTopicsIntoMap(t *testing.T) { |
||||
tests := setupTopicsTests() |
||||
|
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
outMap := make(map[string]interface{}) |
||||
if err := ParseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { |
||||
t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr) |
||||
} |
||||
resultMap := tt.args.resultMap() |
||||
if !reflect.DeepEqual(outMap, resultMap) { |
||||
t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap) |
||||
} |
||||
}) |
||||
} |
||||
} |
Loading…
Reference in new issue