hyperledger composer部署示例代码失败

时间:2017-09-01 14:49:42

标签: hyperledger-composer

目前正在使用hyperledger开发环境的最新版本,并正在使用管理服务。 admin的示例代码 - >部署记录为:

// Deploy a Business Network Definition
var adminConnection = new AdminConnection();
var businessNetworkDefinition = BusinessNetworkDefinition.fromArchive(myArchive);
return adminConnection.deploy(businessNetworkDefinition)
.then(function(){
    // Business network definition deployed
})
.catch(function(error){
    // Add optional error handling here.
});

在提供的代码中,第二行失败,因为BusinessNetworkDefinition不是composer-admin节点模块的一部分。我有两个选项来创建BusinessNetworkDefinition,一个是使用composer-client。此操作失败,并显示以下消息:TypeError: composerClient.BusinessNetworkDefinition is not a constructor 用于此尝试的代码总结如下:

'use strict';
var fs = require('fs');
var path = require('path');
var composer = require('composer-admin');
var composerClient = require('composer-client');
var composerCommon = require('composer-common');

    var businessNetworkDefinition = new composerClient.BusinessNetworkDefinition();

另一个选项是使用composer-common,它失败并显示以下消息:TypeError: businessNetworkDefinition.fromArchive is not a function 用于此尝试的代码是:

var fs = require('fs');
var path = require('path');
var composer = require('composer-admin');
var composerClient = require('composer-client');
var composerCommon = require('composer-common');

    var net_identifier = "zerotoblockchain-network@0.1.6";
    var net_description = "Z2B network";
    var net_package = require("../../../../network/package.json");
    var net_readme = "../../../../README.md";

    var businessNetworkDefinition = new composerCommon.BusinessNetworkDefinition(net_identifier, net_description, net_package, net_readme);
    var archive = businessNetworkDefinition.fromArchive(req.body.myArchive);

其中req.body.myArchive是要在fromArchive方法中使用的归档文件的名称。检查通过new命令创建的BusinessNetworkDefinition显示以下内容:

object property: modelManager
object property: aclManager
object property: queryManager
object property: scriptManager
object property: introspector
object property: factory
object property: serializer
object property: metadata

所以,有两个问题:

  • 一:使用new命令和
  • 创建的内容
  • 二:如何正确创建BusinessNetworkDefinition作为fromArchive()函数的对象?

1 个答案:

答案 0 :(得分:0)

hyperledger composer文档中的示例代码存在缺陷。以下是将成功执行业务网络部署的代码。 (1)必需的定义:

let fs = require('fs');
let path = require('path');
let composerAdmin = require('composer-admin');
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;

以下内容在nodejs中编写为可导出,可路由的例程,并已通过客户端(浏览器)进行测试。客户端以要部署的网络名称传递。文件布局是:

root/network/dist/network-archive-file.bna

(2)读入存档文件并创建管理连接:

/**
 * Deploys a new BusinessNetworkDefinition to the Hyperledger Fabric. The connection must be connected for this method to succeed.
 * @param {express.req} req - the inbound request object from the client
 *  req.body.myArchive: _string - string name of object
 *  req.body.deployOptions: _object - string name of object
 * @param {express.res} res - the outbound response object for communicating back to client
 * @param {express.next} next - an express service to enable post processing prior to responding to the client
 * returns composerAdmin.connection - either an error or a connection object
 * @function
 */
exports.deploy = function(req, res, next) {

    let newFile = path.join(path.dirname(require.main.filename),'network/dist',req.body.myArchive);
    let archiveFile = fs.readFileSync(newFile);

    let adminConnection = new composerAdmin.AdminConnection();

(3)调用(异步)fromArchive函数,然后部署fromArchive调用的结果。

return BusinessNetworkDefinition.fromArchive(archiveFile)
    .then(function(archive) {
        adminConnection.connect(config.composer.connectionProfile, config.composer.adminID, config.composer.adminPW)
        .then(function(){
            adminConnection.deploy(archive)

(4)。回复(基于浏览器)请求:

           .then(function(){
                console.log('business network deploy successful: ');
                res.send({deploy: 'succeeded'});
            })
            .catch(function(error){
                console.log('business network deploy failed: ',error);
                res.send({deploy: error});
                });

...(5)并且对于任何复制所有代码的人来说,有几个结束括号:

        });
    });
};