-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
343 lines (271 loc) · 8.88 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
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
let debug = require('./debug')('indexd')
let dbwrapper = require('./dbwrapper')
let { EventEmitter } = require('events')
let parallel = require('run-parallel')
let rpcUtil = require('./rpc')
let FeeIndex = require('./indexes/fee')
let MtpIndex = require('./indexes/mediantime')
let ScriptIndex = require('./indexes/script')
let TxIndex = require('./indexes/tx')
let TxinIndex = require('./indexes/txin')
let TxoIndex = require('./indexes/txo')
function txoToString ({ txId, vout }) {
return `${txId}:${vout}`
}
function Indexd (db, rpc) {
this.db = dbwrapper(db)
this.rpc = rpc
this.emitter = new EventEmitter() // TODO: bind to this
this.emitter.setMaxListeners(Infinity)
this.indexes = {
fee: new FeeIndex(),
mtp: new MtpIndex(),
script: new ScriptIndex(),
tx: new TxIndex(),
txin: new TxinIndex(),
txo: new TxoIndex()
}
}
Indexd.prototype.tips = function (callback) {
let tasks = {}
for (let indexName in this.indexes) {
let index = this.indexes[indexName]
tasks[indexName] = (next) => index.tip(this.db, next)
}
parallel(tasks, callback)
}
// recurses until `nextBlockId` is falsy
Indexd.prototype.connectFrom = function (prevBlockId, blockId, callback) {
this.tips((err, tips) => {
if (err) return callback(err)
let todo = {}
for (let indexName in tips) {
let tip = tips[indexName]
if (tip && tip.blockId !== prevBlockId) continue
if (indexName === 'fee') {
if (!tips.txo) continue
if (tip && tips.fee.height > tips.txo.height) continue
}
todo[indexName] = true
}
let todoList = Object.keys(todo)
if (todoList.length === 0) return callback(new RangeError('Misconfiguration'))
debug(`Downloading ${blockId} (for ${todoList})`)
rpcUtil.block(this.rpc, blockId, (err, block) => {
if (err) return callback(err)
let atomic = this.db.atomic()
let events // TODO
let { height } = block
debug(`Connecting ${blockId} @ ${height}`)
// connect block to relevant chain tips
for (let indexName in todo) {
let index = this.indexes[indexName]
if (!index.connect) continue
index.connect(atomic, block, events)
}
atomic.write((err) => {
if (err) return callback(err)
debug(`Connected ${blockId} @ ${height}`)
let self = this
function loop (err) {
if (err) return callback(err)
// recurse until nextBlockId is falsy
if (!block.nextBlockId) return callback(null, true)
self.connectFrom(blockId, block.nextBlockId, callback)
}
if (!todo.fee) return loop()
debug(`Connecting ${blockId} (2nd Order)`)
let atomic2 = this.db.atomic()
this.indexes.fee.connect2ndOrder(this.db, this.indexes.txo, atomic2, block, (err) => {
if (err) return loop(err)
debug(`Connected ${blockId} (2nd Order)`)
atomic2.write(loop)
})
})
})
})
}
Indexd.prototype.disconnect = function (blockId, callback) {
debug(`Disconnecting ${blockId}`)
function fin (err) {
if (err) return callback(err)
debug(`Disconnected ${blockId}`)
callback()
}
this.tips((err, tips) => {
if (err) return fin(err)
// TODO: fetch lazily
rpcUtil.block(this.rpc, blockId, (err, block) => {
if (err) return fin(err)
let atomic = this.db.atomic()
// disconnect block from relevant chain tips
for (let indexName in this.indexes) {
let index = this.indexes[indexName]
let tip = tips[indexName]
if (!tip) continue
if (tip.blockId !== block.blockId) continue
index.disconnect(atomic, block)
}
atomic.write(fin)
})
})
}
// empties the mempool
Indexd.prototype.clear = function () {
for (let indexName in this.indexes) {
this.indexes[indexName].constructor()
}
}
Indexd.prototype.lowestTip = function(callback) {
this.tips((err, tips) => {
if (err) return callback(err)
let lowest
for (let key in tips) {
let tip = tips[key]
if (!tip) return callback()
if (!lowest) lowest = tip
if (lowest.height < tip.height) continue
lowest = tip
}
callback(null, lowest)
})
}
Indexd.prototype.__resync = function (done) {
debug('resynchronizing')
parallel({
bitcoind: (f) => rpcUtil.tip(this.rpc, f),
indexd: (f) => this.lowestTip(f)
}, (err, r) => {
if (err) return done(err)
// Step 0, genesis?
if (!r.indexd) {
debug('genesis')
return rpcUtil.blockIdAtHeight(this.rpc, 0, (err, genesisId) => {
if (err) return done(err)
this.connectFrom(null, genesisId, done)
})
}
// Step 1, equal?
debug('...', r)
if (r.bitcoind.blockId === r.indexd.blockId) return done()
// Step 2, is indexd behind? [aka, does bitcoind have the indexd tip]
rpcUtil.headerJSON(this.rpc, r.indexd.blockId, (err, common) => {
// if (err && /not found/.test(err.message)) return fin(err) // uh, burn it to the ground
if (err) return done(err)
// forked?
if (common.confirmations === -1) {
debug('forked')
return this.disconnect(r.indexd.blockId, (err) => {
if (err) return done(err)
this.__resync(done)
})
}
// yes, indexd is behind
debug('bitcoind is ahead')
this.connectFrom(common.blockId, common.nextBlockId, done)
})
})
}
Indexd.prototype.tryResync = function (callback) {
if (callback) {
this.emitter.once('resync', callback)
}
if (this.syncing) return
this.syncing = true
let self = this
function fin (err) {
self.syncing = false
self.emitter.emit('resync', err)
}
this.__resync((err, updated) => {
if (err) return fin(err)
if (updated) return this.tryResyncMempool(fin)
fin()
})
}
Indexd.prototype.tryResyncMempool = function (callback) {
rpcUtil.mempool(this.rpc, (err, txIds) => {
if (err) return callback(err)
this.clear()
parallel(txIds.map((txId) => (next) => this.notify(txId, next)), callback)
})
}
Indexd.prototype.notify = function (txId, callback) {
rpcUtil.transaction(this.rpc, txId, (err, tx) => {
if (err) return callback(err)
for (let indexName in this.indexes) {
let index = this.indexes[indexName]
if (!index.mempool) continue
index.mempool(tx)
}
callback()
})
}
// QUERIES
Indexd.prototype.blockIdByTransactionId = function (txId, callback) {
this.indexes.tx.heightBy(this.db, txId, (err, height) => {
if (err) return callback(err)
if (height === -1) return callback()
rpcUtil.blockIdAtHeight(this.rpc, height, callback)
})
}
Indexd.prototype.latestFeesForNBlocks = function (nBlocks, callback) {
this.indexes.fee.latestFeesFor(this.db, nBlocks, callback)
}
// returns a txo { txId, vout, value, script }, by key { txId, vout }
Indexd.prototype.txoByTxo = function (txo, callback) {
this.indexes.txo.txoBy(this.db, txo, callback)
}
// returns the height at scId was first-seen (-1 if unconfirmed)
Indexd.prototype.firstSeenScriptId = function (scId, callback) {
this.indexes.script.firstSeenScriptId(this.db, scId, callback)
}
// returns a list of txIds with inputs/outputs from/to a { scId, heightRange, ?mempool }
Indexd.prototype.transactionIdsByScriptRange = function (scRange, dbLimit, callback) {
this.txosByScriptRange(scRange, dbLimit, (err, txos) => {
if (err) return callback(err)
let txIdSet = {}
let tasks = txos.map((txo) => {
txIdSet[txo.txId] = true
return (next) => this.indexes.txin.txinBy(this.db, txo, next)
})
parallel(tasks, (err, txins) => {
if (err) return callback(err)
txins.forEach((txin) => {
if (!txin) return
txIdSet[txin.txId] = true
})
callback(null, Object.keys(txIdSet))
})
})
}
// returns a list of txos { txId, vout, height, value } by { scId, heightRange, ?mempool }
Indexd.prototype.txosByScriptRange = function (scRange, dbLimit, callback) {
this.indexes.script.txosBy(this.db, scRange, dbLimit, callback)
}
// returns a list of (unspent) txos { txId, vout, height, value }, by { scId, heightRange, ?mempool }
// XXX: despite txo queries being bound by heightRange, the UTXO status is up-to-date
Indexd.prototype.utxosByScriptRange = function (scRange, dbLimit, callback) {
this.txosByScriptRange(scRange, dbLimit, (err, txos) => {
if (err) return callback(err)
let taskMap = {}
let unspentMap = {}
txos.forEach((txo) => {
let txoId = txoToString(txo)
unspentMap[txoId] = txo
taskMap[txoId] = (next) => this.indexes.txin.txinBy(this.db, txo, next)
})
parallel(taskMap, (err, txinMap) => {
if (err) return callback(err)
let unspents = []
for (let txoId in txinMap) {
let txin = txinMap[txoId]
// has a txin, therefore spent
if (txin) continue
unspents.push(unspentMap[txoId])
}
callback(null, unspents)
})
})
}
module.exports = Indexd