【Fabric】简单测试:链码单元测试+fabric-go-sdk

【Fabric】简单测试:链码单元测试+fabric-go-sdk,第1张

纯粹记录一下,也不知道干了啥~

链码单元测试 在Ubuntu中安装VScode 下载安装包

打开Ubuntu的火狐,访问网址:https://code.visualstudio.com/docs=》点击右上角Downdload=》点击.deb就,等待下载即可。

安装

在Ubuntu的Download目录中打开终端,然后输入安装命令。

//安装过程挺快的,
gan@gan-virtual-machine:~/Downloads$ sudo dpkg -i code_1.68.1-1655263094_amd64.deb 
[sudo] password for gan: 
Selecting previously unselected package code.
(Reading database ... 217754 files and directories currently installed.)
Preparing to unpack code_1.68.1-1655263094_amd64.deb ...
Unpacking code (1.68.1-1655263094) ...
Setting up code (1.68.1-1655263094) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...
Processing triggers for gnome-menus (3.36.0-1ubuntu3) ...
Processing triggers for desktop-file-utils (0.26-1ubuntu3) ...
Processing triggers for shared-mime-info (2.1-2) ...

安装过程很快,安装好以后,就能在 Applications 里找到它了===》右键并点击 Add to Favorites ,把它固定到左侧,以后就方便使用了。

别的嘛~安装扩展,需要啥就安装啥就好啦!
问题(1)——然后要是装不上那些东西,运行一下这个命令

//修改/etc/profile
export GOROOT=/usr/local/go
export GOPATH=/root/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
export GOARCH=amd64
export GOOS=linux
export GOTOOLS=$GOROOT/pkg/tool
//然后 source /etc/profile
//顺便把~/.bashrc 文件的GOPATH也对应改改,因为我们之后要用,为了跑别人的代码~
root@gan-virtual-machine:~# vim ~/.bashrc 
root@gan-virtual-machine:~# source ~/.bashrc 

然后,在GOPATH目录下,创建src/github.com/golang/文件夹,以及src/golang.org/x/文件夹。不存在的话可以手动创建每级目录。
并在刚刚创建src/github.com/golang/的目录打开终端,输入:git clone https://github.com/golang/tools.git
下载完成后,可以通过文件管理器将Github.com/golang下的tools文件夹复制到golang.org/x文件夹下。

go env -w GO111MODULE=on
go env -w GOPROXY=https://proxy.golang.com.cn,direct
另外可以参考博客:https://blog.csdn.net/RogerSCL/article/details/100852840

问题(2)——另外,如果像我一样遇到这个问题:“…: permission denied”
可以这样解决

//go env 查看环境变量,然后可以看到
GOROOT="/usr/local/go"
GOPATH="/root/go"
//然后进入对应目录,修改其权限。就不会被Denied了!
root@gan-virtual-machine:/usr/local#  sudo chmod -R 777 go/
root@gan-virtual-machine:/root#  sudo chmod -R 777 go/

成功之后的提示,看到就很开心,哈哈哈~

Tools environment: GOPATH=/home/go
Installing 8 tools at /home/go/bin in module mode.
  gotests
  gomodifytags
  impl
  goplay
  dlv
  staticcheck
  gopls
  go-outline

Installing github.com/cweill/gotests/gotests@latest (/home/go/bin/gotests) SUCCEEDED
Installing github.com/cweill/gotests/gotests@latest (/home/go/bin/gotests) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (/home/go/bin/gomodifytags) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (/home/go/bin/gomodifytags) SUCCEEDED
Installing github.com/josharian/impl@latest (/home/go/bin/impl) SUCCEEDED
Installing github.com/josharian/impl@latest (/home/go/bin/impl) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (/home/go/bin/goplay) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (/home/go/bin/goplay) SUCCEEDED

问题(3)——如果重新从软件中心安装VSCode出现问题“status-code=409”的报错
解决方案:

