From b02ddb3bec8784fbaaa102d9817d687f9fca1e63 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Tue, 23 Jul 2019 17:08:12 +0200 Subject: [PATCH] Load watched outputs from db. --- basicswap/basicswap.py | 23 +++++++++++++++++------ basicswap/http_server.py | 2 +- bin/basicswap_prepare.py | 11 ++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 2865190..d01eba9 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -629,7 +629,6 @@ class BasicSwap(): self.log.info('Loading data from db') self.mxDB.acquire() try: - self.log.debug('loadFromDB TODO') session = scoped_session(self.session_factory) for bid in session.query(Bid): if bid.state and bid.state > BidStates.BID_RECEIVED and bid.state < BidStates.SWAP_COMPLETED: @@ -639,7 +638,17 @@ class BasicSwap(): assert(offer), 'Offer not found' self.swaps_in_progress[bid.bid_id] = (bid, offer) - # TODO: load txns, must store height 1st seen + coin_from = Coins(offer.coin_from) + coin_to = Coins(offer.coin_to) + if bid.initiate_txid: + self.addWatchedOutput(coin_from, bid.bid_id, bid.initiate_txid.hex(), bid.initiate_txn_n, BidStates.SWAP_INITIATED) + if bid.participate_txid: + self.addWatchedOutput(coin_to, bid.bid_id, bid.participate_txid.hex(), bid.participate_txn_n, BidStates.SWAP_PARTICIPATING) + + if self.coin_clients[coin_from]['last_height_checked'] < 1: + self.coin_clients[coin_from]['last_height_checked'] = bid.initiate_txn_height + if self.coin_clients[coin_to]['last_height_checked'] < 1: + self.coin_clients[coin_to]['last_height_checked'] = bid.participate_txn_height finally: session.close() @@ -1493,8 +1502,7 @@ class BasicSwap(): # TODO: Check connection type bid.participate_txn_height = self.setLastHeightChecked(coin_type, tx_height) - self.log.debug('Adding watched output %s bid %s tx %s type %s', coin_type, bid_id.hex(), txid_hex, BidStates.SWAP_PARTICIPATING) - self.coin_clients[coin_type]['watched_outputs'].append((bid_id, txid_hex, vout, BidStates.SWAP_PARTICIPATING)) + self.addWatchedOutput(coin_type, bid_id, txid_hex, vout, BidStates.SWAP_PARTICIPATING) def participateTxnConfirmed(self, bid_id, bid, offer): self.log.debug('participateTxnConfirmed for bid %s', bid_id.hex()) @@ -1601,8 +1609,7 @@ class BasicSwap(): bid.initiate_txn_n = index # Start checking for spends of initiate_txn before fully confirmed bid.initiate_txn_height = self.setLastHeightChecked(coin_from, tx_height) - self.log.debug('Adding watched output %s bid %s tx %s type %s', coin_from, bid_id.hex(), initiate_txnid_hex, BidStates.SWAP_INITIATED) - self.coin_clients[coin_from]['watched_outputs'].append((bid_id, initiate_txnid_hex, bid.initiate_txn_n, BidStates.SWAP_INITIATED)) + self.addWatchedOutput(coin_from, bid_id, initiate_txnid_hex, bid.initiate_txn_n, BidStates.SWAP_INITIATED) if bid.initiate_txn_state is None or bid.initiate_txn_state < TxStates.TX_SENT: bid.setITXState(TxStates.TX_SENT) save_bid = True @@ -1692,6 +1699,10 @@ class BasicSwap(): except Exception: return None + def addWatchedOutput(self, coin_type, bid_id, txid_hex, vout, tx_type): + self.log.debug('Adding watched output %s bid %s tx %s type %s', coin_type, bid_id.hex(), txid_hex, tx_type) + self.coin_clients[coin_type]['watched_outputs'].append((bid_id, txid_hex, vout, tx_type)) + def removeWatchedOutput(self, coin_type, bid_id, txid_hex): # Remove all for bid if txid is None self.log.debug('removeWatchedOutput %s %s %s', str(coin_type), bid_id.hex(), txid_hex) diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 6bcf45b..022a74d 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -517,7 +517,7 @@ class HttpThread(threading.Thread, HTTPServer): self.port_no = port_no self.allow_cors = allow_cors self.swap_client = swap_client - self.title = 'Simple Atomic Swap Demo' + self.title = 'Simple Atomic Swap Demo, ' + self.swap_client.chain self.last_form_id = dict() self.timeout = 60 diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index 576d61d..25cdae8 100644 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -264,19 +264,20 @@ def main(): sys.stderr.write('Error: {} exists, exiting.\n'.format(config_path)) exit(1) + port_offset = 300 if chain == testnet else 0 settings = { 'debug': True, 'zmqhost': 'tcp://127.0.0.1', - 'zmqport': 20792, + 'zmqport': 20792 + port_offset, 'htmlhost': 'localhost', - 'htmlport': 12700, + 'htmlport': 12700 + port_offset, 'network_key': '7sW2UEcHXvuqEjkpE5mD584zRaQYs6WXYohue4jLFZPTvMSxwvgs', 'network_pubkey': '035758c4a22d7dd59165db02a56156e790224361eb3191f02197addcb3bde903d2', 'chainclients': { 'particl': { 'connection_type': 'rpc', 'manage_daemon': True, - 'rpcport': 19792, + 'rpcport': 19792 + port_offset, 'datadir': os.path.join(data_dir, 'particl'), 'bindir': os.path.join(data_dir, 'bins', 'particl'), 'blocks_confirmed': 2 @@ -284,7 +285,7 @@ def main(): 'litecoin': { 'connection_type': 'rpc' if 'litecoin' in with_coins else 'none', 'manage_daemon': True if 'litecoin' in with_coins else False, - 'rpcport': 19795, + 'rpcport': 19795 + port_offset, 'datadir': os.path.join(data_dir, 'litecoin'), 'bindir': os.path.join(data_dir, 'bins', 'litecoin'), 'use_segwit': True, @@ -293,7 +294,7 @@ def main(): 'bitcoin': { 'connection_type': 'rpc' if 'bitcoin' in with_coins else 'none', 'manage_daemon': True if 'bitcoin' in with_coins else False, - 'rpcport': 19796, + 'rpcport': 19796 + port_offset, 'datadir': os.path.join(data_dir, 'bitcoin'), 'bindir': os.path.join(data_dir, 'bins', 'bitcoin'), 'use_segwit': True