From 4ee0f394d151182c46f664527accc352c654839f Mon Sep 17 00:00:00 2001 From: Alex Cato Date: Sat, 11 Feb 2017 23:07:23 +0100 Subject: [PATCH 1/2] support.py: checkes if makers have overlapping offers and removes them from being considered for joins --- joinmarket/support.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/joinmarket/support.py b/joinmarket/support.py index 5c991ad7..c685c95e 100644 --- a/joinmarket/support.py +++ b/joinmarket/support.py @@ -249,6 +249,7 @@ def pick_order(orders, n): def choose_orders(db, cj_amount, n, chooseOrdersBy, ignored_makers=None): if ignored_makers is None: ignored_makers = [] + remove_invalid_makers_from_db(db) sqlorders = db.execute( 'SELECT * FROM orderbook WHERE minsize <= :cja AND :cja <= maxsize;', {'cja': cj_amount}).fetchall() @@ -381,6 +382,44 @@ def calc_zero_change_cj_amount(ordercombo): return result, cj_amount, total_fee +def remove_invalid_makers_from_db(db): + # checks if the combination of offers from a makers is valid + # note that individual offers have already been checked + # for validity before they were entered into the DB + crows = db.execute( + 'SELECT DISTINCT counterparty FROM orderbook WHERE oid > 1;').fetchall() + + for row in crows: + nick = row["counterparty"] + if maker_offers_are_invalid(db, nick): + db.execute('DELETE FROM orderbook WHERE counterparty=?;', (nick,)) + log.info('The following maker had malformed offers and was removed: ' + nick) + + +def maker_offers_are_invalid(db, makernick): + # checks for overlapping offer ranges + + this_maker_offers = db.execute( + 'SELECT minsize, maxsize FROM orderbook WHERE counterparty=?;', (makernick,)).fetchall() + + cur_lowest_min = this_maker_offers[0]['minsize'] + cur_highest_max = this_maker_offers[0]['maxsize'] + cur_combined_range = 0 + + for offer in this_maker_offers: + if offer['minsize'] < cur_lowest_min: + cur_lowest_min = offer['minsize'] + if offer['maxsize'] > cur_highest_max: + cur_highest_max = offer['maxsize'] + cur_combined_range += offer['maxsize'] - offer['minsize'] + if cur_combined_range > cur_highest_max - cur_lowest_min: + # there must be overlapping offer ranges + log.info('This maker has overlapping offer ranges: ' + makernick) + return True + + return False + + def debug_dump_object(obj, skip_fields=None): if skip_fields is None: skip_fields = [] From 5df803b1fe71603e6b1b08175bb873e97a3d6b24 Mon Sep 17 00:00:00 2001 From: Alex Cato Date: Sun, 12 Feb 2017 15:24:22 +0100 Subject: [PATCH 2/2] support.py overlapping offers fix1: also checked for sweep orders now --- joinmarket/support.py | 1 + 1 file changed, 1 insertion(+) diff --git a/joinmarket/support.py b/joinmarket/support.py index c685c95e..7159238c 100644 --- a/joinmarket/support.py +++ b/joinmarket/support.py @@ -319,6 +319,7 @@ def choose_sweep_orders(db, if ignored_makers is None: ignored_makers = [] + remove_invalid_makers_from_db(db) def calc_zero_change_cj_amount(ordercombo): sumabsfee = 0