snap changes
snap abort 【你想要安装的软件的ID】
//例如我的
root@gan-virtual-machine:~# snap changes
ID   Status  Spawn               Ready               Summary
4    Error   today at 15:17 CST  today at 15:19 CST  Install "code" snap from "latest/stable" channel
root@gan-virtual-machine:~# sudo snap abort 4
//然后重新回到应用中心安装即可
root@gan-virtual-machine:~#
//如果已经运行成功了再运行一次sudo snap abort 4,会出现下面的error
//error: cannot abort change 4 with nothing pending

Fabric go sdk开发简单介绍 一、能运行的简单实例

B站找了个有视频的,还有源码。我只是搬运工罢了,侵删!
视频:https://www.bilibili.com/video/BV1Ty4y1j71M?spm_id_from=333.999.0.0&vd_source=ca5a850733d431c9c80fd776c0581455
代码:https://github.com/sxguan/fabric-go-sdk

root@gan-virtual-machine:~# cd /root/go/src
//在hosts文件中写上 节点docker容器的ip 都是127.0.0.1
root@gan-virtual-machine:~/go/src# vim /etc/hosts
//即在hosts中写入下面6行语句
// 127.0.0.1 orderer.example.com
// 127.0.0.1 peer0.org1.example.com
// 127.0.0.1 peer1.org1.example.com
// 127.0.0.1 ca.org1.example.com
// 127.0.0.1 couchdb0
// 127.0.0.1 couchdb1

//配置go代理。因为之前为了下VSCode中go的一些插件改了go代理,所以这时候需要重新配一下
root@gan-virtual-machine:~/go/src# go env -w GOPROXY=https://goproxy.cn,direct
root@gan-virtual-machine:~/go/src# git clone https://github.com/sxguan/fabric-go-sdk.git
// Cloning into 'fabric-go-sdk'...
// remote: Enumerating objects: 732, done.
// remote: Counting objects: 100% (449/449), done.
// remote: Compressing objects: 100% (318/318), done.
// remote: Total 732 (delta 197), reused 131 (delta 131), pack-reused 283
// Receiving objects: 100% (732/732), 12.57 MiB | 2.63 MiB/s, done.
// Resolving deltas: 100% (239/239), done.
 
//拉取对应镜像,启动节点docker容器
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# docker-compose up -d
// Creating network "fixtures_test" with the default driver
// Creating volume "fixtures_orderer.example.com" with default driver
// Creating volume "fixtures_peer0.org1.example.com" with default driver
// Creating volume "fixtures_peer1.org1.example.com" with default driver
// Pulling ca.org1.example.com (hyperledger/fabric-ca:1.4.9)...
// 1.4.9: Pulling from hyperledger/fabric-ca
// b8f262c62ec6: Pull complete
// db9dbc6799b3: Pull complete
// 9c5c884a6020: Pull complete
// 370868734ec0: Pull complete
// cbce57ae4cad: Pull complete
// fc00299dbf2c: Pull complete
// a1e42212f1ce: Pull complete
// add0e148da22: Pull complete
// Digest: sha256:28f50c6aa4f4642842e706d3ae6dcee181921d03bd30ab2a8b09b66e0349d92f
// Status: Downloaded newer image for hyperledger/fabric-ca:1.4.9
// Pulling couchdb0 (hyperledger/fabric-couchdb:latest)...
// latest: Pulling from hyperledger/fabric-couchdb
// 8f91359f1fff: Pull complete
// 25382a7438ae: Pull complete
// 043089fd442c: Pull complete
// 51901bb19330: Pull complete
// 7e45b1a430cf: Pull complete
// c0c197a7fd22: Pull complete
// beb39dc557f4: Pull complete
// 1ada71a639d6: Pull complete
// 952d5b6650fc: Pull complete
// 37552ae4d0e1: Pull complete
// 45df897db071: Pull complete
// Digest: sha256:626cc21b7d614f0dbc9e3577c4e6a7bbb72eaee5e14cf75d1fe516f1bb2684dc
// Status: Downloaded newer image for hyperledger/fabric-couchdb:latest
// Creating orderer.example.com ... done
// Creating ca.org1.example.com    ... done
// Creating couchdb1            ... done
// Creating couchdb0               ... done
// Creating peer0.org1.example.com ... done
// Creating peer1.org1.example.com ... done

