Hyperledger Fabric SDK 示例fabric-samples-《balance-transfer》之十一《查询已经安装/实例化的chaincode》

本文已在我的公众号Fabric技术分享原创首发。
转载请标明出处:
https://blog.csdn.net/qq_27818541/article/details/78579641
本文出自:【BigManing的博客】

前言

查询节点上的chaincode信息。需要如下参数:

  • peer节点

这里展示下org1下的peer1节点已经安装的chaincode信息。

路由

app.js

//查询chaincode信息 Query to fetch all Installed/instantiated chaincodes
app.get('/chaincodes', function(req, res) {
    var peer = req.query.peer;
    var installType = req.query.type;
    // 这个api是根据installType(installed / instantiated)来区分查找的是哪类chaincode
    if (installType === 'installed') {
        logger.debug(
            '================ GET INSTALLED CHAINCODES ======================');
    } else {
        logger.debug(
            '================ GET INSTANTIATED CHAINCODES ======================');
    }
    //具体实现 
    query.getInstalledChaincodes(peer, installType, req.username, req.orgname)
    .then(function(message) {
        res.send(message);
    });
});

具体实现

query.js

var getInstalledChaincodes = function(peer, type, username, org) {
    // peer地址
    var target = buildTarget(peer, org);
    // channel实例
    var channel = helper.getChannelForOrg(org);
    //client实例
    var client = helper.getClientForOrg(org);

    return helper.getOrgAdmin(org).then((member) => {
        //关键是API的调用
        if (type === 'installed') {
            return client.queryInstalledChaincodes(target);
        } else {
            return channel.queryInstantiatedChaincodes(target);
        }
    }, (err) => {
        logger.info('Failed to get submitter "' + username + '"');
        return 'Failed to get submitter "' + username + '". Error: ' + err.stack ?
            err.stack : err;
    }).then((response) => {
        if (response) {
            // 根据类型 区分打印结果
            if (type === 'installed') {
                logger.debug('<<< Installed Chaincodes >>>');
            } else {
                logger.debug('<<< Instantiated Chaincodes >>>');
            }
            var details = [];
            for (let i = 0; i < response.chaincodes.length; i++) {
                logger.debug('name: ' + response.chaincodes[i].name + ', version: ' +
                    response.chaincodes[i].version + ', path: ' + response.chaincodes[i].path
                );
                details.push('name: ' + response.chaincodes[i].name + ', version: ' +
                    response.chaincodes[i].version + ', path: ' + response.chaincodes[i].path
                );
            }
            return details;
        } else {
            logger.error('response is null');
            return 'response is null';
        }
    }, (err) => {
        logger.error('Failed to send query due to error: ' + err.stack ? err.stack :
            err);
        return 'Failed to send query due to error: ' + err.stack ? err.stack : err;
    }).catch((err) => {
        logger.error('Failed to query with error:' + err.stack ? err.stack : err);
        return 'Failed to query with error:' + err.stack ? err.stack : err;
    });
};

基本流程

Created with Raphaël 2.1.2 Start 获取到admin 查询已经安装的chaincode client.queryInstalledChaincodes 处理结果 End channel.queryInstantiatedChaincodes yes no

API访问

echo "GET query Installed chaincodes"
echo
curl -s -X GET \
  "http://localhost:4000/chaincodes?peer=peer1&type=installed" \
  -H "authorization: Bearer $ORG1_TOKEN" \
  -H "content-type: application/json"
echo
echo

控制台打印:

GET query Installed chaincodes

["name: mycc, version: v0, path: github.com/example_cc"]

后台打印:

[2017-10-16 11:07:43.140] [DEBUG] SampleWebApp - Decoded from JWT token: username - Jim, orgname - org1
[2017-10-16 11:07:43.140] [DEBUG] SampleWebApp - ================ GET TRANSACTION BY TRANSACTION_ID ======================
[2017-10-16 11:07:43.141] [DEBUG] SampleWebApp - channelName : mychannel
[2017-10-16 11:07:43.142] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:43.144] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:43.145] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:43.146] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:43.146] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:43.146] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.146] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.146] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.147] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:43.147] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:43.147] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.147] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.148] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.148] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.148] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.148] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:43.148] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.148] [INFO] Helper - Successfully loaded member from persistence
[2017-10-16 11:07:43.155] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature:  Signature {
  r: 6d0ae79a06d8b5d9d593d8a22f9667e41ef41d7005f635e071d61730e4519868>,
  s: 2aa650cffd0dcf227f76add8a490a94eae4717a572576e2f56105d58cb21d292>,
  recoveryParam: 1 }
