Shitaibin / shitaibin.github.io

博客:Go语言、区块链
http://lessisbetter.site
67 stars 21 forks source link

使用fabric-sdk-go操作链码 | Go语言充电站 #40

Open Shitaibin opened 5 years ago

Shitaibin commented 5 years ago

http://lessisbetter.site/2019/09/02/fabric-sdk-go-chaincode/

前言本文把与fabric网络交互的baas、应用程序、客户端统称成为客户端,它们可以使用sdk和fabric网络进行交互,sdk调用grpc可以跟指定的peer和orderer进行通信。 fabric-sdk-go项目简介fabric-sdk-go是Fabric官方的Go语言SDK,它的目录结构如下: 有2个目录需要注意一下,internal和third_party,它们两个包含了sdk依赖的

shuaizhang66 commented 4 years ago

我找了很久 不知道如何完成添加组织的操作

shuaizhang66 commented 4 years ago

您好,您开源的项目我看了,写的很不错。但是想请问一下,动态添加组织的接口,fabric-sdk-go有提供吗?

Shitaibin commented 4 years ago

@shuailaotou 您好,您开源的项目我看了,写的很不错。但是想请问一下,动态添加组织的接口,fabric-sdk-go有提供吗?

这个我还没用过,组织的增加属于配置,建议研究下修改channel配置的接口: https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt#Client.SaveChannel

iceriverdog commented 4 years ago

@shuailaotou 我找了很久 不知道如何完成添加组织的操作 你好,我注册 user,您有资料吗?我搞了几天了没弄成。谢谢

iceriverdog commented 4 years ago

@Shitaibin

@shuailaotou 您好,您开源的项目我看了,写的很不错。但是想请问一下,动态添加组织的接口,fabric-sdk-go有提供吗?

这个我还没用过,组织的增加属于配置,建议研究下修改channel配置的接口: https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt#Client.SaveChannel

老板,后续还有其他功能的加入吗?或者有资料分享吗?我想注册用户和组织。谢谢

Shitaibin commented 4 years ago

@iceriverdog

@shuailaotou 我找了很久 不知道如何完成添加组织的操作 你好,我注册 user,您有资料吗?我搞了几天了没弄成。谢谢

加入组织是需要多个步骤的,可以直接参考fabric中文文档,过程已经很清晰了:https://hyperledger-fabric.readthedocs.io/zh_CN/release-1.4/channel_update_tutorial.html?highlight=%E5%8A%A0%E5%85%A5%E7%BB%84%E7%BB%87

需要使用configtxlator、jq等命令行工具生成加入组织的配置块,再使用SDK的SaveChannel接口

hehui0316 commented 4 years ago

请问能分享main.go文件吗?

Shitaibin commented 4 years ago

@hehui0316

请问能分享main.go文件吗?

https://github.com/Shitaibin/fabric-sdk-go-sample

iceriverdog commented 4 years ago

楼主您好,怎么用 sdk go 进行富查询呢?我想查一个用户的资产,用selector,我cc name 是正确的,但是显示 Transaction processing for endorser [localhost:7051]: Chaincode status Code: (500) UNKNOWN. Description: Invalid Smart Contract function name. 我去看了这个错误不是来自 合约里面的 name 判断。

hehui0316 commented 4 years ago

@Shitaibin @hehui0316

请问能分享main.go文件吗?

https://github.com/Shitaibin/fabric-sdk-go-sample

每次执行 gobuild的时候出现 ubuntu@VM-0-5-ubuntu:~/go/src/github.com/hyperledger/fabric-sdk-go-sample$ go run main.go stat main.go: no such file or directory

Shitaibin commented 4 years ago

@hehui0316

样例更新了,看仓库最新的README,样例在samples目录,有链码和事件2个样例。

Shitaibin commented 4 years ago

楼主您好,怎么用 sdk go 进行富查询呢?我想查一个用户的资产,用selector,我cc name 是正确的,但是显示 Transaction processing for endorser [localhost:7051]: Chaincode status Code: (500) UNKNOWN. Description: Invalid Smart Contract function name. 我去看了这个错误不是来自 合约里面的 name 判断。

与你的链码有关,这个不是fabric和sdk的代码,检查链码invoke中的函数,是否有你传递的函数名

hehui0316 commented 4 years ago

@Shitaibin @hehui0316

样例更新了,看仓库最新的README,样例在samples目录,有链码和事件2个样例。 还是有问题 [fabsdk/fab] 2020/07/28 09:15:23 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:15:23 Instantiated chaincode error: instantiate chaincode error: error registering for TxStatus event: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] [fabsdk/fab] 2020/07/28 09:15:33 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] [fabsdk] 2020/07/28 09:15:33 UTC - chpvdr.(*EventClientRef).Unregister -> WARN Error unregistering event registration: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051]

Shitaibin commented 4 years ago

@hehui0316 你fabric-samples项目的BFYN启动了吗?

