How to use hyperledger getnative API

佐手、 提交于 2019-11-28 08:28:42

问题


I came across the getNative API through which one call chaincode from Hyperledger composer. See here: https://github.com/hyperledger/composer/issues/3120

Can someone please tell me exactly how does this work? Say if I have a very simple chaincode with a getter and setter, can I invoke those from the JS code in composer


回答1:


To call the Hyperledger Fabric API in a transaction processor function of composer, the function getNativeAPI must be called. getNativeAPI allow users to call directly of the Fabric shim API which provides the APIs for application developers to implement "Smart Contracts" for the Hyperledger Fabric backend, also known as Chaincodes.

Here is a sample example of using getNativeAPI in composer which called the Hyperledger Fabric API function getHistoryForKey to returns the history of a specified asset as an iterator. The transaction processor function then stores the returned data in an array.

async function simpleNativeHistoryTransaction (transaction) {    
    const id = transaction.assetId;
    const nativeSupport = transaction.nativeSupport;

    const nativeKey = getNativeAPI().createCompositeKey('Asset:systest.transactions.SimpleStringAsset', [id]);
    const iterator = await getNativeAPI().getHistoryForKey(nativeKey);
    let results = [];
    let res = {done : false};
    while (!res.done) {
        res = await iterator.next();

        if (res && res.value && res.value.value) {
            let val = res.value.value.toString('utf8');
            if (val.length > 0) {
                results.push(JSON.parse(val));
            }
        }
        if (res && res.done) {
            try {
                iterator.close();
            }
            catch (err) {
            }
        }
    }
}

Remember: The getState and putState Hyperledger Fabric API functions will bypass the Hyperledger Composer access control rules.

Some resource :

  • Fabric Chainode
  • Fabric Shim
  • Composer Transaction

Hope these will help you to understand.




回答2:


There's Composer chaincode (written in javascript) and Fabric's native chaincode (written in GO). If you want to invoke the chaincode deployed by Composer tools (as a part of a Business Network Definition), you don't need to call native API. You'll need it for low-level calls like getting information on a particular transaction.



来源:https://stackoverflow.com/questions/49535602/how-to-use-hyperledger-getnative-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!