From b7cdb9a7ac4d19bf583790dc7999f396686e5d12 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Mon, 24 Feb 2020 15:09:32 -0800 Subject: [PATCH 1/3] Reproduction case for issue 3389 --- test/e2e.contract.events.js | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/e2e.contract.events.js b/test/e2e.contract.events.js index 8d0462799cf..7405e0c6d30 100644 --- a/test/e2e.contract.events.js +++ b/test/e2e.contract.events.js @@ -5,6 +5,10 @@ var Parent = require('./sources/Parent'); var utils = require('./helpers/test.utils'); var Web3 = utils.getWeb3(); +async function delay(secs=0){ + return new Promise(resolve => setTimeout(() => resolve(), secs * 1000)) +} + describe('contract.events [ @E2E ]', function() { // `getPastEvents` not working with Geth instamine over websockets. if (process.env.GETH_INSTAMINE) return; @@ -30,6 +34,59 @@ describe('contract.events [ @E2E ]', function() { instance = await basic.deploy().send({from: accounts[0]}); }); + it.only('gets all events starting from block 0', async function(){ + // Only test geth, automining at 2s interval + if (!process.env.GETH_AUTOMINE) return; + + this.timeout(20000); + + let startingBlock = 0; + let newBlock = 0; + + startingBlock = await web3.eth.getBlockNumber(); + console.log(`startingBlock = ${startingBlock}`); + + // Fire 1st event + await instance + .methods + .firesEvent(accounts[0], 1) + .send({from: accounts[0]}); + + // Wait for blocks to mine + await delay(3); + + newBlock = await web3.eth.getBlockNumber(); + console.log(`newBlock = ${newBlock}`); + + // Fire 2nd event + await instance + .methods + .firesEvent(accounts[0], 1) + .send({from: accounts[0]}); + + + // Wait for blocks to mine + await delay(3); + newBlock = await web3.eth.getBlockNumber(); + console.log(`newBlock = ${newBlock}`); + + // Subscribe to events, requesting all from the past... + let counter = 0; + return new Promise(async function(resolve){ + instance.events.BasicEvent({ + fromBlock: 0, + }) + .on('data', function (event) { + counter++; + console.log(`got events: ${JSON.stringify(event, null, ' ')}`) + + if (counter === 2){ + resolve() + } + }) + }) + }); + it('contract.getPastEvents', async function(){ await instance .methods From c08c6a6c0e19f9979360d6d631e806b40e90474d Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 25 Feb 2020 11:31:33 -0800 Subject: [PATCH 2/3] Model subscription for low numbered blocks --- scripts/e2e.geth.automine.sh | 2 +- test/e2e.contract.events.js | 66 +++++++++++------------------------- 2 files changed, 21 insertions(+), 47 deletions(-) diff --git a/scripts/e2e.geth.automine.sh b/scripts/e2e.geth.automine.sh index 0282d15d4e7..aa8000fbf1b 100755 --- a/scripts/e2e.geth.automine.sh +++ b/scripts/e2e.geth.automine.sh @@ -24,7 +24,7 @@ echo " " # Launch client w/ two unlocked accounts. # + accounts[0] default geth unlocked bal = ~infinity # + accounts[1] unlocked, signing password = 'left-hand-of-darkness' -geth-dev-assistant --period 2 --accounts 1 --tag 'stable' +geth-dev-assistant --period 20 --accounts 1 --tag 'stable' # Test GETH_AUTOMINE=true nyc --no-clean --silent _mocha -- \ diff --git a/test/e2e.contract.events.js b/test/e2e.contract.events.js index 7405e0c6d30..aa257f40bbc 100644 --- a/test/e2e.contract.events.js +++ b/test/e2e.contract.events.js @@ -11,7 +11,7 @@ async function delay(secs=0){ describe('contract.events [ @E2E ]', function() { // `getPastEvents` not working with Geth instamine over websockets. - if (process.env.GETH_INSTAMINE) return; + //if (process.env.GETH_INSTAMINE) return; var web3; var accounts; @@ -34,57 +34,31 @@ describe('contract.events [ @E2E ]', function() { instance = await basic.deploy().send({from: accounts[0]}); }); - it.only('gets all events starting from block 0', async function(){ - // Only test geth, automining at 2s interval + it.only('subscribes to events from a block numbered less than 10', async function(){ + // Only test geth, automining at 20s interval if (!process.env.GETH_AUTOMINE) return; this.timeout(20000); - let startingBlock = 0; - let newBlock = 0; - - startingBlock = await web3.eth.getBlockNumber(); - console.log(`startingBlock = ${startingBlock}`); - - // Fire 1st event - await instance - .methods - .firesEvent(accounts[0], 1) - .send({from: accounts[0]}); - - // Wait for blocks to mine - await delay(3); - - newBlock = await web3.eth.getBlockNumber(); - console.log(`newBlock = ${newBlock}`); - - // Fire 2nd event - await instance - .methods - .firesEvent(accounts[0], 1) - .send({from: accounts[0]}); - - - // Wait for blocks to mine - await delay(3); - newBlock = await web3.eth.getBlockNumber(); - console.log(`newBlock = ${newBlock}`); - - // Subscribe to events, requesting all from the past... - let counter = 0; return new Promise(async function(resolve){ - instance.events.BasicEvent({ - fromBlock: 0, - }) - .on('data', function (event) { - counter++; - console.log(`got events: ${JSON.stringify(event, null, ' ')}`) - - if (counter === 2){ - resolve() + web3.eth.subscribe('logs', {}, function(err, event){ + if (!err){ + console.log(`EVENT --> ${JSON.stringify(event, null, ' ')}`); + resolve(); + } else { + console.log("err:" + err); + reject(); } - }) - }) + }); + + const startingBlock = await web3.eth.getBlockNumber(); + console.log(`startingBlock = ${startingBlock}`); + + await instance + .methods + .firesEvent(accounts[0], 1) + .send({from: accounts[0]}); + }); }); it('contract.getPastEvents', async function(){ From 718147e7eaece99c361d7dbc09f48c8bde062fd1 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Tue, 25 Feb 2020 11:56:43 -0800 Subject: [PATCH 3/3] Increase timeout & add blockNumber assertion --- test/e2e.contract.events.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/e2e.contract.events.js b/test/e2e.contract.events.js index aa257f40bbc..bc3128299cc 100644 --- a/test/e2e.contract.events.js +++ b/test/e2e.contract.events.js @@ -10,6 +10,7 @@ async function delay(secs=0){ } describe('contract.events [ @E2E ]', function() { + this.timeout(25000); // `getPastEvents` not working with Geth instamine over websockets. //if (process.env.GETH_INSTAMINE) return; @@ -38,10 +39,10 @@ describe('contract.events [ @E2E ]', function() { // Only test geth, automining at 20s interval if (!process.env.GETH_AUTOMINE) return; - this.timeout(20000); - return new Promise(async function(resolve){ web3.eth.subscribe('logs', {}, function(err, event){ + assert(event.blockNumber < 10); + if (!err){ console.log(`EVENT --> ${JSON.stringify(event, null, ' ')}`); resolve();