base_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bind_test
  17. import (
  18. "context"
  19. "math/big"
  20. "reflect"
  21. "strings"
  22. "testing"
  23. "github.com/ethereum/go-ethereum"
  24. "github.com/ethereum/go-ethereum/accounts/abi"
  25. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/common/hexutil"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/rlp"
  31. )
  32. type mockCaller struct {
  33. codeAtBlockNumber *big.Int
  34. callContractBlockNumber *big.Int
  35. }
  36. func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
  37. mc.codeAtBlockNumber = blockNumber
  38. return []byte{1, 2, 3}, nil
  39. }
  40. func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
  41. mc.callContractBlockNumber = blockNumber
  42. return nil, nil
  43. }
  44. func TestPassingBlockNumber(t *testing.T) {
  45. mc := &mockCaller{}
  46. bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{
  47. Methods: map[string]abi.Method{
  48. "something": {
  49. Name: "something",
  50. Outputs: abi.Arguments{},
  51. },
  52. },
  53. }, mc, nil, nil)
  54. var ret string
  55. blockNumber := big.NewInt(42)
  56. bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, &ret, "something")
  57. if mc.callContractBlockNumber != blockNumber {
  58. t.Fatalf("CallContract() was not passed the block number")
  59. }
  60. if mc.codeAtBlockNumber != blockNumber {
  61. t.Fatalf("CodeAt() was not passed the block number")
  62. }
  63. bc.Call(&bind.CallOpts{}, &ret, "something")
  64. if mc.callContractBlockNumber != nil {
  65. t.Fatalf("CallContract() was passed a block number when it should not have been")
  66. }
  67. if mc.codeAtBlockNumber != nil {
  68. t.Fatalf("CodeAt() was passed a block number when it should not have been")
  69. }
  70. }
  71. const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158"
  72. func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
  73. hash := crypto.Keccak256Hash([]byte("testName"))
  74. topics := []common.Hash{
  75. common.HexToHash("0x0"),
  76. hash,
  77. }
  78. mockLog := newMockLog(topics, common.HexToHash("0x0"))
  79. 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"}]`
  80. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  81. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  82. expectedReceivedMap := map[string]interface{}{
  83. "name": hash,
  84. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  85. "amount": big.NewInt(1),
  86. "memo": []byte{88},
  87. }
  88. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  89. }
  90. func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
  91. sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. hash := crypto.Keccak256Hash(sliceBytes)
  96. topics := []common.Hash{
  97. common.HexToHash("0x0"),
  98. hash,
  99. }
  100. mockLog := newMockLog(topics, common.HexToHash("0x0"))
  101. 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"}]`
  102. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  103. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  104. expectedReceivedMap := map[string]interface{}{
  105. "names": hash,
  106. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  107. "amount": big.NewInt(1),
  108. "memo": []byte{88},
  109. }
  110. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  111. }
  112. func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
  113. arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")})
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. hash := crypto.Keccak256Hash(arrBytes)
  118. topics := []common.Hash{
  119. common.HexToHash("0x0"),
  120. hash,
  121. }
  122. mockLog := newMockLog(topics, common.HexToHash("0x0"))
  123. 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"}]`
  124. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  125. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  126. expectedReceivedMap := map[string]interface{}{
  127. "addresses": hash,
  128. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  129. "amount": big.NewInt(1),
  130. "memo": []byte{88},
  131. }
  132. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  133. }
  134. func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
  135. mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
  136. addrBytes := mockAddress.Bytes()
  137. hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)"))
  138. functionSelector := hash[:4]
  139. functionTyBytes := append(addrBytes, functionSelector...)
  140. var functionTy [24]byte
  141. copy(functionTy[:], functionTyBytes[0:24])
  142. topics := []common.Hash{
  143. common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
  144. common.BytesToHash(functionTyBytes),
  145. }
  146. mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
  147. 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"}]`
  148. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  149. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  150. expectedReceivedMap := map[string]interface{}{
  151. "function": functionTy,
  152. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  153. "amount": big.NewInt(1),
  154. "memo": []byte{88},
  155. }
  156. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  157. }
  158. func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
  159. bytes := []byte{1, 2, 3, 4, 5}
  160. hash := crypto.Keccak256Hash(bytes)
  161. topics := []common.Hash{
  162. common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
  163. hash,
  164. }
  165. mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
  166. 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"}]`
  167. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  168. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  169. expectedReceivedMap := map[string]interface{}{
  170. "content": hash,
  171. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  172. "amount": big.NewInt(1),
  173. "memo": []byte{88},
  174. }
  175. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  176. }
  177. func unpackAndCheck(t *testing.T, bc *bind.BoundContract, expected map[string]interface{}, mockLog types.Log) {
  178. received := make(map[string]interface{})
  179. if err := bc.UnpackLogIntoMap(received, "received", mockLog); err != nil {
  180. t.Error(err)
  181. }
  182. if len(received) != len(expected) {
  183. t.Fatalf("unpacked map length %v not equal expected length of %v", len(received), len(expected))
  184. }
  185. for name, elem := range expected {
  186. if !reflect.DeepEqual(elem, received[name]) {
  187. t.Errorf("field %v does not match expected, want %v, got %v", name, elem, received[name])
  188. }
  189. }
  190. }
  191. func newMockLog(topics []common.Hash, txHash common.Hash) types.Log {
  192. return types.Log{
  193. Address: common.HexToAddress("0x0"),
  194. Topics: topics,
  195. Data: hexutil.MustDecode(hexData),
  196. BlockNumber: uint64(26),
  197. TxHash: txHash,
  198. TxIndex: 111,
  199. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  200. Index: 7,
  201. Removed: false,
  202. }
  203. }