github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/event_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package abi 19 20 import ( 21 "bytes" 22 "encoding/hex" 23 "encoding/json" 24 "math/big" 25 "reflect" 26 "strings" 27 "testing" 28 29 "github.com/AigarNetwork/aigar/common" 30 "github.com/AigarNetwork/aigar/crypto" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 var jsonEventTransfer = []byte(`{ 36 "anonymous": false, 37 "inputs": [ 38 { 39 "indexed": true, "name": "from", "type": "address" 40 }, { 41 "indexed": true, "name": "to", "type": "address" 42 }, { 43 "indexed": false, "name": "value", "type": "uint256" 44 }], 45 "name": "Transfer", 46 "type": "event" 47 }`) 48 49 var jsonEventPledge = []byte(`{ 50 "anonymous": false, 51 "inputs": [{ 52 "indexed": false, "name": "who", "type": "address" 53 }, { 54 "indexed": false, "name": "wad", "type": "uint128" 55 }, { 56 "indexed": false, "name": "currency", "type": "bytes3" 57 }], 58 "name": "Pledge", 59 "type": "event" 60 }`) 61 62 var jsonEventMixedCase = []byte(`{ 63 "anonymous": false, 64 "inputs": [{ 65 "indexed": false, "name": "value", "type": "uint256" 66 }, { 67 "indexed": false, "name": "_value", "type": "uint256" 68 }, { 69 "indexed": false, "name": "Value", "type": "uint256" 70 }], 71 "name": "MixedCase", 72 "type": "event" 73 }`) 74 75 // 1000000 76 var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240" 77 78 // "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd" 79 var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000" 80 81 // 1000000,2218516807680,1000001 82 var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241" 83 84 func TestEventId(t *testing.T) { 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 var table = []struct { 117 definition string 118 expectations map[string]string 119 }{ 120 { 121 definition: `[ 122 { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, 123 { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }, 124 { "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] } 125 ]`, 126 expectations: map[string]string{ 127 "Balance": "event Balance(uint256 in)", 128 "Check": "event Check(address t, uint256 b)", 129 "Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)", 130 }, 131 }, 132 } 133 134 for _, test := range table { 135 abi, err := JSON(strings.NewReader(test.definition)) 136 if err != nil { 137 t.Fatal(err) 138 } 139 140 for name, event := range abi.Events { 141 if event.String() != test.expectations[name] { 142 t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String()) 143 } 144 } 145 } 146 } 147 148 // TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array. 149 func TestEventMultiValueWithArrayUnpack(t *testing.T) { 150 definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` 151 type testStruct struct { 152 Value1 [2]uint8 153 Value2 uint8 154 } 155 abi, err := JSON(strings.NewReader(definition)) 156 require.NoError(t, err) 157 var b bytes.Buffer 158 var i uint8 = 1 159 for ; i <= 3; i++ { 160 b.Write(packNum(reflect.ValueOf(i))) 161 } 162 var rst testStruct 163 require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) 164 require.Equal(t, [2]uint8{1, 2}, rst.Value1) 165 require.Equal(t, uint8(3), rst.Value2) 166 } 167 168 func TestEventTupleUnpack(t *testing.T) { 169 170 type EventTransfer struct { 171 Value *big.Int 172 } 173 174 type EventTransferWithTag struct { 175 // this is valid because `value` is not exportable, 176 // so value is only unmarshalled into `Value1`. 177 value *big.Int 178 Value1 *big.Int `abi:"value"` 179 } 180 181 type BadEventTransferWithSameFieldAndTag struct { 182 Value *big.Int 183 Value1 *big.Int `abi:"value"` 184 } 185 186 type BadEventTransferWithDuplicatedTag struct { 187 Value1 *big.Int `abi:"value"` 188 Value2 *big.Int `abi:"value"` 189 } 190 191 type BadEventTransferWithEmptyTag struct { 192 Value *big.Int `abi:""` 193 } 194 195 type EventPledge struct { 196 Who common.Address 197 Wad *big.Int 198 Currency [3]byte 199 } 200 201 type BadEventPledge struct { 202 Who string 203 Wad int 204 Currency [3]byte 205 } 206 207 type EventMixedCase struct { 208 Value1 *big.Int `abi:"value"` 209 Value2 *big.Int `abi:"_value"` 210 Value3 *big.Int `abi:"Value"` 211 } 212 213 bigint := new(big.Int) 214 bigintExpected := big.NewInt(1000000) 215 bigintExpected2 := big.NewInt(2218516807680) 216 bigintExpected3 := big.NewInt(1000001) 217 addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268") 218 var testCases = []struct { 219 data string 220 dest interface{} 221 expected interface{} 222 jsonLog []byte 223 error string 224 name string 225 }{{ 226 transferData1, 227 &EventTransfer{}, 228 &EventTransfer{Value: bigintExpected}, 229 jsonEventTransfer, 230 "", 231 "Can unpack ERC20 Transfer event into structure", 232 }, { 233 transferData1, 234 &[]interface{}{&bigint}, 235 &[]interface{}{&bigintExpected}, 236 jsonEventTransfer, 237 "", 238 "Can unpack ERC20 Transfer event into slice", 239 }, { 240 transferData1, 241 &EventTransferWithTag{}, 242 &EventTransferWithTag{Value1: bigintExpected}, 243 jsonEventTransfer, 244 "", 245 "Can unpack ERC20 Transfer event into structure with abi: tag", 246 }, { 247 transferData1, 248 &BadEventTransferWithDuplicatedTag{}, 249 &BadEventTransferWithDuplicatedTag{}, 250 jsonEventTransfer, 251 "struct: abi tag in 'Value2' already mapped", 252 "Can not unpack ERC20 Transfer event with duplicated abi tag", 253 }, { 254 transferData1, 255 &BadEventTransferWithSameFieldAndTag{}, 256 &BadEventTransferWithSameFieldAndTag{}, 257 jsonEventTransfer, 258 "abi: multiple variables maps to the same abi field 'value'", 259 "Can not unpack ERC20 Transfer event with a field and a tag mapping to the same abi variable", 260 }, { 261 transferData1, 262 &BadEventTransferWithEmptyTag{}, 263 &BadEventTransferWithEmptyTag{}, 264 jsonEventTransfer, 265 "struct: abi tag in 'Value' is empty", 266 "Can not unpack ERC20 Transfer event with an empty tag", 267 }, { 268 pledgeData1, 269 &EventPledge{}, 270 &EventPledge{ 271 addr, 272 bigintExpected2, 273 [3]byte{'u', 's', 'd'}}, 274 jsonEventPledge, 275 "", 276 "Can unpack Pledge event into structure", 277 }, { 278 pledgeData1, 279 &[]interface{}{&common.Address{}, &bigint, &[3]byte{}}, 280 &[]interface{}{ 281 &addr, 282 &bigintExpected2, 283 &[3]byte{'u', 's', 'd'}}, 284 jsonEventPledge, 285 "", 286 "Can unpack Pledge event into slice", 287 }, { 288 pledgeData1, 289 &[3]interface{}{&common.Address{}, &bigint, &[3]byte{}}, 290 &[3]interface{}{ 291 &addr, 292 &bigintExpected2, 293 &[3]byte{'u', 's', 'd'}}, 294 jsonEventPledge, 295 "", 296 "Can unpack Pledge event into an array", 297 }, { 298 pledgeData1, 299 &[]interface{}{new(int), 0, 0}, 300 &[]interface{}{}, 301 jsonEventPledge, 302 "abi: cannot unmarshal common.Address in to int", 303 "Can not unpack Pledge event into slice with wrong types", 304 }, { 305 pledgeData1, 306 &BadEventPledge{}, 307 &BadEventPledge{}, 308 jsonEventPledge, 309 "abi: cannot unmarshal common.Address in to string", 310 "Can not unpack Pledge event into struct with wrong filed types", 311 }, { 312 pledgeData1, 313 &[]interface{}{common.Address{}, new(big.Int)}, 314 &[]interface{}{}, 315 jsonEventPledge, 316 "abi: insufficient number of elements in the list/array for unpack, want 3, got 2", 317 "Can not unpack Pledge event into too short slice", 318 }, { 319 pledgeData1, 320 new(map[string]interface{}), 321 &[]interface{}{}, 322 jsonEventPledge, 323 "abi: cannot unmarshal tuple into map[string]interface {}", 324 "Can not unpack Pledge event into map", 325 }, { 326 mixedCaseData1, 327 &EventMixedCase{}, 328 &EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3}, 329 jsonEventMixedCase, 330 "", 331 "Can unpack abi variables with mixed case", 332 }} 333 334 for _, tc := range testCases { 335 assert := assert.New(t) 336 tc := tc 337 t.Run(tc.name, func(t *testing.T) { 338 err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert) 339 if tc.error == "" { 340 assert.Nil(err, "Should be able to unpack event data.") 341 assert.Equal(tc.expected, tc.dest, tc.name) 342 } else { 343 assert.EqualError(err, tc.error, tc.name) 344 } 345 }) 346 } 347 } 348 349 func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error { 350 data, err := hex.DecodeString(hexData) 351 assert.NoError(err, "Hex data should be a correct hex-string") 352 var e Event 353 assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI") 354 a := ABI{Events: map[string]Event{"e": e}} 355 return a.Unpack(dest, "e", data) 356 } 357 358 /* 359 Taken from 360 https://github.com/AigarNetwork/aigar/pull/15568 361 */ 362 363 type testResult struct { 364 Values [2]*big.Int 365 Value1 *big.Int 366 Value2 *big.Int 367 } 368 369 type testCase struct { 370 definition string 371 want testResult 372 } 373 374 func (tc testCase) encoded(intType, arrayType Type) []byte { 375 var b bytes.Buffer 376 if tc.want.Value1 != nil { 377 val, _ := intType.pack(reflect.ValueOf(tc.want.Value1)) 378 b.Write(val) 379 } 380 381 if !reflect.DeepEqual(tc.want.Values, [2]*big.Int{nil, nil}) { 382 val, _ := arrayType.pack(reflect.ValueOf(tc.want.Values)) 383 b.Write(val) 384 } 385 if tc.want.Value2 != nil { 386 val, _ := intType.pack(reflect.ValueOf(tc.want.Value2)) 387 b.Write(val) 388 } 389 return b.Bytes() 390 } 391 392 // TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder. 393 func TestEventUnpackIndexed(t *testing.T) { 394 definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` 395 type testStruct struct { 396 Value1 uint8 397 Value2 uint8 398 } 399 abi, err := JSON(strings.NewReader(definition)) 400 require.NoError(t, err) 401 var b bytes.Buffer 402 b.Write(packNum(reflect.ValueOf(uint8(8)))) 403 var rst testStruct 404 require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) 405 require.Equal(t, uint8(0), rst.Value1) 406 require.Equal(t, uint8(8), rst.Value2) 407 } 408 409 // TestEventIndexedWithArrayUnpack verifies that decoder will not overlow when static array is indexed input. 410 func TestEventIndexedWithArrayUnpack(t *testing.T) { 411 definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]` 412 type testStruct struct { 413 Value1 [2]uint8 414 Value2 string 415 } 416 abi, err := JSON(strings.NewReader(definition)) 417 require.NoError(t, err) 418 var b bytes.Buffer 419 stringOut := "abc" 420 // number of fields that will be encoded * 32 421 b.Write(packNum(reflect.ValueOf(32))) 422 b.Write(packNum(reflect.ValueOf(len(stringOut)))) 423 b.Write(common.RightPadBytes([]byte(stringOut), 32)) 424 425 var rst testStruct 426 require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) 427 require.Equal(t, [2]uint8{0, 0}, rst.Value1) 428 require.Equal(t, stringOut, rst.Value2) 429 }