Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving hyperledger complete world state

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?

like image 562
Foo L Avatar asked Feb 28 '17 23:02

Foo L


2 Answers

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.

like image 101
Dave Enyeart Avatar answered Oct 06 '22 02:10

Dave Enyeart


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

like image 23
Clyde D'Cruz Avatar answered Oct 06 '22 01:10

Clyde D'Cruz