2019-02-20 07:08:54 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2019-01-03 11:54:24 +00:00
|
|
|
package bind_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
2020-04-27 13:07:33 +00:00
|
|
|
"reflect"
|
2019-04-01 13:42:59 +00:00
|
|
|
"strings"
|
2019-01-03 11:54:24 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-04-01 13:42:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2019-01-03 11:54:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-04-01 13:42:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2019-01-03 11:54:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockCaller struct {
|
|
|
|
codeAtBlockNumber *big.Int
|
|
|
|
callContractBlockNumber *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
mc.codeAtBlockNumber = blockNumber
|
|
|
|
return []byte{1, 2, 3}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
mc.callContractBlockNumber = blockNumber
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func TestPassingBlockNumber(t *testing.T) {
|
|
|
|
|
|
|
|
mc := &mockCaller{}
|
|
|
|
|
|
|
|
bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{
|
|
|
|
Methods: map[string]abi.Method{
|
|
|
|
"something": {
|
|
|
|
Name: "something",
|
|
|
|
Outputs: abi.Arguments{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, mc, nil, nil)
|
|
|
|
var ret string
|
|
|
|
|
|
|
|
blockNumber := big.NewInt(42)
|
|
|
|
|
|
|
|
bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, &ret, "something")
|
|
|
|
|
|
|
|
if mc.callContractBlockNumber != blockNumber {
|
|
|
|
t.Fatalf("CallContract() was not passed the block number")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mc.codeAtBlockNumber != blockNumber {
|
|
|
|
t.Fatalf("CodeAt() was not passed the block number")
|
|
|
|
}
|
|
|
|
|
|
|
|
bc.Call(&bind.CallOpts{}, &ret, "something")
|
|
|
|
|
|
|
|
if mc.callContractBlockNumber != nil {
|
|
|
|
t.Fatalf("CallContract() was passed a block number when it should not have been")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mc.codeAtBlockNumber != nil {
|
|
|
|
t.Fatalf("CodeAt() was passed a block number when it should not have been")
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 13:42:59 +00:00
|
|
|
|
|
|
|
const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158"
|
|
|
|
|
|
|
|
func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
|
|
|
|
hash := crypto.Keccak256Hash([]byte("testName"))
|
2020-04-27 13:07:33 +00:00
|
|
|
topics := []common.Hash{
|
|
|
|
common.HexToHash("0x0"),
|
|
|
|
hash,
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
mockLog := newMockLog(topics, common.HexToHash("0x0"))
|
2019-04-01 13:42:59 +00:00
|
|
|
|
|
|
|
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]`
|
|
|
|
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
|
|
|
|
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
|
|
|
|
|
|
|
|
expectedReceivedMap := map[string]interface{}{
|
|
|
|
"name": hash,
|
|
|
|
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
|
|
|
|
"amount": big.NewInt(1),
|
|
|
|
"memo": []byte{88},
|
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
|
|
|
|
sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
hash := crypto.Keccak256Hash(sliceBytes)
|
2020-04-27 13:07:33 +00:00
|
|
|
topics := []common.Hash{
|
|
|
|
common.HexToHash("0x0"),
|
|
|
|
hash,
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
mockLog := newMockLog(topics, common.HexToHash("0x0"))
|
2019-04-01 13:42:59 +00:00
|
|
|
|
|
|
|
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"names","type":"string[]"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]`
|
|
|
|
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
|
|
|
|
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
|
|
|
|
|
|
|
|
expectedReceivedMap := map[string]interface{}{
|
|
|
|
"names": hash,
|
|
|
|
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
|
|
|
|
"amount": big.NewInt(1),
|
|
|
|
"memo": []byte{88},
|
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
|
|
|
|
arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
hash := crypto.Keccak256Hash(arrBytes)
|
2020-04-27 13:07:33 +00:00
|
|
|
topics := []common.Hash{
|
|
|
|
common.HexToHash("0x0"),
|
|
|
|
hash,
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
mockLog := newMockLog(topics, common.HexToHash("0x0"))
|
2019-04-01 13:42:59 +00:00
|
|
|
|
|
|
|
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"addresses","type":"address[2]"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]`
|
|
|
|
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
|
|
|
|
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
|
|
|
|
|
|
|
|
expectedReceivedMap := map[string]interface{}{
|
|
|
|
"addresses": hash,
|
|
|
|
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
|
|
|
|
"amount": big.NewInt(1),
|
|
|
|
"memo": []byte{88},
|
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
|
|
|
|
mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
|
|
|
|
addrBytes := mockAddress.Bytes()
|
|
|
|
hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)"))
|
|
|
|
functionSelector := hash[:4]
|
|
|
|
functionTyBytes := append(addrBytes, functionSelector...)
|
|
|
|
var functionTy [24]byte
|
|
|
|
copy(functionTy[:], functionTyBytes[0:24])
|
2020-04-27 13:07:33 +00:00
|
|
|
topics := []common.Hash{
|
|
|
|
common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
|
|
|
|
common.BytesToHash(functionTyBytes),
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
|
2019-04-01 13:42:59 +00:00
|
|
|
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"function","type":"function"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]`
|
|
|
|
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
|
|
|
|
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
|
|
|
|
|
|
|
|
expectedReceivedMap := map[string]interface{}{
|
|
|
|
"function": functionTy,
|
|
|
|
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
|
|
|
|
"amount": big.NewInt(1),
|
|
|
|
"memo": []byte{88},
|
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
|
2020-04-27 13:07:33 +00:00
|
|
|
bytes := []byte{1, 2, 3, 4, 5}
|
|
|
|
hash := crypto.Keccak256Hash(bytes)
|
|
|
|
topics := []common.Hash{
|
|
|
|
common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
|
|
|
|
hash,
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
|
2019-04-01 13:42:59 +00:00
|
|
|
|
|
|
|
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"content","type":"bytes"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]`
|
|
|
|
parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
|
|
|
|
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
|
|
|
|
|
|
|
|
expectedReceivedMap := map[string]interface{}{
|
|
|
|
"content": hash,
|
|
|
|
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
|
|
|
|
"amount": big.NewInt(1),
|
|
|
|
"memo": []byte{88},
|
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unpackAndCheck(t *testing.T, bc *bind.BoundContract, expected map[string]interface{}, mockLog types.Log) {
|
|
|
|
received := make(map[string]interface{})
|
|
|
|
if err := bc.UnpackLogIntoMap(received, "received", mockLog); err != nil {
|
2019-04-01 13:42:59 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2020-04-27 13:07:33 +00:00
|
|
|
if len(received) != len(expected) {
|
|
|
|
t.Fatalf("unpacked map length %v not equal expected length of %v", len(received), len(expected))
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
for name, elem := range expected {
|
|
|
|
if !reflect.DeepEqual(elem, received[name]) {
|
|
|
|
t.Errorf("field %v does not match expected, want %v, got %v", name, elem, received[name])
|
|
|
|
}
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
2020-04-27 13:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockLog(topics []common.Hash, txHash common.Hash) types.Log {
|
|
|
|
return types.Log{
|
|
|
|
Address: common.HexToAddress("0x0"),
|
|
|
|
Topics: topics,
|
|
|
|
Data: hexutil.MustDecode(hexData),
|
|
|
|
BlockNumber: uint64(26),
|
|
|
|
TxHash: txHash,
|
|
|
|
TxIndex: 111,
|
|
|
|
BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
|
|
|
|
Index: 7,
|
|
|
|
Removed: false,
|
2019-04-01 13:42:59 +00:00
|
|
|
}
|
|
|
|
}
|