Skip to content

Commit

Permalink
npm run net-search-performance-plateau
Browse files Browse the repository at this point in the history
  • Loading branch information
SurfingNerd committed Sep 14, 2022
1 parent 310b21e commit 361e77e
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@
"net-send-dummy-txs": "ts-node src/runContinousTransactionsSender.ts",
"net-add-validator-each-epoch": "ts-node src/regression/runAddOneValidatorEachEpoch.ts",
"net-register-names": "ts-node src/net/runRegisterNames.ts",
"net-search-performance-plateau": "ts-node src/net/runSearchPerformancePlateau.ts",
"localnet-blockscout-fresh": "cd testnet/blockscout-local && docker-compose down && docker-compose up",
"testnet-start-current": "ts-node src/runWatchdog.ts --boot",
"testnet-watchdog": "ts-node src/runWatchdog.ts",
"testnet-state-analysis": "ts-node testnet/control/runGetCurrentTestnetState.ts",
"testnet-print-state": "ts-node src/regression/runPrintTestnetState.ts",
"testnet-clear-validator-dbs": "ts-node src/regression/runClearValidatorsDB.ts",
"testnet-fresh-grow-each-epoch": "npm-run-all testnet-fresh addOneValidatorEachEpoch",
"testnet-fresh": "export STAKING_TRANSITION_WINDOW_LENGTH=120 && export STAKING_EPOCH_DURATION=240 && export MINIMUM_BLOCK_TIME=1 && cd testnet && ./setup_testnet.py 5 5 && cd -",
"testnet-fresh": "export STAKING_TRANSITION_WINDOW_LENGTH=120 && export STAKING_EPOCH_DURATION=240 && export MINIMUM_BLOCK_TIME=1 && cd testnet && ./setup_testnet.py 4 4 && cd -",
"testnet-fresh-12hour": "export STAKING_TRANSITION_WINDOW_LENGTH=600 && export STAKING_EPOCH_DURATION=43200 && export MINIMUM_BLOCK_TIME=1 && export MAXIMUM_BLOCK_TIME=600 && cd testnet && ./setup_testnet.py 25 25 && cd -",
"testnet-test-availability-handling-current": "ts-node src/regression/testAvailabilityHandling.ts",
"testnet-test-availability-handling-fresh": "export STAKING_TRANSITION_WINDOW_LENGTH=90 && export STAKING_EPOCH_DURATION=120 && export MINIMUM_BLOCK_TIME=1 && export NETWORK_ID=666001 && cd testnet && ./setup_testnet.py 1 17 && cd - && export NODE_ENV=localhost && npm run testnet-test-availability-handling-current",
Expand Down
114 changes: 114 additions & 0 deletions src/net/runSearchPerformancePlateau.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { object } from "underscore";
import { ConfigManager } from "../configManager";
import { ContractManager } from "../contractManager";
import { sleep } from "../utils/time";
import fs from "fs";
import { awaitTransactions } from "../tools/awaitTransactions";


function toNumber(value: string | number) : number {
if (typeof value === "number") {
return value;
}

if (typeof value === "string") {
return Number.parseInt(value);
}

throw Error('not a number');

}

