-
Notifications
You must be signed in to change notification settings - Fork 4
/
SnapshotInject.js
273 lines (232 loc) · 9.78 KB
/
SnapshotInject.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
const fs = require('fs');
const readline = require('readline');
const eosjs = require('eosjs');
const fetch = require('node-fetch');
const {TextDecoder, TextEncoder} = require('text-encoding');
const Throttle = require('promise-parallel-throttle');
const logger = require('single-line-log');
const log = logger.stdout;
class SnapshotHandler {
constructor(opts) {
this.jsonrpc = new eosjs.JsonRpc(opts.httpEndpoint, {fetch});
this.shouldInject = opts.inject;
this.shouldValidate = opts.validate;
this.shouldValidateStake = opts.validateStake;
this.shouldWriteCsv = opts.writeCsv;
this.debugAccounts = opts.debugAccounts;
this.debug = opts.debug;
let sigProvider = this.shouldInject ? new eosjs.JsSignatureProvider([opts.privateKey]) : null;
this.api = new eosjs.Api({
rpc: this.jsonrpc,
signatureProvider: sigProvider,
textEncoder: new TextEncoder,
textDecoder: new TextDecoder
});
this.snapshotInput = opts.snapshotInput;
this.balancesChecked = 0;
this.accountsCreated = 0;
this.queuesWritten = 0;
this.creationActionQueue = [];
this.accounts = {};
if (this.shouldWriteCsv) {
this.snapshotOutput = opts.snapshotOutput;
console.log("Writing to " + this.snapshotOutput);
try {
fs.unlinkSync(this.snapshotOutput);
} catch (e) {
console.warn(this.snapshotOutput + " did not yet exist");
}
}
}
forcePrecision(val) {
return parseFloat(parseFloat(val, 10).toFixed(4), 10);
}
async run() {
await this.checkIssued();
await this.parseFile();
}
async checkIssued() {
await this.jsonrpc.get_table_rows({
code: "eosio.token",
table: "stat",
scope: "TLOS"
}).then(res => {
this.contractSupply = parseFloat(res.rows[0].supply.split(" ")[0]);
});
}
checkBalance(acctResult) {
let acctObj = this.accounts[acctResult.account_name];
let liquid = acctResult.hasOwnProperty("core_liquid_balance") ? acctResult.core_liquid_balance.split(" ")[0] : 0;
let cpu = acctResult.total_resources && acctResult.total_resources.hasOwnProperty("cpu_weight") ? acctResult.total_resources.cpu_weight.split(" ")[0] : 0;
let net = acctResult.total_resources && acctResult.total_resources.hasOwnProperty("net_weight") ? acctResult.total_resources.net_weight.split(" ")[0] : 0;
let liquidFloat = parseFloat(liquid);
let cpuStakedFloat = parseFloat(cpu);
let netStakedFloat = parseFloat(net);
if (acctResult.hasOwnProperty("refund_request") && acctResult.refund_request) {
cpuStakedFloat += parseFloat(acctResult.refund_request && acctResult.refund_request.hasOwnProperty("cpu_amount") ? acctResult.refund_request.cpu_amount.split(" ")[0] : 0);
netStakedFloat += parseFloat(acctResult.refund_request && acctResult.refund_request.hasOwnProperty("net_amount") ? acctResult.refund_request.net_amount.split(" ")[0] : 0);
}
let foundBalance = (liquidFloat + cpuStakedFloat + netStakedFloat).toFixed(4);
let snapshotBalance = parseFloat(acctObj.snapshotBalance.startsWith(".") ? "0" + acctObj.snapshotBalance : acctObj.snapshotBalance).toFixed(4);
if (foundBalance != snapshotBalance)
console.log(acctResult.account_name + " snapshot had balance: " + snapshotBalance + " but this script found: " + foundBalance);
acctObj.balance = foundBalance;
}
async injectAccount(accountName) {
let thisAcct = this.accounts[accountName];
if (this.debug) {
console.log(JSON.stringify(thisAcct, null, 4));
}
let actions = [{
account: 'snapshots.tf',
name: 'setbalance',
authorization: [{
actor: 'jesse.tf',
permission: 'inject',
}],
data: {
snapshot_id: 1,
account: accountName,
amount: Math.round(parseFloat(thisAcct.snapshotBalance.startsWith(".") ? "0" + thisAcct.snapshotBalance : thisAcct.snapshotBalance, 10) * 10000)
}
}];
let createResult = await this.api.transact({
actions
}, {
blocksBehind: 3,
expireSeconds: 30,
}).then(r => {
log("Created " + ++this.accountsCreated + " balances");
}).catch(e => {
console.log("Error while sending action: " + e.message + "\naction was: " + JSON.stringify(actions, null, 4));
process.exit(1);
});
}
async validateAccount(accountName) {
let thisParser = this;
await this.jsonrpc.get_account(accountName).then(acct => {
this.checkBalance(acct);
}).catch(err => {
console.error("Error with accountName: " + accountName + " and error:\n" + err);
});
}
writeRow(accountName) {
let acct = this.accounts[accountName];
if (!acct) {
console.error("Couldn't get user object to write row: " + accountName);
} else if (balanceInt !== 0) {
writer.write(thisAcct.accountName +
"," + thisAcct.balance +
"," + thisAcct.cpuStake.toFixed(4) +
"," + thisAcct.netStake.toFixed(4) +
"," + thisAcct.liquid.toFixed(4) + "\n");
}
}
async injectAll() {
console.log("Injecting all accounts " + new Date());
let actionBatches = [];
let currentBatch = [];
let max = 600;
let accountList = Object.keys(this.accounts);
for (let i = 0; i < accountList.length; i++) {
if (currentBatch.length >= max) {
actionBatches.push(currentBatch);
currentBatch = [];
}
let accountName = accountList[i];
let thisAcct = this.accounts[accountName];
currentBatch.push({
account: 'snapshots.tf',
name: 'setbalance',
authorization: [{
actor: 'jesse.tf',
permission: 'inject',
}],
data: {
snapshot_id: 1,
account: accountName,
amount: Math.round(parseFloat(thisAcct.snapshotBalance.startsWith(".") ? "0" + thisAcct.snapshotBalance : thisAcct.snapshotBalance, 10) * 10000)
}});
}
if (currentBatch.length > 0)
actionBatches.push(currentBatch);
for (let i = 0; i < actionBatches.length; i++) {
await this.api.transact({
actions: actionBatches[i]
}, {
blocksBehind: 3,
expireSeconds: 30,
}).then(r => {
log("Created " + ++this.accountsCreated + " balances");
}).catch(e => {
console.log("Error while sending action: " + e.message + "\naction was: " + JSON.stringify(actionBatches[i], null, 4));
process.exit(1);
});
}
console.log("Injecting complete " + new Date());
}
async validateAll() {
console.log("Validating all accounts " + new Date());
const queue = Object.keys(this.accounts).map(account => () => this.validateAccount(account));
const completedQueue = await Throttle.all(queue, {
maxInProgress: 8
});
console.log("Validating complete " + new Date());
}
async writeCsv() {
for (let accountName in this.accounts)
this.writeRow(accountName);
}
async parseFile() {
console.log("Parsing: " + JSON.stringify(this.snapshotInput));
let snapMeta = {
account_count: 0,
total_balance: 0.0
};
this.snapMeta = snapMeta;
let accounts = {};
let rl = readline.createInterface({
input: fs.createReadStream(this.snapshotInput),
terminal: false
});
let thisParser = this;
rl.on('line', async function(line) {
/*
let parts = line.split(',');
let accountName = parts[2];
let pubKey = parts[3];
let balance = parts[4];
if (accountName.length != 12 || pubKey.length != 53) {
console.log("CANNOT HANDLE THIS LINE, SKIPPING IT: " + line);
return;
}
*/
snapMeta.account_count++;
//let accountName = line.replace(/"/g, "")
let parts = line.split(',');
let accountName = parts[0];
let snapshotBalance = parts[1];
accounts[accountName] = {
snapshotBalance
};
});
rl.on('close', async function() {
thisParser.accounts = accounts;
if (thisParser.shouldInject)
await thisParser.injectAll();
if (thisParser.shouldValidate)
await thisParser.validateAll();
if (thisParser.shouldWriteCsv)
await thisParser.writeCsv();
console.log("Account count: " + snapMeta.account_count + "\n" +
"Total balance: " + snapMeta.total_balance.toFixed(4));
let contractSupply = thisParser.contractSupply.toFixed(4);
let snapshotSupply = thisParser.snapMeta.total_balance.toFixed(4);
if (contractSupply != snapshotSupply) {
console.error("Contract supply was " + contractSupply + " TLOS and snapshot file had " + snapshotSupply + " TLOS, a difference of " + (thisParser.snapMeta.total_balance - thisParser.contractSupply).toFixed(4) + " TLOS\n\n");
}
});
}
}
module.exports = SnapshotHandler;