Merge pull request #505 from sublimator/reserve

[WIP] Fix reserve calculation
This commit is contained in:
Chris Clark
2015-08-17 10:01:10 -07:00
3 changed files with 18 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
'use strict';
const _ = require('lodash');
const assert = require('assert');
const util = require('util');
const url = require('url');
const HttpsProxyAgent = require('https-proxy-agent');
@@ -621,7 +623,7 @@ Server.prototype._handleLedgerClosed = function(message) {
Server.prototype._handleServerStatus = function(message) {
// This message is only received when online.
// As we are connected, it is the definitive final state.
const isOnline = ~Server.onlineStates.indexOf(message.server_status);
const isOnline = _.includes(Server.onlineStates, message.server_status);
this._setState(isOnline ? 'online' : 'offline');
@@ -732,7 +734,7 @@ Server.prototype._handleResponseSubscribe = function(message) {
this._ledgerRanges.parseAndAddRanges(message.validated_ledgers);
}
if (~Server.onlineStates.indexOf(message.server_status)) {
if (_.includes(Server.onlineStates, message.server_status)) {
this._setState('online');
}
};
@@ -905,6 +907,9 @@ Server.prototype._feeTxUnit = function() {
*/
Server.prototype._reserve = function(ownerCount) {
// We should be in a valid state before calling this method
assert(this._reserve_base && this._reserve_inc);
const reserve_base = Amount.from_json(String(this._reserve_base));
const reserve_inc = Amount.from_json(String(this._reserve_inc));
const owner_count = ownerCount || 0;
@@ -913,7 +918,7 @@ Server.prototype._reserve = function(ownerCount) {
throw new Error('Owner count must not be negative.');
}
return reserve_base.add(reserve_inc.product_human(owner_count));
return reserve_base.add(reserve_inc.multiply(owner_count));
};
/**

View File

@@ -935,11 +935,10 @@ describe('Remote', function() {
remote.feeTxUnit(10).to_json();
});
});
it('Get reserve', function() {
it('reserve() before reserve rate known', function() {
remote._connected = true;
remote._servers[0]._connected = true;
assert.strictEqual(remote.reserve(1).to_json(), 'NaN');
remote._servers = [];
// Throws because the server has not had reserve_inc, reserve_base set
assert.throws(function() {
remote.reserve(10).to_json();
});

View File

@@ -305,7 +305,7 @@ describe('Server', function() {
server.emit('message', {
type: 'serverStatus',
load_base: 256 * 1,
load_base: 256,
load_factor: 256 * 10,
server_status: 'full'
});
@@ -1063,6 +1063,13 @@ describe('Server', function() {
assert.strictEqual(server._reserve().to_json(), '20000000');
});
it('Compute reserve, positive OwnerCount', function() {
const server = new Server(new Remote(), 'ws://localhost:5748');
server._reserve_base = 20000000;
server._reserve_inc = 5000000;
assert.strictEqual(server._reserve(4).to_json(), '40000000');
});
it('Cache hostid', function(done) {
const wss = new ws.Server({
port: 5748