State queries and CouchDB

By default, Fabric uses LevelDB as storage for the Worldstate. Fabric also offers the option to configure peers to store Worldstate in CouchDB. When assets are stored in the form of JSON documents, CouchDB allows you to perform complex queries for assets based on the asset state.

The queries are formatted in the native CouchDB declarative JSON querying syntax. The current version of this syntax is available at:
http://docs.couchdb.org/en/2.1.1/api/database/find.html.

Fabric forwards queries to CouchDB and returns an iterator (StateQueryIteratorInterface()), which can be used to iterate over the result set. The declaration of the  state based query function is as follows:

func GetQueryResult(query string) (StateQueryIteratorInterface, error)

In the following snippet, we can see a state-based query for all trade agreements that have the status ACCEPTED and a received payment of over 1000. The query is then executed and the found documents are written to the terminal, shown as follows:

// CouchDB query definition
queryString :=
`{
"selector": {
"status": "ACCEPTED"
"payment": {
"$gt": 1000
}
}
}`

fmt.Printf("queryString: %s ", queryString)

// Invoke query
resultsIterator, err := stub.GetQueryResult(queryString)
if err != nil {
return nil, err
}
defer resultsIterator.Close()

var buffer bytes.Buffer
buffer.WriteString("[")

// Iterate through all returned assets
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{"Key":")
buffer.WriteString(""")
buffer.WriteString(queryResponse.Key)
buffer.WriteString(""")

buffer.WriteString(", "Record":")
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")

fmt.Printf("queryResult: %s ", buffer.String())

Note that unlike queries over keys, the queries over state are not recorded into the ReadSet of the transaction. Thus, the validation of the transaction cannot actually verify whether changes to the Worldstate occurred between the execution and commitment of the transaction. The chaincode design must therefore take that into consideration; if a query is based on an expected invocation sequence, an invalid transaction may appear.

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

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