event_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2016 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 abi
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "encoding/json"
  21. "math/big"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/stretchr/testify/assert"
  28. "github.com/stretchr/testify/require"
  29. )
  30. var jsonEventTransfer = []byte(`{
  31. "anonymous": false,
  32. "inputs": [
  33. {
  34. "indexed": true, "name": "from", "type": "address"
  35. }, {
  36. "indexed": true, "name": "to", "type": "address"
  37. }, {
  38. "indexed": false, "name": "value", "type": "uint256"
  39. }],
  40. "name": "Transfer",
  41. "type": "event"
  42. }`)
  43. var jsonEventPledge = []byte(`{
  44. "anonymous": false,
  45. "inputs": [{
  46. "indexed": false, "name": "who", "type": "address"
  47. }, {
  48. "indexed": false, "name": "wad", "type": "uint128"
  49. }, {
  50. "indexed": false, "name": "currency", "type": "bytes3"
  51. }],
  52. "name": "Pledge",
  53. "type": "event"
  54. }`)
  55. // 1000000
  56. var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240"
  57. // "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd"
  58. var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000"
  59. func TestEventId(t *testing.T) {
  60. var table = []struct {
  61. definition string
  62. expectations map[string]common.Hash
  63. }{
  64. {
  65. definition: `[
  66. { "type" : "event", "name" : "balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
  67. { "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
  68. ]`,
  69. expectations: map[string]common.Hash{
  70. "balance": crypto.Keccak256Hash([]byte("balance(uint256)")),
  71. "check": crypto.Keccak256Hash([]byte("check(address,uint256)")),
  72. },
  73. },
  74. }
  75. for _, test := range table {
  76. abi, err := JSON(strings.NewReader(test.definition))
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. for name, event := range abi.Events {
  81. if event.Id() != test.expectations[name] {
  82. t.Errorf("expected id to be %x, got %x", test.expectations[name], event.Id())
  83. }
  84. }
  85. }
  86. }
  87. // TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array.
  88. func TestEventMultiValueWithArrayUnpack(t *testing.T) {
  89. definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
  90. type testStruct struct {
  91. Value1 [2]uint8
  92. Value2 uint8
  93. }
  94. abi, err := JSON(strings.NewReader(definition))
  95. require.NoError(t, err)
  96. var b bytes.Buffer
  97. var i uint8 = 1
  98. for ; i <= 3; i++ {
  99. b.Write(packNum(reflect.ValueOf(i)))
  100. }
  101. var rst testStruct
  102. require.NoError(t, abi.Unpack(&rst, "test", b.Bytes()))
  103. require.Equal(t, [2]uint8{1, 2}, rst.Value1)
  104. require.Equal(t, uint8(3), rst.Value2)
  105. }
  106. func TestEventTupleUnpack(t *testing.T) {
  107. type EventTransfer struct {
  108. Value *big.Int
  109. }
  110. type EventPledge struct {
  111. Who common.Address
  112. Wad *big.Int
  113. Currency [3]byte
  114. }
  115. type BadEventPledge struct {
  116. Who string
  117. Wad int
  118. Currency [3]byte
  119. }
  120. bigint := new(big.Int)
  121. bigintExpected := big.NewInt(1000000)
  122. bigintExpected2 := big.NewInt(2218516807680)
  123. addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268")
  124. var testCases = []struct {
  125. data string
  126. dest interface{}
  127. expected interface{}
  128. jsonLog []byte
  129. error string
  130. name string
  131. }{{
  132. transferData1,
  133. &EventTransfer{},
  134. &EventTransfer{Value: bigintExpected},
  135. jsonEventTransfer,
  136. "",
  137. "Can unpack ERC20 Transfer event into structure",
  138. }, {
  139. transferData1,
  140. &[]interface{}{&bigint},
  141. &[]interface{}{&bigintExpected},
  142. jsonEventTransfer,
  143. "",
  144. "Can unpack ERC20 Transfer event into slice",
  145. }, {
  146. pledgeData1,
  147. &EventPledge{},
  148. &EventPledge{
  149. addr,
  150. bigintExpected2,
  151. [3]byte{'u', 's', 'd'}},
  152. jsonEventPledge,
  153. "",
  154. "Can unpack Pledge event into structure",
  155. }, {
  156. pledgeData1,
  157. &[]interface{}{&common.Address{}, &bigint, &[3]byte{}},
  158. &[]interface{}{
  159. &addr,
  160. &bigintExpected2,
  161. &[3]byte{'u', 's', 'd'}},
  162. jsonEventPledge,
  163. "",
  164. "Can unpack Pledge event into slice",
  165. }, {
  166. pledgeData1,
  167. &[3]interface{}{&common.Address{}, &bigint, &[3]byte{}},
  168. &[3]interface{}{
  169. &addr,
  170. &bigintExpected2,
  171. &[3]byte{'u', 's', 'd'}},
  172. jsonEventPledge,
  173. "",
  174. "Can unpack Pledge event into an array",
  175. }, {
  176. pledgeData1,
  177. &[]interface{}{new(int), 0, 0},
  178. &[]interface{}{},
  179. jsonEventPledge,
  180. "abi: cannot unmarshal common.Address in to int",
  181. "Can not unpack Pledge event into slice with wrong types",
  182. }, {
  183. pledgeData1,
  184. &BadEventPledge{},
  185. &BadEventPledge{},
  186. jsonEventPledge,
  187. "abi: cannot unmarshal common.Address in to string",
  188. "Can not unpack Pledge event into struct with wrong filed types",
  189. }, {
  190. pledgeData1,
  191. &[]interface{}{common.Address{}, new(big.Int)},
  192. &[]interface{}{},
  193. jsonEventPledge,
  194. "abi: insufficient number of elements in the list/array for unpack, want 3, got 2",
  195. "Can not unpack Pledge event into too short slice",
  196. }, {
  197. pledgeData1,
  198. new(map[string]interface{}),
  199. &[]interface{}{},
  200. jsonEventPledge,
  201. "abi: cannot unmarshal tuple into map[string]interface {}",
  202. "Can not unpack Pledge event into map",
  203. }}
  204. for _, tc := range testCases {
  205. assert := assert.New(t)
  206. tc := tc
  207. t.Run(tc.name, func(t *testing.T) {
  208. err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert)
  209. if tc.error == "" {
  210. assert.Nil(err, "Should be able to unpack event data.")
  211. assert.Equal(tc.expected, tc.dest)
  212. } else {
  213. assert.EqualError(err, tc.error)
  214. }
  215. })
  216. }
  217. }
  218. func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error {
  219. data, err := hex.DecodeString(hexData)
  220. assert.NoError(err, "Hex data should be a correct hex-string")
  221. var e Event
  222. assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI")
  223. a := ABI{Events: map[string]Event{"e": e}}
  224. return a.Unpack(dest, "e", data)
  225. }