UT: Lots of improvements and fixes.

- Parse negative amounts.
- Make Amount.negate() return a new value.
- Add Amount.equals().
- Rename Remote.trace() to set_trace().
- Fix request_ripple_balnce.
- Add more tests to send-test.js.
- Add more tests to amount-test.js.
- Add helper functions create_accounts and credit_limits to
  testutils.js.
This commit is contained in:
Arthur Britto
2012-10-21 20:04:25 -07:00
committed by Stefan Thomas
parent 0beab03255
commit 1aac962116
4 changed files with 135 additions and 31 deletions

View File

@@ -38,6 +38,10 @@ UInt160.prototype.copyTo = function(d) {
return d;
};
UInt160.prototype.equals = function(d) {
return this.value === d.value;
};
// value = NaN on error.
UInt160.prototype.parse_json = function (j) {
// Canonicalize and validate
@@ -166,6 +170,12 @@ var Amount = function () {
this.issuer = new UInt160();
};
// Given "100/USD/mtgox" return the a string with mtgox remapped.
Amount.text_full_rewrite = function (j) {
return Amount.from_json(j).to_text_full();
}
// Given "100/USD/mtgox" return the json.
Amount.json_rewrite = function(j) {
return Amount.from_json(j).to_json();
};
@@ -174,15 +184,18 @@ Amount.from_json = function(j) {
return (new Amount()).parse_json(j);
};
Amount.prototype.clone = function() {
return this.copyTo(new Amount());
Amount.prototype.clone = function(negate) {
return this.copyTo(new Amount(), negate);
};
// Returns copy.
Amount.prototype.copyTo = function(d) {
Amount.prototype.copyTo = function(d, negate) {
if ('object' === typeof this.value)
{
this.value.copyTo(d.value);
if (this.is_native && negate)
this.value.negate.copyTo(d.value);
else
this.value.copyTo(d.value);
}
else
{
@@ -191,7 +204,7 @@ Amount.prototype.copyTo = function(d) {
d.offset = this.offset;
d.is_native = this.is_native;
d.is_negative = this.is_negative;
d.is_negative = this.is_negative ? undefined : !this.is_negative;
this.currency.copyTo(d.currency);
this.issuer.copyTo(d.issuer);
@@ -266,15 +279,13 @@ Amount.prototype.canonicalize = function() {
this.offset += 1;
}
}
return this;
};
// Return a new value.
Amount.prototype.negate = function () {
if (this.is_native) {
this.value.negate();
}
else {
this.is_negative = !this.is_negative;
}
return this.clone('NEGATE');
};
Amount.prototype.to_json = function() {
@@ -307,23 +318,26 @@ Amount.prototype.parse_native = function(j) {
var m;
if ('string' === typeof j)
m = j.match(/^(\d+)(\.\d{1,6})?$/);
m = j.match(/^(-?)(\d+)(\.\d{1,6})?$/);
if (m) {
if (undefined === m[2]) {
if (undefined === m[3]) {
// Integer notation
this.value = new BigInteger(m[1]);
this.value = new BigInteger(m[2]);
}
else {
// Decimal notation
var int_part = (new BigInteger(m[1])).multiply(exports.consts.bi_xns_unit);
var fraction_part = (new BigInteger(m[2])).multiply(new BigInteger(String(Math.pow(10, 1+exports.consts.xns_precision-m[2].length))));
var int_part = (new BigInteger(m[2])).multiply(exports.consts.bi_xns_unit);
var fraction_part = (new BigInteger(m[3])).multiply(new BigInteger(String(Math.pow(10, 1+exports.consts.xns_precision-m[3].length))));
this.value = int_part.add(fraction_part);
}
if (m[1])
this.value = this.value.negate();
this.is_native = true;
this.offset = undefined;
this.is_negative = undefined;
@@ -442,6 +456,20 @@ Amount.prototype.parse_issuer = function (issuer) {
return this;
};
// Check BigInteger NaN
Amount.prototype.equals = function (d) {
return 'string' === typeof (d)
? this.equals(Amount.from_json(d))
: this === d
|| (d.constructor === Amount
&& this.is_native === d.is_native
&& (this.is_native
? this.value.equals(d.value)
: this.is_negative === d.is_negative
? this.value.equals(d.value)
: this.value.equals(BigInteger.ZERO) && d.value.equals(BigInteger.ZERO)));
};
exports.Amount = Amount;
exports.Currency = Currency;
exports.UInt160 = UInt160;

View File

@@ -244,8 +244,8 @@ Remote.prototype._set_state = function (state) {
}
};
Remote.prototype.trace = function () {
this.trace = true;
Remote.prototype.set_trace = function (trace) {
this.trace = undefined === trace || trace;
return this;
};
@@ -731,6 +731,8 @@ Remote.prototype.dirty_account_root = function (account) {
// --> issuer: String
// --> currency: String
// --> current: bool : true = current ledger
//
// If does not exist: emit('error', 'error' : 'remoteError', 'remote' : { 'error' : 'entryNotFound' })
Remote.prototype.request_ripple_balance = function (account, issuer, currency, current) {
var request = this.request_ledger_entry('ripple_state'); // YYY Could be cached per ledger.
@@ -739,20 +741,25 @@ Remote.prototype.request_ripple_balance = function (account, issuer, currency, c
.ledger_choose(current)
.on('success', function (message) {
var node = message.node;
var lowLimit = Amount.from_json(node.LowLimit);
var highLimit = Amount.from_json(node.HighLimit);
// The amount account holds of issuer (after negation if needed).
var balance = Amount.from_json(node.Balance);
var flip = UInt160.from_json(account) == highLimit.issuer;
var issuerLimit = flip ? lowLimit : highLimit;
var accountLimit = flip ? highLimit : lowLimit;
var issuerBalance = (flip ? balance.negate() : balance).parse_issuer(issuer);
var accountBalance = issuerBalance.clone().parse_issuer(issuer);
// accountHigh implies: for account: balance is negated, highLimit is the limit set by account.
var accountHigh = UInt160.from_json(account).equals(highLimit.issuer);
// The limit set by issuer.
var issuerLimit = (accountHigh ? lowLimit : highLimit).parse_issuer(issuer);
// The limit set by account.
var accountLimit = (accountHigh ? highLimit : lowLimit).parse_issuer(account);
var issuerBalance = (accountHigh ? balance.negate() : balance).parse_issuer(issuer);
var accountBalance = issuerBalance.clone().negate().parse_issuer(account);
request.emit('ripple_state', {
'issuer_balance' : issuerBalance, // Balance with dst as issuer.
'account_balance' : accountBalance, // Balance with account as issuer.
'issuer_limit' : issuerLimit.clone().parse_issuer(account), // Limit set by issuer with src as issuer.
'account_limit' : accountLimit.clone().parse_issuer(issuer) // Limit set by account with dst as issuer.
'issuer_balance' : issuerBalance, // Balance with dst as issuer.
'account_balance' : accountBalance, // Balance with account as issuer.
'issuer_limit' : issuerLimit, // Limit set by issuer with src as issuer.
'account_limit' : accountLimit // Limit set by account with dst as issuer.
});
});
}
@@ -774,7 +781,7 @@ Remote.prototype.transaction = function () {
// Events:
// 'success' : Transaction submitted without error.
// 'error' : Error submitting transaction.
// 'proposed: Advisory proposed status transaction.
// 'proposed' : Advisory proposed status transaction.
// - A client should expect 0 to multiple results.
// - Might not get back. The remote might just forward the transaction.
// - A success could be reverted in final.
@@ -1115,7 +1122,7 @@ Transaction.prototype.ripple_line_set = function (src, limit, quality_in, qualit
// Allow limit of 0 through.
if (undefined !== limit)
this.transaction.LimitAmount = limit.to_json();
this.transaction.LimitAmount = Amount.json_rewrite(limit);
if (quality_in)
this.transaction.QualityIn = quality_in;