forked from tmpim/Krist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertdb.js
executable file
·291 lines (244 loc) · 6.83 KB
/
convertdb.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Created by Drew Lemmy, 2016
*
* This file is part of Krist.
*
* Krist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Krist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Krist. If not, see <http://www.gnu.org/licenses/>.
*
* For more project information, see <https://github.com/Lemmmy/Krist>.
*/
var config = require('./config.js'),
package = require('./package.json'),
colours = require('colors'),
yesno = require('yesno'),
fs = require('fs'),
Progress = require('progress'),
sqlite3 = require('sqlite3'),
database = require('./src/database.js');
var progress = null;
var schemas = null;
var db = null;
function connect() {
console.log('Connecting to DB...');
database.init().then(function() {
schemas = require('./src/schemas.js');
db = new sqlite3.Database('data.db');
confirmBegin();
});
}
function confirmBegin() {
console.log('');
console.log('======[WARNING]======'.red.bold);
console.log('This script will delete any existing Krist data in your database.');
console.log('');
console.log('The following tables will be ' + 'deleted'.red + ': ')
console.log('');
for (var schema in schemas) {
if (schemas.hasOwnProperty(schema)) {
console.log(' - ' + schemas[schema].getTableName().toString().bold);
}
}
console.log('');
yesno.ask('Continue? [Y/n]', true, function(ok) {
if (ok) {
console.log('Checking prerequisites');
fs.stat('data.db', function(err, stat) {
if (err) {
console.log('Error:'.red + ' data.db does not exist or have read permissions. Exiting.');
process.exit();
}
begin();
});
} else {
console.log('Exiting.');
process.exit();
}
});
}
function begin() {
console.log('Cleaning up old database');
database.getSequelize().truncate().then(function() {
convertAddresses().then(function() {
db.get('SELECT COUNT(*) as count FROM blocks', function (err, result) {
convertBlocks(2500, result.count).then(function() {
convertNames().then(function() {
db.get('SELECT COUNT(*) as count FROM transactions', function (err, res) {
convertTransactions(2500, res.count).then(function() {
console.log('Done.');
process.exit();
});
});
});
});
});
});
});
}
function convertAddresses() {
return new Promise(function(resolve, reject) {
db.all("SELECT * FROM 'addresses'", function(err, rows) {
if (err) {
reject(err);
return;
}
progress = new Progress('Addresses [:bar] :percent :etas', {
total: rows.length,
complete: '='.green.bold,
incomplete: '-'.red
});
rows.forEach(function(row) {
schemas.address.create({
id: row.id,
address: row.address,
balance: row.balance,
totalin: row.totalin,
totalout: row.totalout,
firstseen: new Date(row.firstseen * 1000)
}).then(function() {
progress.tick();
if (progress.complete) {
resolve();
}
}).catch(reject);
});
});
});
}
function convertBlocks(limit, count) {
return new Promise(function(resolve, reject) {
progress = new Progress('Blocks [:bar] :percent :etas', {
total: count,
complete: '='.green.bold,
incomplete: '-'.red
});
function convertBlocksPart(limit, offset, count) {
db.all("SELECT * FROM blocks LIMIT " + limit + " OFFSET " + offset, function (err, rows) {
if (err) {
reject(err);
return;
}
var raws = []; // what the hell was i thinking when i wrote this? whatever
rows.forEach(function (row) {
if (row.nonce == Number.POSITIVE_INFINITY || row.nonce == Number.NEGATIVE_INFINITY) {
row.nonce = 0;
}
raws.push({
value: row.value,
hash: row.hash,
address: row.address,
nonce: row.nonce,
time: new Date(row.time * 1000),
difficulty: row.difficulty
});
})
schemas.block.bulkCreate(raws).then(function () {
if (offset + limit > count) {
progress.tick(limit);
if (progress.complete) {
resolve();
}
} else {
progress.tick(limit);
convertBlocksPart(limit, offset + limit, count);
}
}).catch(reject);
});
}
convertBlocksPart(limit, 0, count);
});
}
function convertNames() {
return new Promise(function(resolve, reject) {
db.all("SELECT * FROM 'names'", function (err, rows) {
if (err) {
reject(err);
return;
}
progress = new Progress('Names [:bar] :percent :etas', {
total: rows.length,
complete: '='.green.bold,
incomplete: '-'.red
});
rows.forEach(function(row) {
schemas.block.findOne({
where: {
id: row.registered
}
}).then(function (r1) {
schemas.block.findOne({
where: {
id: row.updated
}
}).then(function (r2) {
schemas.name.create({
id: row.id,
name: row.name,
owner: row.owner,
registered: r1.time,
updated: r2.time,
a: row.a,
unpaid: row.unpaid
}).then(function() {
progress.tick();
if (progress.complete) {
resolve();
}
}).catch(reject);
}).catch(reject);
}).catch(reject);
});
});
});
}
function convertTransactions(limit, count) {
return new Promise(function(resolve, reject) {
progress = new Progress('Transactions [:bar] :percent :etas', {
total: count,
complete: '='.green.bold,
incomplete: '-'.red
});
function convertTransactionsPart(limit, offset, count) {
db.all("SELECT * FROM transactions LIMIT " + limit + " OFFSET " + offset, function (err, rows) {
if (err) {
reject(err);
return;
}
var raws = [];
rows.forEach(function (row) {
raws.push({
from: row.from,
to: row.to,
value: row.value,
time: new Date(row.time * 1000),
name: row.name,
op: row.op
});
})
schemas.transaction.bulkCreate(raws).then(function () {
if (offset + limit > count) {
progress.tick(limit);
if (progress.complete) {
resolve();
}
} else {
progress.tick(limit);
convertTransactionsPart(limit, offset + limit, count);
}
}).catch(reject);
});
}
convertTransactionsPart(limit, 0, count);
});
}
connect();