如何解决fabric-sdk-java中的“实例化链代码”错误?

问题描述 投票:0回答:1

我使用fabrc-sdk-java来操作e2e_cli网络.e2e使用CA并禁用TLS。

我成功创建了频道并安装了链码。

创建频道:

Channel newChannel = client.newChannel(myChannel.getChannelName(), orderer, channelConfiguration, channelConfigurationSignatures.toArray(new byte[myPeerOrgs.size()][]));

channelConfigurationSignatures包含来自两个组织的签名。

安装链码:

每个组织都必须使用自己的peerAdmin组织发送一次安装提案。

参考:https://github.com/IBM/blockchain-application-using-fabric-java-sdk

但是,当我准备实例化链代码时,我得到错误:

0endorser failed with Sending proposal to peer0.org1.example.com failed because of: gRPC failure=Status{code=UNKNOWN, description=Failed to deserialize creator identity, err MSP Org1 is unknown, cause=null}. Was verified:false

这些是相关代码:

 client.setUserContext(myPeerOrgs.get(0).getPeerAdmin());

        InstantiateProposalRequest instantiateProposalRequest = client.newInstantiationProposalRequest();
    instantiateProposalRequest.setProposalWaitTime(fabricConfig.getProposalWaitTime());
        instantiateProposalRequest.setChaincodeID(chaincodeID);

        instantiateProposalRequest.setFcn(ininFun);
        instantiateProposalRequest.setArgs(args);

        Map<String, byte[]> tm = new HashMap<>();
        tm.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
        tm.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
        instantiateProposalRequest.setTransientMap(tm);

        ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
        chaincodeEndorsementPolicy.fromYamlFile(new File(myChaincode.getChaincodeEndorsementPolicyPath()));
        instantiateProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);

        logger.trace("Sending instantiateProposalRequest to all peers with arguments: " + Arrays.toString(args));

        Collection<ProposalResponse> successful = new LinkedList<>();
        Collection<ProposalResponse> failed = new LinkedList<>();

        Collection<ProposalResponse> responses = channel.sendInstantiationProposal(instantiateProposalRequest);

        for (ProposalResponse response : responses) {
            if (response.isVerified() && response.getStatus() == ProposalResponse.Status.SUCCESS) {
                successful.add(response);
                logger.trace(String.format("Succesful instantiate proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName()));
            } else {
                failed.add(response);
            }
        }
        logger.trace(String.format("Received %d instantiate proposal responses. Successful+verified: %d . Failed: %d", responses.size(), successful.size(), failed.size()));
        if (failed.size() > 0) {
            ProposalResponse first = failed.iterator().next();
            logger.error("Not enough endorsers for instantiate :" + successful.size() + "endorser failed with " + first.getMessage() + ". Was verified:" + first.isVerified());
            System.exit(1);
        }

我认为这是一个序列化问题,但MyUser类和MyEnrollement类都继承了Serializable接口,并且都定义了serialVersionUID。

我比较了blockchain-application-using-fabric-java-sdk并没有发现问题。

java hyperledger-fabric
1个回答
0
投票

我终于解决了这个问题。问题在于以下代码:

Channel newChannel = client.newChannel(myChannel.getChannelName(), orderer, channelConfiguration, channelConfigurationSignatures.toArray(new byte[myPeerOrgs.size()][]));

以上代码由我参考End2endIT撰写:

//Create channel that has only one signer that is this orgs peer admin. If channel creation policy needed more signature they would need to be added too.
Channel newChannel = client.newChannel(name, anOrderer, channelConfiguration, client.getChannelConfigurationSignature(channelConfiguration, sampleOrg.getPeerAdmin()));

我不知道我的用法是否有问题。但是我的代码,错误就在这句话中,当稍后加入节点时,会报告错误。

我引用了https://github.com/IBM/blockchain-application-using-fabric-java-sdk/blob/master/java/src/main/java/org/app/network/CreateChannel.java并找到了正确的写作方式。

public Channel createChannel() {

        logger.info("Begin  create channel: " + myChannel.getChannelName());

        ChannelConfiguration channelConfiguration = new ChannelConfiguration(new File(fabricConfig.getChannelArtifactsPath() + "/" + myChannel.getChannelName() + ".tx"));
        logger.trace("Read channel " + myChannel.getChannelName() + " configuration file:" + fabricConfig.getChannelArtifactsPath() + "/" + myChannel.getChannelName() + ".tx");
        byte[] channelConfigurationSignatures = client.getChannelConfigurationSignature(channelConfiguration, myPeerOrgs.get(0).getPeerAdmin());

        Channel newChannel = client.newChannel(myChannel.getChannelName(), orderer, channelConfiguration, channelConfigurationSignatures);;

        for (Peer peer : myPeerOrgs.get(0).getPeers()) {
            // create a channel for the first time, only `joinPeer` here, not `addPeer`
            newChannel.joinPeer(peer);
        }
        for (EventHub eventHub : myPeerOrgs.get(0).getEventHubs()) {
            newChannel.addEventHub(eventHub);
        }

        if (!newChannel.isInitialized()) {
            newChannel.initialize();
        }

        // I have only tested two organizations
        // I don’t know if there are any errors in the three organizations.
        for (int i = 1; i < myPeerOrgs.size(); i++) {
            client.setUserContext(myPeerOrgs.get(i).getPeerAdmin());
            newChannel = client.getChannel(myChannel.getChannelName());
            for (Peer peer : myPeerOrgs.get(i).getPeers()) {
                newChannel.joinPeer(peer);
            }
            for (EventHub eventHub : myPeerOrgs.get(i).getEventHubs()) {
                newChannel.addEventHub(eventHub);
            }
        }

        logger.trace("Node that has joined the channel:");
        Collection<Peer> peers = newChannel.getPeers();
        for (Peer peer : peers) {
            logger.trace(peer.getName() + " at " + peer.getUrl());
        }

        logger.info("Success, end create channel: " + myChannel.getChannelName() + "\n");

        return newChannel;
    }

稍后的相关代码,如安装和初始化链代码,也可以参考https://github.com/IBM/blockchain-application-using-fabric-java-sdk。这是一个很好的例子。

如果有人知道如何使用newChannel的第四个变量参数,请告诉我。谢谢。

最后,我不知道如何动态加入节点,组织和渠道,我正在寻找和测试,网络上只有nodejs的例子,没有java,如果有人知道,请告诉我,我真的需要。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.