我有三个客户端应用程序,它们分别使用 Java、Node.js 和 Go SDK 与我的区 block 链 Fabric 进行交互。使用它们,我可以成功查询和更新分类帐。
现在我想测量分类帐更新期间的延迟。所以,我想在提交请求之前取一个时间戳,在交易成功提交到账本之后取另一个时间戳,然后计算差值。
我的问题是我找不到任何关于 SDK 的 Java、Go 和 Node.js API 的完整文档,所以我不知道当提交方法返回时,我是否可以认为事务已正确提交到账本。
这是我的三个客户的代码。 java :
NetworkConfig ccp = NetworkConfig.fromJsonFile(networkConfigPath.toFile());
// initialize default cryptosuite and setup the client
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(cryptoSuite);
Channel channel = client.loadChannelFromConfig(ccp.getChannelNames().iterator().next(), ccp);
channel.initialize();
TransactionProposalRequest transactionProposal = client.newTransactionProposalRequest();
// build chaincode id providing the chaincode name
ChaincodeID mychaincodeID = ChaincodeID.newBuilder().setName("mychaincode").build();
transactionProposal.setChaincodeID(mychaincodeID);
// calling chaincode function
transactionProposal.setFcn("mymethod");
transactionProposal.setArgs("a1");
Collection<ProposalResponse> res = channel.sendTransactionProposal(transactionProposal);
channel.sendTransaction(res);
节点.js:
const gateway = new Gateway();
await gateway.connect(ccp, { wallet: wallet, identity: userName, discovery: { enabled: false } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
// Get the contract from the network.
const contract = network.getContract('mychaincode');
const result = await contract.submitTransaction('mymethod', 'a1');
开始:
sdk, err := fabsdk.New(config.FromFile(configFile))
if err != nil {
fmt.Printf("failed to create SDK: %v\n", err)
return
}
fmt.Println("SDK created")
// Prepare channel client context using client context
clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser(userName), fabsdk.WithOrg(orgName))
// ChannelClient is used to query and execute transactions
client, err := channel.New(clientChannelContext)
if err != nil {
fmt.Printf("failed to create new channel client: %v\n", err)
return
}
fmt.Println("channel client created")
response, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "mymethod", Args: [][]byte{[]byte("a1")}}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
fmt.Printf("failed to execute the invoke function: %v\n", err)
} else {
fmt.Println("Proposal responses: ")
for _, element := range response.Responses {
fmt.Printf("Endorser: %s Status: %d ChaincodeStatus: %d\n", element.Endorser, element.Status, element.ChaincodeStatus)
}
fmt.Println("chaincode transaction completed: " + string(response.Payload))
}
// Close SDK
sdk.Close()
这些代码有效。我的问题是:我可以确定在行之后
channel.sendTransaction(res)
(在 Java 中)
const result = await contract.submitTransaction('mymethod', 'a1');
(在 Node.js 中)
response, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "mymethod", Args: [][]byte{[]byte("a1")}}, channel.WithRetry(retry.DefaultChannelOpts))
(围棋) 交易已经提交到账本?
我只在文档上发现:
“向分类帐提交交易。交易功能名称将在背书节点上进行评估,然后提交给排序服务以提交到分类帐。”用于 Node.js 中的 submitTransaction https://fabric-sdk-node.github.io/release-1.4/module-fabric-network.Contract.html#submitTransaction__anchor
和
“Execute 使用请求和可选的请求选项准备和执行事务”,用于在 https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/channel#Client.Execute 中执行 Go
对于 Java,我找不到文档...而且我也不确定 Node.js 和 Go。
最佳答案
另一个答案可以帮助那些可能需要 CLI 中的此功能的人。
默认情况下,当订单收到交易时,CLI 返回成功。
要等到 peer chaincode invoke
提交,commnad 添加标志 --waitForEvent
这样,cli 将等待来自同行的提交事件。
希望对您有所帮助。
关于hyperledger-fabric - super 账本结构 : check if the transaction has been committed to the ledger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103391/