diff --git a/fabric-network/lib/impl/event/blockeventlistener.js b/fabric-network/lib/impl/event/blockeventlistener.js index 3f7a4111dd..f78e78dff5 100644 --- a/fabric-network/lib/impl/event/blockeventlistener.js +++ b/fabric-network/lib/impl/event/blockeventlistener.js @@ -68,7 +68,12 @@ class BlockEventListener extends AbstractEventListener { * @private */ async _onEvent(block) { - const blockNumber = Number(block.number); + let blockNumber; + if (!this._filtered) { + blockNumber = Number(block.header.number); + } else { + blockNumber = Number(block.number); + } try { await this.eventCallback(null, block); diff --git a/fabric-network/test/impl/event/blockeventlistener.js b/fabric-network/test/impl/event/blockeventlistener.js index 73853e2cbd..99e6c21e74 100644 --- a/fabric-network/test/impl/event/blockeventlistener.js +++ b/fabric-network/test/impl/event/blockeventlistener.js @@ -96,7 +96,7 @@ describe('BlockEventListener', () => { }); it('should call the event callback', async () => { - const block = {number: '10'}; + const block = {header: {number: '10'}}; await blockEventListener._onEvent(block); sinon.assert.calledWith(blockEventListener.eventCallback, null, block); sinon.assert.notCalled(checkpointerStub.save); @@ -104,7 +104,7 @@ describe('BlockEventListener', () => { }); it('should save a checkpoint', async () => { - const block = {number: '10'}; + const block = {header: {number: '10'}}; checkpointerStub.load.returns({}); blockEventListener.checkpointer = checkpointerStub; await blockEventListener._onEvent(block); @@ -112,7 +112,7 @@ describe('BlockEventListener', () => { }); it('should unregister if registration.unregister is set', async () => { - const block = {number: '10'}; + const block = {header: {number: '10'}}; blockEventListener._registration.unregister = true; await blockEventListener._onEvent(block); sinon.assert.calledWith(blockEventListener.eventCallback, null, block); @@ -120,7 +120,7 @@ describe('BlockEventListener', () => { }); it ('should not save a checkpoint if the callback fails', async () => { - const block = {number: '10'}; + const block = {header: {number: '10'}}; blockEventListener.eventCallback.throws(new Error()); blockEventListener.checkpointer = checkpointerStub; await blockEventListener._onEvent(block); @@ -234,14 +234,14 @@ describe('BlockEventListener', () => { blockEventListener.eventHub = eventHubStub; eventHubStub.registerBlockEvent.returns({}); await blockEventListener.register(); - await blockEventListener._onEvent({number: 1}); + await blockEventListener._onEvent({header: {number: 1}}); const checkpoint = await checkpointer.load(); expect(checkpoint.blockNumber).to.equal(1); }); - it('should update with a new block number', async () => { + it('should update with a new block number (filtered)', async () => { checkpointer = new InMemoryCheckpointer(); - blockEventListener = new BlockEventListener(networkStub, 'listener', (block) => {}, {replay: true, checkpointer: {factory: () => checkpointer}}); + blockEventListener = new BlockEventListener(networkStub, 'listener', (block) => {}, {filtered: true, replay: true, checkpointer: {factory: () => checkpointer}}); blockEventListener.eventHub = eventHubStub; eventHubStub.registerBlockEvent.returns({}); await blockEventListener.register(); @@ -251,6 +251,18 @@ describe('BlockEventListener', () => { expect(checkpoint.blockNumber).to.equal(2); }); + it('should update with a new block number (unfiltered)', async () => { + checkpointer = new InMemoryCheckpointer(); + blockEventListener = new BlockEventListener(networkStub, 'listener', (block) => {}, {replay: true, checkpointer: {factory: () => checkpointer}}); + blockEventListener.eventHub = eventHubStub; + eventHubStub.registerBlockEvent.returns({}); + await blockEventListener.register(); + await blockEventListener._onEvent({header: {number: 2}}); + await blockEventListener._onEvent({header: {number: 1}}); + const checkpoint = await checkpointer.load(); + expect(checkpoint.blockNumber).to.equal(2); + }); + it('should set the start block', async() => { channelStub.queryInfo.resolves({height: '3'}); checkpointer = new InMemoryCheckpointer();