From 96b46d2394b63bdd331e9909d9fc4292f1d0ce1c Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Tue, 24 Dec 2013 14:57:06 -0800 Subject: [PATCH 1/6] Added Request#addBook. --- src/js/ripple/request.js | 67 ++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/js/ripple/request.js b/src/js/ripple/request.js index fe2bd5eb..df7cc92d 100644 --- a/src/js/ripple/request.js +++ b/src/js/ripple/request.js @@ -275,42 +275,49 @@ Request.prototype.rtAccounts = function(accounts) { }; Request.prototype.books = function(books, snapshot) { - var processedBooks = [ ]; + // Reset list of books (this method overwrites the current list) + this.message.books = [ ]; for (var i = 0, l = books.length; i < l; i++) { var book = books[i]; - var json = { }; - - function processSide(side) { - if (!book[side]) { - throw new Error('Missing ' + side); - } - - var obj = json[side] = { - currency: Currency.json_rewrite(book[side].currency) - }; - if (obj.currency !== 'XRP') { - obj.issuer = UInt160.json_rewrite(book[side].issuer); - } - } - - processSide('taker_gets'); - processSide('taker_pays'); - - if (snapshot) { - json.snapshot = true; - } - - if (book.both) { - json.both = true; - } - - processedBooks.push(json); + this.addBook(book, snapshot); } - this.message.books = processedBooks; - return this; }; +Request.prototype.addBook = function (book, snapshot) { + if (!Array.isArray(this.message.books)) { + this.message.books = []; + } + + var json = { }; + + function processSide(side) { + if (!book[side]) { + throw new Error('Missing ' + side); + } + + var obj = json[side] = { + currency: Currency.json_rewrite(book[side].currency) + }; + if (obj.currency !== 'XRP') { + obj.issuer = UInt160.json_rewrite(book[side].issuer); + } + } + + processSide('taker_gets'); + processSide('taker_pays'); + + if (snapshot) { + json.snapshot = true; + } + + if (book.both) { + json.both = true; + } + + this.message.books.push(json); +}; + exports.Request = Request; From 38a6e8b0096aa2bc6faa909d879d2d1f85097bd6 Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Tue, 24 Dec 2013 14:57:39 -0800 Subject: [PATCH 2/6] Fixing OrderBook#_subscribe (WIP). --- src/js/ripple/orderbook.js | 51 +++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index fcd79634..be26f38d 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -37,9 +37,11 @@ function OrderBook(remote, currency_gets, issuer_gets, currency_pays, issuer_pay this.on('newListener', function (type, listener) { if (~OrderBook.subscribe_events.indexOf(type)) { if (!self._subs && self._remote._connected) { + self._subs += 1; self._subscribe(); + } else { + self._subs += 1; } - self._subs += 1; } }); @@ -55,11 +57,8 @@ function OrderBook(remote, currency_gets, issuer_gets, currency_pays, issuer_pay } }); - this._remote.on('connect', function () { - if (self._subs) { - self._subscribe(); - } - }); + // ST: This *should* call _prepareSubscribe. + this._remote.on('prepare_subscribe', this._subscribe.bind(this)); this._remote.on('disconnect', function () { self._sync = false; @@ -82,19 +81,43 @@ OrderBook.subscribe_events = ['transaction', 'model', 'trade']; */ OrderBook.prototype._subscribe = function () { var self = this; - var request = self._remote.request_subscribe(); - request.books([ self.to_json() ], true); - request.callback(function(err, res) { - if (err) { - // XXX What now? - } else { + if (self.is_valid() && self._subs) { + var request = this._remote.request_subscribe(); + request.addBook(self.to_json(), true); + request.once('success', function(res) { self._sync = true; self._offers = res.offers; self.emit('model', self._offers); - } - }); + }); + request.once('error', function(err) { + // XXX What now? + }); + request.request(); + } }; +/** + * Adds this orderbook to a subscription request. + +// ST: Currently this is not working because the server cannot give snapshots +// for more than one order book in the same subscribe message. + +OrderBook.prototype._prepareSubscribe = function (request) { + var self = this; + if (self.is_valid() && self._subs) { + request.addBook(self.to_json(), true); + request.once('success', function(res) { + self._sync = true; + self._offers = res.offers; + self.emit('model', self._offers); + }); + request.once('error', function(err) { + // XXX What now? + }); + } +}; + */ + OrderBook.prototype.to_json = function () { var json = { taker_gets: { From 5229a3d5078ba55b1a19ba06d3be703592cf5eeb Mon Sep 17 00:00:00 2001 From: Vahe Hovhannisyan Date: Fri, 27 Dec 2013 11:56:47 -0800 Subject: [PATCH 3/6] Add Account.prototype.line. Retrieve this account's single trust line. --- src/js/ripple/account.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/js/ripple/account.js b/src/js/ripple/account.js index b18e3209..b762462f 100644 --- a/src/js/ripple/account.js +++ b/src/js/ripple/account.js @@ -202,6 +202,32 @@ Account.prototype.lines = function (callback) { return this; }; +/** + * Retrieve this account's single trust line. + * + * @param {string} currency Currency + * @param {string} address Ripple address + * @param {function (err, line)} callback Called with the result + * @returns {Account} + */ + +Account.prototype.line = function (currency,address,callback) { + var self = this; + var callback = typeof callback === 'function' ? callback : function(){}; + + self.lines(function(err,data){ + data.lines.forEach(function(line){ + if (address === line.account && currency === line.currency) { + callback(null,line); + } + }); + + callback(); + }); + + return this; +}; + /** * Notify object of a relevant transaction. * From f10bc6718cadba1c70d68da641f80ff798a87df2 Mon Sep 17 00:00:00 2001 From: Vahe Hovhannisyan Date: Fri, 27 Dec 2013 14:31:49 -0800 Subject: [PATCH 4/6] Account.prototype.line: add a check. --- src/js/ripple/account.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/js/ripple/account.js b/src/js/ripple/account.js index b762462f..0a8d6349 100644 --- a/src/js/ripple/account.js +++ b/src/js/ripple/account.js @@ -212,17 +212,20 @@ Account.prototype.lines = function (callback) { */ Account.prototype.line = function (currency,address,callback) { - var self = this; - var callback = typeof callback === 'function' ? callback : function(){}; + var self = this, + found, + callback = typeof callback === 'function' ? callback : function(){}; self.lines(function(err,data){ data.lines.forEach(function(line){ if (address === line.account && currency === line.currency) { callback(null,line); + found = true; } }); - callback(); + if (!found) + callback(); }); return this; From 65b035267c840d6e4a2cb9a0bf70cdf77cbf7425 Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Fri, 27 Dec 2013 16:54:53 -0800 Subject: [PATCH 5/6] Remove comment that doesn't make sense in the new context. --- src/js/sjcl-custom/sjcl-secp256k1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/sjcl-custom/sjcl-secp256k1.js b/src/js/sjcl-custom/sjcl-secp256k1.js index 4d34db7f..d87bcc6f 100755 --- a/src/js/sjcl-custom/sjcl-secp256k1.js +++ b/src/js/sjcl-custom/sjcl-secp256k1.js @@ -1,6 +1,6 @@ // ----- for secp256k1 ------ -// Overwrite NIST-P256 with secp256k1 so we're on even footing +// Overwrite NIST-P256 with secp256k1 sjcl.ecc.curves.c256 = new sjcl.ecc.curve( sjcl.bn.pseudoMersennePrime(256, [[0,-1],[4,-1],[6,-1],[7,-1],[8,-1],[9,-1],[32,-1]]), "0x14551231950b75fc4402da1722fc9baee", From 95c7acb2108e3ee461cf81a9f68242bb64a2cbcf Mon Sep 17 00:00:00 2001 From: Nicholas Dudfield Date: Sun, 29 Dec 2013 11:55:54 +0700 Subject: [PATCH 6/6] Fix bug in (redundant) check for transaction result immediately before resubmission --- src/js/ripple/transaction.js | 14 ++++++++++++++ src/js/ripple/transactionmanager.js | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/js/ripple/transaction.js b/src/js/ripple/transaction.js index b88f6436..42507f5e 100644 --- a/src/js/ripple/transaction.js +++ b/src/js/ripple/transaction.js @@ -222,6 +222,20 @@ Transaction.prototype.addSubmittedTxnID = function(hash) { } }; +Transaction.prototype.findResultInCache = function(cache) { + var cached; + + for (var i = this.submittedTxnIDs.length - 1; i >= 0; i--) { + var hash = this.submittedTxnIDs[i]; + cached = cache[hash]; + if (cached != null) { + break; + }; + }; + + return cached; +}; + Transaction.prototype.hash = function(prefix, as_uint256) { if (typeof prefix === 'string') { if (typeof hashprefixes[prefix] === 'undefined') { diff --git a/src/js/ripple/transactionmanager.js b/src/js/ripple/transactionmanager.js index 347993dc..b7ffcee1 100644 --- a/src/js/ripple/transactionmanager.js +++ b/src/js/ripple/transactionmanager.js @@ -215,7 +215,7 @@ TransactionManager.prototype._resubmit = function(ledgers, pending) { return; } - var hashCached = self._cache[pending._hash]; + var hashCached = pending.findResultInCache(self._cache); //[pending._hash]; self._remote._trace('transactionmanager: resubmit: %s', pending.tx_json); if (hashCached) {