I'm new to Node.js and asynchronous calls, but I'm trying to build a program that can automatically make multiple transactions. The thing is, right now I first connect to Hyperledger Fabric and then run the function through a for loop.
This is fairly fast, but I'm looking to greatly improve the speed. This is the code that initiates the connection:
init() {
return this.businessNetworkConnection.connect(this.connectionProfile, this.businessNetworkIdentifier, participantId, participantPwd)
.then((result) => {
console.log(chalk.green('Connected to Hyperledger!'));
this.businessNetworkDefinition = result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
This is the code that allows me to make transactions on the ledger:
makeTransaction(fromID, toID, funds) {
const METHOD = 'makeTransaction';
let from;
let walletRegistry;
let to;
return this.businessNetworkConnection.getAssetRegistry('org.acme.Wallet')
.then((registry) => {
console.log(1);
walletRegistry = registry;
return walletRegistry.get(fromID);
})
.then((fromm) => {
console.log(2);
from = fromm;
return walletRegistry.get(toID);
})
.then((too) => {
to = too;
})
.then(() => {
let serializer = this.businessNetworkDefinition.getSerializer();
let resource = serializer.fromJSON({
"$class": "org.acme.Transfer",
"amount": funds,
"from": {
"$class": "org.acme.Wallet",
"id": from.getIdentifier(),
"balance": from.balance,
"owner": "resource:org.acme.Client#" + from.owner.getIdentifier()
},
"to": {
"$class": "org.acme.Wallet",
"id": to.getIdentifier(),
"balance": to.balance,
"owner": "resource:org.acme.Client#" + to.owner.getIdentifier()
}
});
return this.businessNetworkConnection.submitTransaction(resource);
})
.catch(function (error) {
throw (error);
})
}
But the function that right now makes the transaction happen looks like this.
static transfer(fromID, toID, funds) {
let bm = new BlockchainManager();
return bm.init()
.then(() => {
return bm.makeTransaction(fromID, toID, funds);
})
.then(() => {
console.log('Success!');
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
process.exit(1);
});
}
I don't think this is the best way to make a lot of transactions (I'm looking to run over 1000 per second at some point). Which would be the best way to program this?