gpt4 book ai didi

javascript - 从 Hyperledger Fabric 事务中检索结果

转载 作者:行者123 更新时间:2023-12-03 01:51:59 26 4
gpt4 key购买 nike

我向 Hyperledger Fabric 提交了一笔交易,但我想获取由此创建的对象。

我从中得到的对象是未定义。观察:Hyperledger Fabric 中交易已成功创建。

async submit(resource, method) {
try{
this.businessNetworkDefinition = await this.bizNetworkConnection.connect(cardname);
if (!this.businessNetworkDefinition) {
console.log("Error in network connection");
throw "Error in network connection";
}

let factory = this.businessNetworkDefinition.getFactory();
let transaction = factory.newTransaction(NS, method);

Object.assign(transaction, resource)
return await this.bizNetworkConnection.submitTransaction(transaction);
}catch(error){
console.log(error);
throw error;
}
}

最佳答案

目前,submitTransaction 函数没有返回任何内容。这是一个错误或按预期工作。

更详细的内容:当您深入研究 Composer 的源代码时,您最终将在 Composer-connector-hlfv1 中找到以下代码。

invokeChainCode(securityContext, functionName, args, options) {
const method = 'invokeChainCode';
LOG.entry(method, securityContext, functionName, args, options);

if (!this.businessNetworkIdentifier) {
return Promise.reject(new Error('No business network has been specified for this connection'));
}

// Check that a valid security context has been specified.
HLFUtil.securityCheck(securityContext);

// Validate all the arguments.
if (!functionName) {
return Promise.reject(new Error('functionName not specified'));
} else if (!Array.isArray(args)) {
return Promise.reject(new Error('args not specified'));
}

try {
args.forEach((arg) => {
if (typeof arg !== 'string') {
throw new Error('invalid arg specified: ' + arg);
}
});
} catch(error) {
return Promise.reject(error);
}

let txId = this._validateTxId(options);

let eventHandler;

// initialize the channel if it hasn't been initialized already otherwise verification will fail.
LOG.debug(method, 'loading channel configuration');
return this._initializeChannel()
.then(() => {

// check the event hubs and reconnect if possible. Do it here as the connection attempts are asynchronous
this._checkEventhubs();

// Submit the transaction to the endorsers.
const request = {
chaincodeId: this.businessNetworkIdentifier,
txId: txId,
fcn: functionName,
args: args
};
return this.channel.sendTransactionProposal(request); // node sdk will target all peers on the channel that are endorsingPeer
})
.then((results) => {
// Validate the endorsement results.
LOG.debug(method, `Received ${results.length} result(s) from invoking the composer runtime chaincode`, results);
const proposalResponses = results[0];
let {validResponses} = this._validatePeerResponses(proposalResponses, true);

// Submit the endorsed transaction to the primary orderers.
const proposal = results[1];
const header = results[2];

// check that we have a Chaincode listener setup and ready.
this._checkCCListener();
eventHandler = HLFConnection.createTxEventHandler(this.eventHubs, txId.getTransactionID(), this.commitTimeout);
eventHandler.startListening();
return this.channel.sendTransaction({
proposalResponses: validResponses,
proposal: proposal,
header: header
});
})
.then((response) => {
// If the transaction was successful, wait for it to be committed.
LOG.debug(method, 'Received response from orderer', response);

if (response.status !== 'SUCCESS') {
eventHandler.cancelListening();
throw new Error(`Failed to send peer responses for transaction '${txId.getTransactionID()}' to orderer. Response status '${response.status}'`);
}
return eventHandler.waitForEvents();
})
.then(() => {
LOG.exit(method);
})
.catch((error) => {
const newError = new Error('Error trying invoke business network. ' + error);
LOG.error(method, newError);
throw newError;
});
}

正如您在最后看到的,所发生的一切都是在等待 Events 和 Log.exit,它们什么也不返回。所以目前您必须通过其他方式获取交易结果。

关于javascript - 从 Hyperledger Fabric 事务中检索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50378913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com