Skip to content

Commit

Permalink
progress in ledger.py splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 16, 2025
1 parent 0207d6a commit 8936396
Show file tree
Hide file tree
Showing 12 changed files with 817 additions and 823 deletions.
14 changes: 7 additions & 7 deletions counterparty-core/counterpartycore/lib/api/apiv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,16 +678,16 @@ def get_supply(asset):
if asset == "BTC":
return backend.bitcoind.get_btc_supply(normalize=False)
elif asset == "XCP":
return ledger.ledger.xcp_supply(db)
return ledger.supplies.xcp_supply(db)
else:
asset = ledger.ledger.resolve_subasset_longname(db, asset)
return ledger.ledger.asset_supply(db, asset)
return ledger.supplies.asset_supply(db, asset)

@dispatcher.add_method
def get_xcp_supply():
logger.warning("Deprecated method: `get_xcp_supply`")
with LedgerDBConnectionPool().connection() as db:
return ledger.ledger.xcp_supply(db)
return ledger.supplies.xcp_supply(db)

@dispatcher.add_method
def get_asset_info(assets=None, asset=None):
Expand All @@ -708,7 +708,7 @@ def get_asset_info(assets=None, asset=None):
if asset == config.BTC:
supply = backend.bitcoind.get_btc_supply(normalize=False)
else:
supply = ledger.ledger.xcp_supply(db)
supply = ledger.supplies.xcp_supply(db)

assets_info.append(
{
Expand Down Expand Up @@ -745,7 +745,7 @@ def get_asset_info(assets=None, asset=None):
"owner": last_issuance["issuer"],
"divisible": bool(last_issuance["divisible"]),
"locked": locked,
"supply": ledger.ledger.asset_supply(db, asset),
"supply": ledger.supplies.asset_supply(db, asset),
"description": last_issuance["description"],
"issuer": last_issuance["issuer"],
}
Expand Down Expand Up @@ -928,7 +928,7 @@ def get_asset_longnames():
def get_holder_count(asset):
with LedgerDBConnectionPool().connection() as db:
asset = ledger.ledger.resolve_subasset_longname(db, asset)
holders = ledger.ledger.holders(db, asset, True)
holders = ledger.supplies.holders(db, asset, True)
addresses = []
for holder in holders:
addresses.append(holder["address"])
Expand All @@ -938,7 +938,7 @@ def get_holder_count(asset):
def get_holders(asset):
with LedgerDBConnectionPool().connection() as db:
asset = ledger.ledger.resolve_subasset_longname(db, asset)
holders = ledger.ledger.holders(db, asset, True)
holders = ledger.supplies.holders(db, asset, True)
return holders

@dispatcher.add_method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def apply(db):
"asset": "XCP",
"divisible": True,
"locked": True,
"supply": ledger.ledger.xcp_supply(ledger_db),
"supply": ledger.supplies.xcp_supply(ledger_db),
"description": "The Counterparty protocol native currency",
"first_issuance_block_index": 278319,
"last_issuance_block_index": 283810,
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/ledger/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import blocks, ledger # noqa F401
from . import blocks, ledger, supplies, caches # noqa F401
81 changes: 81 additions & 0 deletions counterparty-core/counterpartycore/lib/ledger/caches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import logging
import time

from counterpartycore.lib import config
from counterpartycore.lib.utils import helpers

logger = logging.getLogger(config.LOGGER_NAME)


class AssetCache(metaclass=helpers.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"]
Loading

0 comments on commit 8936396

Please sign in to comment.