base_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. "bytes"
  19. "context"
  20. "math/big"
  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. mockLog := types.Log{
  75. Address: common.HexToAddress("0x0"),
  76. Topics: []common.Hash{
  77. common.HexToHash("0x0"),
  78. hash,
  79. },
  80. Data: hexutil.MustDecode(hexData),
  81. BlockNumber: uint64(26),
  82. TxHash: common.HexToHash("0x0"),
  83. TxIndex: 111,
  84. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  85. Index: 7,
  86. Removed: false,
  87. }
  88. 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"}]`
  89. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  90. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  91. receivedMap := make(map[string]interface{})
  92. expectedReceivedMap := map[string]interface{}{
  93. "name": hash,
  94. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  95. "amount": big.NewInt(1),
  96. "memo": []byte{88},
  97. }
  98. if err := bc.UnpackLogIntoMap(receivedMap, "received", mockLog); err != nil {
  99. t.Error(err)
  100. }
  101. if len(receivedMap) != 4 {
  102. t.Fatal("unpacked map expected to have length 4")
  103. }
  104. if receivedMap["name"] != expectedReceivedMap["name"] {
  105. t.Error("unpacked map does not match expected map")
  106. }
  107. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  108. t.Error("unpacked map does not match expected map")
  109. }
  110. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  111. t.Error("unpacked map does not match expected map")
  112. }
  113. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  114. t.Error("unpacked map does not match expected map")
  115. }
  116. }
  117. func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
  118. sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"})
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. hash := crypto.Keccak256Hash(sliceBytes)
  123. mockLog := types.Log{
  124. Address: common.HexToAddress("0x0"),
  125. Topics: []common.Hash{
  126. common.HexToHash("0x0"),
  127. hash,
  128. },
  129. Data: hexutil.MustDecode(hexData),
  130. BlockNumber: uint64(26),
  131. TxHash: common.HexToHash("0x0"),
  132. TxIndex: 111,
  133. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  134. Index: 7,
  135. Removed: false,
  136. }
  137. 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"}]`
  138. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  139. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  140. receivedMap := make(map[string]interface{})
  141. expectedReceivedMap := map[string]interface{}{
  142. "names": hash,
  143. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  144. "amount": big.NewInt(1),
  145. "memo": []byte{88},
  146. }
  147. if err := bc.UnpackLogIntoMap(receivedMap, "received", mockLog); err != nil {
  148. t.Error(err)
  149. }
  150. if len(receivedMap) != 4 {
  151. t.Fatal("unpacked map expected to have length 4")
  152. }
  153. if receivedMap["names"] != expectedReceivedMap["names"] {
  154. t.Error("unpacked map does not match expected map")
  155. }
  156. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  157. t.Error("unpacked map does not match expected map")
  158. }
  159. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  160. t.Error("unpacked map does not match expected map")
  161. }
  162. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  163. t.Error("unpacked map does not match expected map")
  164. }
  165. }
  166. func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
  167. arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")})
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. hash := crypto.Keccak256Hash(arrBytes)
  172. mockLog := types.Log{
  173. Address: common.HexToAddress("0x0"),
  174. Topics: []common.Hash{
  175. common.HexToHash("0x0"),
  176. hash,
  177. },
  178. Data: hexutil.MustDecode(hexData),
  179. BlockNumber: uint64(26),
  180. TxHash: common.HexToHash("0x0"),
  181. TxIndex: 111,
  182. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  183. Index: 7,
  184. Removed: false,
  185. }
  186. 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"}]`
  187. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  188. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  189. receivedMap := make(map[string]interface{})
  190. expectedReceivedMap := map[string]interface{}{
  191. "addresses": hash,
  192. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  193. "amount": big.NewInt(1),
  194. "memo": []byte{88},
  195. }
  196. if err := bc.UnpackLogIntoMap(receivedMap, "received", mockLog); err != nil {
  197. t.Error(err)
  198. }
  199. if len(receivedMap) != 4 {
  200. t.Fatal("unpacked map expected to have length 4")
  201. }
  202. if receivedMap["addresses"] != expectedReceivedMap["addresses"] {
  203. t.Error("unpacked map does not match expected map")
  204. }
  205. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  206. t.Error("unpacked map does not match expected map")
  207. }
  208. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  209. t.Error("unpacked map does not match expected map")
  210. }
  211. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  212. t.Error("unpacked map does not match expected map")
  213. }
  214. }
  215. func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
  216. mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
  217. addrBytes := mockAddress.Bytes()
  218. hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)"))
  219. functionSelector := hash[:4]
  220. functionTyBytes := append(addrBytes, functionSelector...)
  221. var functionTy [24]byte
  222. copy(functionTy[:], functionTyBytes[0:24])
  223. mockLog := types.Log{
  224. Address: common.HexToAddress("0x0"),
  225. Topics: []common.Hash{
  226. common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
  227. common.BytesToHash(functionTyBytes),
  228. },
  229. Data: hexutil.MustDecode(hexData),
  230. BlockNumber: uint64(26),
  231. TxHash: common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"),
  232. TxIndex: 111,
  233. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  234. Index: 7,
  235. Removed: false,
  236. }
  237. 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"}]`
  238. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  239. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  240. receivedMap := make(map[string]interface{})
  241. expectedReceivedMap := map[string]interface{}{
  242. "function": functionTy,
  243. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  244. "amount": big.NewInt(1),
  245. "memo": []byte{88},
  246. }
  247. if err := bc.UnpackLogIntoMap(receivedMap, "received", mockLog); err != nil {
  248. t.Error(err)
  249. }
  250. if len(receivedMap) != 4 {
  251. t.Fatal("unpacked map expected to have length 4")
  252. }
  253. if receivedMap["function"] != expectedReceivedMap["function"] {
  254. t.Error("unpacked map does not match expected map")
  255. }
  256. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  257. t.Error("unpacked map does not match expected map")
  258. }
  259. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  260. t.Error("unpacked map does not match expected map")
  261. }
  262. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  263. t.Error("unpacked map does not match expected map")
  264. }
  265. }
  266. func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
  267. byts := []byte{1, 2, 3, 4, 5}
  268. hash := crypto.Keccak256Hash(byts)
  269. mockLog := types.Log{
  270. Address: common.HexToAddress("0x0"),
  271. Topics: []common.Hash{
  272. common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
  273. hash,
  274. },
  275. Data: hexutil.MustDecode(hexData),
  276. BlockNumber: uint64(26),
  277. TxHash: common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"),
  278. TxIndex: 111,
  279. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  280. Index: 7,
  281. Removed: false,
  282. }
  283. 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"}]`
  284. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  285. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  286. receivedMap := make(map[string]interface{})
  287. expectedReceivedMap := map[string]interface{}{
  288. "content": hash,
  289. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  290. "amount": big.NewInt(1),
  291. "memo": []byte{88},
  292. }
  293. if err := bc.UnpackLogIntoMap(receivedMap, "received", mockLog); err != nil {
  294. t.Error(err)
  295. }
  296. if len(receivedMap) != 4 {
  297. t.Fatal("unpacked map expected to have length 4")
  298. }
  299. if receivedMap["content"] != expectedReceivedMap["content"] {
  300. t.Error("unpacked map does not match expected map")
  301. }
  302. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  303. t.Error("unpacked map does not match expected map")
  304. }
  305. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  306. t.Error("unpacked map does not match expected map")
  307. }
  308. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  309. t.Error("unpacked map does not match expected map")
  310. }
  311. }
  312. func TestUnpackIntoMapNamingConflict(t *testing.T) {
  313. hash := crypto.Keccak256Hash([]byte("testName"))
  314. mockLog := types.Log{
  315. Address: common.HexToAddress("0x0"),
  316. Topics: []common.Hash{
  317. common.HexToHash("0x0"),
  318. hash,
  319. },
  320. Data: hexutil.MustDecode(hexData),
  321. BlockNumber: uint64(26),
  322. TxHash: common.HexToHash("0x0"),
  323. TxIndex: 111,
  324. BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}),
  325. Index: 7,
  326. Removed: false,
  327. }
  328. 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"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"received","type":"event"}]`
  329. parsedAbi, _ := abi.JSON(strings.NewReader(abiString))
  330. bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil)
  331. receivedMap := make(map[string]interface{})
  332. if err := bc.UnpackLogIntoMap(receivedMap, "received", mockLog); err == nil {
  333. t.Error("naming conflict between two events; error expected")
  334. }
  335. }