src/fabric/e2eUtils.js
/**
 * Deploy the given chaincode to the given organization's peers.
 * @param {string} org The name of the organization.
 * @param {object} chaincode The chaincode object from the configuration file.
 * @return {Promise} The return promise.
 */
Deploy the given chaincode to the given organization's peers.
@param {string} org The name of the organization.
@param {object} chaincode The chaincode object from the configuration file.
@return {Promise} The return promise.
Deploy the given chaincode to the given organization ' s peers . @param { string } org The name of the organization . @param { object } chaincode The chaincode object from the configuration file . @return { Promise } The return promise .
function installChaincode(org, chaincode) {
    Client.setConfigSetting('request-timeout', 60000);
    const channel_name = chaincode.channel;
    const client = new Client();
    const channel = client.newChannel(channel_name);
    const orgName = ORGS[org].name;
    const cryptoSuite = Client.newCryptoSuite();
    cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: testUtil.storePathForOrg(orgName)}));
    client.setCryptoSuite(cryptoSuite);
    const caRootsPath = ORGS.orderers[0].tls_cacerts;
    let data = fs.readFileSync(commUtils.resolvePath(caRootsPath));
    let caroots = Buffer.from(data).toString();
    channel.addOrderer(
        client.newOrderer(
            ORGS.orderers[0].url,
            {
                'pem': caroots,
                'ssl-target-name-override': ORGS.orderers[0]['server-hostname']
            }
        )
    );
    const targets = [];
    for (let key in ORGS[org]) {
        if (ORGS[org].hasOwnProperty(key)) {
            if (key.indexOf('peer') === 0) {
                let data = fs.readFileSync(commUtils.resolvePath(ORGS[org][key].tls_cacerts));
                let peer = client.newPeer(
                    ORGS[org][key].requests,
                    {
                        pem: Buffer.from(data).toString(),
                        'ssl-target-name-override': ORGS[org][key]['server-hostname']
                    }
                );
                targets.push(peer);
                channel.addPeer(peer);
            }
        }
    }
    return Client.newDefaultKeyValueStore({
        path: testUtil.storePathForOrg(orgName)
    }).then((store) => {
        client.setStateStore(store);
        return testUtil.getSubmitter(client, true , org);
    }).then((admin) => {
        the_user = admin;
        let resolvedPath = chaincode.path;
        let metadataPath = chaincode.metadataPath ? commUtils.resolvePath(chaincode.metadataPath) : chaincode.metadataPath;
        if (chaincode.language === 'node') {
            resolvedPath = commUtils.resolvePath(chaincode.path);
        }
        const request = {
            targets: targets,
            chaincodePath: resolvedPath,
            metadataPath: metadataPath,
            chaincodeId: chaincode.id,
            chaincodeType: chaincode.language,
            chaincodeVersion: chaincode.version
        };
        return client.installChaincode(request);
    },
    (err) => {
        throw new Error('Failed to enroll user \'admin\'. ' + err);
    }).then((results) => {
        const proposalResponses = results[0];
        let all_good = true;
        const errors = [];
        for(let i in proposalResponses) {
            let one_good = false;
            if (proposalResponses && proposalResponses[i].response && proposalResponses[i].response.status === 200) {
                one_good = true;
            } else {
                logger.error('install proposal was bad');
                errors.push(proposalResponses[i]);
            }
            all_good = all_good && one_good;
        }
        if (!all_good) {
            throw new Error(util.format('Failed to send install Proposal or receive valid response: %s', errors));
        }
    },
    (err) => {
        throw new Error('Failed to send install proposal due to error: ' + (err.stack ? err.stack : err));
    })
        .catch((err) => {
            return Promise.reject(err);
        });
}
function installChaincode ( org , chaincode ) { Client . setConfigSetting ( 'request-timeout' , 60000 ) ; const channel_name = chaincode . channel ; const client = new Client ( ) ; const channel = client . newChannel ( channel_name ) ; const orgName = ORGS [ org ] . name ; const cryptoSuite = Client . newCryptoSuite ( ) ; cryptoSuite . setCryptoKeyStore ( Client . newCryptoKeyStore ( { path : testUtil . storePathForOrg ( orgName ) } ) ) ; client . setCryptoSuite ( cryptoSuite ) ; const caRootsPath = ORGS . orderers [ 0 ] . tls_cacerts ; let data = fs . readFileSync ( commUtils . resolvePath ( caRootsPath ) ) ; let caroots = Buffer . from ( data ) . toString ( ) ; channel . addOrderer ( client . newOrderer ( ORGS . orderers [ 0 ] . url , { 'pem' : caroots , 'ssl-target-name-override' : ORGS . orderers [ 0 ] [ 'server-hostname' ] } ) ) ; const targets = [ ] ; for ( let key in ORGS [ org ] ) { if ( ORGS [ org ] . hasOwnProperty ( key ) ) { if ( key . indexOf ( 'peer' ) === 0 ) { let data = fs . readFileSync ( commUtils . resolvePath ( ORGS [ org ] [ key ] . tls_cacerts ) ) ; let peer = client . newPeer ( ORGS [ org ] [ key ] . requests , { pem : Buffer . from ( data ) . toString ( ) , 'ssl-target-name-override' : ORGS [ org ] [ key ] [ 'server-hostname' ] } ) ; targets . push ( peer ) ; channel . addPeer ( peer ) ; } } } return Client . newDefaultKeyValueStore ( { path : testUtil . storePathForOrg ( orgName ) } ) . then ( ( store ) => { client . setStateStore ( store ) ; return testUtil . getSubmitter ( client , true , org ) ; } ) . then ( ( admin ) => { the_user = admin ; let resolvedPath = chaincode . path ; let metadataPath = chaincode . metadataPath ? commUtils . resolvePath ( chaincode . metadataPath ) : chaincode . metadataPath ; if ( chaincode . language === 'node' ) { resolvedPath = commUtils . resolvePath ( chaincode . path ) ; } const request = { targets : targets , chaincodePath : resolvedPath , metadataPath : metadataPath , chaincodeId : chaincode . id , chaincodeType : chaincode . language , chaincodeVersion : chaincode . version } ; return client . installChaincode ( request ) ; } , ( err ) => { throw new Error ( 'Failed to enroll user \'admin\'. ' + err ) ; } ) . then ( ( results ) => { const proposalResponses = results [ 0 ] ; let all_good = true ; const errors = [ ] ; for ( let i in proposalResponses ) { let one_good = false ; if ( proposalResponses && proposalResponses [ i ] . response && proposalResponses [ i ] . response . status === 200 ) { one_good = true ; } else { logger . error ( 'install proposal was bad' ) ; errors . push ( proposalResponses [ i ] ) ; } all_good = all_good && one_good ; } if ( ! all_good ) { throw new Error ( util . format ( 'Failed to send install Proposal or receive valid response: %s' , errors ) ) ; } } , ( err ) => { throw new Error ( 'Failed to send install proposal due to error: ' + ( err . stack ? err . stack : err ) ) ; } ) . catch ( ( err ) => { return Promise . reject ( err ) ; } ) ; }
Deploy the given chaincode to the given organization's peers.
Deploy the given chaincode to the given organization ' s peers .
// get the peer org's admin required to send install chaincode requests /* get peer org admin */ // send proposal to endorser