投票系统的 fabric-sdk-node的集成

下载投票系统项目代码

git clone https://github.com/didianV5/voteApp.git

运行项目

$ chmod -R 777 voteApp

$ cd voteApp

$ cd app

# 删除docker 容器
$ docker rm -f $(docker ps -aq)

# 启动 fabric 的网络
$ ./startFabric.sh 

# 安装 node 依赖包
$ npm install

# 注册管理员
$ node enrollAdmin.js

# 注册用户
$ node registerUser.js

# 启动项目
$ node index.js

# 访问 http://localhost:3000

代码解析

项目目录

app 存放node代码,负责调用fabric-sdk-node

basic-network 存放fabric network相应配置

chaincode 存放链码

项目流程

访问 http://localhost:3000 会调用 app/controllers/index.js 中的 indexPage 方法

// queryTuna - requires 1 argument, ex: args: ['4'],
            const request = {
                chaincodeId: 'vote',
                txId: tx_id,
                fcn: 'getUserVote',
                args: [""]
            };

            // send the query proposal to the peer
            return channel.queryByChaincode(request);

当提交投票人的姓名时,会调用 app/controllers/index.js 中的 saveUser 方法,构造合法的交易提案提交给背书节点进行背书,当数据写入账本后,通过监听事件得到最后的结果

const request = {
                //targets : --- letting this default to the peers assigned to the channel
                chaincodeId: 'vote',
                fcn: 'voteUser',
                args: [username],
                chainId: 'mychannel',
                txId: tx_id
            };

            // send the transaction proposal to the peers
            return channel.sendTransactionProposal(request);

results matching ""

    No results matching ""