问题
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
orfabric-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?
回答1:
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.
回答2:
Hyperledger Composer has changed how we work with the Hyperledger Fabric Blockchain. Although I cannot give you all the introductions here I provide you a snippet of code on how (credits to):
- To define an event
- Emit an event from your transaction
- Listen to events from your application
1) Defining an event: here I have an event that consists a doctor, a patient, and a message.
event MedicalEvent {
--> Doctor thedoctor
--> Patient thePatient
o String theMessage
}
transaction sampleTransaction {
--> Doctor thedoctor
--> Patient thePatient
}
2) Emitting an event from a transaction: here we will have the logic of the Chain code.
/**
*@ALL NECCESSARY DECORATIONS GO HERE
/
sampleTransaction(obj) {
var factory = getFactory();
var patient = obj.thepatient;
var doctor = obj.thedoctor;
var message = 'Take your medications PROPERLY';
return getParticipantRegistry('org.acme.WHATEVER.Patient')
.then(function(patientRegistry) {
var basicEvent = factory.newEvent('org.acme.WHATEVER', 'addMeLiveEvent');
basicEvent.theDoctor=doctor;
basicEvent.thePatient=patient;
basicEvent.theMessage = message;
emit(basicEvent);
})
}
3) Listen to events: here I have my Nodejs listening to events.
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
this.bizNetworkConnection = new BusinessNetworkConnection();
this.cardName ='admin@YOUR-NETWORK';
this.businessNetworkIdentifier = 'YOUR-NETWORK';
this.bizNetworkConnection.connect(this.cardName)
.then((result) => {
//You can do ANYTHING
})
.catch((error) => {
throw error;
});
this.bizNetworkConnection.on('event',(evt)=>{
console.log('Amount Transfered: '+evt.theMessage);
});
来源:https://stackoverflow.com/questions/46462151/how-to-listen-to-the-eventcommit-event-in-hyperledger-fabric