scripts/caliper-workspace/node_modules/fabric-network/lib/transaction.js
/**
     * Submit a transaction to the ledger. The transaction function <code>name</code>
     * will be evaluated on the endorsing peers and then submitted to the ordering service
     * for committing to the ledger.
     * @async
     * @param {...string} [args] Transaction function arguments.
     * @returns {Buffer} Payload response from the transaction function.
     * @throws {module:fabric-network.TimeoutError} If the transaction was successfully submitted to the orderer but
     * timed out before a commit event was received from peers.
     */
Submit a transaction to the ledger. The transaction function name
will be evaluated on the endorsing peers and then submitted to the ordering service
for committing to the ledger.
@async
@param {...string} [args] Transaction function arguments.
@returns {Buffer} Payload response from the transaction function.
@throws {module:fabric-network.TimeoutError} If the transaction was successfully submitted to the orderer but
timed out before a commit event was received from peers.
Submit a transaction to the ledger . The transaction function name will be evaluated on the endorsing peers and then submitted to the ordering service for committing to the ledger . @async @param { ... string } [ args ] Transaction function arguments . @returns { Buffer } Payload response from the transaction function . @throws { module : fabric - network . TimeoutError } If the transaction was successfully submitted to the orderer but timed out before a commit event was received from peers .
async submit(...args) {
        const method = `submit[${this._name}]`;
        logger.debug('%s - start', method);
        const channel = this.contract.network.channel;
        const transactionOptions = this._options.eventHandlerOptions;
        const endorsementOptions = this._buildEndorsementOptions(args);
        if (Number.isInteger(transactionOptions.endorseTimeout)) {
            endorsementOptions.requestTimeout = transactionOptions.endorseTimeout * 1000; 
        }
        const endorsement = channel.newEndorsement(this.contract.chaincodeId);
        if (this._endorsingPeers) {
            logger.debug('%s - user has assigned targets', method);
            endorsementOptions.targets = this._endorsingPeers;
        }
        else if (this.contract.network.discoveryService) {
            logger.debug('%s - discovery handler will be used for endorsing', method);
            endorsementOptions.handler = await this.contract.getDiscoveryHandler(endorsement);
            if (this._endorsingOrgs) {
                logger.debug('%s - using discovery and user has assigned endorsing orgs %s', method, this._endorsingOrgs);
                endorsementOptions.requiredOrgs = this._endorsingOrgs;
            }
        }
        else if (this._endorsingOrgs) {
            logger.debug('%s - user has assigned endorsing orgs %s', method, this._endorsingOrgs);
            const flatten = (accumulator, value) => {
                accumulator.push(...value);
                return accumulator;
            };
            endorsementOptions.targets = this._endorsingOrgs.map(channel.getEndorsers).reduce(flatten, []);
        }
        else {
            logger.debug('%s - targets will default to all that are assigned to this channel', method);
            endorsementOptions.targets = channel.getEndorsers();
        }
        logger.debug('%s - build and send the endorsement', method);
        endorsement.build(this.identityContext, endorsementOptions);
        this._transactionId = endorsement.getTransactionId();
        endorsement.sign(this.identityContext);
        const proposalResponse = await endorsement.send(endorsementOptions);
        try {
            const result = getResponsePayload(proposalResponse);
            const eventHandler = this._eventHandlerStrategyFactory(endorsement.getTransactionId(), this.contract.network);
            await eventHandler.startListening();
            const commitOptions = {};
            if (Number.isInteger(transactionOptions.commitTimeout)) {
                commitOptions.requestTimeout = transactionOptions.commitTimeout * 1000; 
            }
            if (endorsementOptions.handler) {
                logger.debug('%s - use discovery to commit', method);
                commitOptions.handler = endorsementOptions.handler;
            }
            else {
                logger.debug('%s - use the orderers assigned to the channel', method);
                commitOptions.targets = channel.getCommitters();
            }
            const commit = endorsement.newCommit();
            commit.build(this.identityContext, commitOptions);
            commit.sign(this.identityContext);
            const commitResponse = await commit.send(commitOptions);
            logger.debug('%s - commit response %j', method, commitResponse);
            if (commitResponse.status !== 'SUCCESS') {
                const msg = `Failed to commit transaction %${endorsement.transactionId}, orderer response status: ${commitResponse.status}`;
                logger.error('%s - %s', method, msg);
                eventHandler.cancelListening();
                throw new Error(msg);
            }
            else {
                logger.debug('%s - successful commit', method);
            }
            logger.debug('%s - wait for the transaction to be committed on the peer', method);
            await eventHandler.waitForEvents();
            return result;
        }
        catch (err) {
            err.responses = proposalResponse.responses;
            err.errors = proposalResponse.errors;
            throw err;
        }
    }
