-
Notifications
You must be signed in to change notification settings - Fork 5
/
processor.js
executable file
·53 lines (53 loc) · 1.69 KB
/
processor.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
const bitwork = require('bitwork')
const tape = require('./tape')
const tapeFile = "/tape.txt"
// Next command
class Processor {
constructor (o) {
this.gene = o.gene;
this.crawler = o.crawler;
this.cmd = o.cmd;
}
async next () {
try {
let info = await this.crawler.fetcher.get("info")
let current = await tape.current({ start: 1, end: info.blocks }, this.gene)
let header = await this.cmd.BLOCK(current, info) // BLOCK
console.log("next blockhash: ", header.nextblockhash)
if (header.nextblockhash) {
await this.next();
} else {
console.log("Finished crawl mode. Enter listen mode...")
this.listen()
}
} catch (e) {
if (e.code === -8) { // -8: block height out of range
console.log("Enter listen mode...")
this.listen()
} else {
console.log("error", e)
}
}
}
listen () {
this.crawler.listener.on("mempool", async (e) => {
await this.gene.onmempool({
tx: e, network: this.crawler.fetcher
})
await tape.write("MEMPOOL " + e.tx.h + " " + Date.now(), this.gene.tape + tapeFile)
})
this.crawler.listener.on("block", async (block) => {
let info = await this.crawler.fetcher.get("info")
let current = await tape.current({ start: 1, end: info.blocks }, this.gene)
await this.gene.onblock({
header: block.header,
tx: block.tx,
tape: current.tape,
network: this.crawler.fetcher
})
await tape.write("BLOCK " + block.header.height + " " + block.header.hash + " " + block.header.prevHash + " " + Date.now(), this.gene.tape + tapeFile)
await this.next();
})
}
}
module.exports = Processor