Skip to content

Commit

Permalink
Merge pull request #2464 from CounterpartyXCP/develop
Browse files Browse the repository at this point in the history
v10.5.0-alpha.3
  • Loading branch information
ouziel-slama authored Oct 19, 2024
2 parents 57ede8a + 03ac9c9 commit edea73e
Show file tree
Hide file tree
Showing 35 changed files with 3,722 additions and 3,506 deletions.
3,649 changes: 1,838 additions & 1,811 deletions apiary.apib

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/api/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def handle_route(**kwargs):
logger.trace(f"API Request - {request.remote_addr} {request.method} {request.url}")
logger.debug(get_log_prefix(query_args))

if wsgi.BACKEND_HEIGHT is None:
if not config.FORCE and wsgi.BACKEND_HEIGHT is None:
return return_result(
503,
error="Backend still not ready. Please try again later.",
Expand All @@ -269,7 +269,7 @@ def handle_route(**kwargs):
with configure_sentry_scope() as scope:
scope.set_transaction_name(get_transaction_name(rule))

if not wsgi.is_server_ready() and not return_result_if_not_ready(rule):
if not config.FORCE and not wsgi.is_server_ready() and not return_result_if_not_ready(rule):
return return_result(
503, error="Counterparty not ready", start_time=start_time, query_args=query_args
)
Expand Down
10 changes: 9 additions & 1 deletion counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def select_rows(
elif key.endswith("__in"):
where_field.append(f"{key[:-4]} IN ({','.join(['?'] * len(value))})")
bindings += value
elif key.endswith("__notnull"):
where_field.append(f"{key[:-9]} IS NOT 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 @@ -344,7 +346,13 @@ def get_last_block(db):
"""
Return the information of the last block
"""
return select_row(db, "blocks", where={})
return select_row(
db,
"blocks",
where={
"ledger_hash__notnull": None,
},
)


def get_transactions(db, cursor: str = None, limit: int = 10, offset: int = None):
Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,15 +979,15 @@ def software_version():
requests.exceptions.ReadTimeout,
TimeoutError,
):
logger.warning("Unable to check Counterparty version.", exc_info=sys.exc_info())
logger.warning("Unable to check Counterparty version.")
return False

for change_name in versions:
protocol_change = versions[change_name]
try:
check_change(protocol_change, change_name)
except VersionUpdateRequiredError: # noqa: F841
logger.error("Version Update Required", exc_info=sys.exc_info())
logger.error("Version Update Required")
sys.exit(config.EXITCODE_UPDATE_REQUIRED)

logger.debug("Version check passed.")
Expand Down
4 changes: 3 additions & 1 deletion counterparty-core/counterpartycore/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


# Semantic Version
__version__ = "10.5.0-alpha.2" # for hatch
__version__ = "10.5.0-alpha.3" # for hatch
VERSION_STRING = __version__
version = VERSION_STRING.split("-")[0].split(".")
VERSION_MAJOR = int(version[0])
Expand Down Expand Up @@ -162,6 +162,8 @@
MPMA_LIMIT = 1000

PROTOCOL_CHANGES_URL = "https://counterparty.io/protocol_changes.json"
# PROTOCOL_CHANGES_URL = "https://raw.githubusercontent.com/CounterpartyXCP/counterparty-core/refs/heads/master/counterparty-core/counterpartycore/protocol_changes.json"

BOOTSTRAP_URL_MAINNET = "https://bootstrap.counterparty.io/counterparty.latest.tar.gz"
BOOTSTRAP_URL_MAINNET_SIG = "https://bootstrap.counterparty.io/counterparty.latest.sig"
BOOTSTRAP_URL_TESTNET = "https://bootstrap.counterparty.io/counterparty-testnet.latest.tar.gz"
Expand Down
163 changes: 133 additions & 30 deletions counterparty-core/counterpartycore/lib/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def insert_record(db, table_name, record, event, event_info={}): # noqa: B006

with get_cursor(db) as cursor:
cursor.execute(query, list(record.values()))
if table_name in ["issuances", "destructions"] and not util.PARSING_MEMPOOL:
cursor.execute("SELECT last_insert_rowid() AS rowid")
inserted_rowid = cursor.fetchone()["rowid"]
new_record = cursor.execute(
f"SELECT * FROM {table_name} WHERE rowid = ?", # noqa: S608
(inserted_rowid,),
).fetchone()
if table_name == "issuances":
AssetCache(db).add_issuance(new_record)
elif table_name == "destructions":
AssetCache(db).add_destroyed(new_record)

add_to_journal(db, util.CURRENT_BLOCK_INDEX, "insert", table_name, event, record | event_info)

Expand Down Expand Up @@ -983,6 +994,113 @@ def get_assets_by_longname(db, asset_longname):
return cursor.fetchall()


def get_last_issuance_no_cache(db, asset):
last_issuance = get_asset(db, asset)
del last_issuance["supply"]
return last_issuance


def asset_destroyed_total_no_cache(db, asset):
"""Return asset total destroyed."""
cursor = db.cursor()
query = """
SELECT SUM(quantity) AS total
FROM destructions
WHERE (status = ? AND asset = ?)
"""
bindings = ("valid", asset)
cursor.execute(query, bindings)
destroyed_total = list(cursor)[0]["total"] or 0
cursor.close()
return destroyed_total


class AssetCache(metaclass=util.SingletonMeta):
def __init__(self, db) -> None:
self.assets = {}
self.assets_total_issued = {}
self.assets_total_destroyed = {}
self.init(db)

def init(self, db):
start = time.time()
logger.debug("Initialising asset cache...")
# asset info
sql = """
SELECT *, MAX(rowid) AS rowid FROM issuances
WHERE status = 'valid'
GROUP BY asset
"""
cursor = db.cursor()
all_assets = cursor.execute(sql)
self.assets = {}
for asset in all_assets:
del asset["rowid"]
if asset["asset_longname"] is not None:
self.assets[asset["asset_longname"]] = asset
self.assets[asset["asset"]] = asset
duration = time.time() - start
# asset total issued
sql = """
SELECT SUM(quantity) AS total, asset
FROM issuances
WHERE status = 'valid'
GROUP BY asset
"""
cursor.execute(sql)
all_counts = cursor.fetchall()
self.assets_total_issued = {}
for count in all_counts:
self.assets_total_issued[count["asset"]] = count["total"]
# asset total destroyed
sql = """
SELECT SUM(quantity) AS total, asset
FROM destructions
WHERE status = 'valid'
GROUP BY asset
"""
cursor.execute(sql)
all_counts = cursor.fetchall()
self.assets_total_destroyed = {}
for count in all_counts:
self.assets_total_destroyed[count["asset"]] = count["total"]

logger.debug(f"Asset cache initialised in {duration:.2f} seconds")

def add_issuance(self, issuance):
if "rowid" in issuance:
del issuance["rowid"]
if issuance["asset_longname"] is not None:
self.assets[issuance["asset_longname"]] = issuance
self.assets[issuance["asset"]] = issuance
if issuance["quantity"] is not None:
if issuance["asset"] in self.assets_total_issued:
self.assets_total_issued[issuance["asset"]] += issuance["quantity"]
else:
self.assets_total_issued[issuance["asset"]] = issuance["quantity"]

def add_destroyed(self, destroyed):
if "rowid" in destroyed:
del destroyed["rowid"]
if destroyed["quantity"] is not None:
if destroyed["asset"] in self.assets_total_destroyed:
self.assets_total_destroyed[destroyed["asset"]] += destroyed["quantity"]
else:
self.assets_total_destroyed[destroyed["asset"]] = destroyed["quantity"]


def asset_destroyed_total(db, asset):
return AssetCache(db).assets_total_destroyed.get(asset, 0)


def get_last_issuance(db, asset):
return AssetCache(db).assets.get(asset)


def asset_issued_total(db, asset):
return AssetCache(db).assets_total_issued.get(asset, 0)


def get_asset(db, asset):
cursor = db.cursor()
name_field = "asset_longname" if "." in asset else "asset"
Expand All @@ -1009,6 +1127,21 @@ def get_asset(db, asset):
return asset


def asset_issued_total_no_cache(db, asset):
"""Return asset total issued."""
cursor = db.cursor()
query = """
SELECT SUM(quantity) AS total
FROM issuances
WHERE (status = ? AND asset = ?)
"""
bindings = ("valid", asset)
cursor.execute(query, bindings)
issued_total = list(cursor)[0]["total"] or 0
cursor.close()
return issued_total


#####################
# BROADCASTS #
#####################
Expand Down Expand Up @@ -2399,36 +2532,6 @@ def destructions(db):
return destructions


def asset_issued_total(db, asset):
"""Return asset total issued."""
cursor = db.cursor()
query = """
SELECT SUM(quantity) AS total
FROM issuances
WHERE (status = ? AND asset = ?)
"""
bindings = ("valid", asset)
cursor.execute(query, bindings)
issued_total = list(cursor)[0]["total"] or 0
cursor.close()
return issued_total


def asset_destroyed_total(db, asset):
"""Return asset total destroyed."""
cursor = db.cursor()
query = """
SELECT SUM(quantity) AS total
FROM destructions
WHERE (status = ? AND asset = ?)
"""
bindings = ("valid", asset)
cursor.execute(query, bindings)
destroyed_total = list(cursor)[0]["total"] or 0
cursor.close()
return destroyed_total


def asset_supply(db, asset):
"""Return asset supply."""
return asset_issued_total(db, asset) - asset_destroyed_total(db, asset)
Expand Down
3 changes: 1 addition & 2 deletions counterparty-core/counterpartycore/lib/messages/fairmint.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def parse(db, tx, message):
ledger.insert_record(db, "fairmints", bindings, "NEW_FAIRMINT")

# we prepare the new issuance
last_issuance = ledger.get_asset(db, asset)
last_issuance = ledger.get_last_issuance(db, asset)
bindings = last_issuance | {
"tx_index": tx["tx_index"],
"tx_hash": tx["tx_hash"],
Expand Down Expand Up @@ -307,7 +307,6 @@ def parse(db, tx, message):
ledger.update_fairminter(db, fairminter["tx_hash"], {"status": "closed"})

# we insert the new issuance
del bindings["supply"]
ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE", {"asset_events": "fairmint"})

# log
Expand Down
2 changes: 2 additions & 0 deletions counterparty-core/counterpartycore/lib/messages/fairminter.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def validate(
# check if hard cap is already reached
if hard_cap and existing_asset["supply"] + premint_quantity >= hard_cap:
problems.append(f"Hard cap of asset `{asset_name}` is already reached.")
if existing_asset["divisible"] != divisible:
problems.append(f"Divisibility of asset `{asset_name}` is different.")
else:
if premint_quantity > 0 and premint_quantity >= hard_cap:
problems.append("Premint quantity must be < hard cap.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ def init(self, utxo_locks_max_addresses=None):
self.utxo_locks = None
if self.utxo_locks_max_addresses > 0:
self.utxo_locks = util.DictCache(self.utxo_locks_max_addresses)
print("UTXOLocks initialized")
print(self.utxo_locks, self.utxo_locks_max_addresses)

def make_outkey_vin_txid(self, txid, vout):
if (txid, vout) not in self.utxo_p2sh_encoding_locks_cache:
Expand Down
10 changes: 10 additions & 0 deletions counterparty-core/counterpartycore/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,13 @@ def determine_encoding(

monkeypatch.setattr("counterpartycore.lib.gas.get_transaction_fee", get_transaction_fee)
monkeypatch.setattr("counterpartycore.lib.transaction.determine_encoding", determine_encoding)

monkeypatch.setattr(
"counterpartycore.lib.ledger.asset_issued_total", ledger.asset_issued_total_no_cache
)
monkeypatch.setattr(
"counterpartycore.lib.ledger.get_last_issuance", ledger.get_last_issuance_no_cache
)
monkeypatch.setattr(
"counterpartycore.lib.ledger.asset_destroyed_total", ledger.asset_destroyed_total_no_cache
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Creating connection to `:memory:`...
Initializing database...
Initialising asset cache...
Asset cache initialised in 0.00 seconds
TX Construct - Transaction constructed.
1_mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc_mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns_2 burned 62000000 BTC for 93000000000 XCP (9b6b1abb696d8d1b70c5beed046d7cddd23cd95b69ef18946cb18c5b56cfde30) [valid]
TX Construct - Transaction constructed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,8 @@ CREATE INDEX issuances_issuer_idx ON issuances (issuer)
;
CREATE INDEX issuances_source_idx ON issuances (source)
;
CREATE INDEX issuances_status_asset_longname_tx_index_idx ON issuances (status, asset_longname, tx_index DESC)
;
CREATE INDEX issuances_status_asset_tx_index_idx ON issuances (status, asset, tx_index DESC)
;
CREATE INDEX issuances_status_idx ON issuances (status)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Creating connection to `:memory:`...
Initializing database...
Initialising asset cache...
Asset cache initialised in 0.00 seconds
TX Construct - Transaction constructed.
1_mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc_mnfAHmddVibnZNSkh8DvKaQoiEfNsxjXzH_mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns_3 burned 62000000 BTC for 93000000000 XCP (63f8a75a06328d984c928bdcf6bebb20d9c2b154712f1d03041d07c6f319efd2) [valid]
TX Construct - Transaction constructed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,8 @@ CREATE INDEX issuances_issuer_idx ON issuances (issuer)
;
CREATE INDEX issuances_source_idx ON issuances (source)
;
CREATE INDEX issuances_status_asset_longname_tx_index_idx ON issuances (status, asset_longname, tx_index DESC)
;
CREATE INDEX issuances_status_asset_tx_index_idx ON issuances (status, asset, tx_index DESC)
;
CREATE INDEX issuances_status_idx ON issuances (status)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Creating connection to `:memory:`...
Initializing database...
Initialising asset cache...
Asset cache initialised in 0.00 seconds
TX Construct - Transaction constructed.
2_mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc_mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns_2 burned 62000000 BTC for 93000000000 XCP (5fde1c728d8d00aaa1b5f8dae963ceb4fd30c415eb0b8a982ba2d8d676fec0bb) [valid]
TX Construct - Transaction constructed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,8 @@ CREATE INDEX issuances_issuer_idx ON issuances (issuer)
;
CREATE INDEX issuances_source_idx ON issuances (source)
;
CREATE INDEX issuances_status_asset_longname_tx_index_idx ON issuances (status, asset_longname, tx_index DESC)
;
CREATE INDEX issuances_status_asset_tx_index_idx ON issuances (status, asset, tx_index DESC)
;
CREATE INDEX issuances_status_idx ON issuances (status)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Creating connection to `:memory:`...
Initializing database...
Initialising asset cache...
Asset cache initialised in 0.00 seconds
TX Construct - Transaction constructed.
2_mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc_mnfAHmddVibnZNSkh8DvKaQoiEfNsxjXzH_mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns_3 burned 62000000 BTC for 93000000000 XCP (15b35d5497f454c43576f21a1b61d99bde25dfc5aae1a4796dcf55e739d6a399) [valid]
TX Construct - Transaction constructed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,8 @@ CREATE INDEX issuances_issuer_idx ON issuances (issuer)
;
CREATE INDEX issuances_source_idx ON issuances (source)
;
CREATE INDEX issuances_status_asset_longname_tx_index_idx ON issuances (status, asset_longname, tx_index DESC)
;
CREATE INDEX issuances_status_asset_tx_index_idx ON issuances (status, asset, tx_index DESC)
;
CREATE INDEX issuances_status_idx ON issuances (status)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Creating connection to `:memory:`...
Initializing database...
Initialising asset cache...
Asset cache initialised in 0.00 seconds
TX Construct - Transaction constructed.
3_mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc_mnfAHmddVibnZNSkh8DvKaQoiEfNsxjXzH_mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns_3 burned 62000000 BTC for 93000000000 XCP (c9ff1be2579378fad6d83ca87e6c91428b1eb8cfd1b0f341b3c7e452764404f5) [valid]
TX Construct - Transaction constructed.
Expand Down
Loading

0 comments on commit edea73e

Please sign in to comment.