Load watched outputs from db.

This commit is contained in:
tecnovert 2019-07-23 17:08:12 +02:00
parent e039d90bee
commit b02ddb3bec
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
3 changed files with 24 additions and 12 deletions

View file

@ -629,7 +629,6 @@ class BasicSwap():
self.log.info('Loading data from db') self.log.info('Loading data from db')
self.mxDB.acquire() self.mxDB.acquire()
try: try:
self.log.debug('loadFromDB TODO')
session = scoped_session(self.session_factory) session = scoped_session(self.session_factory)
for bid in session.query(Bid): for bid in session.query(Bid):
if bid.state and bid.state > BidStates.BID_RECEIVED and bid.state < BidStates.SWAP_COMPLETED: 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' assert(offer), 'Offer not found'
self.swaps_in_progress[bid.bid_id] = (bid, offer) 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: finally:
session.close() session.close()
@ -1493,8 +1502,7 @@ class BasicSwap():
# TODO: Check connection type # TODO: Check connection type
bid.participate_txn_height = self.setLastHeightChecked(coin_type, tx_height) 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.addWatchedOutput(coin_type, bid_id, txid_hex, vout, BidStates.SWAP_PARTICIPATING)
self.coin_clients[coin_type]['watched_outputs'].append((bid_id, txid_hex, vout, BidStates.SWAP_PARTICIPATING))
def participateTxnConfirmed(self, bid_id, bid, offer): def participateTxnConfirmed(self, bid_id, bid, offer):
self.log.debug('participateTxnConfirmed for bid %s', bid_id.hex()) self.log.debug('participateTxnConfirmed for bid %s', bid_id.hex())
@ -1601,8 +1609,7 @@ class BasicSwap():
bid.initiate_txn_n = index bid.initiate_txn_n = index
# Start checking for spends of initiate_txn before fully confirmed # Start checking for spends of initiate_txn before fully confirmed
bid.initiate_txn_height = self.setLastHeightChecked(coin_from, tx_height) 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.addWatchedOutput(coin_from, bid_id, initiate_txnid_hex, bid.initiate_txn_n, BidStates.SWAP_INITIATED)
self.coin_clients[coin_from]['watched_outputs'].append((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: if bid.initiate_txn_state is None or bid.initiate_txn_state < TxStates.TX_SENT:
bid.setITXState(TxStates.TX_SENT) bid.setITXState(TxStates.TX_SENT)
save_bid = True save_bid = True
@ -1692,6 +1699,10 @@ class BasicSwap():
except Exception: except Exception:
return None 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): def removeWatchedOutput(self, coin_type, bid_id, txid_hex):
# Remove all for bid if txid is None # Remove all for bid if txid is None
self.log.debug('removeWatchedOutput %s %s %s', str(coin_type), bid_id.hex(), txid_hex) self.log.debug('removeWatchedOutput %s %s %s', str(coin_type), bid_id.hex(), txid_hex)

View file

@ -517,7 +517,7 @@ class HttpThread(threading.Thread, HTTPServer):
self.port_no = port_no self.port_no = port_no
self.allow_cors = allow_cors self.allow_cors = allow_cors
self.swap_client = swap_client 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.last_form_id = dict()
self.timeout = 60 self.timeout = 60

View file

@ -264,19 +264,20 @@ def main():
sys.stderr.write('Error: {} exists, exiting.\n'.format(config_path)) sys.stderr.write('Error: {} exists, exiting.\n'.format(config_path))
exit(1) exit(1)
port_offset = 300 if chain == testnet else 0
settings = { settings = {
'debug': True, 'debug': True,
'zmqhost': 'tcp://127.0.0.1', 'zmqhost': 'tcp://127.0.0.1',
'zmqport': 20792, 'zmqport': 20792 + port_offset,
'htmlhost': 'localhost', 'htmlhost': 'localhost',
'htmlport': 12700, 'htmlport': 12700 + port_offset,
'network_key': '7sW2UEcHXvuqEjkpE5mD584zRaQYs6WXYohue4jLFZPTvMSxwvgs', 'network_key': '7sW2UEcHXvuqEjkpE5mD584zRaQYs6WXYohue4jLFZPTvMSxwvgs',
'network_pubkey': '035758c4a22d7dd59165db02a56156e790224361eb3191f02197addcb3bde903d2', 'network_pubkey': '035758c4a22d7dd59165db02a56156e790224361eb3191f02197addcb3bde903d2',
'chainclients': { 'chainclients': {
'particl': { 'particl': {
'connection_type': 'rpc', 'connection_type': 'rpc',
'manage_daemon': True, 'manage_daemon': True,
'rpcport': 19792, 'rpcport': 19792 + port_offset,
'datadir': os.path.join(data_dir, 'particl'), 'datadir': os.path.join(data_dir, 'particl'),
'bindir': os.path.join(data_dir, 'bins', 'particl'), 'bindir': os.path.join(data_dir, 'bins', 'particl'),
'blocks_confirmed': 2 'blocks_confirmed': 2
@ -284,7 +285,7 @@ def main():
'litecoin': { 'litecoin': {
'connection_type': 'rpc' if 'litecoin' in with_coins else 'none', 'connection_type': 'rpc' if 'litecoin' in with_coins else 'none',
'manage_daemon': True if 'litecoin' in with_coins else False, 'manage_daemon': True if 'litecoin' in with_coins else False,
'rpcport': 19795, 'rpcport': 19795 + port_offset,
'datadir': os.path.join(data_dir, 'litecoin'), 'datadir': os.path.join(data_dir, 'litecoin'),
'bindir': os.path.join(data_dir, 'bins', 'litecoin'), 'bindir': os.path.join(data_dir, 'bins', 'litecoin'),
'use_segwit': True, 'use_segwit': True,
@ -293,7 +294,7 @@ def main():
'bitcoin': { 'bitcoin': {
'connection_type': 'rpc' if 'bitcoin' in with_coins else 'none', 'connection_type': 'rpc' if 'bitcoin' in with_coins else 'none',
'manage_daemon': True if 'bitcoin' in with_coins else False, 'manage_daemon': True if 'bitcoin' in with_coins else False,
'rpcport': 19796, 'rpcport': 19796 + port_offset,
'datadir': os.path.join(data_dir, 'bitcoin'), 'datadir': os.path.join(data_dir, 'bitcoin'),
'bindir': os.path.join(data_dir, 'bins', 'bitcoin'), 'bindir': os.path.join(data_dir, 'bins', 'bitcoin'),
'use_segwit': True 'use_segwit': True