tests: Use debug_ind in non-xmr swaps.

This commit is contained in:
tecnovert 2021-01-30 16:29:07 +02:00
parent 2e0edef9da
commit df732713ac
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
8 changed files with 149 additions and 62 deletions

View file

@ -1,3 +1,3 @@
name = "basicswap"
__version__ = "0.0.14"
__version__ = "0.0.15"

View file

@ -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:

View file

@ -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

View file

@ -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

View file

@ -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):

View file

@ -79,6 +79,14 @@
{% endfor %}
</table>
<h4>Events</h4>
<table>
<tr><th>At</th><th>Event</th></tr>
{% for e in data.events %}
<tr><td>{{ e.at | formatts }}</td><td>{{ e.desc }}</td></tr>
{% endfor %}
</table>
<p><a href="/">home</a></p>
<script>
function confirmPopup() {

View file

@ -174,6 +174,7 @@ def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_b
'participate_conf': 'None' if (not bid.participate_tx or not bid.participate_tx.conf) else bid.participate_tx.conf,
'show_txns': show_txns,
'can_abandon': True if bid.state not in (BidStates.BID_ABANDONED, BidStates.SWAP_COMPLETED) else False,
'events': bid_events,
}
if edit_bid:
@ -225,6 +226,4 @@ def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_b
if view_tx_id == xmr_swap.a_lock_refund_spend_tx_id and xmr_swap.a_lock_refund_spend_tx:
data['view_tx_hex'] = xmr_swap.a_lock_refund_spend_tx.hex()
data['events'] = bid_events
return data

View file

