This guide walks you through running the Hyperledger Fabric Go SDK sample against an Alibaba Cloud Blockchain as a Service (BaaS) network. By the end, you will have enrolled a user, queried channel data, and invoked a chaincode — all through the Go SDK.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud BaaS network with at least one channel and one peer
Go 1.10.x installed (versions
>=1.10.1, <1.11are tested and supported)A
connection-profile.yamldownloaded from the BaaS console for your network
Set up the Go development environment
Verify that Go is installed and GOPATH is set correctly:
$ go version
go version go1.10.1
$ go env GOPATH
/root/gosIf either command fails, follow the official Go installation guide before continuing.
Download and set up the sample
Download go-sdk-demo-1.4.5.
Extract the archive to
$GOPATH/src:tar -xzf go-sdk-demo-1.4.5.tar.gz -C $GOPATH/srcCopy your
connection-profile.yamlinto the demo directory:cp connection-profile.yaml $GOPATH/src/go-sdk-demo/
After setup, the workspace should look like this:
go-sdk-demo
├── chaincode/
├── main.go
├── play.sh
├── README.md
├── vendor/
└── connection-profile.yaml| File | Description |
|---|---|
play.sh | Entry script that provides an interactive CLI to drive the demo |
main.go | Sample program built on the Hyperledger Fabric Go SDK |
chaincode/sacc.out | Pre-packaged chaincode, ready to upload directly to the BaaS console |
chaincode/src | Source code for sacc.out |
vendor/github.com/hyperledger/fabric-sdk-go | Bundled Fabric Go SDK source |
Upload the chaincode
Upload chaincode/sacc.out to BaaS and instantiate it on your channel. For step-by-step instructions, see Deploy chaincodes.
Run the sample
Navigate to the demo directory and start the entry script:
cd $GOPATH/src/go-sdk-demo
bash play.shFollow the prompts to enter your network credentials. play.sh drives main.go through six operations in sequence:
| Step | Operation | Purpose | Expected result |
|---|---|---|---|
| 1 | Enroll a user | Downloads certificates from the Certificate Authority (CA) and stores them locally, so subsequent SDK calls can sign transactions | Certificate files are written to the local filesystem |
| 2 | Query channel config | Fetches the channel configuration block | Channel configuration is printed to the console |
| 3 | Query channel info | Fetches the current block height and chain metadata | Block height and chain info are printed to the console |
| 4 | Query chaincode | Reads the current value of a key from the ledger | The key's current value is printed to the console |
| 5 | Invoke chaincode | Writes a new value to the ledger by submitting a transaction | The transaction is submitted and the new value is committed |
| 6 | Query chaincode again | Re-reads the key to confirm the invoke succeeded | The key's value matches the value written in step 5 |
A successful run ends with the value queried in step 6 matching the value written in step 5.
What's next
Explore the Hyperledger Fabric Go SDK to build your own application logic on top of the patterns shown in
main.go.Review Deploy chaincodes to deploy your own chaincode to the BaaS network.