1
votes

I am trying to use custom chaincode events in Hyperledger fabric through node-sdk API

registerChaincodeEvent(ccid, eventname, onEvent, onError)

I am getting the error Timeout-Failed to receive the chaincode event. However

registerTxEvent(txid, onEvent, onError)

is working fine but i need to listen to some custom chaincode events. I am attaching chaincode function which emit events and invokechaincode.js, logs and network-config.yaml file.

1
I got some success in getting the event. However it still doesn't return block_num, txnid and status which it should be giving as per the node-sdk events documentation for chaincode events. For me it still shows below log invoke-chaincode - Successfully got a chaincode event with transid:undefined with status:undefined with event:{ "PropertyNumber" : "Property2", "message" : "This account already exists", "code" : "503"} - Sandeep Gupta
can not see your chain code. - MichaelWang
The files in the comment are no more available, could you please update them again? I was not able to use registerChaincodeEvent() successfully. - Riccardo Persiani

1 Answers

0
votes

Add the below code snippet in for registerChaincodeEvent(). I have replaced registerTxEvent() with registerChaincodeEvent().

var promises = [];
let event_hubs = channel.getChannelEventHubsForOrg();
event_hubs.forEach((eh) => {
    let event_monitor_execute = new Promise((resolve, reject) => {
            let regid1 = null;
            let handle = setTimeout(() => {
                    if (regid1) {
                             // might need to do the clean up this listener
                            eh.unregisterChaincodeEvent(regid1);
                            logger.info('Timeout - Failed to receive the chaincode event');
                    }
               reject(new Error('Timed out waiting for chaincode event'));
            }, 50000);
            eh.connect(true);
            regid1 = eh.registerChaincodeEvent(chaincodeName,'evtsender',(event,block_num,tx,status) => {

                    logger.info('Successfully got a chaincode event with transid:'+tx + ' with status:'+status);
                    let event_payload = event.payload;
                    logger.info('Successfully got a chaincode event with payload:'+ event_payload.toString('utf8'));
              //      if(event_payload.indexOf('CHAINCODE') > -1) {
                    if(block_num){
                            clearTimeout(handle);
                            eh.unregisterChaincodeEvent(regid1);
                            logger.info('Successfully received the chaincode event on block number '+ block_num);
                            resolve('RECEIVED');
                    } else {
                            logger.info('Successfully got chaincode event ... just not the one we are looking for on block number '+ block_num);
                    }
            }, (error)=> {
                    clearTimeout(handle);
                    logger.info('Failed to receive the chaincode event ::'+error);
                    reject(error);
            }
            );

    });
    promises.push(event_monitor_execute);
});

The above code need to be added in the balance-transfer code of invoke-trnasactions.js file.Link:https://github.com/hyperledger/fabric-samples/tree/release-1.1/balance-transfer

Also please edit the chaincode of the same by adding the below code:

    err = stub.SetEvent("evtsender", []byte("MOVE EXECUTED"))
    if err != nil {
            logger.Infof("Error while set eventfunction:%v", err)
            return shim.Error("error")
    }

In the end of the move() of example_cc.go file.