Skip to content

Commit

Permalink
Add send_type field in sends table
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 22, 2024
1 parent d2bd26e commit dd513df
Show file tree
Hide file tree
Showing 43 changed files with 12,118 additions and 11,988 deletions.
4,760 changes: 2,363 additions & 2,397 deletions apiary.apib

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,15 +1181,7 @@ def prepare_sends_where(send_type: SendType, other_conditions=None):
where = [other_conditions] if other_conditions else []
break
if type_send in typing.get_args(SendType):
where_send = {}
if type_send == "send":
where_send = {"source__notlike": "%:%", "destination__notlike": "%:%"}
elif type_send == "move":
where_send = {"source__like": "%:%", "destination__like": "%:%"}
elif type_send == "attach":
where_send = {"source__notlike": "%:%", "destination__like": "%:%"}
elif type_send == "detach":
where_send = {"source__like": "%:%", "destination__notlike": "%:%"}
where_send = {"send_type": type_send}
if other_conditions:
where_send.update(other_conditions)
if where_send:
Expand Down
2 changes: 2 additions & 0 deletions counterparty-core/counterpartycore/lib/messages/attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def parse(db, tx, message):
"msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]),
"block_index": tx["block_index"],
"status": status,
"send_type": "attach",
}
ledger.insert_record(db, "sends", bindings, "ATTACH_TO_UTXO")
# return here to avoid further processing
Expand Down Expand Up @@ -223,6 +224,7 @@ def parse(db, tx, message):
"asset": asset,
"quantity": quantity,
"fee_paid": fee,
"send_type": "attach",
}
ledger.insert_record(db, "sends", bindings, "ATTACH_TO_UTXO")

Expand Down
2 changes: 2 additions & 0 deletions counterparty-core/counterpartycore/lib/messages/detach.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def detach_assets(db, tx, source, destination=None):
"msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]),
"block_index": tx["block_index"],
"status": status,
"send_type": "detach",
}
ledger.insert_record(db, "sends", bindings, "DETACH_FROM_UTXO")
# stop here to avoid further processing
Expand Down Expand Up @@ -125,6 +126,7 @@ def detach_assets(db, tx, source, destination=None):
"asset": balance["asset"],
"quantity": balance["quantity"],
"fee_paid": 0,
"send_type": "detach",
}
ledger.insert_record(db, "sends", bindings, "DETACH_FROM_UTXO")

Expand Down
1 change: 1 addition & 0 deletions counterparty-core/counterpartycore/lib/messages/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def move_balances(db, tx, source, destination):
"asset": balance["asset"],
"quantity": balance["quantity"],
"msg_index": msg_index,
"send_type": "move",
}

ledger.insert_record(db, "sends", bindings, "UTXO_MOVE")
Expand Down
71 changes: 68 additions & 3 deletions counterparty-core/counterpartycore/lib/messages/send.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#! /usr/bin/python3
import logging
import time

from counterpartycore.lib import config, database, exceptions, util
from counterpartycore.lib.messages.versions import enhanced_send, mpma, send1

ID = send1.ID

logger = logging.getLogger(config.LOGGER_NAME)


def initialise(db):
cursor = db.cursor()
Expand Down Expand Up @@ -93,15 +96,77 @@ def initialise(db):
if "fee_paid" not in columns:
cursor.execute("""ALTER TABLE sends ADD COLUMN fee_paid INTEGER DEFAULT 0""")

if "send_type" not in columns:
logger.info("Adding `send_type` column to sends table")
start_time = time.time()
database.unlock_update(db, "sends")
cursor.execute("""ALTER TABLE sends ADD COLUMN send_type TEXT""")
utxo_support_start = util.get_change_block_index("utxo_support")
cursor.execute(
"""
UPDATE sends SET send_type = ? WHERE block_index < ?
""",
(
"send",
utxo_support_start,
),
)
cursor.execute(
"""
UPDATE sends SET send_type = ? WHERE block_index >= ?
AND source NOT LIKE '%:%' AND destination NOT LIKE '%:%'
""",
(
"send",
utxo_support_start,
),
)
cursor.execute(
"""
UPDATE sends SET send_type = ? WHERE block_index >= ?
AND source NOT LIKE '%:%' AND destination LIKE '%:%'
""",
(
"attach",
utxo_support_start,
),
)
cursor.execute(
"""
UPDATE sends SET send_type = ? WHERE block_index >= ?
AND source LIKE '%:%' AND destination NOT LIKE '%:%'
""",
(
"detach",
utxo_support_start,
),
)
cursor.execute(
"""
UPDATE sends SET send_type = ? WHERE block_index >= ?
AND source LIKE '%:%' AND destination LIKE '%:%'
""",
(
"move",
utxo_support_start,
),
)
database.lock_update(db, "sends")
logger.info(
f"Added `send_type` column to sends table in {time.time() - start_time:.2f} seconds"
)

database.create_indexes(
cursor,
"sends",
[
["block_index"],
["block_index", "send_type"],
["source"],
["destination"],
["asset"],
["asset", "send_type"],
["memo"],
["status"],
["send_type"],
],
)

Expand Down
3 changes: 3 additions & 0 deletions counterparty-core/counterpartycore/lib/messages/utxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ def parse(db, tx, message):
problems.append("source does not match the UTXO source")
action = "detach from utxo"
event = "DETACH_FROM_UTXO"
send_type = "detach"
# attach if source is an address
else:
if source != tx["source"]:
problems.append("source does not match the source address")
action = "attach to utxo"
event = "ATTACH_TO_UTXO"
send_type = "attach"

status = "valid"
if problems:
Expand Down Expand Up @@ -243,6 +245,7 @@ def parse(db, tx, message):
"asset": asset,
"quantity": quantity,
"fee_paid": fee,
"send_type": send_type,
}
# update counter
if action == "attach to utxo":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def parse(db, tx, message):
"status": status,
"memo": memo_bytes,
"msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]),
"send_type": "send",
}
if "integer overflow" not in status and "quantity must be in satoshis" not in status:
ledger.insert_record(db, "sends", bindings, "ENHANCED_SEND")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def parse(db, tx, message):
"status": status,
"memo": memo_bytes,
"msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]),
"send_type": "send",
}

ledger.insert_record(db, "sends", bindings, "MPMA_SEND")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def parse(db, tx, message):
"quantity": quantity,
"status": status,
"msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]),
"send_type": "send",
}
if "integer overflow" not in status and "quantity must be in satoshis" not in status:
ledger.insert_record(db, "sends", bindings, "SEND")
Expand Down
Loading

0 comments on commit dd513df

Please sign in to comment.