Prechádzať zdrojové kódy

Assert the invariant

Eren Yilmaz 6 rokov pred
rodič
commit
d80ace7b2b
1 zmenil súbory, kde vykonal 11 pridanie a 0 odobranie
  1. 11 0
      trading_bot.py

+ 11 - 0
trading_bot.py

@@ -3,6 +3,7 @@ from datetime import timedelta, datetime
 from math import log2, ceil
 from math import log2, ceil
 
 
 import model
 import model
+from debug import debug
 
 
 order_expiry = 43200
 order_expiry = 43200
 
 
@@ -77,12 +78,22 @@ def notify_order_traded(ownable_id):
         return False
         return False
     ownership_id = model.get_ownership_id(ownable_id, model.bank_id())
     ownership_id = model.get_ownership_id(ownable_id, model.bank_id())
 
 
+    if debug:  # the bot should only have one order
+        model.cursor.execute('''
+        SELECT COUNT(*) <= 1
+        FROM orders
+        WHERE ownership_id = ? 
+        ''', (ownership_id,))
+        if not model.cursor.fetchone()[0]:
+            raise AssertionError('The bot should have at most one order.')
+
     model.cursor.execute('''
     model.cursor.execute('''
         SELECT rowid, amount, expiry
         SELECT rowid, amount, expiry
         FROM orders 
         FROM orders 
         WHERE ownership_id = ? 
         WHERE ownership_id = ? 
          -- no need for ORDER since the bot should have only one order
          -- no need for ORDER since the bot should have only one order
          -- ORDER BY rowid DESC -- equivalent to ordering by time created
          -- ORDER BY rowid DESC -- equivalent to ordering by time created
+         -- LIMIT might still improve performance
         LIMIT 1
         LIMIT 1
     ''', (ownership_id,))
     ''', (ownership_id,))
     data = model.cursor.fechtall()
     data = model.cursor.fechtall()