五、hyperledger fabric 投票SDK(java)
阿新 • • 發佈:2018-12-20
一、編寫啟動指令碼,包含啟動網路、初始化鏈碼、安裝鏈碼。
#!/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/ [email protected]/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/[email protected]/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/ [email protected]/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("chenjunfei");
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);
}
}