aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/event_test.go
blob: e735cceb88847de3e95e898a273ac4f6baaafbf0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package abi

import (
    "bytes"
    "encoding/hex"
    "encoding/json"
    "math/big"
    "reflect"
    "strings"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/require"
)

var jsonEventTransfer = []byte(`{
  "anonymous": false,
  "inputs": [
    {
      "indexed": true, "name": "from", "type": "address"
    }, {
      "indexed": true, "name": "to", "type": "address"
    }, {
      "indexed": false, "name": "value", "type": "uint256"
  }],
  "name": "Transfer",
  "type": "event"
}`)

var jsonEventPledge = []byte(`{
  "anonymous": false,
  "inputs": [{
      "indexed": false, "name": "who", "type": "address"
    }, {
      "indexed": false, "name": "wad", "type": "uint128"
    }, {
      "indexed": false, "name": "currency", "type": "bytes3"
  }],
  "name": "Pledge",
  "type": "event"
}`)

var jsonEventMixedCase = []byte(`{
    "anonymous": false,
    "inputs": [{
        "indexed": false, "name": "value", "type": "uint256"
      }, {
        "indexed": false, "name": "_value", "type": "uint256"
      }, {
        "indexed": false, "name": "Value", "type": "uint256"
    }],
    "name": "MixedCase",
    "type": "event"
  }`)

// 1000000
var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240"

// "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd"
var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000"

// 1000000,2218516807680,1000001
var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241"

func TestEventId(t *testing.T) {
    var table = []struct {
        definition   string
        expectations map[string]common.Hash
    }{
        {
            definition: `[
            { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
            { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
            ]`,
            expectations: map[string]common.Hash{
                "Balance": crypto.Keccak256Hash([]byte("Balance(uint256)")),
                "Check":   crypto.Keccak256Hash([]byte("Check(address,uint256)")),
            },
        },
    }

    for _, test := range table {
        abi, err := JSON(strings.NewReader(test.definition))
        if err != nil {
            t.Fatal(err)
        }

        for name, event := range abi.Events {
            if event.Id() != test.expectations[name] {
                t.Errorf("expected id to be %x, got %x", test.expectations[name], event.Id())
            }
        }
    }
}

func TestEventString(t *testing.T) {
    var table = []struct {
        definition   string
        expectations map[string]string
    }{
        {
            definition: `[
            { "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] },
            { "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] },
            { "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] }
            ]`,
            expectations: map[string]string{
                "Balance":  "event Balance(uint256 in)",
                "Check":    "event Check(address t, uint256 b)",
                "Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)",
            },
        },
    }

    for _, test := range table {
        abi, err := JSON(strings.NewReader(test.definition))
        if err != nil {
            t.Fatal(err)
        }

        for name, event := range abi.Events {
            if event.String() != test.expectations[name] {
                t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String())
            }
        }
    }
}

// TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array.
func TestEventMultiValueWithArrayUnpack(t *testing.T) {
    definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
    type testStruct struct {
        Value1 [2]uint8
        Value2 uint8
    }
    abi, err := JSON(strings.NewReader(definition))
    require.NoError(t, err)
    var b bytes.Buffer
    var i uint8 = 1
    for ; i <= 3; i++ {
        b.Write(packNum(reflect.ValueOf(i)))
    }
    var rst testStruct
    require.NoError(t, abi.Unpack(&rst, "test", b.Bytes()))
    require.Equal(t, [2]uint8{1, 2}, rst.Value1)
    require.Equal(t, uint8(3), rst.Value2)
}

