在Ubuntu开发环境上安装Hyperledger Fabric

区块链是时下热门的技术,其中IBM Hyperledger(开源)是联盟链中的佼佼者。IBM有相关的课程。
如下是在自家Ubuntu开发环境上搭建Hyperledger Fabric的全过程供参考。
先上个步骤图

(原文不小心被dsm加密了,还原不出来只能屏幕截图):
在Ubuntu开发环境上安装Hyperledger Fabric_第1张图片
2/讲师预先给的安装包给了我很大帮助,主要是 hyperledger-fabric-source.tar.gz 这个2G中有我所需安装包,省去了很多下载时间

安装前要做的事情(都是我推测出来的):
1/ sodu mkdir /data /data/install
2/ 将hyperledger-fabric-source.tar.gz拷贝到install目录
3/ 新建用户 fabric : sudo adduser fabric

修改后的安装脚本(注意加上执行权限)
修改是注释掉了第11步(部分开发工具的dev安装,这些不用)
file:///home/vincent/Hyperledger/hyperledger-fabric-setup.sh

运行命令检查安装是否成功:
peer version 返回:Fabric peer server version 0.6.1-preview-snapshot-4a78b41

peer node status

peer node start
(如果这条命令若加了sudo,注意只使用fabirc用户启动,否则反而是报错 Error reading CORE_PBFT plugin config: Unsupported Config Type "")

之前报错:目录创建不了 /var/Hyperledger/production
因为我是用vincent用户启动的,但是如上目录我没有权限去创建目录,所以我在配置文件《 core.yaml 》中将其修改为

    # Path on the file system where peer will store data
    fileSystemPath: /home/vincent/Hyperledger

就可以启动了


