github.com/ethereum/go-ethereum@v1.14.3/accounts/abi/event_test.go (about) 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 17 package abi 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "encoding/json" 23 "math/big" 24 "reflect" 25 "strings" 26 "testing" 27 28 "github.com/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 var jsonEventTransfer = []byte(`{ 35 "anonymous": false, 36 "inputs": [ 37 { 38 "indexed": true, "name": "from", "type": "address" 39 }, { 40 "indexed": true, "name": "to", "type": "address" 41 }, { 42 "indexed": false, "name": "value", "type": "uint256" 43 }], 44 "name": "Transfer", 45 "type": "event" 46 }`) 47 48 var jsonEventPledge = []byte(`{ 49 "anonymous": false, 50 "inputs": [{ 51 "indexed": false, "name": "who", "type": "address" 52 }, { 53 "indexed": false, "name": "wad", "type": "uint128" 54 }, { 55 "indexed": false, "name": "currency", "type": "bytes3" 56 }], 57 "name": "Pledge", 58 "type": "event" 59 }`) 60 61 var jsonEventMixedCase = []byte(`{ 62 "anonymous": false, 63 "inputs": [{ 64 "indexed": false, "name": "value", "type": "uint256" 65 }, { 66 "indexed": false, "name": "_value", "type": "uint256" 67 }, { 68 "indexed": false, "name": "Value", "type": "uint256" 69 }], 70 "name": "MixedCase", 71 "type": "event" 72 }`) 73 74 // 1000000 75 var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240" 76 77 // "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd" 78 var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000" 79 80 // 1000000,2218516807680,1000001 81 var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241" 82 83 func TestEventId(t *testing.T) { 84 t.Parallel() 85 var table = []struct { 86 definition string 87 expectations map[string]common.Hash 88 }{ 89 { 90 definition: `[ 91 { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, 92 { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] } 93 ]`, 94 expectations: map[string]common.Hash{ 95 "Balance": crypto.Keccak256Hash([]byte("Balance(uint256)")), 96 "Check": crypto.Keccak256Hash([]byte("Check(address,uint256)")), 97 }, 98 }, 99 } 100 101 for _, test := range table { 102 abi, err := JSON(strings.NewReader(test.definition)) 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 for name, event := range abi.Events { 108 if event.ID != test.expectations[name] { 109 t.Errorf("expected id to be %x, got %x", test.expectations[name], event.ID) 110 } 111 } 112 } 113 } 114 115 func TestEventString(t *testing.T) { 116 t.Parallel() 117 var table = []struct { 118 definition string 119 expectations map[string]string 120 }{ 121 { 122 definition: `[ 123 { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, 124 { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }, 125 { "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] } 126 ]`, 127 expectations: map[string]string{ 128 "Balance": "event Balance(uint256 in)", 129 "Check": "event Check(address t, uint256 b)", 130 "Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)", 131 }, 132 }, 133 } 134 135 for _, test := range table { 136 abi, err := JSON(strings.NewReader(test.definition)) 137 if err != nil { 138 t.Fatal(err) 139 } 140 141 for name, event := range abi.Events { 142 if event.String() != test.expectations[name] { 143 t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String()) 144 } 145 } 146 } 147 } 148 149 // TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array. 150 func TestEventMultiValueWithArrayUnpack(t *testing.T) { 151 t.Parallel() 152 definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` 153 abi, err := JSON(strings.NewReader(definition)) 154 require.NoError(t, err) 155 var b bytes.Buffer 156 var i uint8 = 1 157 for ; i <= 3; i++ { 158 b.Write(packNum(reflect.ValueOf(i))) 159 } 160 unpacked, err := abi.Unpack("test", b.Bytes()) 161 require.NoError(t, err) 162 require.Equal(t, [2]uint8{1, 2}, unpacked[0]) 163 require.Equal(t, uint8(3), unpacked[1]) 164 } 165 166 func TestEventTupleUnpack(t *testing.T) { 167 t.Parallel() 168 type EventTransfer struct { 169 Value *big.Int 170 } 171 172 type EventTransferWithTag struct { 173 // this is valid because `value` is not exportable, 174 // so value is only unmarshalled into `Value1`. 175 value *big.Int //lint:ignore U1000 unused field is part of test 176 Value1 *big.Int `abi:"value"` 177 } 178 179 type BadEventTransferWithSameFieldAndTag struct { 180 Value *big.Int 181 Value1 *big.Int `abi:"value"` 182 } 183 184 type BadEventTransferWithDuplicatedTag struct { 185 Value1 *big.Int `abi:"value"` 186 Value2 *big.Int `abi:"value"` 187 } 188 189 type BadEventTransferWithEmptyTag struct { 190 Value *big.Int `abi:""` 191 } 192 193 type EventPledge struct { 194 Who common.Address 195 Wad *big.Int 196 Currency [3]byte 197 } 198 199 type BadEventPledge struct { 200 Who string 201 Wad int 202 Currency [3]byte 203 } 204 205 type EventMixedCase struct { 206 Value1 *big.Int `abi:"value"` 207 Value2 *big.Int `abi:"_value"` 208 Value3 *big.Int `abi:"Value"` 209 } 210 211 bigint := new(big.Int) 212 bigintExpected := big.NewInt(1000000) 213 bigintExpected2 := big.NewInt(2218516807680) 214 bigintExpected3 := big.NewInt(1000001) 215 addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268") 216 var testCases = []struct { 217 data string 218 dest interface{} 219 expected interface{} 220 jsonLog []byte 221 error string 222 name string 223 }{{ 224 transferData1, 225 &EventTransfer{}, 226 &EventTransfer{Value: bigintExpected}, 227 jsonEventTransfer, 228 "", 229 "Can unpack ERC20 Transfer event into structure", 230 }, { 231 transferData1, 232 &[]interface{}{&bigint}, 233 &[]interface{}{&bigintExpected}, 234 jsonEventTransfer, 235 "", 236 "Can unpack ERC20 Transfer event into slice", 237 }, { 238 transferData1, 239 &EventTransferWithTag{}, 240 &EventTransferWithTag{Value1: bigintExpected}, 241 jsonEventTransfer, 242 "", 243 "Can unpack ERC20 Transfer event into structure with abi: tag", 244 }, { 245 transferData1, 246 &BadEventTransferWithDuplicatedTag{}, 247 &BadEventTransferWithDuplicatedTag{}, 248 jsonEventTransfer, 249 "struct: abi tag in 'Value2' already mapped", 250 "Can not unpack ERC20 Transfer event with duplicated abi tag", 251 }, { 252 transferData1, 253 &BadEventTransferWithSameFieldAndTag{}, 254 &BadEventTransferWithSameFieldAndTag{}, 255 jsonEventTransfer, 256 "abi: multiple variables maps to the same abi field 'value'", 257 "Can not unpack ERC20 Transfer event with a field and a tag mapping to the same abi variable", 258 }, { 259 transferData1, 260 &BadEventTransferWithEmptyTag{}, 261 &BadEventTransferWithEmptyTag{}, 262 jsonEventTransfer, 263 "struct: abi tag in 'Value' is empty", 264 "Can not unpack ERC20 Transfer event with an empty tag", 265 }, { 266 pledgeData1, 267 &EventPledge{}, 268 &EventPledge{ 269 addr, 270 bigintExpected2, 271 [3]byte{'u', 's', 'd'}}, 272 jsonEventPledge, 273 "", 274 "Can unpack Pledge event into structure", 275 }, { 276 pledgeData1, 277 &[]interface{}{&common.Address{}, &bigint, &[3]byte{}}, 278 &[]interface{}{ 279 &addr, 280 &bigintExpected2, 281 &[3]byte{'u', 's', 'd'}}, 282 jsonEventPledge, 283 "", 284 "Can unpack Pledge event into slice", 285 }, { 286 pledgeData1, 287 &[3]interface{}{&common.Address{}, &bigint, &[3]byte{}}, 288 &[3]interface{}{ 289 &addr, 290 &bigintExpected2, 291 &[3]byte{'u', 's', 'd'}}, 292 jsonEventPledge, 293 "", 294 "Can unpack Pledge event into an array", 295 }, { 296 pledgeData1, 297 &[]interface{}{new(int), 0, 0}, 298 &[]interface{}{}, 299 jsonEventPledge, 300 "abi: cannot unmarshal common.Address in to int", 301 "Can not unpack Pledge event into slice with wrong types", 302 }, { 303 pledgeData1, 304 &BadEventPledge{}, 305 &BadEventPledge{}, 306 jsonEventPledge, 307 "abi: cannot unmarshal common.Address in to string", 308 "Can not unpack Pledge event into struct with wrong filed types", 309 }, { 310 pledgeData1, 311 &[]interface{}{common.Address{}, new(big.Int)}, 312 &[]interface{}{}, 313 jsonEventPledge, 314 "abi: insufficient number of arguments for unpack, want 3, got 2", 315 "Can not unpack Pledge event into too short slice", 316 }, { 317 pledgeData1, 318 new(map[string]interface{}), 319 &[]interface{}{}, 320 jsonEventPledge, 321 "abi:[2] cannot unmarshal tuple in to map[string]interface {}", 322 "Can not unpack Pledge event into map", 323 }, { 324 mixedCaseData1, 325 &EventMixedCase{}, 326 &EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3}, 327 jsonEventMixedCase, 328 "", 329 "Can unpack abi variables with mixed case", 330 }} 331 332 for _, tc := range testCases { 333 assert := assert.New(t) 334 tc := tc 335 t.Run(tc.name, func(t *testing.T) { 336 err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert) 337 if tc.error == "" { 338 assert.Nil(err, "Should be able to unpack event data.") 339 assert.Equal(tc.expected, tc.dest, tc.name) 340 } else { 341 assert.EqualError(err, tc.error, tc.name) 342 } 343 }) 344 } 345 } 346 347 func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error { 348 data, err := hex.DecodeString(hexData) 349 assert.NoError(err, "Hex data should be a correct hex-string") 350 var e Event 351 assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI") 352 a := ABI{Events: map[string]Event{"e": e}} 353 return a.UnpackIntoInterface(dest, "e", data) 354 } 355 356 // TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder. 357 func TestEventUnpackIndexed(t *testing.T) { 358 t.Parallel() 359 definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` 360 type testStruct struct { 361 Value1 uint8 // indexed 362 Value2 uint8 363 } 364 abi, err := JSON(strings.NewReader(definition)) 365 require.NoError(t, err) 366 var b bytes.Buffer 367 b.Write(packNum(reflect.ValueOf(uint8(8)))) 368 var rst testStruct 369 require.NoError(t, abi.UnpackIntoInterface(&rst, "test", b.Bytes())) 370 require.Equal(t, uint8(0), rst.Value1) 371 require.Equal(t, uint8(8), rst.Value2) 372 } 373 374 // TestEventIndexedWithArrayUnpack verifies that decoder will not overflow when static array is indexed input. 375 func TestEventIndexedWithArrayUnpack(t *testing.T) { 376 t.Parallel() 377 definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]` 378 type testStruct struct { 379 Value1 [2]uint8 // indexed 380 Value2 string 381 } 382 abi, err := JSON(strings.NewReader(definition)) 383 require.NoError(t, err) 384 var b bytes.Buffer 385 stringOut := "abc" 386 // number of fields that will be encoded * 32 387 b.Write(packNum(reflect.ValueOf(32))) 388 b.Write(packNum(reflect.ValueOf(len(stringOut)))) 389 b.Write(common.RightPadBytes([]byte(stringOut), 32)) 390 391 var rst testStruct 392 require.NoError(t, abi.UnpackIntoInterface(&rst, "test", b.Bytes())) 393 require.Equal(t, [2]uint8{0, 0}, rst.Value1) 394 require.Equal(t, stringOut, rst.Value2) 395 }