- 在学习Fabric -sdk -java 时,使用 ChaincodeInstantiated() 方法报错
public void ChaincodeInstantiated() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, org.bouncycastle.crypto.CryptoException, InvalidArgumentException, CryptoException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException, TransactionException, ProposalException {
UserContext userContext = new UserContext();
userContext.setAffiliation("Org1");
userContext.setMspId("Org1MSP");
userContext.setAccount("李伟");
userContext.setName("admin");
Enrollment enrollment = UserUtils.getEnrollment(keyFolderPath,keyFileName,certFoldePath,certFileName);
userContext.setEnrollment(enrollment);
FabricClient fabricClient = new FabricClient(userContext);
Peer peer = fabricClient.getPeer("peer0.org1.nwpu.com","grpcs://peer0.org1.nwpu.com:7051",tlsPeerFilePath);
Orderer order = fabricClient.getOrderer("orderer.nwpu.com","grpcs://orderer.nwpu.com:7050",tlsOrderFilePath);
String initArgs[] = {"a","100","b","100"};
fabricClient.initChaincode("agiot-channel", TransactionRequest.Type.GO_LANG,"testcc","2.0",order,peer,"init",initArgs);
}
报错看区块链节点的输出日志如图下所示:
Failed to generate platform-specific docker build: Error returned from build: 1 "can’t load package: package mainresourceschaincode: cannot find package “mainresourceschaincode” in any of:
peer0.org1.nwpu.com | /opt/go/src/mainresourceschaincode (from $GOROOT)
peer0.org1.nwpu.com | /chaincode/input/src/mainresourceschaincode (from $GOPATH)
peer0.org1.nwpu.com | /opt/gopath/src/mainresourceschaincode
选择重新安装, 将链码的路径由.
fabricClient.installChaincode(TransactionRequest.Type.GO_LANG,"testcc","2.0","E:\\Java\\fabric\\","main\\resources\\chaincode",peers);
路径改为
fabricClient.installChaincode(TransactionRequest.Type.GO_LANG,"testcc","2.0","E:/Java/fabric/","main/resources/chaincode",peers);
链码为test.go 在项目中的路径为结构如:
安装链码:
@Test
public void installChaincodeToOrg1() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, org.bouncycastle.crypto.CryptoException, InvalidArgumentException, CryptoException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException, ProposalException {
UserContext userContext = new UserContext();
userContext.setAffiliation("Org1");
userContext.setMspId("Org1MSP");
userContext.setAccount("李伟");
userContext.setName("admin");
Enrollment enrollment = UserUtils.getEnrollment(keyFolderPath,keyFileName,certFoldePath,certFileName);
userContext.setEnrollment(enrollment);
FabricClient fabricClient = new FabricClient(userContext);
Peer peer0 = fabricClient.getPeer("peer0.org1.nwpu.com","grpcs://peer0.org1.nwpu.com:7051",tlsPeerFilePath);
Peer peer1 = fabricClient.getPeer("peer1.org1.nwpu.com","grpcs://peer1.org1.nwpu.com:8051",tlsPeerFilePath);
List<Peer> peers = new ArrayList<Peer>();
peers.add(peer0);
peers.add(peer1);
fabricClient.installChaincode(TransactionRequest.Type.GO_LANG,"testcc","2.0","E:/Java/fabric/","main/resources/chaincode",peers);
}
@Test
public void installChaincodeToOrg2() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, org.bouncycastle.crypto.CryptoException, InvalidArgumentException, CryptoException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException, ProposalException {
List list = new ArrayList();
UserContext userContext = new UserContext();
userContext.setAffiliation("Org2");
userContext.setMspId("Org2MSP");
userContext.setAccount("赵而");
userContext.setName("admin");
Enrollment enrollment = UserUtils.getEnrollment(keyFolderPath2,keyFileName2,certFoldePath2,certFileName2);
userContext.setEnrollment(enrollment);
FabricClient fabricClient = new FabricClient(userContext);
Peer peer0 = fabricClient.getPeer("peer0.org2.nwpu.com","grpcs://peer0.org2.nwpu.com:9051",tlsPeerFilePath2);
Peer peer1 = fabricClient.getPeer("peer1.org2.nwpu.com","grpcs://peer1.org2.nwpu.com:10051",tlsPeerFilePath2);
List<Peer> peers = new ArrayList<Peer>();
peers.add(peer0);
peers.add(peer1);
//链码的路径一定要用 / 的方式来写,不能使用 \\ ,否则在实例化链码的时候会 Failed to generate platform-specific docker build: Error returned from build: 1 "can't load package: package mainresourceschaincode: cannot find package "mainresourceschaincode" in any of: /opt/go/src/mainresourceschaincode
fabricClient.installChaincode(TransactionRequest.Type.GO_LANG,"testcc","2.0","E:/Java/fabric/","main/resources/chaincode",peers);
}