Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const AbstractEventListener = require('./abstracteventlistener');
const logger = require('fabric-network/lib/logger').getLogger('CommitEventListener');
const util = require('util');
/**
* The Commit Event Listener handles transaction commit events
*
* @memberof module:fabric-network
* @class
*/
class CommitEventListener extends AbstractEventListener {
/**
*
* @param {module:fabric-network.Network} network The fabric network
* @param {string} transactionId the transaction id being listened to
* @param {Function} eventCallback The event callback called when a transaction is committed.
* It has signature (err, transactionId, status, blockNumber)
* @param {module:fabric-network.Network~ListenerOptions} options
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const TimeoutError = require('fabric-network/lib/errors/timeouterror');
const logger = require('fabric-network/lib/logger').getLogger('TransactionEventHandler');
const util = require('util');
/**
* Handles events for a given transaction. Used to wait for a submitted transaction to be successfully commited to
* the ledger.
* Delegates to an event strategy to decide whether events or errors received should be interpreted as success or
* failure of a transaction.
* @private
*/
class TransactionEventHandler {
/**
* @typedef {Object} TransactionEventHandlerOptions
* @property {Number} [commitTimeout = 0] Number of seconds to wait for transaction completion. A value of zero
* indicates that the handler should wait indefinitely.
*/
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const EventHandlerStrategies = require('./defaulteventhandlerstrategies');
const TransactionEventHandler = require('./transactioneventhandler');
const EventHubFactory = require('./eventhubfactory');
const logger = require('fabric-network/lib/logger').getLogger('DefaultEventHandlerManager');
class DefaultEventHandlerManager {
/**
* @typedef {Object} EventHandlerOptions
* @property {Function} [strategy = EventHandlerStrategies.MSPID_SCOPE_ALLFORTX] Event strategy factory.
* @property {Number} [commitTimeout = 0] Number of seconds to wait for transaction completion. A value of zero
* indicates that the handler should wait indefinitely.
*/
/**
* Constructor.
* @param {Network} network Network on which events will be processed.
* @param {String} mspId Member Services Provider identifier.
* @param {EventHandlerOptions} options Additional options for event handling behaviour.
*/
constructor(network, mspId, options) {
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const FabricError = require('fabric-network/lib/errors/fabricerror');
const util = require('util');
const logger = require('fabric-network/lib/logger').getLogger('RoundRobinQueryHandler');
class RoundRobinQueryHandler {
constructor(peers) {
logger.debug('constructor: peers=%j', peers.map((peer) => peer.getName()));
this._peers = peers;
this._currentPeerIndex = 0;
}
async evaluate(query) {
const startPeerIndex = this._currentPeerIndex++;
const errorMessages = [];
for (let i = 0; i < this._peers.length; i++) {
const peerIndex = (startPeerIndex + i) % this._peers.length;
const peer = this._peers[peerIndex];
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const EventHubFactory = require('./eventhubfactory');
const logger = require('fabric-network/lib/logger').getLogger('EventHubManager');
/**
* The Event Hub Manager is responsible for creating and distributing event hubs.
* It uses the event hub factory to reuse event hubs that exists, and maintains
* its own list of new event hubs that are used for event replay
* @memberof module:fabric-network
* @class
*/
class EventHubManager {
/**
* Constructor
* @param {module:fabric-network.Network} network The network
*/
constructor(network) {
this.channel = network.getChannel();
this.eventHubFactory = new EventHubFactory(this.channel);
this.eventHubSelectionStrategy = network.getEventHubSelectionStrategy();
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const Query = require('fabric-network/lib/impl/query/query');
const logger = require('fabric-network/lib/logger').getLogger('Transaction');
const util = require('util');
const noOpTxEventHandler = {
startListening: async () => {},
waitForEvents: async () => {},
cancelListening: () => {}
};
/**
* Ensure supplied transaction arguments are not strings.
* @private
* @static
* @param {Array} args transaction arguments.
* @throws {Error} if any arguments are invalid.
*/
function verifyArguments(args) {
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const AbstractEventListener = require('./abstracteventlistener');
const BaseCheckpointer = require('./basecheckpointer');
const logger = require('fabric-network/lib/logger').getLogger('ContractEventListener');
const util = require('util');
/**
* The Contract Event Listener handles contract events from the chaincode.
*
* @memberof module:fabric-network
* @class
*/
class ContractEventListener extends AbstractEventListener {
/**
* Constructor.
* @param {Contract} contract The contract instance
* @param {string} listenerName a unique name identifying the listener
* @param {string} eventName The name of the contract event being listened for
* @param {function} eventCallback The event callback called when an event is received.
* It has signature (err, BlockEvent, blockNumber, transactionId)
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const FabricError = require('fabric-network/lib/errors/fabricerror');
const util = require('util');
const logger = require('fabric-network/lib/logger').getLogger('SingleQueryHandler');
class SingleQueryHandler {
constructor(peers) {
logger.debug('constructor: peers=%j', peers.map((peer) => peer.getName()));
this._peers = peers;
this._currentPeerIndex = 0;
}
async evaluate(query) {
const startPeerIndex = this._currentPeerIndex;
const errorMessages = [];
for (let i = 0; i < this._peers.length; i++) {
const peerIndex = (startPeerIndex + i) % this._peers.length;
this._currentPeerIndex = peerIndex;
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const AbstractEventStrategy = require('fabric-network/lib/impl/event/abstracteventstrategy');
const logger = require('fabric-network/lib/logger').getLogger('AnyForTxStrategy');
/**
* Event handling strategy that:
* - Waits for first successful reponse from an event hub.
* - Fails if all responses are errors.
* - Succeeds if any reponses are successful.
*
* Instances of the strategy are stateful and must only be used for a single transaction.
* @private
* @class
*/
class AnyForTxStrategy extends AbstractEventStrategy {
/**
* @inheritdoc
*/
checkCompletion(counts, successFn, failFn) {
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const logger = require('fabric-network/lib/logger').getLogger('EventHubFactory');
/**
* Factory for obtaining event hubs for peers on a given channel.
* Where possible, ensures that event hubs are connected.
* @private
* @class
*/
class EventHubFactory {
/**
* Constructor.
* @param {Channel} channel Channel used to create event hubs.
*/
constructor(channel) {
if (!channel) {
const message = 'Channel not set';
logger.error('constructor:', message);