我想在结构链码上做很多 txs,如何调用它async
或 in batch
?
虽然我只找到了
channelClient.Execute
channelClient.Query
方法,但它会一一发送 txs 并等待 tx 在块中提交。
问问题
393 次
1 回答
-1
在单个客户端中,在代码中,事务似乎是一一提交的,但当您广泛地查看网络拓扑时,情况并非如此。
我将根据面料的交易流程进行解释。
- 多个客户端同时向多个对等方请求对交易进行背书,并返回背书结果。
- Endorsed transaction,即背书同时传递给orderer,orderer确定交易的顺序,进行区块打包,提交给peer。
- 换句话说,交易被收集在排序器中并存储在块中。实际上,交易包含在块体内。
- 通过增加块的批处理时间可以很容易地观察到这个过程。
- BatchTimeout 可以在 Orderer 项下定义为 configtx.yaml 文件中通道的设置值。面料/sampleconfig/configtx.yaml
如果您的问题是一次从单个客户端请求多个事务,您可以通过 javascript 中的异步请求轻松实现如下。代码部分修改了fabric-samples/fabcar/invoke.js。
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { Gateway, Wallets } = require('fabric-network');
const fs = require('fs');
const path = require('path');
async function main(tx_params) {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
let ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const identity = await wallet.get('appUser');
if (!identity) {
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// 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('fabcar');
// Submit the specified transaction.
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave')
await contract.submitTransaction(...tx_params);
console.log(tx_params + ' Transaction has been submitted');
// Disconnect from the gateway.
await gateway.disconnect();
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}
const tx1_params = ['createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'];
const tx2_params = ['createCar', 'CAR13', 'Hyundai', 'Genesis', 'Gray', 'Myeongkil'];
const tx3_params = ['createCar', 'CAR14', 'Porsche', 'Macan', 'Blue', 'Dave'];
main(tx1_params);
main(tx2_params);
main(tx3_params);
- 结果:标准输出
Wallet path: /Users/kmk/Project/src/github.com/hyperledger/fabric-samples/fabcar/javascript/wallet
Wallet path: /Users/kmk/Project/src/github.com/hyperledger/fabric-samples/fabcar/javascript/wallet
Wallet path: /Users/kmk/Project/src/github.com/hyperledger/fabric-samples/fabcar/javascript/wallet
createCar,CAR13,Hyundai,Genesis,Gray,Myeongkil Transaction has been submitted
createCar,CAR12,Honda,Accord,Black,Tom Transaction has been submitted
createCar,CAR14,Porsche,Macan,Blue,Dave Transaction has been submitted
- 订购者日志
# 3 endorsments
# -> into 1 block
2020-12-30 02:15:08.948 UTC [comm.grpc.server] 1 -> INFO 096 streaming call completed grpc.service=orderer.AtomicBroadcast grpc.method=Broadcast grpc.peer_address=172.30.0.1:49302 grpc.peer_subject="CN=fabric-common" grpc.code=OK grpc.call_duration=7.792537ms
2020-12-30 02:15:08.953 UTC [comm.grpc.server] 1 -> INFO 097 streaming call completed grpc.service=orderer.AtomicBroadcast grpc.method=Broadcast grpc.peer_address=172.30.0.1:49304 grpc.peer_subject="CN=fabric-common" grpc.code=OK grpc.call_duration=6.326148ms
2020-12-30 02:15:08.967 UTC [comm.grpc.server] 1 -> INFO 098 streaming call completed grpc.service=orderer.AtomicBroadcast grpc.method=Broadcast grpc.peer_address=172.30.0.1:49306 grpc.peer_subject="CN=fabric-common" grpc.code=OK grpc.call_duration=2.909602ms
2020-12-30 02:15:10.943 UTC [orderer.consensus.etcdraft] propose -> INFO 099 Created block [20], there are 0 blocks in flight channel=mychannel node=1
2020-12-30 02:15:10.946 UTC [orderer.consensus.etcdraft] writeBlock -> INFO 09a Writing block [20] (Raft index: 22) to ledger channel=mychannel node=1
于 2020-12-30T02:12:49.540 回答