From f184a71360a56d010eb63ffc406dd1f3501c8086 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Thu, 29 Jan 2015 11:12:58 -0800 Subject: [PATCH] [TEST] test and refactor orderbooks --- src/js/ripple/meta.js | 3 +- src/js/ripple/orderbook.js | 1124 +++++++++----------- test/fixtures/addresses.js | 5 + test/fixtures/orderbook.js | 788 ++++++++++++++ test/metadata-test.js | 3 +- test/orderbook-test.js | 2008 +++++++++++++++++++++--------------- 6 files changed, 2472 insertions(+), 1459 deletions(-) create mode 100644 test/fixtures/addresses.js create mode 100644 test/fixtures/orderbook.js diff --git a/src/js/ripple/meta.js b/src/js/ripple/meta.js index 5ad3bd5b..c4f9b781 100644 --- a/src/js/ripple/meta.js +++ b/src/js/ripple/meta.js @@ -131,7 +131,6 @@ Meta.prototype.getNodes = function(options) { } }; - Meta.prototype.getAffectedAccounts = function(from) { if (this._affectedAccounts) { return this._affectedAccounts; @@ -164,7 +163,7 @@ Meta.prototype.getAffectedAccounts = function(from) { this._affectedAccounts = utils.arrayUnique(accounts); - return this._affectedAccounts; + return this._affectedAccounts; }; Meta.prototype.getAffectedBooks = function() { diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index 593547d2..c04e753f 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -8,6 +8,7 @@ // - trade // - transaction +var _ = require('lodash'); var util = require('util'); var extend = require('extend'); var assert = require('assert'); @@ -43,18 +44,22 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) { this._shouldSubscribe = true; this._listeners = 0; this._offers = [ ]; - this._offerCounts = { }; - this._ownerFunds = { }; - this._ownerOffers = { }; + this._offerCounts = {}; + this._ownerFundsUnadjusted = {}; + this._ownerFunds = {}; + this._ownerOffersTotal = {}; // We consider ourselves synchronized if we have a current - // copy of the offers, we are online and subscribed to updates. + // copy of the offers, we are online and subscribed to updates this._synchronized = false; + // Transfer rate of the taker gets currency issuer + this._issuerTransferRate = null; + function listenersModified(action, event) { // Automatically subscribe and unsubscribe to orderbook // on the basis of existing event listeners - if (~OrderBook.EVENTS.indexOf(event)) { + if (_.contains(OrderBook.EVENTS, event)) { switch (action) { case 'add': if (++self._listeners === 1) { @@ -70,6 +75,10 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) { } }; + function updateFundedAmountsWrapper (transaction) { + self.updateFundedAmounts(transaction); + }; + this.on('newListener', function(event) { listenersModified('add', event); }); @@ -78,15 +87,12 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) { listenersModified('remove', event); }); - function updateFundedAmounts(transaction) { - self.updateFundedAmounts(transaction); - }; - - this._remote.on('transaction', updateFundedAmounts); + this._remote.on('transaction', updateFundedAmountsWrapper); this.on('unsubscribe', function() { self.resetCache(); - self._remote.removeListener('transaction', updateFundedAmounts); + + self._remote.removeListener('transaction', updateFundedAmountsWrapper); }); this._remote.once('prepare_subscribe', function() { @@ -119,6 +125,36 @@ OrderBook.DEFAULT_TRANSFER_RATE = 1000000000; OrderBook.IOU_SUFFIX = '/000/rrrrrrrrrrrrrrrrrrrrrhoLvTp'; +/** + * Normalize offers from book_offers and transaction stream + * + * @param {Object} offer + * @return {Object} normalized + */ + +OrderBook.offerRewrite = function(offer) { + var result = {}; + var keys = Object.keys(offer); + + for (var i=0, l=keys.length; i