4

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

user1274878
  • 1,275
  • 4
  • 25
  • 56

2 Answers2

11

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 :

Hope these will help you to understand.

mohammadjh
  • 722
  • 5
  • 12
  • 2
    +1 @mohammadjh: just adding a link to the functions (for the reader) that you can call from `getnativeAPI()` -> https://fabric-shim.github.io/ChaincodeStub.html feel free to add it to your answer (albeit you provide the main index page already). – Paul O'Mahony Apr 05 '18 at 16:13
3

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.

ulu
  • 5,872
  • 4
  • 42
  • 51
  • When we deploy the native chaincode, we see a sandbox container for the peer. And recently, we know that Fabric has started supporting JS chaincode. Is the composer chaincode -> same as js chaincode? Or is the composer chaincode another container which comes up, in the docker network, which will invoke the generic container of Fabric native chaincode ? The point is, how many containers comes up per peer when we deploy a Composer chaincode ? 1 or 2 ? – Ashishkel Jul 11 '18 at 00:34
  • 1
    Composer is not related to docker containers at all. All containers are related to the Fabric -- peers, couchdb, and SA. Each peer is in one container, as always. If you're familiar with Web programming, then native chaincode (js or GO) is like parsing a raw request. You take the raw data, parse it into URL, method, headers etc, and handle it based on this. Composer chaincode looks like you're using a Web framework, so you have multiple functions each handling its own route/method. Plus, queries and ACLs are purely declarative. – ulu Jul 11 '18 at 08:56