-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinter.js
More file actions
executable file
·119 lines (99 loc) · 3.7 KB
/
Copy pathinter.js
File metadata and controls
executable file
·119 lines (99 loc) · 3.7 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const Web3 = require("./web3.js-1.2.6");
const MyContract = require("./build/contracts/System.json");
const API = require("./contract-interaction.js");
const noOfUsers = 7;
const ports = ["http://localhost:22000", "http://localhost:22001",
"http://localhost:22002", "http://localhost:22003",
"http://localhost:22004", "http://localhost:22005",
"http://localhost:22006"];
const names = ["bank", "alice", "bob", "charlie", "david", "emily", "felix"];
const latencies = [0, 0, 0, 0, 0, 0, 0];
// parse scenario
var fs = require('fs');
var lines = fs.readFileSync('scenarios/7.txt').toString().split("\n");
var eachLine = []
for (var l in lines) {
eachLine.push(lines[l].split(" "))
}
const RECEIVE = 10000; // to set
const PAUSE = 10000; // to set
// run txs
const init = async() => {
var users = [];
var accounts = [];
for (var it = 0; it < noOfUsers; it++) {
const web3 = new Web3(ports[it]);
const id = await web3.eth.net.getId();
const deployedNetwork = MyContract.networks[id];
const contract = new web3.eth.Contract(
MyContract.abi,
deployedNetwork.address
);
const thisAccounts = await web3.eth.getAccounts();
const account = thisAccounts[0];
const user = new API(contract, account);
await user.setUsername(names[it]);
users.push(user);
accounts.push(account);
}
const begin = new Date();
const pattern = async(fn, when) => {
setTimeout(async () => {
const i = new Date();
await fn();
const end = new Date();
console.log("SCHEDULED FOR ", when, " BUT FROM ", i - begin, " TO ", end - begin, " (", end - i, ")");
}, when);
}
await users[0].restart();
setInterval(async() => {
const end = new Date();
console.log("RUN AT: ", end - begin);
await users[0].restart();
}, RECEIVE + PAUSE);
var round = 0;
setTimeout(async () => {
round += 1;
const start = new Date();
await users[0].pause();
const end = new Date();
console.log("round : ", round, " ", end - start);
setInterval(async() => {
round += 1;
const start = new Date();
await users[0].pause();
const end = new Date();
console.log("round : ", round, " ", end - start);
}, PAUSE + RECEIVE);
}, RECEIVE);
const no_of_txs = parseInt(eachLine[0]);
for (var it = 1; it <= no_of_txs; it++) {
const type = eachLine[it][1];
const owner = parseInt(eachLine[it][2]);
const when = parseInt(eachLine[it][0]) + latencies[owner];
if (type == 'DEPOSIT') {
const to = parseInt(eachLine[it][3]);
const stock = eachLine[it][4];
const amount = parseInt(eachLine[it][5]);
if (stock == 'TOKENS') {
pattern(async() => {await users[owner].addTokens(accounts[to], amount);}, when);
} else {
pattern(async() => {await users[owner].addStocks(accounts[to], stock, amount);}, when);
}
}
else {
const stock = eachLine[it][3];
const amount = parseInt(eachLine[it][4]);
const price = parseInt(eachLine[it][5]);;
if (type == 'SELL') {
pattern(async() => {await users[owner].sell(stock, amount, price);}, when);
}
else if (type == 'BUY') {
pattern(async() => {await users[owner].buy(stock, amount, price);}, when);
} else {
console.error("invalid tx type");
}
}
}
}
init()