0

I'm testing below chaincode using first network setup and fabric:v1.4

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "strconv"

    "github.com/hyperledger/fabric/core/chaincode/shim"
    peer "github.com/hyperledger/fabric/protos/peer"
    shell "github.com/ipfs/go-ipfs-api"

)


// SimpleAsset implements a simple chaincode to manage an asset
type SimpleAsset struct {
}

func (t *SimpleAsset) Init(APIstub shim.ChaincodeStubInterface) peer.Response {
    return shim.Success(nil)
}

func (t *SimpleAsset) Invoke(APIstub shim.ChaincodeStubInterface) peer.Response {
    // Extract the function and args from the transaction proposal
    fn, args := APIstub.GetFunctionAndParameters()

    var result string
    var err error

    if fn == "set-to-ipfs" {

        result, err = setToIpfs(APIstub, args)

    } else if fn == "get-from-ipfs" {

        result, err = getFromIpfs(APIstub, args)
    }

    if err != nil {
        return shim.Error(err.Error())
    }

    return shim.Success([]byte(result))
}

func setToIpfs(APIstub shim.ChaincodeStubInterface, args []string) (string, error) {

    if len(args) != 1 {
        return "", fmt.Errorf("Incorrect arguments. Expecting a value")
    }

    sh := shell.NewShell("ipfs_host:5001")

    // ipfs add
    cid, err := sh.Add(strings.NewReader(args[0]))

    if err != nil {
        fmt.Fprintf(os.Stderr, "error: %s", err)
        return "", err
    }

    // show hash
    fmt.Println("added ", cid)

    if err != nil {
        return "", fmt.Errorf("Failed to set asset: %s", args[0])
    }

    return cid, nil
}

func getFromIpfs(APIstub shim.ChaincodeStubInterface, args []string) (string, error) {

    if len(args) != 1 {
        return "", fmt.Errorf("Incorrect arguments. Expecting a hash")
    }

    sh := shell.NewShell("ipfs_host:5001")

    hashKey := args[0]

    // ipfs cat
    catResult, err := sh.Cat(hashKey)
    defer catResult.Close()

    if err != nil {
        fmt.Fprintf(os.Stderr, "error: %s", err)
        return "", err
    }

    // read string from buffer
    buf := new(bytes.Buffer)
    buf.ReadFrom(catResult)

    message := buf.String()

    return message, 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)
    }
}

But when I tried to execute the above chaincode on first network and on docker-devmode(can't even build) but then I receive below error:

cannot find package github.com/hyperledger/fabric/core/chaincode/shim in any of: /usr/local/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOROOT) /go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOPATH) ipfsfabric.go:29:5: cannot find package "github.com/hyperledger/fabric/protos/peer" in any of: /usr/local/go/src/github.com/hyperledger/fabric/protos/peer (from $GOROOT) /go/src/github.com/github.com/hyperledger/fabric/protos/peer (from $GOPATH) can't load package: package github.com/ipfs/go-ipfs-api: cannot find package "github.com/ipfs/go-ipfs-api" in any of: /opt/go/src/github.com/ipfs/go-ipfs-api (from $GOROOT) /opt/gopath/src/github.com/ipfs/go-ipfs-api (from $GOPATH)

commands to get the packages

go get -u github.com/ipfs/go-ipfs-api
go get -u github.com/hyperledger/fabric/protos/peer
go get -u github.com/hyperledger/fabric/core/chaincode/shim

When I installed go I added it in ~./profile file:

snippet from file

export PATH=$PATH:/usr/local/go/bin

go version

go version go1.14.8 linux/amd64
HectorCode
  • 205
  • 2
  • 11
  • go to chaincode directory and try to vendor it. run `go mod init` inside `chaincode` directory and then `go mode vendor` – metadata Sep 07 '20 at 04:37
  • its `go mod vendor` not `go mode vendor` – metadata Sep 07 '20 at 07:10
  • @alpha when i execute **go mod vendor** the output is this: github.com/hyperledger/fabric/core/chaincode/shim: module github.com/hyperledger/fabric@latest found (v2.1.1+incompatible), but does not contain package github.com/hyperledger/fabric/core/chaincode/shim github.com/ipfs/go-ipfs-api imports github.com/hyperledger/fabric/protos/peer: module github.com/hyperledger/fabric@latest found (v2.1.1+incompatible), but does not contain package github.com/hyperledger/fabric/protos/peer – HectorCode Sep 07 '20 at 08:29
  • can you share `go.mod` file? – metadata Sep 07 '20 at 09:10
  • also `shim` package moved to `"github.com/hyperledger/fabric-chaincode-go/shim"` and `peer` moved to `"github.com/hyperledger/fabric-protos-go/peer"` – metadata Sep 07 '20 at 09:14
  • While close, none of these are technically accurate, you can use modules to do this, but `1.4` fabric doesn't support modules, also dont update the shim and protos location, that is only for 2.x fabric. So the command is `GO111MODULE=on go mod init && GO111MODULE=on go get github.com/hyperledger/fabric@11cbae972e223b3545bc2e87d1842d055a95b908 && GO111MODULE=on go mod vendor`. You had to pull fabric in on a specific 1.4 commit (chose latest) since 1.4 didn't support modules. Just doing `go mod init && go mod vendor` isn't enough as it pulls in the latest fabric release (not what you wanted). – lindluni Sep 08 '20 at 03:32

0 Answers0