//go build 并且执行fabric-go-sdk文件
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# cd .. && go build && ./fabric-go-sdk
// go: downloading github.com/hyperledger/fabric-protos-go v0.0.0-20210318103044-13fdee960194
// go: downloading github.com/hyperledger/fabric-sdk-go v1.0.0
// go: downloading github.com/golang/protobuf v1.3.3
// go: downloading google.golang.org/grpc v1.29.1
// go: downloading github.com/pkg/errors v0.8.1
// go: downloading github.com/spf13/viper v1.1.1
// go: downloading github.com/Knetic/govaluate v3.0.0+incompatible
// go: downloading google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55
// go: downloading golang.org/x/net v0.0.0-20190613194153-d28f0bde5980
// go: downloading github.com/spf13/cast v1.3.1
// go: downloading github.com/hyperledger/fabric-config v0.0.5
// go: downloading github.com/fsnotify/fsnotify v1.4.7
// go: downloading github.com/hashicorp/hcl v1.0.0
// go: downloading github.com/magiconair/properties v1.8.1
// go: downloading github.com/mitchellh/mapstructure v1.3.2
// go: downloading github.com/pelletier/go-toml v1.8.0
// go: downloading github.com/spf13/afero v1.3.1
// go: downloading github.com/spf13/jwalterweatherman v1.1.0
// go: downloading gopkg.in/yaml.v2 v2.3.0
// go: downloading github.com/go-kit/kit v0.8.0
// go: downloading github.com/hyperledger/fabric-lib-go v1.0.0
// go: downloading github.com/prometheus/client_golang v1.1.0
// go: downloading golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3
// go: downloading github.com/cloudflare/cfssl v1.4.1
// go: downloading golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
// go: downloading github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4
// go: downloading github.com/prometheus/common v0.6.0
// go: downloading github.com/golang/mock v1.4.3
// go: downloading github.com/google/certificate-transparency-go v1.0.21
// go: downloading github.com/go-logfmt/logfmt v0.4.0
// go: downloading github.com/beorn7/perks v1.0.1
// go: downloading github.com/prometheus/procfs v0.0.3
// go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
// go: downloading github.com/zmap/zlint v0.0.0-20190806154020-fd021b4cfbeb
// go: downloading github.com/zmap/zcrypto v0.0.0-20190729165852-9051775e6a2e
// go: downloading github.com/weppos/publicsuffix-go v0.5.0
//——————————————————————————————————运行成功的结果如下——————————————————————————
// >> 开始创建通道......
// >>>> 使用每个org的管理员身份更新锚节点配置...
// >>>> 使用每个org的管理员身份更新锚节点配置完成
// >> 创建通道成功
// >> 加入通道......
// >> 加入通道成功
// >> 开始打包链码......
// >> 打包链码成功
// >> 开始安装链码......
// >> 安装链码成功
// >> 组织认可智能合约定义......
// >>> chaincode approved by Org1 peers:
// 	peer0.org1.example.com:7051
// 	peer1.org1.example.com:9051
// >> 组织认可智能合约定义完成
// >> 检查智能合约是否就绪......
// LifecycleCheckCCCommitReadiness cc = simplecc, = {map[Org1MSP:true]}
// LifecycleCheckCCCommitReadiness cc = simplecc, = {map[Org1MSP:true]}
// >> 智能合约已经就绪
// >> 提交智能合约定义......
// >> 智能合约定义提交完成
// >> 调用智能合约初始化方法......
// >> 完成智能合约初始化
// >> 通过链码外部服务设置链码状态......
// >> 设置链码状态完成
// 2022/06/24 17:26:10 Registered block event
// Transaction processing for endorser [peer1.org1.example.com:9051]: Chaincode status Code: (500) UNKNOWN. Description: error in simulation: failed to execute transaction 63578075890155a577a6681d495519e2e5ad0fe41b9aac3f1a7a82da128effef: invalid invocation: chaincode 'simplecc' has not been initialized for this version, must call as init first
// <--- 添加信息 --->: 
// Transaction processing for endorser [peer1.org1.example.com:9051]: Chaincode status Code: (500) UNKNOWN. Description: error in simulation: failed to execute transaction 98351ea81b384d7f5d6bc2e787f62d608736e644b9b9ea3987f32cf9a655cdb1: invalid invocation: chaincode 'simplecc' has not been initialized for this version, must call as init first
// <--- 添加信息 --->: 
// 2022/06/24 17:26:10 Receive block event:
// SourceURL: peer1.org1.example.com:9051
// Number: 4
// Hash: 213a622c3d811f492a9aee5b4da127301422699a2af16ab0f5872e39c4284e38
// PreviousHash: 2a4ea25352d1654f432305cb1a63335921ab02eb28842825595184ed5e19b358