async submit ( ... args ) { const method = ` ${ this . _name } ` ; logger . debug ( '%s - start' , method ) ; const channel = this . contract . network . channel ; const transactionOptions = this . _options . eventHandlerOptions ; const endorsementOptions = this . _buildEndorsementOptions ( args ) ; if ( Number . isInteger ( transactionOptions . endorseTimeout ) ) { endorsementOptions . requestTimeout = transactionOptions . endorseTimeout * 1000 ; } const endorsement = channel . newEndorsement ( this . contract . chaincodeId ) ; if ( this . _endorsingPeers ) { logger . debug ( '%s - user has assigned targets' , method ) ; endorsementOptions . targets = this . _endorsingPeers ; } else if ( this . contract . network . discoveryService ) { logger . debug ( '%s - discovery handler will be used for endorsing' , method ) ; endorsementOptions . handler = await this . contract . getDiscoveryHandler ( endorsement ) ; if ( this . _endorsingOrgs ) { logger . debug ( '%s - using discovery and user has assigned endorsing orgs %s' , method , this . _endorsingOrgs ) ; endorsementOptions . requiredOrgs = this . _endorsingOrgs ; } } else if ( this . _endorsingOrgs ) { logger . debug ( '%s - user has assigned endorsing orgs %s' , method , this . _endorsingOrgs ) ; const flatten = ( accumulator , value ) => { accumulator . push ( ... value ) ; return accumulator ; } ; endorsementOptions . targets = this . _endorsingOrgs . map ( channel . getEndorsers ) . reduce ( flatten , [ ] ) ; } else { logger . debug ( '%s - targets will default to all that are assigned to this channel' , method ) ; endorsementOptions . targets = channel . getEndorsers ( ) ; } logger . debug ( '%s - build and send the endorsement' , method ) ; endorsement . build ( this . identityContext , endorsementOptions ) ; this . _transactionId = endorsement . getTransactionId ( ) ; endorsement . sign ( this . identityContext ) ; const proposalResponse = await endorsement . send ( endorsementOptions ) ; try { const result = getResponsePayload ( proposalResponse ) ; const eventHandler = this . _eventHandlerStrategyFactory ( endorsement . getTransactionId ( ) , this . contract . network ) ; await eventHandler . startListening ( ) ; const commitOptions = { } ; if ( Number . isInteger ( transactionOptions . commitTimeout ) ) { commitOptions . requestTimeout = transactionOptions . commitTimeout * 1000 ; } if ( endorsementOptions . handler ) { logger . debug ( '%s - use discovery to commit' , method ) ; commitOptions . handler = endorsementOptions . handler ; } else { logger . debug ( '%s - use the orderers assigned to the channel' , method ) ; commitOptions . targets = channel . getCommitters ( ) ; } const commit = endorsement . newCommit ( ) ; commit . build ( this . identityContext , commitOptions ) ; commit . sign ( this . identityContext ) ; const commitResponse = await commit . send ( commitOptions ) ; logger . debug ( '%s - commit response %j' , method , commitResponse ) ; if ( commitResponse . status !== 'SUCCESS' ) { const msg = ` ${ endorsement . transactionId } ${ commitResponse . status } ` ; logger . error ( '%s - %s' , method , msg ) ; eventHandler . cancelListening ( ) ; throw new Error ( msg ) ; } else { logger . debug ( '%s - successful commit' , method ) ; } logger . debug ( '%s - wait for the transaction to be committed on the peer' , method ) ; await eventHandler . waitForEvents ( ) ; return result ; } catch ( err ) { err . responses = proposalResponse . responses ; err . errors = proposalResponse . errors ; throw err ; } }
Submit a transaction to the ledger.
Submit a transaction to the ledger .
// in ms; // This is the object that will centralize this endorsement activities // with the fabric network // by now we should have targets or a discovery handler to be used // by the send() of the proposal instance // build the outbound request along with getting a new transactionId // from the identity context // get the transaction ID generated by the endorsement build // ------- S E N D   P R O P O S A L // This is where the request gets sent to the peers // ------- E V E N T   M O N I T O R // in ms; // by now we should have a discovery handler or use the target orderers // that have been assigned from the channel to perform the commit // -----  C O M M I T   E N D O R S E M E N T // this is where the endorsement results are sent to the orderer