-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
98 lines (88 loc) · 3.02 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const web3 = require('./web3.js');
const fs = require('fs');
const json2csv = require('json2csv').parse;
const acc2 = '0xC202251cabC1393f8Face351057666c43f9a432A' //client
const PARALLELTRANS = process.argv[2];
const SECONDS = process.argv[3];
const ETHER = process.argv[4];
let times = [];
let account;
const noop = () => {};
const getAccount = async () => {
const accounts = await web3.eth.getAccounts();
account = accounts[0];
console.log('Working from account ', account);
};
let isFinished = times => {
return times.filter(a => typeof(a['completionTime']) === 'undefined');
}
let c = getAccount().then( async function() {
let nonce = await web3.eth.getTransactionCount(account);
//console.log(nonce);
/*
for (rep in PARALLELTRANS) {
for (sec in SECONDS) {
waitForSecond(SECONDS[sec]);
sendTransactions(nonce, PARALLELTRANS[rep], ETHER, SECONDS[sec]);
}
}*/
console.log(PARALLELTRANS,SECONDS,ETHER);
sendTransactions(nonce, PARALLELTRANS, ETHER, SECONDS);
});
let waitForSecond = second => {
const date = new Date().getSeconds();
while ((new Date().getSeconds() % 3) != 0){
setTimeout(function(){
noop();
}, 100);
}
return true;
}
function sendTransactions(nonce, repetitions, etherVal, initSecond) {
for (let id = 0; id <repetitions; id++) {
nonce += 1;
times[id] = {};
times[id]['id'] = id;
times[id]['start'] = Date.now();
web3.eth.sendTransaction({
from: account,
to: acc2,
//nonce: nonce, // increment the nonce for every transaction
value: web3.utils.toWei(String(1)),
gas: 3000000
}).on('confirmation', function(confirmationNumber, receipt){
if (confirmationNumber==0) {
times[id]['mindTime']=Date.now()-times[id]['start'];
console.log(times[id]);
}
if (confirmationNumber==12) {
times[id]['completionTime']=Date.now()-times[id]['start'];
console.log('ID: '+id);
console.log(times[id]);
if (isFinished(times).length == 0) {
console.log('Process completed for '+String(repetitions)+' repetitions starting at the '+String(initSecond)+' second');
//convert epoch to date
times.map(a => {
a['start']=new Date(0).setUTCSeconds(a['start']);
})
try{
let jsonContent = JSON.stringify(times);
fs.writeFileSync("mennan.json", jsonContent, 'utf8');
console.log("JSON file has been saved.");
} catch (err){
console.error(err);
}
try {
const csv = json2csv(times, ['id','start', 'deploymentTime', 'mindTime', 'completionTime']);
fs.writeFileSync('results_'+String(repetitions)+'_'+String(initSecond)+'.csv', csv, 'utf8');
console.log("CSV file has been saved.");
process.exit(0)
} catch (err) {
console.error(err);
}
}
};
});
times[id]['deploymentTime'] = Date.now()-times[id]['start'];
}
}