From fc637f50ed9a6d5b77cf0d7ff44ecf325ae6b3de Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 20 Apr 2013 15:28:48 -0700 Subject: [PATCH 1/4] JS: Fixes and improvements for Amount. - Add from_quality() - Add parse_quality() - Improve add. - Better peformance for copyTo(). - Fix parse_issuer(), set_issuer(), & set_currency() not to modify sub object. - Correctly set currency and issuer of XRP in parse_json(). - Make not_equals_why more explicit. --- src/js/amount.js | 58 +++++++++++++++++++++++++++++++-------------- test/amount-test.js | 2 +- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/js/amount.js b/src/js/amount.js index 6302c39b05..3a6b2797ec 100644 --- a/src/js/amount.js +++ b/src/js/amount.js @@ -69,6 +69,10 @@ Amount.from_json = function (j) { return (new Amount()).parse_json(j); }; +Amount.from_quality = function (q, c, i) { + return (new Amount()).parse_quality(q, c, i); +}; + Amount.from_human = function (j) { return (new Amount()).parse_human(j); }; @@ -103,6 +107,16 @@ Amount.prototype.add = function (v) { if (!this.is_comparable(v)) { result = Amount.NaN(); } + else if (v.is_zero()) { + result = this; + } + else if (this.is_zero()) { + result = v.clone(); + result._is_negative = false; + result._is_native = this._is_native; + result._currency = this._currency; + result._issuer = this._issuer; + } else if (this._is_native) { result = new Amount(); @@ -112,13 +126,6 @@ Amount.prototype.add = function (v) { result._is_negative = s.compareTo(BigInteger.ZERO) < 0; result._value = result._is_negative ? s.negate() : s; - } - else if (v.is_zero()) { - result = this; - } - else if (this.is_zero()) { - result = v.clone(); - // YYY Why are these cloned? We never modify them. result._currency = this._currency; result._issuer = this._issuer; } @@ -258,7 +265,8 @@ Amount.prototype.compareTo = function (v) { return result; }; -// Returns copy. +// Make d a copy of this. Returns d. +// Modification of objects internally refered to is not allowed. Amount.prototype.copyTo = function (d, negate) { if ('object' === typeof this._value) { @@ -275,8 +283,8 @@ Amount.prototype.copyTo = function (d, negate) { ? !this._is_negative // Negating. : this._is_negative; // Just copying. - this._currency.copyTo(d._currency); - this._issuer.copyTo(d._issuer); + d._currency = this._currency; + d._issuer = this._issuer; // Prevent negative zero if (d.is_zero()) d._is_negative = false; @@ -606,11 +614,25 @@ Amount.prototype.parse_human = function (j) { }; Amount.prototype.parse_issuer = function (issuer) { - this._issuer.parse_json(issuer); + this._issuer = UInt160.from_json(issuer); return this; }; +// --> h: 8 hex bytes quality or 32 hex bytes directory index. +Amount.prototype.parse_quality = function (q, c, i) { + this._is_negative = false; + this._value = new BigInteger(q.substring(q.length-14), 16); + this._offset = parseInt(q.substring(q.length-16, q.length-14), 16)-100; + this._currency = Currency.from_json(c); + this._issuer = UInt160.from_json(i); + this._is_native = this._currency.is_native(); + + this.canonicalize(); + + return this; +} + // <-> j Amount.prototype.parse_json = function (j) { if ('string' === typeof j) { @@ -628,8 +650,8 @@ Amount.prototype.parse_json = function (j) { } else { this.parse_native(j); - this._currency = new Currency(); - this._issuer = new UInt160(); + this._currency = Currency.from_json("0"); + this._issuer = UInt160.from_json("0"); } } else if ('number' === typeof j) { @@ -757,11 +779,11 @@ Amount.prototype.parse_value = function (j) { Amount.prototype.set_currency = function (c) { if ('string' === typeof c) { - this._currency.parse_json(c); + this._currency = Currency.from_json(c); } else { - c.copyTo(this._currency); + this._currency = c; } this._is_native = this._currency.is_native(); @@ -770,9 +792,9 @@ Amount.prototype.set_currency = function (c) { Amount.prototype.set_issuer = function (issuer) { if (issuer instanceof UInt160) { - issuer.copyTo(this._issuer); + this._issuer = issuer; } else { - this._issuer.parse_json(issuer); + this._issuer = UInt160.from_json(issuer); } return this; @@ -975,7 +997,7 @@ Amount.prototype.not_equals_why = function (d, ignore_issuer) { if (!this._is_native) { if (!this._currency.equals(d._currency)) return "Non-XRP currency differs."; if (!ignore_issuer && !this._issuer.equals(d._issuer)) { - return "Non-XRP issuer differs."; + return "Non-XRP issuer differs: " + d._issuer.to_json() + "/" + this._issuer.to_json(); } } return false; diff --git a/test/amount-test.js b/test/amount-test.js index 6a62aff24f..fac357ad26 100644 --- a/test/amount-test.js +++ b/test/amount-test.js @@ -329,7 +329,7 @@ buster.testCase("Amount", { var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL"); var b = Amount.from_json("1/USD/rH5aWQJ4R7v4Mpyf4kDBUvDFT5cbpFq3XP"); buster.refute(a.equals(b)); - buster.assert.equals(a.not_equals_why(b), "Non-XRP issuer differs."); + buster.assert.equals(a.not_equals_why(b), "Non-XRP issuer differs: rH5aWQJ4R7v4Mpyf4kDBUvDFT5cbpFq3XP/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL"); }, "1 USD != 1 EUR" : function () { var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL"); From 8747b0a5d2aa003ac5e437f101476f3bf4cc0310 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 20 Apr 2013 15:43:27 -0700 Subject: [PATCH 2/4] JS: Fix request_ripple_balance() setting issuer. --- src/js/remote.js | 8 ++++---- test/send-test.js | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/js/remote.js b/src/js/remote.js index 14efb3d045..e57e2f54b1 100644 --- a/src/js/remote.js +++ b/src/js/remote.js @@ -1164,11 +1164,11 @@ Remote.prototype.request_ripple_balance = function (account, issuer, currency, c var accountHigh = UInt160.from_json(account).equals(highLimit.issuer()); request.emit('ripple_state', { - 'account_balance' : ( accountHigh ? balance.negate() : balance).parse_issuer(account), - 'peer_balance' : (!accountHigh ? balance.negate() : balance).parse_issuer(issuer), + 'account_balance' : ( accountHigh ? balance.negate() : balance.clone()).parse_issuer(account), + 'peer_balance' : (!accountHigh ? balance.negate() : balance.clone()).parse_issuer(issuer), - 'account_limit' : ( accountHigh ? highLimit : lowLimit).parse_issuer(issuer), - 'peer_limit' : (!accountHigh ? highLimit : lowLimit).parse_issuer(account), + 'account_limit' : ( accountHigh ? highLimit : lowLimit).clone().parse_issuer(issuer), + 'peer_limit' : (!accountHigh ? highLimit : lowLimit).clone().parse_issuer(account), 'account_quality_in' : ( accountHigh ? node.HighQualityIn : node.LowQualityIn), 'peer_quality_in' : (!accountHigh ? node.HighQualityIn : node.LowQualityIn), diff --git a/test/send-test.js b/test/send-test.js index 2c2c390a82..7e2c251d69 100644 --- a/test/send-test.js +++ b/test/send-test.js @@ -372,7 +372,7 @@ buster.testCase("Sending future", { .submit(); }, function (callback) { - self.what = "Verify balance from alice's pov."; + self.what = "Verify balance from alice's pov: 1"; self.remote.request_ripple_balance("alice", "bob", "USD", 'CURRENT') .once('ripple_state', function (m) { @@ -397,7 +397,7 @@ buster.testCase("Sending future", { .submit(); }, function (callback) { - self.what = "Verify balance from alice's pov."; + self.what = "Verify balance from alice's pov: 2"; self.remote.request_ripple_balance("alice", "bob", "USD", 'CURRENT') .once('ripple_state', function (m) { @@ -422,7 +422,7 @@ buster.testCase("Sending future", { .submit(); }, function (callback) { - self.what = "Verify balance from alice's pov."; + self.what = "Verify balance from alice's pov: 3"; self.remote.request_ripple_balance("alice", "bob", "USD", 'CURRENT') .once('ripple_state', function (m) { @@ -445,7 +445,7 @@ buster.testCase("Sending future", { .submit(); }, function (callback) { - self.what = "Verify balance from alice's pov."; + self.what = "Verify balance from alice's pov: 4"; self.remote.request_ripple_balance("alice", "bob", "USD", 'CURRENT') .once('ripple_state', function (m) { @@ -467,7 +467,7 @@ buster.testCase("Sending future", { // .ledger_accept(); // }, // function (callback) { -// self.what = "Verify balance from alice's pov."; +// self.what = "Verify balance from alice's pov: 5"; // // self.remote.request_ripple_balance("alice", "bob", "USD", 'CURRENT') // .once('ripple_state', function (m) { From 16a19ed5236453a6ebec0fe00cc3c9681e0ce46c Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sat, 20 Apr 2013 16:45:06 -0700 Subject: [PATCH 3/4] ripple-lib version 0.7.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eb6358bbb4..79962bca92 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ripple-lib", - "version": "0.7.5", + "version": "0.7.6", "description": "Ripple JavaScript client library", "files": [ From a0dc6184e308d3886c44bd65c9d95d5120a067b8 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Sun, 21 Apr 2013 11:06:57 -0700 Subject: [PATCH 4/4] Augment book_offers with quality. --- src/cpp/ripple/NetworkOPs.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cpp/ripple/NetworkOPs.cpp b/src/cpp/ripple/NetworkOPs.cpp index 600e39f6ba..1416fd3b19 100644 --- a/src/cpp/ripple/NetworkOPs.cpp +++ b/src/cpp/ripple/NetworkOPs.cpp @@ -1972,8 +1972,8 @@ void NetworkOPs::getBookPage(Ledger::pointer lpLedger, const uint160& uTakerPays // Only provide, if not fully funded. jvOffer["taker_gets_funded"] = saTakerGetsFunded.getJson(0); jvOffer["taker_pays_funded"] = saTakerPaysFunded.getJson(0); - } + STAmount saOwnerPays = QUALITY_ONE == uOfferRate ? saTakerGetsFunded : std::min(saOwnerFunds, STAmount::multiply(saTakerGetsFunded, STAmount(CURRENCY_ONE, ACCOUNT_ONE, uOfferRate, -9))); @@ -1985,6 +1985,8 @@ void NetworkOPs::getBookPage(Ledger::pointer lpLedger, const uint160& uTakerPays if (!saOwnerFunds.isZero() || uOfferOwnerID == uTakerID) { // Only provide funded offers and offers of the taker. + jvOffer["quality"] = saDirRate.getText(); + jvOffers.append(jvOffer); }