Add bid request accepted state.

2024-05-20_merge
tecnovert 1 year ago
parent 303499fc6f
commit 705ac2c6fc
No known key found for this signature in database
GPG Key ID: 8ED6D8750C4E3F93
  1. 36
      basicswap/basicswap.py
  2. 40
      basicswap/basicswap_util.py
  3. 2
      basicswap/db_upgrades.py
  4. 2
      basicswap/js_server.py

@ -294,9 +294,9 @@ class BasicSwap(BaseApp):
self.network_pubkey = self.settings['network_pubkey'] self.network_pubkey = self.settings['network_pubkey']
self.network_addr = pubkeyToAddress(chainparams[Coins.PART][self.chain]['pubkey_address'], bytes.fromhex(self.network_pubkey)) self.network_addr = pubkeyToAddress(chainparams[Coins.PART][self.chain]['pubkey_address'], bytes.fromhex(self.network_pubkey))
self.db_echo = self.settings.get('db_echo', False) self.db_echo: bool = self.settings.get('db_echo', False)
self.sqlite_file = os.path.join(self.data_dir, 'db{}.sqlite'.format('' if self.chain == 'mainnet' else ('_' + self.chain))) self.sqlite_file: str = os.path.join(self.data_dir, 'db{}.sqlite'.format('' if self.chain == 'mainnet' else ('_' + self.chain)))
db_exists = os.path.exists(self.sqlite_file) db_exists: bool = os.path.exists(self.sqlite_file)
# HACK: create_all hangs when using tox, unless create_engine is called with echo=True # HACK: create_all hangs when using tox, unless create_engine is called with echo=True
if not db_exists: if not db_exists:
@ -1143,7 +1143,11 @@ class BasicSwap(BaseApp):
def validateSwapType(self, coin_from, coin_to, swap_type): def validateSwapType(self, coin_from, coin_to, swap_type):
if (coin_from in self.adaptor_swap_only_coins or coin_to in self.adaptor_swap_only_coins) and swap_type != SwapTypes.XMR_SWAP: if (coin_from in self.adaptor_swap_only_coins or coin_to in self.adaptor_swap_only_coins) and swap_type != SwapTypes.XMR_SWAP:
raise ValueError('Invalid swap type for: {} -> {}'.format(coin_from.name, coin_to.name)) raise ValueError('Invalid swap type for: {} -> {}'.format(coin_from.name, coin_to.name))
if (coin_from in self.secret_hash_swap_only_coins or coin_to in self.secret_hash_swap_only_coins) and swap_type == SwapTypes.XMR_SWAP:
if swap_type == SwapTypes.XMR_SWAP:
if (coin_from in self.secret_hash_swap_only_coins or coin_to in self.secret_hash_swap_only_coins):
raise ValueError('Invalid swap type for: {} -> {}'.format(coin_from.name, coin_to.name))
if (coin_from in self.scriptless_coins and coin_to in self.scriptless_coins):
raise ValueError('Invalid swap type for: {} -> {}'.format(coin_from.name, coin_to.name)) raise ValueError('Invalid swap type for: {} -> {}'.format(coin_from.name, coin_to.name))
def notify(self, event_type, event_data, session=None) -> None: def notify(self, event_type, event_data, session=None) -> None:
@ -2780,7 +2784,7 @@ class BasicSwap(BaseApp):
if ci_to.curve_type() == Curves.ed25519: if ci_to.curve_type() == Curves.ed25519:
self.sendXmrSplitMessages(XmrSplitMsgTypes.BID_ACCEPT, addr_from, addr_to, xmr_swap.bid_id, xmr_swap.kbsl_dleag, msg_valid, bid_msg_ids) self.sendXmrSplitMessages(XmrSplitMsgTypes.BID_ACCEPT, addr_from, addr_to, xmr_swap.bid_id, xmr_swap.kbsl_dleag, msg_valid, bid_msg_ids)
bid.setState(BidStates.BID_ACCEPTED) bid.setState(BidStates.BID_ACCEPTED) # ADS
session = self.openSession() session = self.openSession()
try: try:
@ -2862,18 +2866,19 @@ class BasicSwap(BaseApp):
addr_from: str = offer.addr_from addr_from: str = offer.addr_from
addr_to: str = bid.bid_addr addr_to: str = bid.bid_addr
msg_valid: int = self.getAcceptBidMsgValidTime(bid) msg_valid: int = self.getAcceptBidMsgValidTime(bid)
bid_accept_id = self.sendSmsg(addr_from, addr_to, payload_hex, msg_valid)
bid_msg_ids = {} bid_msg_ids = {}
bid_msg_ids[0] = self.sendSmsg(addr_from, addr_to, payload_hex, msg_valid)
if ci_to.curve_type() == Curves.ed25519: if ci_to.curve_type() == Curves.ed25519:
self.sendXmrSplitMessages(XmrSplitMsgTypes.BID, addr_from, addr_to, xmr_swap.bid_id, xmr_swap.kbsf_dleag, msg_valid, bid_msg_ids) self.sendXmrSplitMessages(XmrSplitMsgTypes.BID, addr_from, addr_to, xmr_swap.bid_id, xmr_swap.kbsf_dleag, msg_valid, bid_msg_ids)
bid.setState(BidStates.BID_REQUEST_ACCEPTED)
session = self.openSession() session = self.openSession()
try: try:
self.addMessageLink(Concepts.BID, bid_id, MessageTypes.ADS_BID_ACCEPT_FL, bid_accept_id, session=session)
for k, msg_id in bid_msg_ids.items(): for k, msg_id in bid_msg_ids.items():
self.addMessageLink(Concepts.BID, bid_id, MessageTypes.BID, msg_id, msg_sequence=k, session=session) self.addMessageLink(Concepts.BID, bid_id, MessageTypes.ADS_BID_ACCEPT_FL, msg_id, msg_sequence=k, session=session)
self.log.info('Sent ADS_BID_ACCEPT_FL %s', bid_accept_id.hex()) self.log.info('Sent ADS_BID_ACCEPT_FL %s', bid_msg_ids[0].hex())
self.saveBidInSession(bid_id, bid, session, xmr_swap=xmr_swap) self.saveBidInSession(bid_id, bid, session, xmr_swap=xmr_swap)
finally: finally:
self.closeSession(session) self.closeSession(session)
@ -4615,7 +4620,7 @@ class BasicSwap(BaseApp):
bid.setState(BidStates.BID_RECEIVED) bid.setState(BidStates.BID_RECEIVED)
self.saveBid(bid_id, bid) self.saveBid(bid_id, bid)
self.notify(NT.BID_RECEIVED, {'type': 'atomic', 'bid_id': bid_id.hex(), 'offer_id': bid_data.offer_msg_id.hex()}) self.notify(NT.BID_RECEIVED, {'type': 'secrethash', 'bid_id': bid_id.hex(), 'offer_id': bid_data.offer_msg_id.hex()})
if self.shouldAutoAcceptBid(offer, bid): if self.shouldAutoAcceptBid(offer, bid):
delay = random.randrange(self.min_delay_event, self.max_delay_event) delay = random.randrange(self.min_delay_event, self.max_delay_event)
@ -4761,7 +4766,7 @@ class BasicSwap(BaseApp):
ensure(ci_from.verifyPubkey(xmr_swap.pkaf), 'Invalid pubkey, pkaf') ensure(ci_from.verifyPubkey(xmr_swap.pkaf), 'Invalid pubkey, pkaf')
if not reverse_bid: # notify already ran in processADSBidReversed if not reverse_bid: # notify already ran in processADSBidReversed
self.notify(NT.BID_RECEIVED, {'type': 'xmr', 'bid_id': bid.bid_id.hex(), 'offer_id': bid.offer_id.hex()}, session) self.notify(NT.BID_RECEIVED, {'type': 'ads', 'bid_id': bid.bid_id.hex(), 'offer_id': bid.offer_id.hex()}, session)
bid.setState(BidStates.BID_RECEIVED) bid.setState(BidStates.BID_RECEIVED)
@ -4829,7 +4834,7 @@ class BasicSwap(BaseApp):
if xmr_swap.pkal == xmr_swap.pkaf: if xmr_swap.pkal == xmr_swap.pkaf:
raise ValueError('Duplicate script spend pubkey.') raise ValueError('Duplicate script spend pubkey.')
bid.setState(BidStates.BID_ACCEPTED) # XMR bid.setState(BidStates.BID_ACCEPTED) # ADS
self.saveBidInSession(bid.bid_id, bid, session, xmr_swap) self.saveBidInSession(bid.bid_id, bid, session, xmr_swap)
self.notify(NT.BID_ACCEPTED, {'bid_id': bid.bid_id.hex()}, session) self.notify(NT.BID_ACCEPTED, {'bid_id': bid.bid_id.hex()}, session)
@ -5000,7 +5005,7 @@ class BasicSwap(BaseApp):
v = ci_from.verifyTxSig(xmr_swap.a_lock_refund_tx, xmr_swap.al_lock_refund_tx_sig, xmr_swap.pkal, 0, xmr_swap.a_lock_tx_script, prevout_amount) v = ci_from.verifyTxSig(xmr_swap.a_lock_refund_tx, xmr_swap.al_lock_refund_tx_sig, xmr_swap.pkal, 0, xmr_swap.a_lock_tx_script, prevout_amount)
ensure(v, 'Invalid coin A lock refund tx leader sig') ensure(v, 'Invalid coin A lock refund tx leader sig')
allowed_states = [BidStates.BID_SENT, BidStates.BID_RECEIVED] allowed_states = [BidStates.BID_SENT, BidStates.BID_RECEIVED, BidStates.BID_REQUEST_ACCEPTED]
if bid.was_sent and offer.was_sent: if bid.was_sent and offer.was_sent:
allowed_states.append(BidStates.BID_ACCEPTED) # TODO: Split BID_ACCEPTED into received and sent allowed_states.append(BidStates.BID_ACCEPTED) # TODO: Split BID_ACCEPTED into received and sent
ensure(bid.state in allowed_states, 'Invalid state for bid {}'.format(bid.state)) ensure(bid.state in allowed_states, 'Invalid state for bid {}'.format(bid.state))
@ -5632,6 +5637,7 @@ class BasicSwap(BaseApp):
# Validate data # Validate data
ensure(len(msg_data.msg_id) == 28, 'Bad msg_id length') ensure(len(msg_data.msg_id) == 28, 'Bad msg_id length')
self.log.debug('for bid %s', msg_data.msg_id.hex())
# TODO: Wait for bid msg to arrive first # TODO: Wait for bid msg to arrive first
@ -5783,7 +5789,7 @@ class BasicSwap(BaseApp):
try: try:
session = self.openSession() session = self.openSession()
self.notify(NT.BID_RECEIVED, {'type': 'as_reversed', 'bid_id': bid.bid_id.hex(), 'offer_id': bid.offer_id.hex()}, session) self.notify(NT.BID_RECEIVED, {'type': 'ads_reversed', 'bid_id': bid.bid_id.hex(), 'offer_id': bid.offer_id.hex()}, session)
options = {'reverse_bid': True, 'bid_rate': bid_data.rate} options = {'reverse_bid': True, 'bid_rate': bid_data.rate}
if self.shouldAutoAcceptBid(offer, bid, session, options=options): if self.shouldAutoAcceptBid(offer, bid, session, options=options):