hehui0316 commented 4 years ago

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

启动了

hehui0316 commented 4 years ago

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

这个是完整的报错ig/overrides/local_entity_matchers.yaml for samples 2020/07/28 17:48:04 Initialized fabric sdk 2020/07/28 17:48:05 Initialized resource client 2020/07/28 17:48:05 Initialized channel client [fabsdk/fab] 2020/07/28 09:48:05 UTC - fab.detectDeprecatedNetworkConfig -> WARN Getting orderers from endpoint config channels.orderer is deprecated, use entity matchers to override orderer configuration [fabsdk/fab] 2020/07/28 09:48:05 UTC - fab.detectDeprecatedNetworkConfig -> WARN visit https://github.com/hyperledger/fabric-sdk-go/blob/master/test/fixtures/config/overrides/local_entity_matchers.yaml for samples 2020/07/28 17:48:05 Initialized fabric sdk 2020/07/28 17:48:05 Initialized resource client 2020/07/28 17:48:05 Initialized channel client [fabsdk/fab] 2020/07/28 09:48:15 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:15 Register block event error: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:15 Registered block event [fabsdk/fab] 2020/07/28 09:48:25 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:25 Register filtered block event error: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:25 Registered filtered block event 2020/07/28 17:48:25 Listen chaincode event: . 2020/07/28 17:48:25 Using event client to register chaincode event 2020/07/28 17:48:25 Transaction listener start [fabsdk/fab] 2020/07/28 09:48:35 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:35 Register chaincode event error: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:35 Install response status: 200 2020/07/28 17:48:35 Chaincode has been installed on org1's peers [fabsdk/fab] 2020/07/28 09:48:45 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] 2020/07/28 17:48:45 Instantiated chaincode error: instantiate chaincode error: error registering for TxStatus event: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] [fabsdk/fab] 2020/07/28 09:48:55 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer1.org1.example.com:7051: dialing connection timed out [peer1.org1.example.com:7051] [fabsdk] 2020/07/28 09:48:55 UTC - chpvdr.(EventClientRef).Unregister -> WARN Error unregistering event registration: could not create client conn: could not connect to peer1.org1.example.com:7051: dialing connection timed out [peer1.org1.example.com:7051] [fabsdk/fab] 2020/07/28 09:49:05 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] [fabsdk] 2020/07/28 09:49:05 UTC - chpvdr.(EventClientRef).Unregister -> WARN Error unregistering event registration: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] [fabsdk/fab] 2020/07/28 09:49:15 UTC - dispatcher.(Dispatcher).HandleConnectEvent -> WARN error creating connection: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] [fabsdk] 2020/07/28 09:49:15 UTC - chpvdr.(*EventClientRef).Unregister -> WARN Error unregistering event registration: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051] panic: Instantiated chaincode error: instantiate chaincode error: error registering for TxStatus event: could not create client conn: could not connect to peer0.org1.example.com:7051: dialing connection timed out [peer0.org1.example.com:7051]

goroutine 1 [running]: log.Panicf(0xd473ca, 0x20, 0xc0004dfe38, 0x1, 0x1) /usr/local/go/src/log/log.go:358 +0xc0 main.DoChainCode(0xc000123cc0, 0xc0004fe1e0) /home/ubuntu/go/src/github.com/hyperledger/fabric-sdk-go-sample/samples/event/main.go:187 +0x1a9 main.main() /home/ubuntu/go/src/github.com/hyperledger/fabric-sdk-go-sample/samples/event/main.go:55 +0x405 exit status 2

Shitaibin commented 4 years ago

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

启动了

我刚跑了一下,只发现配置文件路径错误,修复只有2个case都可以跑。

本机情况下,网络连通性不是问题,并且sdk的配置文件中也对各节点的域名进行了映射。

检查一下你的环境配置吧,peer、order是否正常,看看有没有其他原因

hehui0316 commented 4 years ago

@Shitaibin

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

启动了

我刚跑了一下,只发现配置文件路径错误,修复只有2个case都可以跑。

本机情况下,网络连通性不是问题,并且sdk的配置文件中也对各节点的域名进行了映射。

检查一下你的环境配置吧,peer、order是否正常,看看有没有其他原因

是不是因为bfyn已经装chaincode安装上了,再装同一个chaincode会有问题呀?

hehui0316 commented 4 years ago

@Shitaibin

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

启动了

我刚跑了一下,只发现配置文件路径错误,修复只有2个case都可以跑。

本机情况下,网络连通性不是问题,并且sdk的配置文件中也对各节点的域名进行了映射。

检查一下你的环境配置吧,peer、order是否正常,看看有没有其他原因

感觉像是端口占用了

Shitaibin commented 4 years ago

@Shitaibin

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

启动了

