forked from SaitoTech/saito-lite-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wh.ts
204 lines (190 loc) · 6.09 KB
/
wh.ts
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
// import "source-map-support/register";
import StorageCore from "./lib/saito/core/storage-core";
import { Saito } from "./apps/core";
import fs from "fs-extra";
import mods_config from "./config/modules.config";
import * as blake3 from "blake3";
async function initCLI() {
const app = new Saito({
mod_paths: mods_config.core,
});
//app.server = new Server(app);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
app.storage = new StorageCore(app);
app.hash = (data) => {
return blake3.hash(data).toString("hex");
};
app.BROWSER = 0;
app.SPVMODE = 0;
console.log("npm run cli help - for help");
switch (process.argv[2]) {
case "count":
count(process.argv[3], process.argv[4]);
break;
case "save":
loadTxToDatabase(process.argv[3]);
break;
case "version":
console.log(1);
break;
case "help":
printHelp();
break;
case "h":
printHelp();
break;
default:
console.log("Argument not recognised.");
}
function loadTxToDatabase(dir) {
let count = 0;
const files = fs.readdirSync(dir);
const total = files.length;
files.forEach((file) => {
count += 1;
console.log("Processing block " + count + " of " + total + ".");
try {
if (file !== "empty") {
app.storage.loadBlockByFilename(dir + file).then((blk) => {
addTransactionsToDatabase(blk);
});
}
} catch (err) {
console.error(err);
}
});
}
function count(what, dir) {
let x = 0;
const files = fs.readdirSync(dir);
if (what == "blocks") {
x = files.length;
console.log(x + " " + what + " on disk.");
} else {
files.forEach((file) => {
try {
if (file !== "empty") {
app.storage.loadBlockByFilename(dir + file).then((blk) => {
x += blk.transactions.length;
console.log("another: " + blk.transactions.length + " for " + x + " total.");
if (file == files[files.length - 1]) {
console.log(x + " " + what + " on disk.");
}
});
}
} catch (err) {
console.error(err);
}
});
}
}
async function addTransactionsToDatabase(blk) {
try {
for (let i = 0; i < blk.transactions.length; i++) {
if (blk.transactions[i].transaction.type >= -999) {
for (let ii = 0; ii < blk.transactions[i].transaction.to.length; ii++) {
if (blk.transactions[i].transaction.type >= -999) {
let sql = `INSERT OR IGNORE INTO transactions (
address,
amt,
bid,
tid,
sid,
bhash,
lc,
rebroadcast,
sig,
ts,
block_ts,
type,
tx_from,
tx_to,
name,
module
)
VALUES (
$address,
$amt,
$bid,
$tid,
$sid,
$bhash,
$lc,
$rebroadcast,
$sig,
$ts,
$block_ts,
$type,
$tx_from,
$tx_to,
$name,
$module
)`;
let ttype = 0;
let tname = "";
let tmodule = "";
if (blk.transactions[i].msg.type) {
ttype = blk.transactions[i].msg.type;
}
if (blk.transactions[i].msg.name) {
tname = blk.transactions[i].msg.name;
}
if (blk.transactions[i].msg.module) {
tmodule = blk.transactions[i].msg.module;
}
let tx_from = "";
if (blk.transactions[i].transaction.from.length > 0) {
tx_from = blk.transactions[i].transaction.from[0].add;
}
let params = {
$address: blk.transactions[i].transaction.to[ii].add,
$amt: blk.transactions[i].transaction.to[ii].amt,
$bid: blk.block.id,
$tid: blk.transactions[i].transaction.id,
$sid: ii,
$bhash: blk.returnHash(),
$lc: 1,
$rebroadcast: 0,
$sig: blk.transactions[i].transaction.sig,
$ts: blk.transactions[i].transaction.ts,
$block_ts: blk.block.ts,
$type: ttype,
$tx_from: tx_from,
$tx_to: blk.transactions[i].transaction.to[ii].add,
$name: tname,
$module: tmodule,
};
await app.storage.executeDatabase(sql, params, "warehouse");
}
}
}
}
return;
} catch (err) {
console.error(err);
}
}
function printHelp() {
let help = `
Commands:
block <path and file name> print block;
block.tx <path and file name> print transactions from block
blocks <directory> print out blocks in directory
blocks.tx <directory> print out all tx from all bocks in direcory
`;
console.log(help);
}
/////////////////////
// Cntl-C to Close //
/////////////////////
process.on("SIGTERM", function () {
console.log("Network Shutdown");
process.exit(0);
});
process.on("SIGINT", function () {
console.log("Network Shutdown");
process.exit(0);
});
}
initCLI();