// <--- 添加信息 --->: 34933c145d7982f9ce4a0c9dc7a9c6b82c995ebf6829ae3ab057701535d00d39
// <--- 查询信息 --->: 789
// 2022/06/24 17:26:13 Receive cc event, ccid: simplecc 
// eventName: chaincode-event
// payload: {"EventName":"set"} 
// txid: 34933c145d7982f9ce4a0c9dc7a9c6b82c995ebf6829ae3ab057701535d00d39 
// block: 5 
// sourceURL: peer1.org1.example.com:9051
// 2022/06/24 17:26:13 Receive block event:
// SourceURL: peer1.org1.example.com:9051
// Number: 5
// Hash: 0725fbb8795cfc49a1c585b075d0b6e96b112a353eb6558a06c162ec540ece05
// PreviousHash: 28a8ba538301f466061ca15d309cc53d7b6e2215b6cd93edab71d4de1e133d5b


//问题:
root@gan-virtual-machine:~/go/src/fabric-go-sdk# ./fabric-go-sdk
// >> 开始创建通道......
// >> Create channel and join error: Create channel error: error should be nil for SaveChannel of orgchannel: create channel failed: create channel failed: SendEnvelope failed: calling orderer 'orderer.example.com:7050' failed: Orderer Server Status Code: (400) BAD_REQUEST. Description: error applying config update to existing channel 'mychannel': error authorizing update: error validating ReadSet: proposed update requires that key [Group]  /Channel/Application be at version 0, but it is currently at version 1
//先关闭docker容器
root@gan-virtual-machine:~/go/src/fabric-go-sdk# cd fixtures/
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# docker-compose down && docker volume prune
// Stopping peer1.org1.example.com ... done
// Stopping peer0.org1.example.com ... done
// Stopping couchdb0               ... done
// Stopping couchdb1               ... done
// Stopping orderer.example.com    ... done
// Stopping ca.org1.example.com    ... done
// Removing peer1.org1.example.com ... done
// Removing peer0.org1.example.com ... done
// Removing couchdb0               ... done
// Removing couchdb1               ... done
// Removing orderer.example.com    ... done
// Removing ca.org1.example.com    ... done
// Removing network fixtures_test
// WARNING! This will remove all local volumes not used by at least one container.
// Are you sure you want to continue? [y/N] y
// Deleted Volumes:
// 99483ce2b4907e4f7b8359c548c8804480d2bfd3b66f07282ad6cefc91f1d198
// a3720081482fce6e3fb13a46d266a1c341cecd2403a5497c09b21ddbd65d805e
// fixtures_peer0.org1.example.com
// 19dde2d6511702c2fe3b3ce3bb75be5b5bb10f99530fa7ad2ea40a926a90c25e
// 37c71afb37a27ea7754a84bd64b36e4967fa0f2f6d7b436e711bd6e941a820ec
// e0c5a7be8b0b6805f43aa8a01bed1cf4f63ca840d459e9f19d2d3eec2f5fef9f
// cafae62d88ed07976adf6100db38edda1578ff1197c6ce2d6933d02e89664433
// fixtures_peer1.org1.example.com
// 1190475f08faf3a9af40959598f3541241162ff9c9370a962d7e819c2d302d11
// 25e6d7741b66d63033305469c1916ec78b4cc84cbfec472e03306504ba2c56dc
// c42a6405461f760e0f9c37ff5d5e2197dab1839b8958fdd5c4e067cee08b5b26
// 5496f68a1ea021f6e25a6d36cfde88e7f16176c896df7718e158b3308b325635
// ea2b5b9922ac372eed6c53c1d10c0815b191d3df78cc1c61c65560f2e7a114c0
// 5ac848ce33b571eef37f14542e7f70620b1bfadff1df0381a547e288d1cca811
// fixtures_orderer.example.com
// bb953079bdb09941c977aa24bd7c95664261d9fa31219398a65da55784d20058
// 29b43ea0be91542c3676f773673054aa4d2a10f0cb14690e1b8422feebf9a200
// 57a3e446245350ebb0238f805ea68278fbb42b0b768d58603d7d2e1929eb5e3e
// 7ec666fc600d9fd6c1b05bf4d8f15dcf822b53d8ee3426cf61f63c3959cc0ed8
// 239d88df4403a85a7e4df281dac809bc7cc20c2017a24762e74f305a593a990f
// db62b985432f1b5c25c9741707f2874f6c39b490025de72af1a85f2744fe85d6

