Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit fea01f0

Browse files
FABN-1492: Define a specific BlockEvent type (#160)
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
1 parent 94c9a2e commit fea01f0

File tree

6 files changed

+140
-57
lines changed

6 files changed

+140
-57
lines changed

fabric-network/index.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,19 @@
261261
*/
262262

263263
/**
264-
* @typedef {EventInfo} Network~BlockEvent
264+
* @typedef {object} Network~BlockEvent
265265
* @memberof module:fabric-network
266+
* @property {Long} blockNumber The number of the block this event represents.
267+
* @property {('filtered'|'full'|'private')} type Type of block event. The type will dictate the actual implementation
268+
* sub-type for this event.
269+
* @see module:fabric-network.Network~FilteredBlockEvent
270+
*/
271+
272+
/**
273+
* @typedef {module:fabric-network.Network~BlockEvent} Network~FilteredBlockEvent
274+
* @memberof module:fabric-network
275+
* @property {"filtered"} type Type of block event.
276+
* @property {FilteredBlock} blockData The raw filtered block data.
266277
*/
267278

268279
/**
@@ -333,10 +344,17 @@
333344
* @returns {module:fabric-network.Network~BlockListener} The added listener.
334345
* @example
335346
* const listener: BlockListener = async (event) => {
336-
* // Handle block event, then (optionally) remove myself as a listener
337-
* network.removeBlockListener(listener);
347+
* // Handle block event
348+
*
349+
* // Listener may remove itself if desired
350+
* if (event.blockNumber === endBlock) {
351+
* network.removeBlockListener(listener);
352+
* }
338353
* }
339-
* await network.addBlockListener(listener);
354+
* const options: ListenerOptions = {
355+
* startBlock: 1
356+
* };
357+
* await network.addBlockListener(listener, options);
340358
*/
341359

342360
/**
@@ -346,6 +364,14 @@
346364
* @param listener {module:fabric-network.Network~BlockListener} A block listener callback function.
347365
*/
348366

367+
/**
368+
* A callback function that will be invoked when either a peer communication error occurs or a transaction commit event
369+
* is received. Only one of the two arguments will have a value for any given invocation.
370+
* @callback Network~BlockListener
371+
* @memberof module:fabric-network
372+
* @param {module:fabric-network.Network~BlockEvent} event A block event.
373+
*/
374+
349375

350376
module.exports.Gateway = require('./lib/gateway');
351377
module.exports.Wallet = require('./lib/impl/wallet/wallet').Wallet;

fabric-network/src/impl/event/blockeventsource.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import { BlockEvent, BlockListener } from './blocklistener';
7+
import { BlockEvent, BlockListener, FilteredBlockEvent } from './blocklistener';
88
import { OrderedBlockQueue } from './orderedblockqueue';
99
import { AsyncNotifier } from './asyncnotifier';
1010
import { EventServiceManager } from './eventservicemanager';
@@ -16,6 +16,7 @@ import {
1616
EventService
1717
} from 'fabric-common';
1818
import Long = require('long');
19+
import util = require('util');
1920

2021
import * as Logger from '../../logger';
2122
const logger = Logger.getLogger('BlockEventSource');
@@ -117,13 +118,27 @@ export class BlockEventSource {
117118
}
118119
}
119120

120-
private onBlockEvent(event: EventInfo) {
121-
this.blockQueue.addBlock(event!);
121+
private onBlockEvent(eventInfo: EventInfo) {
122+
const blockEvent = this.newBlockEvent(eventInfo);
123+
this.blockQueue.addBlock(blockEvent);
122124
if (this.blockQueue.size() > 0) {
123125
this.asyncNotifier.notify();
124126
}
125127
}
126128

129+
private newBlockEvent(eventInfo: EventInfo): BlockEvent {
130+
if (eventInfo.filteredBlock) {
131+
const blockEvent: FilteredBlockEvent = {
132+
type: 'filtered',
133+
blockNumber: eventInfo.blockNumber,
134+
blockData: eventInfo.filteredBlock
135+
};
136+
return blockEvent;
137+
} else {
138+
throw new Error('Unexpected block event info: ' + util.inspect(eventInfo));
139+
}
140+
}
141+
127142
private async notifyListeners(event: BlockEvent) {
128143
const promises = Array.from(this.listeners).map((listener) => listener(event));
129144
const results = await allSettled(promises);

fabric-network/src/impl/event/blocklistener.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import { EventInfo } from 'fabric-common';
7+
import { FilteredBlock } from 'fabric-common';
8+
import Long = require('long');
9+
10+
export interface BlockEvent {
11+
type: string;
12+
blockNumber: Long;
13+
}
14+
15+
export interface FilteredBlockEvent extends BlockEvent {
16+
type: 'filtered';
17+
blockData: FilteredBlock;
18+
}
819

9-
export type BlockEvent = EventInfo;
1020
export type BlockListener = (event: BlockEvent) => Promise<void>;

fabric-network/src/impl/event/contractlistenersession.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as Logger from '../../logger';
88
import { Network } from '../../network';
9-
import { BlockEvent, BlockListener } from './blocklistener';
9+
import { BlockListener, FilteredBlockEvent, BlockEvent } from './blocklistener';
1010
import { ContractEvent, ContractListener } from './contractlistener';
1111
import { ListenerSession } from './listenersession';
1212
// @ts-ignore no implicit any
@@ -23,7 +23,7 @@ export class ContractListenerSession implements ListenerSession {
2323
this.listener = listener;
2424
this.chaincodeId = chaincodeId;
2525
this.network = network;
26-
this.blockListener = (blockEvent: BlockEvent) => this.getContractEventsforFilteredBlock(blockEvent);
26+
this.blockListener = (blockEvent: BlockEvent) => this.onBlockEvent(blockEvent);
2727
}
2828

2929
public async start() {
@@ -34,13 +34,21 @@ export class ContractListenerSession implements ListenerSession {
3434
this.network.removeBlockListener(this.blockListener);
3535
}
3636

37-
private async getContractEventsforFilteredBlock(blockEvent: BlockEvent) {
37+
private async onBlockEvent(blockEvent: BlockEvent): Promise<void> {
38+
if (blockEvent.type === 'filtered') {
39+
await this.getContractEventsforFilteredBlock(blockEvent as FilteredBlockEvent);
40+
} else {
41+
throw new Error(`Unexpected block event type: ${blockEvent.type}`);
42+
}
43+
}
44+
45+
private async getContractEventsforFilteredBlock(blockEvent: FilteredBlockEvent) {
3846

3947
// For filtered blocks
40-
if (blockEvent.filteredBlock && blockEvent.filteredBlock.filtered_transactions) {
48+
if (blockEvent.blockData.filtered_transactions) {
4149
// const blockNumber: string = blockEvent.filteredBlock.number;
4250

43-
for (const filteredTransaction of blockEvent.filteredBlock.filtered_transactions) {
51+
for (const filteredTransaction of blockEvent.blockData.filtered_transactions) {
4452

4553
// const transactionId: string = filteredTransaction.txid;
4654
// const transactionValidationCode: string = this.getTransactionValidationCode(filteredTransaction);

0 commit comments

Comments
 (0)