Update sqlalchemy from v1.4 to 2.0.

This commit is contained in:
tecnovert
2024-10-11 21:34:45 +02:00
parent bdc173187d
commit 445aa116ad
5 changed files with 216 additions and 186 deletions

View File

@@ -25,6 +25,7 @@ import concurrent.futures
from typing import Optional from typing import Optional
from sqlalchemy.sql import text
from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.orm.session import close_all_sessions from sqlalchemy.orm.session import close_all_sessions
@@ -1113,7 +1114,7 @@ class BasicSwap(BaseApp):
coin_id = int(coin_type) coin_id = int(coin_type)
info_type = 1 # wallet info_type = 1 # wallet
query_str = f'DELETE FROM wallets WHERE coin_id = {coin_id} AND balance_type = {info_type}' query_str = f'DELETE FROM wallets WHERE coin_id = {coin_id} AND balance_type = {info_type}'
session.execute(query_str) session.execute(text(query_str))
finally: finally:
self.closeSession(session) self.closeSession(session)
@@ -1176,7 +1177,7 @@ class BasicSwap(BaseApp):
def clearStringKV(self, str_key: str, session=None) -> None: def clearStringKV(self, str_key: str, session=None) -> None:
try: try:
use_session = self.openSession(session) use_session = self.openSession(session)
use_session.execute('DELETE FROM kv_string WHERE key = :key', {'key': str_key}) use_session.execute(text('DELETE FROM kv_string WHERE key = :key'), {'key': str_key})
finally: finally:
if session is None: if session is None:
self.closeSession(use_session) self.closeSession(use_session)
@@ -1280,10 +1281,10 @@ class BasicSwap(BaseApp):
use_session = self.openSession(session) use_session = self.openSession(session)
# Remove any delayed events # Remove any delayed events
query: str = 'DELETE FROM actions WHERE linked_id = x\'{}\' '.format(bid.bid_id.hex())
if self.debug: if self.debug:
use_session.execute('UPDATE actions SET active_ind = 2 WHERE linked_id = x\'{}\' '.format(bid.bid_id.hex())) query = 'UPDATE actions SET active_ind = 2 WHERE linked_id = x\'{}\' '.format(bid.bid_id.hex())
else: use_session.execute(text(query))
use_session.execute('DELETE FROM actions WHERE linked_id = x\'{}\' '.format(bid.bid_id.hex()))
reverse_bid: bool = self.is_reverse_ads_bid(offer.coin_from) reverse_bid: bool = self.is_reverse_ads_bid(offer.coin_from)
# Unlock locked inputs (TODO) # Unlock locked inputs (TODO)
@@ -1407,7 +1408,7 @@ class BasicSwap(BaseApp):
event_data=bytes(json.dumps(event_data), 'UTF-8'), event_data=bytes(json.dumps(event_data), 'UTF-8'),
)) ))
use_session.execute(f'DELETE FROM notifications WHERE record_id NOT IN (SELECT record_id FROM notifications WHERE active_ind=1 ORDER BY created_at ASC LIMIT {self._keep_notifications})') use_session.execute(text(f'DELETE FROM notifications WHERE record_id NOT IN (SELECT record_id FROM notifications WHERE active_ind=1 ORDER BY created_at ASC LIMIT {self._keep_notifications})'))
if show_event: if show_event:
self._notifications_cache[now] = (event_type, event_data) self._notifications_cache[now] = (event_type, event_data)
@@ -1421,7 +1422,7 @@ class BasicSwap(BaseApp):
def buildNotificationsCache(self, session): def buildNotificationsCache(self, session):
self._notifications_cache.clear() self._notifications_cache.clear()
q = session.execute(f'SELECT created_at, event_type, event_data FROM notifications WHERE active_ind = 1 ORDER BY created_at ASC LIMIT {self._show_notifications}') q = session.execute(text(f'SELECT created_at, event_type, event_data FROM notifications WHERE active_ind = 1 ORDER BY created_at ASC LIMIT {self._show_notifications}'))
for entry in q: for entry in q:
self._notifications_cache[entry[0]] = (entry[1], json.loads(entry[2].decode('UTF-8'))) self._notifications_cache[entry[0]] = (entry[1], json.loads(entry[2].decode('UTF-8')))
@@ -1439,12 +1440,12 @@ class BasicSwap(BaseApp):
try: try:
now: int = self.getTime() now: int = self.getTime()
session = self.openSession() session = self.openSession()
q = session.execute('SELECT COUNT(*) FROM knownidentities WHERE address = :address', {'address': address}).first() q = session.execute(text('SELECT COUNT(*) FROM knownidentities WHERE address = :address'), {'address': address}).first()
if q[0] < 1: if q[0] < 1:
session.execute('INSERT INTO knownidentities (active_ind, address, created_at) VALUES (1, :address, :now)', {'address': address, 'now': now}) session.execute(text('INSERT INTO knownidentities (active_ind, address, created_at) VALUES (1, :address, :now)'), {'address': address, 'now': now})
if 'label' in data: if 'label' in data:
session.execute('UPDATE knownidentities SET label = :label WHERE address = :address', {'address': address, 'label': data['label']}) session.execute(text('UPDATE knownidentities SET label = :label WHERE address = :address'), {'address': address, 'label': data['label']})
if 'automation_override' in data: if 'automation_override' in data:
new_value: int = 0 new_value: int = 0
@@ -1465,7 +1466,7 @@ class BasicSwap(BaseApp):
else: else:
raise ValueError('Unknown automation_override type') raise ValueError('Unknown automation_override type')
session.execute('UPDATE knownidentities SET automation_override = :new_value WHERE address = :address', {'address': address, 'new_value': new_value}) session.execute(text('UPDATE knownidentities SET automation_override = :new_value WHERE address = :address'), {'address': address, 'new_value': new_value})
if 'visibility_override' in data: if 'visibility_override' in data:
new_value: int = 0 new_value: int = 0
@@ -1486,10 +1487,10 @@ class BasicSwap(BaseApp):
else: else:
raise ValueError('Unknown visibility_override type') raise ValueError('Unknown visibility_override type')
session.execute('UPDATE knownidentities SET visibility_override = :new_value WHERE address = :address', {'address': address, 'new_value': new_value}) session.execute(text('UPDATE knownidentities SET visibility_override = :new_value WHERE address = :address'), {'address': address, 'new_value': new_value})
if 'note' in data: if 'note' in data:
session.execute('UPDATE knownidentities SET note = :note WHERE address = :address', {'address': address, 'note': data['note']}) session.execute(text('UPDATE knownidentities SET note = :note WHERE address = :address'), {'address': address, 'note': data['note']})
finally: finally:
self.closeSession(session) self.closeSession(session)
@@ -1519,7 +1520,7 @@ class BasicSwap(BaseApp):
if offset is not None: if offset is not None:
query_str += f' OFFSET {offset}' query_str += f' OFFSET {offset}'
q = session.execute(query_str) q = session.execute(text(query_str))
rv = [] rv = []
for row in q: for row in q:
identity = { identity = {
@@ -1543,7 +1544,7 @@ class BasicSwap(BaseApp):
def vacuumDB(self): def vacuumDB(self):
try: try:
session = self.openSession() session = self.openSession()
return session.execute('VACUUM') return session.execute(text('VACUUM'))
finally: finally:
self.closeSession(session) self.closeSession(session)
@@ -2180,7 +2181,7 @@ class BasicSwap(BaseApp):
def getNewContractId(self, session): def getNewContractId(self, session):
self._contract_count += 1 self._contract_count += 1
session.execute('UPDATE kv_int SET value = :value WHERE KEY="contract_count"', {'value': self._contract_count}) session.execute(text('UPDATE kv_int SET value = :value WHERE KEY="contract_count"'), {'value': self._contract_count})
return self._contract_count return self._contract_count
def getProofOfFunds(self, coin_type, amount_for: int, extra_commit_bytes): def getProofOfFunds(self, coin_type, amount_for: int, extra_commit_bytes):
@@ -2281,7 +2282,7 @@ class BasicSwap(BaseApp):
self.logEvent(Concepts.BID, bid_id, event_type, event_msg, session) self.logEvent(Concepts.BID, bid_id, event_type, event_msg, session)
def countBidEvents(self, bid, event_type, session): def countBidEvents(self, bid, event_type, session):
q = session.execute('SELECT COUNT(*) FROM eventlog WHERE linked_type = {} AND linked_id = x\'{}\' AND event_type = {}'.format(int(Concepts.BID), bid.bid_id.hex(), int(event_type))).first() q = session.execute(text('SELECT COUNT(*) FROM eventlog WHERE linked_type = {} AND linked_id = x\'{}\' AND event_type = {}'.format(int(Concepts.BID), bid.bid_id.hex(), int(event_type)))).first()
return q[0] return q[0]
def getEvents(self, linked_type: int, linked_id: bytes): def getEvents(self, linked_type: int, linked_id: bytes):
@@ -2316,7 +2317,7 @@ class BasicSwap(BaseApp):
def getLinkedMessageId(self, linked_type: int, linked_id: int, msg_type: int, msg_sequence: int = 0, session=None) -> bytes: def getLinkedMessageId(self, linked_type: int, linked_id: int, msg_type: int, msg_sequence: int = 0, session=None) -> bytes:
try: try:
use_session = self.openSession(session) use_session = self.openSession(session)
q = use_session.execute('SELECT msg_id FROM message_links WHERE linked_type = :linked_type AND linked_id = :linked_id AND msg_type = :msg_type AND msg_sequence = :msg_sequence', q = use_session.execute(text('SELECT msg_id FROM message_links WHERE linked_type = :linked_type AND linked_id = :linked_id AND msg_type = :msg_type AND msg_sequence = :msg_sequence'),
{'linked_type': linked_type, 'linked_id': linked_id, 'msg_type': msg_type, 'msg_sequence': msg_sequence}).first() {'linked_type': linked_type, 'linked_id': linked_id, 'msg_type': msg_type, 'msg_sequence': msg_sequence}).first()
return q[0] return q[0]
finally: finally:
@@ -2326,7 +2327,7 @@ class BasicSwap(BaseApp):
def countMessageLinks(self, linked_type: int, linked_id: int, msg_type: int, msg_sequence: int = 0, session=None) -> int: def countMessageLinks(self, linked_type: int, linked_id: int, msg_type: int, msg_sequence: int = 0, session=None) -> int:
try: try:
use_session = self.openSession(session) use_session = self.openSession(session)
q = use_session.execute('SELECT COUNT(*) FROM message_links WHERE linked_type = :linked_type AND linked_id = :linked_id AND msg_type = :msg_type AND msg_sequence = :msg_sequence', q = use_session.execute(text('SELECT COUNT(*) FROM message_links WHERE linked_type = :linked_type AND linked_id = :linked_id AND msg_type = :msg_type AND msg_sequence = :msg_sequence'),
{'linked_type': linked_type, 'linked_id': linked_id, 'msg_type': msg_type, 'msg_sequence': msg_sequence}).first() {'linked_type': linked_type, 'linked_id': linked_id, 'msg_type': msg_type, 'msg_sequence': msg_sequence}).first()
return q[0] return q[0]
finally: finally:
@@ -2587,14 +2588,14 @@ class BasicSwap(BaseApp):
def list_bid_events(self, bid_id: bytes, session): def list_bid_events(self, bid_id: bytes, session):
query_str = 'SELECT created_at, event_type, event_msg FROM eventlog ' + \ query_str = 'SELECT created_at, event_type, event_msg FROM eventlog ' + \
'WHERE active_ind = 1 AND linked_type = {} AND linked_id = x\'{}\' '.format(Concepts.BID, bid_id.hex()) 'WHERE active_ind = 1 AND linked_type = {} AND linked_id = x\'{}\' '.format(Concepts.BID, bid_id.hex())
q = session.execute(query_str) q = session.execute(text(query_str))
events = [] events = []
for row in q: for row in q:
events.append({'at': row[0], 'desc': describeEventEntry(row[1], row[2])}) events.append({'at': row[0], 'desc': describeEventEntry(row[1], row[2])})
query_str = 'SELECT created_at, trigger_at FROM actions ' + \ query_str = 'SELECT created_at, trigger_at FROM actions ' + \
'WHERE active_ind = 1 AND linked_id = x\'{}\' '.format(bid_id.hex()) 'WHERE active_ind = 1 AND linked_id = x\'{}\' '.format(bid_id.hex())
q = session.execute(query_str) q = session.execute(text(query_str))
for row in q: for row in q:
events.append({'at': row[0], 'desc': 'Delaying until: {}'.format(format_timestamp(row[1], with_seconds=True))}) events.append({'at': row[0], 'desc': 'Delaying until: {}'.format(format_timestamp(row[1], with_seconds=True))})
@@ -4473,7 +4474,7 @@ class BasicSwap(BaseApp):
try: try:
use_session = self.openSession(session) use_session = self.openSession(session)
q = use_session.execute('SELECT COUNT(*) FROM checkedblocks WHERE block_hash = :block_hash', {'block_hash': previousblockhash}).first() q = use_session.execute(text('SELECT COUNT(*) FROM checkedblocks WHERE block_hash = :block_hash'), {'block_hash': previousblockhash}).first()
if q[0] > 0: if q[0] > 0:
return True return True
@@ -4495,7 +4496,7 @@ class BasicSwap(BaseApp):
query = '''INSERT INTO checkedblocks (created_at, coin_type, block_height, block_hash, block_time) query = '''INSERT INTO checkedblocks (created_at, coin_type, block_height, block_hash, block_time)
VALUES (:now, :coin_type, :block_height, :block_hash, :block_time)''' VALUES (:now, :coin_type, :block_height, :block_hash, :block_time)'''
use_session.execute(query, {'now': now, 'coin_type': int(ci.coin_type()), 'block_height': block_height, 'block_hash': bytes.fromhex(block['hash']), 'block_time': int(block['time'])}) use_session.execute(text(query), {'now': now, 'coin_type': int(ci.coin_type()), 'block_height': block_height, 'block_hash': bytes.fromhex(block['hash']), 'block_time': int(block['time'])})
finally: finally:
if session is None: if session is None:
@@ -4647,7 +4648,7 @@ class BasicSwap(BaseApp):
try: try:
query_str = 'SELECT bid_id FROM bids ' + \ query_str = 'SELECT bid_id FROM bids ' + \
'WHERE active_ind = 1 AND state = :accepted_state AND expire_at + :grace_period <= :now ' 'WHERE active_ind = 1 AND state = :accepted_state AND expire_at + :grace_period <= :now '
q = session.execute(query_str, {'accepted_state': int(BidStates.BID_ACCEPTED), 'now': now, 'grace_period': grace_period}) q = session.execute(text(query_str), {'accepted_state': int(BidStates.BID_ACCEPTED), 'now': now, 'grace_period': grace_period})
for row in q: for row in q:
bid_id = row[0] bid_id = row[0]
self.log.info('Timing out bid {}.'.format(bid_id.hex())) self.log.info('Timing out bid {}.'.format(bid_id.hex()))
@@ -4728,10 +4729,10 @@ class BasicSwap(BaseApp):
bid.setState(BidStates.BID_ERROR, err_msg) bid.setState(BidStates.BID_ERROR, err_msg)
self.saveBidInSession(bid_id, bid, session) self.saveBidInSession(bid_id, bid, session)
query: str = 'DELETE FROM actions WHERE trigger_at <= :now'
if self.debug: if self.debug:
session.execute('UPDATE actions SET active_ind = 2 WHERE trigger_at <= :now', {'now': now}) query = 'UPDATE actions SET active_ind = 2 WHERE trigger_at <= :now'
else: session.execute(text(query), {'now': now})
session.execute('DELETE FROM actions WHERE trigger_at <= :now', {'now': now})
except Exception as ex: except Exception as ex:
self.handleSessionErrors(ex, session, 'checkQueuedActions') self.handleSessionErrors(ex, session, 'checkQueuedActions')
@@ -4749,7 +4750,7 @@ class BasicSwap(BaseApp):
session = self.openSession() session = self.openSession()
q = session.query(Bid).filter(Bid.state == BidStates.BID_RECEIVING) q = session.query(Bid).filter(Bid.state == BidStates.BID_RECEIVING)
for bid in q: for bid in q:
q = session.execute('SELECT COUNT(*) FROM xmr_split_data WHERE bid_id = x\'{}\' AND msg_type = {}'.format(bid.bid_id.hex(), XmrSplitMsgTypes.BID)).first() q = session.execute(text('SELECT COUNT(*) FROM xmr_split_data WHERE bid_id = x\'{}\' AND msg_type = {}'.format(bid.bid_id.hex(), XmrSplitMsgTypes.BID))).first()
num_segments = q[0] num_segments = q[0]
if num_segments > 1: if num_segments > 1:
try: try:
@@ -4769,7 +4770,7 @@ class BasicSwap(BaseApp):
q = session.query(Bid).filter(Bid.state == BidStates.BID_RECEIVING_ACC) q = session.query(Bid).filter(Bid.state == BidStates.BID_RECEIVING_ACC)
for bid in q: for bid in q:
q = session.execute('SELECT COUNT(*) FROM xmr_split_data WHERE bid_id = x\'{}\' AND msg_type = {}'.format(bid.bid_id.hex(), XmrSplitMsgTypes.BID_ACCEPT)).first() q = session.execute(text('SELECT COUNT(*) FROM xmr_split_data WHERE bid_id = x\'{}\' AND msg_type = {}'.format(bid.bid_id.hex(), XmrSplitMsgTypes.BID_ACCEPT))).first()
num_segments = q[0] num_segments = q[0]
if num_segments > 1: if num_segments > 1:
try: try:
@@ -4860,7 +4861,7 @@ class BasicSwap(BaseApp):
if msg['to'] != self.network_addr: if msg['to'] != self.network_addr:
# Double check active_ind, shouldn't be possible to receive message if not active # Double check active_ind, shouldn't be possible to receive message if not active
query_str = 'SELECT COUNT(addr_id) FROM smsgaddresses WHERE addr = "{}" AND use_type = {} AND active_ind = 1'.format(msg['to'], AddressTypes.RECV_OFFER) query_str = 'SELECT COUNT(addr_id) FROM smsgaddresses WHERE addr = "{}" AND use_type = {} AND active_ind = 1'.format(msg['to'], AddressTypes.RECV_OFFER)
rv = session.execute(query_str).first() rv = session.execute(text(query_str)).first()
if rv[0] < 1: if rv[0] < 1:
raise ValueError('Offer received on incorrect address') raise ValueError('Offer received on incorrect address')
@@ -4962,7 +4963,7 @@ class BasicSwap(BaseApp):
def getCompletedAndActiveBidsValue(self, offer, session): def getCompletedAndActiveBidsValue(self, offer, session):
bids = [] bids = []
total_value = 0 total_value = 0
q = session.execute( q = session.execute(text(
'''SELECT bid_id, amount, state FROM bids '''SELECT bid_id, amount, state FROM bids
JOIN bidstates ON bidstates.state_id = bids.state AND (bidstates.state_id = {1} OR bidstates.in_progress > 0) JOIN bidstates ON bidstates.state_id = bids.state AND (bidstates.state_id = {1} OR bidstates.in_progress > 0)
WHERE bids.active_ind = 1 AND bids.offer_id = x\'{0}\' WHERE bids.active_ind = 1 AND bids.offer_id = x\'{0}\'
@@ -4970,7 +4971,7 @@ class BasicSwap(BaseApp):
SELECT bid_id, amount, state FROM bids SELECT bid_id, amount, state FROM bids
JOIN actions ON actions.linked_id = bids.bid_id AND actions.active_ind = 1 AND (actions.action_type = {2} OR actions.action_type = {3}) JOIN actions ON actions.linked_id = bids.bid_id AND actions.active_ind = 1 AND (actions.action_type = {2} OR actions.action_type = {3})
WHERE bids.active_ind = 1 AND bids.offer_id = x\'{0}\' WHERE bids.active_ind = 1 AND bids.offer_id = x\'{0}\'
'''.format(offer.offer_id.hex(), BidStates.SWAP_COMPLETED, ActionTypes.ACCEPT_XMR_BID, ActionTypes.ACCEPT_BID)) '''.format(offer.offer_id.hex(), BidStates.SWAP_COMPLETED, ActionTypes.ACCEPT_XMR_BID, ActionTypes.ACCEPT_BID)))
for row in q: for row in q:
bid_id, amount, state = row bid_id, amount, state = row
bids.append((bid_id, amount, state)) bids.append((bid_id, amount, state))
@@ -6199,7 +6200,7 @@ class BasicSwap(BaseApp):
if msg_data.msg_type == XmrSplitMsgTypes.BID or msg_data.msg_type == XmrSplitMsgTypes.BID_ACCEPT: if msg_data.msg_type == XmrSplitMsgTypes.BID or msg_data.msg_type == XmrSplitMsgTypes.BID_ACCEPT:
session = self.openSession() session = self.openSession()
try: try:
q = session.execute('SELECT COUNT(*) FROM xmr_split_data WHERE bid_id = x\'{}\' AND msg_type = {} AND msg_sequence = {}'.format(msg_data.msg_id.hex(), msg_data.msg_type, msg_data.sequence)).first() q = session.execute(text('SELECT COUNT(*) FROM xmr_split_data WHERE bid_id = x\'{}\' AND msg_type = {} AND msg_sequence = {}'.format(msg_data.msg_id.hex(), msg_data.msg_type, msg_data.sequence))).first()
num_exists = q[0] num_exists = q[0]
if num_exists > 0: if num_exists > 0:
self.log.warning('Ignoring duplicate xmr_split_data entry: ({}, {}, {})'.format(msg_data.msg_id.hex(), msg_data.msg_type, msg_data.sequence)) self.log.warning('Ignoring duplicate xmr_split_data entry: ({}, {}, {})'.format(msg_data.msg_id.hex(), msg_data.msg_type, msg_data.sequence))
@@ -6503,11 +6504,11 @@ class BasicSwap(BaseApp):
UNION ALL UNION ALL
SELECT 2, offer_id, expire_at FROM offers WHERE active_ind = 1 AND state IN (:offer_received, :offer_sent) AND expire_at <= :check_time SELECT 2, offer_id, expire_at FROM offers WHERE active_ind = 1 AND state IN (:offer_received, :offer_sent) AND expire_at <= :check_time
''' '''
q = session.execute(query, {'bid_received': int(BidStates.BID_RECEIVED), q = session.execute(text(query), {'bid_received': int(BidStates.BID_RECEIVED),
'offer_received': int(OfferStates.OFFER_RECEIVED), 'offer_received': int(OfferStates.OFFER_RECEIVED),
'bid_sent': int(BidStates.BID_SENT), 'bid_sent': int(BidStates.BID_SENT),
'offer_sent': int(OfferStates.OFFER_SENT), 'offer_sent': int(OfferStates.OFFER_SENT),
'check_time': now + self.check_expiring_bids_offers_seconds}) 'check_time': now + self.check_expiring_bids_offers_seconds})
for entry in q: for entry in q:
record_id = entry[1] record_id = entry[1]
expire_at = entry[2] expire_at = entry[2]
@@ -6523,7 +6524,7 @@ class BasicSwap(BaseApp):
offers_to_expire.add(record_id) offers_to_expire.add(record_id)
for bid_id in bids_to_expire: for bid_id in bids_to_expire:
query = 'SELECT expire_at, states FROM bids WHERE bid_id = :bid_id AND active_ind = 1 AND state IN (:bid_received, :bid_sent)' query = text('SELECT expire_at, states FROM bids WHERE bid_id = :bid_id AND active_ind = 1 AND state IN (:bid_received, :bid_sent)')
rows = session.execute(query, {'bid_id': bid_id, rows = session.execute(query, {'bid_id': bid_id,
'bid_received': int(BidStates.BID_RECEIVED), 'bid_received': int(BidStates.BID_RECEIVED),
'bid_sent': int(BidStates.BID_SENT)}).fetchall() 'bid_sent': int(BidStates.BID_SENT)}).fetchall()
@@ -6531,18 +6532,18 @@ class BasicSwap(BaseApp):
new_state: int = int(BidStates.BID_EXPIRED) new_state: int = int(BidStates.BID_EXPIRED)
states = (bytes() if rows[0][1] is None else rows[0][1]) + pack_state(new_state, now) states = (bytes() if rows[0][1] is None else rows[0][1]) + pack_state(new_state, now)
query = 'UPDATE bids SET state = :new_state, states = :states WHERE bid_id = :bid_id' query = 'UPDATE bids SET state = :new_state, states = :states WHERE bid_id = :bid_id'
session.execute(query, {'bid_id': bid_id, 'new_state': new_state, 'states': states}) session.execute(text(query), {'bid_id': bid_id, 'new_state': new_state, 'states': states})
bids_expired += 1 bids_expired += 1
for offer_id in offers_to_expire: for offer_id in offers_to_expire:
query = 'SELECT expire_at, states FROM offers WHERE offer_id = :offer_id AND active_ind = 1 AND state IN (:offer_received, :offer_sent)' query = 'SELECT expire_at, states FROM offers WHERE offer_id = :offer_id AND active_ind = 1 AND state IN (:offer_received, :offer_sent)'
rows = session.execute(query, {'offer_id': offer_id, rows = session.execute(text(query), {'offer_id': offer_id,
'offer_received': int(OfferStates.OFFER_RECEIVED), 'offer_received': int(OfferStates.OFFER_RECEIVED),
'offer_sent': int(OfferStates.OFFER_SENT)}).fetchall() 'offer_sent': int(OfferStates.OFFER_SENT)}).fetchall()
if len(rows) > 0: if len(rows) > 0:
new_state: int = int(OfferStates.OFFER_EXPIRED) new_state: int = int(OfferStates.OFFER_EXPIRED)
states = (bytes() if rows[0][1] is None else rows[0][1]) + pack_state(new_state, now) states = (bytes() if rows[0][1] is None else rows[0][1]) + pack_state(new_state, now)
query = 'UPDATE offers SET state = :new_state, states = :states WHERE offer_id = :offer_id' query = 'UPDATE offers SET state = :new_state, states = :states WHERE offer_id = :offer_id'
session.execute(query, {'offer_id': offer_id, 'new_state': new_state, 'states': states}) session.execute(text(query), {'offer_id': offer_id, 'new_state': new_state, 'states': states})
offers_expired += 1 offers_expired += 1
finally: finally:
self.closeSession(session) self.closeSession(session)
@@ -6940,32 +6941,35 @@ class BasicSwap(BaseApp):
num_watched_outputs += len(v['watched_outputs']) num_watched_outputs += len(v['watched_outputs'])
now: int = self.getTime() now: int = self.getTime()
q_str = '''SELECT q_bids_str = '''SELECT
COUNT(CASE WHEN b.was_sent THEN 1 ELSE NULL END) AS count_sent, COUNT(CASE WHEN b.was_sent THEN 1 ELSE NULL END) AS count_sent,
COUNT(CASE WHEN b.was_sent AND (s.in_progress OR (s.swap_ended = 0 AND b.expire_at > {} AND o.expire_at > {})) THEN 1 ELSE NULL END) AS count_sent_active, COUNT(CASE WHEN b.was_sent AND (s.in_progress OR (s.swap_ended = 0 AND b.expire_at > {} AND o.expire_at > {})) THEN 1 ELSE NULL END) AS count_sent_active,
COUNT(CASE WHEN b.was_received THEN 1 ELSE NULL END) AS count_received, COUNT(CASE WHEN b.was_received THEN 1 ELSE NULL END) AS count_received,
COUNT(CASE WHEN b.was_received AND b.state = {} AND b.expire_at > {} AND o.expire_at > {} THEN 1 ELSE NULL END) AS count_available, COUNT(CASE WHEN b.was_received AND b.state = {} AND b.expire_at > {} AND o.expire_at > {} THEN 1 ELSE NULL END) AS count_available,
COUNT(CASE WHEN b.was_received AND (s.in_progress OR (s.swap_ended = 0 AND b.expire_at > {} AND o.expire_at > {})) THEN 1 ELSE NULL END) AS count_recv_active COUNT(CASE WHEN b.was_received AND (s.in_progress OR (s.swap_ended = 0 AND b.expire_at > {} AND o.expire_at > {})) THEN 1 ELSE NULL END) AS count_recv_active
FROM bids b FROM bids b
JOIN offers o ON b.offer_id = o.offer_id JOIN offers o ON b.offer_id = o.offer_id
JOIN bidstates s ON b.state = s.state_id JOIN bidstates s ON b.state = s.state_id
WHERE b.active_ind = 1'''.format(now, now, BidStates.BID_RECEIVED, now, now, now, now) WHERE b.active_ind = 1'''.format(now, now, BidStates.BID_RECEIVED, now, now, now, now)
q = self.engine.execute(q_str).first()
bids_sent = q[0]
bids_sent_active = q[1]
bids_received = q[2]
bids_available = q[3]
bids_recv_active = q[4]
q_str = '''SELECT q_offers_str = '''SELECT
COUNT(CASE WHEN expire_at > {} THEN 1 ELSE NULL END) AS count_active, COUNT(CASE WHEN expire_at > {} THEN 1 ELSE NULL END) AS count_active,
COUNT(CASE WHEN was_sent THEN 1 ELSE NULL END) AS count_sent, COUNT(CASE WHEN was_sent THEN 1 ELSE NULL END) AS count_sent,
COUNT(CASE WHEN was_sent AND expire_at > {} THEN 1 ELSE NULL END) AS count_sent_active COUNT(CASE WHEN was_sent AND expire_at > {} THEN 1 ELSE NULL END) AS count_sent_active
FROM offers WHERE active_ind = 1'''.format(now, now) FROM offers WHERE active_ind = 1'''.format(now, now)
q = self.engine.execute(q_str).first()
num_offers = q[0] with self.engine.connect() as conn:
num_sent_offers = q[1] q = conn.execute(text(q_bids_str)).first()
num_sent_active_offers = q[2] bids_sent = q[0]
bids_sent_active = q[1]
bids_received = q[2]
bids_available = q[3]
bids_recv_active = q[4]
q = conn.execute(text(q_offers_str)).first()
num_offers = q[0]
num_sent_offers = q[1]
num_sent_active_offers = q[2]
rv = { rv = {
'network': self.chain, 'network': self.chain,
@@ -7050,7 +7054,7 @@ class BasicSwap(BaseApp):
now: int = self.getTime() now: int = self.getTime()
session.add(Wallets(coin_id=coin, balance_type=info_type, wallet_data=json.dumps(wi), created_at=now)) session.add(Wallets(coin_id=coin, balance_type=info_type, wallet_data=json.dumps(wi), created_at=now))
query_str = f'DELETE FROM wallets WHERE (coin_id = {coin_id} AND balance_type = {info_type}) AND record_id NOT IN (SELECT record_id FROM wallets WHERE coin_id = {coin_id} AND balance_type = {info_type} ORDER BY created_at DESC LIMIT 3 )' query_str = f'DELETE FROM wallets WHERE (coin_id = {coin_id} AND balance_type = {info_type}) AND record_id NOT IN (SELECT record_id FROM wallets WHERE coin_id = {coin_id} AND balance_type = {info_type} ORDER BY created_at DESC LIMIT 3 )'
session.execute(query_str) session.execute(text(query_str))
session.commit() session.commit()
except Exception as e: except Exception as e:
self.log.error(f'addWalletInfoRecord {e}') self.log.error(f'addWalletInfoRecord {e}')
@@ -7116,7 +7120,7 @@ class BasicSwap(BaseApp):
inner_str = f'SELECT coin_id, balance_type, MAX(created_at) as max_created_at FROM wallets {where_str} GROUP BY coin_id, balance_type' inner_str = f'SELECT coin_id, balance_type, MAX(created_at) as max_created_at FROM wallets {where_str} GROUP BY coin_id, balance_type'
query_str = 'SELECT a.coin_id, a.balance_type, wallet_data, created_at FROM wallets a, ({}) b WHERE a.coin_id = b.coin_id AND a.balance_type = b.balance_type AND a.created_at = b.max_created_at'.format(inner_str) query_str = 'SELECT a.coin_id, a.balance_type, wallet_data, created_at FROM wallets a, ({}) b WHERE a.coin_id = b.coin_id AND a.balance_type = b.balance_type AND a.created_at = b.max_created_at'.format(inner_str)
q = session.execute(query_str) q = session.execute(text(query_str))
for row in q: for row in q:
coin_id = row[0] coin_id = row[0]
@@ -7130,7 +7134,7 @@ class BasicSwap(BaseApp):
wallet_data['updating'] = self._updating_wallets_info.get(coin_id, False) wallet_data['updating'] = self._updating_wallets_info.get(coin_id, False)
# Ensure the latest addresses are displayed # Ensure the latest addresses are displayed
q = session.execute('SELECT key, value FROM kv_string WHERE key = "receive_addr_{0}" OR key = "stealth_addr_{0}"'.format(chainparams[coin_id]['name'])) q = session.execute(text('SELECT key, value FROM kv_string WHERE key = "receive_addr_{0}" OR key = "stealth_addr_{0}"'.format(chainparams[coin_id]['name'])))
for row in q: for row in q:
if row[0].startswith('stealth'): if row[0].startswith('stealth'):
@@ -7166,9 +7170,9 @@ class BasicSwap(BaseApp):
session = self.openSession() session = self.openSession()
try: try:
if offer_id: if offer_id:
q = session.execute('SELECT COUNT(*) FROM bids WHERE state >= {} AND offer_id = x\'{}\''.format(BidStates.BID_ACCEPTED, offer_id.hex())).first() q = session.execute(text('SELECT COUNT(*) FROM bids WHERE state >= {} AND offer_id = x\'{}\''.format(BidStates.BID_ACCEPTED, offer_id.hex()))).first()
else: else:
q = session.execute('SELECT COUNT(*) FROM bids WHERE state >= {}'.format(BidStates.BID_ACCEPTED)).first() q = session.execute(text('SELECT COUNT(*) FROM bids WHERE state >= {}'.format(BidStates.BID_ACCEPTED))).first()
return q[0] return q[0]
finally: finally:
self.closeSession(session, commit=False) self.closeSession(session, commit=False)
@@ -7296,7 +7300,7 @@ class BasicSwap(BaseApp):
if offset is not None: if offset is not None:
query_str += f' OFFSET {offset}' query_str += f' OFFSET {offset}'
q = session.execute(query_str, {'ads_swap': SwapTypes.XMR_SWAP, 'itx_type': TxTypes.ITX, 'ptx_type': TxTypes.PTX, 'al_type': TxTypes.XMR_SWAP_A_LOCK, 'bl_type': TxTypes.XMR_SWAP_B_LOCK}) q = session.execute(text(query_str), {'ads_swap': SwapTypes.XMR_SWAP, 'itx_type': TxTypes.ITX, 'ptx_type': TxTypes.PTX, 'al_type': TxTypes.XMR_SWAP_A_LOCK, 'bl_type': TxTypes.XMR_SWAP_B_LOCK})
for row in q: for row in q:
result = [x for x in row] result = [x for x in row]
coin_from = result[9] coin_from = result[9]
@@ -7387,7 +7391,7 @@ class BasicSwap(BaseApp):
try: try:
use_session = self.openSession(session) use_session = self.openSession(session)
rv = [] rv = []
q = use_session.execute(query_str, query_data) q = use_session.execute(text(query_str), query_data)
for row in q: for row in q:
rv.append({ rv.append({
'id': row[0], 'id': row[0],
@@ -7416,7 +7420,7 @@ class BasicSwap(BaseApp):
try: try:
session = self.openSession() session = self.openSession()
rv = [] rv = []
q = session.execute('SELECT sa.addr, ki.label FROM smsgaddresses AS sa LEFT JOIN knownidentities AS ki ON sa.addr = ki.address WHERE sa.use_type = {} AND sa.active_ind = 1 ORDER BY sa.addr_id DESC'.format(use_type)) q = session.execute(text('SELECT sa.addr, ki.label FROM smsgaddresses AS sa LEFT JOIN knownidentities AS ki ON sa.addr = ki.address WHERE sa.use_type = {} AND sa.active_ind = 1 ORDER BY sa.addr_id DESC'.format(use_type)))
for row in q: for row in q:
rv.append((row[0], row[1])) rv.append((row[0], row[1]))
return rv return rv
@@ -7446,7 +7450,7 @@ class BasicSwap(BaseApp):
if offset is not None: if offset is not None:
query_str += f' OFFSET {offset}' query_str += f' OFFSET {offset}'
q = session.execute(query_str) q = session.execute(text(query_str))
for row in q: for row in q:
rv.append(row) rv.append(row)
return rv return rv
@@ -7476,7 +7480,7 @@ class BasicSwap(BaseApp):
query_str = 'SELECT links.strategy_id, strats.label FROM automationlinks links' + \ query_str = 'SELECT links.strategy_id, strats.label FROM automationlinks links' + \
' LEFT JOIN automationstrategies strats ON strats.record_id = links.strategy_id' + \ ' LEFT JOIN automationstrategies strats ON strats.record_id = links.strategy_id' + \
' WHERE links.linked_type = {} AND links.linked_id = x\'{}\' AND links.active_ind = 1'.format(int(linked_type), linked_id.hex()) ' WHERE links.linked_type = {} AND links.linked_id = x\'{}\' AND links.active_ind = 1'.format(int(linked_type), linked_id.hex())
q = session.execute(query_str).first() q = session.execute(text(query_str)).first()
return q return q
finally: finally:
self.closeSession(session, commit=False) self.closeSession(session, commit=False)
@@ -7560,10 +7564,10 @@ class BasicSwap(BaseApp):
query_str += ', note = :note' query_str += ', note = :note'
query_str += ' WHERE addr = :addr' query_str += ' WHERE addr = :addr'
rv = use_session.execute(query_str, values) rv = use_session.execute(text(query_str), values)
if rv.rowcount < 1: if rv.rowcount < 1:
query_str: str = 'INSERT INTO smsgaddresses (addr, active_ind, use_type) VALUES (:addr, :active_ind, :use_type)' query_str: str = 'INSERT INTO smsgaddresses (addr, active_ind, use_type) VALUES (:addr, :active_ind, :use_type)'
use_session.execute(query_str, values) use_session.execute(text(query_str), values)
finally: finally:
if session is None: if session is None:
self.closeSession(use_session) self.closeSession(use_session)
@@ -7784,6 +7788,6 @@ class BasicSwap(BaseApp):
session = self.openSession() session = self.openSession()
key_str = 'saved_filters_' + prefix key_str = 'saved_filters_' + prefix
query_str = 'DELETE FROM kv_string WHERE key = :key_str' query_str = 'DELETE FROM kv_string WHERE key = :key_str'
session.execute(query_str, {'key_str': key_str}) session.execute(text(query_str), {'key_str': key_str})
finally: finally:
self.closeSession(session) self.closeSession(session)

View File

@@ -8,7 +8,7 @@ import time
import sqlalchemy as sa import sqlalchemy as sa
from enum import IntEnum, auto from enum import IntEnum, auto
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import declarative_base
CURRENT_DB_VERSION = 24 CURRENT_DB_VERSION = 24

View File

@@ -7,6 +7,7 @@
import json import json
import time import time
from sqlalchemy.sql import text
from sqlalchemy.orm import scoped_session from sqlalchemy.orm import scoped_session
from .db import ( from .db import (
BidState, BidState,
@@ -79,7 +80,7 @@ def upgradeDatabaseData(self, data_version):
in_error = isErrorBidState(state) in_error = isErrorBidState(state)
swap_failed = isFailingBidState(state) swap_failed = isFailingBidState(state)
swap_ended = isFinalBidState(state) swap_ended = isFinalBidState(state)
session.execute('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)}) session.execute(text('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)}))
if data_version > 0 and data_version < 4: if data_version > 0 and data_version < 4:
for state in (BidStates.BID_REQUEST_SENT, BidStates.BID_REQUEST_ACCEPTED): for state in (BidStates.BID_REQUEST_SENT, BidStates.BID_REQUEST_ACCEPTED):
session.add(BidState( session.add(BidState(
@@ -112,16 +113,16 @@ def upgradeDatabase(self, db_version):
current_version = db_version current_version = db_version
if current_version == 6: if current_version == 6:
session.execute('ALTER TABLE bids ADD COLUMN security_token BLOB') session.execute(text('ALTER TABLE bids ADD COLUMN security_token BLOB'))
session.execute('ALTER TABLE offers ADD COLUMN security_token BLOB') session.execute(text('ALTER TABLE offers ADD COLUMN security_token BLOB'))
db_version += 1 db_version += 1
elif current_version == 7: elif current_version == 7:
session.execute('ALTER TABLE transactions ADD COLUMN block_hash BLOB') session.execute(text('ALTER TABLE transactions ADD COLUMN block_hash BLOB'))
session.execute('ALTER TABLE transactions ADD COLUMN block_height INTEGER') session.execute(text('ALTER TABLE transactions ADD COLUMN block_height INTEGER'))
session.execute('ALTER TABLE transactions ADD COLUMN block_time INTEGER') session.execute(text('ALTER TABLE transactions ADD COLUMN block_time INTEGER'))
db_version += 1 db_version += 1
elif current_version == 8: elif current_version == 8:
session.execute(''' session.execute(text('''
CREATE TABLE wallets ( CREATE TABLE wallets (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
coin_id INTEGER, coin_id INTEGER,
@@ -129,30 +130,30 @@ def upgradeDatabase(self, db_version):
wallet_data VARCHAR, wallet_data VARCHAR,
balance_type INTEGER, balance_type INTEGER,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
db_version += 1 db_version += 1
elif current_version == 9: elif current_version == 9:
session.execute('ALTER TABLE wallets ADD COLUMN wallet_data VARCHAR') session.execute(text('ALTER TABLE wallets ADD COLUMN wallet_data VARCHAR'))
db_version += 1 db_version += 1
elif current_version == 10: elif current_version == 10:
session.execute('ALTER TABLE smsgaddresses ADD COLUMN active_ind INTEGER') session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN active_ind INTEGER'))
session.execute('ALTER TABLE smsgaddresses ADD COLUMN created_at INTEGER') session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN created_at INTEGER'))
session.execute('ALTER TABLE smsgaddresses ADD COLUMN note VARCHAR') session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN note VARCHAR'))
session.execute('ALTER TABLE smsgaddresses ADD COLUMN pubkey VARCHAR') session.execute(text('ALTER TABLE smsgaddresses ADD COLUMN pubkey VARCHAR'))
session.execute('UPDATE smsgaddresses SET active_ind = 1, created_at = 1') session.execute(text('UPDATE smsgaddresses SET active_ind = 1, created_at = 1'))
session.execute('ALTER TABLE offers ADD COLUMN addr_to VARCHAR') session.execute(text('ALTER TABLE offers ADD COLUMN addr_to VARCHAR'))
session.execute(f'UPDATE offers SET addr_to = "{self.network_addr}"') session.execute(text(f'UPDATE offers SET addr_to = "{self.network_addr}"'))
db_version += 1 db_version += 1
elif current_version == 11: elif current_version == 11:
session.execute('ALTER TABLE bids ADD COLUMN chain_a_height_start INTEGER') session.execute(text('ALTER TABLE bids ADD COLUMN chain_a_height_start INTEGER'))
session.execute('ALTER TABLE bids ADD COLUMN chain_b_height_start INTEGER') session.execute(text('ALTER TABLE bids ADD COLUMN chain_b_height_start INTEGER'))
session.execute('ALTER TABLE bids ADD COLUMN protocol_version INTEGER') session.execute(text('ALTER TABLE bids ADD COLUMN protocol_version INTEGER'))
session.execute('ALTER TABLE offers ADD COLUMN protocol_version INTEGER') session.execute(text('ALTER TABLE offers ADD COLUMN protocol_version INTEGER'))
session.execute('ALTER TABLE transactions ADD COLUMN tx_data BLOB') session.execute(text('ALTER TABLE transactions ADD COLUMN tx_data BLOB'))
db_version += 1 db_version += 1
elif current_version == 12: elif current_version == 12:
session.execute(''' session.execute(text('''
CREATE TABLE knownidentities ( CREATE TABLE knownidentities (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
address VARCHAR, address VARCHAR,
@@ -167,15 +168,15 @@ def upgradeDatabase(self, db_version):
note VARCHAR, note VARCHAR,
updated_at BIGINT, updated_at BIGINT,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute('ALTER TABLE bids ADD COLUMN reject_code INTEGER') session.execute(text('ALTER TABLE bids ADD COLUMN reject_code INTEGER'))
session.execute('ALTER TABLE bids ADD COLUMN rate INTEGER') session.execute(text('ALTER TABLE bids ADD COLUMN rate INTEGER'))
session.execute('ALTER TABLE offers ADD COLUMN amount_negotiable INTEGER') session.execute(text('ALTER TABLE offers ADD COLUMN amount_negotiable INTEGER'))
session.execute('ALTER TABLE offers ADD COLUMN rate_negotiable INTEGER') session.execute(text('ALTER TABLE offers ADD COLUMN rate_negotiable INTEGER'))
db_version += 1 db_version += 1
elif current_version == 13: elif current_version == 13:
db_version += 1 db_version += 1
session.execute(''' session.execute(text('''
CREATE TABLE automationstrategies ( CREATE TABLE automationstrategies (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
active_ind INTEGER, active_ind INTEGER,
@@ -187,9 +188,9 @@ def upgradeDatabase(self, db_version):
note VARCHAR, note VARCHAR,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute(''' session.execute(text('''
CREATE TABLE automationlinks ( CREATE TABLE automationlinks (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
active_ind INTEGER, active_ind INTEGER,
@@ -204,9 +205,9 @@ def upgradeDatabase(self, db_version):
note VARCHAR, note VARCHAR,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute(''' session.execute(text('''
CREATE TABLE history ( CREATE TABLE history (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
concept_type INTEGER, concept_type INTEGER,
@@ -215,9 +216,9 @@ def upgradeDatabase(self, db_version):
note VARCHAR, note VARCHAR,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute(''' session.execute(text('''
CREATE TABLE bidstates ( CREATE TABLE bidstates (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
active_ind INTEGER, active_ind INTEGER,
@@ -227,31 +228,31 @@ def upgradeDatabase(self, db_version):
note VARCHAR, note VARCHAR,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute('ALTER TABLE wallets ADD COLUMN active_ind INTEGER') session.execute(text('ALTER TABLE wallets ADD COLUMN active_ind INTEGER'))
session.execute('ALTER TABLE knownidentities ADD COLUMN active_ind INTEGER') session.execute(text('ALTER TABLE knownidentities ADD COLUMN active_ind INTEGER'))
session.execute('ALTER TABLE eventqueue RENAME TO actions') session.execute(text('ALTER TABLE eventqueue RENAME TO actions'))
session.execute('ALTER TABLE actions RENAME COLUMN event_id TO action_id') session.execute(text('ALTER TABLE actions RENAME COLUMN event_id TO action_id'))
session.execute('ALTER TABLE actions RENAME COLUMN event_type TO action_type') session.execute(text('ALTER TABLE actions RENAME COLUMN event_type TO action_type'))
session.execute('ALTER TABLE actions RENAME COLUMN event_data TO action_data') session.execute(text('ALTER TABLE actions RENAME COLUMN event_data TO action_data'))
elif current_version == 14: elif current_version == 14:
db_version += 1 db_version += 1
session.execute('ALTER TABLE xmr_swaps ADD COLUMN coin_a_lock_release_msg_id BLOB') session.execute(text('ALTER TABLE xmr_swaps ADD COLUMN coin_a_lock_release_msg_id BLOB'))
session.execute('ALTER TABLE xmr_swaps RENAME COLUMN coin_a_lock_refund_spend_tx_msg_id TO coin_a_lock_spend_tx_msg_id') session.execute(text('ALTER TABLE xmr_swaps RENAME COLUMN coin_a_lock_refund_spend_tx_msg_id TO coin_a_lock_spend_tx_msg_id'))
elif current_version == 15: elif current_version == 15:
db_version += 1 db_version += 1
session.execute(''' session.execute(text('''
CREATE TABLE notifications ( CREATE TABLE notifications (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
active_ind INTEGER, active_ind INTEGER,
event_type INTEGER, event_type INTEGER,
event_data BLOB, event_data BLOB,
created_at BIGINT, created_at BIGINT,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
elif current_version == 16: elif current_version == 16:
db_version += 1 db_version += 1
session.execute(''' session.execute(text('''
CREATE TABLE prefunded_transactions ( CREATE TABLE prefunded_transactions (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
active_ind INTEGER, active_ind INTEGER,
@@ -261,25 +262,25 @@ def upgradeDatabase(self, db_version):
tx_type INTEGER, tx_type INTEGER,
tx_data BLOB, tx_data BLOB,
used_by BLOB, used_by BLOB,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
elif current_version == 17: elif current_version == 17:
db_version += 1 db_version += 1
session.execute('ALTER TABLE knownidentities ADD COLUMN automation_override INTEGER') session.execute(text('ALTER TABLE knownidentities ADD COLUMN automation_override INTEGER'))
session.execute('ALTER TABLE knownidentities ADD COLUMN visibility_override INTEGER') session.execute(text('ALTER TABLE knownidentities ADD COLUMN visibility_override INTEGER'))
session.execute('ALTER TABLE knownidentities ADD COLUMN data BLOB') session.execute(text('ALTER TABLE knownidentities ADD COLUMN data BLOB'))
session.execute('UPDATE knownidentities SET active_ind = 1') session.execute(text('UPDATE knownidentities SET active_ind = 1'))
elif current_version == 18: elif current_version == 18:
db_version += 1 db_version += 1
session.execute('ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING') session.execute(text('ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING'))
session.execute('ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING') session.execute(text('ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING'))
elif current_version == 19: elif current_version == 19:
db_version += 1 db_version += 1
session.execute('ALTER TABLE bidstates ADD COLUMN in_error INTEGER') session.execute(text('ALTER TABLE bidstates ADD COLUMN in_error INTEGER'))
session.execute('ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER') session.execute(text('ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER'))
session.execute('ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER') session.execute(text('ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER'))
elif current_version == 20: elif current_version == 20:
db_version += 1 db_version += 1
session.execute(''' session.execute(text('''
CREATE TABLE message_links ( CREATE TABLE message_links (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
active_ind INTEGER, active_ind INTEGER,
@@ -291,18 +292,18 @@ def upgradeDatabase(self, db_version):
msg_type INTEGER, msg_type INTEGER,
msg_sequence INTEGER, msg_sequence INTEGER,
msg_id BLOB, msg_id BLOB,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute('ALTER TABLE offers ADD COLUMN bid_reversed INTEGER') session.execute(text('ALTER TABLE offers ADD COLUMN bid_reversed INTEGER'))
elif current_version == 21: elif current_version == 21:
db_version += 1 db_version += 1
session.execute('ALTER TABLE offers ADD COLUMN proof_utxos BLOB') session.execute(text('ALTER TABLE offers ADD COLUMN proof_utxos BLOB'))
session.execute('ALTER TABLE bids ADD COLUMN proof_utxos BLOB') session.execute(text('ALTER TABLE bids ADD COLUMN proof_utxos BLOB'))
elif current_version == 22: elif current_version == 22:
db_version += 1 db_version += 1
session.execute('ALTER TABLE offers ADD COLUMN amount_to INTEGER') session.execute(text('ALTER TABLE offers ADD COLUMN amount_to INTEGER'))
elif current_version == 23: elif current_version == 23:
db_version += 1 db_version += 1
session.execute(''' session.execute(text('''
CREATE TABLE checkedblocks ( CREATE TABLE checkedblocks (
record_id INTEGER NOT NULL, record_id INTEGER NOT NULL,
created_at BIGINT, created_at BIGINT,
@@ -310,8 +311,8 @@ def upgradeDatabase(self, db_version):
block_height INTEGER, block_height INTEGER,
block_hash BLOB, block_hash BLOB,
block_time INTEGER, block_time INTEGER,
PRIMARY KEY (record_id))''') PRIMARY KEY (record_id))'''))
session.execute('ALTER TABLE bids ADD COLUMN pkhash_buyer_to BLOB') session.execute(text('ALTER TABLE bids ADD COLUMN pkhash_buyer_to BLOB'))
if current_version != db_version: if current_version != db_version:
self.db_version = db_version self.db_version = db_version
self.setIntKV('db_version', db_version, session) self.setIntKV('db_version', db_version, session)

View File

@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2023 The BSX Developers # Copyright (c) 2023-2024 The BSX Developers
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
from sqlalchemy.sql import text
from .db import ( from .db import (
Concepts, Concepts,
) )
@@ -21,38 +22,38 @@ def remove_expired_data(self, time_offset: int = 0):
''' '''
num_offers = 0 num_offers = 0
num_bids = 0 num_bids = 0
offer_rows = session.execute(query_str, {'expired_at': now - time_offset}) offer_rows = session.execute(text(query_str), {'expired_at': now - time_offset})
for offer_row in offer_rows: for offer_row in offer_rows:
num_offers += 1 num_offers += 1
bid_rows = session.execute('SELECT bids.bid_id FROM bids WHERE bids.offer_id = :offer_id', {'offer_id': offer_row[0]}) bid_rows = session.execute(text('SELECT bids.bid_id FROM bids WHERE bids.offer_id = :offer_id'), {'offer_id': offer_row[0]})
for bid_row in bid_rows: for bid_row in bid_rows:
num_bids += 1 num_bids += 1
session.execute('DELETE FROM transactions WHERE transactions.bid_id = :bid_id', {'bid_id': bid_row[0]}) session.execute(text('DELETE FROM transactions WHERE transactions.bid_id = :bid_id'), {'bid_id': bid_row[0]})
session.execute('DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :bid_id', {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]}) session.execute(text('DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :bid_id'), {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]})
session.execute('DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :bid_id', {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]}) session.execute(text('DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :bid_id'), {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]})
session.execute('DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :bid_id', {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]}) session.execute(text('DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :bid_id'), {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]})
session.execute('DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :bid_id', {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]}) session.execute(text('DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :bid_id'), {'type_ind': int(Concepts.BID), 'bid_id': bid_row[0]})
session.execute('DELETE FROM xmr_swaps WHERE xmr_swaps.bid_id = :bid_id', {'bid_id': bid_row[0]}) session.execute(text('DELETE FROM xmr_swaps WHERE xmr_swaps.bid_id = :bid_id'), {'bid_id': bid_row[0]})
session.execute('DELETE FROM actions WHERE actions.linked_id = :bid_id', {'bid_id': bid_row[0]}) session.execute(text('DELETE FROM actions WHERE actions.linked_id = :bid_id'), {'bid_id': bid_row[0]})
session.execute('DELETE FROM addresspool WHERE addresspool.bid_id = :bid_id', {'bid_id': bid_row[0]}) session.execute(text('DELETE FROM addresspool WHERE addresspool.bid_id = :bid_id'), {'bid_id': bid_row[0]})
session.execute('DELETE FROM xmr_split_data WHERE xmr_split_data.bid_id = :bid_id', {'bid_id': bid_row[0]}) session.execute(text('DELETE FROM xmr_split_data WHERE xmr_split_data.bid_id = :bid_id'), {'bid_id': bid_row[0]})
session.execute('DELETE FROM bids WHERE bids.bid_id = :bid_id', {'bid_id': bid_row[0]}) session.execute(text('DELETE FROM bids WHERE bids.bid_id = :bid_id'), {'bid_id': bid_row[0]})
session.execute('DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :linked_id', {'type_ind': int(Concepts.BID), 'linked_id': bid_row[0]}) session.execute(text('DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :linked_id'), {'type_ind': int(Concepts.BID), 'linked_id': bid_row[0]})
session.execute('DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :offer_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) session.execute(text('DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :offer_id'), {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]})
session.execute('DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :offer_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) session.execute(text('DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :offer_id'), {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]})
session.execute('DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :offer_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) session.execute(text('DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :offer_id'), {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]})
session.execute('DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :offer_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) session.execute(text('DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :offer_id'), {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]})
session.execute('DELETE FROM xmr_offers WHERE xmr_offers.offer_id = :offer_id', {'offer_id': offer_row[0]}) session.execute(text('DELETE FROM xmr_offers WHERE xmr_offers.offer_id = :offer_id'), {'offer_id': offer_row[0]})
session.execute('DELETE FROM sentoffers WHERE sentoffers.offer_id = :offer_id', {'offer_id': offer_row[0]}) session.execute(text('DELETE FROM sentoffers WHERE sentoffers.offer_id = :offer_id'), {'offer_id': offer_row[0]})
session.execute('DELETE FROM actions WHERE actions.linked_id = :offer_id', {'offer_id': offer_row[0]}) session.execute(text('DELETE FROM actions WHERE actions.linked_id = :offer_id'), {'offer_id': offer_row[0]})
session.execute('DELETE FROM offers WHERE offers.offer_id = :offer_id', {'offer_id': offer_row[0]}) session.execute(text('DELETE FROM offers WHERE offers.offer_id = :offer_id'), {'offer_id': offer_row[0]})
session.execute('DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :offer_id', {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]}) session.execute(text('DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :offer_id'), {'type_ind': int(Concepts.OFFER), 'offer_id': offer_row[0]})
if num_offers > 0 or num_bids > 0: if num_offers > 0 or num_bids > 0:
self.log.info('Removed data for {} expired offer{} and {} bid{}.'.format(num_offers, 's' if num_offers != 1 else '', num_bids, 's' if num_bids != 1 else '')) self.log.info('Removed data for {} expired offer{} and {} bid{}.'.format(num_offers, 's' if num_offers != 1 else '', num_bids, 's' if num_bids != 1 else ''))
session.execute('DELETE FROM checkedblocks WHERE created_at <= :expired_at', {'expired_at': now - time_offset}) session.execute(text('DELETE FROM checkedblocks WHERE created_at <= :expired_at'), {'expired_at': now - time_offset})
finally: finally:
self.closeSession(session) self.closeSession(session)

View File

@@ -68,20 +68,41 @@ pyzmq==26.2.0 \
--hash=sha256:e6fa2e3e683f34aea77de8112f6483803c96a44fd726d7358b9888ae5bb394ec \ --hash=sha256:e6fa2e3e683f34aea77de8112f6483803c96a44fd726d7358b9888ae5bb394ec \
--hash=sha256:ea7f69de383cb47522c9c208aec6dd17697db7875a4674c4af3f8cfdac0bdeae \ --hash=sha256:ea7f69de383cb47522c9c208aec6dd17697db7875a4674c4af3f8cfdac0bdeae \
--hash=sha256:eac5174677da084abf378739dbf4ad245661635f1600edd1221f150b165343f4 --hash=sha256:eac5174677da084abf378739dbf4ad245661635f1600edd1221f150b165343f4
SQLAlchemy==1.4.39 \ SQLAlchemy==2.0.35 \
--hash=sha256:1745987ada1890b0e7978abdb22c133eca2e89ab98dc17939042240063e1ef21 \ --hash=sha256:016b2e665f778f13d3c438651dd4de244214b527a275e0acf1d44c05bc6026a9 \
--hash=sha256:26146c59576dfe9c546c9f45397a7c7c4a90c25679492ff610a7500afc7d03a6 \ --hash=sha256:042622a5306c23b972192283f4e22372da3b8ddf5f7aac1cc5d9c9b222ab3ff6 \
--hash=sha256:365b75938049ae31cf2176efd3d598213ddb9eb883fbc82086efa019a5f649df \ --hash=sha256:0f9f3f9a3763b9c4deb8c5d09c4cc52ffe49f9876af41cc1b2ad0138878453cf \
--hash=sha256:50e7569637e2e02253295527ff34666706dbb2bc5f6c61a5a7f44b9610c9bb09 \ --hash=sha256:1b56961e2d31389aaadf4906d453859f35302b4eb818d34a26fab72596076bb8 \
--hash=sha256:7f13644b15665f7322f9e0635129e0ef2098409484df67fcd225d954c5861559 \ --hash=sha256:22b83aed390e3099584b839b93f80a0f4a95ee7f48270c97c90acd40ee646f0b \
--hash=sha256:8194896038753b46b08a0b0ae89a5d80c897fb601dd51e243ed5720f1f155d27 \ --hash=sha256:25b0f63e7fcc2a6290cb5f7f5b4fc4047843504983a28856ce9b35d8f7de03cc \
--hash=sha256:8b773c9974c272aae0fa7e95b576d98d17ee65f69d8644f9b6ffc90ee96b4d19 \ --hash=sha256:2a275a806f73e849e1c309ac11108ea1a14cd7058577aba962cd7190e27c9e3c \
--hash=sha256:91d2b89bb0c302f89e753bea008936acfa4e18c156fb264fe41eb6bbb2bbcdeb \ --hash=sha256:2ab3f0336c0387662ce6221ad30ab3a5e6499aab01b9790879b6578fd9b8faa1 \
--hash=sha256:b0538b66f959771c56ff996d828081908a6a52a47c5548faed4a3d0a027a5368 \ --hash=sha256:4668bd8faf7e5b71c0319407b608f278f279668f358857dbfd10ef1954ac9f90 \
--hash=sha256:c6d00cb9da8d0cbfaba18cad046e94b06de6d4d0ffd9d4095a3ad1838af22528 \ --hash=sha256:4fdcd72a789c1c31ed242fd8c1bcd9ea186a98ee8e5408a50e610edfef980d71 \
--hash=sha256:d1f665e50592caf4cad3caed3ed86f93227bffe0680218ccbb293bd5a6734ca8 \ --hash=sha256:627dee0c280eea91aed87b20a1f849e9ae2fe719d52cbf847c0e0ea34464b3f7 \
--hash=sha256:e7a7667d928ba6ee361a3176e1bef6847c1062b37726b33505cc84136f657e0d \ --hash=sha256:67219632be22f14750f0d1c70e62f204ba69d28f62fd6432ba05ab295853de9b \
--hash=sha256:ede13a472caa85a13abe5095e71676af985d7690eaa8461aeac5c74f6600b6c0 --hash=sha256:6921ee01caf375363be5e9ae70d08ce7ca9d7e0e8983183080211a062d299468 \
--hash=sha256:69683e02e8a9de37f17985905a5eca18ad651bf592314b4d3d799029797d0eb3 \
--hash=sha256:6a93c5a0dfe8d34951e8a6f499a9479ffb9258123551fa007fc708ae2ac2bc5e \
--hash=sha256:732e026240cdd1c1b2e3ac515c7a23820430ed94292ce33806a95869c46bd139 \
--hash=sha256:7befc148de64b6060937231cbff8d01ccf0bfd75aa26383ffdf8d82b12ec04ff \
--hash=sha256:890da8cd1941fa3dab28c5bac3b9da8502e7e366f895b3b8e500896f12f94d11 \
--hash=sha256:89b64cd8898a3a6f642db4eb7b26d1b28a497d4022eccd7717ca066823e9fb01 \
--hash=sha256:8a6219108a15fc6d24de499d0d515c7235c617b2540d97116b663dade1a54d62 \
--hash=sha256:8cdf1a0dbe5ced887a9b127da4ffd7354e9c1a3b9bb330dce84df6b70ccb3a8d \
--hash=sha256:93a71c8601e823236ac0e5d087e4f397874a421017b3318fd92c0b14acf2b6db \
--hash=sha256:a29762cd3d116585278ffb2e5b8cc311fb095ea278b96feef28d0b423154858e \
--hash=sha256:aee110e4ef3c528f3abbc3c2018c121e708938adeeff9006428dd7c8555e9b3f \
--hash=sha256:b76d63495b0508ab9fc23f8152bac63205d2a704cd009a2b0722f4c8e0cba8e0 \
--hash=sha256:c0d8326269dbf944b9201911b0d9f3dc524d64779a07518199a58384c3d37a44 \
--hash=sha256:c68fe3fcde03920c46697585620135b4ecfdfc1ed23e75cc2c2ae9f8502c10b8 \
--hash=sha256:cb8bea573863762bbf45d1e13f87c2d2fd32cee2dbd50d050f83f87429c9e1ea \
--hash=sha256:ccae5de2a0140d8be6838c331604f91d6fafd0735dbdcee1ac78fc8fbaba76b4 \
--hash=sha256:e04b622bb8a88f10e439084486f2f6349bf4d50605ac3e445869c7ea5cf0fa8c \
--hash=sha256:e11d7ea4d24f0a262bccf9a7cd6284c976c5369dac21db237cff59586045ab9f \
--hash=sha256:e21f66748ab725ade40fa7af8ec8b5019c68ab00b929f6643e1b1af461eddb60 \
--hash=sha256:eb60b026d8ad0c97917cb81d3662d0b39b8ff1335e3fabb24984c6acd0c900a2 \
--hash=sha256:f552023710d4b93d8fb29a91fadf97de89c5926c6bd758897875435f2a939f33
python-gnupg==0.5.3 \ python-gnupg==0.5.3 \
--hash=sha256:290d8ddb9cd63df96cfe9284b9b265f19fd6e145e5582dc58fd7271f026d0a47 \ --hash=sha256:290d8ddb9cd63df96cfe9284b9b265f19fd6e145e5582dc58fd7271f026d0a47 \
--hash=sha256:2f8a4c6f63766feca6cc1416408f8b84e1b914fe7b54514e570fc5cbe92e9248 --hash=sha256:2f8a4c6f63766feca6cc1416408f8b84e1b914fe7b54514e570fc5cbe92e9248
@@ -311,3 +332,6 @@ MarkupSafe==3.0.1 \
pycparser==2.22 \ pycparser==2.22 \
--hash=sha256:491c8be9c040f5390f5bf44a5b07752bd07f56edf992381b05c701439eec10f6 \ --hash=sha256:491c8be9c040f5390f5bf44a5b07752bd07f56edf992381b05c701439eec10f6 \
--hash=sha256:c3702b6d3dd8c7abc1afa565d7e63d53a1d0bd86cdc24edd75470f4de499cfcc --hash=sha256:c3702b6d3dd8c7abc1afa565d7e63d53a1d0bd86cdc24edd75470f4de499cfcc
typing-extensions==4.6.0 \
--hash=sha256:6ad00b63f849b7dcc313b70b6b304ed67b2b2963b3098a33efe18056b1a9a223 \
--hash=sha256:ff6b238610c747e44c268aa4bb23c8c735d665a63726df3f9431ce707f2aa768