async function run() {

const config = ConfigManager.getConfig();
const contractManager = ContractManager.get();
const web3 = contractManager.web3;

const wallets = ConfigManager.insertWallets(web3, 50);

const defaultGasPrice = '1000000000000';
console.log("Warmup: Funding Accounts.");

let confirmed = 0;
let feedAccount = web3.eth.defaultAccount!;
let nonceFeed = await web3.eth.getTransactionCount(feedAccount);
for(const wallet of wallets) {
web3.eth.sendTransaction({ from: feedAccount, to: wallet.address, nonce: nonceFeed, value: web3.utils.toWei('1', "ether"), gas: "21000", gasPrice: defaultGasPrice})
.once("receipt", () => {
confirmed++
});
nonceFeed++;
}

console.log('waiting for Accounts to be funded.');

while ( confirmed < wallets.length) {
console.log(`confirmed ${confirmed}/${wallets.length}`);
await sleep(1000);
}

console.log('funds transfers confirmed.');
console.log('Ramping up - finding plateu');

let txPerAccount = 1;
let outputFile = 'find-plateau.csv';
console.log(`writing output to ${outputFile}`);
fs.writeFileSync(outputFile, "tx-per-account;num-of-accounts;total-txs;number-of-blocks;sum-of-block-time;blockStart;blockEnd;block-per-second;txs-per-second;\n");


// make a transaction to ensure the start of block production on hbbft.
web3.eth.sendTransaction({ from: web3.eth.defaultAccount!, to: web3.eth.defaultAccount!, nonce: nonceFeed, value: web3.utils.toWei('1', "ether"), gas: "21000", gasPrice: defaultGasPrice});

while (txPerAccount < 10) { // this while condition is kind of a max - we early exit if we have found a plateau.

const blockStart = await web3.eth.getBlockNumber();
const blockStartInstance = await web3.eth.getBlock(blockStart);
const blockStartTime = toNumber(blockStartInstance.timestamp);

let totalTxs = txPerAccount * wallets.length;
let transactionHashesToConfirm : Array<string> = [];
for(const wallet of wallets) {

let nonce = await web3.eth.getTransactionCount(wallet.address);

for(let i = 0; i<txPerAccount; i++) {
web3.eth.sendTransaction({ from: wallet.address, to: wallet.address, value: '0', nonce, gas: "21000", gasPrice: defaultGasPrice})
.once("transactionHash", (transactionHash) => {
transactionHashesToConfirm.push(transactionHash);
});
nonce++;
}
}

console.log(`transactions Sent: ${transactionHashesToConfirm.length} scanning blocks to verify transaction receipts...`);



const blockEnd = await awaitTransactions(web3, blockStart, transactionHashesToConfirm);
console.log(`all transactions confirmed with block:`, blockEnd);

const blockEndInstance = await web3.eth.getBlock(blockEnd);
const numOfBlocks = blockEnd - blockStart;
const blockEndTime = toNumber(blockEndInstance.timestamp);
const sumOfBlockTime = blockEndTime - blockStartTime;
const blocksPerSecond = numOfBlocks / sumOfBlockTime;
const txsPerSecond = totalTxs / sumOfBlockTime;

fs.appendFileSync(outputFile, `${txPerAccount};${wallets.length};${totalTxs};${numOfBlocks};${sumOfBlockTime};${blockStart};${blockEnd};${blocksPerSecond.toPrecision(4)};${txsPerSecond.toPrecision(4)};\n`);
txPerAccount++;
}




console.log('transactions funded.');






}

run();
33 changes: 33 additions & 0 deletions src/tools/awaitTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Web3 from "web3";
import { sleep } from "../utils/time";



export async function awaitTransactions(web3: Web3, blockBeforeTxSend: number, transactions: Array<string>) : Promise<number> {

let lastAnalysedBlock = blockBeforeTxSend;

while ( transactions.length > 0 ) {
await sleep(200);
// console.log("awaiting confirmation of txs: ", transactions.length);
let currentBlock = await web3.eth.getBlockNumber();

for (let blockToAnalyse = lastAnalysedBlock; blockToAnalyse < currentBlock; blockToAnalyse ++) {
console.log('analysing block', blockToAnalyse);

const block = await web3.eth.getBlock(blockToAnalyse);

const txCountBeforeFilter = transactions.length;
transactions = transactions.filter(x => !block.transactions.includes(x));
const txCountAfterFilter = transactions.length;
const txCountConfirmed = txCountBeforeFilter - txCountAfterFilter;
console.log(`block ${blockToAnalyse} proccessed. confirmed txs: ${txCountConfirmed}`);
}

lastAnalysedBlock = currentBlock;
}

console.log('all transactions confirmed at block:', lastAnalysedBlock);

return lastAnalysedBlock;
}

0 comments on commit 361e77e

Please sign in to comment.