我们设置了一个结构服务器,并将一些事务放入其中。我们有一些应用程序将与Fabric服务器合作。这是一种情况。
fabric-sdk-java
或fabric-sdk-node
chaincode
如您所知,chaincode
的执行会耗费一些时间。特别是,blockchain
将在几十秒后提交。所以我希望peer
回调为url
或者让应用程序知道执行的结果。有可能吗?
答案 0 :(得分:3)
您可以注册到甚至集线器并列出正在提交的块/交易的通知。如果您正在寻找示例,请考虑从fabcar查看fabric-samples。特别是您可能需要查看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);
Java和Golang SDK中也存在类似的API。
答案 1 :(得分:-4)
Hyperledger Composer改变了我们使用Hyperledger Fabric Blockchain的方式。虽然我不能在这里给你所有的介绍,但我提供了一些关于如何(credits to)代码的代码:
1)定义一个事件:在这里,我有一个由医生,患者和消息组成的事件。
event MedicalEvent {
--> Doctor thedoctor
--> Patient thePatient
o String theMessage
}
transaction sampleTransaction {
--> Doctor thedoctor
--> Patient thePatient
}
2)从事务中发出事件:这里我们将拥有Chain代码的逻辑。
/**
*@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)听取事件:在这里,我让我的Nodejs听取事件。
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);
});