UNPKG

@wavesenterprise/generator-cli

Version:

Waves Enterprise transactions generator CLI

78 lines (77 loc) 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const js_sdk_1 = require("@wavesenterprise/js-sdk"); const interfaces_1 = require("./interfaces"); const DefaultIdle = 1; const MaxIdle = 1000; const MinIdle = 0; function createTx104(wavesApi, txData, publicKey) { 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, data) => { const { id, config, options, grpcAddress } = data; console.log(`Worker ${grpcAddress} initialized`); const { generator: { crypto, networkByte, templates }, broadcast: { senderSeedPhrase } } = config; const { concurrency } = options; const seed = wavesApi.Seed.fromExistingPhrase(senderSeedPhrase); const txBody = templates[interfaces_1.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; const { concurrency } = options; const wavesApiConfig = { ...js_sdk_1.MAINNET_CONFIG, crypto: crypto, networkByte: networkByte.charCodeAt(0), grpcAddress }; const wavesApi = js_sdk_1.create({ initialConfiguration: wavesApiConfig }); runBroadcast(wavesApi, data); });