// Total reclaimed space: 6.621MB

//然后重新执行即可
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# docker-compose up -d
// Creating network "fixtures_test" with the default driver
// Creating volume "fixtures_orderer.example.com" with default driver
// Creating volume "fixtures_peer0.org1.example.com" with default driver
// Creating volume "fixtures_peer1.org1.example.com" with default driver
// Creating couchdb0               ... done
// Creating orderer.example.com ... done
// Creating ca.org1.example.com    ... done
// Creating couchdb1               ... done
// Creating peer0.org1.example.com ... done
// Creating peer1.org1.example.com ... done
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# cd .. && go build && ./fabric-go-sdk
// >> 开始创建通道......
// >>>> 使用每个org的管理员身份更新锚节点配置...
// >>>> 使用每个org的管理员身份更新锚节点配置完成
// >> 创建通道成功
// >> 加入通道......
// >> 加入通道成功
// >> 开始打包链码......
// >> 打包链码成功
// >> 开始安装链码......
// >> 安装链码成功
// >> 组织认可智能合约定义......
// >>> chaincode approved by Org1 peers:
// 	peer0.org1.example.com:7051
// 	peer1.org1.example.com:9051
// >> 组织认可智能合约定义完成
// >> 检查智能合约是否就绪......
// LifecycleCheckCCCommitReadiness cc = simplecc, = {map[Org1MSP:true]}
// LifecycleCheckCCCommitReadiness cc = simplecc, = {map[Org1MSP:true]}
// >> 智能合约已经就绪
// >> 提交智能合约定义......
// >> 智能合约定义提交完成
// >> 调用智能合约初始化方法......
// >> 完成智能合约初始化
// >> 通过链码外部服务设置链码状态......
// >> 设置链码状态完成
// 2022/06/24 22:52:45 Registered block event
// 2022/06/24 22:52:45 Receive block event:
// SourceURL: peer1.org1.example.com:9051
// Number: 4
// Hash: 8ede443ca1d7f86876013c4fdf0234eff76a1dc56c1f2fe9eea499b54eb08af2
// PreviousHash: 4aa24438714330d4f8b3cef26de377e832bc04054f2ecdf6dd122a1eeb5825bc

// <--- 添加信息 --->: 5cc98204568565659cd75e31e3a7d914948f41c31ed6d8abc85ae193da49b25b
// 2022/06/24 22:52:47 Receive cc event, ccid: simplecc 
// eventName: chaincode-event
// payload: {"EventName":"set"} 
// txid: 5cc98204568565659cd75e31e3a7d914948f41c31ed6d8abc85ae193da49b25b 
// block: 5 
// sourceURL: peer1.org1.example.com:9051
// 2022/06/24 22:52:47 Receive block event:
// SourceURL: peer1.org1.example.com:9051
// Number: 5
// Hash: 860e733399828b968d10eb8c0340bc3d65c1f81a62e68360b0bfa63f16e0ed56
// PreviousHash: d3c792f39e898d208b56716657905f53f3165111ed2e2f6e5f424987d45d524d

