mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-22 21:25:49 +00:00
UT: Rework server.js and clean up test output.
This commit is contained in:
committed by
Stefan Thomas
parent
d658f0b685
commit
a6a8b0b29a
14
js/amount.js
14
js/amount.js
@@ -4,14 +4,11 @@
|
|||||||
var utils = require('./utils.js');
|
var utils = require('./utils.js');
|
||||||
var jsbn = require('./jsbn.js');
|
var jsbn = require('./jsbn.js');
|
||||||
|
|
||||||
|
// Don't include in browser context.
|
||||||
|
var config = require('../test/config.js');
|
||||||
|
|
||||||
var BigInteger = jsbn.BigInteger;
|
var BigInteger = jsbn.BigInteger;
|
||||||
|
|
||||||
var accounts = {};
|
|
||||||
|
|
||||||
var setAccounts = function (accounts_new) {
|
|
||||||
accounts = accounts_new;
|
|
||||||
};
|
|
||||||
|
|
||||||
var UInt160 = function () {
|
var UInt160 = function () {
|
||||||
// Internal form:
|
// Internal form:
|
||||||
// 0, 1, 'iXXXXX', 20 byte string, or NaN.
|
// 0, 1, 'iXXXXX', 20 byte string, or NaN.
|
||||||
@@ -44,8 +41,8 @@ UInt160.prototype.copyTo = function(d) {
|
|||||||
// value = NaN on error.
|
// value = NaN on error.
|
||||||
UInt160.prototype.parse_json = function (j) {
|
UInt160.prototype.parse_json = function (j) {
|
||||||
// Canonicalize and validate
|
// Canonicalize and validate
|
||||||
if (j in accounts)
|
if (config.accounts && j in config.accounts)
|
||||||
j = accounts[j].account;
|
j = config.accounts[j].account;
|
||||||
|
|
||||||
switch (j) {
|
switch (j) {
|
||||||
case undefined:
|
case undefined:
|
||||||
@@ -445,7 +442,6 @@ Amount.prototype.parse_issuer = function (issuer) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setAccounts = setAccounts;
|
|
||||||
exports.Amount = Amount;
|
exports.Amount = Amount;
|
||||||
exports.Currency = Currency;
|
exports.Currency = Currency;
|
||||||
exports.UInt160 = UInt160;
|
exports.UInt160 = UInt160;
|
||||||
|
|||||||
44
js/remote.js
44
js/remote.js
@@ -16,6 +16,9 @@ var WebSocket = require('ws');
|
|||||||
var Amount = require('./amount.js').Amount;
|
var Amount = require('./amount.js').Amount;
|
||||||
var UInt160 = require('./amount.js').UInt160;
|
var UInt160 = require('./amount.js').UInt160;
|
||||||
|
|
||||||
|
// Don't include in browser context.
|
||||||
|
var config = require('../test/config.js');
|
||||||
|
|
||||||
// Request events emmitted:
|
// Request events emmitted:
|
||||||
// 'success' : Request successful.
|
// 'success' : Request successful.
|
||||||
// 'error' : Request failed.
|
// 'error' : Request failed.
|
||||||
@@ -140,12 +143,11 @@ Request.prototype.ripple_state = function (account, issuer, currency) {
|
|||||||
//
|
//
|
||||||
|
|
||||||
// --> trusted: truthy, if remote is trusted
|
// --> trusted: truthy, if remote is trusted
|
||||||
var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
|
var Remote = function (trusted, websocket_ip, websocket_port, trace) {
|
||||||
this.trusted = trusted;
|
this.trusted = trusted;
|
||||||
this.websocket_ip = websocket_ip;
|
this.websocket_ip = websocket_ip;
|
||||||
this.websocket_port = websocket_port;
|
this.websocket_port = websocket_port;
|
||||||
this.id = 0;
|
this.id = 0;
|
||||||
this.config = config;
|
|
||||||
this.trace = trace;
|
this.trace = trace;
|
||||||
this.ledger_closed = undefined;
|
this.ledger_closed = undefined;
|
||||||
this.ledger_current_index = undefined;
|
this.ledger_current_index = undefined;
|
||||||
@@ -155,6 +157,7 @@ var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
|
|||||||
this.state = 'offline'; // 'online', 'offline'
|
this.state = 'offline'; // 'online', 'offline'
|
||||||
this.retry_timer = undefined;
|
this.retry_timer = undefined;
|
||||||
this.retry = undefined;
|
this.retry = undefined;
|
||||||
|
this.config = config || { 'accounts' : {}};
|
||||||
|
|
||||||
// Cache information for accounts.
|
// Cache information for accounts.
|
||||||
this.accounts = {
|
this.accounts = {
|
||||||
@@ -176,10 +179,10 @@ var Remote = function (trusted, websocket_ip, websocket_port, config, trace) {
|
|||||||
|
|
||||||
Remote.prototype = new EventEmitter;
|
Remote.prototype = new EventEmitter;
|
||||||
|
|
||||||
var remoteConfig = function (config, server, trace) {
|
Remote.from_config = function (name, trace) {
|
||||||
var serverConfig = config.servers[server];
|
var serverConfig = config.servers[name];
|
||||||
|
|
||||||
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port, config, trace);
|
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port, trace);
|
||||||
};
|
};
|
||||||
|
|
||||||
var isTemMalformed = function (engine_result_code) {
|
var isTemMalformed = function (engine_result_code) {
|
||||||
@@ -190,7 +193,7 @@ var isTefFailure = function (engine_result_code) {
|
|||||||
return (engine_result_code >= -299 && engine_result_code < 199);
|
return (engine_result_code >= -299 && engine_result_code < 199);
|
||||||
};
|
};
|
||||||
|
|
||||||
var flags = {
|
Remote.flags = {
|
||||||
'OfferCreate' : {
|
'OfferCreate' : {
|
||||||
'Passive' : 0x00010000,
|
'Passive' : 0x00010000,
|
||||||
},
|
},
|
||||||
@@ -204,7 +207,7 @@ var flags = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// XXX This needs to be determined from the network.
|
// XXX This needs to be determined from the network.
|
||||||
var fees = {
|
Remote.fees = {
|
||||||
'default' : Amount.from_json("100"),
|
'default' : Amount.from_json("100"),
|
||||||
'account_create' : Amount.from_json("1000"),
|
'account_create' : Amount.from_json("1000"),
|
||||||
'nickname_create' : Amount.from_json("1000"),
|
'nickname_create' : Amount.from_json("1000"),
|
||||||
@@ -233,6 +236,12 @@ Remote.prototype._set_state = function (state) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Remote.prototype.trace = function () {
|
||||||
|
this.trace = true;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
// Set the target online state. Defaults to false.
|
// Set the target online state. Defaults to false.
|
||||||
Remote.prototype.connect = function (online) {
|
Remote.prototype.connect = function (online) {
|
||||||
var target = undefined === online || online;
|
var target = undefined === online || online;
|
||||||
@@ -751,7 +760,7 @@ Remote.prototype.transaction = function () {
|
|||||||
// Construction:
|
// Construction:
|
||||||
// remote.transaction() // Build a transaction object.
|
// remote.transaction() // Build a transaction object.
|
||||||
// .offer_create(...) // Set major parameters.
|
// .offer_create(...) // Set major parameters.
|
||||||
// .flags() // Set optional parameters.
|
// .set_flags() // Set optional parameters.
|
||||||
// .on() // Register for events.
|
// .on() // Register for events.
|
||||||
// .submit(); // Send to network.
|
// .submit(); // Send to network.
|
||||||
//
|
//
|
||||||
@@ -895,12 +904,12 @@ Transaction.prototype.submit = function () {
|
|||||||
|
|
||||||
if (undefined === transaction.Fee) {
|
if (undefined === transaction.Fee) {
|
||||||
if ('Payment' === transaction.TransactionType
|
if ('Payment' === transaction.TransactionType
|
||||||
&& transaction.Flags & exports.flags.Payment.CreateAccount) {
|
&& transaction.Flags & Remote.flags.Payment.CreateAccount) {
|
||||||
|
|
||||||
transaction.Fee = fees.account_create.to_json();
|
transaction.Fee = Remote.fees.account_create.to_json();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
transaction.Fee = fees['default'].to_json();
|
transaction.Fee = Remote.fees['default'].to_json();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -973,9 +982,9 @@ Transaction.prototype.send_max = function (send_max) {
|
|||||||
|
|
||||||
// Add flags to a transaction.
|
// Add flags to a transaction.
|
||||||
// --> flags: undefined, _flag_, or [ _flags_ ]
|
// --> flags: undefined, _flag_, or [ _flags_ ]
|
||||||
Transaction.prototype.flags = function (flags) {
|
Transaction.prototype.set_flags = function (flags) {
|
||||||
if (flags) {
|
if (flags) {
|
||||||
var transaction_flags = exports.flags[this.transaction.TransactionType];
|
var transaction_flags = Remote.flags[this.transaction.TransactionType];
|
||||||
|
|
||||||
if (undefined == this.transaction.Flags) // We plan to not define this field on new Transaction.
|
if (undefined == this.transaction.Flags) // We plan to not define this field on new Transaction.
|
||||||
this.transaction.Flags = 0;
|
this.transaction.Flags = 0;
|
||||||
@@ -994,8 +1003,8 @@ Transaction.prototype.flags = function (flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.transaction.Flags & exports.flags.Payment.CreateAccount)
|
if (this.transaction.Flags & Remote.flags.Payment.CreateAccount)
|
||||||
this.transaction.Fee = fees.account_create.to_json();
|
this.transaction.Fee = Remote.fees.account_create.to_json();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@@ -1029,7 +1038,7 @@ Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expi
|
|||||||
this.secret = this.account_secret(src);
|
this.secret = this.account_secret(src);
|
||||||
this.transaction.TransactionType = 'OfferCreate';
|
this.transaction.TransactionType = 'OfferCreate';
|
||||||
this.transaction.Account = UInt160.from_json(src).to_json();
|
this.transaction.Account = UInt160.from_json(src).to_json();
|
||||||
this.transaction.Fee = fees.offer.to_json();
|
this.transaction.Fee = Remote.fees.offer.to_json();
|
||||||
this.transaction.TakerPays = Amount.json_rewrite(taker_pays);
|
this.transaction.TakerPays = Amount.json_rewrite(taker_pays);
|
||||||
this.transaction.TakerGets = Amount.json_rewrite(taker_gets);
|
this.transaction.TakerGets = Amount.json_rewrite(taker_gets);
|
||||||
|
|
||||||
@@ -1077,8 +1086,5 @@ Transaction.prototype.ripple_line_set = function (src, limit, quality_in, qualit
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.Remote = Remote;
|
exports.Remote = Remote;
|
||||||
exports.remoteConfig = remoteConfig;
|
|
||||||
exports.fees = fees;
|
|
||||||
exports.flags = flags;
|
|
||||||
|
|
||||||
// vim:sw=2:sts=2:ts=8
|
// vim:sw=2:sts=2:ts=8
|
||||||
|
|||||||
Reference in New Issue
Block a user