diff --git a/lib/mempool/fees.js b/lib/mempool/fees.js index 3208a9b8..98fd6a17 100644 --- a/lib/mempool/fees.js +++ b/lib/mempool/fees.js @@ -439,8 +439,6 @@ function PolicyEstimator(network, logger) { this.map = {}; this.mapSize = 0; this.bestHeight = 0; - - this.init(); } /** diff --git a/lib/mempool/layout-browser.js b/lib/mempool/layout-browser.js index 3ea916be..a260c448 100644 --- a/lib/mempool/layout-browser.js +++ b/lib/mempool/layout-browser.js @@ -10,6 +10,7 @@ var layout = { binary: false, R: 'R', V: 'V', + F: 'F', e: function e(hash) { return 'e' + hex(hash); }, diff --git a/lib/mempool/layout.js b/lib/mempool/layout.js index 558c06a2..70228297 100644 --- a/lib/mempool/layout.js +++ b/lib/mempool/layout.js @@ -17,6 +17,7 @@ var layout = { binary: true, R: new Buffer([0x52]), V: new Buffer([0x76]), + F: new Buffer([0x46]), e: function e(hash) { var key = new Buffer(33); key[0] = 0x65; diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 2a9eb491..c71dfac2 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -27,6 +27,7 @@ var Network = require('../protocol/network'); var encoding = require('../utils/encoding'); var layout = require('./layout'); var LDB = require('../db/ldb'); +var Fees = require('./fees'); var VerifyError = errors.VerifyError; var VerifyResult = errors.VerifyResult; @@ -106,7 +107,7 @@ util.inherits(Mempool, AsyncObject); Mempool.prototype._open = co(function* open() { var size = (this.options.maxSize / 1024).toFixed(2); - var i, entries, entry; + var i, entries, entry, fees; yield this.chain.open(); yield this.cache.open(); @@ -123,6 +124,17 @@ Mempool.prototype._open = co(function* open() { entry = entries[i]; this.updateAncestors(entry); } + + if (this.fees) { + fees = yield this.cache.getFees(); + + if (fees) { + this.fees.inject(fees); + this.logger.info( + 'Loaded mempool fee data (rate=%d).', + this.fees.estimateFee()); + } + } } this.logger.info('Mempool loaded (maxsize=%dkb).', size); @@ -189,8 +201,11 @@ Mempool.prototype._addBlock = co(function* addBlock(block, txs) { entries.push(entry); } - if (this.fees) + if (this.fees) { this.fees.processBlock(block.height, entries, this.chain.synced); + // Note: disable for now. + // this.cache.writeFees(this.fees); + } // We need to reset the rejects filter periodically. // There may be a locktime in a TX that is now valid. @@ -2299,6 +2314,7 @@ function MempoolCache(options) { this.logger = options.logger; this.chain = options.chain; + this.network = options.network; this.db = null; this.batch = null; @@ -2326,6 +2342,15 @@ MempoolCache.prototype.getTip = co(function* getTip() { return hash.toString('hex'); }); +MempoolCache.prototype.getFees = co(function* getFees() { + var data = yield this.db.get(layout.F); + + if (!data) + return; + + return Fees.fromRaw(data, this.network); +}); + MempoolCache.prototype.getEntries = function getEntries() { return this.db.values({ gte: layout.e(encoding.ZERO_HASH), @@ -2380,6 +2405,13 @@ MempoolCache.prototype.sync = function sync(hash) { this.batch.put(layout.R, new Buffer(hash, 'hex')); }; +MempoolCache.prototype.writeFees = function writeFees(fees) { + if (!this.db) + return; + + this.batch.put(layout.F, fees.toRaw()); +}; + MempoolCache.prototype.clear = function clear() { this.batch.clear(); this.batch = this.db.batch(); @@ -2459,6 +2491,7 @@ MempoolCache.prototype.wipe = co(function* wipe() { batch.put(layout.V, encoding.U32(MempoolCache.VERSION)); batch.put(layout.R, new Buffer(this.chain.tip.hash, 'hex')); + batch.del(layout.F); yield batch.write(); diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index bdf0692e..fcc6dc4d 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -65,6 +65,7 @@ function FullNode(options) { // Fee estimation. this.fees = new Fees(this.network, this.logger); + this.fees.init(); // Mempool needs access to the chain. this.mempool = new Mempool({