Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement chain Code Development example FabCar by HyperLedger

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "HyperLedger how to achieve chain code development example FabCar", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "HyperLedger how to achieve chain code development example FabCar" this article.

Application and chain code development-FabCar writes the first application

Through several example programs to understand the working principle of Fabric application.

SDK to invoke smart contracts (details on both can be found in the SDK application and smart contract processing chapters)

Three steps

Establish a development environment. * * use first-network to build a basic network (two organizations and a sorting node; there is a mychannel between the two groups)

Learn the smart contracts sample Fabcar.** to see how smart contracts work and how applications can use them to query and update ledgers

Develop a sample application that uses Fabcar. * * applications use smart contracts to query and update car assets on their books. Use the application to create transactions-query cars, create new cars.

To complete this tutorial, you should learn how applications interact with smart contracts, and how to host and replicate ledger interactions on peer entities in a Fabric network.

Docker rm-f (docker ps-aq)

Docker rmi-f (docker images | grep fabcar | awk'{print 3}')

Use the above command to terminate all previously run fabric containers.

Cd xxx/fabric-samples/FabCar/ & &. / startFabric.sh javascript

The above command automatically initializes first-network while deploying the JavaScript version of chaincode to both peer nodes.

Cd. / javascript & & npm install

Node enrollAdmin.js

Create an administrator user (called the administrator is actually the registrant of the certificate authority CA)

Node registerUsre.js

Register a new user with administrator credentials (this user can be used to query and update ledgers) user1

Node query.js

Query book information

Parsing the javascript application of Fabcar

Const {FileSystemWallet, Gateway} = require ('fabric-network') introduces two key classes, FileSystemWallet and Gateway, from the fabric-network module.

Cosnt gateway = new Gateway ()

Await gateway.connect (ccp, {wallet,identity:'user1'})

Connect the application to the network (using the identity of user1). Where ccp represents the network to which the gateway will access. Ccp is configured with the following code:

Const ccpPath = path.resolve (_ _ dirname,'..','..','first-network','connection.json') const ccpJSON = fs.readFileSync (ccpPath,'utf8') const ccp = JSON.parse (ccpJSON)

(you can see the connection profile about the structure of the network profile and how to define the network)-when we are connected to the network, we need to connect to a specific channel in order to gain access to a specific world state.

Const network = await gateway.getNetwork ('mychannel')

After connecting to the channel, you need to connect to a specific chaincode in order to interact with the world state through an intelligent contract.

Const contact = network.getContract ('fabcar')

Many transactions are defined in Fabcar (transaction)

Use const result = await contact.evaluateTransaction ('queryAllCars') to query the state of the world.

The above operation is the simplest intelligent contract interaction, which selects a node defined in the connection profile to send the request, and the node executes the evaluation command-> the intelligent contract queries all the car information recorded in the node's account copy-> returns the result to the application.

Use contract.submitTransaction ('createCar','CAR12','Honda','Accord','Black','Tom') to create information about a new car. SubmitTransaction () means to submit a transaction, which is much more complex than evaluateTransaction. It works as follows: first, the submitTransaction day is sent to each peer entity-> each peer entity executes the command Return the result to SDK- > SDK collect all back into a transaction and send it to the Order node-> Order collect and block broadcast-> after each transaction is verified and committed, it is allowed to return control to the application (submitTransaction includes a listener).

Use the await contract.submitTransaction ('changeCarOwner','CAR12','Dave') command to change the owner of the car

Analysis of Fabcar's javascript Intelligent contract

Cd xxx/fabric-samples/chaincode/fabcar/javascript/lib/fabcar.js

Learn how to use the Contract class to define smart contracts

Class FabCar extends Contract {}

The following transactions will be defined in this structure: initLedger, queryCar, queryAllCars, createCar, and changeCarWoner.

Async queryCar (ctx, carnumber) {...}

Async queryAllCars (ctx) {...}

Analyze the querAllCars transaction

Async queryAllCars (ctx) {const startKey = 'CAR0' const endKey =' CAR999' cosnt iterator = await ctx.stub.getStateByRange (starKey,endKey)...}

This code iterates through each car and packages the results into JSON and returns them to the application. GetStateByRange () is the API of the transaction, and API can access the javascript intelligent contract API.

The above is all the contents of the article "how to implement chain code development example FabCar in HyperLedger". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report