Skip to content

Commit 48a97bd

Browse files
author
Daniel Cousens
committed
add MTP index, resolves #9
1 parent ccf187f commit 48a97bd

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎index.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let parallel = require('run-parallel')
55
let rpcUtil = require('./rpc')
66

77
let FeeIndex = require('./indexes/fee')
8+
let MtpIndex = require('./indexes/mediantime')
89
let ScriptIndex = require('./indexes/script')
910
let TxIndex = require('./indexes/tx')
1011
let TxinIndex = require('./indexes/txin')
@@ -20,11 +21,12 @@ function Indexd (db, rpc) {
2021
this.emitter = new EventEmitter() // TODO: bind to this
2122
this.emitter.setMaxListeners(Infinity)
2223
this.indexes = {
24+
fee: new FeeIndex(),
25+
mtp: new MtpIndex(),
2326
script: new ScriptIndex(),
2427
tx: new TxIndex(),
2528
txin: new TxinIndex(),
26-
txo: new TxoIndex(),
27-
fee: new FeeIndex()
29+
txo: new TxoIndex()
2830
}
2931
}
3032

‎indexes/mediantime.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
let typeforce = require('typeforce')
2+
let types = require('./types')
3+
let vstruct = require('varstruct')
4+
5+
let MTPPREFIX = 0x83
6+
let MTPTIP = types.tip(MTPPREFIX)
7+
let MTP = {
8+
keyType: typeforce.compile({
9+
medianTime: typeforce.UInt32,
10+
height: typeforce.UInt32
11+
}),
12+
key: vstruct([
13+
['prefix', vstruct.Value(vstruct.UInt8, MTPPREFIX)],
14+
['medianTime', vstruct.UInt32BE], // big-endian for lexicographical sort
15+
['height', vstruct.UInt32LE]
16+
]),
17+
valueType: typeforce.Null
18+
}
19+
20+
function MtpIndex () {}
21+
22+
MtpIndex.prototype.tip = function (db, callback) {
23+
db.get(MTPTIP, {}, callback)
24+
}
25+
26+
MtpIndex.prototype.connect = function (atomic, block) {
27+
let { height, medianTime } = block
28+
29+
atomic.put(MTP, { medianTime, height })
30+
atomic.put(MTPTIP, {}, block)
31+
}
32+
33+
MtpIndex.prototype.disconnect = function (atomic, block) {
34+
let { height, medianTime } = block
35+
36+
atomic.del(MTP, { medianTime, height })
37+
atomic.put(MTPTIP, {}, { blockId: block.prevBlockId, height })
38+
}
39+
40+
module.exports = MtpIndex
41+
module.exports.TYPE = MTP

‎rpc.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function block (rpc, blockId, done) {
5151
delete block.nextblockhash
5252
block.prevBlockId = block.previousblockhash
5353
delete block.prevblockhash
54+
block.medianTime = block.mediantime
55+
delete block.mediantime
5456

5557
block.transactions = block.tx.map(t => augment(t))
5658
delete block.tx

0 commit comments

Comments
 (0)