尝试在go中构建Hyperledger-Fabric链码时,如何解决“不允许导入周期”错误?

时间:2019-06-18 14:30:36

标签: hyperledger-fabric hyperledger hyperledger-chaincode

我正在尝试构建此处给出的“简单资产链码”:https://hyperledger-fabric.readthedocs.io/en/release-1.4/chaincode4ade.html

我正在使用Ubuntu 16.04

当我尝试运行时:

ORG

我收到以下错误:

user@ubuntu:~/go/src/sacc$ go get -u github.com/hyperledger/fabric/core/chaincode/shim

代码如下:

import cycle not allowed
package github.com/hyperledger/fabric/core/chaincode/shim
imports container/list
imports runtime
imports internal/cpu
imports runtime

$ GOPATH是/ home / user / go Hyperledger Fabric存储库也被克隆到正确的目录中。

    package main

    import (
    "fmt"

    "github.com/hyperledger/fabric/core/chaincode/shim"
    "github.com/hyperledger/fabric/protos/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 chaincode upgrade also calls this function to reset
    // or to migrate 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. Expecting a key and a value")
    }

    // Set up any variables or assets here by calling stub.PutState()

    // 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 { // assume 'get' even if fn is nil
            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")
    }

    value, err := stub.GetState(args[0])
    if err != nil {
            return "", fmt.Errorf("Failed to get 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() {
    if err := shim.Start(new(SimpleAsset)); err != nil {
            fmt.Printf("Error starting SimpleAsset chaincode: %s", err)
    }
    }

我真的不知道自己在哪里搞砸了。

1 个答案:

答案 0 :(得分:0)

您的SACC软件包在错误的位置。这正在创建导入周期。您的GOPATH中的软件包需要嵌套在一个存储库中,例如GitHub.com。因此,您可以将SACC软件包移到github.com仓库中,或者在src中创建一个新目录,然后将SACC移到那里。