func TestEventTupleUnpack(t *testing.T) {

    type EventTransfer struct {
        Value *big.Int
    }

    type EventTransferWithTag struct {
        // this is valid because `value` is not exportable,
        // so value is only unmarshalled into `Value1`.
        value  *big.Int
        Value1 *big.Int `abi:"value"`
    }

    type BadEventTransferWithSameFieldAndTag struct {
        Value  *big.Int
        Value1 *big.Int `abi:"value"`
    }

    type BadEventTransferWithDuplicatedTag struct {
        Value1 *big.Int `abi:"value"`
        Value2 *big.Int `abi:"value"`
    }

    type BadEventTransferWithEmptyTag struct {
        Value *big.Int `abi:""`
    }

    type EventPledge struct {
        Who      common.Address
        Wad      *big.Int
        Currency [3]byte
    }

    type BadEventPledge struct {
        Who      string
        Wad      int
        Currency [3]byte
    }

    type EventMixedCase struct {
        Value1 *big.Int `abi:"value"`
        Value2 *big.Int `abi:"_value"`
        Value3 *big.Int `abi:"Value"`
    }

    bigint := new(big.Int)
    bigintExpected := big.NewInt(1000000)
    bigintExpected2 := big.NewInt(2218516807680)
    bigintExpected3 := big.NewInt(1000001)
    addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268")
    var testCases = []struct {
        data     string
        dest     interface{}
        expected interface{}
        jsonLog  []byte
        error    string
        name     string
    }{{
        transferData1,
        &EventTransfer{},
        &EventTransfer{Value: bigintExpected},
        jsonEventTransfer,
        "",
        "Can unpack ERC20 Transfer event into structure",
    }, {
        transferData1,
        &[]interface{}{&bigint},
        &[]interface{}{&bigintExpected},
        jsonEventTransfer,
        "",
        "Can unpack ERC20 Transfer event into slice",
    }, {
        transferData1,
        &EventTransferWithTag{},
        &EventTransferWithTag{Value1: bigintExpected},
        jsonEventTransfer,
        "",
        "Can unpack ERC20 Transfer event into structure with abi: tag",
    }, {
        transferData1,
        &BadEventTransferWithDuplicatedTag{},
        &BadEventTransferWithDuplicatedTag{},
        jsonEventTransfer,
        "struct: abi tag in 'Value2' already mapped",
        "Can not unpack ERC20 Transfer event with duplicated abi tag",
    }, {
        transferData1,
        &BadEventTransferWithSameFieldAndTag{},
        &BadEventTransferWithSameFieldAndTag{},
        jsonEventTransfer,
        "abi: multiple variables maps to the same abi field 'value'",
        "Can not unpack ERC20 Transfer event with a field and a tag mapping to the same abi variable",
    }, {
        transferData1,
        &BadEventTransferWithEmptyTag{},
        &BadEventTransferWithEmptyTag{},
        jsonEventTransfer,
        "struct: abi tag in 'Value' is empty",
        "Can not unpack ERC20 Transfer event with an empty tag",
    }, {
        pledgeData1,
        &EventPledge{},
        &EventPledge{
            addr,
            bigintExpected2,
            [3]byte{'u', 's', 'd'}},
        jsonEventPledge,
        "",
        "Can unpack Pledge event into structure",
    }, {
        pledgeData1,
        &[]interface{}{&common.Address{}, &bigint, &[3]byte{}},
        &[]interface{}{
            &addr,
            &bigintExpected2,
            &[3]byte{'u', 's', 'd'}},
        jsonEventPledge,
        "",
        "Can unpack Pledge event into slice",
    }, {
        pledgeData1,
        &[3]interface{}{&common.Address{}, &bigint, &[3]byte{}},
        &[3]interface{}{
            &addr,
            &bigintExpected2,
            &[3]byte{'u', 's', 'd'}},
        jsonEventPledge,
        "",
        "Can unpack Pledge event into an array",
    }, {
        pledgeData1,
        &[]interface{}{new(int), 0, 0},
        &[]interface{}{},
        jsonEventPledge,
        "abi: cannot unmarshal common.Address in to int",
        "Can not unpack Pledge event into slice with wrong types",
    }, {
        pledgeData1,
        &BadEventPledge{},
        &BadEventPledge{},
        jsonEventPledge,
        "abi: cannot unmarshal common.Address in to string",
        "Can not unpack Pledge event into struct with wrong filed types",
    }, {
        pledgeData1,
        &[]interface{}{common.Address{}, new(big.Int)},
        &[]interface{}{},
        jsonEventPledge,
        "abi: insufficient number of elements in the list/array for unpack, want 3, got 2",
        "Can not unpack Pledge event into too short slice",
    }, {
        pledgeData1,
        new(map[string]interface{}),
        &[]interface{}{},
        jsonEventPledge,
        "abi: cannot unmarshal tuple into map[string]interface {}",
        "Can not unpack Pledge event into map",
    }, {
        mixedCaseData1,
        &EventMixedCase{},
        &EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3},
        jsonEventMixedCase,
        "",
        "Can unpack abi variables with mixed case",
    }}

    for _, tc := range testCases {
        assert := assert.New(t)
        tc := tc
        t.Run(tc.name, func(t *testing.T) {
            err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert)
            if tc.error == "" {
                assert.Nil(err, "Should be able to unpack event data.")
                assert.Equal(tc.expected, tc.dest, tc.name)
            } else {
                assert.EqualError(err, tc.error, tc.name)
            }
        })
    }
}

