六、hyperledger fabric 投票应用(java-sdk)

继上篇文章编写的投票链码,并且在fabric网络中测试成功,本节将通过java-sdk调用写好的投票链码。

一、编写启动脚本,包含启动网络、初始化链码、安装链码。

#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error
set -e

# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1

CC_SRC_PATH=github.com/vote

starttime=$(date +%s)

# launch network; create channel and join peer to channel
cd ../basic-network
./start.sh

# Now launch the CLI container in order to install, instantiate chaincode
# and prime the ledger with our 10 cars
docker-compose -f ./docker-compose.yml up -d cli

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n vote -v 1.0 -p "$CC_SRC_PATH"
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n vote -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
sleep 10
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n vote -c '{"function":"getUserVote","Args":[""]}'

二、启动网络

三、Java调用链码程序(java-sdk)

1、注册admin用户

public void enroll(String username, String password, String certDir) throws Exception {
        HFClient client = HFClient.createNewInstance();
        CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
        client.setCryptoSuite(cs);
        Properties prop = new Properties();
        prop.put("verify", false);
        HFCAClient caClient = HFCAClient.createNewInstance("http://192.168.137.3:7054", prop);
        caClient.setCryptoSuite(cs);
        Enrollment enrollment = caClient.enroll(username, password);
        CertUtils.saveEnrollment(enrollment, certDir, username);
    }

2、注册普通用户

public void regist(String username, String certDir) throws Exception {
        HFClient client = HFClient.createNewInstance();
        CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
        client.setCryptoSuite(cs);
        Properties prop = new Properties();
        prop.put("verify", false);
        HFCAClient caClient = HFCAClient.createNewInstance("http://192.168.137.3:7054", prop);
        caClient.setCryptoSuite(cs);
        RegistrationRequest rr = new RegistrationRequest(username, "org1");
        String enrollmentSecret = caClient.register(rr,  new CarUser(
                "admin",
                CertUtils.loadEnrollment("cert", "admin")));
        Enrollment enrollment = caClient.enroll(username, enrollmentSecret);
        CertUtils.saveEnrollment(enrollment, certDir, username);
    }

3、给指定用户投票、存在则票数+1,不存在则新建用户

public void voteUser(final String name) throws Exception {
        HFClient client = HFClient.createNewInstance();
        Channel channel = initChannel(client);
        TransactionProposalRequest req = client.newTransactionProposalRequest();
        ChaincodeID cid = ChaincodeID.newBuilder().setName("vote").build();
        req.setChaincodeID(cid);
        req.setFcn("voteUser");
        req.setArgs(name);
        Collection<ProposalResponse> resps = channel.sendTransactionProposal(req);
        channel.sendTransaction(resps);
    }

4、查询所有用户的票数情况

public void queryVote() throws Exception {
        HFClient client = HFClient.createNewInstance();
        Channel channel = initChannel(client);
        QueryByChaincodeRequest req = client.newQueryProposalRequest();
        ChaincodeID cid = ChaincodeID.newBuilder().setName("vote").build();
        req.setChaincodeID(cid);
        req.setFcn("getUserVote"); // queryAllCars
        req.setArgs("");
        Collection<ProposalResponse> resps = channel.queryByChaincode(req);
        for (ProposalResponse resp : resps) {
            String payload = new String(resp.getChaincodeActionResponsePayload());
            System.out.println("response: " + payload);
        }
    }

查询结果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨晨晨晨晨晨晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值