Clear event queue when abandoning a bid.

2024-05-20_merge
tecnovert 4 years ago
parent c5faaeccf8
commit 69ed836496
No known key found for this signature in database
GPG Key ID: 8ED6D8750C4E3F93
  1. 48
      basicswap/basicswap.py
  2. 12
      basicswap/http_server.py
  3. 9
      basicswap/js_server.py
  4. 4
      basicswap/templates/bid_xmr.html
  5. 2
      basicswap/templates/offer_new_2.html
  6. 2
      basicswap/ui.py
  7. 8
      basicswap/util.py

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2019-2020 tecnovert # Copyright (c) 2019-2021 tecnovert
# 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.
@ -36,6 +36,7 @@ from .util import (
pubkeyToAddress, pubkeyToAddress,
format8, format8,
format_amount, format_amount,
format_timestamp,
encodeAddress, encodeAddress,
decodeAddress, decodeAddress,
DeserialiseNum, DeserialiseNum,
@ -895,6 +896,12 @@ class BasicSwap(BaseApp):
if ptx_state is not None and ptx_state != TxStates.TX_REFUNDED: if ptx_state is not None and ptx_state != TxStates.TX_REFUNDED:
self.returnAddressToPool(bid.bid_id, TxTypes.PTX_REFUND) self.returnAddressToPool(bid.bid_id, TxTypes.PTX_REFUND)
# Remove any delayed events
if self.debug:
session.execute('UPDATE eventqueue SET active_ind = 2 WHERE linked_id = {}'.format(bid.bid_id))
else:
session.execute('DELETE FROM eventqueue WHERE linked_id = {}'.format(bid.bid_id))
def loadFromDB(self): def loadFromDB(self):
self.log.info('Loading data from db') self.log.info('Loading data from db')
self.mxDB.acquire() self.mxDB.acquire()
@ -955,7 +962,7 @@ class BasicSwap(BaseApp):
def validateOfferLockValue(self, coin_from, coin_to, lock_type, lock_value): def validateOfferLockValue(self, coin_from, coin_to, lock_type, lock_value):
if lock_type == OfferMessage.SEQUENCE_LOCK_TIME: if lock_type == OfferMessage.SEQUENCE_LOCK_TIME:
assert(lock_value >= 2 * 60 * 60 and lock_value <= 96 * 60 * 60), 'Invalid lock_value time' assert(lock_value >= 1 * 60 * 60 and lock_value <= 96 * 60 * 60), 'Invalid lock_value time'
assert(self.coin_clients[coin_from]['use_csv'] and self.coin_clients[coin_to]['use_csv']), 'Both coins need CSV activated.' assert(self.coin_clients[coin_from]['use_csv'] and self.coin_clients[coin_to]['use_csv']), 'Both coins need CSV activated.'
elif lock_type == OfferMessage.SEQUENCE_LOCK_BLOCKS: elif lock_type == OfferMessage.SEQUENCE_LOCK_BLOCKS:
assert(lock_value >= 5 and lock_value <= 1000), 'Invalid lock_value blocks' assert(lock_value >= 5 and lock_value <= 1000), 'Invalid lock_value blocks'
@ -1681,11 +1688,19 @@ class BasicSwap(BaseApp):
def list_bid_events(self, bid_id, session): def list_bid_events(self, bid_id, session):
session = scoped_session(self.session_factory) session = scoped_session(self.session_factory)
query_str = 'SELECT created_at, event_type, event_msg FROM eventlog ' + \ query_str = 'SELECT created_at, event_type, event_msg FROM eventlog ' + \
'WHERE linked_type = {} AND linked_id = x\'{}\' '.format(TableTypes.BID, bid_id.hex()) 'WHERE active_ind = 1 AND linked_type = {} AND linked_id = x\'{}\' '.format(TableTypes.BID, bid_id.hex())
q = self.engine.execute(query_str) q = self.engine.execute(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 eventqueue ' + \
'WHERE active_ind = 1 AND linked_id = x\'{}\' '.format(bid_id.hex())
q = self.engine.execute(query_str)
events = []
for row in q:
events.append({'at': row[0], 'desc': 'Delaying until: {}'.format(format_timestamp(row[1]))})
return events return events
def acceptBid(self, bid_id): def acceptBid(self, bid_id):
@ -2037,24 +2052,6 @@ class BasicSwap(BaseApp):
finally: finally:
self.mxDB.release() self.mxDB.release()
def abandonOffer(self, offer_id):
self.log.info('Abandoning Offer %s', offer_id.hex())
self.mxDB.acquire()
try:
session = scoped_session(self.session_factory)
offer = session.query(Offer).filter_by(offer_id=offer_id).first()
assert(offer), 'Offer not found'
# TODO: abandon linked bids?
# Mark bid as abandoned, no further processing will be done
offer.setState(OfferStates.OFFER_ABANDONED)
session.commit()
finally:
session.close()
session.remove()
self.mxDB.release()
def abandonBid(self, bid_id): def abandonBid(self, bid_id):
self.log.info('Abandoning Bid %s', bid_id.hex()) self.log.info('Abandoning Bid %s', bid_id.hex())
self.mxDB.acquire() self.mxDB.acquire()
@ -3289,6 +3286,7 @@ class BasicSwap(BaseApp):
self.log.info('Verify xmr bid {} failed: {}'.format(bid.bid_id.hex(), str(ex))) self.log.info('Verify xmr bid {} failed: {}'.format(bid.bid_id.hex(), str(ex)))
bid.setState(BidStates.BID_ERROR, 'Failed validation: ' + str(ex)) bid.setState(BidStates.BID_ERROR, 'Failed validation: ' + str(ex))
session.add(bid) session.add(bid)
self.updateBidInProgress(bid)
continue continue
if bid.created_at + ttl_xmr_split_messages < now: if bid.created_at + ttl_xmr_split_messages < now:
self.log.debug('Expiring partially received bid: {}'.format(bid.bid_id.hex())) self.log.debug('Expiring partially received bid: {}'.format(bid.bid_id.hex()))
@ -4500,7 +4498,7 @@ class BasicSwap(BaseApp):
else: else:
self.log.debug('TODO - determine in-progress for manualBidUpdate') self.log.debug('TODO - determine in-progress for manualBidUpdate')
if offer.swap_type == SwapTypes.XMR_SWAP: if offer.swap_type == SwapTypes.XMR_SWAP:
if bid.state and bid.state in (BidStates.XMR_SWAP_HAVE_SCRIPT_COIN_SPEND_TX, BidStates.XMR_SWAP_SCRIPT_COIN_LOCKED, BidStates.XMR_SWAP_LOCK_RELEASED, BidStates.XMR_SWAP_NOSCRIPT_TX_REDEEMED): if bid.state and bid.state in (BidStates.XMR_SWAP_HAVE_SCRIPT_COIN_SPEND_TX, BidStates.XMR_SWAP_SCRIPT_COIN_LOCKED, BidStates.XMR_SWAP_NOSCRIPT_COIN_LOCKED, BidStates.XMR_SWAP_LOCK_RELEASED, BidStates.XMR_SWAP_NOSCRIPT_TX_REDEEMED):
activate_bid = True activate_bid = True
if activate_bid: if activate_bid:
@ -4792,6 +4790,12 @@ class BasicSwap(BaseApp):
return True if passed is True else False # _possibly_revoked_offers should not contain duplicates return True if passed is True else False # _possibly_revoked_offers should not contain duplicates
return False return False
def updateBidInProgress(self, bid):
swap_in_progress = self.swaps_in_progress.get(bid.bid_id, None)
if swap_in_progress is None:
return
self.swaps_in_progress[bid.bid_id] = (bid, swap_in_progress[1])
def add_connection(self, host, port, peer_pubkey): def add_connection(self, host, port, peer_pubkey):
self.log.info('add_connection %s %d %s', host, port, peer_pubkey.hex()) self.log.info('add_connection %s %d %s', host, port, peer_pubkey.hex())
self._network.add_connection(host, port, peer_pubkey) self._network.add_connection(host, port, peer_pubkey)

@ -1,11 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2019 tecnovert # Copyright (c) 2019-2021 tecnovert
# 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.
import os import os
import time
import struct import struct
import traceback import traceback
import threading import threading
@ -16,6 +15,7 @@ from jinja2 import Environment, PackageLoader
from . import __version__ from . import __version__
from .util import ( from .util import (
format_timestamp,
dumpj, dumpj,
) )
from .chainparams import ( from .chainparams import (
@ -49,10 +49,6 @@ from .ui import (
) )
def format_timestamp(value):
return time.strftime('%Y-%m-%d %H:%M', time.localtime(value))
env = Environment(loader=PackageLoader('basicswap', 'templates')) env = Environment(loader=PackageLoader('basicswap', 'templates'))
env.filters['formatts'] = format_timestamp env.filters['formatts'] = format_timestamp
@ -635,7 +631,7 @@ class HttpHandler(BaseHTTPRequestHandler):
ci_from = swap_client.ci(Coins(o.coin_from)) ci_from = swap_client.ci(Coins(o.coin_from))
ci_to = swap_client.ci(Coins(o.coin_to)) ci_to = swap_client.ci(Coins(o.coin_to))
formatted_offers.append(( formatted_offers.append((
time.strftime('%Y-%m-%d %H:%M', time.localtime(o.created_at)), format_timestamp(o.created_at),
o.offer_id.hex(), o.offer_id.hex(),
ci_from.coin_name(), ci_to.coin_name(), ci_from.coin_name(), ci_to.coin_name(),
ci_from.format_amount(o.amount_from), ci_from.format_amount(o.amount_from),
@ -758,7 +754,7 @@ class HttpHandler(BaseHTTPRequestHandler):
title=self.server.title, title=self.server.title,
h2=self.server.title, h2=self.server.title,
page_type='Sent' if sent else 'Received', page_type='Sent' if sent else 'Received',
bids=[(time.strftime('%Y-%m-%d %H:%M', time.localtime(b[0])), bids=[(format_timestamp(b[0]),
b[1].hex(), b[2].hex(), strBidState(b[4]), strTxState(b[6]), strTxState(b[7])) for b in bids], b[1].hex(), b[2].hex(), strBidState(b[4]), strTxState(b[6]), strTxState(b[7])) for b in bids],
), 'UTF-8') ), 'UTF-8')

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2020 tecnovert # Copyright (c) 2020-2021 tecnovert
# 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.
@ -8,7 +8,10 @@ import json
import time import time
import urllib.parse import urllib.parse
from .util import format8 from .util import (
format8,
format_timestamp,
)
from .basicswap import ( from .basicswap import (
strBidState, strBidState,
SwapTypes, SwapTypes,
@ -143,7 +146,7 @@ def js_bids(self, url_split, post_string):
return bytes(json.dumps([{ return bytes(json.dumps([{
'bid_id': b[1].hex(), 'bid_id': b[1].hex(),
'offer_id': b[2].hex(), 'offer_id': b[2].hex(),
'created_at': time.strftime('%Y-%m-%d %H:%M', time.localtime(b[0])), 'created_at': format_timestamp(b[0]),
'amount_from': format8(b[3]), 'amount_from': format8(b[3]),
'bid_state': strBidState(b[4]) 'bid_state': strBidState(b[4])
} for b in bids]), 'UTF-8') } for b in bids]), 'UTF-8')

@ -40,10 +40,10 @@
<input name="edit_bid_submit" type="submit" value="Submit"> <input name="edit_bid_submit" type="submit" value="Submit">
{% else %} {% else %}
{% if data.was_received == 'True' %} {% if data.was_received == 'True' %}
<input name="accept_bid" type="submit" value="Accept Bid" onclick="confirmPopup(\"Accept\")"><br/> <input name="accept_bid" type="submit" value="Accept Bid" onclick='confirmPopup("Accept")'><br/>
{% endif %} {% endif %}
{% if data.can_abandon == true %} {% if data.can_abandon == true %}
<input name="abandon_bid" type="submit" value="Abandon Bid" onclick="confirmPopup(\"Abandon\")"> <input name="abandon_bid" type="submit" value="Abandon Bid" onclick='confirmPopup("Abandon")'>
{% endif %} {% endif %}
{% if data.show_txns %} {% if data.show_txns %}
<input name="hide_txns" type="submit" value="Hide Info"> <input name="hide_txns" type="submit" value="Hide Info">

@ -51,7 +51,7 @@
</select></td></tr> </select></td></tr>
{% endif %} {% endif %}
<tr class="padded_row"><td>Contract locked (hrs)</td><td><input type="number" name="lockhrs" min="1" max="64" value="{{ data.lockhrs }}"></td><td colspan=2>Participate txn will be locked for half the time.</td></tr> <tr class="padded_row"><td>Contract locked (hrs)</td><td><input type="number" name="lockhrs" min="1" max="96" value="{{ data.lockhrs }}"></td><td colspan=2>Participate txn will be locked for half the time.</td></tr>
<tr><td>Auto Accept Bids</td><td colspan=3><input type="checkbox" name="autoaccept" value="aa" {% if data.autoaccept==true %} checked="true"{% endif %}></td></tr> <tr><td>Auto Accept Bids</td><td colspan=3><input type="checkbox" name="autoaccept" value="aa" {% if data.autoaccept==true %} checked="true"{% endif %}></td></tr>
</table> </table>

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2020 tecnovert # Copyright (c) 2020-2021 tecnovert
# 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.

@ -1,10 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2018-2020 tecnovert # Copyright (c) 2018-2021 tecnovert
# 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.
import json import json
import time
import decimal import decimal
import hashlib import hashlib
@ -304,6 +306,10 @@ def format_amount(i, display_scale, scale=None):
return rv return rv
def format_timestamp(value):
return time.strftime('%Y-%m-%d %H:%M', time.localtime(value))
def getP2SHScriptForHash(p2sh): def getP2SHScriptForHash(p2sh):
return bytes([OpCodes.OP_HASH160, 0x14]) \ return bytes([OpCodes.OP_HASH160, 0x14]) \
+ p2sh \ + p2sh \

Loading…
Cancel
Save