Fabric操作用例

利用fabric-java-sdk来install和instan

2018-07-05  本文已影响297人  CodingCode

利用fabric-java-sdk调用install和instantiate chaincode的例子

这个文章是基于之前的《一个利用fabric-java-sdk调用balance-tranfer的例子》的扩展,所以他们共用的代码就在此省略,请参考前面。

Install Chaincode

    Collection<ProposalResponse> installProposal(HFClient client, Channel channel, ChaincodeID chaincodeID,
                      Type chaincodeLang,         // Type.GO_LANG
                      String chaincodeVer,        // "v1"
                      String chaincodeSourceLoc,  // "/opt/gopath"
                      String chaincodePath        // "github.com/hyperledger/fabric/peer/chaincode/go/chaincode_example02
    ) throws InvalidArgumentException, ProposalException {
        InstallProposalRequest installProposalRequest = client.newInstallProposalRequest();
        installProposalRequest.setChaincodeID(chaincodeID);
        installProposalRequest.setChaincodeVersion(chaincodeVer);
        installProposalRequest.setChaincodeLanguage(chaincodeLang);
        installProposalRequest.setChaincodeSourceLocation(new File(chaincodeSourceLoc));
        installProposalRequest.setChaincodePath(chaincodePath);

        Collection<ProposalResponse> propResp = client.sendInstallProposal(installProposalRequest, channel.getPeers());
        for (ProposalResponse response : propResp) {
            if (response.getStatus() == Status.SUCCESS) {
                out("Install proposal response SUCC Txid=[%s], peer=[%s]", response.getTransactionID(), response.getPeer().getUrl());
                dumpRWSet(response);
            } else {
                out("Install proposal response FAIL Txid=[%s], peer=[%s]", response.getTransactionID(), response.getPeer().getUrl());
            }
        }

        return propResp;
    }

注意:目录 {chaincodeSourceLoc}/src/{chaincodePath}/ 下面包含chaincode的源文件(.go文件)
通常{chaincodeSourceLoc}是GOPATH的值。

Instantiate Chaincode

    Collection<ProposalResponse> instantiateProposal(HFClient client, Channel channel, ChaincodeID chaincodeID,Type chaincodeLang) throws InvalidArgumentException, ChaincodeEndorsementPolicyParseException, IOException, ProposalException {
        InstantiateProposalRequest instantiateProposalRequest = client.newInstantiationProposalRequest();
        instantiateProposalRequest.setProposalWaitTime(120000);//time in milliseconds
        instantiateProposalRequest.setChaincodeID(chaincodeID);
        instantiateProposalRequest.setChaincodeLanguage(chaincodeLang);
        instantiateProposalRequest.setFcn("init");
        instantiateProposalRequest.setArgs(new String[] {"a", "1500", "b", "2500"});

        // I do not know the purpose of transient map works for.
        Map<String, byte[]> transientMap = new HashMap<>();
        transientMap.put("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".getBytes(UTF_8));
        transientMap.put("method", "InstantiateProposalRequest".getBytes(UTF_8));
        instantiateProposalRequest.setTransientMap(transientMap);

        //ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
        //chaincodeEndorsementPolicy.fromYamlFile(new File({policyfile}));
        //instantiateProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);

        Collection<ProposalResponse> propResp = channel.sendInstantiationProposal(instantiateProposalRequest, channel.getPeers());
        for (ProposalResponse response : propResp) {
            if (response.getStatus() == Status.SUCCESS) {
                out("Instantiate proposal response SUCC Txid=[%s], peer=[%s]", response.getTransactionID(), response.getPeer().getUrl());
                dumpRWSet(response);
            } else {
                out("Instantiate proposal response FAIL Txid=[%s], peer=[%s]", response.getTransactionID(), response.getPeer().getUrl());
            }
        }

        return propResp;
    }
上一篇下一篇

猜你喜欢

热点阅读