Fabric Node.js chain code development Description

Hyperledger Fabric is a block chain alliance, Fabric also supports intelligent block chain contract, known as the chain code (Chaincode). Fabric chain code is a standard (run docker vessel) operating system process, and by gRPC Fabric node communication protocol. So in theory you can use any language to develop Fabric chain code. This article briefly describes how to use node.js developers Fabric chain code.
Fabric provides two ways of official development node.js chain code: fabric-shim and fabric-contract-api.

I. Fabric development using fabric-shim chain code

  • fabric-shim is the deeper chain code development package, which encapsulates the communication with the node grpc protocol. Installation is as follows:
npm install fabric-shim
  • fabric-shim claim chain code developers to define a class two predefined Method.
    • Init (stub): season point chain code calls the initialization method
    • Invoke (stub): node applications of this method calls into calls to the chain code
  • Stub parameters passed by the node, which provides access to the chain books, books to read or update the status.
    For example, the following code implements a minimized node.js chain code, the chain codes are updated every time the status of the call acc0 (e.g.: This state can be used from the account balance):
const shim = require('fabric-shim');

class EzChaincode {
    async Init(stub) {       
        return shim.success(Buffer.from('init done!'));//返回success对象
    }

    async Invoke(stub) {
        let key = 'acc0';
        let oldValue = await stub.getState(key); //读取账本中acc0的状态

        let newValue = oldValue + 100; 
        await stub.putState(key, Buffer.from(newValue)); //更新acc0的状态

        return shim.success(Buffer.from('update done!'));//返回success对象
    }
};

Once defined chain code, can be used shim.start () method to start a chain code examples. E.g:

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

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

node demo.js

Second, the use of fabric-contract-api chain code Fabric Development

fabric-shim is a relatively underlying fabric grpc protocol encapsulation, which is exposed directly to the chain code of the interface to the developer, although straightforward, but if you want to achieve a relatively complicated chain code, the developer needs its own implementation in Invoke methods route.

    npm install fabric-contract-api

Chain code sample code fabric-contract-api below, in addition to a method other than the method for each constructor automatically called chain code, call for external application:

//demo.js
const { 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 done!');
    }

    async remove(ctx) {
        //.....
    }

};
module.exports.contracts = ['EzContract'];

And fabric-shim different, fabric-contract-api Export contracts only chain code array, and therefore can not be used directly to start node.js chain code, and the need to use fabric-chaincode-node process. E.g:

fabric-chaincode-node demo.js

Reference Wise Network

Guess you like

Origin www.cnblogs.com/smqh-bokeyuan/p/12613129.html