fabric-sdk-java调用链码不起作用

时间:2018-07-31 07:43:14

标签: hyperledger-fabric

我使用fabric-sdk-java查询和调用以byfn fabric开头的chaincode。当查询值时,a为90,然后调用a,b,10。那么a的值应该是80,对吧?但是在调用链码之后,我查询a的值仍然是90。所以我想问路吗?以及如何解决?

下面是我的代码:

public class FabricJavaSdkSample {

    public static void main(String[] args){

        Security.addProvider(new BouncyCastleProvider());
        final CryptoSuite crypto = CryptoSuite.Factory.getCryptoSuite();

        System.out.println("Creating client");
        final HFClient client = HFClient.createNewInstance();
        client.setCryptoSuite(crypto);


        System.out.println("Loading org1 User1 from disk");

        final String privateKeyFileName = "/Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/c050bce32a20c8c3bda5b7a9320c6e6f043fe2ff22af645b12844a46f7e97f0c_sk";

        final String certificateFileName = "/Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem";
        final File privateKeyFile = new File(privateKeyFileName);
        final File certificateFile = new File(certificateFileName);
        final User User1 = Utils.getUser("PeerAdmin", "Org1MSP", privateKeyFile, certificateFile);
        client.setUserContext(User1);

        // Accessing channel, should already exist
        System.out.println("Accessing channel");
        Channel myChannel = client.newChannel("mychannel");

        System.out.println("Setting channel configuration");
        final List<Peer> peers = new LinkedList<>();
        Properties peerProperties = new Properties();

        peerProperties.setProperty("pemFile",
                "/Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt");

        peerProperties.setProperty("hostnameOverride", "peer0.org1.example.com");
        peerProperties.setProperty("sslProvider", "openSSL");
        peerProperties.setProperty("negotiationType", "TLS");
        peerProperties.setProperty("trustServerCertificate", "true"); // testing // // PRODUCTION!
        peerProperties.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
        peers.add(client.newPeer("peer0.org1.example.com", "grpcs://localhost:7051", peerProperties));

        myChannel.addPeer(peers.get(0), createPeerOptions().setPeerRoles(EnumSet.of(PeerRole.ENDORSING_PEER,
                PeerRole.LEDGER_QUERY, PeerRole.CHAINCODE_QUERY, PeerRole.EVENT_SOURCE)));

        Properties ordererProperties = new Properties();
        ordererProperties.setProperty("pemFile",
                "/Users/zhangyulong/Documents/gopath/src/github.com/hyperledger/fabric-samples/first-network/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt");
        ordererProperties.setProperty("trustServerCertificate", "true");
        ordererProperties.setProperty("hostnameOverride", "orderer.example.com");
        ordererProperties.setProperty("sslProvider", "openSSL");
        ordererProperties.setProperty("negotiationType", "TLS");
        ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[] { 5L, TimeUnit.MINUTES });
        ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[] { 8L, TimeUnit.SECONDS });
        myChannel.addOrderer(client.newOrderer("orderer.example.com", "grpcs://localhost:7050", ordererProperties));

        myChannel.initialize();

        // Creating proposal for query
        System.out.println("Creating proposal for query(a)");
        final QueryByChaincodeRequest queryAProposalRequest = client.newQueryProposalRequest();
        final ChaincodeID queryChaincodeID = ChaincodeID.newBuilder().setName("mycc").setVersion("1.0").build();
        queryAProposalRequest.setChaincodeID(queryChaincodeID);
        queryAProposalRequest.setFcn("query");
        // queryAProposalRequest.setFcn("queryAllCars");
        queryAProposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(10));
        queryAProposalRequest.setArgs(new String[] { "a" });
        int blocksize = (int) myChannel.queryBlockchainInfo().getHeight();
        System.out.println("NO of Blocks: " + blocksize);
        // Send proposal and wait for responses
        System.out.println("Sending proposal for query");
        final Collection<ProposalResponse> queryAResponses = myChannel.queryByChaincode(queryAProposalRequest,
                myChannel.getPeers());

        for (ProposalResponse resp : queryAResponses) {
            System.out.println(
                    "Response from peer " + resp.getPeer().getName() + " is " + resp.getProposalResponse().getResponse()
                            + resp.getProposalResponse().getResponse().getPayload().toStringUtf8());
        }

        // Creating proposal for query
        System.out.println("Creating proposal for invoke(a,b,10)");

        final TransactionProposalRequest invokeProposalRequest = client.newTransactionProposalRequest();
        invokeProposalRequest.setChaincodeID(queryChaincodeID);
        invokeProposalRequest.setFcn("invoke");
        invokeProposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(10));
        invokeProposalRequest.setArgs(new String[] { "a", "b", "10" });

        final Collection<ProposalResponse> queryBResponses = myChannel.sendTransactionProposal(invokeProposalRequest,
                myChannel.getPeers());
        for (ProposalResponse resp : queryBResponses) {
            System.out.println("Response from peer " + resp.getPeer().getName() + " is"
                    + resp.getProposalResponse().getResponse());
        }

2 个答案:

答案 0 :(得分:0)

没有任何内容发送给订购者。请按照README的建议查看SDK提供给您的集成测试。它显示了带有测试变更的查询的平衡转移交易。

答案 1 :(得分:0)

要在结构网络中写入新块,您将执行3步:

  1. 向交易对象发送交易建议请求(他们在此阶段模拟交易);
  2. 收集所有同行的提案回复;

如果所有答复都一致,则可以执行以下步骤:

  1. 将交易发送给所有同行。

同时,对于查询,您只需要执行步骤1。


因此,在您的代码中仅执行步骤1。要完成此操作,您需要添加以下代码:

// STEP 1
final Collection<ProposalResponse> queryBResponses = myChannel.sendTransactionProposal(invokeProposalRequest,
                myChannel.getPeers());
        for (ProposalResponse resp : queryBResponses) {
            System.out.println("Response from peer " + resp.getPeer().getName() + " is"
                    + resp.getProposalResponse().getResponse());
        }


// STEP 2
Collection<ProposalResponse> successful = new ArrayList<>();
Collection<ProposalResponse> failed = new ArrayList<>();
for (ProposalResponse response : queryBResponses) {
    if (response.getStatus() == ProposalResponse.Status.SUCCESS) {
        successful.add(response);
    } else {
        failed.add(response);
    }
}

if (failed.size() > 0) throw new BlockChainException("...");
// CHECK CONSISTENCY
Collection<Set<ProposalResponse>> proposalConsistencySets = SDKUtils.getProposalConsistencySets(queryBResponses);
if (proposalConsistencySets.size() != 1) {
    throw new BlockChainException("...");
}
// STEP 3
CompletableFuture<BlockEvent.TransactionEvent> transactionEventCompletableFuture = channel.sendTransaction(successful);