func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error {
    data, err := hex.DecodeString(hexData)
    assert.NoError(err, "Hex data should be a correct hex-string")
    var e Event
    assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI")
    a := ABI{Events: map[string]Event{"e": e}}
    return a.Unpack(dest, "e", data)
}

/*
Taken from
https://github.com/ethereum/go-ethereum/pull/15568
*/

type testResult struct {
    Values [2]*big.Int
    Value1 *big.Int
    Value2 *big.Int
}

type testCase struct {
    definition string
    want       testResult
}

func (tc testCase) encoded(intType, arrayType Type) []byte {
    var b bytes.Buffer
    if tc.want.Value1 != nil {
        val, _ := intType.pack(reflect.ValueOf(tc.want.Value1))
        b.Write(val)
    }

    if !reflect.DeepEqual(tc.want.Values, [2]*big.Int{nil, nil}) {
        val, _ := arrayType.pack(reflect.ValueOf(tc.want.Values))
        b.Write(val)
    }
    if tc.want.Value2 != nil {
        val, _ := intType.pack(reflect.ValueOf(tc.want.Value2))
        b.Write(val)
    }
    return b.Bytes()
}

// TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder.
func TestEventUnpackIndexed(t *testing.T) {
    definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]`
    type testStruct struct {
        Value1 uint8
        Value2 uint8
    }
    abi, err := JSON(strings.NewReader(definition))
    require.NoError(t, err)
    var b bytes.Buffer
    b.Write(packNum(reflect.ValueOf(uint8(8))))
    var rst testStruct
    require.NoError(t, abi.Unpack(&rst, "test", b.Bytes()))
    require.Equal(t, uint8(0), rst.Value1)
    require.Equal(t, uint8(8), rst.Value2)
}

// TestEventIndexedWithArrayUnpack verifies that decoder will not overlow when static array is indexed input.
func TestEventIndexedWithArrayUnpack(t *testing.T) {
    definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]`
    type testStruct struct {
        Value1 [2]uint8
        Value2 string
    }
    abi, err := JSON(strings.NewReader(definition))
    require.NoError(t, err)
    var b bytes.Buffer
    stringOut := "abc"
    // number of fields that will be encoded * 32
    b.Write(packNum(reflect.ValueOf(32)))
    b.Write(packNum(reflect.ValueOf(len(stringOut))))
    b.Write(common.RightPadBytes([]byte(stringOut), 32))

    var rst testStruct
    require.NoError(t, abi.Unpack(&rst, "test", b.Bytes()))
    require.Equal(t, [2]uint8{0, 0}, rst.Value1)
    require.Equal(t, stringOut, rst.Value2)
}