forked from SilentNotaryEcosystem/Cil-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (154 loc) · 5.43 KB
/
index.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
const factory = require('./factory');
const {
readCmdLineOptions,
sleep,
stripAddressPrefix,
readPrivateKeyFromFile,
mapEnvToOptions,
mapOptionsToNodeParameters
} = require('./utils');
process.on('warning', e => console.warn(e.stack));
(async () => {
await factory.asyncLoad();
logger.log(`Using ${factory.Constants.strIdent} config`);
// Read user-defined parameters
const objUserParams = {
// read ENV options
...mapEnvToOptions(),
// read command line options (have precedence over ENV)
...readCmdLineOptions()
};
// override global parameters
if (objUserParams.genesisHash) factory.Constants.GENESIS_BLOCK = objUserParams.genesisHash;
if (objUserParams.conciliumDefContract) {
factory.Constants.CONCILIUM_DEFINITION_CONTRACT_ADDRESS = objUserParams.conciliumDefContract;
}
let commonOptions = {
...setImpliedParameters(objUserParams),
...mapOptionsToNodeParameters(objUserParams)
};
// if there are wallet tasks - program will terminate after completion!
await walletTasks(commonOptions);
// if there is rebuild task - program will terminate after completion!
await rebuildDb(commonOptions);
// this will completely erase DB, and resync it from neighbors
await clearDb(commonOptions);
let node;
if (objUserParams.privateKey) {
const decryptedPk = await readPrivateKeyFromFile(factory.Crypto, objUserParams.privateKey);
if (!decryptedPk) throw new Error('failed to decrypt file with private key');
const witnessWallet = new factory.Wallet(decryptedPk);
node = new factory.Witness({
...commonOptions,
wallet: witnessWallet
});
} else {
node = new factory.Node({
...commonOptions
});
}
process.on('SIGINT', node.gracefulShutdown.bind(node));
process.on('SIGTERM', node.gracefulShutdown.bind(node));
await node.ensureLoaded();
await node.bootstrap();
// it's a witness node
if (typeof node.start === 'function') {
// if it returns false, than we still have no definition for our witness,
// possibly it's because we haven't loaded respective block. let's loop until we got it
while (!(await node.start())) await sleep(1000);
}
})().catch(err => {
console.error(err);
});
async function rebuildDb(objCmdLineParams) {
const {rebuildDb} = objCmdLineParams;
if (!rebuildDb) return;
try {
const storage = new factory.Storage({...objCmdLineParams, mutex: new factory.Mutex()});
await storage.dropAllForReIndex();
const node = new factory.Node({...objCmdLineParams, workerSuspended: true, networkSuspended: true});
await node.ensureLoaded();
await node.rebuildDb();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
}
async function clearDb(objCmdLineParams) {
try {
const storage = new factory.Storage({...objCmdLineParams, mutex: new factory.Mutex()});
if (await storage.hasBlock('5cd32a04238a61a29d95ed48ce6b08ba2973b6fb0858446b76bb20c93e5492b4')) {
await storage.dropAllForReIndex(true);
console.log('Db cleared');
}
await storage.close();
} catch (e) {
console.error(e);
process.exit(1);
}
}
async function walletTasks(objCmdLineParams) {
const {listWallets, reIndexWallet, watchAddress} = objCmdLineParams;
if (!(listWallets || reIndexWallet || watchAddress)) return;
const storage = new factory.Storage({
walletSupport: true,
mutex: new factory.Mutex(),
...objCmdLineParams
});
// add new wallets
if (watchAddress && watchAddress.length) await taskWatchWallets(storage, watchAddress);
// reindex
if (reIndexWallet) await taskReindexWallets(storage);
// list
if (listWallets) await taskListWallets(storage);
process.exit(0);
// -----------------------------
async function taskListWallets(storage) {
const arrAddresses = await storage.getWalletsAddresses();
if (!arrAddresses.length) {
console.log('No addresses found in wallet');
} else {
console.log('Addresses found in wallets');
console.dir(await storage.getWalletsAddresses(), {colors: true, depth: null});
}
}
async function taskReindexWallets(storage) {
await storage.walletReIndex();
}
async function taskWatchWallets(storage, arrWatchAddresses) {
for (let addr of arrWatchAddresses) {
await storage.walletWatchAddress(stripAddressPrefix(factory.Constants, addr));
}
}
}
/**
* Let user skip some parameters.
* I.e. if he set rpcUser, we think he wants RPC, but RPC will be started only if rpcAddress present. So let's set it
*
* @param {Object} objUserParams
*/
function setImpliedParameters(objUserParams) {
let objAddOn = {};
if (objUserParams.localDevNode) {
objAddOn = {
...objAddOn,
arrDnsSeeds: ['non-existed.cil'],
listenPort: 28223,
arrSeedAddresses: ['1.1.1.1']
};
}
if (objUserParams.rpcUser) {
objAddOn = {
...objAddOn,
rpcAddress: '0.0.0.0'
};
}
if (objUserParams.reIndexWallet || objUserParams.watchAddress) {
objAddOn = {
...objAddOn,
walletSupport: true
};
}
return objAddOn;
}