Skip to content

Commit

Permalink
FABN-1492: Define a specific BlockEvent type (#160)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday authored Mar 10, 2020
1 parent 94c9a2e commit fea01f0
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 57 deletions.
34 changes: 30 additions & 4 deletions fabric-network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,19 @@
*/

/**
* @typedef {EventInfo} Network~BlockEvent
* @typedef {object} Network~BlockEvent
* @memberof module:fabric-network
* @property {Long} blockNumber The number of the block this event represents.
* @property {('filtered'|'full'|'private')} type Type of block event. The type will dictate the actual implementation
* sub-type for this event.
* @see module:fabric-network.Network~FilteredBlockEvent
*/

/**
* @typedef {module:fabric-network.Network~BlockEvent} Network~FilteredBlockEvent
* @memberof module:fabric-network
* @property {"filtered"} type Type of block event.
* @property {FilteredBlock} blockData The raw filtered block data.
*/

/**
Expand Down Expand Up @@ -333,10 +344,17 @@
* @returns {module:fabric-network.Network~BlockListener} The added listener.
* @example
* const listener: BlockListener = async (event) => {
* // Handle block event, then (optionally) remove myself as a listener
* network.removeBlockListener(listener);
* // Handle block event
*
* // Listener may remove itself if desired
* if (event.blockNumber === endBlock) {
* network.removeBlockListener(listener);
* }
* }
* await network.addBlockListener(listener);
* const options: ListenerOptions = {
* startBlock: 1
* };
* await network.addBlockListener(listener, options);
*/

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

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


module.exports.Gateway = require('./lib/gateway');
module.exports.Wallet = require('./lib/impl/wallet/wallet').Wallet;
Expand Down
21 changes: 18 additions & 3 deletions fabric-network/src/impl/event/blockeventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { BlockEvent, BlockListener } from './blocklistener';
import { BlockEvent, BlockListener, FilteredBlockEvent } from './blocklistener';
import { OrderedBlockQueue } from './orderedblockqueue';
import { AsyncNotifier } from './asyncnotifier';
import { EventServiceManager } from './eventservicemanager';
Expand All @@ -16,6 +16,7 @@ import {
EventService
} from 'fabric-common';
import Long = require('long');
import util = require('util');

import * as Logger from '../../logger';
const logger = Logger.getLogger('BlockEventSource');
Expand Down Expand Up @@ -117,13 +118,27 @@ export class BlockEventSource {
}
}

private onBlockEvent(event: EventInfo) {
this.blockQueue.addBlock(event!);
private onBlockEvent(eventInfo: EventInfo) {
const blockEvent = this.newBlockEvent(eventInfo);
this.blockQueue.addBlock(blockEvent);
if (this.blockQueue.size() > 0) {
this.asyncNotifier.notify();
}
}

private newBlockEvent(eventInfo: EventInfo): BlockEvent {
if (eventInfo.filteredBlock) {
const blockEvent: FilteredBlockEvent = {
type: 'filtered',
blockNumber: eventInfo.blockNumber,
blockData: eventInfo.filteredBlock
};
return blockEvent;
} else {
throw new Error('Unexpected block event info: ' + util.inspect(eventInfo));
}
}

private async notifyListeners(event: BlockEvent) {
const promises = Array.from(this.listeners).map((listener) => listener(event));
const results = await allSettled(promises);
Expand Down
14 changes: 12 additions & 2 deletions fabric-network/src/impl/event/blocklistener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { EventInfo } from 'fabric-common';
import { FilteredBlock } from 'fabric-common';
import Long = require('long');

export interface BlockEvent {
type: string;
blockNumber: Long;
}

export interface FilteredBlockEvent extends BlockEvent {
type: 'filtered';
blockData: FilteredBlock;
}

export type BlockEvent = EventInfo;
export type BlockListener = (event: BlockEvent) => Promise<void>;
18 changes: 13 additions & 5 deletions fabric-network/src/impl/event/contractlistenersession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as Logger from '../../logger';
import { Network } from '../../network';
import { BlockEvent, BlockListener } from './blocklistener';
import { BlockListener, FilteredBlockEvent, BlockEvent } from './blocklistener';
import { ContractEvent, ContractListener } from './contractlistener';
import { ListenerSession } from './listenersession';
// @ts-ignore no implicit any
Expand All @@ -23,7 +23,7 @@ export class ContractListenerSession implements ListenerSession {
this.listener = listener;
this.chaincodeId = chaincodeId;
this.network = network;
this.blockListener = (blockEvent: BlockEvent) => this.getContractEventsforFilteredBlock(blockEvent);
this.blockListener = (blockEvent: BlockEvent) => this.onBlockEvent(blockEvent);
}

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

private async getContractEventsforFilteredBlock(blockEvent: BlockEvent) {
private async onBlockEvent(blockEvent: BlockEvent): Promise<void> {
if (blockEvent.type === 'filtered') {
await this.getContractEventsforFilteredBlock(blockEvent as FilteredBlockEvent);
} else {
throw new Error(`Unexpected block event type: ${blockEvent.type}`);
}
}

private async getContractEventsforFilteredBlock(blockEvent: FilteredBlockEvent) {

// For filtered blocks
if (blockEvent.filteredBlock && blockEvent.filteredBlock.filtered_transactions) {
if (blockEvent.blockData.filtered_transactions) {
// const blockNumber: string = blockEvent.filteredBlock.number;

for (const filteredTransaction of blockEvent.filteredBlock.filtered_transactions) {
for (const filteredTransaction of blockEvent.blockData.filtered_transactions) {

// const transactionId: string = filteredTransaction.txid;
// const transactionValidationCode: string = this.getTransactionValidationCode(filteredTransaction);
Expand Down
Loading

0 comments on commit fea01f0

Please sign in to comment.