[2017-10-16 11:07:43.167] [DEBUG] Query - { validationCode: 0,
  transactionEnvelope: 
   { signature: 30 45 02 21 00 b1 1a 25 98 70 e0 bb f0 72 58 4f 35 3a b1 51 5f 47 59 c8 54 eb 47 5b ed cc ba 38 b5 c3 62 a4 d7 02 20 66 79 6e 05 ea e9 01 5e 63 ee a2 ... >,
     payload: { header: [Object], data: [Object] } } }[2017-10-16 11:07:43.180] [DEBUG] SampleWebApp - Decoded from JWT token: username - Jim, orgname - org1
[2017-10-16 11:07:43.180] [DEBUG] SampleWebApp - ================ GET CHANNEL INFORMATION ======================
[2017-10-16 11:07:43.180] [DEBUG] SampleWebApp - channelName : mychannel
[2017-10-16 11:07:43.181] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:43.182] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:43.184] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:43.185] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:43.185] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:43.186] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:43.186] [DEBUG] Helper [2017-10-16 11:07:43.219] [DEBUG] SampleWebApp - Decoded from JWT token: username - Jim, orgname - org1
[2017-10-16 11:07:43.220] [DEBUG] SampleWebApp - ================ GET INSTALLED CHAINCODES ======================
[2017-10-16 11:07:43.222] [DEBUG] Helper - [crypto_ecdsa_aes]: constructor, keySize: 256
[2017-10-16 11:07:43.222] [DEBUG] Helper - [crypto_ecdsa_aes]: Hash algorithm: SHA2, hash output size: 256
[2017-10-16 11:07:43.223] [DEBUG] Helper - [utils.CryptoKeyStore]: CryptoKeyStore, constructor - start
[2017-10-16 11:07:43.224] [DEBUG] Helper - [utils.CryptoKeyStore]: constructor, no super class specified, using config: fabric-client/lib/impl/FileKeyValueStore.js
[2017-10-16 11:07:43.224] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:43.225] [DEBUG] Helper - Msp ID : Org1MSP
[2017-10-16 11:07:43.225] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:43.225] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:43.226] [DEBUG] Helper - [utils.CryptoKeyStore]: This class requires a CryptoKeyStore to save keys, using the store: {"opts":{"path":"/tmp/fabric-client-kvs_peerOrg1"}}
[2017-10-16 11:07:43.226] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:43.226] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore returning ks
[2017-10-16 11:07:43.226] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c
[2017-10-16 11:07:43.227] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46
[2017-10-16 11:07:43.227] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.227] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:43.228] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:43.228] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:43.228] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c
[2017-10-16 11:07:43.228] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46
[2017-10-16 11:07:43.229] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.229] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c
[2017-10-16 11:07:43.229] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46
[2017-10-16 11:07:43.229] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c
[2017-10-16 11:07:43.229] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46
[2017-10-16 11:07:43.230] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c
[2017-10-16 11:07:43.230] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46
[2017-10-16 11:07:43.230] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:43.235] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature:  Signature {
  r: 7998f934a2a39ba42f71db18fad6e5f36e3f537e218a777fff0c95944cbea02a>,
  s: 21f6d2e03770cb886555b549ac0baf658c0858f79512a38d1811a1ad3c60b6b0>,
  recoveryParam: 0 }
[2017-10-16 11:07:43.255] [DEBUG] Query - <<< Installed Chaincodes >>>
[2017-10-16 11:07:43.255] [DEBUG] Query - name: mycc, version: v0, path: github.com/example_cc

你可能感兴趣的:(Hyperleder,Fabric,Node,SDK,1.0,Hyperldger,Fabric,SDK)