cache last fee level
This commit is contained in:
parent
167f70b02c
commit
5f5aed6345
|
@ -1148,6 +1148,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
|
WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
@ -1169,13 +1170,15 @@ WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
|
||||||
|
|
||||||
if (failed.length) {
|
if (failed.length) {
|
||||||
var logger = network == 'livenet' ? log.warn : log.debug;
|
var logger = network == 'livenet' ? log.warn : log.debug;
|
||||||
logger('Could not compute fee estimation in ' + network + ': ' + failed.join(','));
|
logger('Could not compute fee estimation in ' + network + ': ' + failed.join(', ') + ' blocks.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return cb(null, levels);
|
return cb(null, levels);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
WalletService._feeLevelCache = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns fee levels for the current state of the network.
|
* Returns fee levels for the current state of the network.
|
||||||
* @param {Object} opts
|
* @param {Object} opts
|
||||||
|
@ -1191,6 +1194,8 @@ WalletService.prototype.getFeeLevels = function(opts, cb) {
|
||||||
if (network != 'livenet' && network != 'testnet')
|
if (network != 'livenet' && network != 'testnet')
|
||||||
return cb(new ClientError('Invalid network'));
|
return cb(new ClientError('Invalid network'));
|
||||||
|
|
||||||
|
var cache = WalletService._feeLevelCache[network] || {};
|
||||||
|
|
||||||
var levels = Defaults.FEE_LEVELS;
|
var levels = Defaults.FEE_LEVELS;
|
||||||
var samplePoints = _.uniq(_.pluck(levels, 'nbBlocks'));
|
var samplePoints = _.uniq(_.pluck(levels, 'nbBlocks'));
|
||||||
self._sampleFeeLevels(network, samplePoints, function(err, feeSamples) {
|
self._sampleFeeLevels(network, samplePoints, function(err, feeSamples) {
|
||||||
|
@ -1199,8 +1204,13 @@ WalletService.prototype.getFeeLevels = function(opts, cb) {
|
||||||
level: level.name,
|
level: level.name,
|
||||||
};
|
};
|
||||||
if (err || feeSamples[level.nbBlocks] < 0) {
|
if (err || feeSamples[level.nbBlocks] < 0) {
|
||||||
|
if (cache[level.nbBlocks] >= 0) {
|
||||||
|
result.feePerKb = cache[level.nbBlocks];
|
||||||
|
result.nbBlocks = level.nbBlocks;
|
||||||
|
} else {
|
||||||
result.feePerKb = level.defaultValue;
|
result.feePerKb = level.defaultValue;
|
||||||
result.nbBlocks = null;
|
result.nbBlocks = null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
result.feePerKb = feeSamples[level.nbBlocks];
|
result.feePerKb = feeSamples[level.nbBlocks];
|
||||||
result.nbBlocks = level.nbBlocks;
|
result.nbBlocks = level.nbBlocks;
|
||||||
|
@ -1208,6 +1218,14 @@ WalletService.prototype.getFeeLevels = function(opts, cb) {
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var obtainedValues = _.zipObject(_.map(_.reject(values, {
|
||||||
|
nbBlocks: null
|
||||||
|
}), function(v) {
|
||||||
|
return [v.nbBlocks, v.feePerKb];
|
||||||
|
}));
|
||||||
|
|
||||||
|
WalletService._feeLevelCache[network] = _.assign(cache, obtainedValues);
|
||||||
|
|
||||||
return cb(null, values);
|
return cb(null, values);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -1823,6 +1823,7 @@ describe('Wallet service', function() {
|
||||||
helpers.createAndJoinWallet(1, 1, function(s, w) {
|
helpers.createAndJoinWallet(1, 1, function(s, w) {
|
||||||
server = s;
|
server = s;
|
||||||
wallet = w;
|
wallet = w;
|
||||||
|
WalletService._feeLevelCache = {};
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1887,6 +1888,48 @@ describe('Wallet service', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('should get cached value if network cannot estimate but an estimation was retrieved previously', function(done) {
|
||||||
|
helpers.stubFeeLevels({
|
||||||
|
1: 40000,
|
||||||
|
2: 20000,
|
||||||
|
6: 18000,
|
||||||
|
});
|
||||||
|
server.getFeeLevels({}, function(err, fees) {
|
||||||
|
should.not.exist(err);
|
||||||
|
fees = _.zipObject(_.map(fees, function(item) {
|
||||||
|
return [item.level, item];
|
||||||
|
}));
|
||||||
|
fees.priority.feePerKb.should.equal(40000);
|
||||||
|
fees.priority.nbBlocks.should.equal(1);
|
||||||
|
|
||||||
|
fees.normal.feePerKb.should.equal(20000);
|
||||||
|
fees.normal.nbBlocks.should.equal(2);
|
||||||
|
|
||||||
|
fees.economy.feePerKb.should.equal(18000);
|
||||||
|
fees.economy.nbBlocks.should.equal(6);
|
||||||
|
|
||||||
|
helpers.stubFeeLevels({
|
||||||
|
1: -1,
|
||||||
|
2: 25000,
|
||||||
|
6: 10000,
|
||||||
|
});
|
||||||
|
server.getFeeLevels({}, function(err, fees) {
|
||||||
|
should.not.exist(err);
|
||||||
|
fees = _.zipObject(_.map(fees, function(item) {
|
||||||
|
return [item.level, item];
|
||||||
|
}));
|
||||||
|
fees.priority.feePerKb.should.equal(40000);
|
||||||
|
fees.priority.nbBlocks.should.equal(1);
|
||||||
|
|
||||||
|
fees.normal.feePerKb.should.equal(25000);
|
||||||
|
fees.normal.nbBlocks.should.equal(2);
|
||||||
|
|
||||||
|
fees.economy.feePerKb.should.equal(10000);
|
||||||
|
fees.economy.nbBlocks.should.equal(6);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Wallet not complete tests', function() {
|
describe('Wallet not complete tests', function() {
|
||||||
|
|
Loading…
Reference in New Issue