vincent@minipc:~$ peer node start
17:46:51.737 [nodeCmd] serve -> INFO 001 Security enabled status: false
17:46:51.737 [nodeCmd] serve -> INFO 002 Privacy enabled status: false
17:46:51.737 [eventhub_producer] start -> INFO 003 event processor started
17:46:51.857 [chaincode] NewChaincodeSupport -> INFO 004 Chaincode support using peerAddress: 0.0.0.0:7051
17:46:51.858 [sysccapi] RegisterSysCC -> INFO 005 system chaincode (noop,http://http://github.com/hyperledger/fabric/bddtests/syschaincode/noop) disabled
17:46:51.858 [state] loadConfig -> INFO 006 Loading configurations...
17:46:51.859 [state] loadConfig -> INFO 007 Configurations loaded. stateImplName=[buckettree], stateImplConfigs=map[numBuckets:%!s(int=1000003) maxGroupingAtEachLevel:%!s(int=5) bucketCacheSize:%!s(int=100)], deltaHistorySize=[500]
17:46:51.859 [state] NewState -> INFO 008 Initializing state implementation [buckettree]
17:46:51.859 [buckettree] initConfig -> INFO 009 configs passed during initialization = map[string]interface {}{"bucketCacheSize":100, "numBuckets":1000003, "maxGroupingAtEachLevel":5}
17:46:51.859 [buckettree] initConfig -> INFO 00a Initializing bucket tree state implemetation with configurations &{maxGroupingAtEachLevel:5 lowestLevel:9 levelToNumBucketsMap:map[8:200001 5:1601 4:321 3:65 0:1 7:40001 6:8001 9:1000003 2:13 1:3] hashFunc:0xab0aa0}
17:46:51.859 [buckettree] newBucketCache -> INFO 00b Constructing bucket-cache with max bucket cache size = [100] MBs
17:46:51.859 [buckettree] loadAllBucketNodesFromDB -> INFO 00c Loaded buckets data in cache. Total buckets in DB = [0]. Total cache size:=0
17:46:51.859 [genesis] func1 -> INFO 00d Creating genesis block.--注:生成了创世区块
17:46:51.868 [consensus/controller] NewConsenter -> INFO 00e Creating default consensus plugin (noops) --注:共识算法都是core.yaml配置的
17:46:51.869 [consensus/noops] newNoops -> INFO 00f NOOPS consensus type = *noops.Noops
17:46:51.869 [consensus/noops] newNoops -> INFO 010 NOOPS block size = 500
17:46:51.869 [consensus/noops] newNoops -> INFO 011 NOOPS block wait = 1s
17:46:51.870 [nodeCmd] serve -> INFO 012 Starting peer with ID=name:"jdoe" , network ID=dev, address=0.0.0.0:7051, rootnodes=, validator=true
17:46:51.875 [rest] StartOpenchainRESTServer -> INFO 013 Initializing the REST service on 0.0.0.0:7050, TLS is disabled.
17:46:51.875 [consensus/statetransfer] blockThread -> INFO 014 Validated blockchain to the genesis block



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

开始部署智能合约:(要进一步信息请搜索关键字 hyperledger的智能合约demo解析)
chaincode_example02 主要执行三个核心函数,init、invoke、query。

init函数在起始过程中初始化两个账户A,B,并且在A,B账户上发行一定数量的资产。

invoke函数在A、B上进行转账。

query函数查询A、B函数上的账户余额。

 

如下就是部署这个例子,并init,其中a发行了100块,b发行了200块


vincent@minipc:~$ peer chaincode deploy -n test_cc -p http://http://github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","a","100","b","200"]}'
Deploy chaincode:(部署成功了返回chaincode,可以理解为某种版本号) ee5b24a1f17c356dd5f6e37307922e39ddba12e5d2e203ed93401d7d05eb0dd194fb9070549c5dc31eb63f4e654dbd5a1d86cbb30c48e3ab1812590cd0f78539

peer chaincode deploy -n test_cc -p http://http://github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","amy","10000","vincent","20000"]}'
Deploy chaincode: 936bc00cf66064d62e8650338db80870063ae9ae4200af0a1e06e744566d21811630c2e45a064418a77a85e0042bc8a4fbfe627299b0116555d1b26ffe44dfac

生成了一些交易:
21:57:42.637 [devops] invokeOrQuery -> INFO 019 Transaction ID: beb4626e-abe3-4b68-84d9-47d97b08d90f
21:57:53.940 [devops] invokeOrQuery -> INFO 01a Transaction ID: a63a77af-5135-4f51-9359-53f8adb32378
22:29:42.455 [devops] invokeOrQuery -> INFO 01b Transaction ID: 49cf1bbc-bab7-463b-8ffa-d900912ecbfd

fabric@minipc:/data/sh$ peer chaincode query -c '{"Function": "query", "Args": ["a"]}' -n ee5b24a1f17c356dd5f6e37307922e39ddba12e5d2e203ed93401d7d05eb0dd194fb9070549c5dc31eb63f4e654dbd5a1d86cbb30c48e3ab1812590cd0f78539
Query Result: 100

peer chaincode invoke -c '{"Function": "query", "Args": ["amy"]}' -n 936bc00cf66064d62e8650338db80870063ae9ae4200af0a1e06e744566d21811630c2e45a064418a77a85e0042bc8a4fbfe627299b0116555d1b26ffe44dfac

这里用了invoke会报错

在另外start那个窗口的输出
22:34:57.778 [container] WriteGopathSrc -> INFO 01c rootDirectory = /data/Hyperledger/src
22:34:57.778 [container] WriteFolderToTarPackage -> INFO 01d rootDirectory = /data/Hyperledger/src
22:35:26.115 [devops] invokeOrQuery -> INFO 01e Transaction ID: 3e25c75d-a881-4d71-8be8-c15f14118eec
22:35:39.036 [devops] invokeOrQuery -> INFO 01f Transaction ID: af697214-2c5e-46eb-9a53-60dce9145dbe
22:36:10.114 [devops] invokeOrQuery -> INFO 020 Transaction ID: 815d2ce0-9bb0-4026-be04-32a3bb63109c


peer chaincode invoke -c '{"Function": "invoke", "Args": ["amy","vincent","999"]}' -n 936bc00cf66064d62e8650338db80870063ae9ae4200af0a1e06e744566d21811630c2e45a064418a77a85e0042bc8a4fbfe627299b0116555d1b26ffe44dfac



运行了之后,amy余额就减少了,vincent增加了999块钱



peer chaincode invoke -c '{"Function": "invoke", "Args": ["a","b","99"]}' -n ee5b24a1f17c356dd5f6e37307922e39ddba12e5d2e203ed93401d7d05eb0dd194fb9070549c5dc31eb63f4e654dbd5a1d86cbb30c48e3ab1812590cd0f78539

注意:金额控制要在智能合约中体现。连续调用99元转账后,否则智能合约中又没有对负值的控制,导致余额是负的!

 

你可能感兴趣的:(超级账本,IBM,Fabric)