// 2022/06/24 22:52:50 Receive cc event, ccid: simplecc 
// eventName: chaincode-event
// payload: {"EventName":"set"} 
// txid: 125e362311b7d4d32e1a59ace998cedff21805fc739bf46c181517a4775e9e45 
// block: 6 
// sourceURL: peer1.org1.example.com:9051
// 2022/06/24 22:52:50 Receive block event:
// SourceURL: peer1.org1.example.com:9051
// Number: 6
// Hash: 0cf8a430a843bb897113a5252a0bf891e66b24ea8ff12c9667c167ceec8079b1
// PreviousHash: df4fbcf3a78666fc83a1af85c0bfbb227692f40c22d6a356b0c05693b0272a2d

// <--- 添加信息 --->: 125e362311b7d4d32e1a59ace998cedff21805fc739bf46c181517a4775e9e45
// 2022/06/24 22:52:52 Receive cc event, ccid: simplecc 
// eventName: chaincode-event
// payload: {"EventName":"set"} 
// txid: 82d5b92c2235bddcd25d68d8bcd81402c187bb411b47ca8a6b5181e17c4039d1 
// block: 7 
// sourceURL: peer1.org1.example.com:9051
// 2022/06/24 22:52:52 Receive block event:
// SourceURL: peer1.org1.example.com:9051
// Number: 7
// Hash: cab2cd31665d0dbab1b7230d38a1101c64c34438fc54108fa22b79b2bd4d5745
// PreviousHash: 49e6070dc24887baf2f5244eac560543d0153a406d7592b14def05c08ba3a1aa

// <--- 添加信息 --->: 82d5b92c2235bddcd25d68d8bcd81402c187bb411b47ca8a6b5181e17c4039d1
// <--- 查询信息 --->: 789

1、简单介绍

软件开发工具包(外语首字母缩写:SDK、外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包、软件框架、硬件平台、 *** 作系统等建立应用软件时的开发工具的集合。软件开发工具包括广义上指辅助开发某一类软件的相关文档、范例和工具的集合。软件开发工具包是一些被软件工程师用于为特定的软件包、软件框架、硬件平台、 *** 作系统等创建应用软件的开发工具的集合,一般而言SDK即开发 Windows 平台下的应用程序所使用的 SDK。它可以简单的为某个程序设计语言提供应用程序接口 API 的一些文件,但也可能包括能与某种嵌入式系统通讯的复杂的硬件。一般的工具包括用于调试和其他用途的实用工具。SDK 还经常包括示例代码、支持性的技术注解或者其他的为基本参考资料澄清疑点的支持文档。为了鼓励开发者使用其系统或者语言,许多 SDK 是免费提供的。
Fabric的Peer节点和Orderer节点都提供了基于GRPC协议(Google开发的远程过程调用RPC)的接口,通过这些接口可以和Peer节点与Orderer节点进行命令/数据交互,为了简化开发,官方提供了多语言版本的SDK。Golang是Fabric原生的开发语言,Fabric,Fabric-ca,Chaincode都是采用Golang开发的,本文关注Fabric Go SDK。
fabric-go-sdk官方网址为https://github.com/hyperledger/fabric-sdk-go
其目录结中pkg目录是fabric-go-sdk的主要实现,internel目录和third_party目录包含了fabric-go-sdk依赖的一些代码。

1)pkg目录包含了以下这些软件包:
pkg/fabsdk:Fabric SDK的主软件包。此程序包使您可以基于配置创建上下文。这些上下文由下面列出的客户端软件包使用。参考:​
pkg/client/channel:提供通道交易功能。​
pkg/client/event:提供通道事件功能。
pkg/client/ledger:启用对渠道基础分类账的查询。​
pkg/client/resmgmt:提供资源管理功能,例如安装链码。
pkg/client/msp:启用身份管理功能。​

