Getting world state by value

0 votes

I'm new to blockchain and working with hyperledger fabric (v:0.6 for now) to create an application for learning perspective.

I'm keeping a ledger of financial transactions on blockchain, soon as a transaction takes place (A web based component notifies on transaction occurrence and calls the chaincode).

The structure of transactions looks something like this:

type Transactions struct { 
    ReferenceNumber string `json:"ReferenceNumber"`
    BillNumber string `json:"BillNumber"`
    BillingCompany string `json:"BillingCompany"`
    Amount string `json:"Amount"`
    Status string `json:"Status"`
}

I json marshal this and save it to state with ReferenceNumber as the key.

Now I can get the transaction from state on the basis of ReferenceNumber. But what if I want to get the transaction from state on the basis of let's say 'Status' like how many transactions on the ledger have status as 'reconciled'.

Is there any way to query state not on the basis of key but value?

Jun 27, 2018 in Blockchain by charlie_brown
• 7,720 points
776 views

1 answer to this question.

0 votes

Worldstate level storage works at the {key,value} level. And as obvious its only intended for a single value lookup for a specified key. I think what you are looking for calls for a next level higher level of Abstraction of WorldState - called Table constructs.fabric/examples/chaincode/go/asset_management_interactive/asset_management.go has an example on how to create a table with the columns you want. While defining the primary keys of your data structure to hold the transaction, you include Status as one of the keys and you would be able to retrieve data on the basis of Status as well.

Some sample code to create the table is as below

func createTableTwo(stub shim.ChaincodeStubInterface) error {
    var columnDefsTableTwo []*shim.ColumnDefinition
    columnOneTableTwoDef := shim.ColumnDefinition{Name: "colOneTableTwo",
        Type: shim.ColumnDefinition_STRING, Key: true}
    columnTwoTableTwoDef := shim.ColumnDefinition{Name: "colTwoTableTwo",
        Type: shim.ColumnDefinition_INT32, Key: false}
    columnThreeTableTwoDef := shim.ColumnDefinition{Name: "colThreeTableThree",
        Type: shim.ColumnDefinition_INT32, Key: true}
    columnFourTableTwoDef := shim.ColumnDefinition{Name: "colFourTableFour",
        Type: shim.ColumnDefinition_STRING, Key: true}
    columnDefsTableTwo = append(columnDefsTableTwo, &columnOneTableTwoDef)
    columnDefsTableTwo = append(columnDefsTableTwo, &columnTwoTableTwoDef)
    columnDefsTableTwo = append(columnDefsTableTwo, &columnThreeTableTwoDef)
    columnDefsTableTwo = append(columnDefsTableTwo, &columnFourTableTwoDef)
    return stub.CreateTable("tableTwo", columnDefsTableTwo)
}

answered Jun 27, 2018 by aryya
• 7,460 points

Related Questions In Blockchain

0 votes
1 answer

How to retrieve complete world state of hyperledger?

you can achieve this by iteration process ...READ MORE

answered Jul 5, 2018 in Blockchain by slayer
• 29,370 points
1,395 views
0 votes
1 answer

Error in Chaincodes not getting picked up by ./startFabric.sh

After changing chaincode in fabric-samples/chaincode/fabcar/go/fabcar.go (this is ...READ MORE

answered Jul 31, 2018 in Blockchain by Shashank
• 10,400 points
1,254 views
+1 vote
1 answer

How does a miner get to know that a transaction is verified by all the nodes?

Contrary to the popular belief, it is ...READ MORE

answered Mar 27, 2018 in Blockchain by Johnathon
• 9,090 points
2,838 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,004 views
+1 vote
1 answer
+1 vote
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP