JS: Make currency and issuer private in Amount.

This commit is contained in:
Arthur Britto
2012-11-09 17:33:37 -08:00
parent 9960bfa63f
commit 8d32892609
5 changed files with 48 additions and 42 deletions

View File

@@ -282,10 +282,10 @@ var Amount = function () {
this._value = new BigInteger(); // NaN for bad value. Always positive for non-XRP. this._value = new BigInteger(); // NaN for bad value. Always positive for non-XRP.
this._offset = undefined; // For non-XRP. this._offset = undefined; // For non-XRP.
this._is_native = true; // Default to XRP. Only valid if value is not NaN. this._is_native = true; // Default to XRP. Only valid if value is not NaN.
this._is_negative = undefined; // Undefined for XRP. this._is_negative = undefined; // For non-XRP. Undefined for XRP.
this.currency = new Currency(); this._currency = new Currency();
this.issuer = new UInt160(); this._issuer = new UInt160();
}; };
// Given "100/USD/mtgox" return the a string with mtgox remapped. // Given "100/USD/mtgox" return the a string with mtgox remapped.
@@ -328,17 +328,25 @@ Amount.prototype.copyTo = function(d, negate) {
? !this._is_negative // Negating. ? !this._is_negative // Negating.
: this._is_negative; // Just copying. : this._is_negative; // Just copying.
this.currency.copyTo(d.currency); this._currency.copyTo(d._currency);
this.issuer.copyTo(d.issuer); this._issuer.copyTo(d._issuer);
return d; return d;
}; };
Amount.prototype.currency = function() {
return this._currency;
};
// YYY Might also provide is_valid_json. // YYY Might also provide is_valid_json.
Amount.prototype.is_valid = function() { Amount.prototype.is_valid = function() {
return !isNaN(this._value); return !isNaN(this._value);
}; };
Amount.prototype.issuer = function() {
return this._issuer;
};
// Convert only value to JSON wire format. // Convert only value to JSON wire format.
Amount.prototype.to_text = function(allow_nan) { Amount.prototype.to_text = function(allow_nan) {
if (isNaN(this._value)) { if (isNaN(this._value)) {
@@ -382,7 +390,7 @@ Amount.prototype.to_text = function(allow_nan) {
}; };
Amount.prototype.canonicalize = function() { Amount.prototype.canonicalize = function() {
if (isNaN(this._value) || !this.currency) { if (isNaN(this._value) || !this._currency) {
// nothing // nothing
} }
else if (this._value.equals(BigInteger.ZERO)) { else if (this._value.equals(BigInteger.ZERO)) {
@@ -422,8 +430,8 @@ Amount.prototype.to_json = function() {
{ {
return { return {
'value' : this.to_text(), 'value' : this.to_text(),
'currency' : this.currency.to_json(), 'currency' : this._currency.to_json(),
'issuer' : this.issuer.to_json(), 'issuer' : this._issuer.to_json(),
}; };
} }
}; };
@@ -433,7 +441,7 @@ Amount.prototype.to_text_full = function() {
? NaN ? NaN
: this._is_native : this._is_native
? this.to_text() + "/XRP" ? this.to_text() + "/XRP"
: this.to_text() + "/" + this.currency.to_json() + "/" + this.issuer.to_json(); : this.to_text() + "/" + this._currency.to_json() + "/" + this._issuer.to_json();
}; };
// Parse a XRP value from untrusted input. // Parse a XRP value from untrusted input.
@@ -550,13 +558,13 @@ Amount.prototype.parse_json = function(j) {
if (m) { if (m) {
this.parse_value(m[1]); this.parse_value(m[1]);
this.currency = Currency.from_json(m[2]); this._currency = Currency.from_json(m[2]);
this.issuer = UInt160.from_json(m[3]); this._issuer = UInt160.from_json(m[3]);
} }
else { else {
this.parse_native(j); this.parse_native(j);
this.currency = new Currency(); this._currency = new Currency();
this.issuer = new UInt160(); this._issuer = new UInt160();
} }
} }
else if ('object' === typeof j && j.constructor == Amount) { else if ('object' === typeof j && j.constructor == Amount) {
@@ -566,8 +574,8 @@ Amount.prototype.parse_json = function(j) {
// Parse the passed value to sanitize and copy it. // Parse the passed value to sanitize and copy it.
this.parse_value(j.value); this.parse_value(j.value);
this.currency.parse_json(j.currency); // Never XRP. this._currency.parse_json(j.currency); // Never XRP.
this.issuer.parse_json(j.issuer); this._issuer.parse_json(j.issuer);
} }
else { else {
this._value = NaN; this._value = NaN;
@@ -577,7 +585,7 @@ Amount.prototype.parse_json = function(j) {
}; };
Amount.prototype.parse_issuer = function (issuer) { Amount.prototype.parse_issuer = function (issuer) {
this.issuer.parse_json(issuer); this._issuer.parse_json(issuer);
return this; return this;
}; };

View File

@@ -844,7 +844,7 @@ Remote.prototype.request_ripple_balance = function (account, issuer, currency, c
// The amount the low account holds of issuer. // The amount the low account holds of issuer.
var balance = Amount.from_json(node.Balance); var balance = Amount.from_json(node.Balance);
// accountHigh implies: for account: balance is negated, highLimit is the limit set by account. // accountHigh implies: for account: balance is negated, highLimit is the limit set by account.
var accountHigh = UInt160.from_json(account).equals(highLimit.issuer); var accountHigh = UInt160.from_json(account).equals(highLimit.issuer());
// The limit set by account. // The limit set by account.
var accountLimit = (accountHigh ? highLimit : lowLimit).parse_issuer(account); var accountLimit = (accountHigh ? highLimit : lowLimit).parse_issuer(account);
// The limit set by issuer. // The limit set by issuer.

View File

@@ -339,8 +339,6 @@ buster.testCase("Offer tests", {
var self = this; var self = this;
var seq; var seq;
self.remote.set_trace();
async.waterfall([ async.waterfall([
function (callback) { function (callback) {
self.what = "Create accounts."; self.what = "Create accounts.";
@@ -367,6 +365,8 @@ buster.testCase("Offer tests", {
callback); callback);
}, },
function (callback) { function (callback) {
self.what = "Create offer.";
self.remote.transaction() self.remote.transaction()
.offer_create("bob", "100/USD/mtgox", "500") .offer_create("bob", "100/USD/mtgox", "500")
.on('proposed', function (m) { .on('proposed', function (m) {
@@ -389,7 +389,7 @@ buster.testCase("Offer tests", {
.payment("alice", "alice", "500") .payment("alice", "alice", "500")
.send_max("100/USD/mtgox") .send_max("100/USD/mtgox")
.on('proposed', function (m) { .on('proposed', function (m) {
console.log("proposed: %s", JSON.stringify(m)); // console.log("proposed: %s", JSON.stringify(m));
callback(m.result !== 'tesSUCCESS'); callback(m.result !== 'tesSUCCESS');
}) })
@@ -421,8 +421,6 @@ buster.testCase("Offer tests", {
var self = this; var self = this;
var seq; var seq;
self.remote.set_trace();
async.waterfall([ async.waterfall([
function (callback) { function (callback) {
self.what = "Create accounts."; self.what = "Create accounts.";
@@ -449,6 +447,8 @@ buster.testCase("Offer tests", {
callback); callback);
}, },
function (callback) { function (callback) {
self.what = "Create offer.";
self.remote.transaction() self.remote.transaction()
.offer_create("bob", "100/USD/mtgox", "500") .offer_create("bob", "100/USD/mtgox", "500")
.on('proposed', function (m) { .on('proposed', function (m) {
@@ -466,7 +466,7 @@ buster.testCase("Offer tests", {
.payment("alice", "alice", "200") .payment("alice", "alice", "200")
.send_max("100/USD/mtgox") .send_max("100/USD/mtgox")
.on('proposed', function (m) { .on('proposed', function (m) {
console.log("proposed: %s", JSON.stringify(m)); // console.log("proposed: %s", JSON.stringify(m));
callback(m.result !== 'tesSUCCESS'); callback(m.result !== 'tesSUCCESS');
}) })
@@ -494,10 +494,8 @@ buster.testCase("Offer tests", {
.payment("alice", "alice", "600") .payment("alice", "alice", "600")
.send_max("100/USD/mtgox") .send_max("100/USD/mtgox")
.on('proposed', function (m) { .on('proposed', function (m) {
console.log("proposed: %s", JSON.stringify(m)); // console.log("proposed: %s", JSON.stringify(m));
console.log("callback: %d", m.result !== 'tepPATH_PARTIAL');
console.log("callback: %s", m.result);
callback(m.result !== 'tepPATH_PARTIAL'); callback(m.result !== 'tepPATH_PARTIAL');
}) })
.submit(); .submit();
@@ -510,7 +508,7 @@ buster.testCase("Offer tests", {
.send_max("100/USD/mtgox") .send_max("100/USD/mtgox")
.set_flags('PartialPayment') .set_flags('PartialPayment')
.on('proposed', function (m) { .on('proposed', function (m) {
console.log("proposed: %s", JSON.stringify(m)); // console.log("proposed: %s", JSON.stringify(m));
callback(m.result !== 'tesSUCCESS'); callback(m.result !== 'tesSUCCESS');
}) })

View File

@@ -13,7 +13,7 @@ require("../src/js/remote.js").config = require("./config.js");
// How long to wait for server to start. // How long to wait for server to start.
var serverDelay = 1500; var serverDelay = 1500;
buster.testRunner.timeout = 3000; buster.testRunner.timeout = 5000;
buster.testCase("Sending", { buster.testCase("Sending", {
'setUp' : testutils.build_setup(), 'setUp' : testutils.build_setup(),
@@ -451,7 +451,7 @@ buster.testCase("Sending future", {
}); });
buster.testCase("Indirect ripple", { buster.testCase("Indirect ripple", {
'setUp' : testutils.build_setup({ verbose: false, no_server: false }), 'setUp' : testutils.build_setup(),
'tearDown' : testutils.build_teardown(), 'tearDown' : testutils.build_teardown(),
"indirect ripple" : "indirect ripple" :

View File

@@ -187,7 +187,7 @@ var payments = function (remote, balances, callback) {
var amount_json = values[index]; var amount_json = values[index];
var amount = Amount.from_json(amount_json); var amount = Amount.from_json(amount_json);
sends.push( { "source" : src, "destination" : amount.issuer.to_json(), "amount" : amount_json } ); sends.push( { "source" : src, "destination" : amount.issuer().to_json(), "amount" : amount_json } );
} }
} }
@@ -229,7 +229,7 @@ var verify_balance = function (remote, src, amount_json, callback) {
callback(); callback();
} }
else { else {
remote.request_ripple_balance(src, amount.issuer.to_json(), amount.currency.to_json(), 'CURRENT') remote.request_ripple_balance(src, amount.issuer().to_json(), amount.currency().to_json(), 'CURRENT')
.once('ripple_state', function (m) { .once('ripple_state', function (m) {
// console.log("BALANCE: %s", JSON.stringify(m)); // console.log("BALANCE: %s", JSON.stringify(m));
// console.log("account_balance: %s", m.account_balance.to_text_full()); // console.log("account_balance: %s", m.account_balance.to_text_full());
@@ -303,7 +303,7 @@ var verify_offer_not_found = function (remote, owner, seq, callback) {
callback('entryFound'); callback('entryFound');
}) })
.on('error', function (m) { .on('error', function (m) {
console.log("verify_no_offer: success: %s", JSON.stringify(m)); // console.log("verify_no_offer: success: %s", JSON.stringify(m));
callback('remoteError' !== m.error callback('remoteError' !== m.error
|| 'entryNotFound' !== m.remote.error); || 'entryNotFound' !== m.remote.error);