Skip to content
a fugitive ledger based on hyperledger fabric java sdk
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
build-network
docs
fugitivec
network-resources
webui
.gitignore
LICENSE
README.md

README.md

Fugitive Consortium

Introduction

基于fabric-java-sdk实现的区块链追逃记录账本. 项目分为网络, 应用和链码三部分:

  • 网络: 生成容器网络启动所需的文件(见build-network文件夹), 除开编写和设置容器网络配置信息外, 将启动/关闭网络的命令写好shell脚本方便调用. 并预生成了可用的网络配置文件(见network-resources文件夹)
  • 应用: 根据fabric-java-sdk给出的几个类, 封装成更便于使用的manager类, 并将其用于完成创建通道, 注册用户, 部署链码, 调用链码等操作.
  • 链码: 创建了一个Person类存储逃犯人员信息, 重载ChaincodeBase类的initinvoke方法, 在init方法内完成账本的初始化操作, 在invoke方法内根据应用请求(增删查改)进入到不同的处理方法中, 读取更改账本上的信息.

网络拓扑结构如下所示:

network-topology.jpg

.
├── build-network
│   ├── common-services.yml             # 对peer节点, orderer节点和ca节点的通用配置, 用于docker-compose.yml
│   ├── configtx.yaml                   # 包含对所创建的容器网络的定义
│   ├── crypto-config.yaml              # 包含加密文件(证书等)的生成配置
│   ├── docker-compose.yml              # docker-compose的配置文件, 根据该配置文件信息生成容器网络
│   ├── generate.sh                     # 用于证书文件, orderer节点创世区块, 通道配置以及背书节点配置
│   ├── init.sh                         # 用于初始化docker容器网络构建环境
│   ├── restart.sh                      # teardown.sh 和 start.sh 的整合. 用于开发时重置容器网络
│   ├── start.sh                        # 命令docker-compose启动容器网络
│   ├── stop.sh                         # 命令docker-compose停止容器网络
│   └── teardown.sh                     # 命令docker-compose停止容器网络, 并清除所有docker容器和链码对应的docker镜像
├── docs
│   ├── Instructions.md                 # 描述手动部署分布式账本网络所需要的一些命令
│   └── VSCodeSetup.md                  # 描述在VS Code中进行构建应用, 调试代码, 单元测试的步骤与命令
├── fugitivec
│   ├── pom.xml                         # maven工程配置文件
│   ├── src                             # 应用源代码所在目录
│   │   ├── com
│   │   │   └── vancir
│   │   │       ├── integration
│   │   │       │   ├── CreateChannel.java              # 用于创建通道并进行配置
│   │   │       │   ├── DeployChaincode.java            # 用于部署链码到网络
│   │   │       │   ├── InvokeChaincode.java            # 用于调用链码, 获取链码返回响应
│   │   │       │   └── RegisterAndEnrollUser.java      # 用于注册应用用户
│   │   │       ├── manager
│   │   │       │   ├── CAManager.java                  # 用于管理HFCAClient类的使用
│   │   │       │   ├── ChannelManager.java             # 用于管理Channel类的使用
│   │   │       │   └── FabricManager.java              # 用于管理HFClient类的使用
│   │   │       ├── network
│   │   │       │   └── CAEnrollment.java               # 实现Enrollment类接口方法
│   │   │       ├── user
│   │   │       │   └── AppUser.java                    # 用户类, 定义了应用用户的相关信息
│   │   │       └── utilities
│   │   │           ├── Config.java                     # 配置信息类, 定义了项目需要的一些配置信息
│   │   │           └── Util.java                       # 实用工具类, 定义了一些辅助性的方法
│   │   ├── resources
│   │   │   └── log4j.properties                        # 日志工具log4j的配置文件
│   │   └── test                                        # 单元测试所在目录
│   │       └── java
│   │           └── TestOrgAdmin.java                   # 对orgAdmin相关操作的单元测试
├── LICENSE
├── network-resources
│   ├── chaincode
│   │   ├── pom.xml                     # maven工程配置文件
│   │   ├── src                         # 链码源代码所在目录
│   │   │   ├── fugitivec
│   │   │   │   └── Chaincode.java      # 链码源代码
│   │   │   │   └── Person.java         # 逃犯类, 用于描述逃犯档案信息
│   │   │   └── resources               # 资源文件目录
│   │   │       └── log4j.properties    # 日志工具log4j的配置文件
│   ├── channel-artifacts               # 包含运行generate.sh后生成的创世区块, 通道配置, 背书节点配置.
│   └── crypto-config                   # 包含运行generate.sh后生成的证书文件信息
└── README.md

Prerequisites

  1. Java 8u181: 建议使用这个版本的JDK用作开发.
  2. Hyperledger Fabric Java SDK 1.3.0: 使用Fabric给出的java sdk编写链码以及应用.
  3. Apache Maven: 使用Maven管理Java工程项目, 生成应用.
  4. Docker 18.09.0: 使用Docker部署容器, 用于构建网络拓扑结构.
  5. Docker Compose 1.22.0: 使用配置文件来定义和运行管理容器的Docker工具, 配置可见build-network/docker-compose.yml

