This is a creation in Article, where the information may have evolved or changed.
HyperLedger Fabcar Study notes (writing the first app based on the Super Ledger)
Translated from: Http://hyperledger-fabric.rea ...
Writing the first application
We need to implement the following three steps
- First set up a development environment
- Learn some simple parameters of the smart contract that our app will use
- Develop an app to update queries for a ledger
Setting up the development environment
- First make sure that the necessary fabric image has been installed successfully, if not installed, please refer to (https://hyperledger-fabric.re ... )
- Download Fabric-samples, and install the necessary running tools. (Refer to Address: https://hyperledger-fabric.re ... )
- The above steps if you do not want to see the English students, you can refer to (build the first super-Ledger network method) Https://segmentfault.com/a/11 ...
- If you follow the tutorial on creating a Super Ledger network, you might start a network that will have an impact on the next process and need to shut down the network.
./byfn.sh -m down
- Go to the Fabric-sample/fabcar directory: This is an app for car trading
cd fabric-samples/fabcar && ls显示结果enrollAdmin.js invoke.js package.json query.js registerUser.js startFabric.sh
- Kill all the Docker image services currently running
docker rm -f $(docker ps -aq)
- Clear all Cache networks
docker network prune
- If you have already run this tutorial, you will need to do the following to remove the Chaincode image if the first run is not available
docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
Install the client and log on to the network
- The first thing to make sure that fabric-ca-client and fabric-client are ready is that the two module is the SDK for node. js to access the Super Ledger network
npm install fabric-client
npm install fabric-ca-client
The Node_module directory and a Package.json file are displayed in the Fabcar directory. If you don't understand this section, you can learn about node. js.
- Create a Super ledger network with commands
<!--run the chaincode--> written by Golang
./startFabric.sh
Description: This script creates a network of super ledgers, including nodes such as Ca-server peer order, and installs and instantiates the Chaincode.
<!--running chaincode--> written by node. js
./startFabric.sh node
How the app uses the web
Create Admin user
- The first app Admin user should send a certificate enrollment request to Ca-server, accept a registration certificate for this user (Ecert), follow up we will use this admin to register and authenticate a new user. The command is as follows:
node enrollAdmin.js
The successful execution of the command creates a Hfc-key-store directory in which the admin's identity is stored, and a pair of public and private keys are available.
Create a regular user
- Create an ordinary user user1 that the user uses to query and update the ledger. The Admin user identity is used to create User1 users. Execute the following command:
node registerUser.js
Query Ledger information
- Execute query command to query current car information
node query.js
- All car information is displayed CAR0-CAR9
[{"Key": "CAR0", "Record": {"colour": "Blue", "Make": "Toyota", "model": "Prius", "owner": "Tomoko"}},{"key": "CAR1", "record": {"colour": "Red", "make": "Ford", "model": "Mustang", "owner": "Brad"}},{"Key": "CAR2", "record": {" Colour ":" Green "," make ":" Hyundai "," model ":" Tucson "," owner ":" Jin soo "}},{" Key ":" CAR3 "," Record ": {" colour ":" Yellow " , "Make": "Volkswagen", "model": "Passat", "owner": "Max"}},{"Key": "CAR4", "Record": {"colour": "Black", "make": "Tesla", "Model": "S", "owner": "Adriana"}},{"Key": "CAR5", "Record": {"colour": "Purple", "Make": "Peugeot", "Model": "205", " Owner ":" Michel "}},{" Key ":" CAR6 "," Record ": {" colour ":" White "," Make ":" Chery "," model ":" s22l "," owner ":" Aarav "}},{" Key ":" CAR7 "," record ": {" colour ":" Violet "," Make ":" Fiat "," model ":" Punto "," owner ":" Pari "}},{" Key ":" CAR8 "," Record ": {"Colour": "Indigo", "Make": "Tata", "model": "Nano", "owner": "Valeria"}},{"Key": "CAR9", "Record": {"colour": "Brown", " Make ":" Holden "," model ":" Barina "," owner ":" Shotaro "}}]
- We can try to modify query.js and run the script again
const request = { //targets : --- letting this default to the peers assigned to the channel chaincodeId: 'fabcar', fcn: 'queryCar', //此处修改为查询单个CAR信息 args: ['CAR5'] //通过修改参数查询指定的CAR信息};显示结果如下Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-storeSuccessfully loaded user1 from persistenceQuery has completed, checking resultsResponse is {"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}
- We view the smart contract code by looking at Fabric-sample/chaincode/fabcar, and our app calls the functions supported in the smart contract via RPC. The support functions are as follows: Initledger used to create 10 car information for initialization
initLedger, queryCar, queryAllCars, createCar, and changeCarOwner.
- App Execution flowchart
Update ledger information
The
- update ledger information is similar to the query on the app side, which updates the data via the interface provided by the smart contract. Currently available features include the creation of car and the modification of the owner property of car. We can implement calls to different interfaces by modifying the Invoke.js code.
var request = { //targets:let default to the peer assigned to the CLIENT&N Bsp; chaincodeid: ' Fabcar ', &NBSP;FCN: ' Createcar ', args: [' CAR12 ', ' Honda ', ' Accord ', ' Black ', ' Tom '], chainid: ' MyChannel ', txId:tx_id }; execution Result: Lydeimac:fabcar ly$ node Invoke.js Store path:/users/ly/go/src /github.com/hyperledger/fabric-samples/fabcar/hfc-key-storesuccessfully loaded user1 from persistenceassigning Transaction_id:a5b684603b1f2a0296851409cecb143c3109220014182721165ef8fe5c326b2etransaction proposal was Goodsuccessfully sent proposal and received proposalresponse:status-200, message-"OK" The transaction has been Committ Ed on peer Localhost:7053send transaction promise and Event listener promise has compLetedsuccessfully sent transaction to the Orderer. Successfully committed the ledger by the peer
- to query all car information after creation, you can see that the CAR12 information is added
[{"Key": "CAR0", "Record": {"colour": "Blue", "Make": "Toyota", "model": "Prius", "owner": "Tomoko"}},{"key": "CAR1", " Record ": {" colour ":" Red "," make ":" Ford "," model ":" Mustang "," owner ":" Brad "}},{" Key ":" CAR12 "," Record ": {" colour ":" Black "," Make ":" Honda "," model ":" Accord "," owner ":" Tom "}},{" Key ":" CAR2 "," Record ": {" colour ":" Green "," Make ":" Hyundai "," model ":" Tucson "," owner ":" Jin soo "}},{" Key ":" CAR3 "," Record ": {" colour ":" Yellow "," make ":" Volkswagen "," Model ":" Passat "," owner ":" Max "}},{" Key ":" CAR4 "," Record ": {" colour ":" Black "," make ":" Tesla "," model ":" S "," Owner ":" Adriana "}},{" Key ":" CAR5 "," Record ": {" colour ":" Purple "," Make ":" Peugeot "," Model ":" 205 "," owner ":" Michel "}},{" key ": "CAR6", "record": {"colour": "White", "Make": "Chery", "model": "s22l", "owner": "Aarav"}},{"Key": "CAR7", "record": {" Colour ":" Violet "," Make ":" Fiat "," model ":" Punto "," owner ":" Pari "}},{" Key ":" CAR8 "," Record ": {" colour ":" Indigo "," Make ":" Tata "," model ":" Nano "," owner ":" Valeria "}},{" Key ":" CAR9 "," Record ": {" colour ":" Brown "," Make ":" Holden "," Model ":" Barina "," owner":" Shotaro "}}]
- Contract Execution Process
- 先通过客户端提交交易- 通过背书节点检查后,客户端得到背书节点响应- 客户端将背书节点响应结果提交给order排序节点- order节点创建区块后,广播给所有的peer节点更新账本