gpt4 book ai didi

java - 如何解决fabric-sdk-java中的 “instantiate chaincode”错误?

转载 作者:行者123 更新时间:2023-12-02 06:18:32 24 4
gpt4 key购买 nike

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

我成功创建了 channel 并安装了链代码。

创建 channel :

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类都继承了Serialized接口(interface),并且都定义了serialVersionUID。

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

最佳答案

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

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的第四个变量参数,请告诉我。谢谢。

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

关于java - 如何解决fabric-sdk-java中的 “instantiate chaincode”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com