事务承诺超时错误

时间:2017-08-30 22:57:15

标签: javascript node.js hyperledger-composer

我正在尝试通过事务Peer动态创建enrollPeer(参与者)。我希望链代码通过获取num of peers并添加1来自行生成peerID。

当我让链码自己创建一个peerID时,我得到了超时。当我在事务中传递peerID(可选字段)时,它已成功添加到注册表中。我如何解决我的承诺有问题吗?

function enrollNewPeer(enroll){
    var factory = getFactory();
    var NS = 'org.acme.destro';

    if(enroll.typeOfPeer == null){
        enroll.typeOfPeer = 'Peer';
    }

    /* Make sure type exists */
    enroll.typeOfPeer = 'Peer';
    /* Make sure peerID does not exist */
    return getParticipantRegistry(NS + '.Peer')
    .then(function (peerRegistry) {            
        return peerRegistry.exists(enroll.peerID);  
    }).then(function (assetFound) {
        if (assetFound) {
            throw new Error("Peer Already Exitsts");
        }
        if(enroll.peerID == null){
            getParticipantRegistry(NS + '.Peer')
            .then(function (peerRegistry) {            
                return peerRegistry.getAll(); //returns list of Peers
            }).then(function(allPeers){ 
                var peerNum = (allPeers.length) + 1; // get #of peers and add 1
                var event = factory.newEvent(NS,'numPeers');
                event.peers = peerNum;
                emit(event);
            }).then(function() {
                enroll.peerID = 'peer' + peerNum;
            });
        }
    }).then(function() {
        var peer = factory.newResource(NS, enroll.typeOfPeer, enroll.peerID);
        peer.peerPubKey = assignKey(peer.peerID);

        var event = factory.newEvent(NS,'newPeer');
        event.peer = peer;
        emit(event);

        return getParticipantRegistry(NS + '.' + enroll.typeOfPeer)
        .then(function(participantRegistry) {
            return participantRegistry.add(peer);
        });
    });
}

1 个答案:

答案 0 :(得分:0)

当您在真正的分布式网络中运行多个对等体时,获取对等体数量然后添加1的逻辑可能会失败。每个Fabric对等体都将查询对等体的数量,并且可能存在竞争条件,其中不同的对等体获得不同的值 - 导致事务由于共识失败而被回滚。您应该分配"新的对等ID"在客户端上并将其传递给事务 - 这将确保所有Fabric对等体使用相同的id。