How to listen to the event(commit event) in Hyperledger Fabric?

Viewed 4424

We set up a fabric server, and put some transaction into it. And we have some applications those will cooperate with the fabric server. Here is a situation.

  • Application send a transaction with fabric-sdk-java or fabric-sdk-node
  • The fabric excute the chaincode
  • The fabric notify the application about the result / The application listen to the event of the transaction (commit transaction, generate blockchain, append the blockchain, update the world state, etc.)
  • Application notify the custom about the transaction result.

As you know, the excution of the chaincode would cost some time. Especially, the blockchain would be commit after tens of seconds. So I want the peer callback a url or just let the application know the result of the execution. Is is possible?

2 Answers

You can register to even hub and listed for notifications of block/transaction being committed. If you are looking for example please consider to take a look at fabcar from fabric-samples. In particular you might want to take a look on invoke.js.

var options = {
    wallet_path: path.join(__dirname, './creds'),
    user_id: 'PeerAdmin',
    channel_id: 'mychannel',
    chaincode_id: 'fabcar',
    peer_url: 'grpc://localhost:7051',
    event_url: 'grpc://localhost:7053',
    orderer_url: 'grpc://localhost:7050'
};



    let eh = client.newEventHub();
    eh.setPeerAddr(options.event_url);
    eh.connect();


    let txPromise = new Promise((resolve, reject) => {
        let handle = setTimeout(() => {
            eh.disconnect();
            reject();
        }, 30000);

        eh.registerTxEvent(transactionID, (tx, code) => {
            clearTimeout(handle);
            eh.unregisterTxEvent(transactionID);
            eh.disconnect();

            if (code !== 'VALID') {
                console.error(
                    'The transaction was invalid, code = ' + code);
                reject();
            } else {
                console.log(
                    'The transaction has been committed on peer ' +
                    eh._ep._endpoint.addr);
                resolve();
            }
        });
    });
    eventPromises.push(txPromise);

Similar API exists also in Java and Golang SDKs.

Related