base_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. pendingCodeAtCalled bool
  36. pendingCallContractCalled bool
  37. }
  38. func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
  39. mc.codeAtBlockNumber = blockNumber
  40. return []byte{1, 2, 3}, nil
  41. }
  42. func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
  43. mc.callContractBlockNumber = blockNumber
  44. return nil, nil
  45. }
  46. func (mc *mockCaller) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
  47. mc.pendingCodeAtCalled = true
  48. return nil, nil
  49. }
  50. func (mc *mockCaller) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) {
  51. mc.pendingCallContractCalled = true
  52. return nil, nil
  53. }
  54. func TestPassingBlockNumber(t *testing.T) {
  55. mc := &mockCaller{}
  56. bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{
  57. Methods: map[string]abi.Method{
  58. "something": {
  59. Name: "something",
  60. Outputs: abi.Arguments{},
  61. },
  62. },
  63. }, mc, nil, nil)
  64. blockNumber := big.NewInt(42)
  65. bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, nil, "something")
  66. if mc.callContractBlockNumber != blockNumber {
  67. t.Fatalf("CallContract() was not passed the block number")
  68. }
  69. if mc.codeAtBlockNumber != blockNumber {
  70. t.Fatalf("CodeAt() was not passed the block number")
  71. }
  72. bc.Call(&bind.CallOpts{}, nil, "something")
  73. if mc.callContractBlockNumber != nil {
  74. t.Fatalf("CallContract() was passed a block number when it should not have been")
  75. }
  76. if mc.codeAtBlockNumber != nil {
  77. t.Fatalf("CodeAt() was passed a block number when it should not have been")
  78. }
  79. bc.Call(&bind.CallOpts{BlockNumber: blockNumber, Pending: true}, nil, "something")
  80. if !mc.pendingCallContractCalled {
  81. t.Fatalf("CallContract() was not passed the block number")
  82. }
  83. if !mc.pendingCodeAtCalled {
  84. t.Fatalf("CodeAt() was not passed the block number")
  85. }
  86. }
  87. const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158"
  88. func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
  89. hash := crypto.Keccak256Hash([]byte("testName"))
  90. topics := []common.Hash{
  91. common.HexToHash("0x0"),
  92. hash,
  93. }
  94. mockLog := newMockLog(topics, common.HexToHash("0x0"))
  95. 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"}]`
  96. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  97. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  98. expectedReceivedMap := map[string]interface{}{
  99. "name": hash,
  100. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  101. "amount": big.NewInt(1),
  102. "memo": []byte{88},
  103. }
  104. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  105. }
  106. func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
  107. sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. hash := crypto.Keccak256Hash(sliceBytes)
  112. topics := []common.Hash{
  113. common.HexToHash("0x0"),
  114. hash,
  115. }
  116. mockLog := newMockLog(topics, common.HexToHash("0x0"))
  117. 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"}]`
  118. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  119. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  120. expectedReceivedMap := map[string]interface{}{
  121. "names": hash,
  122. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  123. "amount": big.NewInt(1),
  124. "memo": []byte{88},
  125. }
  126. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  127. }
  128. func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
  129. arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")})
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. hash := crypto.Keccak256Hash(arrBytes)
  134. topics := []common.Hash{
  135. common.HexToHash("0x0"),
  136. hash,
  137. }
  138. mockLog := newMockLog(topics, common.HexToHash("0x0"))
  139. 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"}]`
  140. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  141. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  142. expectedReceivedMap := map[string]interface{}{
  143. "addresses": hash,
  144. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  145. "amount": big.NewInt(1),
  146. "memo": []byte{88},
  147. }
  148. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  149. }
  150. func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
  151. mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
  152. addrBytes := mockAddress.Bytes()
  153. hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)"))
  154. functionSelector := hash[:4]
  155. functionTyBytes := append(addrBytes, functionSelector...)
  156. var functionTy [24]byte
  157. copy(functionTy[:], functionTyBytes[0:24])
  158. topics := []common.Hash{
  159. common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
  160. common.BytesToHash(functionTyBytes),
  161. }
  162. mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
  163. 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"}]`
  164. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  165. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  166. expectedReceivedMap := map[string]interface{}{
  167. "function": functionTy,
  168. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  169. "amount": big.NewInt(1),
  170. "memo": []byte{88},
  171. }
  172. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  173. }
  174. func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
  175. bytes := []byte{1, 2, 3, 4, 5}
  176. hash := crypto.Keccak256Hash(bytes)
  177. topics := []common.Hash{
  178. common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
  179. hash,
  180. }
  181. mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
  182. 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"}]`
  183. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  184. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  185. expectedReceivedMap := map[string]interface{}{
  186. "content": hash,
  187. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  188. "amount": big.NewInt(1),
  189. "memo": []byte{88},
  190. }
  191. unpackAndCheck(t, bc, expectedReceivedMap, mockLog)
  192. }
  193. func unpackAndCheck(t *testing.T, bc *bind.BoundContract, expected map[string]interface{}, mockLog types.Log) {
  194. received := make(map[string]interface{})
  195. if err := bc.UnpackLogIntoMap(received, "received", mockLog); err != nil {
  196. t.Error(err)
  197. }
  198. if len(received) != len(expected) {
  199. t.Fatalf("unpacked map length %v not equal expected length of %v", len(received), len(expected))
  200. }
  201. for name, elem := range expected {
  202. if !reflect.DeepEqual(elem, received[name]) {
  203. t.Errorf("field %v does not match expected, want %v, got %v", name, elem, received[name])
  204. }
  205. }
  206. }
  207. func newMockLog(topics []common.Hash, txHash common.Hash) types.Log {
  208. return types.Log{
  209. Address: common.HexToAddress("0x0"),
  210. Topics: topics,
  211. Data: hexutil.MustDecode(hexData),
  212. BlockNumber: uint64(26),
  213. TxHash: txHash,
  214. TxIndex: 111,
  215. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  216. Index: 7,
  217. Removed: false,
  218. }
  219. }