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

Commit ddf1d71

Browse files
FABN-1491: Updated realtime block event listening (#144)
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
1 parent c31fa5e commit ddf1d71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1733
-1109
lines changed

fabric-common/types/index.d.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,31 @@ export interface UserConfig {
5252
roles?: string[];
5353
}
5454

55+
export interface ConnectionInfo {
56+
type: string;
57+
name: string;
58+
url: string;
59+
options: object;
60+
}
61+
export interface ServiceError extends Error {
62+
connection: ConnectionInfo;
63+
}
64+
5565
export interface ProposalResponse {
56-
errors: Error[];
66+
errors: ServiceError[];
5767
responses: EndorsementResponse[];
5868
queryResults: Buffer[];
5969
}
6070

6171
export interface EndorsementResponse {
72+
connection: ConnectionInfo;
6273
response: {
6374
status: number;
6475
message: string;
6576
payload: Buffer;
6677
};
6778
payload: Buffer;
68-
endorsment: {
79+
endorsement: {
6980
endorser: Buffer;
7081
signature: Buffer;
7182
};
@@ -113,8 +124,10 @@ export class DiscoveryHandler extends ServiceHandler {
113124

114125
export class ServiceEndpoint {
115126
public readonly name: string;
127+
public readonly mspid: string;
128+
public readonly endpoint: Endpoint;
116129
constructor(name: string, client: Client, mspid?: string);
117-
public connect(endpoint: Endpoint, options: ConnectOptions): Promise<void>;
130+
public connect(endpoint: Endpoint, options?: ConnectOptions): Promise<void>;
118131
public disconnect(): void;
119132
public checkConnection(): Promise<boolean>;
120133
public isTLS(): boolean;
@@ -152,8 +165,8 @@ export class ServiceAction {
152165

153166
export class Commit extends Proposal {
154167
constructor(chaincodeName: string, channel: Channel, endorsement: Endorsement);
155-
public build(idContext: IdentityContext, request: any): Buffer;
156-
public send(request: any, options: any): Promise<any>;
168+
public build(idContext: IdentityContext, request?: any): Buffer;
169+
public send(request?: any): Promise<any>;
157170
}
158171

159172
export class Endorsement extends Proposal {
@@ -171,8 +184,8 @@ export class Proposal extends ServiceAction {
171184
public buildProposalInterest(): any;
172185
public addCollectionInterest(collectionName: string): Proposal;
173186
public addChaincodeCollectionsInterest(collectionName: string, collectionNames: string[]): Proposal;
174-
public build(idContext: IdentityContext, request: any): Buffer;
175-
public send(request: any, options: any): Promise<ProposalResponse>;
187+
public build(idContext: IdentityContext, request?: any): Buffer;
188+
public send(request?: any): Promise<ProposalResponse>;
176189
public verifyProposalResponse(proposalResponse?: any): boolean;
177190
public compareProposalResponseResults(proposalResponses: any[]): boolean;
178191
}
@@ -181,8 +194,8 @@ export class DiscoveryService extends ServiceAction {
181194
constructor(chaincodeName: string, channel: Channel);
182195
public setDiscoverer(discoverer: Discoverer): DiscoveryService;
183196
public newHandler(): DiscoveryHandler;
184-
public build(idContext: IdentityContext, request: any): Buffer;
185-
public send(request: any): Promise<any>;
197+
public build(idContext: IdentityContext, request?: any): Buffer;
198+
public send(request?: any): Promise<any>;
186199
public getDiscoveryResults(refresh?: boolean): Promise<any>;
187200
public close(): void;
188201
}
@@ -247,6 +260,7 @@ export class Client {
247260
public newEndpoint(options: ConnectOptions): Endpoint;
248261
public newEndorser(name: string, mspid?: string): Endorser;
249262
public getEndorser(name: string, mspid?: string): Endorser;
263+
public getEndorsers(mspid?: string): Endorser[];
250264
public newCommitter(name: string, mspid?: string): Committer;
251265
public getCommitter(name: string, mspid?: string): Committer;
252266
public newEventer(name: string, mspid?: string): Eventer;
@@ -270,6 +284,9 @@ export interface ConnectOptions {
270284
}
271285

272286
export class Channel {
287+
readonly name: string;
288+
readonly client: Client;
289+
273290
constructor(name: string, client: Client);
274291
public close(): void;
275292

fabric-network/index.js

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,208 @@
143143
* @returns {Promise<void>}
144144
*/
145145

146+
/**
147+
* Factory function to obtain transaction event handler instances. Called on every transaction submit.
148+
* @typedef {Function} TxEventHandlerFactory
149+
* @memberof module:fabric-network
150+
* @param {string} transactionId The ID of the transaction being submitted.
151+
* @param {module:fabric-network.Network} network The network on which this transaction is being submitted.
152+
* @returns {module:fabric-network.TxEventHandler} A transaction event handler.
153+
*/
154+
155+
/**
156+
* Handler used to wait for commit events when a transaction is submitted.
157+
* @interface TxEventHandler
158+
* @memberof module:fabric-network
159+
*/
160+
/**
161+
* Resolves when the handler has started listening for transaction commit events. Called after the transaction proposal
162+
* has been accepted and prior to submission of the transaction to the orderer.
163+
* @function module:fabric-network.TxEventHandler#startListening
164+
* @async
165+
* @returns {Promise<void>}
166+
*/
167+
/**
168+
* Resolves (or rejects) when suitable transaction commit events have been received. Called after submission of the
169+
* transaction to the orderer.
170+
* @function module:fabric-network.TxEventHandler#waitForEvents
171+
* @async
172+
* @returns {Promise<void>}
173+
*/
174+
/**
175+
* Called if submission of the transaction to the orderer fails.
176+
* @function module:fabric-network.TxEventHandler#cancelListening
177+
* @returns {void}
178+
*/
179+
180+
/**
181+
* Factory function to obtain query handler instances. Called on every network creation.
182+
* @typedef {Function} QueryHandlerFactory
183+
* @memberof module:fabric-network
184+
* @param {module:fabric-network.Network} network The network on which queries are being evaluated.
185+
* @returns {module:fabric-network.QueryHandler} A query handler.
186+
*/
187+
188+
/**
189+
* Handler used to obtain query results from peers when a transaction is evaluated.
190+
* @interface QueryHandler
191+
* @memberof module:fabric-network
192+
*/
193+
/**
194+
* Called when a transaction is evaluated to obtain query results from suitable network peers.
195+
* @function module:fabric-network.QueryHandler#evaluate
196+
* @async
197+
* @param {module:fabric-network.Query} query Query object that can be used by the handler to send the query to
198+
* specific peers.
199+
* @returns {Promise<Buffer>}
200+
*/
201+
202+
/**
203+
* Used by query handler implementations to evaluate transactions on peers of their choosing.
204+
* @interface Query
205+
* @memberof module:fabric-network
206+
*/
207+
/**
208+
* Get query results from specified peers.
209+
* @function module:fabric-network.Query#evaluate
210+
* @async
211+
* @param {Endorser[]} peers
212+
* @returns {Promise<Array<module:fabric-network.Query~QueryResponse | Error>>}
213+
*/
214+
215+
/**
216+
* @typedef {Object} Query~QueryResponse
217+
* @memberof module:fabric-network
218+
* @property {boolean} isEndorsed True if the proposal was endorsed by the peer.
219+
* @property {number} status The status value from the endorsement. This attriibute will be set by the chaincode.
220+
* @property {Buffer} payload The payload value from the endorsement. This attribute may be considered the query value
221+
* if the proposal was endorsed by the peer.
222+
* @property {string} message The message value from the endorsement. This property contains the error message from
223+
* the peer if it did not endorse the proposal.
224+
*/
225+
226+
/**
227+
* A callback function that will be invoked when either a peer communication error occurs or a transaction commit event
228+
* is received. Only one of the two arguments will have a value for any given invocation.
229+
* @callback Network~CommitListener
230+
* @memberof module:fabric-network
231+
* @param {module:fabric-network.Network~CommitError} [error] Peer communication error.
232+
* @param {module:fabric-network.Network~CommitEvent} [event] Transaction commit event from a specific peer.
233+
*/
234+
235+
/**
236+
* @typedef {Error} Network~CommitError
237+
* @memberof module:fabric-network
238+
* @property {Endorser} peer The peer that raised this error.
239+
*/
240+
241+
/**
242+
* @typedef {EventInfo} Network~CommitEvent
243+
* @memberof module:fabric-network
244+
* @property {Endorser} peer The peer that raised this error.
245+
*/
246+
247+
/**
248+
* A callback function that will be invoked when a block event is received. Block events will be received in order and
249+
* without duplication.
250+
* @callback Network~CommitListener
251+
* @memberof module:fabric-network
252+
* @async
253+
* @param {module:fabric-network.Network~BlockEvent} event Block event.
254+
* @returns {Promise<void>}
255+
*/
256+
257+
/**
258+
* @typedef {EventInfo} Network~BlockEvent
259+
* @memberof module:fabric-network
260+
*/
261+
262+
/**
263+
* A Network represents the set of peers in a Fabric network.
264+
* Applications should get a Network instance using the
265+
* gateway's [getNetwork]{@link module:fabric-network.Gateway#getNetwork} method.
266+
* @interface Network
267+
* @memberof module:fabric-network
268+
*/
269+
270+
/**
271+
* Get the owning Gateway connection.
272+
* @method Network#getGateway
273+
* @memberof module:fabric-network
274+
* @returns {module:fabric-network.Gateway} A Gateway.
275+
*/
276+
277+
/**
278+
* Get an instance of a contract (chaincode) on the current network.
279+
* @method Network#getContract
280+
* @memberof module:fabric-network
281+
* @param {string} chaincodeId - the chaincode identifier.
282+
* @param {string} [name] - the name of the contract.
283+
* @param {string[]} [collections] - the names of collections defined for this chaincode.
284+
* @returns {module:fabric-network.Contract} the contract.
285+
*/
286+
287+
/**
288+
* Get the underlying channel object representation of this network.
289+
* @method Network#getChannel
290+
* @memberof module:fabric-network
291+
* @returns {Channel} A channel.
292+
*/
293+
294+
/**
295+
* Add a listener to receive transaction commit and peer disconnect events for a set of peers.
296+
* @method Network#addCommitListener
297+
* @memberof module:fabric-network
298+
* @param {module:fabric-network.Network~CommitListener} listener A transaction commit listener callback function.
299+
* @param {Endorser[]} peers The peers from which to receive events.
300+
* @param {string} transactionId A transaction ID.
301+
* @returns {module:fabric-network.Network~CommitListener} The added listener.
302+
* @example
303+
* const listener: CommitListener = (error, event) => {
304+
* if (error) {
305+
* // Handle peer communication error
306+
* } else {
307+
* // Handle transaction commit event
308+
* }
309+
* }
310+
* const peers = network.channel.getEndorsers();
311+
* await network.addCommitListener(listener, peers, transactionId);
312+
*/
313+
314+
/**
315+
* Remove a previously added transaction commit listener.
316+
* @method Network#removeCommitListener
317+
* @memberof module:fabric-network
318+
* @param {module:fabric-network.Network~CommitListener} listener A transaction commit listener callback function.
319+
*/
320+
321+
/**
322+
* Add a listener to receive block events for this network. Blocks will be received in order and without duplication.
323+
* @method Network#addBlockListener
324+
* @memberof module:fabric-network
325+
* @param {module:fabric-network.Network~BlockListener} listener A block listener callback function.
326+
* @returns {module:fabric-network.Network~BlockListener} The added listener.
327+
* @example
328+
* const listener: BlockListener = async (event) => {
329+
* // Handle block event, then (optionally) remove myself as a listener
330+
* network.removeBlockListener(listener);
331+
* }
332+
* await network.addBlockListener(listener);
333+
*/
334+
335+
/**
336+
* Remove a previously added block listener.
337+
* @method Network#removeBlockListener
338+
* @memberof module:fabric-network
339+
* @param listener {module:fabric-network.Network~BlockListener} A block listener callback function.
340+
*/
341+
342+
146343
module.exports.Gateway = require('./lib/gateway');
147344
module.exports.Wallet = require('./lib/impl/wallet/wallet').Wallet;
148345
module.exports.Wallets = require('./lib/impl/wallet/wallets').Wallets;
149346
module.exports.IdentityProviderRegistry = require('./lib/impl/wallet/identityproviderregistry').IdentityProviderRegistry;
150347
module.exports.HsmX509Provider = require('./lib/impl/wallet/hsmx509identity').HsmX509Provider;
151348
module.exports.DefaultEventHandlerStrategies = require('./lib/impl/event/defaulteventhandlerstrategies');
152-
module.exports.QueryHandlerStrategies = require('./lib/impl/query/queryhandlerstrategies');
349+
module.exports.DefaultQueryHandlerStrategies = require('./lib/impl/query/defaultqueryhandlerstrategies');
153350
module.exports.TimeoutError = require('./lib/errors/timeouterror').TimeoutError;

fabric-network/src/contract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Contract {
133133
if (eventService) {
134134
listener.eventService = eventService;
135135
}
136-
this.network.saveListener(listener, listener);
136+
this.network.saveListener(listener);
137137
await listener.register();
138138

139139
return listener;

fabric-network/src/gateway.js

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
'use strict';
88

9-
const Network = require('./network');
9+
const {NetworkImpl: Network} = require('./network');
1010
const NetworkConfig = require('./impl/ccp/networkconfig');
1111
const {Client} = require('fabric-common');
1212
const EventStrategies = require('./impl/event/defaulteventhandlerstrategies');
13-
const QueryStrategies = require('./impl/query/queryhandlerstrategies');
13+
const QueryStrategies = require('./impl/query/defaultqueryhandlerstrategies');
1414

1515
const logger = require('./logger').getLogger('Gateway');
1616

@@ -35,57 +35,22 @@ const logger = require('./logger').getLogger('Gateway');
3535
* for commit notification to complete.
3636
* @property {number} [endorseTimeout = 30] The timeout period in seconds to wait
3737
* for the endorsement to complete.
38-
* @property {?module:fabric-network.Gateway~TxEventHandlerFactory} [strategy=MSPID_SCOPE_ALLFORTX]
38+
* @property {?module:fabric-network.TxEventHandlerFactory} [strategy=MSPID_SCOPE_ALLFORTX]
3939
* Event handling strategy to identify successful transaction commits. A null value indicates
4040
* that no event handling is desired. The default is
4141
* [MSPID_SCOPE_ALLFORTX]{@link module:fabric-network.EventHandlerStrategies}.
4242
*/
4343

44-
/**
45-
* @typedef {Function} Gateway~TxEventHandlerFactory
46-
* @memberof module:fabric-network
47-
* @param {Transaction} transaction The transaction for which the handler should listen for commit events.
48-
* @param {module:fabric-network.Network} network The network on which this transaction is being submitted.
49-
* @returns {module:fabric-network.Gateway~TxEventHandler} A transaction event handler.
50-
*/
51-
52-
/**
53-
* @typedef {Object} Gateway~TxEventHandler
54-
* @memberof module:fabric-network
55-
* @property {Function} startListening Async function that resolves when the handler has started listening for
56-
* transaction commit events. Called after the transaction proposal has been accepted and prior to submission of
57-
* the transaction to the orderer.
58-
* @property {Function} waitForEvents Async function that resolves (or rejects) when suitable transaction
59-
* commit events have been received. Called after submission of the transaction to the orderer.
60-
* @property {Function} cancelListening Cancel listening. Called if submission of the transaction to the orderer
61-
* fails.
62-
*/
63-
6444
/**
6545
* @typedef {Object} Gateway~QueryOptions
6646
* @memberof module:fabric-network
6747
* @property {number} [timeout = 30] The timeout period in seconds to wait for the query to
6848
* complete.
69-
* @property {module:fabric-network.Gateway~QueryHandlerFactory} [strategy=MSPID_SCOPE_SINGLE]
49+
* @property {module:fabric-network.QueryHandlerFactory} [strategy=MSPID_SCOPE_SINGLE]
7050
* Query handling strategy used to evaluate queries. The default is
7151
* [MSPID_SCOPE_SINGLE]{@link module:fabric-network.QueryHandlerStrategies}.
7252
*/
7353

74-
/**
75-
* @typedef {Function} Gateway~QueryHandlerFactory
76-
* @memberof module:fabric-network
77-
* @param {module:fabric-network.Network} network The network on which queries are being evaluated.
78-
* @param {Object} options The request options to use when queries are being evaluated.
79-
* @returns {module:fabric-network.Gateway~QueryHandler} A query handler.
80-
*/
81-
82-
/**
83-
* @typedef {Object} Gateway~QueryHandler
84-
* @memberof module:fabric-network
85-
* @property {Function} evaluate Async function that takes a [Query]{@link module:fabric-common.Query}
86-
* and resolves with the result of the query evaluation.
87-
*/
88-
8954
/**
9055
* @typedef {Object} Gateway~DiscoveryOptions
9156
* @memberof module:fabric-network

0 commit comments

Comments
 (0)