-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestapi.js
389 lines (335 loc) · 16.2 KB
/
testapi.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
const pool_params = require('./config/pool_params')
const exec = require('child_process').exec;
const bitcoin_rpc = require('node-bitcoin-rpc')
const fs = require('fs')
const readLastLines = require('read-last-lines');
const axios = require('axios');
const getFreeOutputs = require('./free_outputs')
const credentials = {
rpcuser: "wael",
rpcpassword: "wael",
CREATE_MASTERNODE_FILE: "HMN_coin.sh",
rootDirectory: "/root/.hostmasternodecore"
}
function create_masternode_Promise(credentials) {
return new Promise(function (resolve, reject) {
exec(`sh ${credentials.CREATE_MASTERNODE_FILE}`, (err, stdout, stderr) => {
if (!err) {
resolve(stdout)
} else {
reject(err)
}
});
})
}
function readMnFromFile() {
return new Promise(function (resolve, reject) {
exec(`sh "getLastLine.sh"`, (err, stdout, stderr) => {
if (!err) {
resolve(stdout)
} else {
reject(err)
}
});
});
}
function writeToFile(a) {
return new Promise(function (resolve, reject) {
const data = new Uint8Array(Buffer.from(a));
fs.writeFile('newmn.txt', data, (err) => {
if (err) reject(err);
resolve('The file has been saved!');
});
})
}
function addNewMn(root, filename, ligne) {
return new Promise(function (resolve, reject) {
const data = new Uint8Array(Buffer.from(ligne));
fs.appendFile(`${root}/${filename}`, ligne, (err) => {
if (err) reject(err);
resolve('The file has been saved!');
});
})
}
function start_deamon(filename, deamon_name) {
return new Promise(function (resolve, reject) {
exec(`sh ${filename} ${deamon_name}`, (err, stdout, stderr) => {
if (stdout) {
console.log("deamon started ", stdout)
resolve(stdout)
} else {
console.log("deamon NOT started ", stderr, "and error is ", err)
reject(err, stderr)
}
})
})
}
function kill_deamon(namefile, name) {
return new Promise(function (resolve, reject) {
exec(`sh ${namefile} ${name}`, (err, stdout, stderr) => {
if (!err) {
resolve(stdout)
} else {
reject(err)
}
});
})
}
function get_mn_outputs(mn_cli_file) {
return new Promise(function (resolve, reject) {
exec(`sh get_mn_outputs.sh ${mn_cli_file}`, (err, stdout, stderr) => {
if (!err) {
resolve(stdout)
} else {
if (err) {
reject(err)
} else {
reject(stderr)
}
}
});
})
}
function get_block_count(mn_cli_file) {
return new Promise(function (resolve, reject) {
exec(`sh get_block_count.sh ${mn_cli_file}`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else if (stderr) {
reject(stderr)
} else {
if (stdout) {
resolve(stdout)
} else {
reject("no blocks founded")
}
}
});
})
}
function add_sync_block(sync_block_script_file, block_file) {
return new Promise(function (resolve, reject) {
exec(`sh ${sync_block_script_file} ${block_file}`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else if (stderr) {
reject(stderr)
} else {
if (stdout) {
resolve(stdout)
} else {
reject("add_sync_block no blocks founded")
}
}
});
})
}
function restart_server(restart_server_script_file, restart_server_cli, restart_serverd) {
console.log("start restarting server", restart_server_cli, restart_serverd)
return new Promise(function (resolve, reject) {
exec(`sh ${restart_server_script_file} ${restart_server_cli} ${restart_serverd}`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else if (stderr) {
reject(stderr)
} else {
if (stdout) {
resolve(stdout)
} else {
reject("restart_server_file error")
}
}
});
})
}
function masternode_start(masternode_start_script_file, masternode_start_cli, mnalias) {
return new Promise(function (resolve, reject) {
exec(`sh ${masternode_start_script_file} ${masternode_start_cli} ${mnalias}`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else if (stderr) {
reject(stderr)
} else {
if (stdout) {
resolve(stdout)
} else {
reject("masternode_start_file error")
}
}
});
})
}
function getTransactionInfo(txinfo_script_file, txinfo_cli, txid) {
return new Promise(function (resolve, reject) {
exec(`sh ${txinfo_script_file} ${txinfo_cli} ${txid}`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else if (stderr) {
reject(stderr)
} else {
if (stdout) {
resolve(stdout)
} else {
reject("tx info error")
}
}
});
})
}
module.exports.createMasternode = async function () {
try {
var processkilled = await kill_deamon("kill_hmn.sh", "hostmasternoded")
try {
var result = await create_masternode_Promise(credentials)
console.log("create_masternode_Promise ",create_masternode_Promise)
var res = result.toString()
try {
var newmn = await writeToFile(res)
console.log("newmn ",newmn)
try {
var readLine = await readMnFromFile()
console.log("readLine ",readLine)
console.log(readLine)
const explorer_url = "https://explorer.hostmasternode.com/insight-api/blocks?limit=1"
axios.get(`${explorer_url}`)
.then(async (response) => {
console.log("data from explorer ", response.data.blocks[0].height);
console.log("type of data from explorer ", typeof (response.data.blocks[0].height));
let block_count = 0
let explorer_block = response.data.blocks[0].height
console.log("initial blockcout ", block_count)
try {
var res = await add_sync_block('add_sync_block.sh', 'blocks')
console.log("add_sync_block ",add_sync_block)
try {
var serverrestarted = await restart_server('restart_server.sh', 'hostmasternode-cli', 'hostmasternoded')
console.log("serverrestarted ",serverrestarted)
console.log("-----------------------------------------")
console.log("-----------------------------------------")
console.log("-----------------------------------------")
console.log("-----------------------------------------")
console.log("server started ", serverrestarted)
console.log("-----------------------------------------")
console.log("-----------------------------------------")
console.log("-----------------------------------------")
console.log("-----------------------------------------")
/* while (block_count < explorer_block) {
console.log(block_count, " < ",explorer_block)
try {
block_count = await get_block_count('hostmasternode-cli')
console.log("block_count ", block_count)
} catch (error) {
console.log(error)
}
} */
/* let i = 0;
async function f1(block_countloc,explorer_blockloc) {
console.log(block_countloc, " < ",explorer_blockloc)
try {
block_count = await get_block_count('hostmasternode-cli')
console.log("block_count ", block_count)
} catch (error) {
console.log(error)
}
};
async function f() {
await f1(block_count,explorer_block);
i += 1;
var tmout = setTimeout(async() => {
if(i < 5 && (block_count < explorer_block)) {
f();
}else {
clearTimeout(tmout)
}
}, 1000);
}
var test = await f(); */
/* if (Number(block_count) == Number(response.data.blocks[0].height)) {
*/ // console.log("true condition")
// var outputs = await get_mn_outputs('hostmasternode-cli')
// console.log("outputs are ", outputs)
// call get free outputs module (newjs.js)
try {
var freeOutputs = await getFreeOutputs.getFreeOutputs();
console.log("-----------------------------------------")
console.log("-----------------------------------------")
console.log(freeOutputs)
console.log("-----------------------------------------")
console.log("-----------------------------------------")
} catch (error) {
console.log(error)
}
if (freeOutputs) {
try {
var newLine = "masternode" + freeOutputs.indexInMongodb + " " + readLine.replace(/(\r\n\t|\n|\r\t)/gm, "") + " " + freeOutputs.txid + " " + freeOutputs.index + "\n"
var mnAdded = await addNewMn(credentials.rootDirectory, "masternode.conf", newLine)
console.log("mnAdded ",mnAdded)
try {
/* var processkilled = await kill_deamon("kill_hmn.sh", "hostmasternoded")
try {
var startdeamon = await start_deamon("start_deamon.sh", "hostmasternoded")
} catch (error) { console.log(error) } */
var serverrestarted2 = await restart_server('restart_server.sh', 'hostmasternode-cli', 'hostmasternoded')
console.log("serverrestarted2 ",serverrestarted2)
try {
var mnalias = "masternode" + freeOutputs.indexInMongodb
var startmasternode = await masternode_start('masternode_start.sh', 'hostmasternode-cli', mnalias)
console.log("startmasternode ",startmasternode)
try {
var transactionInfo = await getTransactionInfo('gettransactioninfo.sh', 'hostmasternode-cli', freeOutputs.txid)
console.log("transactionInfo ",transactionInfo)
if (transactionInfo) {
return {
success: true,
masternode_name: mnalias,
masternode_address: transactionInfo.details.address
}
} else {
return { success: false, msg: "error when retrieving txInfo" }
}
} catch (error) {
return { success: false, msg: error }
}
} catch (error) {
return { success: false, msg: error }
}
console.log(serverrestarted2)
} catch (error) {
return { success: false, msg: error }
}
} catch (error) {
return { success: false, msg: error }
}
} else {
console.log("there are no free outputs")
}
} catch (error) {
return { success: false, msg: error }
}
} catch (error) {
return { success: false, msg: error }
}
}).catch(error => {
if (error.response) {
if (error.response.data == "Invalid address: Checksum mismatch. Code:1") {
return { success: false, msg: error.response.data }
} else {
return { success: false, msg: error }
}
} else {
return { success: false, msg: error }
}
});
} catch (error) {
return { success: false, msg: error }
}
} catch (error) {
return { success: false, msg: error }
}
} catch (error) {
return { success: false, msg: error }
}
} catch (error) {
return { success: false, msg: error }
}
}