Created
March 7, 2020 12:02
-
-
Save uluhonolulu/5728e99536b9393c0e2f540eb451d60e to your computer and use it in GitHub Desktop.
Testing Parity performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3'); | |
var net = require('net'); | |
const provider = new Web3.providers.IpcProvider(process.env.HOME + '/node/jsonrpc.ipc', net); | |
// const nodeUrl = "http://127.0.0.1:8545"; | |
// const provider = new Web3.providers.HttpProvider(nodeUrl, { timeout: 300000 }); | |
const web3 = new Web3(provider); | |
require('dotenv').config(); | |
const { TIMES_TO_RUN, DELAY } = process.env; //TIMES_TO_RUN = 10000, DELAY = 1 | |
const PASSWORD = 'p4ss'; | |
(async () => { | |
let accounts = await web3.eth.getAccounts(); | |
let coinbase = accounts[0]; | |
if (!coinbase) { | |
console.error("No coinbase!"); | |
process.exit(1); | |
} | |
//use Promise.all to wait till all transactions are hashed | |
//the argument will be the the array of transaction promises | |
await Promise.all(createArray(parseInt(TIMES_TO_RUN)).map(async (_, index) =>{ | |
await delay(DELAY * index); | |
console.log(`${index}/${TIMES_TO_RUN}`); | |
try { | |
return sendTransaction(coinbase); | |
} catch (e) { | |
console.error(e); | |
process.exit(1); | |
} | |
} )); | |
process.exit(0); | |
}) (); | |
async function sendTransaction(coinbase) { | |
return new Promise((resolve, reject) => { | |
web3.eth.sendTransaction({ | |
from: coinbase, | |
to: '0xa2352ECd13042EfD0A1a0F41A3e8aF72F1D447aC', | |
value: web3.utils.toWei('0.0001', "ether"), | |
gas: 21000 | |
}, PASSWORD) | |
.on('transactionHash', async hash => { | |
console.log("Sent transaction: ", hash); | |
resolve(); | |
}) | |
.on('receipt', async receipt => { | |
console.log(receipt); | |
}) | |
// .on('confirmation', function(confirmationNumber, receipt){ ... }) | |
.on('error', reject); // If a out of gas error, the second parameter is the receipt. | |
}); | |
} | |
async function delay(ms){ | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve(), ms); | |
}) | |
} | |
function createArray(count) { | |
const array = new Array(count); | |
array.fill(0); | |
return array; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment