Skip to content

Commit

Permalink
clean ledger import
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 16, 2025
1 parent 3d29a7d commit 288b1b2
Show file tree
Hide file tree
Showing 41 changed files with 520 additions and 536 deletions.
50 changes: 23 additions & 27 deletions counterparty-core/counterpartycore/lib/api/apiv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
backend,
config,
exceptions,
ledger,
)
from counterpartycore.lib.api import composer, healthz
from counterpartycore.lib.api.apiwatcher import STATE_DB_TABLES
from counterpartycore.lib.cli.log import init_api_access_log
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.messages import (
bet, # noqa: F401
Expand Down Expand Up @@ -384,7 +384,7 @@ def adjust_get_balances_results(query_result, ledger_db):
for balances_row in list(query_result):
asset = balances_row["asset"]
if asset not in assets:
assets[asset] = ledger.ledger.is_divisible(ledger_db, asset)
assets[asset] = ledger.is_divisible(ledger_db, asset)

balances_row["divisible"] = assets[asset]
filtered_results.append(balances_row)
Expand Down Expand Up @@ -652,7 +652,7 @@ def get_messages(block_index):
if not isinstance(block_index, int):
raise exceptions.APIError("block_index must be an integer.")
with LedgerDBConnectionPool().connection() as db:
messages = ledger.ledger.get_messages(db, block_index=block_index)
messages = ledger.get_messages(db, block_index=block_index)
return messages

@dispatcher.add_method
Expand All @@ -669,7 +669,7 @@ def get_messages_by_index(message_indexes):
if not isinstance(idx, int):
raise exceptions.APIError("All items in message_indexes are not integers")
with LedgerDBConnectionPool().connection() as db:
messages = ledger.ledger.get_messages(db, message_index_in=message_indexes)
messages = ledger.get_messages(db, message_index_in=message_indexes)
return messages

@dispatcher.add_method
Expand All @@ -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.xcp_supply(db)
else:
asset = ledger.ledger.resolve_subasset_longname(db, asset)
return ledger.ledger.asset_supply(db, asset)
asset = ledger.resolve_subasset_longname(db, asset)
return ledger.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.xcp_supply(db)

@dispatcher.add_method
def get_asset_info(assets=None, asset=None):
Expand All @@ -701,14 +701,14 @@ def get_asset_info(assets=None, asset=None):
assets_info = []
with LedgerDBConnectionPool().connection() as db:
for asset in assets:
asset = ledger.ledger.resolve_subasset_longname(db, asset) # noqa: PLW2901
asset = ledger.resolve_subasset_longname(db, asset) # noqa: PLW2901

# BTC and XCP.
if asset in [config.BTC, config.XCP]:
if asset == config.BTC:
supply = backend.bitcoind.get_btc_supply(normalize=False)
else:
supply = ledger.ledger.xcp_supply(db)
supply = ledger.xcp_supply(db)

assets_info.append(
{
Expand All @@ -726,9 +726,7 @@ def get_asset_info(assets=None, asset=None):

# User‐created asset.
cursor = db.cursor()
issuances = ledger.ledger.get_issuances(
db, asset=asset, status="valid", first=True
)
issuances = ledger.get_issuances(db, asset=asset, status="valid", first=True)
cursor.close()
if not issuances:
continue # asset not found, most likely
Expand All @@ -745,7 +743,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.asset_supply(db, asset),
"description": last_issuance["description"],
"issuer": last_issuance["issuer"],
}
Expand Down Expand Up @@ -805,9 +803,7 @@ def get_blocks(block_indexes, min_message_index=None):
)
blocks = cursor.fetchall() # noqa: F811

messages = collections.deque(
ledger.ledger.get_messages(db, block_index_in=block_indexes)
)
messages = collections.deque(ledger.get_messages(db, block_index_in=block_indexes))

# Discard any messages less than min_message_index
if min_message_index:
Expand Down Expand Up @@ -835,10 +831,10 @@ def get_running_info():
else:
caught_up = True

last_block = ledger.ledger.get_last_block(db)
last_block = ledger.get_last_block(db)

try:
last_message = ledger.ledger.last_message(db)
last_message = ledger.last_message(db)
except: # noqa: E722
last_message = None

Expand Down Expand Up @@ -910,7 +906,7 @@ def get_element_counts():
@dispatcher.add_method
def get_asset_names(longnames=False):
with LedgerDBConnectionPool().connection() as db:
all_assets = ledger.ledger.get_valid_assets(db)
all_assets = ledger.get_valid_assets(db)
if longnames:
names = [
{"asset": row["asset"], "asset_longname": row["asset_longname"]}
Expand All @@ -927,8 +923,8 @@ def get_asset_longnames():
@dispatcher.add_method
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)
asset = ledger.resolve_subasset_longname(db, asset)
holders = ledger.holders(db, asset, True)
addresses = []
for holder in holders:
addresses.append(holder["address"])
Expand All @@ -937,8 +933,8 @@ def get_holder_count(asset):
@dispatcher.add_method
def get_holders(asset):
with LedgerDBConnectionPool().connection() as db:
asset = ledger.ledger.resolve_subasset_longname(db, asset)
holders = ledger.ledger.holders(db, asset, True)
asset = ledger.resolve_subasset_longname(db, asset)
holders = ledger.holders(db, asset, True)
return holders

@dispatcher.add_method
Expand Down Expand Up @@ -1021,9 +1017,9 @@ def get_dispenser_info(tx_hash=None, tx_index=None):
dispensers = []
with LedgerDBConnectionPool().connection() as db:
if tx_hash is not None:
dispensers = ledger.ledger.get_dispenser_info(db, tx_hash=tx_hash)
dispensers = ledger.get_dispenser_info(db, tx_hash=tx_hash)
else:
dispensers = ledger.ledger.get_dispenser_info(db, tx_index=tx_index)
dispensers = ledger.get_dispenser_info(db, tx_index=tx_index)

if len(dispensers) == 1:
dispenser = dispensers[0]
Expand All @@ -1040,7 +1036,7 @@ def get_dispenser_info(tx_hash=None, tx_index=None):
oracle_fee,
oracle_fiat_label,
oracle_price_last_updated,
) = ledger.ledger.get_oracle_last_price(
) = ledger.get_oracle_last_price(
db, dispenser["oracle_address"], CurrentState().current_block_index()
)

Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/api/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
backend,
config,
exceptions,
ledger,
)
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.parser import deserialize, utxosinfo
from counterpartycore.lib.utils import helpers, multisig, opcodes, script

Expand Down Expand Up @@ -505,7 +505,7 @@ def filter_utxos_with_balances(db, source, unspent_list, construct_params):
if str_input == source:
new_unspent_list.append(utxo)
continue
utxo_balances = ledger.ledger.get_utxo_balances(db, str_input)
utxo_balances = ledger.get_utxo_balances(db, str_input)
with_balances = len(utxo_balances) > 0 and any(
balance["quantity"] > 0 for balance in utxo_balances
)
Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/api/healthz.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
backend,
config,
exceptions,
ledger,
)
from counterpartycore.lib.api import composer
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.utils import helpers

Expand All @@ -18,7 +18,7 @@

def check_last_parsed_block(db, blockcount):
"""Checks database to see if is caught up with backend."""
last_block = ledger.ledger.get_last_block(db)
last_block = ledger.get_last_block(db)
if last_block is None:
raise exceptions.DatabaseError(
f"{config.XCP_NAME} database is behind backend."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import logging
import time

from counterpartycore.lib import config, ledger
from counterpartycore.lib import config
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.utils import database
from yoyo import step

Expand Down Expand Up @@ -110,7 +111,7 @@ def apply(db):
"asset": "XCP",
"divisible": True,
"locked": True,
"supply": ledger.ledger.xcp_supply(ledger_db),
"supply": ledger.xcp_supply(ledger_db),
"description": "The Counterparty protocol native currency",
"first_issuance_block_index": 278319,
"last_issuance_block_index": 283810,
Expand Down
10 changes: 5 additions & 5 deletions counterparty-core/counterpartycore/lib/api/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from counterpartycore.lib import (
config,
exceptions,
ledger,
)
from counterpartycore.lib.api import compose
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.utils import helpers

Expand Down Expand Up @@ -87,10 +87,10 @@ def inject_issuances_and_block_times(ledger_db, state_db, result_list):
asset_list.append(item[field_name])

# get asset issuances
issuance_by_asset = ledger.ledger.get_assets_last_issuance(state_db, asset_list)
issuance_by_asset = ledger.get_assets_last_issuance(state_db, asset_list)

# get block_time for each block_index
block_times = ledger.ledger.get_blocks_time(ledger_db, block_indexes)
block_times = ledger.get_blocks_time(ledger_db, block_indexes)

# inject issuance and block_time
for result_item in result_list:
Expand Down Expand Up @@ -367,7 +367,7 @@ def inject_fiat_price(ledger_db, dispenser):
_oracle_fee,
dispenser["fiat_unit"],
dispenser["oracle_price_last_updated"],
) = ledger.ledger.get_oracle_last_price(
) = ledger.get_oracle_last_price(
ledger_db, dispenser["oracle_address"], CurrentState().current_block_index()
)

