Skip to content

Commit

Permalink
Merge pull request #2875 from CounterpartyXCP/fixes
Browse files Browse the repository at this point in the history
API Fixes and tweaks
  • Loading branch information
ouziel-slama authored Dec 20, 2024
2 parents 6990fda + 1a776ed commit d2bd26e
Show file tree
Hide file tree
Showing 25 changed files with 4,531 additions and 4,342 deletions.
4,651 changes: 2,339 additions & 2,312 deletions apiary.apib

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def dict_factory(cursor, row):

def apply(db):
start_time = time.time()
logger.debug("Populating `address_events` table...")
logger.debug("Populating the `address_events` table...")

if hasattr(db, "row_factory"):
db.row_factory = dict_factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ def apply(db):
},
)

start_time_supply = time.time()
logger.debug("Updating the `supply` field...")

db.execute("""
CREATE TEMP TABLE issuances_quantity AS
SELECT asset, SUM(quantity) AS quantity FROM issuances WHERE status = 'valid' GROUP BY asset
""")
db.execute("""
CREATE TEMP TABLE destructions_quantity AS
SELECT asset, SUM(quantity) AS quantity FROM destructions WHERE status = 'valid' GROUP BY asset
""")

db.execute("""
CREATE TEMP TABLE supplies AS
SELECT
issuances_quantity.asset,
issuances_quantity.quantity - COALESCE(destructions_quantity.quantity, 0) AS supply
FROM issuances_quantity
LEFT JOIN destructions_quantity ON issuances_quantity.asset = destructions_quantity.asset
WHERE issuances_quantity.asset = destructions_quantity.asset
""")

db.execute("""
CREATE INDEX temp.supplies_asset_idx ON supplies(asset)
""")

db.execute("""
UPDATE assets_info SET
supply = COALESCE((SELECT supplies.supply FROM supplies WHERE assets_info.asset = supplies.asset), supply)
""")

db.execute("DROP TABLE issuances_quantity")
db.execute("DROP TABLE destructions_quantity")
db.execute("DROP TABLE supplies")

logger.debug(f"Updated the `supply` field in {time.time() - start_time_supply:.2f} seconds")

