fees: refactor estimator.
This commit is contained in:
parent
42a2e61d7f
commit
e7f850b4ba
@ -39,14 +39,13 @@ var MAX_PRIORITY = 1e16;
|
||||
var INF_PRIORITY = 1e9 * consensus.MAX_MONEY;
|
||||
var FEE_SPACING = 1.1;
|
||||
var PRI_SPACING = 2;
|
||||
var FREE_THRESHOLD = policy.FREE_THRESHOLD;
|
||||
|
||||
/**
|
||||
* Confirmation stats.
|
||||
* @alias module:mempool.ConfirmStats
|
||||
* @constructor
|
||||
* @param {String} type
|
||||
* @param {Logger} logger
|
||||
* @param {Logger?} logger
|
||||
*/
|
||||
|
||||
function ConfirmStats(type, logger) {
|
||||
@ -345,15 +344,15 @@ ConfirmStats.prototype.toRaw = function toRaw() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate confirm stats from serialized data.
|
||||
* Inject properties from serialized data.
|
||||
* @private
|
||||
* @param {Buffer} data
|
||||
* @param {String} type
|
||||
* @returns {ConfirmStats}
|
||||
*/
|
||||
|
||||
ConfirmStats.fromRaw = function fromRaw(data, type, logger) {
|
||||
ConfirmStats.prototype.fromRaw = function fromRaw(data) {
|
||||
var br = new BufferReader(data);
|
||||
var i, decay, buckets, avg, txAvg, maxConfirms, confAvg, stats;
|
||||
var i, decay, buckets, avg, txAvg, maxConfirms, confAvg;
|
||||
|
||||
decay = br.readDouble();
|
||||
buckets = readArray(br);
|
||||
@ -385,39 +384,49 @@ ConfirmStats.fromRaw = function fromRaw(data, type, logger) {
|
||||
throw new Error('Mismatch in fee/pri conf average bucket count.');
|
||||
}
|
||||
|
||||
stats = new ConfirmStats(type, logger);
|
||||
this.init(buckets, maxConfirms, decay);
|
||||
|
||||
stats.init(buckets, maxConfirms, decay);
|
||||
this.avg = avg;
|
||||
this.txAvg = txAvg;
|
||||
this.confAvg = confAvg;
|
||||
|
||||
stats.avg = avg;
|
||||
stats.txAvg = txAvg;
|
||||
stats.confAvg = confAvg;
|
||||
return this;
|
||||
};
|
||||
|
||||
return stats;
|
||||
/**
|
||||
* Instantiate confirm stats from serialized data.
|
||||
* @param {Buffer} data
|
||||
* @param {String} type
|
||||
* @param {Logger?} logger
|
||||
* @returns {ConfirmStats}
|
||||
*/
|
||||
|
||||
ConfirmStats.fromRaw = function fromRaw(data, type, logger) {
|
||||
return new ConfirmStats(type, logger).fromRaw(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Estimator for fees and priority.
|
||||
* @alias module:mempool.PolicyEstimator
|
||||
* @constructor
|
||||
* @param {Rate} minRelay
|
||||
* @param {Network|NetworkType} network
|
||||
* @param {Logger?} logger
|
||||
*/
|
||||
|
||||
function PolicyEstimator(minRelay, network, logger) {
|
||||
function PolicyEstimator(network, logger) {
|
||||
if (!(this instanceof PolicyEstimator))
|
||||
return new PolicyEstimator(minRelay, network, logger);
|
||||
return new PolicyEstimator(network, logger);
|
||||
|
||||
this.network = Network.get(network);
|
||||
this.logger = logger || Logger.global;
|
||||
|
||||
this.minTrackedFee = minRelay < MIN_FEERATE
|
||||
? MIN_FEERATE
|
||||
: minRelay;
|
||||
this.minTrackedFee = policy.MIN_RELAY >= MIN_FEERATE
|
||||
? policy.MIN_RELAY
|
||||
: MIN_FEERATE;
|
||||
|
||||
this.minTrackedPri = FREE_THRESHOLD < MIN_PRIORITY
|
||||
? MIN_PRIORITY
|
||||
: FREE_THRESHOLD;
|
||||
this.minTrackedPri = policy.FREE_THRESHOLD >= MIN_PRIORITY
|
||||
? policy.FREE_THRESHOLD
|
||||
: MIN_PRIORITY;
|
||||
|
||||
this.feeStats = new ConfirmStats('FeeRate', this.logger);
|
||||
this.priStats = new ConfirmStats('Priority', this.logger);
|
||||
@ -817,26 +826,54 @@ PolicyEstimator.prototype.toRaw = function toRaw() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate a policy estimator from serialized data.
|
||||
* Inject properties from serialized data.
|
||||
* @private
|
||||
* @param {Buffer} data
|
||||
* @param {Rate} minRelay
|
||||
* @param {Network|NetworkType} network
|
||||
* @returns {PolicyEstimator}
|
||||
*/
|
||||
|
||||
PolicyEstimator.fromRaw = function fromRaw(data, minRelay, logger) {
|
||||
PolicyEstimator.prototype.fromRaw = function fromRaw(data) {
|
||||
var br = new BufferReader(data);
|
||||
var network = Network.fromMagic(br.readU32());
|
||||
var bestHeight = br.readU32();
|
||||
var estimator = new PolicyEstimator(minRelay, network, logger);
|
||||
var feeStats = ConfirmStats.fromRaw(br.readVarBytes(), 'FeeRate', logger);
|
||||
var priStats = ConfirmStats.fromRaw(br.readVarBytes(), 'Priority', logger);
|
||||
|
||||
estimator.bestHeight = bestHeight;
|
||||
estimator.feeStats = feeStats;
|
||||
estimator.priStats = priStats;
|
||||
assert(this.network === network,
|
||||
'Network mistmatch for policy estimator.');
|
||||
|
||||
return estimator;
|
||||
this.bestHeight = bestHeight;
|
||||
this.feeStats.fromRaw(br.readVarBytes());
|
||||
this.priStats.fromRaw(br.readVarBytes());
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate a policy estimator from serialized data.
|
||||
* @param {Buffer} data
|
||||
* @param {Network?} network
|
||||
* @param {Logger?} logger
|
||||
* @returns {PolicyEstimator}
|
||||
*/
|
||||
|
||||
PolicyEstimator.fromRaw = function fromRaw(data, network, logger) {
|
||||
return new PolicyEstimator(network, logger).fromRaw(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Inject properties from estimator.
|
||||
* @param {PolicyEstimator} estimator
|
||||
* @returns {PolicyEstimator}
|
||||
*/
|
||||
|
||||
PolicyEstimator.prototype.inject = function inject(estimator) {
|
||||
assert(this.network === estimator.network,
|
||||
'Network mismatch for policy estimator.');
|
||||
|
||||
this.bestHeight = estimator.bestHeight;
|
||||
this.feeStats = estimator.feeStats;
|
||||
this.priStats = estimator.priStats;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -64,10 +64,7 @@ function FullNode(options) {
|
||||
});
|
||||
|
||||
// Fee estimation.
|
||||
this.fees = new Fees(
|
||||
policy.MIN_RELAY,
|
||||
this.network,
|
||||
this.logger);
|
||||
this.fees = new Fees(this.network, this.logger);
|
||||
|
||||
// Mempool needs access to the chain.
|
||||
this.mempool = new Mempool({
|
||||
|
||||
Loading…
Reference in New Issue
Block a user