Chaincode functions

ChaincodeStub is implemented by the fabric-shim library. It is supplied to ChaincodeInterface and encapsulates the APIs between the chaincode implementation and the Fabric peer.

Although stub has many functions, this section lists a few of them that are frequently used:

  • getFunctionAndParameters() (string, []string): This method helps to get the function and the parameters from the stub. This method returns two values: the name of the function as a string and the parameters as a string array.
  • getState(key string) ([]byte, error): This method fetches data from the state ledger by the given key. It doesn't read data from the ledger that has not been committed. It returns data as a byte array and error information if there is any.
  • putState(key string, value []byte) (error): This method will put the given value in a transaction's write set as a proposal. This doesn't affect the ledger until the transaction is valid and successfully committed. This decision will be taken by Orderer. All the transaction data in the ledger is stored as a key-value pair only. This method takes two parameters: key—a unique string value for data, and value—a byte array of data to be stored in the ledger. This method returns the error parameter if there are any errors while executing. The same method can be used for both insert and update.
  • delState(key string) error: This method deletes the value of the given key from the ledger. As the data in the blockchain ledger cannot be deleted permanently, this method marks the data deleted and the block remains in the ledger. The input for this method is a key and it returns an error if there is one.
  • getHistoryForKey(key string) (HistoryQueryIteratorInterface, error): This is a read-only method to fetch the history of committed transactions of the given key in the ledger along with the transaction ID and timestamp. This method takes the key as input and returns an iterator of the history records and errors if there were any.
  • getQueryResult(query string) (StateQueryIteratorInterface, error): This method executes a rich query against a state database. It is only supported for state databases that support rich queries, such as Oracle, ATP, or ADW. The input for this method is a query string in the native syntax of the underlying state database. This method returns an iterator of the result and errors if there were any.
  • setEvent(name string, payload []byte) error: This sets an event as a proposal on the response to be included in a transaction. Regardless of the validity of the transaction, the event will be available within the committed transaction block.

Along with the earlier important and highly used methods in the chaincode, the stub also has the following methods:

getArgs() [][]byte

getStringArgs() []string

getArgsSlice() ([]byte, error)

getTxID() string

invokeChaincode(chaincodeName string, args [][]byte, channel string)
pb.Response

getStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)

getStateByPartialCompositeKey(objectType string, keys []string) (StateQueryIteratorInterface, error)

createCompositeKey(objectType string, attributes []string) (string, error)

splitCompositeKey(compositeKey string) (string, []string, error)

getCreator() ([]byte, error)

getTransient() (map[string][]byte, error)

getBinding() ([]byte, error)

getSignedProposal() (*pb.SignedProposal, error)

getTxTimestamp() (*timestamp.Timestamp, error)

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

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