官术网_书友最值得收藏!

Accessing the API with SDK

In this recipe, the application uses an SDK to access the APIs that permit queries and updates to the ledger. Now we will perform the following steps:

  1. Enroll an admin user with the enrollAdmin.js script:
        $ sudo node enrollAdmin.js

When we launch the network, an admin user needs to be registered with certificate authority. We send an enrollment call to the CA server and retrieve the enrollment certificate (eCert) for this user. We then use this admin user to subsequently register and enroll other users:

  1. Register and enroll a user called user1 using the registerUser.js script:
        $ sudo node registerUser.js
  1. With the newly-generated eCert for the admin user, let's communicate with the CA server once more to register and enroll user1. We can use the ID of user1 to query and update the ledger:
  1. Let's run a query against the ledger:
        $ sudo node query.js
  1. It returns the following screenshot. You will find that there are 10 cars on the network, from CAR0 to CAR9. Each has a color, doctype, make, model, and owner:

  1. The following chaincode constructs the query using the queryAllCars function to query all cars:
         // queryCar chaincode function - requires 1 argument,
ex: args: ['CAR4'],
// queryAllCars chaincode function - requires no arguments,
ex: args: [''],
const request = {
//targets : --- letting this default to the
peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryAllCars',
args: ['']
}
  1. Update the ledger. To do this, we will update the invoke.js script. This time, the fabcar chaincode uses the createCar function to insert a new car, CAR10, into the ledger:
        var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'createCar',
args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
chainId: 'mychannel',
txId: tx_id
};

sudo node invoke.js

Here we will complete the transaction when CAR10 is created.

  1. Execute a query to verify the changes made. Change query.js using the queryCar function to query CAR10:
        var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR10'],
chainId: 'mychannel',
txId: tx_id
};

  1. Run query.js again. We can now extract CAR10 from the ledger with the response as {"color":"Red","docType":"car","make":"Chevy","model":"Volt","owner":"Nick"}:
      sudo node query.js

This will result in the following query:

  1. Shut down the Fabric network:
      sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker ps

We have gone through the steps to query and update the transaction using smart contract chaincode. Now, let's see how it works under the hood.

主站蜘蛛池模板: 舞钢市| 沭阳县| 满洲里市| 名山县| 江华| 增城市| 铁岭县| 博乐市| 阳泉市| 上蔡县| 西丰县| 湛江市| 株洲市| 延川县| 宜川县| 建湖县| 普兰店市| 新沂市| 河西区| 油尖旺区| 崇文区| 乐亭县| 雷州市| 宣汉县| 科技| 尚志市| 胶南市| 德州市| 达日县| 桃源县| 建瓯市| 赤水市| 安庆市| 平阴县| 宁明县| 麻城市| 西华县| 宁海县| 饶阳县| 镇雄县| 都安|