我刚跑了一下,只发现配置文件路径错误,修复只有2个case都可以跑。 本机情况下,网络连通性不是问题,并且sdk的配置文件中也对各节点的域名进行了映射。 检查一下你的环境配置吧,peer、order是否正常,看看有没有其他原因

是不是因为bfyn已经装chaincode安装上了,再装同一个chaincode会有问题呀?

会是不同的错误,你这个报的是网络问题

hehui0316 commented 4 years ago

@Shitaibin

@Shitaibin

@Shitaibin @hehui0316 你fabric-samples项目的BFYN启动了吗?

启动了

我刚跑了一下,只发现配置文件路径错误,修复只有2个case都可以跑。 本机情况下,网络连通性不是问题,并且sdk的配置文件中也对各节点的域名进行了映射。 检查一下你的环境配置吧,peer、order是否正常,看看有没有其他原因

是不是因为bfyn已经装chaincode安装上了,再装同一个chaincode会有问题呀?

会是不同的错误,你这个报的是网络问题 但是peer和order都启动了呀,请问能不能留个联系方式,方便咨询

hehui0316 commented 4 years ago

[fabsdk/fab] 2020/07/29 02:15:23 UTC - fab.detectDeprecatedNetworkConfig -> WARN Getting orderers from endpoint config channels.orderer is deprecated, use entity matchers to override orderer configuration [fabsdk/fab] 2020/07/29 02:15:23 UTC - fab.detectDeprecatedNetworkConfig -> WARN visit https://github.com/hyperledger/fabric-sdk-go/blob/master/test/fixtures/config/overrides/local_entity_matchers.yaml for samples 2020/07/29 10:15:23 Initialized fabric sdk 2020/07/29 10:15:23 Initialized resource client 2020/07/29 10:15:23 Initialized channel client [fabsdk/fab] 2020/07/29 02:15:23 UTC - fab.detectDeprecatedNetworkConfig -> WARN Getting orderers from endpoint config channels.orderer is deprecated, use entity matchers to override orderer configuration [fabsdk/fab] 2020/07/29 02:15:23 UTC - fab.detectDeprecatedNetworkConfig -> WARN visit https://github.com/hyperledger/fabric-sdk-go/blob/master/test/fixtures/config/overrides/local_entity_matchers.yaml for samples 2020/07/29 10:15:23 Initialized fabric sdk 2020/07/29 10:15:23 Initialized resource client 2020/07/29 10:15:23 Initialized channel client 2020/07/29 10:15:23 Registered block event 2020/07/29 10:15:23 Registered filtered block event 2020/07/29 10:15:23 Listen chaincode event: .* 2020/07/29 10:15:23 Using event client to register chaincode event 2020/07/29 10:15:23 Transaction listener start 2020/07/29 10:15:23 Receive filterd block event: Number: 4 len(transactions): 1 SourceURL: grpcs://localhost:8051 2020/07/29 10:15:23 tx index 0: type: ENDORSER_TRANSACTION, txid: 1cf4108051e210c845c1f4abeb216461dd058e017e613f5f0bf288bfbbb2b807, validation code: VALID 2020/07/29 10:15:23 2020/07/29 10:15:23 Receive block event: SourceURL: grpcs://localhost:8051 Number: 4 Hash: 4fe5de4393105037dae497a0221fac5f28558e382aed79dabbbe188da2680dd5 PreviousHash: d7a5483bdf65ebaedbe133cb54538d7a1da2f6242941d63a849e50ef83b5d5a3

2020/07/29 10:15:25 Invoke chaincode response: id: 37ec1d620bd557b8f54eaf6fe59278c17de39ba0c6845249d6d37f63d13e9e8d validate: ENDORSEMENT_POLICY_FAILURE chaincode status: 200

2020/07/29 10:15:25 Invoke chaincode error: invoke chaincode error: Event Server Status Code: (10) ENDORSEMENT_POLICY_FAILURE. Description: received invalid transaction panic: Invoke chaincode error: invoke chaincode error: Event Server Status Code: (10) ENDORSEMENT_POLICY_FAILURE. Description: received invalid transaction

goroutine 1 [running]: log.Panicf(0xd3ea9e, 0x1a, 0xc0004e5e38, 0x1, 0x1) /usr/local/go/src/log/log.go:358 +0xc0 请问这个怎么解决呀

Shitaibin commented 4 years ago

@hehui0316 [fabsdk/fab] 2020/07/29 02:15:23 UTC - fab.detectDeprecatedNetworkConfig -> WARN Getting orderers from endpoint config channels.orderer is deprecated, use entity matchers to override orderer configuration ..... goroutine 1 [running]: log.Panicf(0xd3ea9e, 0x1a, 0xc0004e5e38, 0x1, 0x1) /usr/local/go/src/log/log.go:358 +0xc0 请问这个怎么解决呀

  1. 看调用栈选择问题原因
  2. 看fabric代码,搞清楚报错ENDORSEMENT_POLICY_FAILURE是什么含义,什么情况下会报错