Skip to content

Commit

Permalink
Fix transaction_type field
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 4, 2025
1 parent 39315ef commit 8e61090
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
23 changes: 16 additions & 7 deletions counterparty-core/counterpartycore/lib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,22 +429,24 @@ def parse_block(
return None, None, None


def update_transaction_type(db):
def update_transaction_type(db, fix_utxo_move=False):
start = time.time()
logger.info("Updating `transaction_type` column in `transactions` table...")

with db:
cursor = db.cursor()
cursor.execute("ALTER TABLE transactions ADD COLUMN transaction_type TEXT")
cursor.execute(
"SELECT tx_index, destination, block_index, data, supported FROM transactions"
)
if not fix_utxo_move:
cursor.execute("ALTER TABLE transactions ADD COLUMN transaction_type TEXT")
sql = "SELECT tx_index, destination, block_index, data, utxos_info, supported FROM transactions"
if fix_utxo_move:
sql += " WHERE transaction_type = 'utxomove'"
cursor.execute(sql)
counter = 0
for tx in cursor.fetchall():
transaction_type = "unknown"
if tx["supported"]:
transaction_type = message_type.get_transaction_type(
tx["data"], tx["destination"], tx["block_index"]
tx["data"], tx["destination"], tx["utxos_info"].split(" "), tx["block_index"]
)

cursor.execute(
Expand Down Expand Up @@ -585,6 +587,11 @@ def initialise(db):
if "transaction_type" not in transactions_columns:
update_transaction_type(db)

if database.get_config_value(db, "FIX_TRANSACTION_TYPE_1") is None:
with db:
update_transaction_type(db, fix_utxo_move=True)
database.set_config_value(db, "FIX_TRANSACTION_TYPE_1", True)

database.create_indexes(
cursor,
"transactions",
Expand Down Expand Up @@ -1055,7 +1062,9 @@ def list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index, decoded_
"fee": fee,
"data": data,
"utxos_info": " ".join(utxos_info),
"transaction_type": message_type.get_transaction_type(data, destination, block_index),
"transaction_type": message_type.get_transaction_type(
data, destination, utxos_info, block_index
),
}
ledger.insert_record(db, "transactions", transaction_bindings, "NEW_TRANSACTION")

Expand Down
4 changes: 3 additions & 1 deletion counterparty-core/counterpartycore/lib/gettxinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,9 @@ def get_utxos_info(db, decoded_tx):

def update_utxo_balances_cache(db, utxos_info, data, destination, block_index):
if util.enabled("utxo_support", block_index=block_index) and not util.PARSING_MEMPOOL:
transaction_type = message_type.get_transaction_type(data, destination, block_index)
transaction_type = message_type.get_transaction_type(
data, destination, utxos_info, block_index
)
if utxos_info[0] != "":
# always remove from cache inputs with balance
ledger.UTXOBalancesCache(db).remove_balance(utxos_info[0])
Expand Down
10 changes: 7 additions & 3 deletions counterparty-core/counterpartycore/lib/message_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def unpack(packed_data, block_index=None):
return (message_type_id, message_remainder)


def get_transaction_type(data: bytes, destination: str, block_index: int):
def get_transaction_type(data: bytes, destination: str, utxos_info: list, block_index: int):

Check warning

Code scanning / pylint

Too many return statements (10/6). Warning

Too many return statements (10/6).
TRANSACTION_TYPE_BY_ID = {
messages.bet.ID: "bet",
messages.broadcast.ID: "broadcast",
Expand Down Expand Up @@ -75,9 +75,13 @@ def get_transaction_type(data: bytes, destination: str, block_index: int):
if not data:
if destination == config.UNSPENDABLE and block_index <= config.BURN_END:
return "burn"
if block_index >= util.get_change_block_index("utxo_support"):
if block_index >= util.get_change_block_index("utxo_support") and utxos_info[0] != "":
return "utxomove"
if block_index >= util.get_change_block_index("dispensers"):
if (
destination != config.UNSPENDABLE

Check warning

Code scanning / pylint

Simplify chained comparison between the operands. Warning

Simplify chained comparison between the operands.
and block_index >= util.get_change_block_index("dispensers")
and block_index < util.get_change_block_index("disable_vanilla_btc_dispense")
):
return "dispense"
return "unknown"

Expand Down

0 comments on commit 8e61090

Please sign in to comment.