[WIP] Fix reserve calculation

This commit is contained in:
Nicholas Dudfield
2015-08-15 18:11:43 +07:00
parent f077a563c4
commit 52879febb9
3 changed files with 15 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
'use strict'; 'use strict';
const _ = require('lodash'); const _ = require('lodash');
const assert = require('assert');
const util = require('util'); const util = require('util');
const url = require('url'); const url = require('url');
const HttpsProxyAgent = require('https-proxy-agent'); const HttpsProxyAgent = require('https-proxy-agent');
@@ -905,6 +907,9 @@ Server.prototype._feeTxUnit = function() {
*/ */
Server.prototype._reserve = function(ownerCount) { 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_base = Amount.from_json(String(this._reserve_base));
const reserve_inc = Amount.from_json(String(this._reserve_inc)); const reserve_inc = Amount.from_json(String(this._reserve_inc));
const owner_count = ownerCount || 0; const owner_count = ownerCount || 0;
@@ -913,7 +918,7 @@ Server.prototype._reserve = function(ownerCount) {
throw new Error('Owner count must not be negative.'); 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(); remote.feeTxUnit(10).to_json();
}); });
}); });
it('Get reserve', function() { it('reserve() before reserve rate known', function() {
remote._connected = true; remote._connected = true;
remote._servers[0]._connected = true; remote._servers[0]._connected = true;
assert.strictEqual(remote.reserve(1).to_json(), 'NaN'); // Throws because the server has not had reserve_inc, reserve_base set
remote._servers = [];
assert.throws(function() { assert.throws(function() {
remote.reserve(10).to_json(); remote.reserve(10).to_json();
}); });

View File

@@ -1063,6 +1063,13 @@ describe('Server', function() {
assert.strictEqual(server._reserve().to_json(), '20000000'); 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) { it('Cache hostid', function(done) {
const wss = new ws.Server({ const wss = new ws.Server({
port: 5748 port: 5748