mempool: preliminary work for persistent mempool.

This commit is contained in:
Christopher Jeffrey 2017-02-26 00:10:42 -08:00
parent 914b66b94f
commit 45952a4140
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 132 additions and 0 deletions

View File

@ -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;

40
lib/mempool/layout.js Normal file
View File

@ -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;

View File

@ -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
*/