如何配置Fabric环境可参见: Hyperledger Fabric Docs - PrerequisitesInstall Samples, Binaries and Docker Images.

Quick Start

  1. 生成容器网络运行所需的文件并启动网络

注意: 运行generate.sh重新生成证书等信息后, 需要重新修改docker-compose.ymlca.org1.vancir.comca.org2.vancir.com的环境变量FABRIC_CA_SERVER_CA_KEYFILEFABRIC_CA_SERVER_CA_CERTFILE为相应的证书及密钥路径. 对应的路径在network-resources/crypto-config/peerOrganizations/org1(2).vancir.com/ca. 当然默认我已经预生成好的证书等信息并配置好, 无需再次运行init.shgenerate.sh

$ cd build-network
$ chmod +x *.sh
$ (./init.sh)
$ (./generate.sh)
$ ./start.sh

你也可用使用stop.sh停止网络, teardown.sh停止并清除网络.

  1. 生成应用
$ cd fugitivec
$ ls
pom.xml  src
$ mvn install

确保你安装好了maven并将其添加到了你的环境变量中去. mvn install将会下载项目的依赖包并进行测试构建并生成应用, 输出到target文件夹内.

$ ls
archive-tmp  fugitivec-1.0-SNAPSHOT.jar                        generated-sources  maven-status  generated-test-sources       
classes      fugitivec-1.0-SNAPSHOT-jar-with-dependencies.jar  maven-archiver     test-classes  surefire-reports
  1. 生成链码
$ cd network-resources/chaincode
$ ls
pom.xml  src 
$ mvn install

同上生成应用步骤, 该步骤将会生成链码对应的jar包, 放置于target文件夹内.

  1. 运行应用
  • 将应用对应的jar包放置在network-resources目录下.
$ cd network-resources
$ cp ../fugitivec/target/fugitivec-1.0-SNAPSHOT-jar-with-dependencies.jar .

因为运行时的一些路径原因, 需要将生成的fugitivec-1.0-SNAPSHOT-jar-with-dependencies.jar放置在network-resources文件夹下.

  • 部署链码

使用DeployChaincode来部署并实例化链码.

