Is there a chaincode shim function with which I can retrieve all the keys (maybe including values) of the world state in a Hyperledger Fabric chaincode?
In chaincode API GetStateByRange(startKey, endKey string), the startKey and endKey can be empty string, which implies an unbounded range query on start or end. Leave them both as empty string to get the full set of key/values returned.
It is possible to iterate over all the keys in the chaincode state of a particular chaincode using the stub.GetStateByRange()
function.
Eg:
keysIter, err := stub.GetStateByRange(startKey, endKey)
if err != nil {
return shim.Error(fmt.Sprintf("keys operation failed. Error accessing state: %s", err))
}
defer keysIter.Close()
var keys []string
for keysIter.HasNext() {
key, _, iterErr := keysIter.Next()
if iterErr != nil {
return shim.Error(fmt.Sprintf("keys operation failed. Error accessing state: %s", err))
}
keys = append(keys, key)
}
See the complete chaincode in the Hyperledger fabric repo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With