Replace sqlalchemy with manbearpigSQL

This commit is contained in:
tecnovert
2024-11-19 21:45:19 +02:00
parent 0bd626d659
commit 757f8f2762
15 changed files with 2182 additions and 2155 deletions

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2019-2024 tecnovert
# Copyright (c) 2024 The Basicswap developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@@ -18,6 +19,9 @@ import subprocess
from sockshandler import SocksiPyHandler
from .db import (
DBMethods,
)
from .rpc import (
callrpc,
)
@@ -34,7 +38,7 @@ def getaddrinfo_tor(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (args[0], args[1]))]
class BaseApp:
class BaseApp(DBMethods):
def __init__(self, fp, data_dir, settings, chain, log_name="BasicSwap"):
self.log_name = log_name
self.fp = fp

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,29 +1,28 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2022-2024 tecnovert
# Copyright (c) 2024 The Basicswap developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import json
import time
from sqlalchemy.sql import text
from sqlalchemy.orm import scoped_session
from .db import (
AutomationStrategy,
BidState,
Concepts,
AutomationStrategy,
CURRENT_DB_VERSION,
CURRENT_DB_DATA_VERSION,
CURRENT_DB_VERSION,
)
from .basicswap_util import (
BidStates,
strBidState,
isActiveBidState,
isErrorBidState,
isFailingBidState,
isFinalBidState,
strBidState,
)
@@ -36,110 +35,110 @@ def upgradeDatabaseData(self, data_version):
data_version,
CURRENT_DB_DATA_VERSION,
)
with self.mxDB:
try:
session = scoped_session(self.session_factory)
cursor = self.openDB()
try:
now = int(time.time())
now = int(time.time())
if data_version < 1:
session.add(
AutomationStrategy(
active_ind=1,
label="Accept All",
type_ind=Concepts.OFFER,
data=json.dumps(
{"exact_rate_only": True, "max_concurrent_bids": 5}
).encode("utf-8"),
only_known_identities=False,
created_at=now,
)
)
session.add(
AutomationStrategy(
active_ind=1,
label="Accept Known",
type_ind=Concepts.OFFER,
data=json.dumps(
{"exact_rate_only": True, "max_concurrent_bids": 5}
).encode("utf-8"),
only_known_identities=True,
note="Accept bids from identities with previously successful swaps only",
created_at=now,
)
)
for state in BidStates:
session.add(
BidState(
active_ind=1,
state_id=int(state),
in_progress=isActiveBidState(state),
in_error=isErrorBidState(state),
swap_failed=isFailingBidState(state),
swap_ended=isFinalBidState(state),
label=strBidState(state),
created_at=now,
)
)
if data_version > 0 and data_version < 2:
for state in (
BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS,
BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX,
):
session.add(
BidState(
active_ind=1,
state_id=int(state),
in_progress=isActiveBidState(state),
label=strBidState(state),
created_at=now,
)
)
if data_version > 0 and data_version < 3:
for state in BidStates:
in_error = isErrorBidState(state)
swap_failed = isFailingBidState(state)
swap_ended = isFinalBidState(state)
session.execute(
text(
"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:
for state in (
BidStates.BID_REQUEST_SENT,
BidStates.BID_REQUEST_ACCEPTED,
):
session.add(
BidState(
active_ind=1,
state_id=int(state),
in_progress=isActiveBidState(state),
in_error=isErrorBidState(state),
swap_failed=isFailingBidState(state),
swap_ended=isFinalBidState(state),
label=strBidState(state),
created_at=now,
)
)
self.db_data_version = CURRENT_DB_DATA_VERSION
self.setIntKV("db_data_version", self.db_data_version, session)
session.commit()
self.log.info(
"Upgraded database records to version {}".format(self.db_data_version)
if data_version < 1:
self.add(
AutomationStrategy(
active_ind=1,
label="Accept All",
type_ind=Concepts.OFFER,
data=json.dumps(
{"exact_rate_only": True, "max_concurrent_bids": 5}
).encode("utf-8"),
only_known_identities=False,
created_at=now,
),
cursor,
)
finally:
session.close()
session.remove()
self.add(
AutomationStrategy(
active_ind=1,
label="Accept Known",
type_ind=Concepts.OFFER,
data=json.dumps(
{"exact_rate_only": True, "max_concurrent_bids": 5}
).encode("utf-8"),
only_known_identities=True,
note="Accept bids from identities with previously successful swaps only",
created_at=now,
),
cursor,
)
for state in BidStates:
self.add(
BidState(
active_ind=1,
state_id=int(state),
in_progress=isActiveBidState(state),
in_error=isErrorBidState(state),
swap_failed=isFailingBidState(state),
swap_ended=isFinalBidState(state),
label=strBidState(state),
created_at=now,
),
cursor,
)
if data_version > 0 and data_version < 2:
for state in (
BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS,
BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX,
):
self.add(
BidState(
active_ind=1,
state_id=int(state),
in_progress=isActiveBidState(state),
label=strBidState(state),
created_at=now,
),
cursor,
)
if data_version > 0 and data_version < 3:
for state in BidStates:
in_error = isErrorBidState(state)
swap_failed = isFailingBidState(state)
swap_ended = isFinalBidState(state)
cursor.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:
for state in (
BidStates.BID_REQUEST_SENT,
BidStates.BID_REQUEST_ACCEPTED,
):
self.add(
BidState(
active_ind=1,
state_id=int(state),
in_progress=isActiveBidState(state),
in_error=isErrorBidState(state),
swap_failed=isFailingBidState(state),
swap_ended=isFinalBidState(state),
label=strBidState(state),
created_at=now,
),
cursor,
)
self.db_data_version = CURRENT_DB_DATA_VERSION
self.setIntKV("db_data_version", self.db_data_version, cursor)
self.commitDB()
self.log.info(
"Upgraded database records to version {}".format(self.db_data_version)
)
finally:
self.closeDB(cursor, commit=False)
def upgradeDatabase(self, db_version):
@@ -147,312 +146,276 @@ def upgradeDatabase(self, db_version):
return
self.log.info(
"Upgrading database from version %d to %d.", db_version, CURRENT_DB_VERSION
f"Upgrading database from version {db_version} to {CURRENT_DB_VERSION}."
)
while True:
session = scoped_session(self.session_factory)
try:
cursor = self.openDB()
current_version = db_version
if current_version == 6:
session.execute(text("ALTER TABLE bids ADD COLUMN security_token BLOB"))
session.execute(text("ALTER TABLE offers ADD COLUMN security_token BLOB"))
db_version += 1
elif current_version == 7:
session.execute(text("ALTER TABLE transactions ADD COLUMN block_hash BLOB"))
session.execute(
text("ALTER TABLE transactions ADD COLUMN block_height INTEGER")
)
session.execute(
text("ALTER TABLE transactions ADD COLUMN block_time INTEGER")
)
db_version += 1
elif current_version == 8:
session.execute(
text(
"""
CREATE TABLE wallets (
record_id INTEGER NOT NULL,
coin_id INTEGER,
wallet_name VARCHAR,
wallet_data VARCHAR,
balance_type INTEGER,
created_at BIGINT,
PRIMARY KEY (record_id))"""
current_version = db_version
if current_version == 6:
cursor.execute("ALTER TABLE bids ADD COLUMN security_token BLOB")
cursor.execute("ALTER TABLE offers ADD COLUMN security_token BLOB")
db_version += 1
elif current_version == 7:
cursor.execute("ALTER TABLE transactions ADD COLUMN block_hash BLOB")
cursor.execute(
"ALTER TABLE transactions ADD COLUMN block_height INTEGER"
)
)
db_version += 1
elif current_version == 9:
session.execute(text("ALTER TABLE wallets ADD COLUMN wallet_data VARCHAR"))
db_version += 1
elif current_version == 10:
session.execute(
text("ALTER TABLE smsgaddresses ADD COLUMN active_ind INTEGER")
)
session.execute(
text("ALTER TABLE smsgaddresses ADD COLUMN created_at INTEGER")
)
session.execute(text("ALTER TABLE smsgaddresses ADD COLUMN note VARCHAR"))
session.execute(text("ALTER TABLE smsgaddresses ADD COLUMN pubkey VARCHAR"))
session.execute(
text("UPDATE smsgaddresses SET active_ind = 1, created_at = 1")
)
session.execute(text("ALTER TABLE offers ADD COLUMN addr_to VARCHAR"))
session.execute(text(f'UPDATE offers SET addr_to = "{self.network_addr}"'))
db_version += 1
elif current_version == 11:
session.execute(
text("ALTER TABLE bids ADD COLUMN chain_a_height_start INTEGER")
)
session.execute(
text("ALTER TABLE bids ADD COLUMN chain_b_height_start INTEGER")
)
session.execute(
text("ALTER TABLE bids ADD COLUMN protocol_version INTEGER")
)
session.execute(
text("ALTER TABLE offers ADD COLUMN protocol_version INTEGER")
)
session.execute(text("ALTER TABLE transactions ADD COLUMN tx_data BLOB"))
db_version += 1
elif current_version == 12:
session.execute(
text(
cursor.execute("ALTER TABLE transactions ADD COLUMN block_time INTEGER")
db_version += 1
elif current_version == 8:
cursor.execute(
"""
CREATE TABLE knownidentities (
record_id INTEGER NOT NULL,
address VARCHAR,
label VARCHAR,
publickey BLOB,
num_sent_bids_successful INTEGER,
num_recv_bids_successful INTEGER,
num_sent_bids_rejected INTEGER,
num_recv_bids_rejected INTEGER,
num_sent_bids_failed INTEGER,
num_recv_bids_failed INTEGER,
note VARCHAR,
updated_at BIGINT,
created_at BIGINT,
PRIMARY KEY (record_id))"""
CREATE TABLE wallets (
record_id INTEGER NOT NULL,
coin_id INTEGER,
wallet_name VARCHAR,
wallet_data VARCHAR,
balance_type INTEGER,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
)
session.execute(text("ALTER TABLE bids ADD COLUMN reject_code INTEGER"))
session.execute(text("ALTER TABLE bids ADD COLUMN rate INTEGER"))
session.execute(
text("ALTER TABLE offers ADD COLUMN amount_negotiable INTEGER")
)
session.execute(
text("ALTER TABLE offers ADD COLUMN rate_negotiable INTEGER")
)
db_version += 1
elif current_version == 13:
db_version += 1
session.execute(
text(
db_version += 1
elif current_version == 9:
cursor.execute("ALTER TABLE wallets ADD COLUMN wallet_data VARCHAR")
db_version += 1
elif current_version == 10:
cursor.execute(
"ALTER TABLE smsgaddresses ADD COLUMN active_ind INTEGER"
)
cursor.execute(
"ALTER TABLE smsgaddresses ADD COLUMN created_at INTEGER"
)
cursor.execute("ALTER TABLE smsgaddresses ADD COLUMN note VARCHAR")
cursor.execute("ALTER TABLE smsgaddresses ADD COLUMN pubkey VARCHAR")
cursor.execute(
"UPDATE smsgaddresses SET active_ind = 1, created_at = 1"
)
cursor.execute("ALTER TABLE offers ADD COLUMN addr_to VARCHAR")
cursor.execute(f'UPDATE offers SET addr_to = "{self.network_addr}"')
db_version += 1
elif current_version == 11:
cursor.execute(
"ALTER TABLE bids ADD COLUMN chain_a_height_start INTEGER"
)
cursor.execute(
"ALTER TABLE bids ADD COLUMN chain_b_height_start INTEGER"
)
cursor.execute("ALTER TABLE bids ADD COLUMN protocol_version INTEGER")
cursor.execute("ALTER TABLE offers ADD COLUMN protocol_version INTEGER")
cursor.execute("ALTER TABLE transactions ADD COLUMN tx_data BLOB")
db_version += 1
elif current_version == 12:
cursor.execute(
"""
CREATE TABLE automationstrategies (
record_id INTEGER NOT NULL,
active_ind INTEGER,
label VARCHAR,
type_ind INTEGER,
only_known_identities INTEGER,
num_concurrent INTEGER,
data BLOB,
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
CREATE TABLE knownidentities (
record_id INTEGER NOT NULL,
address VARCHAR,
label VARCHAR,
publickey BLOB,
num_sent_bids_successful INTEGER,
num_recv_bids_successful INTEGER,
num_sent_bids_rejected INTEGER,
num_recv_bids_rejected INTEGER,
num_sent_bids_failed INTEGER,
num_recv_bids_failed INTEGER,
note VARCHAR,
updated_at BIGINT,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
)
session.execute(
text(
cursor.execute("ALTER TABLE bids ADD COLUMN reject_code INTEGER")
cursor.execute("ALTER TABLE bids ADD COLUMN rate INTEGER")
cursor.execute(
"ALTER TABLE offers ADD COLUMN amount_negotiable INTEGER"
)
cursor.execute("ALTER TABLE offers ADD COLUMN rate_negotiable INTEGER")
db_version += 1
elif current_version == 13:
db_version += 1
cursor.execute(
"""
CREATE TABLE automationlinks (
record_id INTEGER NOT NULL,
active_ind INTEGER,
CREATE TABLE automationstrategies (
record_id INTEGER NOT NULL,
active_ind INTEGER,
label VARCHAR,
type_ind INTEGER,
only_known_identities INTEGER,
num_concurrent INTEGER,
data BLOB,
linked_type INTEGER,
linked_id BLOB,
strategy_id INTEGER,
data BLOB,
repeat_limit INTEGER,
repeat_count INTEGER,
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
)
session.execute(
text(
cursor.execute(
"""
CREATE TABLE history (
record_id INTEGER NOT NULL,
concept_type INTEGER,
concept_id INTEGER,
changed_data BLOB,
CREATE TABLE automationlinks (
record_id INTEGER NOT NULL,
active_ind INTEGER,
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
linked_type INTEGER,
linked_id BLOB,
strategy_id INTEGER,
data BLOB,
repeat_limit INTEGER,
repeat_count INTEGER,
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
)
session.execute(
text(
cursor.execute(
"""
CREATE TABLE bidstates (
record_id INTEGER NOT NULL,
active_ind INTEGER,
state_id INTEGER,
label VARCHAR,
in_progress INTEGER,
CREATE TABLE history (
record_id INTEGER NOT NULL,
concept_type INTEGER,
concept_id INTEGER,
changed_data BLOB,
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
)
session.execute(text("ALTER TABLE wallets ADD COLUMN active_ind INTEGER"))
session.execute(
text("ALTER TABLE knownidentities ADD COLUMN active_ind INTEGER")
)
session.execute(text("ALTER TABLE eventqueue RENAME TO actions"))
session.execute(
text("ALTER TABLE actions RENAME COLUMN event_id TO action_id")
)
session.execute(
text("ALTER TABLE actions RENAME COLUMN event_type TO action_type")
)
session.execute(
text("ALTER TABLE actions RENAME COLUMN event_data TO action_data")
)
elif current_version == 14:
db_version += 1
session.execute(
text("ALTER TABLE xmr_swaps ADD COLUMN coin_a_lock_release_msg_id BLOB")
)
session.execute(
text(
cursor.execute(
"""
CREATE TABLE bidstates (
record_id INTEGER NOT NULL,
active_ind INTEGER,
state_id INTEGER,
label VARCHAR,
in_progress INTEGER,
note VARCHAR,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
cursor.execute("ALTER TABLE wallets ADD COLUMN active_ind INTEGER")
cursor.execute(
"ALTER TABLE knownidentities ADD COLUMN active_ind INTEGER"
)
cursor.execute("ALTER TABLE eventqueue RENAME TO actions")
cursor.execute(
"ALTER TABLE actions RENAME COLUMN event_id TO action_id"
)
cursor.execute(
"ALTER TABLE actions RENAME COLUMN event_type TO action_type"
)
cursor.execute(
"ALTER TABLE actions RENAME COLUMN event_data TO action_data"
)
elif current_version == 14:
db_version += 1
cursor.execute(
"ALTER TABLE xmr_swaps ADD COLUMN coin_a_lock_release_msg_id BLOB"
)
cursor.execute(
"ALTER TABLE xmr_swaps RENAME COLUMN coin_a_lock_refund_spend_tx_msg_id TO coin_a_lock_spend_tx_msg_id"
)
)
elif current_version == 15:
db_version += 1
session.execute(
text(
elif current_version == 15:
db_version += 1
cursor.execute(
"""
CREATE TABLE notifications (
record_id INTEGER NOT NULL,
active_ind INTEGER,
event_type INTEGER,
event_data BLOB,
created_at BIGINT,
PRIMARY KEY (record_id))"""
CREATE TABLE notifications (
record_id INTEGER NOT NULL,
active_ind INTEGER,
event_type INTEGER,
event_data BLOB,
created_at BIGINT,
PRIMARY KEY (record_id))"""
)
)
elif current_version == 16:
db_version += 1
session.execute(
text(
elif current_version == 16:
db_version += 1
cursor.execute(
"""
CREATE TABLE prefunded_transactions (
record_id INTEGER NOT NULL,
active_ind INTEGER,
created_at BIGINT,
linked_type INTEGER,
linked_id BLOB,
tx_type INTEGER,
tx_data BLOB,
used_by BLOB,
PRIMARY KEY (record_id))"""
CREATE TABLE prefunded_transactions (
record_id INTEGER NOT NULL,
active_ind INTEGER,
created_at BIGINT,
linked_type INTEGER,
linked_id BLOB,
tx_type INTEGER,
tx_data BLOB,
used_by BLOB,
PRIMARY KEY (record_id))"""
)
)
elif current_version == 17:
db_version += 1
session.execute(
text(
elif current_version == 17:
db_version += 1
cursor.execute(
"ALTER TABLE knownidentities ADD COLUMN automation_override INTEGER"
)
)
session.execute(
text(
cursor.execute(
"ALTER TABLE knownidentities ADD COLUMN visibility_override INTEGER"
)
)
session.execute(text("ALTER TABLE knownidentities ADD COLUMN data BLOB"))
session.execute(text("UPDATE knownidentities SET active_ind = 1"))
elif current_version == 18:
db_version += 1
session.execute(
text("ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING")
)
session.execute(
text("ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING")
)
elif current_version == 19:
db_version += 1
session.execute(text("ALTER TABLE bidstates ADD COLUMN in_error INTEGER"))
session.execute(
text("ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER")
)
session.execute(text("ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER"))
elif current_version == 20:
db_version += 1
session.execute(
text(
cursor.execute("ALTER TABLE knownidentities ADD COLUMN data BLOB")
cursor.execute("UPDATE knownidentities SET active_ind = 1")
elif current_version == 18:
db_version += 1
cursor.execute("ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING")
cursor.execute("ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING")
elif current_version == 19:
db_version += 1
cursor.execute("ALTER TABLE bidstates ADD COLUMN in_error INTEGER")
cursor.execute("ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER")
cursor.execute("ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER")
elif current_version == 20:
db_version += 1
cursor.execute(
"""
CREATE TABLE message_links (
record_id INTEGER NOT NULL,
active_ind INTEGER,
created_at BIGINT,
CREATE TABLE message_links (
record_id INTEGER NOT NULL,
active_ind INTEGER,
created_at BIGINT,
linked_type INTEGER,
linked_id BLOB,
linked_type INTEGER,
linked_id BLOB,
msg_type INTEGER,
msg_sequence INTEGER,
msg_id BLOB,
PRIMARY KEY (record_id))"""
msg_type INTEGER,
msg_sequence INTEGER,
msg_id BLOB,
PRIMARY KEY (record_id))"""
)
)
session.execute(text("ALTER TABLE offers ADD COLUMN bid_reversed INTEGER"))
elif current_version == 21:
db_version += 1
session.execute(text("ALTER TABLE offers ADD COLUMN proof_utxos BLOB"))
session.execute(text("ALTER TABLE bids ADD COLUMN proof_utxos BLOB"))
elif current_version == 22:
db_version += 1
session.execute(text("ALTER TABLE offers ADD COLUMN amount_to INTEGER"))
elif current_version == 23:
db_version += 1
session.execute(
text(
cursor.execute("ALTER TABLE offers ADD COLUMN bid_reversed INTEGER")
elif current_version == 21:
db_version += 1
cursor.execute("ALTER TABLE offers ADD COLUMN proof_utxos BLOB")
cursor.execute("ALTER TABLE bids ADD COLUMN proof_utxos BLOB")
elif current_version == 22:
db_version += 1
cursor.execute("ALTER TABLE offers ADD COLUMN amount_to INTEGER")
elif current_version == 23:
db_version += 1
cursor.execute(
"""
CREATE TABLE checkedblocks (
record_id INTEGER NOT NULL,
created_at BIGINT,
coin_type INTEGER,
block_height INTEGER,
block_hash BLOB,
block_time INTEGER,
PRIMARY KEY (record_id))"""
CREATE TABLE checkedblocks (
record_id INTEGER NOT NULL,
created_at BIGINT,
coin_type INTEGER,
block_height INTEGER,
block_hash BLOB,
block_time INTEGER,
PRIMARY KEY (record_id))"""
)
)
session.execute(text("ALTER TABLE bids ADD COLUMN pkhash_buyer_to BLOB"))
if current_version != db_version:
self.db_version = db_version
self.setIntKV("db_version", db_version, session)
session.commit()
session.close()
session.remove()
self.log.info("Upgraded database to version {}".format(self.db_version))
continue
cursor.execute("ALTER TABLE bids ADD COLUMN pkhash_buyer_to BLOB")
if current_version != db_version:
self.db_version = db_version
self.setIntKV("db_version", db_version, cursor)
cursor = self.commitDB()
self.log.info("Upgraded database to version {}".format(self.db_version))
continue
except Exception as e:
self.log.error("Upgrade failed {}".format(e))
self.rollbackDB()
finally:
self.closeDB(cursor, commit=False)
break
if db_version != CURRENT_DB_VERSION:

View File

@@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2023-2024 The BSX Developers
# Copyright (c) 2023-2024 The Basicswap Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
from sqlalchemy.sql import text
from .db import (
Concepts,
)
@@ -13,7 +12,7 @@ from .db import (
def remove_expired_data(self, time_offset: int = 0):
now: int = self.getTime()
try:
session = self.openSession()
cursor = self.openDB()
active_bids_insert = self.activeBidsQueryStr(now, "", "b2")
query_str = f"""
@@ -22,118 +21,94 @@ def remove_expired_data(self, time_offset: int = 0):
"""
num_offers = 0
num_bids = 0
offer_rows = session.execute(text(query_str), {"expired_at": now - time_offset})
offer_rows = cursor.execute(query_str, {"expired_at": now - time_offset})
for offer_row in offer_rows:
num_offers += 1
bid_rows = session.execute(
text("SELECT bids.bid_id FROM bids WHERE bids.offer_id = :offer_id"),
bid_rows = cursor.execute(
"SELECT bids.bid_id FROM bids WHERE bids.offer_id = :offer_id",
{"offer_id": offer_row[0]},
)
for bid_row in bid_rows:
num_bids += 1
session.execute(
text(
"DELETE FROM transactions WHERE transactions.bid_id = :bid_id"
),
cursor.execute(
"DELETE FROM transactions WHERE transactions.bid_id = :bid_id",
{"bid_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :bid_id"
),
cursor.execute(
"DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :bid_id",
{"type_ind": int(Concepts.BID), "bid_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :bid_id"
),
cursor.execute(
"DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :bid_id",
{"type_ind": int(Concepts.BID), "bid_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :bid_id"
),
cursor.execute(
"DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :bid_id",
{"type_ind": int(Concepts.BID), "bid_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :bid_id"
),
cursor.execute(
"DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :bid_id",
{"type_ind": int(Concepts.BID), "bid_id": bid_row[0]},
)
session.execute(
text("DELETE FROM xmr_swaps WHERE xmr_swaps.bid_id = :bid_id"),
cursor.execute(
"DELETE FROM xmr_swaps WHERE xmr_swaps.bid_id = :bid_id",
{"bid_id": bid_row[0]},
)
session.execute(
text("DELETE FROM actions WHERE actions.linked_id = :bid_id"),
cursor.execute(
"DELETE FROM actions WHERE actions.linked_id = :bid_id",
{"bid_id": bid_row[0]},
)
session.execute(
text("DELETE FROM addresspool WHERE addresspool.bid_id = :bid_id"),
cursor.execute(
"DELETE FROM addresspool WHERE addresspool.bid_id = :bid_id",
{"bid_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM xmr_split_data WHERE xmr_split_data.bid_id = :bid_id"
),
cursor.execute(
"DELETE FROM xmr_split_data WHERE xmr_split_data.bid_id = :bid_id",
{"bid_id": bid_row[0]},
)
session.execute(
text("DELETE FROM bids WHERE bids.bid_id = :bid_id"),
cursor.execute(
"DELETE FROM bids WHERE bids.bid_id = :bid_id",
{"bid_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :linked_id"
),
cursor.execute(
"DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :linked_id",
{"type_ind": int(Concepts.BID), "linked_id": bid_row[0]},
)
session.execute(
text(
"DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :offer_id"
),
cursor.execute(
"DELETE FROM eventlog WHERE eventlog.linked_type = :type_ind AND eventlog.linked_id = :offer_id",
{"type_ind": int(Concepts.OFFER), "offer_id": offer_row[0]},
)
session.execute(
text(
"DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :offer_id"
),
cursor.execute(
"DELETE FROM automationlinks WHERE automationlinks.linked_type = :type_ind AND automationlinks.linked_id = :offer_id",
{"type_ind": int(Concepts.OFFER), "offer_id": offer_row[0]},
)
session.execute(
text(
"DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :offer_id"
),
cursor.execute(
"DELETE FROM prefunded_transactions WHERE prefunded_transactions.linked_type = :type_ind AND prefunded_transactions.linked_id = :offer_id",
{"type_ind": int(Concepts.OFFER), "offer_id": offer_row[0]},
)
session.execute(
text(
"DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :offer_id"
),
cursor.execute(
"DELETE FROM history WHERE history.concept_type = :type_ind AND history.concept_id = :offer_id",
{"type_ind": int(Concepts.OFFER), "offer_id": offer_row[0]},
)
session.execute(
text("DELETE FROM xmr_offers WHERE xmr_offers.offer_id = :offer_id"),
cursor.execute(
"DELETE FROM xmr_offers WHERE xmr_offers.offer_id = :offer_id",
{"offer_id": offer_row[0]},
)
session.execute(
text("DELETE FROM sentoffers WHERE sentoffers.offer_id = :offer_id"),
cursor.execute(
"DELETE FROM sentoffers WHERE sentoffers.offer_id = :offer_id",
{"offer_id": offer_row[0]},
)
session.execute(
text("DELETE FROM actions WHERE actions.linked_id = :offer_id"),
cursor.execute(
"DELETE FROM actions WHERE actions.linked_id = :offer_id",
{"offer_id": offer_row[0]},
)
session.execute(
text("DELETE FROM offers WHERE offers.offer_id = :offer_id"),
cursor.execute(
"DELETE FROM offers WHERE offers.offer_id = :offer_id",
{"offer_id": offer_row[0]},
)
session.execute(
text(
"DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :offer_id"
),
cursor.execute(
"DELETE FROM message_links WHERE linked_type = :type_ind AND linked_id = :offer_id",
{"type_ind": int(Concepts.OFFER), "offer_id": offer_row[0]},
)
@@ -147,10 +122,10 @@ def remove_expired_data(self, time_offset: int = 0):
)
)
session.execute(
text("DELETE FROM checkedblocks WHERE created_at <= :expired_at"),
cursor.execute(
"DELETE FROM checkedblocks WHERE created_at <= :expired_at",
{"expired_at": now - time_offset},
)
finally:
self.closeSession(session)
self.closeDB(cursor)

View File

@@ -612,11 +612,11 @@ class BCHInterface(BTCInterface):
prevout_script: bytes,
prevout_value: int,
) -> bool:
# simple ecdsa signature verification
# Simple ecdsa signature verification
return self.verifyDataSig(tx_bytes, sig, K)
def verifyDataSig(self, data: bytes, sig: bytes, K: bytes) -> bool:
# simple ecdsa signature verification
# Simple ecdsa signature verification
pubkey = PublicKey(K)
return pubkey.verify(sig, sha256(data), hasher=None)
@@ -637,7 +637,7 @@ class BCHInterface(BTCInterface):
return signature, mining_fee, out_1, out_2, public_key, timelock
def extractScriptLockScriptValues(self, script_bytes):
# see BCHInterface.genScriptLockTxScript for reference
# See BCHInterface.genScriptLockTxScript for reference
o = 0
@@ -1071,6 +1071,9 @@ class BCHInterface(BTCInterface):
)
return out_1
def lockNonSegwitPrevouts(self) -> None:
pass
def createMercyTx(
self,
refund_swipe_tx_bytes: bytes,

View File

@@ -1158,7 +1158,8 @@ class BTCInterface(Secp256k1Interface):
def fundTx(self, tx: bytes, feerate) -> bytes:
feerate_str = self.format_amount(feerate)
# TODO: unlock unspents if bid cancelled
# TODO: Unlock unspents if bid cancelled
# TODO: Manually select only segwit prevouts
options = {
"lockUnspents": True,
"feeRate": feerate_str,
@@ -1166,6 +1167,27 @@ class BTCInterface(Secp256k1Interface):
rv = self.rpc_wallet("fundrawtransaction", [tx.hex(), options])
return bytes.fromhex(rv["hex"])
def lockNonSegwitPrevouts(self) -> None:
# For tests
unspent = self.rpc_wallet("listunspent")
to_lock = []
for u in unspent:
if u.get("spendable", False) is False:
continue
if "desc" in u:
desc = u["desc"]
if self.use_p2shp2wsh():
if not desc.startswith("sh(wpkh"):
to_lock.append({"txid": u["txid"], "vout": u["vout"]})
else:
if not desc.startswith("wpkh"):
to_lock.append({"txid": u["txid"], "vout": u["vout"]})
if len(to_lock) > 0:
self._log.debug(f"Locking {len(to_lock)} non segwit prevouts")
self.rpc_wallet("lockunspent", [False, to_lock])
def listInputs(self, tx_bytes: bytes):
tx = self.loadTx(tx_bytes)

View File

@@ -119,12 +119,12 @@ def extractScriptSecretHash(script):
return script[7:39]
def redeemITx(self, bid_id: bytes, session):
bid, offer = self.getBidAndOffer(bid_id, session)
def redeemITx(self, bid_id: bytes, cursor):
bid, offer = self.getBidAndOffer(bid_id, cursor)
ci_from = self.ci(offer.coin_from)
txn = self.createRedeemTxn(
ci_from.coin_type(), bid, for_txn_type="initiate", session=session
ci_from.coin_type(), bid, for_txn_type="initiate", cursor=cursor
)
txid = ci_from.publishTx(bytes.fromhex(txn))
@@ -135,7 +135,7 @@ def redeemITx(self, bid_id: bytes, session):
ci_from.coin_name(),
bid_id.hex(),
)
self.logEvent(Concepts.BID, bid_id, EventLogTypes.ITX_REDEEM_PUBLISHED, "", session)
self.logEvent(Concepts.BID, bid_id, EventLogTypes.ITX_REDEEM_PUBLISHED, "", cursor)
class AtomicSwapInterface(ProtocolInterface):

View File

@@ -42,17 +42,15 @@ def addLockRefundSigs(self, xmr_swap, ci):
xmr_swap.a_lock_refund_tx = signed_tx
def recoverNoScriptTxnWithKey(self, bid_id: bytes, encoded_key, session=None):
def recoverNoScriptTxnWithKey(self, bid_id: bytes, encoded_key, cursor=None):
self.log.info(f"Manually recovering {bid_id.hex()}")
# Manually recover txn if other key is known
try:
use_session = self.openSession(session)
bid, xmr_swap = self.getXmrBidFromSession(use_session, bid_id)
use_cursor = self.openDB(cursor)
bid, xmr_swap = self.getXmrBidFromSession(use_cursor, bid_id)
ensure(bid, "Bid not found: {}.".format(bid_id.hex()))
ensure(xmr_swap, "Adaptor-sig swap not found: {}.".format(bid_id.hex()))
offer, xmr_offer = self.getXmrOfferFromSession(
use_session, bid.offer_id, sent=False
)
offer, xmr_offer = self.getXmrOfferFromSession(use_cursor, bid.offer_id)
ensure(offer, "Offer not found: {}.".format(bid.offer_id.hex()))
ensure(xmr_offer, "Adaptor-sig offer not found: {}.".format(bid.offer_id.hex()))
@@ -97,10 +95,10 @@ def recoverNoScriptTxnWithKey(self, bid_id: bytes, encoded_key, session=None):
raise ValueError(err_msg)
if ci_follower.coin_type() in (Coins.XMR, Coins.WOW):
address_to = self.getCachedMainWalletAddress(ci_follower, use_session)
address_to = self.getCachedMainWalletAddress(ci_follower, use_cursor)
else:
address_to = self.getCachedStealthAddressForCoin(
ci_follower.coin_type(), use_session
ci_follower.coin_type(), use_cursor
)
amount = bid.amount_to
lock_tx_vout = bid.getLockTXBVout()
@@ -125,17 +123,17 @@ def recoverNoScriptTxnWithKey(self, bid_id: bytes, encoded_key, session=None):
bid.bid_id,
EventLogTypes.LOCK_TX_B_SPEND_TX_PUBLISHED,
txid.hex(),
use_session,
use_cursor,
)
use_session.commit()
self.commitDB()
return txid
except Exception as e:
self.log.error(traceback.format_exc())
raise (e)
finally:
if session is None:
self.closeSession(use_session, commit=False)
if cursor is None:
self.closeDB(use_cursor, commit=False)
def getChainBSplitKey(swap_client, bid, xmr_swap, offer):

View File

@@ -898,7 +898,7 @@ def page_offers(self, url_split, post_string, sent=False):
sent = True
else:
sent = False
offers = swap_client.listOffers(sent, filters, with_bid_info=True)
offers = swap_client.listOffers(sent, filters)
now: int = swap_client.getTime()
formatted_offers = []
@@ -906,7 +906,7 @@ def page_offers(self, url_split, post_string, sent=False):
tla_to = ""
for row in offers:
o, completed_amount = row
o = row
ci_from = swap_client.ci(Coins(o.coin_from))
ci_to = swap_client.ci(Coins(o.coin_to))
is_expired = o.expire_at <= now
@@ -932,7 +932,7 @@ def page_offers(self, url_split, post_string, sent=False):
"Public" if o.addr_to == swap_client.network_addr else o.addr_to,
o.addr_from,
o.was_sent,
ci_from.format_amount(completed_amount),
None, # placeholder, was completed_amount
is_expired,
o.active_ind,
formatted_expired_at,