$ cd network-resources
$ java -cp fugitivec-1.0-SNAPSHOT-jar-with-dependencies.jar com.vancir.integration.DeployChaincode
2018-12-20 14:21:04 WARN  Config:127 - Failed to load any configuration from: config.properties. Using toolkit defaults
2018-12-20 14:21:05 INFO  Channel:770 - Channel{id: 3, name: mychannel} joining Peer{ id: 5, name: peer0.org1.vancir.com, channelName: null, url: grpc://localhost:7051}.
2018-12-20 14:21:06 INFO  Channel:802 - Peer Peer{ id: 5, name: peer0.org1.vancir.com, channelName: mychannel, url: grpc://localhost:7051} joined into channel Channel{id: 3, name: mychannel}
2018-12-20 14:21:06 INFO  Channel:770 - Channel{id: 3, name: mychannel} joining Peer{ id: 6, name: peer1.org1.vancir.com, channelName: null, url: grpc://localhost:7056}.
2018-12-20 14:21:06 INFO  Channel:802 - Peer Peer{ id: 6, name: peer1.org1.vancir.com, channelName: mychannel, url: grpc://localhost:7056} joined into channel Channel{id: 3, name: mychannel}
2018-12-20 14:21:06 INFO  Channel:1147 - Channel Channel{id: 3, name: mychannel} eventThread started shutdown: false  thread: null 
2018-12-20 14:21:06 INFO  Channel:770 - Channel{id: 3, name: mychannel} joining Peer{ id: 7, name: peer0.org2.vancir.com, channelName: null, url: grpc://localhost:8051}.
2018-12-20 14:21:06 WARN  PeerEventServiceClient:230 - PeerEventServiceClient{id: 16, channel: mychannel, peerName: peer0.org2.vancir.com, url: grpc://localhost:8051} PeerEventServiceClient{id: 16, channel: mychannel, peerName: peer0.org2.vancir.com, url: grpc://localhost:8051} attempts 0 Status returned failure code 404 (NOT_FOUND) during peer service event registration
2018-12-20 14:21:07 INFO  Channel:802 - Peer Peer{ id: 7, name: peer0.org2.vancir.com, channelName: mychannel, url: grpc://localhost:8051} joined into channel Channel{id: 3, name: mychannel}
2018-12-20 14:21:07 INFO  Channel:770 - Channel{id: 3, name: mychannel} joining Peer{ id: 8, name: peer1.org2.vancir.com, channelName: null, url: grpc://localhost:8056}.
2018-12-20 14:21:07 WARN  PeerEventServiceClient:230 - PeerEventServiceClient{id: 19, channel: mychannel, peerName: peer1.org2.vancir.com, url: grpc://localhost:8056} PeerEventServiceClient{id: 19, channel: mychannel, peerName: peer1.org2.vancir.com, url: grpc://localhost:8056} attempts 0 Status returned failure code 404 (NOT_FOUND) during peer service event registration
2018-12-20 14:21:07 INFO  Channel:802 - Peer Peer{ id: 8, name: peer1.org2.vancir.com, channelName: mychannel, url: grpc://localhost:8056} joined into channel Channel{id: 3, name: mychannel}
2018-12-20 14:21:07 INFO  InstallProposalBuilder:244 - Installing 'mycc::::1' language Java chaincode from directory: '/home/vancir/Documents/code/fugitivec/network-resources/chaincode' with source location: 'src'. chaincodePath:''
2018-12-20 14:21:09 INFO  DeployChaincode:78 - mycc - Chaincode deployment SUCCESS
2018-12-20 14:21:09 INFO  DeployChaincode:78 - mycc - Chaincode deployment SUCCESS
2018-12-20 14:21:09 INFO  InstallProposalBuilder:244 - Installing 'mycc::::1' language Java chaincode from directory: '/home/vancir/Documents/code/fugitivec/network-resources/chaincode' with source location: 'src'. chaincodePath:''
2018-12-20 14:21:11 INFO  DeployChaincode:87 - mycc - Chaincode deployment SUCCESS
2018-12-20 14:21:11 INFO  DeployChaincode:87 - mycc - Chaincode deployment SUCCESS
2018-12-20 14:24:55 INFO  ChannelManager:82 - java.util.concurrent.CompletableFuture@73302995[Not completed]
2018-12-20 14:24:55 INFO  DeployChaincode:97 - mycc - Chaincode instantiation SUCCESS
2018-12-20 14:24:55 INFO  DeployChaincode:97 - mycc - Chaincode instantiation SUCCESS
2018-12-20 14:24:55 INFO  DeployChaincode:97 - mycc - Chaincode instantiation SUCCESS
2018-12-20 14:24:55 INFO  DeployChaincode:97 - mycc - Chaincode instantiation SUCCESS
  • 调用链码

部署并实例化链码后, 可以使用InvokeChaincode来调用链码

$ java -cp fugitivec-1.0-SNAPSHOT-jar-with-dependencies.jar com.vancir.integration.InvokeChaincode 
2019-03-27 21:20:50 WARN  Config:127 - Failed to load any configuration from: config.properties. Using toolkit defaults
2019-03-27 21:20:50 WARN  Config:78 - Failed to load any configuration from: config.properties. Using toolkit defaults
2019-03-27 21:20:52 INFO  CAManager:91 - CA -http://localhost:7054 Enrolled User - admin
2019-03-27 21:20:53 INFO  Channel:1147 - Channel Channel{id: 1, name: mychannel} eventThread started shutdown: false  thread: null 
2019-03-27 21:20:53 INFO  ChannelManager:101 - added with ID002
2019-03-27 21:20:53 INFO  InvokeChaincode:72 - Please input your command.
add ID003 Sam Female 22 true Sam_is_not_good
2019-03-27 21:21:16 INFO  InvokeChaincode:90 - Your opt is: add and your args is: [ID003, Sam, Female, 22, true, Sam_is_not_good]
2019-03-27 21:21:16 INFO  ChannelManager:101 - added with ID003
query ID002
2019-03-27 21:21:25 INFO  InvokeChaincode:90 - Your opt is: query and your args is: [ID002]
2019-03-27 21:21:25 INFO  ChannelManager:101 - Query Response: Name: Peter, Sex: Male, Age: 19, isFleeing: false, Description: Perter is a good boy
update ID002 Alice_is_fugitive
2019-03-27 21:21:33 INFO  InvokeChaincode:90 - Your opt is: update and your args is: [ID002, Alice_is_fugitive]
2019-03-27 21:21:33 INFO  ChannelManager:101 - Key: ID002. Before updated, the message is Perter is a good boy After updated, the message now is Alice_is_fugitive.
query ID002
2019-03-27 21:21:40 INFO  InvokeChaincode:90 - Your opt is: query and your args is: [ID002]
2019-03-27 21:21:40 INFO  ChannelManager:101 - Query Response: Name: Peter, Sex: Male, Age: 19, isFleeing: false, Description: Alice_is_fugitive

如上所示, 在要求输入命令时:

  • 示例输入add ID003 Sam Female 22 true Sam_is_not_good添加了一名ID为ID003, 姓名为Sam, 性别为Female, 年龄为22, 是否在逃为true, 描述为Sam_is_not_good的逃犯信息.
  • query ID002查询ID为ID002的逃犯信息, 答复为Query Response: Name: Peter, Sex: Male, Age: 19, isFleeing: false, Description: Perter is a good boy
  • update ID002 Alice_is_fugitive将ID为ID002的逃犯信息修改为Alice_is_fugitive
  • query ID002 验证刚才的修改是成功的.

成果展示如下所示:

区块链网络使用步骤.jpg

You can’t perform that action at this time.