Starting With Blockchain Chaincode Using Golang
In this article, you'll learn the four main functions you'll need to know to implement your Chaincode's interface using Go.
Join the DZone community and get the full member experience.
Join For FreeChaincode, or a smart contract, is a fragment of code written in supported languages like Java or Go that is deployed onto a network of HyperLedger Fabric peer nodes.
Chaincodes run network transactions which are validated and then appended to the shared ledger. In simple terms, they are an encapsulation of business network transactions in code.
In this blog, we will learn how to develop chaincode with GoLang for a blockchain network based on Hyperledger Fabric v0.6.
Development Environment Required for Chaincode Development:
- Go 1.6 install
- Hyperledger Fabric
- Use the following command to install Hyperledger fabric 0.6:
git clone -b v0.6 http://gerrit.hyperledger.org/r/fabric
Implementing the Chaincode Interface
The following are the main functions required to implement the chaincode shim interface in your Go code.
Init() :
Init() is called when you first deploy your chaincode.
An example of using this function with Go can be:
func (t *SampleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
err := stub.PutState("emp_name", []byte(args[0]))
if err != nil {
return nil, err
}
return nil, nil
}
As the name suggests, Init() is used to do any initialization your chaincode needs. In this example, stub.PutState()
stores the first value in the args argument in the key “emp_name.”
Invoke()
This is called when we expect some “real work” to be done by the chaincode.
Example: Every time there’s a transaction, the ledger will be updated by invoking the chaincode.
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
fmt.Println("invoke is running " + function)
// Handle different functions
if function == "init" {
return t.Init(stub, "init", args)
} else if function == "write" {
return t.write(stub, args)
}
fmt.Println("invoke did not find func: " + function)
return nil, errors.New("Received unknown function invocation")
}
Here, the Invoke()
function calls a generic write function which will help you store any key value pair in the ledger.
Query()
This function is called to get the state of the chaincode.
The query is used to only read the value of your chaincode state’s key/value pairs and it does not add any new blocks to the ledger.
func (t *SampleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
fmt.Println("query is running " + function)
// Handle different functions
if function == "dummy_query" { //read a variable
fmt.Println("hi there " + function) //error
return nil, nil;
}
fmt.Println("query did not find func: " + function) //error
return nil, errors.New("Received unknown function query: " + function)
}
Main()
The starting point for any Go program is the main function, so here it is used for starting the chaincode.
When the peer deploys its instance of the chaincode, the main function gets executed.
func main() {
err := shim.Start(new(SampleChaincode))
if err != nil {
fmt.Println("Could not start SampleChaincode")
} else {
fmt.Println("SampleChaincode successfully started")
}
}
In this example, shim.Start(new(SampleChaincode))
line is used to start the chaincode and register it with the peer.
To verify this locally, run the chaincode in your development environment, which should produce the following error:
[shim] CRIT : peer.address not configured, can’t connect to peer.
Published at DZone with permission of Himani Arora, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
What to Pay Attention to as Automation Upends the Developer Experience
-
Unlocking Game Development: A Review of ‘Learning C# By Developing Games With Unity'
-
Getting Started With the YugabyteDB Managed REST API
-
5 Key Concepts for MQTT Broker in Sparkplug Specification
Comments