Skip to content

Instantly share code, notes, and snippets.

@mbwhite
Created August 20, 2021 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbwhite/1145129e36851f8ca4448a869d2251dc to your computer and use it in GitHub Desktop.
Save mbwhite/1145129e36851f8ca4448a869d2251dc to your computer and use it in GitHub Desktop.
Package GoLang Fabric Contract for v14 & v2 peer

Scenario: You have a channel that is used withboth 1.4 and v2 Peers. Chaincode needs to be installed on both peers as a CDS file. How to create the CDS file that can be used by both peers?

Taking an example contract from Fabric-Samples, these are the tiles currently in the contract. Note that this MUST have the go.mod, The folder is chaindcode-go

 ls -lart chaincode-go
total 36
-rw-r--r--  1 matthew matthew 16310 Apr 26 08:54 go.sum
-rw-r--r--  1 matthew matthew   420 Apr 26 08:54 go.mod
drwxr-xr-x  3 matthew matthew  4096 Apr 26 08:54 chaincode
-rw-r--r--  1 matthew matthew   530 Apr 26 08:54 assetTransfer.go

Using my current version of go (1.16.3) run

go mod vendor

This will create the vendor folder

ls -lart vendor
total 32
drwxr-xr-x  4 matthew matthew 4096 Aug 20 08:56 ..
drwxr-xr-x 14 matthew matthew 4096 Aug 20 08:56 github.com
drwxr-xr-x  3 matthew matthew 4096 Aug 20 08:56 golang.org
drwxr-xr-x  4 matthew matthew 4096 Aug 20 08:56 google.golang.org
-rw-r--r--  1 matthew matthew 4935 Aug 20 08:56 modules.txt
drwxr-xr-x  3 matthew matthew 4096 Aug 20 08:56 gopkg.in
drwxr-xr-x  6 matthew matthew 4096 Aug 20 08:56 .

The go.mod file contains the following

more go.mod
module github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go

go 1.14

require (
        github.com/golang/protobuf v1.3.2
        github.com/hyperledger/fabric-chaincode-go v0.0.0-20200424173110-d7076418f212
        github.com/hyperledger/fabric-contract-api-go v1.1.0
        github.com/hyperledger/fabric-protos-go v0.0.0-20200424173316-dd554ba3746e
        github.com/stretchr/testify v1.5.1
        golang.org/x/tools v0.1.0 // indirect
)

The most reliable way of properly creating the CDS is use the peer commands in the 1.4 fabric-tools docker image. It's is critical to map in the directory containing the contrace, such that it matches the module name. Run this from the directory that contains chaincode-go


# make a directory to put the cds file
mkdir -p cds

docker run -it -e MYUID=$(id -u) -v $(pwd)/cds:/cds -v $(pwd):/opt/gopath/src/github.com/hyperledger/fabric-samples/asset-transfer-basic/ hyperledger/fabric-tools:1.4.9 bash 

# then in the docker container
peer chaincode package -n chaincode-go -v 1.0 -l golang -p github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go /cds/chaincode-go.cds 

# as this is running as root you need to change the owner
chown ${MYUID} /cds/chaincode-go.cds

And as one long command...

docker run -it -e MYUID=$(id -u) -v $(pwd)/cds:/cds -v $(pwd):/opt/gopath/src/github.com/hyperledger/fabric-samples/asset-transfer-basic/ hyperledger/fabric-tools:1.4.9 bash -c "peer chaincode package -n chaincode-go -v 1.0 -l golang -p github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go /cds/chaincode-go.cds && chown ${MYUID} /cds/chaincode-go.cds"

The cds file that is produced should work with both 1.4 and 2.0 peers

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