首先简单介绍一下此次流程步骤,在go build阶段出现问题,然后说明一下如何解决的问题。 出现的问题的提示代码为: 问题1、go: github.com/hyperledger/fabric-chaincode-go@v0.0.0-20190823162523-04390e015b85: Get "https://proxy.golang.org/github.com/hyperledger/fabric-chaincode-go/@v/v0.0.0-20190823162523-04390e015b85.mod": dial tcp 216.58.200.49:443: connect: connection refused
解决方案1:执行以下命令 $ go env -w GO111MODULE=on $ go env -w GOPROXY=https://goproxy.io,direct 然后再执行go build,不再出现此类错误
问题2:go build github.com/hyperledger/fabric-samples/chaincode/sacc: copying /tmp/go-build462413933/b001/exe/a.out: open sacc: permission denied
解决方案2: 经分析,应该是权限问题,尝试很多办法,包括更改文件夹权限777等,最终发现更改文件夹权限无法作用到docker容器中,所以采用直接简单的一种方法,那就是以root身份进入容器执行各种操作。进入chaincode容器的指令如下:$ docker exec -it -u root chaincode sh
问题3:Error starting SimpleAsset chaincode: flag 'peer.address' must be set
解决方案3:尚未解决,各位大佬如果知道解决方案请留言
1、在$GOPATH下的src文件夹下建立一个sacc文件,并创建一个sacc.go文件进行chaincode编码
$ mkdir -p $GOPATH/src/sacc && cd $GOPATH/src/sacc $ touch sacc.go2、在sacc.go中进行编码,编写Init、Invoke、set、get等函数,进行整合。具体的代码如下:
package main import( "fmt" "github.com/hyperledger/fabric-chaincode-go/shim" "github.com/hyperledger/fabric-protos-go/peer" ) // SimpleAsset implements a simple chaincode to manage an asset type SimpleAsset struct { } // Init is called during chaincode Instantiation to initialize any data // Note that chaincodeee upgrade also calls this function to reset or to migrate data, // so be careful to avoid a scenario where you inadvertently clobber your ledger's data func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response{ // Get the args from the transaction proposal args := stub.GetStringArgs() if len(args) != 2 { return shim.Error("Incorrect arguments. Expection a key and a value...") } // Set up any variables or assets here by calling stub.PutStata() // We store the key and the value on the ledger err := stub.PutState(args[0], []byte(args[1])) if err != nil { return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0])) } return shim.Success(nil) } // Invoke is called per transaction on the chaincode. Each transaction is either a 'get' // or a 'set' on the asset created by Init function. The 'set' method may create a new // asset by specifying a new key-value pair. func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response{ // Extract the function and args from the transaction proposal fn, args := stub.GetFunctionAndParameters() var result string var err error if fn == "set" { result, err = set(stub, args) }else{ result, err = get(stub, args) } if err != nil { return shim.Error(err.Error()) } // Return the result as success payload return shim.Success([]byte(result)) } // Set stores the asset (both key and value) on the ledger. If the key exists, // it will override the value with the new one func set(stub shim.ChaincodeStubInterface, args []string) (string, error){ if len(args) != 2 { return "", fmt.Errorf("Incorrect arguments. Expecting a key and a value") } err := stub.PutState(args[0], []byte(args[1])) if err != nil { return "", fmt.Errorf("Failed to set asset: %s", args[0]) } return args[1], nil } // Get returns the value of the specified asset key func get(stub shim.ChaincodeStubInterface, args []string) (string, error){ if len(args) != 1 { return "", fmt.Errorf("Incorrect arguments. Expecting a key and a value") } value, err := stub.GetState(args[0]) if err != nil { return "", fmt.Errorf("Failed to set asset: %s with error: %s", args[0], err) } if value == nil { return "", fmt.Errorf("Asset not found: %s", args[0]) } return string(value), nil } // main function starts up the chaincode in the container during instantiate func main() { err := shim.Start(new(SimpleAsset)) if err != nil { fmt.Printf("Error starting SimpleAsset chaincode: %s", err) } }3、编译chaincode
$ go get -u --tags nopkcs11 github.com/hyperledger/fabric-chaincode-go/shim $ go build --tags nopkcs114、在开发模式下进行测试(在/fabric-samples/chaincode-docker-devmode目录下启动3个终端)
(tip:需要先安装fabric-samples, 在我之前博客中有篇专门介绍如何安装)
$ cd hyerledger/fabric-samples/chaincode-docker-devmode终端1:
$ docker-compose -f docker-compose-simple.yaml up终端2:
$ docker exec -it chaincode sh /opt/gopath/src/chaincode $ cd sacc /opt/gopath/src/chaincode/sacc $ go build当执行到这里出现错误:go: github.com/hyperledger/fabric-chaincode-go@v0.0.0-20190823162523-04390e015b85: Get "https://proxy.golang.org/github.com/hyperledger/fabric-chaincode-go/@v/v0.0.0-20190823162523-04390e015b85.mod": dial tcp 216.58.200.49:443: connect: connection refused 解决方法参考上述解决方案1,截图 如下 : 上面报的443错误已解决,但是go build后出现新的错误:go build github.com/hyperledger/fabric-samples/chaincode/sacc: copying /tmp/go-build462413933/b001/exe/a.out: open sacc: permission denied 经分析,应该是权限问题,尝试很多办法,包括更改文件夹权限777等,最终发现更改文件夹权限无法作用到docker容器中,所以采用直接简单的一种方法,那就是以root身份进入容器执行各种操作。
更改终端2指令(最终版)如下:
$ docker exec -it -u root chaincode sh /opt/gopath/src/chaincode $ cd sacc /opt/gopath/src/chaincode/sacc $ go env -w GO111MODULE=on /opt/gopath/src/chaincode/sacc $ go env -w GOPROXY=https://goproxy.io,direct /opt/gopath/src/chaincode/sacc $ go build /opt/gopath/src/chaincode/sacc $ CORE_PEEER_ADDRESS=peer:7051 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc又出现了错误:Error starting SimpleAsset chaincode: flag 'peer.address' must be set 尚未解决
终端3:
$ docker exec -it cli bash $ peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0 $ peer chaincode instantiate -n mycc -v 0 -c '{"Args":["a","10"]}' -C myc $ peer chaincode invoke -n mycc -v 0 -c '{"Args":["set","a","20"]}' -C myc $ peer chaincode invoke -n mycc -v 0 -c '{"Args":["query","a"]}' -C myc