@ -102,6 +102,7 @@ class BidStates(IntEnum):
XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS = 27 # XmrBidLockTxSigsMessage XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS = 27 # XmrBidLockTxSigsMessage
XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX = 28 # XmrBidLockSpendTxMessage XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX = 28 # XmrBidLockSpendTxMessage
BID_REQUEST_SENT = 29 BID_REQUEST_SENT = 29
BID_REQUEST_ACCEPTED = 30
class TxStates(IntEnum): class TxStates(IntEnum):
@ -303,6 +304,8 @@ def strBidState(state):
return 'Exchanged script lock spend tx msg' return 'Exchanged script lock spend tx msg'
if state == BidStates.BID_REQUEST_SENT: if state == BidStates.BID_REQUEST_SENT:
return 'Request sent' return 'Request sent'
if state == BidStates.BID_REQUEST_ACCEPTED:
return 'Request accepted'
return 'Unknown' + ' ' + str(state) return 'Unknown' + ' ' + str(state)
@ -494,29 +497,20 @@ inactive_states = [BidStates.SWAP_COMPLETED, BidStates.BID_ERROR, BidStates.BID_
def isActiveBidState(state): def isActiveBidState(state):
if state >= BidStates.BID_ACCEPTED and state < BidStates.SWAP_COMPLETED: if state >= BidStates.BID_ACCEPTED and state < BidStates.SWAP_COMPLETED:
return True return True
if state == BidStates.SWAP_DELAYING: return state in (
return True BidStates.SWAP_DELAYING,
if state == BidStates.XMR_SWAP_HAVE_SCRIPT_COIN_SPEND_TX: BidStates.XMR_SWAP_HAVE_SCRIPT_COIN_SPEND_TX,
return True BidStates.XMR_SWAP_SCRIPT_COIN_LOCKED,
if state == BidStates.XMR_SWAP_SCRIPT_COIN_LOCKED: BidStates.XMR_SWAP_NOSCRIPT_COIN_LOCKED,
return True BidStates.XMR_SWAP_LOCK_RELEASED,
if state == BidStates.XMR_SWAP_NOSCRIPT_COIN_LOCKED: BidStates.XMR_SWAP_NOSCRIPT_TX_REDEEMED,
return True BidStates.XMR_SWAP_SCRIPT_TX_REDEEMED,
if state == BidStates.XMR_SWAP_LOCK_RELEASED: BidStates.XMR_SWAP_SCRIPT_TX_PREREFUND,
return True BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS,
if state == BidStates.XMR_SWAP_NOSCRIPT_TX_REDEEMED: BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX,
return True BidStates.XMR_SWAP_FAILED,
if state == BidStates.XMR_SWAP_SCRIPT_TX_REDEEMED: BidStates.BID_REQUEST_ACCEPTED,
return True )
if state == BidStates.XMR_SWAP_SCRIPT_TX_PREREFUND:
return True
if state == BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS:
return True
if state == BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX:
return True
if state == BidStates.XMR_SWAP_FAILED:
return True
return False
def isErrorBidState(state): def isErrorBidState(state):

@ -81,7 +81,7 @@ def upgradeDatabaseData(self, data_version):
swap_ended = isFinalBidState(state) swap_ended = isFinalBidState(state)
session.execute('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)}) session.execute('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)})
if data_version > 0 and data_version < 4: if data_version > 0 and data_version < 4:
for state in (BidStates.BID_REQUEST_SENT, ): for state in (BidStates.BID_REQUEST_SENT, BidStates.BID_REQUEST_ACCEPTED):
session.add(BidState( session.add(BidState(
active_ind=1, active_ind=1,
state_id=int(state), state_id=int(state),

@ -478,7 +478,7 @@ def js_generatenotification(self, url_split, post_string, is_json) -> bytes:
elif r == 2: elif r == 2:
swap_client.notify(NT.BID_ACCEPTED, {'bid_id': random.randbytes(28).hex()}) swap_client.notify(NT.BID_ACCEPTED, {'bid_id': random.randbytes(28).hex()})
elif r == 3: elif r == 3:
swap_client.notify(NT.BID_RECEIVED, {'type': 'xmr', 'bid_id': random.randbytes(28).hex(), 'offer_id': random.randbytes(28).hex()}) swap_client.notify(NT.BID_RECEIVED, {'type': 'ads', 'bid_id': random.randbytes(28).hex(), 'offer_id': random.randbytes(28).hex()})
return bytes(json.dumps({'type': r}), 'UTF-8') return bytes(json.dumps({'type': r}), 'UTF-8')

Loading…
Cancel
Save