(2)基本使用流程:
使用配置实例化fabsdk实例。 注意:fabsdk维护高速缓存,因此您应最小化fabsdk本身的实例。
使用fabsdk实例基于用户和组织创建上下文。 注意:通道上下文还需要通道ID。
使用其New函数创建一个客户端实例,并传递上下文。 注意:您需要为每个上下文创建一个新的客户端实例。
使用每个客户提供的功能来创建解决方案。
调用fabsdk.Close()释放资源和缓存。
2、上述实例的fixture目录结构介绍

这个能运行的实例代码目录齐全的,但如果我们自己编写的话,只需要编写三个配置文件,另外两个目录可以用命令生成。

证书配置文件

在$GOPATH/src目录下创建一个名为fabric-sdk-go的文件夹做为项目根目录,在此目录下创建名为fixtures的文件夹存放我们网络相关配置文件,在文件夹中创建一个名为crypto-config.yaml的文件。
然后使用以下命令生成证书文件,存到crypto-config目录下:

cryptogen generate --config=crypto-config.yaml

问题记录:
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# cryptogen generate --config=crypto-config.yaml
cryptogen: command not found
解决:
root@gan-virtual-machine:~/go/src/fabric-go-sdk/fixtures# /opt/fabric-2.2.5/scripts/fabric-samples/bin/cryptogen generate --config=crypto-config.yaml
org1.example.com
org2.example.com

通道配置文件

在fixtures路径下创建一个名为configtx.yaml的文件。
(1)生成创始块文件,使用以下命令在当前目录下的channel-artifacts目录下得到一个文件genesis.block

/opt/fabric-2.2.5/scripts/fabric-samples/bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block -channelID fabric-channel

(2)生成通道文件,使用以下命令在当前目录下的channel-artifacts目录下得到一个文件channel.tx

/opt/fabric-2.2.5/scripts/fabric-samples/bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

(3)生成组织一锚节点更新文件,使用以下命令在当前目录下的channel-artifacts目录下得到一个文件Org1MSPanchors.tx。

/opt/fabric-2.2.5/scripts/fabric-samples/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

(4)生成组织二锚节点更新文件,使用以下命令在当前目录下的channel-artifacts目录下得到一个文件Org2MSPanchors.tx。

/opt/fabric-2.2.5/scripts/fabric-samples/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

(5)使用ls命令查看刚刚生成的文件。

ls channel-artifacts
channel.tx  Org1MSPanchors.tx  Org1MSPanchors.tx  genesis.block
准备docker-compose文件(网络配置文件)

在fixtures路径下创建一个名为docker-compose.yaml的文件。
这个文件是用docker-compose命令启动和关闭网络时需要用到的配置文件。

3、上述实例的项目根目录结构介绍

配置文件config.yaml

客户端使用sdk与fabric网络交互,需要告诉sdk两类信息:

我是谁:即当前客户端的信息,包含所属组织、密钥和证书文件的路径等, 这是每个客户端专用的信息。
对方是谁:即fabric网络结构的信息,channel、org、orderer和peer等 的怎么组合起当前fabric网络的,这些结构信息应当与configytx.yaml中是一致的。这是通用配置,每个客户端都可以拿来使用。另外,这部分信息并不需要是完整fabric网络信息,如果当前客户端只和部分节点交互,那配置文件中只需要包含所使用到的网络信息。

sdkInit目录

在项目路径下新建名为sdkInit的文件夹,此文件夹用于存放实现sdk的代码。
初始化与加入通道
在sdkInit路径下
新建sdkInfo.go:定义所需结构体。
新建sdkSetting.go:
(1)初始化,"github.com/hyperledger/fabric-sdk-go/pkg/core/config"获取所需配置文件,“github.com/hyperledger/fabric-sdk-go/pkg/fabsdk” ,fabsdk包允许客户端使用Hyperledger Fabric网络。
(2)调用创建通道函数及加入通道。

链码生命周期
链码运行在一个隔离于背书peer节点进程的安全的Docker容器中。链码通过应用提交的交易来初始化以及管理账本状态。从hyperledger fabric v2.0版本开始启用了新的链码生命周期,Fabric 链码生命周期是一个过程,它允许多个组织在使用一个链码之前就如何 *** 作达成一致。Fabric链码生命周期需要组织同意定义一个链码的参数,比如说名称、版本以及链码背书策略。通道成员通过以下四步达成共识。不是通道上的每一个组织都需要完成每一步。

