base_test.go 9.7 KB

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