cache last fee level

This commit is contained in:
Ivan Socolsky 2016-03-04 17:02:05 -03:00
parent 167f70b02c
commit 5f5aed6345
2 changed files with 64 additions and 3 deletions

View File

@ -1148,6 +1148,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
});
};
WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
var self = this;
@ -1169,13 +1170,15 @@ WalletService.prototype._sampleFeeLevels = function(network, points, cb) {
if (failed.length) {
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);
});
};
WalletService._feeLevelCache = {};
/**
* Returns fee levels for the current state of the network.
* @param {Object} opts
@ -1191,6 +1194,8 @@ WalletService.prototype.getFeeLevels = function(opts, cb) {
if (network != 'livenet' && network != 'testnet')
return cb(new ClientError('Invalid network'));
var cache = WalletService._feeLevelCache[network] || {};
var levels = Defaults.FEE_LEVELS;
var samplePoints = _.uniq(_.pluck(levels, 'nbBlocks'));
self._sampleFeeLevels(network, samplePoints, function(err, feeSamples) {
@ -1199,8 +1204,13 @@ WalletService.prototype.getFeeLevels = function(opts, cb) {
level: level.name,
};
if (err || feeSamples[level.nbBlocks] < 0) {
result.feePerKb = level.defaultValue;
result.nbBlocks = null;
if (cache[level.nbBlocks] >= 0) {
result.feePerKb = cache[level.nbBlocks];
result.nbBlocks = level.nbBlocks;
} else {
result.feePerKb = level.defaultValue;
result.nbBlocks = null;
}
} else {
result.feePerKb = feeSamples[level.nbBlocks];
result.nbBlocks = level.nbBlocks;
@ -1208,6 +1218,14 @@ WalletService.prototype.getFeeLevels = function(opts, cb) {
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);
});
};

View File

@ -1823,6 +1823,7 @@ describe('Wallet service', function() {
helpers.createAndJoinWallet(1, 1, function(s, w) {
server = s;
wallet = w;
WalletService._feeLevelCache = {};
done();
});
});
@ -1887,6 +1888,48 @@ describe('Wallet service', function() {
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() {