100 likes | 109 Views
Node SDK: Fabric Network Event Handling FABN-1100. Liam Grace @14gracel ( Rocket.Chat ). Abstractions. Adding block event listeners added to Network Adding chaincode event listeners added to Contract (and renamed chaincode event to contract event)
E N D
Node SDK: Fabric Network Event Handling FABN-1100 Liam Grace @14gracel (Rocket.Chat)
Abstractions Adding block event listeners added to Network Adding chaincode event listeners added to Contract (and renamed chaincode event to contract event) Adding transaction event listeners added to Transaction and Network Manage event replay on a contract, block and transaction events without the need for custom persistence logic Auto recovering Event Hubs • Identify if an Event Hub goes down and automatically find a new one • Pluggable Event Hub selection logic Event Hub Manager on the Network the responsible for all event hubs across the SDK
APIs Node SDK currently uses ChannelEventHub to manage chaincode, block and transaction events New Classes • AbstractEventListener • Base class for block, transaction and contract (chaincode) event listeners • EventHubManager • Manages event hub creation, reuse and reconnection • BaseCheckpointer • Records the last block height and transaction ID seen by each listener for event replay • BaseEventHubSelectionStrategy • Pluggable strategy for selecting new event hubs New Methods • Contract.addContractListener(listenerName, event, callback, options) • Network.addBlockListener(listenerName, callback, options) • Network.addCommitListener(txId, callback, options, eventHub) • Transaction.addCommitListener(callback, options, eventHub)
Example Contract Event Listener User wants to listen to all sell events to use in its analytics platform. Replay is required • User doesn’t change their callback, SDK controls recording the checkpoint and replaying events Function FILE_SYSTEM_CHECKPOINTER(channelName, listenerName, options) { return new FileSystemCheckpointer(channelName, listenerName, options) } await gateway.connect(ccp, { identity: 'admin', wallet: wallet, checkpointer: { factory: FILE_SYSTEM_CHECKPOINTER, options: {} }, eventHubSelectionOptions: { strategy: EventHubSelectionStrategies.MSPID_SCOPE_ROUND_ROBIN } }); const network = await gateway.getNetwork('market1234'); const contract = network.getContract('commercial-paper’); await contract.addContractListener('sell-listener', 'sell’, (err, event, blockNumber, txid) => { if (err) { // Disconnect error handled for the user (new event hub assigned), but they are still notified console.error(err); return; } analytics.save(txid, blockNumber, event); }, {replay: true});
Example Block Event Listener User wants to listen to all block events but doesn’t want event replay • Checkpointing and replay logic are ignored ... await network.addBlockListener(‘block-listener', (err, block) => { if (err) { // Disconnect error handled for the user (new event hub assigned), but they are still notified console.error(err); return; } const lastTxId = block.filtered_transactions[block.filtered_transactions.length - 1].txid; console.info(`Block height ${block.number}`, Last Transaction ID: ${lastTxId}) });
Example Transaction Event Listener User wants to listen for a commit event to ensure a commercial paper was issued const transaction = contract.createTransaction(‘issue’); await transaction.addCommitListener((err, txId, status, blockHeight) => { if (err) { console.error(err); return; } if (status === ‘VALID’) { console.info(‘Transaction committed’); } else { console.error(‘Transaction failed. Status: ${status}’); }); await transaction.submit('ibm', '1000000', '2019-03-31’)
Checkpointing • Checkpointing allows event listeners to track information about blocks they have seen • Useful when replaying missed events • Tracks the last block number and any transaction ID’s seen from that block preventing event duplication • Pluggable checkpointing mechanisms • File System – Default • Developer provided event callbacks are overridden to automatically checkpoint if a checkpoint is given • No extra code for developers
Pluggable selection strategies Selection strategies are pluggable Select a new or recycle an old event hub if the current one goes down • Use case dependent
Pluggable selection strategies - Factory Provide an event hub selection factory in GatewayOptions Returns an instance of BaseEventHubSelectionStrategy Selects a pool of Event Hub’s it wants the strategy to select from // Example factory included in the SDK. User may create their own function MSPID_SCOPE_ROUND_ROBIN(network) { const orgPeers = getOrganizationPeers(network); return new RoundRobinEventHubSelectionStrategy(orgPeers); }
Summary Added event listener creation to the Contract, Network and Transaction classes • Contract.addContractListener(listenerName, event, callback, options) • Network.addBlockListener(listenerName, callback, options) • Network.addCommitListener(txId, callback, options) • Transaction.addCommitListener(callback, options) Automatic event replay using a pluggable BaseCheckpointer • File System – default Automatic recovery when an Event Hub goes down • Pluggable BaseEventHubSelectionStrategy to control selection behavior