打包链码:这一步可以被一个或者每一个组织完成。
安装链码在你的 peer 节点上:每一个用链码的组织需要完成这一步。
为你的组织批准链码定义:使用链码的每一个组织需要完成这一步。链码能够在通道上运行之前,链码定义需要被足够多的组织批准来满足通道的生命周期背书(LifecycleEndorsement)策略(默认为大多数组织)。
提交链码定义到链上:一旦通道上所需数量的组织已经同意,提交交易需要被提交。提交者首先从已同意组织中的足够的peer节点中收集背书,然后通过提交交易来提交链码声明。

在sdkInit路径下
新建一个名为intergration.go的文件:其中DiscoverLocalPeers方法可以自动查找的所有节点。剩下的方法实现了链码生命周期。

4、main方法

在项目的根目录下新建一个名为main.go的文件,为项目的主函数。在这里实现将组织通道等相关信息实例化,以及调用前面的函数实现创建通道加入通道将链码实例化。

5、添加链码文件

在项目根目录下新建一个名为chaincode的文件夹,在chaincode目录下添加一个名为sacc.go的智能合约。

6、项目启动

在命令行中进入chaincode路径,并使用以下命令为链码添加依赖包。

go mod init
go mod vendor

在命令行中进入项目根目录,并使用以下命令为项目添加依赖包。

go mod init
go mod tidy

在命令行中进入fixture路径,并使用以下命令启动网络。

docker-compose up -d

在命令行中进入项目根目录,并使用以下命令build整个项目。

go build

接着使用以下命令运行项目。

./fabric-go-sdk

运行成功后输出:

>> 开始创建通道......
>>>> 使用每个org的管理员身份更新锚节点配置...
>>>> 使用每个org的管理员身份更新锚节点配置完成
>> 创建通道成功
>> 加入通道......
>> 加入通道成功
>> 开始打包链码......
>> 打包链码成功
>> 开始安装链码......
>> 安装链码成功
>> 组织认可智能合约定义......
>>> chaincode approved by Org1 peers:
        peer0.org1.example.com:7051
>>> chaincode approved by Org2 peers:
        peer0.org2.example.com:9051
>> 组织认可智能合约定义完成
>> 检查智能合约是否就绪......
LifecycleCheckCCCommitReadiness cc = simplecc, = {map[Org1MSP:true]}
LifecycleCheckCCCommitReadiness cc = simplecc, = {map[Org2MSP:true]}
>> 智能合约已经就绪
>> 提交智能合约定义......
>> 智能合约定义提交完成
>> 调用智能合约初始化方法......
>> 完成智能合约初始化
>> 通过链码外部服务设置链码状态......
docker相关命令
prune 命令用来删除不再使用的 docker 对象。
删除所有未被 tag 标记和未被容器使用的镜像:docker image prune
删除所有未被容器使用的镜像:docker image prune -a
删除所有停止运行的容器:docker container prune
删除所有未被挂载的卷:docker volume prune
删除所有网络:docker network prune
删除 docker 所有资源:docker system prune
题外补充,一个很好用的工具—— 安装NotePad++

官网:https://notepad-plus-plus.org/
需要搭梯子才可以哦~不然好像打不开这个网站!下载好之后就下一步下一步就安装好了!这个工具好像挺好用的。和记事本差不多但是比记事本高级些,哈哈哈。

补充小技能:Ubuntu下查看二进制文件
//加上-b参数,以二进制打开
vim -b 【想查看的二进制文件名】
//然后在vim打开的界面输入下面的命令 ,切换到十六进制模式显示
:%!xxd -g 1  

可以看到十六进制模式显示如下:

Fabric go sdk开发入门参考博客:https://developer.aliyun.com/article/750371以及https://www.cnblogs.com/liuhui5599/p/14195513.html

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/2991886.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-23
下一篇 2022-09-23

发表评论

登录后才能评论

评论列表(0条)

保存