@wavesenterprise/generator-cli
Version:
Waves Enterprise transactions generator CLI
102 lines (81 loc) • 2.58 kB
text/typescript
import { create, MAINNET_CONFIG } from "@wavesenterprise/js-sdk";
import { IBroadcastOptions, IConfig, TxType } from "./interfaces";
import { execSync } from 'child_process';
const DefaultIdle = 1
const MaxIdle = 1000
const MinIdle = 0
function createTx104(wavesApi: any, txData: any, publicKey: string) {
const { contractId, contractVersion, fee, params = [] } = txData
const txBody = {
authorPublicKey: publicKey,
contractId,
contractVersion: Number(contractVersion),
timestamp: Date.now(),
params,
fee
};
return wavesApi.API.Transactions.CallContract.V4(txBody)
}
const runBroadcast = async (wavesApi: any, data: any) => {
const { id, config, options, grpcAddress } = data
console.log(`Worker ${grpcAddress} initialized`)
const {
generator: { crypto, networkByte, templates },
broadcast: { senderSeedPhrase }
} = config as IConfig
const { concurrency } = options as IBroadcastOptions
const seed = wavesApi.Seed.fromExistingPhrase(senderSeedPhrase)
const txBody = templates[TxType.DockerCall]
const limit = 1000000
const startedAt = Date.now()
let idle = DefaultIdle
let broadcastError = false
for (let i=0; i < limit; i++) {
createTx104(wavesApi, txBody, seed.keyPair.publicKey).broadcastGrpc(seed.keyPair).catch(e => {
console.log('Broadcast error:', e)
})
await new Promise(resolve => {
setTimeout(resolve, idle)
})
if (i > 0) {
if (i % 50 === 0) {
const averageTPS = i / ((Date.now() - startedAt) / 1000)
if (concurrency && concurrency > 0) {
const tpsDelta = averageTPS - concurrency
if(Math.abs(tpsDelta) > 2) {
if (tpsDelta > 0) {
if (idle < MaxIdle) {
idle += 1
}
} else {
if (idle > MinIdle) {
idle -= 1
}
}
}
}
if (i % 1000 === 0) {
console.log(`${i} txs sent, average speed: ${Math.round(averageTPS)} txs/s`)
}
}
}
}
}
process.on("message", (data) => {
const { id, config, options, grpcAddress } = data
const {
generator: { crypto, networkByte, templates },
broadcast: { senderSeedPhrase }
} = config as IConfig
const { concurrency } = options as IBroadcastOptions
const wavesApiConfig = {
...MAINNET_CONFIG,
crypto: crypto,
networkByte: networkByte.charCodeAt(0),
grpcAddress
};
const wavesApi = create({
initialConfiguration: wavesApiConfig
})
runBroadcast(wavesApi, data)
})