1 / 10

Node SDK: Fabric Network Event Handling FABN-1100

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)

dooleym
Download Presentation

Node SDK: Fabric Network Event Handling FABN-1100

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Node SDK: Fabric Network Event Handling FABN-1100 Liam Grace @14gracel (Rocket.Chat)

  2. 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

  3. 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)

  4. 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});

  5. 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}) });

  6. 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’)

  7. 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

  8. 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

  9. 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); }

  10. 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

More Related