|
| 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 |
0 commit comments