This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathe2e.contract.events.js
More file actions
559 lines (466 loc) · 18.2 KB
/
e2e.contract.events.js
File metadata and controls
559 lines (466 loc) · 18.2 KB
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
var assert = require('assert');
var Basic = require('./sources/Basic');
var Child = require('./sources/Child');
var Parent = require('./sources/Parent');
var utils = require('./helpers/test.utils');
var Web3 = utils.getWeb3();
const prepareEvents = async (instance,address) => {
await instance
.methods
.firesEvent(address, 1)
.send({from: address});
await instance
.methods
.firesEvent(address, 2)
.send({from: address});
await instance
.methods
.firesEvent(address, 3)
.send({from: address});
}
describe('contract.events [ @E2E ]', function() {
// `getPastEvents` not working with Geth instamine over websockets.
if (process.env.GETH_INSTAMINE) return;
var web3;
var accounts;
var basic;
var instance;
var port;
var basicOptions = {
data: Basic.bytecode,
gasPrice: 1000000000,// Default gasPrice set by Geth
gas: 4000000
};
beforeEach(async function(){
this.timeout(15000)
port = utils.getWebsocketPort();
web3 = new Web3('ws://localhost:' + port);
accounts = await web3.eth.getAccounts();
basic = new web3.eth.Contract(Basic.abi, basicOptions);
instance = await basic.deploy().send({from: accounts[0]});
});
it('contract.getPastEvents', async function(){
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
assert.equal(events.length, 3);
assert.equal(events[0].event, 'BasicEvent');
assert.equal(events[1].event, 'BasicEvent');
assert.equal(events[2].event, 'BasicEvent');
assert.notEqual(events[0].id, events[1].id);
});
it('contract.getPastEvents filter by val', async function() {
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents('BasicEvent', {
filter: { val: 2 },
fromBlock: 'earliest',
toBlock: 'latest',
});
assert.equal(events.length, 1);
assert.equal(events[0].returnValues.val, 2);
});
it('contract.getPastEvents without specify event name: filter by val', async function() {
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents({
filter: { val: 2 },
fromBlock: 'earliest',
toBlock: 'latest',
});
assert.equal(events.length, 1);
assert.equal(events[0].returnValues.val, 2);
});
it('contract.getPastEvents all events: filter by val', async function() {
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents('allEvents', {
filter: { val: 2 },
fromBlock: 'earliest',
toBlock: 'latest',
});
assert.equal(events.length, 1);
assert.equal(events[0].returnValues.val, 2);
});
it('contract.getPastEvents filter by val different value', async function() {
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents('BasicEvent', {
filter: { val: 3 },
fromBlock: 'earliest',
toBlock: 'latest',
});
assert.equal(events.length, 1);
assert.equal(events[0].returnValues.val, 3);
});
it('contract.getPastEvents filter by array', async function() {
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents('BasicEvent', {
filter: { val: [2, 3] },
fromBlock: 'earliest',
toBlock: 'latest',
});
assert.equal(events.length, 2);
assert.equal(events[0].returnValues.val, 2);
assert.equal(events[1].returnValues.val, 3);
});
it('contract.getPastEvents allEvents: filter by array', async function() {
await prepareEvents(instance, accounts[0]);
const events = await instance.getPastEvents('allEvents', {
filter: { val: [2, 3] },
fromBlock: 'earliest',
toBlock: 'latest',
});
assert.equal(events.length, 2);
assert.equal(events[0].returnValues.val, 2);
assert.equal(events[1].returnValues.val, 3);
});
it('contract.getPastEvents allEvents: filter by array using callback', async function() {
await prepareEvents(instance, accounts[0]);
instance.getPastEvents('allEvents', {
filter: { val: [2, 3] },
fromBlock: 'earliest',
toBlock: 'latest',
}, (err, events) => {
assert.equal(events.length, 2);
assert.equal(events[0].returnValues.val, 2);
assert.equal(events[1].returnValues.val, 3);
});
});
it('contract.getPastEvents filter by val using callback', async function() {
await prepareEvents(instance, accounts[0]);
instance.getPastEvents('BasicEvent', {
filter: { val: 3 },
fromBlock: 'earliest',
toBlock: 'latest',
}, (err, events) => {
assert.equal(events.length, 1);
assert.equal(events[0].returnValues.val, 3);
});
});
it('contract.events.<eventName>', function(){
return new Promise(async resolve => {
instance
.events
.BasicEvent({
fromBlock: 0
})
.on('data', function(event) {
assert.equal(event.event, 'BasicEvent');
this.removeAllListeners();
resolve();
});
await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
});
});
it('works also when toBlock is passed to contract.events.<eventName>', function () {
const originalWarn = console.warn
let message
console.warn = function(str) { message = str }
return new Promise(async resolve => {
instance
.events
.BasicEvent({
fromBlock: 0,
toBlock: 'latest'
}).on('data', function(event) {
assert.equal(event.event, 'BasicEvent');
this.removeAllListeners();
resolve();
});
assert.equal(message, 'Invalid option: toBlock. Use getPastEvents for specific range.');
console.warn = originalWarn
await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
});
});
it('works also when toBlock is passed to contract.events.allEvents', function () {
const originalWarn = console.warn
let message
console.warn = function(str) { message = str }
return new Promise(async (resolve, reject) => {
instance
.events
.allEvents({
fromBlock: 0,
toBlock: 'latest'
}).on('data', function(event) {
this.removeAllListeners();
resolve();
});
assert.equal(message, 'Invalid option: toBlock. Use getPastEvents for specific range.');
console.warn = originalWarn
await instance
.methods
.firesEvent(accounts[0], 1)
.send({ from: accounts[0] });
});
});
it('should not hear the error handler when connection.closed() called', function(){
this.timeout(15000);
let failed = false;
return new Promise(async (resolve, reject) => {
instance
.events
.BasicEvent({
fromBlock: 0
})
.on('error', function(err) {
failed = true;
this.removeAllListeners();
reject(new Error('err listener should not hear connection.close'));
});
await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
web3.currentProvider.connection.close();
// Resolve only if we haven't already rejected
setTimeout(() => { if(!failed) resolve() }, 2500)
});
});
it('should not hear the error handler when provider.disconnect() called', function(){
this.timeout(15000);
let failed = false;
return new Promise(async (resolve, reject) => {
instance
.events
.BasicEvent({
fromBlock: 0
})
.on('error', function(err) {
failed = true;
this.removeAllListeners();
reject(new Error('err listener should not hear provider.disconnect'));
});
await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
web3.currentProvider.disconnect();
// Resolve only if we haven't already rejected
setTimeout(() => { if(!failed) resolve() }, 2500)
});
});
// Regression test for a race-condition where a fresh web3 instance
// subscribing to past events would have its call parameters deleted while it
// made initial Websocket handshake and return an incorrect response.
it('can immediately listen for events in the past', async function(){
this.timeout(15000);
const first = await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
const second = await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
// Go forward one block...
await utils.mine(web3, accounts[0]);
const latestBlock = await web3.eth.getBlockNumber();
assert(first.blockNumber < latestBlock);
assert(second.blockNumber < latestBlock);
// Re-instantiate web3 & instance to simulate
// subscribing to past events as first request
web3 = new Web3('ws://localhost:' + port);
const newInstance = new web3.eth.Contract(Basic.abi, instance.options.address);
let counter = 0;
await new Promise(async resolve => {
newInstance
.events
.BasicEvent({
fromBlock: 0
})
.on('data', function(event) {
counter++;
assert(event.blockNumber < latestBlock);
if (counter === 2){
this.removeAllListeners();
resolve();
}
});
});
});
it('hears events when subscribed to "logs" (emitter)', function(){
return new Promise(async function(resolve, reject){
assert(typeof instance.options.address === 'string');
assert(instance.options.address.length > 0);
const subscription = web3.eth.subscribe(
"logs",
{
address: instance.options.address
})
.once("data", function(log) {
assert.equal(log.address, instance.options.address);
subscription.unsubscribe();
resolve();
});
await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
});
});
it('hears events when subscribed to "logs" (callback)', function(){
return new Promise(async function(resolve, reject){
assert(typeof instance.options.address === 'string');
assert(instance.options.address.length > 0);
const subscription = web3.eth.subscribe(
"logs",
{
address: instance.options.address
},
function(error, log) {
assert.equal(log.address, instance.options.address);
subscription.unsubscribe();
resolve();
});
await instance
.methods
.firesEvent(accounts[0], 1)
.send({from: accounts[0]});
});
});
// Test models event signature shadowing when one contract calls another.
// Child and parent contracts have an event named `similar` which has the same
// function signature but indexes arguments differently.
it('handles child events with shadowed signatures', async function(){
this.timeout(25000);
let contract;
const options = {
gasPrice: 1000000000,// Default gasPrice set by Geth
gas: 4000000
};
options.data = Child.bytecode;
contract = new web3.eth.Contract(Child.abi, options);
const child = await contract.deploy().send({from: accounts[0]});
options.data = Parent.bytecode;
contract = new web3.eth.Contract(Parent.abi, options);
const parent = await contract.deploy().send({from: accounts[0]});
await parent
.methods
.fireChildSimilarEvent(child.options.address)
.send({from: accounts[0]});
await parent
.methods
.fireChildIdenticalEvent(child.options.address)
.send({from: accounts[0]});
const childEvents = await child.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
const parentEvents = await parent.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
assert.equal(childEvents.length, 2);
assert.equal(parentEvents.length, 0);
assert.equal(childEvents[0].event, 'Similar');
assert.equal(typeof childEvents[0].returnValues._owner, 'string');
assert.equal(childEvents[1].event, 'Identical');
assert.equal(typeof childEvents[1].returnValues.childA, 'string');
});
// This test only runs against the ganache client launched in scripts/e2e.ganache.sh
// It's too complicated for geth auto-mining (we'd have to poll for blocks)
// and geth instamine's websockets connection is too fragile for the tests in this file.
it('backfills missed events when auto-reconnecting', function(){
if(!process.env.GANACHE) return;
this.timeout(20000);
let counter = 0;
const acc = accounts[0];
// Create a parallel connection & contract instance
// so we can trigger events while the WS provider is down...
const _web3 = new Web3('http://localhost:8545');
const shadow = new _web3.eth.Contract(Basic.abi, instance.options.address);
// Create a reconnect-enabled WS provider and set the default Web3 with it.
const provider = new Web3.providers.WebsocketProvider(
'ws://localhost:' + port,
{
reconnect: {
auto: true,
delay: 4000,
maxAttempts: 1
}
}
);
web3.setProvider(provider);
return new Promise(async function (resolve) {
instance
.events
.BasicEvent()
.on('data', function(event) {
counter++;
if (counter === 2){
assert(finalBlock >= event.blockNumber || finalBlock <= event.blockNumber + 2);
this.removeAllListeners();
resolve();
}
});
// First: a regular event
const firstReceipt = await instance.methods.firesEvent(acc, 1).send({from: acc});
// Close connection and let it settle...
provider.connection.close(4000);
await utils.waitSeconds(1);
// Submit another event on parallel connection and mine forward 2 blocks
const secondReceipt = await shadow.methods.firesEvent(acc, 1).send({from: acc});
utils.mine(_web3, acc)
utils.mine(_web3, acc)
const finalBlock = await _web3.eth.getBlockNumber();
assert(finalBlock === secondReceipt.blockNumber + 2)
});
});
it('when event param is a simple string', async function(){
const msg = 'simplestring';
await instance
.methods
.firesStringEvent(msg)
.send({from: accounts[0]});
const events = await instance.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
assert.equal(events[0].returnValues.str, msg)
});
// Malformed utf-8 sequence in the following two tests comes from
// https://www.w3.org/2001/06/utf-8-wrong/UTF-8-test.html
// Section: 3.1.8
it('when an invalid utf-8 string is passed in JS as param to emit', async function(){
const msg = '�������';
await instance
.methods
.firesStringEvent(msg)
.send({from: accounts[0]});
const events = await instance.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
assert.equal(msg, events[0].returnValues.str)
});
it('when Solidity emits an invalid utf-8 string', async function(){
await instance
.methods
.firesIllegalUtf8StringEvent()
.send({from: accounts[0]});
const events = await instance.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
assert.equal('�������', events[0].returnValues.str)
});
it('when wide unicode characters are passed in JS as param to emit', async function(){
const msg = '💐';
await instance
.methods
.firesStringEvent(msg)
.send({from: accounts[0]});
const events = await instance.getPastEvents({
fromBlock: 0,
toBlock: 'latest'
});
assert(msg.length > 'a'.length);
assert.equal(msg, events[0].returnValues.str)
})
});