Skip to content

Deeptiman/offchaindata

Repository files navigation

OffChain Data

GitHub last commit GitHub language count GitHub top language

N|Solid

OffChain Data is a sample demonstration to understand the concept of implementing offchain storage and it's capability in Hyperledger fabric Blockchain network. So, this project will work as a peer block event listener and will store the block details in the CouchDB that can be query through MapReduce.

Medium writeup : https://medium.com/@deeptiman/offchain-storage-in-hyperledger-fabric-77e28bd99e0c

Configuration requirements

You need to add the certain project details in `config.json`, so that it will be used to create an event listener and the Blocks will be received through GRPC delivery client.

export FABRIC_CFG_PATH= /home/user/go/src/github.com/exampleledger/fixtures
       {
            "peer_config_path": "exampleledger/fixtures/crypto-config/peerOrganizations/",
            "msp_id": "Org1MSP",
            "msp_type": "bccsp",    
            "msp_config_dir": "org1.example.ledger.com/users/Admin@org1.example.ledger.com/msp",
            "client_key": "org1.example.ledger.com/peers/peer0.org1.example.ledger.com/tls/server.key",
            "client_cert": "org1.example.ledger.com/peers/peer0.org1.example.ledger.com/tls/server.crt",
            "root_cert": "org1.example.ledger.com/peers/peer0.org1.example.ledger.com/tls/ca.crt",
            "server": "peer0.org1.example.ledger.com:7051",
            "channel_id": "exampleledger",
            "config_file": "configtx"
       }

Create CouchDB local instance

The CouchDB local instance can be created using Docker.

docker run --publish 5990:5984 --detach --name offchaindb hyperledger/fabric-couchdb
docker start offchaindb

Mock Chaincode Model

I have followed a sample user model to create the offchaindb. You can also create your own chaincode model and the offchaindata will listen the `KVWriteSet` to store in the couchdb.

Sample Model

type SampleUser struct {
	Email 	  string 		`json:"email"`	
	Name 	  string 		`json:"name"`
	Age	  string		`json:"age"`
	Country   string		`json:"country"`
 }

Configure MapReduce

MapReduce will query the offchain data from CouchDB. So, you need to configure MapReduce for certain design element from CouchDB collection.

Configure MapReduce for Email

curl -X PUT http://127.0.0.1:5990/offchaindb/_design/emailviewdesign/ -d '{"views":{"emailview":{"map":"function(doc) { emit(doc.email,1);}", "reduce":"function (keys, values, combine) {return sum(values)}"}}}' -H 'Content-Type:application/json'

Output

{"ok": true, "id":"_design/emailviewdesign", "rev": "1-f34147f686003ff5c7da5a5e7e2759b8"}

Query Reduce function to count total email

curl -X GET http://127.0.0.1:5990/offchaindb/_design/emailviewdesign/_view/emailview?reduce=true

Output

{"rows":[
		{"key":null,"value":7}
	]}

Query Map function to list all emails

curl -X GET http://127.0.0.1:5990/offchaindb/_design/emailviewdesign/_view/emailview?group=true

Output

{"rows":[
		{"key":"alice@gmail.com","value":1},
		{"key":"john@gmail.com","value":1},
		{"key":"michale@gmail.com","value":1},
		{"key":"mark@mail.com","value":1},
		{"key":"bob@gmail.com","value":1},
		{"key":"oscar@gmail.com","value":1},
		{"key":"william@example.com","value":1}
	]}

So, all the query peformed in offchain without querying from blockchain ledger.

License

This project is licensed under the MIT License