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 a Fabric chain code in node.js

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to achieve a Fabric chain code in node.js, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Using fabric-shim to develop Fabric chain Code

Fabric-shim is a lower-level chain code development kit that encapsulates the grpc protocol that communicates with nodes. The installation method is as follows:

~ / fabric-shim-chaincode-demo$ npm install fabric-shim

Fabric-shim requires chain code developers to define a class that implements two predefined methods.

Init (stub): this method is called by the node when the chain code is initialized

Invoke (stub): the node converts the application's call to the chain code into a call to the method

The parameter stub is passed in by the node, which provides a way to access the books on the chain in order to read or update the status of the books.

For example, the following code implements a minimized node.js chain code that updates the status of acc0 each time the chain code is called (for example, you can use this status to represent an account balance):

Const shim = require ('fabric-shim'); class EzChaincode {async Init (stub) {return shim.success (Buffer.from (' init clients')); / / return success object} async Invoke (stub) {let key = 'acc0'; let oldValue = await stub.getState (key); / / read the status of acc0 in the account let newValue = oldValue + 100 Await stub.putState (key, Buffer.from (newValue)); / / Update the status of acc0 return shim.success (Buffer.from ('update requests')); / / return the success object}}

Once the chain code is defined, you can start the chain code instance using the shim.start () method. For example:

Const shim = require ('fabric-shim'); class EzChainCode {...} shim.start (new EzChaincode ())

This is a complete Fabric chain code! Save the above code as demo.js, and you can start it directly with node.js:

~ / fabric-shim-chaincode-demo$ node demo.js uses fabric-contract-api to develop Fabric chain codes

Fabric-shim is a relatively low-level fabric grpc protocol encapsulation, which directly exposes the chain code interface to the developer. Although it is simple and straightforward, if you want to implement a relatively complex chain code, the developer needs to route the method in the Invoke implementation.

Fabric-contract-api is a higher-level encapsulation. Developers directly inherit the Contract classes provided by the development package, so they don't have to worry about contract method routing. The fabric-contrac-api development method is as follows:

~ / fabric-contract-api-demo$ npm install fabric-contract-api

The sample chain code using fabric-contract-api is as follows. Every method except the constructor is automatically called a chain code method, which can be called by external applications:

/ / demo.jsconst {Contract} = require ('fabric-contract-api'); class EzContract extends Contract constructor () {super (' EzContract');} async update (ctx, newValue) {await ctx.stub.putState ('acc0', Buffer.from (newValue)); return Buffer.from (' update subscription');} async remove (ctx) {/ /. }}; module.exports.contracts = ['EzContract']

Unlike fabric-shim, fabric-contract-api only needs chain codes to export contracts arrays, so you can't start chain codes directly with node.js. Instead, you need to use fabric-chaincode-node programs. For example:

~ / fabric-contract-api-demo$ fabric-chaincode-node demo.js on how to achieve a Fabric chain code in node.js to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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