Expand Down Expand Up @@ -402,7 +402,7 @@ def inject_dispensers(ledger_db, state_db, result_list):
dispenser_list.append(result_item["dispenser_tx_hash"])

# get dispenser info
dispenser_info = ledger.ledger.get_dispensers_info(state_db, dispenser_list)
dispenser_info = ledger.get_dispensers_info(state_db, dispenser_list)

# inject dispenser info
enriched_result_list = []
Expand Down
16 changes: 8 additions & 8 deletions counterparty-core/counterpartycore/lib/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
backend,
config,
exceptions,
ledger,
)
from counterpartycore.lib.api import apiserver as api_v2
from counterpartycore.lib.api import apiv1, dbbuilder
from counterpartycore.lib.cli import bootstrap, log
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.parser import blocks, check, follow
from counterpartycore.lib.utils import database, helpers
Expand Down Expand Up @@ -52,7 +52,7 @@ def initialise(*args, **kwargs):
)
initialise_config(*args, **kwargs)
db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
CurrentState().set_current_block_index(ledger.last_db_index(db))
return db


Expand Down Expand Up @@ -673,7 +673,7 @@ def start_all(args):
# Initialise database
database.apply_outstanding_migration(config.DATABASE, config.LEDGER_DB_MIGRATIONS_DIR)
db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
CurrentState().set_current_block_index(ledger.last_db_index(db))
blocks.check_database_version(db)
database.optimize(db)

Expand Down Expand Up @@ -771,9 +771,9 @@ def start_all(args):

def reparse(block_index):
ledger_db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(ledger_db))
CurrentState().set_current_block_index(ledger.last_db_index(ledger_db))

last_block = ledger.ledger.get_last_block(ledger_db)
last_block = ledger.get_last_block(ledger_db)
if last_block is None or block_index > last_block["block_index"]:
print(colored("Block index is higher than current block index. No need to reparse.", "red"))
ledger_db.close()
Expand All @@ -792,9 +792,9 @@ def reparse(block_index):

def rollback(block_index=None):
ledger_db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(ledger_db))
CurrentState().set_current_block_index(ledger.last_db_index(ledger_db))

last_block = ledger.ledger.get_last_block(ledger_db)
last_block = ledger.get_last_block(ledger_db)
if last_block is None or block_index > last_block["block_index"]:
print(
colored("Block index is higher than current block index. No need to rollback.", "red")
Expand Down Expand Up @@ -822,7 +822,7 @@ def vacuum():

def check_database():
db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
CurrentState().set_current_block_index(ledger.last_db_index(db))

start_all_time = time.time()

Expand Down
7 changes: 3 additions & 4 deletions counterparty-core/counterpartycore/lib/ledger/currentstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from flask import request

from counterpartycore.lib import backend, ledger
from counterpartycore.lib import backend
from counterpartycore.lib.ledger import ledger
from counterpartycore.lib.utils import helpers
from counterpartycore.lib.utils.database import LedgerDBConnectionPool

Expand Down Expand Up @@ -37,9 +38,7 @@ def set_current_block_index(self, block_index):
self.state["CURRENT_BLOCK_INDEX"] = block_index
if block_index:
with LedgerDBConnectionPool().connection() as ledger_db:
last_block = ledger.ledger.get_block(
ledger_db, CurrentState().current_block_index()
)
last_block = ledger.get_block(ledger_db, CurrentState().current_block_index())
if last_block:
self.state["CURRENT_BLOCK_TIME"] = last_block["block_time"]
else:
Expand Down
Loading

0 comments on commit 288b1b2

Please sign in to comment.