首页 > 解决方案 > 如何解决fabric-sdk-java中的“实例化链码”错误?

问题描述

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

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

创建频道:</p>

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

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

安装链码:

每个组织都必须使用自己的组织发送一次安装建议peerAdmin

参考:<a href="https://github.com/IBM/blockchain-application-using-fabric-java-sdk" rel="nofollow noreferrer">https://github.com/IBM/blockchain-application-使用-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

这些是相关代码:</p>

 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,但没有发现问题。

标签: javahyperledger-fabric

解决方案


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

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;
    }

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

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

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


推荐阅读