Skip to content
This repository has been archived by the owner on Mar 30, 2018. It is now read-only.

Can't build chaincode that uses Go packages used by shim #1832

Open
dubek opened this issue Jun 14, 2016 · 2 comments
Open

Can't build chaincode that uses Go packages used by shim #1832

dubek opened this issue Jun 14, 2016 · 2 comments

Comments

@dubek
Copy link
Contributor

dubek commented Jun 14, 2016

Description

I'm developing a chaincode program in my own repo (outside the fabric repo). I want to define a private function that accepts a parameter of type gp.Timestamp, where gp is package alias for google/protobuf. If I add vendor/google/protobuf (and its dependency vendor/github.com/golang/protobuf) dir to my repo, I get the following error about the colliding types:

./myexample.go:22: cannot use timestamp (type *"github.com/hyperledger/fabric/vendor/google/protobuf".Timestamp) as type "myorg/myexample/vendor/google/protobuf".Timestamp in argument to processTimetstamp

The reason here is that I'm importing shim as a library, but Go libraries should not have a vendor dir; but fabric has a vendor dir because it is also an executable.

A possible solution would be to create a new hyperledger/fabric-chaincode-shim project which includes only the shim package, without vendor. Then chaincode programs should only import that package. It might also save some space for the chaincode docker containers.

Describe How to Reproduce

Inside the fabric vagrant:

mkdir -p /opt/gopath/src/myorg/myexample
cd /opt/gopath/src/myorg/myexample
mkdir -p vendor/google vendor/github.com/golang
cp -r /opt/gopath/src/github.com/hyperledger/fabric/vendor/google/protobuf vendor/google/
cp -r /opt/gopath/src/github.com/hyperledger/fabric/vendor/github.com/golang/protobuf vendor/github.com/golang/

And put the following content in /opt/gopath/src/myorg/myexample/myexample.go:

package main

import (
    "fmt"
    "github.com/hyperledger/fabric/core/chaincode/shim"
    gp "google/protobuf"
)

type MyChaincode struct {
}

func (t *MyChaincode) Init(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    return nil, nil
}

func processTimetstamp(t gp.Timestamp) {
    fmt.Printf("Timestamp seconds = %v", t.Seconds)
}

func (t *MyChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    timestamp, _ := stub.GetTxTimestamp()
    processTimetstamp(timestamp)
    return nil, nil
}

func (t *MyChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
    return nil, nil
}

func main() {
    err := shim.Start(new(MyChaincode))
    if err != nil {
        fmt.Printf("Error starting: %s", err)
    }
}

Try to build:

cd /opt/gopath/src/myorg/myexample
go build .

# myorg/myexample
./myexample.go:22: cannot use timestamp (type *"github.com/hyperledger/fabric/vendor/google/protobuf".Timestamp) as type "myorg/myexample/vendor/google/protobuf".Timestamp in argument to processTimetstamp

Notes

I can write code that uses stub.GetTxTimestamp() as long as I don't refer to the type gp.Timestamp by name. For example, this compiles OK:

func (t *MyChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
        timestamp, _ := stub.GetTxTimestamp()
        fmt.Printf("Timestamp seconds = %v", timestamp.Seconds)
        return nil, nil
}

Also, I think that it might be better for shim not to expose its protobuf internals. For example, GetTxTimestamp() could return a standard Go time.Time.

@dubek
Copy link
Contributor Author

dubek commented Jun 16, 2016

Relevant discussion in golang-dev mailing list: https://groups.google.com/forum/#!topic/golang-dev/4FfTBfN2YaI (topic started with similar problem affecting the coreos/etcd repo, which like fabric holds both binaries and libraries).

@zamrokk
Copy link

zamrokk commented Feb 8, 2018

I have similar problem. I would like to use the TxTimestamp too

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants