Chaincode key concept and APIs

There are three important functions in Fabric chaincode: Init, and Invoke, Query. Every chaincode program must implement the chaincode interface, as follows:

type Chaincode interface {
Init(stub ChaincodeStubInterface) pb.Response
Invoke(stub ChaincodeStubInterface) pb.Response
}

Init() is called when the application initializes its internal data for other chaincode functions to use. It is triggered when a chaincode receives an instantiate or upgrade transaction.

When the application client proposes an update or query transaction, the Invoke(); function is called.

Query() is called when a chaincode queries a chaincode state. Hyperledger Fabric uses LevelDB (key/value store) as the default database to store world;state data. You can use a key to get the current ledger state data. The query function reads the value of a chaincode state by passing in the key value.

The shim package provides APIs for the chaincode to access its state variables, transaction context, and call other chaincodes.

ChaincodeStubInterface is one of the important interfaces. It provides various functions that let you query, update, and delete assets in the ledger. These are as follows:

GetState(key string) ([]byte, error)

GetState returns the value of the specified key from the ledger

PutState(key string, value []byte) error

PutState puts the specified key and value into the transaction's writeset as a data-write proposal

DelState(key string) error

DelState records the specified key to be deleted in the writeset of the transaction proposal

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.250