Skip to content

Commit

Permalink
add MTP index, resolves #9
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Jan 30, 2018
1 parent ccf187f commit 830d723
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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')
Expand All @@ -20,11 +21,12 @@ function Indexd (db, 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(),
fee: new FeeIndex()
txo: new TxoIndex()
}
}

Expand Down
42 changes: 42 additions & 0 deletions indexes/mediantime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let typeforce = require('typeforce')
let types = require('./types')
let vstruct = require('varstruct')

let MTPPREFIX = 0x83
let MTPTIP = types.tip(MTPPREFIX)
let MTP = {
keyType: typeforce.compile({
medianTime: typeforce.UInt32,
height: typeforce.UInt32
}),
key: vstruct([
['prefix', vstruct.Value(vstruct.UInt8, MTPPREFIX)],
['medianTime', vstruct.UInt32BE], // big-endian for lexicographical sort
['height', vstruct.UInt32LE]
]),
valueType: typeforce.Null
}

function MtpIndex () {}

MtpIndex.prototype.tip = function (db, callback) {
db.get(MTPTIP, {}, callback)
}

MtpIndex.prototype.connect = function (db, atomic, block, callback) {
let { height, medianTime } = block

atomic.put(MTP, { medianTime, height })
atomic.put(MTPTIP, {}, block)
callback()
}

MtpIndex.prototype.disconnect = function (atomic, block) {
let { height, medianTime } = block

atomic.del(MTP, { medianTime, height })
atomic.put(MTPTIP, {}, { blockId: block.prevBlockId, height })
}

module.exports = MtpIndex
module.exports.TYPE = MTP
2 changes: 2 additions & 0 deletions rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function block (rpc, blockId, done) {
delete block.nextblockhash
block.prevBlockId = block.previousblockhash
delete block.prevblockhash
block.medianTime = block.mediantime
delete block.mediantime

block.transactions = block.tx.map(t => augment(t))
delete block.tx
Expand Down

0 comments on commit 830d723

Please sign in to comment.