diff --git a/basicswap/__init__.py b/basicswap/__init__.py index fcc0e06..2dd6ee7 100644 --- a/basicswap/__init__.py +++ b/basicswap/__init__.py @@ -1,3 +1,3 @@ name = "basicswap" -__version__ = "0.0.14" +__version__ = "0.0.15" diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 0db581b..e97b4ea 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -211,6 +211,8 @@ class DebugTypes(IntEnum): BID_STOP_AFTER_COIN_A_LOCK = auto() BID_DONT_SPEND_COIN_A_LOCK_REFUND = auto() CREATE_INVALID_COIN_B_LOCK = auto() + BUYER_STOP_AFTER_ITX = auto() + MAKE_INVALID_PTX = auto() SEQUENCE_LOCK_BLOCKS = 1 @@ -754,11 +756,12 @@ class BasicSwap(BaseApp): session.remove() self.log.info('Upgraded database to version {}'.format(self.db_version)) continue - if db_version == 4: + if db_version == 7: session = scoped_session(self.session_factory) - session.execute('ALTER TABLE bids ADD COLUMN withdraw_to_addr TEXT') - session.execute('ALTER TABLE offers ADD COLUMN withdraw_to_addr TEXT') + session.execute('ALTER TABLE transactions ADD COLUMN block_hash BLOB') + session.execute('ALTER TABLE transactions ADD COLUMN block_height INTEGER') + session.execute('ALTER TABLE transactions ADD COLUMN block_time INTEGER') db_version += 1 self.db_version = db_version @@ -1181,7 +1184,7 @@ class BasicSwap(BaseApp): offer = session.query(Offer).filter_by(offer_id=offer_id).first() - if len(offer.security_token > 0) and offer.security_token != security_token: + if offer.security_token is not None and offer.security_token != security_token: raise ValueError('Mismatched security token') msg_buf = OfferRevokeMessage() @@ -1551,7 +1554,19 @@ class BasicSwap(BaseApp): linked_id=bid.bid_id, event_type=int(event_type), event_msg=event_msg) - session.add(entry) + + if session is not None: + session.add(entry) + return + self.mxDB.acquire() + try: + session = scoped_session(self.session_factory) + session.add(entry) + session.commit() + finally: + session.close() + session.remove() + self.mxDB.release() 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(TableTypes.BID), bid.bid_id.hex(), int(event_type))).first() @@ -1748,11 +1763,11 @@ class BasicSwap(BaseApp): xmr_swap = session.query(XmrSwap).filter_by(bid_id=bid.bid_id).first() xmr_offer = session.query(XmrOffer).filter_by(offer_id=bid.offer_id).first() self.loadBidTxns(bid, session) - if list_events: - events = self.list_bid_events(bid.bid_id, session) else: bid.initiate_tx = session.query(SwapTx).filter(sa.and_(SwapTx.bid_id == bid_id, SwapTx.tx_type == TxTypes.ITX)).first() bid.participate_tx = session.query(SwapTx).filter(sa.and_(SwapTx.bid_id == bid_id, SwapTx.tx_type == TxTypes.PTX)).first() + if list_events: + events = self.list_bid_events(bid.bid_id, session) return bid, xmr_swap, offer, xmr_offer, events finally: @@ -2198,7 +2213,6 @@ class BasicSwap(BaseApp): cblock_height = blockchaininfo['blocks'] max_tries = 1000 for i in range(max_tries): - self.log.debug('wtf %d', i) prev_block = self.callcoinrpc(coin_to, 'getblock', [cblock_hash, ]) self.log.debug('prev_block %s', str(prev_block)) @@ -2232,6 +2246,11 @@ class BasicSwap(BaseApp): # Check required? assert(amount_to == (bid.amount * offer.rate) // self.ci(offer.coin_from).COIN()) + if bid.debug_ind == DebugTypes.MAKE_INVALID_PTX: + amount_to -= 1 + self.log.debug('bid %s: Make invalid PTx for testing: %d.', bid_id.hex(), bid.debug_ind) + self.logBidEvent(bid, EventLogTypes.DEBUG_TWEAK_APPLIED, 'ind {}'.format(bid.debug_ind), None) + if self.coin_clients[coin_to]['use_segwit']: p2wsh = getP2WSH(participate_script) addr_to = self.encodeSegwitP2WSH(coin_to, p2wsh) @@ -2491,6 +2510,12 @@ class BasicSwap(BaseApp): bid.setState(BidStates.SWAP_INITIATED) bid.setITxState(TxStates.TX_CONFIRMED) + if bid.debug_ind == DebugTypes.BUYER_STOP_AFTER_ITX: + self.log.debug('bid %s: Abandoning bid for testing: %d.', bid_id.hex(), bid.debug_ind) + bid.setState(BidStates.BID_ABANDONED) + self.logBidEvent(bid, EventLogTypes.DEBUG_TWEAK_APPLIED, 'ind {}'.format(bid.debug_ind), None) + return # Bid saved in checkBidState + # Seller first mode, buyer participates participate_script = self.deriveParticipateScript(bid_id, bid, offer) if bid.was_sent: @@ -2511,7 +2536,7 @@ class BasicSwap(BaseApp): script=participate_script, ) - # bid saved in checkBidState + # Bid saved in checkBidState def setLastHeightChecked(self, coin_type, tx_height): chain_name = chainparams[coin_type]['name'] @@ -2661,6 +2686,7 @@ class BasicSwap(BaseApp): bid.setState(BidStates.BID_ABANDONED) rv = True self.saveBidInSession(bid_id, bid, session, xmr_swap) + self.logBidEvent(bid, EventLogTypes.DEBUG_TWEAK_APPLIED, 'ind {}'.format(bid.debug_ind), session) session.commit() return rv @@ -2872,6 +2898,9 @@ class BasicSwap(BaseApp): coin_to = Coins(offer.coin_to) # TODO: Batch calls to scantxoutset # TODO: timeouts + if state == BidStates.BID_ABANDONED: + self.log.info('Deactivating abandoned bid: %s', bid_id.hex()) + return True # Mark bid for archiving if state == BidStates.BID_ACCEPTED: # Waiting for initiate txn to be confirmed in 'from' chain initiate_txnid_hex = bid.initiate_tx.txid.hex() @@ -3096,13 +3125,13 @@ class BasicSwap(BaseApp): # TODO: Wait for depth? bid.setPTxState(TxStates.TX_REDEEMED) - if bid.was_sent: - txn = self.createRedeemTxn(coin_from, bid, for_txn_type='initiate') - txid = self.submitTxn(coin_from, txn) + if bid.was_sent: + txn = self.createRedeemTxn(coin_from, bid, for_txn_type='initiate') + txid = self.submitTxn(coin_from, txn) - bid.initiate_tx.spend_txid = bytes.fromhex(txid) - # bid.initiate_txn_redeem = bytes.fromhex(txn) # Worth keeping? - self.log.debug('Submitted initiate redeem txn %s to %s chain for bid %s', txid, chainparams[coin_from]['name'], bid_id.hex()) + bid.initiate_tx.spend_txid = bytes.fromhex(txid) + # bid.initiate_txn_redeem = bytes.fromhex(txn) # Worth keeping? + self.log.debug('Submitted initiate redeem txn %s to %s chain for bid %s', txid, chainparams[coin_from]['name'], bid_id.hex()) # TODO: Wait for depth? new state SWAP_TXI_REDEEM_SENT? @@ -4089,6 +4118,7 @@ class BasicSwap(BaseApp): if bid.debug_ind == DebugTypes.CREATE_INVALID_COIN_B_LOCK: self.log.debug('XMR bid %s: Debug %d - Reducing lock b txn amount by 10%%.', bid_id.hex(), bid.debug_ind) bid.amount_to -= int(bid.amount_to * 0.1) + self.logBidEvent(bid, EventLogTypes.DEBUG_TWEAK_APPLIED, 'ind {}'.format(bid.debug_ind), session) try: b_lock_tx_id = ci_to.publishBLockTx(xmr_swap.pkbv, xmr_swap.pkbs, bid.amount_to, xmr_offer.b_fee_rate) except Exception as ex: diff --git a/basicswap/chainparams.py b/basicswap/chainparams.py index 0ba3978..80b1573 100644 --- a/basicswap/chainparams.py +++ b/basicswap/chainparams.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2019-2020 tecnovert +# Copyright (c) 2019-2021 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. from enum import IntEnum from .util import ( COIN, + make_int, format_amount, - make_int ) XMR_COIN = 10 ** 12 diff --git a/basicswap/db.py b/basicswap/db.py index c3e8ebd..bcf325f 100644 --- a/basicswap/db.py +++ b/basicswap/db.py @@ -12,7 +12,7 @@ from sqlalchemy.ext.declarative import declarative_base from enum import IntEnum, auto -CURRENT_DB_VERSION = 7 +CURRENT_DB_VERSION = 8 Base = declarative_base() @@ -178,6 +178,10 @@ class SwapTx(Base): spend_txid = sa.Column(sa.LargeBinary) spend_n = sa.Column(sa.Integer) + block_hash = sa.Column(sa.LargeBinary) + block_height = sa.Column(sa.Integer) + block_time = sa.Column(sa.BigInteger) + state = sa.Column(sa.Integer) states = sa.Column(sa.LargeBinary) # Packed states and times diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 60e84b0..2951a29 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -736,6 +736,9 @@ class HttpHandler(BaseHTTPRequestHandler): data = describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, events, edit_bid, show_txns, view_tx_ind) + if bid.debug_ind is not None and bid.debug_ind > 0: + messages.append('Debug flag set: {}'.format(bid.debug_ind)) + old_states = [] num_states = len(bid.states) // 12 for i in range(num_states): diff --git a/basicswap/templates/bid.html b/basicswap/templates/bid.html index 727feed..c0f2028 100644 --- a/basicswap/templates/bid.html +++ b/basicswap/templates/bid.html @@ -79,6 +79,14 @@ {% endfor %} +

Events

+ + +{% for e in data.events %} + +{% endfor %} +
AtEvent
{{ e.at | formatts }}{{ e.desc }}
+

home