db.execute("CREATE UNIQUE INDEX assets_info_asset_idx ON assets_info (asset)")
db.execute("CREATE UNIQUE INDEX assets_info_asset_id_idx ON assets_info (asset_id)")
db.execute("CREATE INDEX assets_info_asset_longname_idx ON assets_info (asset_longname)")
Expand Down
24 changes: 20 additions & 4 deletions counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def select_rows(
bindings += value
elif key.endswith("__notnull"):
where_field.append(f"{key[:-9]} IS NOT NULL")
elif key.endswith("__null"):
where_field.append(f"{key[:-6]} IS NULL")
else:
if key in ADDRESS_FIELDS and len(value.split(",")) > 1:
where_field.append(f"{key} IN ({','.join(['?'] * len(value.split(',')))})")
Expand Down Expand Up @@ -2196,7 +2198,7 @@ def get_receive_by_address_and_asset(
)


def prepare_dispenser_where(status, other_conditions=None):
def prepare_dispenser_where(status, other_conditions=None, exclude_with_oracle=False):
where = []
statuses = status.split(",")
for s in statuses:
Expand All @@ -2205,12 +2207,16 @@ def prepare_dispenser_where(status, other_conditions=None):

if s == "all":
where = other_conditions or {}
if exclude_with_oracle:
where["oracle_address__null"] = True
break

if s in DispenserStatusNumber:
where_status = {"status": DispenserStatusNumber[s]}
if other_conditions:
where_status.update(other_conditions)
if exclude_with_oracle:
where_status["oracle_address__null"] = True

where.append(where_status)

Expand All @@ -2223,6 +2229,7 @@ def prepare_dispenser_where(status, other_conditions=None):
def get_dispensers(
state_db,
status: DispenserStatus = "all",
exclude_with_oracle: bool = False,
cursor: str = None,
limit: int = 100,
offset: int = None,
Expand All @@ -2231,6 +2238,7 @@ def get_dispensers(
"""
Returns all dispensers
:param str status: The status of the dispensers to return
:param bool exclude_with_oracle: Whether to exclude dispensers with an oracle
:param str cursor: The last index of the dispensers to return
:param int limit: The maximum number of dispensers to return (e.g. 5)
:param int offset: The number of lines to skip before returning results (overrides the `cursor` parameter)
Expand All @@ -2240,7 +2248,7 @@ def get_dispensers(
return select_rows(
state_db,
"dispensers",
where=prepare_dispenser_where(status),
where=prepare_dispenser_where(status, exclude_with_oracle=exclude_with_oracle),
last_cursor=cursor,
limit=limit,
offset=offset,
Expand All @@ -2253,6 +2261,7 @@ def get_dispensers_by_address(
state_db,
address: str,
status: DispenserStatus = "all",
exclude_with_oracle: bool = False,
cursor: str = None,
limit: int = 100,
offset: int = None,
Expand All @@ -2262,6 +2271,7 @@ def get_dispensers_by_address(
Returns the dispensers of an address
:param str address: The address to return (e.g. $ADDRESS_1)
:param str status: The status of the dispensers to return
:param bool exclude_with_oracle: Whether to exclude dispensers with an oracle
:param str cursor: The last index of the dispensers to return
:param int limit: The maximum number of dispensers to return (e.g. 5)
:param int offset: The number of lines to skip before returning results (overrides the `cursor` parameter)
Expand All @@ -2270,7 +2280,9 @@ def get_dispensers_by_address(
return select_rows(
state_db,
"dispensers",
where=prepare_dispenser_where(status, {"source": address}),
where=prepare_dispenser_where(
status, {"source": address}, exclude_with_oracle=exclude_with_oracle
),
last_cursor=cursor,
limit=limit,
offset=offset,
Expand All @@ -2283,6 +2295,7 @@ def get_dispensers_by_asset(
state_db,
asset: str,
status: DispenserStatus = "all",
exclude_with_oracle: bool = False,
cursor: str = None,
limit: int = 100,
offset: int = None,
Expand All @@ -2292,6 +2305,7 @@ def get_dispensers_by_asset(
Returns the dispensers of an asset
:param str asset: The asset to return (e.g. XCP)
:param str status: The status of the dispensers to return
:param bool exclude_with_oracle: Whether to exclude dispensers with an oracle
:param str cursor: The last index of the dispensers to return
:param int limit: The maximum number of dispensers to return (e.g. 5)
:param int offset: The number of lines to skip before returning results (overrides the `cursor` parameter)
Expand All @@ -2300,7 +2314,9 @@ def get_dispensers_by_asset(
return select_rows(
state_db,
"dispensers",
where=prepare_dispenser_where(status, {"asset": asset.upper()}),
where=prepare_dispenser_where(
status, {"asset": asset.upper()}, exclude_with_oracle=exclude_with_oracle
),
last_cursor=cursor,
limit=limit,
offset=offset,
Expand Down
39 changes: 19 additions & 20 deletions counterparty-core/counterpartycore/lib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,26 +772,6 @@ def initialise(db):
],
)

# Consolidated
send.initialise(db)
destroy.initialise(db)
order.initialise(db)
btcpay.initialise(db)
issuance.initialise(db)
broadcast.initialise(db)
bet.initialise(db)
dividend.initialise(db)
burn.initialise(db)
cancel.initialise(db)
rps.initialise(db)
rpsresolve.initialise(db)
sweep.initialise(db)
dispenser.initialise(db)
fairminter.initialise(db)
fairmint.initialise(db)

gas.initialise(db)

# Messages
cursor.execute(
"""CREATE TABLE IF NOT EXISTS messages(
Expand Down Expand Up @@ -859,6 +839,25 @@ def initialise(db):
if "addresses" not in columns:
cursor.execute("""ALTER TABLE mempool ADD COLUMN addresses TEXT""")

# Consolidated
send.initialise(db)
destroy.initialise(db)
order.initialise(db)
btcpay.initialise(db)
issuance.initialise(db)
broadcast.initialise(db)
bet.initialise(db)
dividend.initialise(db)
burn.initialise(db)
cancel.initialise(db)
rps.initialise(db)
rpsresolve.initialise(db)
sweep.initialise(db)
dispenser.initialise(db)
fairminter.initialise(db)
fairmint.initialise(db)
gas.initialise(db)

create_views(db)

# Lock UPDATE on all tables
Expand Down
19 changes: 18 additions & 1 deletion counterparty-core/counterpartycore/lib/messages/issuance.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,29 @@ def initialise(db):
UPDATE issuances SET
asset_events = (
SELECT
json_extract(bindings, '$.asset_events')
json_extract(messages.bindings, '$.asset_events')
FROM messages
WHERE messages.tx_hash = issuances.tx_hash
AND messages.event IN ('ASSET_ISSUANCE', 'RESET_ISSUANCE', 'ASSET_TRANSFER')
);
""")
database.lock_update(db, "issuances")
database.set_config_value(db, "FIX_ASSET_EVENTS_FIELD_1", True)
elif database.get_config_value(db, "FIX_ASSET_EVENTS_FIELD_1") is None:
logger.debug("Fixing issuances `asset_events` field")
database.unlock_update(db, "issuances")
cursor.execute("""
UPDATE issuances SET
asset_events = (
SELECT
json_extract(messages.bindings, '$.asset_events')
FROM messages
WHERE messages.tx_hash = issuances.tx_hash
AND messages.event IN ('ASSET_ISSUANCE', 'RESET_ISSUANCE', 'ASSET_TRANSFER')
);
""")
database.lock_update(db, "issuances")
database.set_config_value(db, "FIX_ASSET_EVENTS_FIELD_1", True)

# remove FOREIGN KEY with transactions
if database.has_fk_on(cursor, "issuances", "transactions.tx_index"):
Expand Down
Loading

0 comments on commit d2bd26e

Please sign in to comment.