diff --git a/lib/mempool/layout-browser.js b/lib/mempool/layout-browser.js new file mode 100644 index 00000000..19f760fb --- /dev/null +++ b/lib/mempool/layout-browser.js @@ -0,0 +1,33 @@ +/*! + * layout-browser.js - mempooldb layout for browser. + * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + */ + +'use strict'; + +var util = require('../utils/util'); +var pad32 = util.pad32; + +var layout = { + R: 'R', + e: function e(id, hash) { + return 'e' + pad32(id) + hex(hash); + } +}; + +/* + * Helpers + */ + +function hex(hash) { + if (typeof hash !== 'string') + hash = hash.toString('hex'); + return hash; +} + +/* + * Expose + */ + +module.exports = layout; diff --git a/lib/mempool/layout.js b/lib/mempool/layout.js new file mode 100644 index 00000000..0d489a94 --- /dev/null +++ b/lib/mempool/layout.js @@ -0,0 +1,40 @@ +/*! + * layout.js - mempool data layout for bcoin + * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + */ + +'use strict'; + +/* + * Database Layout: + * R -> tip hash + * e[id][hash] -> entry + */ + +var layout = { + R: new Buffer([0x52]), + e: function e(id, hash) { + var key = new Buffer(37); + key[0] = 0x65; + key.writeUInt32BE(id, 1, true); + write(key, hash, 5); + return key; + } +}; + +/* + * Helpers + */ + +function write(data, str, off) { + if (Buffer.isBuffer(str)) + return str.copy(data, off); + data.write(str, off, 'hex'); +} + +/* + * Expose + */ + +module.exports = layout; diff --git a/lib/mempool/mempoolentry.js b/lib/mempool/mempoolentry.js index 98347c75..0d6df75b 100644 --- a/lib/mempool/mempoolentry.js +++ b/lib/mempool/mempoolentry.js @@ -186,6 +186,65 @@ MempoolEntry.prototype.isFree = function isFree(height) { return priority > policy.FREE_THRESHOLD; }; +/** + * Get entry serialization size. + * @returns {Number} + */ + +MempoolEntry.prototype.getSize = function getSize() { + return tx.getSize() + 37; +}; + +/** + * Serialize entry to a buffer. + * @returns {Buffer} + */ + +MempoolEntry.prototype.toRaw = function toRaw() { + var bw = new StaticWriter(this.getSize()); + bw.writeBytes(this.tx.toRaw()); + bw.writeU32(this.height); + bw.writeU32(this.size); + bw.writeU32(this.sigops); + bw.writeDouble(this.priority); + bw.writeU32(this.fee); + bw.writeU32(this.ts); + bw.write64(this.value); + bw.writeU8(this.dependencies ? 1 : 0); + return bw.render(); +}; + +/** + * Inject properties from serialized data. + * @private + * @param {Buffer} data + * @returns {MempoolEntry} + */ + +MempoolEntry.prototype.fromRaw = function fromRaw(data) { + var br = new BufferReader(data); + this.tx = TX.fromReader(br); + this.height = br.readU32(); + this.size = br.readU32(); + this.sigops = br.readU32(); + this.priority = br.readDouble(); + this.fee = br.readU32(); + this.ts = br.readU32(); + this.value = br.read64(); + this.dependencies = br.readU8() === 1; + return this; +}; + +/** + * Instantiate entry from serialized data. + * @param {Buffer} data + * @returns {MempoolEntry} + */ + +MempoolEntry.fromRaw = function fromRaw(data) { + return new MempoolEntry().fromRaw(data); +}; + /* * Expose */