Repository navigation
Expand file tree
/
Copy pathplanaria.js
More file actions
executable file
·200 lines (199 loc) · 6.13 KB
/
Copy pathplanaria.js
File metadata and controls
executable file
·200 lines (199 loc) · 6.13 KB
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
const Planaria = require('gridplanaria')
const MongoClient = require('mongodb')
const mingo = require('mingo')
const path = require('path')
const bitpic = require('bitpic')
const fs = require('fs')
var db;
const save = function(tx) {
let hash = tx.tx.h;
let ps = tx.out.map(function(out) {
return new Promise(function(resolve, reject) {
let buf = null
let content
if (out.tape.length > 2) {
if (out.tape[1].cell[1].lb && typeof out.tape[1].cell[1].lb === 'string') {
buf = Buffer.from(out.tape[1].cell[1].lb, 'base64');
} else if (out.tape[1].cell[1].b && typeof out.tape[1].cell[1].b === 'string') {
buf = Buffer.from(out.tape[1].cell[1].b, 'base64');
}
fs.writeFile(process.cwd() + '/files/' + out.tape[2].cell[1].s, buf, function(er) {
if (er) {
console.log("Error = ", er)
reject()
} else {
resolve(out.tape[2].cell[1].s)
}
})
} else {
resolve(null)
}
})
})
return Promise.all(ps)
}
const connect = function(cb) {
MongoClient.connect("mongodb://localhost:27017", {useNewUrlParser: true}, function(err, client) {
if (err) {
console.log("retrying...")
setTimeout(function() {
connect(cb);
}, 1000)
} else {
let id = "bitpic"
db = client.db(id)
cb();
}
})
}
const planaria = new Planaria();
var _filter = new mingo.Query({
"out.tape.cell.s": "18pAqbYqhzErT6Zk3a5dwxHtB9icv8jH2p"
})
planaria.start({
filter: {
//"from": 609000,
"from": 609341,
"host": {
rpc: { user: "root", pass: "bitcoin" }
},
"l": {
filter: (e) => {
let matched = (
e.out[0].tape.length > 2 &&
e.out[0].tape[2] &&
e.out[0].tape[2].cell[0] &&
e.out[0].tape[2].cell[0].s === "18pAqbYqhzErT6Zk3a5dwxHtB9icv8jH2p" ) ||
(
e.out.length > 1 &&
e.out[1].tape.length > 2 &&
e.out[1].tape[2] &&
e.out[1].tape[2].cell[0] &&
e.out[1].tape[2].cell[0].s === "18pAqbYqhzErT6Zk3a5dwxHtB9icv8jH2p"
)
if (matched) console.log("Matched", JSON.stringify(e,null,2))
return matched
}
}
},
onmempool: async function(e) {
console.log("E = ", e)
let valid = await bitpic.verify(e.tx, { format: "bob" })
if (valid) {
let paymails = await save(e.tx)
let paymail = paymails.filter((p) => {
return p
})
console.log("delete u")
try {
await db.collection("u").deleteMany({
"out.tape.cell.s": {
"$in": paymail
}
})
console.log("delete c")
await db.collection("c").deleteMany({
"out.tape.cell.s": {
"$in": paymail
}
})
console.log("insert u", e.tx)
await db.collection("u").insertMany([e.tx])
console.log("Inserted u")
} catch (e) {
console.log("Error", e)
}
} else {
console.log("Invalid format", e.tx.tx.h)
}
},
onblock: function(e) {
console.log("onblock")
return new Promise((resolve, reject) => {
let txArrayStream = e.tx(100) // Get a stream of transaction arrays
console.time("block " + e.header.height)
txArrayStream.on("data", async (txArray) => {
txArrayStream.pause();
let invalidTxs = [];
for(let i=0; i<txArray.length; i++) {
let valid = await bitpic.verify(txArray[i], { format: "bob" })
if (valid) {
let paymails = await save(txArray[i])
let paymail = paymails.filter((p) => {
return p
})
await db.collection("c").deleteMany({
"out.tape.cell.s": {
"$in": paymail
}
})
} else {
console.log("Invalid format", txArray[i].tx.h)
invalidTxs.push(txArray[i].tx.h)
}
}
if (invalidTxs.length > 0) {
console.log("Filtering invalid txs...", invalidTxs)
txArray = txArray.filter((tx) => {
return invalidTxs.includes(tx.tx.h)
})
}
let paymailSet = new Set();
let dedupTxArray = [];
for(let i=txArray.length-1; i>=0; i--) {
let tx = txArray[i];
let paymail = tx.out[0].tape[2].cell[1].s
console.log("FOund paymail", paymail)
if (!paymailSet.has(paymail)) {
dedupTxArray.push(tx)
}
paymailSet.add(paymail)
}
txArray = dedupTxArray.reverse()
console.log("inserting", txArray.length)
try {
await db.collection("c").insertMany(txArray) // insert each batch
} catch (err) {
console.log("Error = ", err)
}
console.log("inserted")
txArrayStream.resume();
})
.on("end", () => {
console.log("block End", e.header.height)
db.collection("u").deleteMany({}).then(() => {
console.timeEnd("block " + e.header.height)
resolve()
})
})
})
},
onstart: function(e) {
return new Promise(async function(resolve, reject) {
if (!e.tape.self.start) {
await planaria.exec("docker", ["pull", "mongo:4.0.4"])
await planaria.exec("docker", ["run", "-d", "-p", "27017-27019:27017-27019", "-v", process.cwd() + "/db:/data/db", "mongo:4.0.4"])
}
if (!fs.existsSync(process.cwd() + "/files")) {
fs.mkdirSync(process.cwd() + "/files")
}
connect(async () => {
console.log("creating index")
await db.collection("c").createIndex({"tx.h": 1}, { unique: true})
await db.collection("c").createIndex({"blk.i": 1})
await db.collection("c").createIndex({"out.tape.cell.s": 1})
await db.collection("u").createIndex({"tx.h": 1}, { unique: true})
await db.collection("u").createIndex({"out.tape.cell.s": 1})
console.log("created index")
if (e.tape.self.start) {
await db.collection("c").deleteMany({
"blk.i": { "$gt": e.tape.self.end }
})
resolve()
} else {
resolve();
}
})
})
},
})