@ -17,6 +17,7 @@ import os
import sys
import json
import time
import random
import shutil
import signal
import logging
@ -31,12 +32,15 @@ from basicswap.basicswap import (
SwapTypes,
BidStates,
TxStates,
DebugTypes,
SEQUENCE_LOCK_BLOCKS,
)
from basicswap.util import (
COIN,
toWIF,
dumpje,
make_int,
format_amount,
)
from basicswap.rpc import (
callrpc_cli,
@ -442,10 +446,8 @@ class Test(unittest.TestCase):
offer_id = swap_clients[1].postOffer(Coins.LTC, Coins.PART, 10 * COIN, 9.0 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST)
wait_for_offer(delay_event, swap_clients[0], offer_id)
offers = swap_clients[0].listOffers()
for offer in offers:
if offer.offer_id == offer_id:
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
offer = swap_clients[0].getOffer(offer_id)
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
wait_for_bid(delay_event, swap_clients[1], bid_id)
swap_clients[1].acceptBid(bid_id)
@ -467,10 +469,8 @@ class Test(unittest.TestCase):
offer_id = swap_clients[0].postOffer(Coins.LTC, Coins.BTC, 10 * COIN, 0.1 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST)
wait_for_offer(delay_event, swap_clients[1], offer_id)
offers = swap_clients[1].listOffers()
for offer in offers:
if offer.offer_id == offer_id:
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
offer = swap_clients[1].getOffer(offer_id)
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[0].acceptBid(bid_id)
@ -497,10 +497,8 @@ class Test(unittest.TestCase):
SEQUENCE_LOCK_BLOCKS, 10)
wait_for_offer(delay_event, swap_clients[1], offer_id)
offers = swap_clients[1].listOffers()
for offer in offers:
if offer.offer_id == offer_id:
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
offer = swap_clients[1].getOffer(offer_id)
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[1].abandonBid(bid_id)
@ -509,6 +507,11 @@ class Test(unittest.TestCase):
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.BID_ABANDONED, sent=True, wait_for=60)
js_0_bid = json.loads(urlopen('http://127.0.0.1:1800/json/bids/{}'.format(bid_id.hex())).read())
js_1_bid = json.loads(urlopen('http://127.0.0.1:1801/json/bids/{}'.format(bid_id.hex())).read())
assert(js_0_bid['itx_state'] == 'Refunded')
assert(js_1_bid['ptx_state'] == 'Unknown')
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
assert(js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
@ -523,10 +526,9 @@ class Test(unittest.TestCase):
offer_id = swap_clients[0].postOffer(Coins.BTC, Coins.LTC, 10 * COIN, 10 * COIN, 10 * COIN, SwapTypes.SELLER_FIRST)
wait_for_offer(delay_event, swap_clients[0], offer_id)
offer = swap_clients[0].getOffer(offer_id)
offers = swap_clients[0].listOffers()
for offer in offers:
if offer.offer_id == offer_id:
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[0].acceptBid(bid_id)
@ -547,10 +549,8 @@ class Test(unittest.TestCase):
offer_id = swap_clients[0].postOffer(Coins.BTC, Coins.LTC, 0.001 * COIN, 1.0 * COIN, 0.001 * COIN, SwapTypes.SELLER_FIRST)
wait_for_offer(delay_event, swap_clients[0], offer_id)
offers = swap_clients[0].listOffers()
for offer in offers:
if offer.offer_id == offer_id:
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
offer = swap_clients[0].getOffer(offer_id)
bid_id = swap_clients[0].postBid(offer_id, offer.amount_from)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[0].acceptBid(bid_id)
@ -569,28 +569,7 @@ class Test(unittest.TestCase):
offer_id = swap_clients[0].postOffer(Coins.PART, Coins.LTC, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.BUYER_FIRST)
return # TODO
wait_for_offer(delay_event, swap_clients[1], offer_id)
offers = swap_clients[1].listOffers()
assert(len(offers) == 1)
for offer in offers:
if offer.offer_id == offer_id:
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[0].acceptBid(bid_id)
wait_for_in_progress(delay_event, swap_clients[1], bid_id, sent=True)
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60)
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
assert(js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
assert(js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
logging.warning('TODO')
def test_09_part_ltc_auto_accept(self):
logging.info('---------- Test PART to LTC, auto accept bid')
@ -608,6 +587,70 @@ class Test(unittest.TestCase):
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60)
def test_10_bad_ptx(self):
# Invalid PTX sent, swap should stall and ITx and PTx should be reclaimed by senders
logging.info('---------- Test bad PTx, LTC to BTC')
swap_clients = self.swap_clients
swap_value = make_int(random.uniform(0.001, 10.0), scale=8, r=1)
logging.info('swap_value {}'.format(format_amount(swap_value, 8)))
offer_id = swap_clients[0].postOffer(Coins.LTC, Coins.BTC, swap_value, 0.1 * COIN, swap_value, SwapTypes.SELLER_FIRST,
SEQUENCE_LOCK_BLOCKS, 10)
wait_for_offer(delay_event, swap_clients[1], offer_id)
offer = swap_clients[1].getOffer(offer_id)
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
swap_clients[1].setBidDebugInd(bid_id, DebugTypes.MAKE_INVALID_PTX)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[0].acceptBid(bid_id)
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=120)
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=120)
js_0_bid = json.loads(urlopen('http://127.0.0.1:1800/json/bids/{}'.format(bid_id.hex())).read())
js_1_bid = json.loads(urlopen('http://127.0.0.1:1801/json/bids/{}'.format(bid_id.hex())).read())
assert(js_0_bid['itx_state'] == 'Refunded')
assert(js_1_bid['ptx_state'] == 'Refunded')
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
assert(js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
assert(js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
'''
def test_11_refund(self):
# Seller submits initiate txn, buyer doesn't respond, repeat of test 5 using debug_ind
logging.info('---------- Test refund, LTC to BTC')
swap_clients = self.swap_clients
swap_value = make_int(random.uniform(0.001, 10.0), scale=8, r=1)
logging.info('swap_value {}'.format(format_amount(swap_value, 8)))
offer_id = swap_clients[0].postOffer(Coins.LTC, Coins.BTC, swap_value, 0.1 * COIN, swap_value, SwapTypes.SELLER_FIRST,
SEQUENCE_LOCK_BLOCKS, 10)
wait_for_offer(delay_event, swap_clients[1], offer_id)
offer = swap_clients[1].getOffer(offer_id)
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
swap_clients[1].setBidDebugInd(bid_id, DebugTypes.BUYER_STOP_AFTER_ITX)
wait_for_bid(delay_event, swap_clients[0], bid_id)
swap_clients[0].acceptBid(bid_id)
wait_for_bid(delay_event, swap_clients[0], bid_id, BidStates.SWAP_COMPLETED, wait_for=120)
wait_for_bid(delay_event, swap_clients[1], bid_id, BidStates.BID_ABANDONED, sent=True, wait_for=120)
js_0_bid = json.loads(urlopen('http://127.0.0.1:1800/json/bids/{}'.format(bid_id.hex())).read())
js_1_bid = json.loads(urlopen('http://127.0.0.1:1801/json/bids/{}'.format(bid_id.hex())).read())
assert(js_0_bid['itx_state'] == 'Refunded')
assert(js_1_bid['ptx_state'] == 'Unknown')
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json').read())
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json').read())
assert(js_0['num_swapping'] == 0 and js_0['num_watched_outputs'] == 0)
assert(js_1['num_swapping'] == 0 and js_1['num_watched_outputs'] == 0)
'''
def pass_99_delay(self):
global stop_test
logging.info('Delay')