nodejs test hyperledger composer v0.15失败,错误:找不到卡:PeerAdmin @ hlfv1

时间:2017-11-20 13:24:14

标签: node.js sdk hyperledger-composer

随着v0.15中的卡的实现,使用配置文件(显然)的先前版本的测试例程将不起作用。但是,我找不到SDK方法来创建运行测试所需的卡。对于我的测试,通过v0.14的代码的“之前”部分如下所示,它使用嵌入的配置文件,创建网络的临时实例并运行测试:

describe('Finance Network', () => {

    // let adminConnection;
    let businessNetworkConnection;

    before(() => {
        BrowserFS.initialize(new BrowserFS.FileSystem.InMemory());
        const adminConnection = new AdminConnection({ fs: bfs_fs });
        return adminConnection.createProfile('defaultProfile', {
            type: 'embedded'
        })
            .then(() => {
                return adminConnection.connect('defaultProfile', adminID, adminPW);
            })
            .then(() => {
                return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));
            })
            .then((businessNetworkDefinition) => {
                return adminConnection.deploy(businessNetworkDefinition);
            })
            .then(() => {
                businessNetworkConnection = new BusinessNetworkConnection({ fs: bfs_fs });
                return businessNetworkConnection.connect('defaultProfile', network, adminID, adminPW);
            });
    });

我想在nodejs文档中找到(现在还没有)的是如何创建必要的卡片以使其工作,同时使用相同的方法。

我希望用创建必要卡片的代码替换以下代码行:

        return adminConnection.createProfile('defaultProfile', {
            type: 'embedded'
        })
            .then(() => {
                return adminConnection.connect('defaultProfile', adminID, adminPW);
            })

我看到卡片创建的唯一地方是作曲家 - 客户参与者注册表,但这是一个Catch-22情况,我必须连接才能创建卡片,但我需要一张卡才能连接。编写基于mocha的测试的推荐方法是什么,就像我们过去发布的Hyperledger-Composer的无数版本一样?

谢谢!

2 个答案:

答案 0 :(得分:0)

我想在建立基于卡片的API时需要进行一些改进,但你应该只使用API​​来查看类似的内容:

// Embedded connection used for local testing
const connectionProfile = {
    name: 'embedded',
    type: 'embedded'
};
// Embedded connection does not need real credentials
const credentials = {
    certificate: 'FAKE CERTIFICATE',
    privateKey: 'FAKE PRIVATE KEY'
};

// PeerAdmin identity used with the admin connection to deploy business networks
const deployerMetadata = {
    version: 1,
    userName: 'PeerAdmin',
    roles: [ 'PeerAdmin', 'ChannelAdmin' ]
};
const deployerCard = new IdCard(deployerMetadata, connectionProfile);
deployerCard.setCredentials(credentials);

// In-memory card store for testing so cards are not persisted to the file system
const cardStore = new MemoryCardStore();
const adminConnection = new AdminConnection({ cardStore: cardStore });

const deployerCardName = 'PeerAdmin';
const adminUserName = 'admin';
let adminCardName;
let businessNetworkDefinition;

return adminConnection.importCard(deployerCardName, deployerCard).then(() => {
    return adminConnection.connect(deployerCardName);
}).then(() => {
    return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));
}).then(definition => {
    businessNetworkDefinition = definition;
    // Install the Composer runtime for the new business network
    return adminConnection.install(businessNetworkDefinition.getName());
}).then(() => {
    // Start the business network and configure an network admin identity
    const startOptions = {
        networkAdmins: [
            {
                userName: adminUserName,
                enrollmentSecret: adminSecret
            }
        ]
    };
    return adminConnection.start(businessNetworkDefinition, startOptions);
}).then(adminCards => {
    // Import the network admin identity for us to use
    adminCardName = 'admin@' + businessNetworkDefinition.getName();
    return adminConnection.importCard(adminCardName, adminCards.get(adminUserName));
}).then(() => {
    // Connect to the business network using the network admin identity
    businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });
    return businessNetworkConnection.connect(adminCardName);
});

需要注意的是,在此问题中添加的过程中,正在调用AdminConnection.start()时返回的网络管理卡是(我输入):hyperledger/composer#2753

CLI命令已经注册了网络管理员身份并为此管理员输出了一张卡片,如果您想使用它而不是将其用于其他人,则可以导入该卡片。

答案 1 :(得分:0)

经过几天的实验,我找到了解决这个问题的方法。以下代码序列使用作为v0.15发行版的一部分创建的PeerAdmin卡。我导入该卡,在adminConnect之后,更新卡的元数据中的businessNetwork元素,然后在重新导入卡后,部署并连接到业务网络。

describe('Finance Network', function () {
    this.timeout(_timeout);
    let businessNetworkConnection;
    let peerName = 'PeerAdmin@hlfv1';
    before(function () {
        const adminConnection = new AdminConnection();
        let idPeer;
        return hlc_idCard.fromDirectory(_home+'/.composer/cards/'+peerName)
        .then ((_idPeer) => {
            idPeer = _idPeer;
            return adminConnection.importCard(peerName, idPeer);
        })
        .then(() => {
            return adminConnection.connect(peerName);
        })
        .then(() => {
            return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));
        })
        .then((businessNetworkDefinition) => {
            idPeer.metadata.businessNetwork = network;
            return adminConnection.importCard(peerName, idPeer)
            .then(() => {
                return adminConnection.deploy(businessNetworkDefinition,{'card': idPeer});
            });
        })
        .then(() => {
            businessNetworkConnection = new BusinessNetworkConnection();
            return businessNetworkConnection.connect(peerName);
        });
    });