diff --git a/benches/bench_txs_send.ts b/benches/bench_txs_send.ts index 75472f06..84f7ff7b 100644 --- a/benches/bench_txs_send.ts +++ b/benches/bench_txs_send.ts @@ -9,19 +9,21 @@ const forSeconds: number = +process.argv[3]; // url const url = process.argv.length > 4 ? process.argv[4] : "http://localhost:8899"; const skip_confirmations = process.argv.length > 5 ? process.argv[5] === "true" : false; -import * as InFile from "./out.json"; -function sleep(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); -} +(async function main() { + //@ts-ignore + const InFile: { + fee_payers: [Keypair], + users: [{ + publicKey: string; + secretKey: Uint8Array; + }]; + tokenAccounts: [PublicKey]; + mint: PublicKey; + minted_amount: number; + } = fs.readFileSync('out.json').toJSON(); -console.log("benching " + tps + " transactions per second on " + url + " for " + forSeconds + " seconds"); - -function delay(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -export async function main() { + console.log("benching " + tps + " transactions per second on " + url + " for " + forSeconds + " seconds"); const connection = new Connection(url, 'finalized'); console.log('get latest blockhash') @@ -49,7 +51,7 @@ export async function main() { signatures_to_unpack[i] = new Array(tps); let blockhash = (await connection.getLatestBlockhash()).blockhash; for (let j = 0; j < tps; ++j) { - if (j%100 == 0) { + if (j % 100 == 0) { blockhash = (await connection.getLatestBlockhash()).blockhash; } const toIndex = Math.floor(Math.random() * users.length); @@ -61,12 +63,12 @@ export async function main() { const userTo = userAccounts[toIndex]; const transaction = new Transaction().add( - splToken.createTransferInstruction(userFrom, userTo, users[fromIndex].publicKey, Math.ceil((Math.random()+1) * 100)) + splToken.createTransferInstruction(userFrom, userTo, users[fromIndex].publicKey, Math.ceil((Math.random() + 1) * 100)) ); transaction.recentBlockhash = blockhash; transaction.feePayer = authority.publicKey; - connection.sendTransaction(transaction, [authority, users[fromIndex]], { skipPreflight: true }).then(p => {signatures_to_unpack[i][j] = p}); + connection.sendTransaction(transaction, [authority, users[fromIndex]], { skipPreflight: true }).then(p => { signatures_to_unpack[i][j] = p }); } const end = performance.now(); const diff = (end - start); @@ -99,10 +101,4 @@ export async function main() { console.log("failures : " + failures) //console.log("time taken to send : " + time_taken_to_send) } -} - -main().then(x => { - console.log('finished sucessfully') -}).catch(e => { - console.log('caught an error : ' + e) -}) +})() diff --git a/benches/create_n_users.bench.ts b/benches/create_n_users.bench.ts index 33dddbc4..26f4c729 100644 --- a/benches/create_n_users.bench.ts +++ b/benches/create_n_users.bench.ts @@ -1,17 +1,31 @@ -import { Connection, Keypair } from '@solana/web3.js'; +import { Connection, Keypair, LAMPORTS_PER_SOL, sendAndConfirmTransaction, SystemProgram, Transaction } from '@solana/web3.js'; import * as splToken from "@solana/spl-token"; import * as fs from 'fs'; import * as os from 'os'; // number of users -const nbUsers = +process.argv[2]; +const nbUsers = process.argv.length > 2 ? +process.argv[2] : 10; // url const url = process.argv.length > 3 ? process.argv[3] : "http://0.0.0.0:8899"; +// tokens to transfer to new accounts 0.5 sol +const fee_payer_balance = process.argv.length > 4 ? +process.argv[4] : (LAMPORTS_PER_SOL / 2); +// tokens to transfer to new accounts 0.5 sol +const number_of_fee_payers = process.argv.length > 4 ? +process.argv[4] : 10; // outfile const outFile = process.argv.length > 4 ? process.argv[4] : "out.json"; +function check_if_out_file_exists() { + if (!fs.existsSync(outFile)) + return; + + console.warn(`{outFile} already exists. Potential loss of funds. Remove and re run`); + process.exit(); +} + (async function main() { - console.log("Creating " + nbUsers + " Users on " + url + " out file " + outFile); + check_if_out_file_exists(); + + console.log(`Creating ${nbUsers} users on ${url} with ${number_of_fee_payers} with balance ${fee_payer_balance} and output file ${outFile}`); console.time('Time taken'); const connection = new Connection(url, 'confirmed'); @@ -25,6 +39,32 @@ const outFile = process.argv.length > 4 ? process.argv[4] : "out.json"; ), ); + const authority_balance = await connection.getBalance(authority.publicKey); + const required_balance = number_of_fee_payers * fee_payer_balance; + + if (authority_balance < required_balance) { + console.warn(`Authority doesn't have enough balance. Required at least ${required_balance} Lamport`); + process.exit(); + } + + const fee_payers = Array(nbUsers).fill(0).map(() => Keypair.generate()); + + console.log(`Sending ${fee_payer_balance} to each of ${number_of_fee_payers} fee payers`); + + await Promise.all(fee_payers.map(async fee_payer => { + const ix = SystemProgram.transfer({ + fromPubkey: authority.publicKey, + toPubkey: fee_payer.publicKey, + lamports: fee_payer_balance + }); + + const tx = new Transaction().add(ix); + + const tx_sig = await sendAndConfirmTransaction(connection, tx, [authority]); + + console.log(`Sent ${tx_sig}`); + })); + const userKps = Array(nbUsers).fill(0).map(() => Keypair.generate()); const mint = await splToken.createMint( @@ -67,6 +107,7 @@ const outFile = process.argv.length > 4 ? process.argv[4] : "out.json"; })); const data = { + 'fee_payers': fee_payers, 'users': users, 'tokenAccounts': accounts, 'mint': mint,