/
block.js
88 lines (88 loc) · 2.58 KB
/
block.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
const fs = require('fs')
const es = require('event-stream')
const crypto = require('crypto')
const Log = require('./log')
const writetape = function(current_block, path) {
let l = "BLOCK " + current_block + " " + Date.now() + "\n"
fs.appendFileSync(path + "/tape.txt", l);
}
const crawl = function(stream, o, path, cb) {
let str = stream
.pipe(es.split())
.pipe(es.filterSync(function(data) { return !(["[", ",", "]"].includes(data.toString())) }))
.pipe(es.parse())
if (o.l && o.l.map) {
str = str.pipe(es.map(function(data, callback) {
let parsed = o.l.map(data)
let e = {
m: parsed,
tx: data.tx,
blk: data.blk
}
callback(null, e)
}))
}
let current_block;
let fileStream
str.on('data', function(data) {
if (process.env.DEV) {
Log.debug("BITBUS", data)
} else {
if (!current_block) {
current_block = data.blk.i;
fileStream = fs.createWriteStream(path + "/" + data.blk.i + ".json")
Log.debug("BITBUS", "start writing block", current_block)
fileStream.write("[\n" + JSON.stringify(data))
} else {
if (current_block < data.blk.i) {
fileStream.write("\n]")
fileStream.close();
writetape(current_block, path);
current_block = data.blk.i;
fileStream = fs.createWriteStream(path + "/" + data.blk.i + ".json")
Log.debug("BITBUS", "start writing block", current_block)
fileStream.write("[\n" + JSON.stringify(data))
} else {
fileStream.write(",\n" + JSON.stringify(data))
}
}
}
});
str.on('error', function(e) {
Log.debug("BITBUS", e)
process.exit();
})
str.on('close', function() {
if (current_block === undefined) {
Log.debug("BITBUS", "finished crawling - no transactions found")
} else if (!process.env.DEV) {
Log.debug("BITBUS", "all finished at block " + current_block)
fileStream.write("]")
fileStream.close();
writetape(current_block, path);
}
cb()
})
}
const save = function(stream, cb) {
let filestream = fs.createWriteStream(filename, {autoClose: false})
filestream.write("{\"bus\":")
stream.on("error", function() {
Log.debug("BITBUS", "stream error")
filestream.close();
})
stream.on("end", function() {
Log.debug("BITBUS", "stream finish")
filestream.write("}")
filestream.close();
})
stream.on("close", function() {
Log.debug("BITBUS", "stream close")
filestream.close();
o.onfinish();
})
stream.pipe(filestream)
}
module.exports = {
crawl: crawl, save: save
}