区块链资讯网>区块链开发>正文

区块链系统开发 区块链投票应用(java-sdk)

摘要
区块链系统开发 区块链投票应用(java-sdk),编写启动脚本,包含启动网络、初始化链码、安装链码。

六、区块链系统开发区块链投票应用(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":[""]}'

二、启动网络

区块链系统开发,区块链投票应用,区块链投票22.jpg

三、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);

        }

    }

查询结果

区块链系统开发,区块链投票应用,区块链投票33.jpg


    【免责声明】以上文章是今日小编为您介绍区块链开发的个人观点,请读者仅作参考,投资有风险,入市须谨慎!本站不拥有所有权,不承担相关法律责任。如需了解“区块链钱包系统开发”请关注:【区块链开发 http://www.hjhfxw.com/develop/】如若转载,请注明。


什么是区块链?区块链价值凸显迎来全新的投资机会?

什么是区块链?区块链价值凸显迎来全新的投资机会?比特币自从冲破5000美元后,处于相对稳定的振幅状态,这对于数字货币投资者来说是件值得庆幸的事情,

04月23日 15:40

什么是区块链技术应用?区块链在全球范围内又掀起了新一轮的研究热潮

什么是区块链技术应用?区块链在全球范围内又掀起了新一轮的研究热潮,区块链的出现实现了从传递信息的信息互联网向传递价值的价值互联网的进化,

04月19日 14:20

什么是区块链技术?不懂区块链技术原理图解说?

什么是区块链技术?区块链技术原理图解说,它不只是金融界的事,更不等于比特币或各种虚拟货币。无论是转帐、听音乐、旅游、物联网,

04月18日 14:41

什么是区块链技术应用?区块链创业学院财富峰会暨大健康产业链

什么是区块链技术应用?区块链创业学院财富峰会暨大健康产业链,区块链建立数据信用,让参与方实现信用共享,这种方式在未来,区块链技术将让造假无所遁形,

04月17日 15:31

区块链技术开发?只做真正的区块链技术培训,选好培训班更重要

区块链技术开发?只做真正的区块链技术培训,选好培训班更重要,区块链也一跃成为了最为火热的词汇,让无数圈内人的视点迅速聚焦。

04月11日 15:07