Merge branch 'master' of https://github.com/chhaileng/fabric-samples into chhaileng

This commit is contained in:
Chhaileng Peng 2020-08-07 06:31:59 +00:00
commit c02c49be55

View file

@ -37,7 +37,7 @@ The configuration for the listener is stored in the `config.json` file:
"channelid": "mychannel", "channelid": "mychannel",
"use_couchdb":true, "use_couchdb":true,
"create_history_log":true, "create_history_log":true,
"couchdb_address": "http://localhost:5990" "couchdb_address": "http://admin:adminpw@localhost:5990"
} }
``` ```
@ -55,13 +55,12 @@ If you set the "use_couchdb" option to true in `config.json`, you can run the
following command start a local instance of CouchDB using docker: following command start a local instance of CouchDB using docker:
``` ```
docker run --publish 5990:5984 --detach --name offchaindb couchdb docker run --publish 5990:5984 --detach -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=adminpw --name offchaindb couchdb
docker start offchaindb
``` ```
### Install dependencies ### Install dependencies
You need to install Node.js version 8.9.x to use the sample application code. You need to install Node.js version 8.17.x to use the sample application code.
Execute the following commands to install the required dependencies: Execute the following commands to install the required dependencies:
``` ```
@ -217,13 +216,13 @@ the blocks. If the listener is running, this table should be the same as the
latest values in the state database running on your peer. The table is named latest values in the state database running on your peer. The table is named
after the channelid and chaincodeid, and is named mychannel_marbles in this after the channelid and chaincodeid, and is named mychannel_marbles in this
example. You can navigate to this table using your browser: example. You can navigate to this table using your browser:
http://127.0.0.1:5990/mychannel_marbles/_all_docs http://admin:adminpw@127.0.0.1:5990/mychannel_marbles/_all_docs
A second table records each block as a historical record entry, and was created A second table records each block as a historical record entry, and was created
using the block data that was recorded in the log file. The table name appends using the block data that was recorded in the log file. The table name appends
history to the name of the first table, and is named mychannel_marbles_history history to the name of the first table, and is named mychannel_marbles_history
in this example. You can also navigate to this table using your browser: in this example. You can also navigate to this table using your browser:
http://127.0.0.1:5990/mychannel_marbles_history/_all_docs http://admin:adminpw@127.0.0.1:5990/mychannel_marbles_history/_all_docs
### Configure a map/reduce view for summarizing counts of marbles by color: ### Configure a map/reduce view for summarizing counts of marbles by color:
@ -236,13 +235,13 @@ created when events are received.
Open a new terminal window and execute the following: Open a new terminal window and execute the following:
``` ```
curl -X PUT http://127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign -d '{"views":{"colorview":{"map":"function (doc) { emit(doc.color, 1);}","reduce":"function ( keys , values , combine ) {return sum( values )}"}}}' -H 'Content-Type:application/json' curl -X PUT http://admin:adminpw@127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign -d '{"views":{"colorview":{"map":"function (doc) { emit(doc.color, 1);}","reduce":"function ( keys , values , combine ) {return sum( values )}"}}}' -H 'Content-Type:application/json'
``` ```
Execute a query to retrieve the total number of marbles (reduce function): Execute a query to retrieve the total number of marbles (reduce function):
``` ```
curl -X GET http://127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?reduce=true curl -X GET http://admin:adminpw@127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?reduce=true
``` ```
If successful, this command will return the number of marbles in the blockchain If successful, this command will return the number of marbles in the blockchain
@ -257,7 +256,7 @@ world state, without having to query the blockchain ledger:
Execute a new query to retrieve the number of marbles by color (map function): Execute a new query to retrieve the number of marbles by color (map function):
``` ```
curl -X GET http://127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?group=true curl -X GET http://admin:adminpw@127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?group=true
``` ```
The command will return a list of marbles by color from the CouchDB database. The command will return a list of marbles by color from the CouchDB database.
@ -279,14 +278,14 @@ will support a query that traces the history of each marble. Execute the
following command to create the index: following command to create the index:
``` ```
curl -X POST http://127.0.0.1:5990/mychannel_marbles_history/_index -d '{"index":{"fields":["blocknumber", "sequence", "key"]},"name":"marble_history"}' -H 'Content-Type:application/json' curl -X POST http://admin:adminpw@127.0.0.1:5990/mychannel_marbles_history/_index -d '{"index":{"fields":["blocknumber", "sequence", "key"]},"name":"marble_history"}' -H 'Content-Type:application/json'
``` ```
Now execute a query to retrieve the history for the marble we transferred and Now execute a query to retrieve the history for the marble we transferred and
then deleted: then deleted:
``` ```
curl -X POST http://127.0.0.1:5990/mychannel_marbles_history/_find -d '{"selector":{"key":{"$eq":"marble110"}}, "fields":["blocknumber","is_delete","value"],"sort":[{"blocknumber":"asc"}, {"sequence":"asc"}]}' -H 'Content-Type:application/json' curl -X POST http://admin:adminpw@127.0.0.1:5990/mychannel_marbles_history/_find -d '{"selector":{"key":{"$eq":"marble110"}}, "fields":["blocknumber","is_delete","value"],"sort":[{"blocknumber":"asc"}, {"sequence":"asc"}]}' -H 'Content-Type:application/json'
``` ```
You should see the transaction history of the marble that was created, You should see the transaction history of the marble that was created,
@ -321,7 +320,7 @@ If you check the current state table using the reduce command, you will only
be able to see the original marbles in your database. be able to see the original marbles in your database.
``` ```
curl -X GET http://127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?reduce=true curl -X GET http://admin:adminpw@127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?reduce=true
``` ```
To add the new data to your off-chain database, remove the `nextblock.txt` To add the new data to your off-chain database, remove the `nextblock.txt`
@ -342,7 +341,7 @@ added to the ledger. If you run the reduce command against your database one
more time, more time,
``` ```
curl -X GET http://127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?reduce=true curl -X GET http://admin:adminpw@127.0.0.1:5990/mychannel_marbles/_design/colorviewdesign/_view/colorview?reduce=true
``` ```
you will be able to see that all of the marbles have been added to your you will be able to see that all of the marbles have been added to your