diff --git a/counterparty-core/counterpartycore/lib/api/apiv1.py b/counterparty-core/counterpartycore/lib/api/apiv1.py index c9e8476dd..a618243e8 100644 --- a/counterparty-core/counterpartycore/lib/api/apiv1.py +++ b/counterparty-core/counterpartycore/lib/api/apiv1.py @@ -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 @@ -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) @@ -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 @@ -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 @@ -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): @@ -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( { @@ -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 @@ -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"], } @@ -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: @@ -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 @@ -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"]} @@ -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"]) @@ -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 @@ -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] @@ -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() ) diff --git a/counterparty-core/counterpartycore/lib/api/composer.py b/counterparty-core/counterpartycore/lib/api/composer.py index eb4e27344..31ad5c6a0 100644 --- a/counterparty-core/counterpartycore/lib/api/composer.py +++ b/counterparty-core/counterpartycore/lib/api/composer.py @@ -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 @@ -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 ) diff --git a/counterparty-core/counterpartycore/lib/api/healthz.py b/counterparty-core/counterpartycore/lib/api/healthz.py index 8ff5f29f1..0d8a924b9 100644 --- a/counterparty-core/counterpartycore/lib/api/healthz.py +++ b/counterparty-core/counterpartycore/lib/api/healthz.py @@ -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 @@ -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." diff --git a/counterparty-core/counterpartycore/lib/api/migrations/0004.create_and_populate_assets_info.py b/counterparty-core/counterpartycore/lib/api/migrations/0004.create_and_populate_assets_info.py index e90758626..ffc374d05 100644 --- a/counterparty-core/counterpartycore/lib/api/migrations/0004.create_and_populate_assets_info.py +++ b/counterparty-core/counterpartycore/lib/api/migrations/0004.create_and_populate_assets_info.py @@ -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 @@ -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, diff --git a/counterparty-core/counterpartycore/lib/api/verbose.py b/counterparty-core/counterpartycore/lib/api/verbose.py index 30c0bd84a..080d1cda9 100644 --- a/counterparty-core/counterpartycore/lib/api/verbose.py +++ b/counterparty-core/counterpartycore/lib/api/verbose.py @@ -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 @@ -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: @@ -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() ) @@ -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 = [] diff --git a/counterparty-core/counterpartycore/lib/cli/server.py b/counterparty-core/counterpartycore/lib/cli/server.py index 87a26c068..0cc4a0be5 100755 --- a/counterparty-core/counterpartycore/lib/cli/server.py +++ b/counterparty-core/counterpartycore/lib/cli/server.py @@ -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 @@ -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 @@ -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) @@ -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() @@ -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") @@ -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() diff --git a/counterparty-core/counterpartycore/lib/ledger/currentstate.py b/counterparty-core/counterpartycore/lib/ledger/currentstate.py index 0d6dd8028..1de1d586a 100644 --- a/counterparty-core/counterpartycore/lib/ledger/currentstate.py +++ b/counterparty-core/counterpartycore/lib/ledger/currentstate.py @@ -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 @@ -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: diff --git a/counterparty-core/counterpartycore/lib/messages/attach.py b/counterparty-core/counterpartycore/lib/messages/attach.py index 7fe03ecaf..9f7ec3961 100644 --- a/counterparty-core/counterpartycore/lib/messages/attach.py +++ b/counterparty-core/counterpartycore/lib/messages/attach.py @@ -1,7 +1,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import gas from counterpartycore.lib.parser import utxosinfo @@ -35,7 +36,7 @@ def validate_asset_and_quantity(asset, quantity): def validate_balance(db, source, asset, quantity, fee=0): problems = [] # check if source has enough funds - asset_balance = ledger.ledger.get_balance(db, source, asset) + asset_balance = ledger.get_balance(db, source, asset) if asset == config.XCP: # fee is always paid in XCP if asset_balance < quantity + fee: @@ -44,7 +45,7 @@ def validate_balance(db, source, asset, quantity, fee=0): if asset_balance < quantity: problems.append("insufficient funds for transfer") if fee > 0: - xcp_balance = ledger.ledger.get_balance(db, source, config.XCP) + xcp_balance = ledger.get_balance(db, source, config.XCP) if xcp_balance < fee: problems.append("insufficient funds for fee") return problems @@ -142,7 +143,7 @@ def unpack(message, return_dict=False): def pay_fee(db, tx, source, fee): # debit fee from the fee payer - ledger.ledger.debit( + ledger.debit( db, source, config.XCP, @@ -162,7 +163,7 @@ def pay_fee(db, tx, source, fee): "tag": "attach to utxo fee", "status": "valid", } - ledger.ledger.insert_record(db, "destructions", destroy_bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", destroy_bindings, "ASSET_DESTRUCTION") def parse(db, tx, message): @@ -197,12 +198,12 @@ def parse(db, tx, message): bindings = { "tx_index": tx["tx_index"], "tx_hash": tx["tx_hash"], - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]), "block_index": tx["block_index"], "status": status, "send_type": "attach", } - ledger.ledger.insert_record(db, "sends", bindings, "ATTACH_TO_UTXO") + ledger.insert_record(db, "sends", bindings, "ATTACH_TO_UTXO") # return here to avoid further processing return @@ -215,10 +216,8 @@ def parse(db, tx, message): # debit asset from source and credit to recipient action = "attach to utxo" - ledger.ledger.debit( - db, source, asset, quantity, tx["tx_index"], action=action, event=tx["tx_hash"] - ) - destination_address = ledger.ledger.credit( + ledger.debit(db, source, asset, quantity, tx["tx_index"], action=action, event=tx["tx_hash"]) + destination_address = ledger.credit( db, destination, asset, @@ -230,7 +229,7 @@ def parse(db, tx, message): bindings = { "tx_index": tx["tx_index"], "tx_hash": tx["tx_hash"], - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]), "block_index": tx["block_index"], "status": "valid", "source": source, @@ -241,7 +240,7 @@ def parse(db, tx, message): "fee_paid": fee, "send_type": "attach", } - ledger.ledger.insert_record(db, "sends", bindings, "ATTACH_TO_UTXO") + ledger.insert_record(db, "sends", bindings, "ATTACH_TO_UTXO") logger.info( "Attach %(asset)s from %(source)s to utxo: %(destination)s (%(tx_hash)s) [%(status)s]", diff --git a/counterparty-core/counterpartycore/lib/messages/bet.py b/counterparty-core/counterpartycore/lib/messages/bet.py index 5ba16a143..dae81941e 100644 --- a/counterparty-core/counterpartycore/lib/messages/bet.py +++ b/counterparty-core/counterpartycore/lib/messages/bet.py @@ -18,8 +18,8 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import helpers @@ -42,7 +42,7 @@ def date_passed(date): def cancel_bet(db, bet, status, block_index, tx_index): # Update status of bet. set_data = {"status": status} - ledger.ledger.update_bet(db, bet["tx_hash"], set_data) + ledger.update_bet(db, bet["tx_hash"], set_data) log_data = set_data | { "bet_hash": bet["tx_hash"], @@ -50,7 +50,7 @@ def cancel_bet(db, bet, status, block_index, tx_index): logger.info("Bet %(bet_hash)s canceled [%(status)s]", log_data) # Refund wager. - ledger.ledger.credit( + ledger.credit( db, bet["source"], config.XCP, @@ -64,7 +64,7 @@ def cancel_bet(db, bet, status, block_index, tx_index): def cancel_bet_match(db, bet_match, status, block_index, tx_index): # Does not re‐open, re‐fill, etc. constituent bets. # Recredit tx0 address. - ledger.ledger.credit( + ledger.credit( db, bet_match["tx0_address"], config.XCP, @@ -75,7 +75,7 @@ def cancel_bet_match(db, bet_match, status, block_index, tx_index): ) # Recredit tx1 address. - ledger.ledger.credit( + ledger.credit( db, bet_match["tx1_address"], config.XCP, @@ -85,7 +85,7 @@ def cancel_bet_match(db, bet_match, status, block_index, tx_index): event=bet_match["id"], ) # Update status of bet match. - ledger.ledger.update_bet_match_status(db, bet_match["id"], status) + ledger.update_bet_match_status(db, bet_match["id"], status) logger.info( "Bet Match %(id)s canceled [%(status)s]", @@ -98,7 +98,7 @@ def cancel_bet_match(db, bet_match, status, block_index, tx_index): def get_fee_fraction(db, feed_address): """Get fee fraction from last broadcast from the feed_address address.""" - broadcasts = ledger.ledger.get_broadcasts_by_source(db, feed_address, "valid", order_by="ASC") + broadcasts = ledger.get_broadcasts_by_source(db, feed_address, "valid", order_by="ASC") if broadcasts: last_broadcast = broadcasts[-1] @@ -141,7 +141,7 @@ def validate( problems.append("integer overflow") # Look at feed to be bet on. - broadcasts = ledger.ledger.get_broadcasts_by_source(db, feed_address, "valid", order_by="ASC") + broadcasts = ledger.get_broadcasts_by_source(db, feed_address, "valid", order_by="ASC") if not broadcasts: problems.append("feed doesn’t exist") elif not broadcasts[-1]["text"]: @@ -213,7 +213,7 @@ def compose( expiration: int, skip_validation: bool = False, ): - if ledger.ledger.get_balance(db, source, config.XCP) < wager_quantity: + if ledger.get_balance(db, source, config.XCP) < wager_quantity: raise exceptions.ComposeError("insufficient funds") problems, leverage = validate( @@ -316,20 +316,20 @@ def parse(db, tx, message): feed_address = tx["destination"] if status == "open": try: - odds = ledger.ledger.price(wager_quantity, counterwager_quantity) + odds = ledger.price(wager_quantity, counterwager_quantity) except ZeroDivisionError: odds = 0 fee_fraction = get_fee_fraction(db, feed_address) # Overbet - balance = ledger.ledger.get_balance(db, tx["source"], config.XCP) + balance = ledger.get_balance(db, tx["source"], config.XCP) if balance == 0: wager_quantity = 0 else: if balance < wager_quantity: wager_quantity = balance - counterwager_quantity = int(ledger.ledger.price(wager_quantity, odds)) + counterwager_quantity = int(ledger.price(wager_quantity, odds)) problems, leverage = validate( db, @@ -349,7 +349,7 @@ def parse(db, tx, message): # Debit quantity wagered. (Escrow.) if status == "open": - ledger.ledger.debit( + ledger.debit( db, tx["source"], config.XCP, @@ -380,7 +380,7 @@ def parse(db, tx, message): "status": status, } if "integer overflow" not in status: - ledger.ledger.insert_record(db, "bets", bindings, "OPEN_BET") + ledger.insert_record(db, "bets", bindings, "OPEN_BET") logger.info("Open Bet (%(tx_hash)s) [%(status)s]", bindings) @@ -393,7 +393,7 @@ def parse(db, tx, message): def match(db, tx): # Get bet in question. - bets = ledger.ledger.get_bet(db, bet_hash=tx["tx_hash"]) + bets = ledger.get_bet(db, bet_hash=tx["tx_hash"]) if not bets: return else: @@ -414,12 +414,12 @@ def match(db, tx): tx1_wager_remaining = tx1["wager_remaining"] tx1_counterwager_remaining = tx1["counterwager_remaining"] - bet_matches = ledger.ledger.get_matching_bets(db, tx1["feed_address"], counterbet_type) + bet_matches = ledger.get_matching_bets(db, tx1["feed_address"], counterbet_type) if protocol.after_block_or_test_network(tx["block_index"], 284501): # Protocol change. sorted(bet_matches, key=lambda x: x["tx_index"]) # Sort by tx index second. sorted( bet_matches, - key=lambda x: ledger.ledger.price(x["wager_quantity"], x["counterwager_quantity"]), + key=lambda x: ledger.price(x["wager_quantity"], x["counterwager_quantity"]), ) # Sort by price first. tx1_status = tx1["status"] @@ -458,22 +458,22 @@ def match(db, tx): # If the odds agree, make the trade. The found order sets the odds, # and they trade as much as they can. - tx0_odds = ledger.ledger.price(tx0["wager_quantity"], tx0["counterwager_quantity"]) - tx0_inverse_odds = ledger.ledger.price(tx0["counterwager_quantity"], tx0["wager_quantity"]) - tx1_odds = ledger.ledger.price(tx1["wager_quantity"], tx1["counterwager_quantity"]) + tx0_odds = ledger.price(tx0["wager_quantity"], tx0["counterwager_quantity"]) + tx0_inverse_odds = ledger.price(tx0["counterwager_quantity"], tx0["wager_quantity"]) + tx1_odds = ledger.price(tx1["wager_quantity"], tx1["counterwager_quantity"]) if tx["block_index"] < 286000: - tx0_inverse_odds = ledger.ledger.price(1, tx0_odds) # Protocol change. + tx0_inverse_odds = ledger.price(1, tx0_odds) # Protocol change. logger.debug(f"Tx0 Inverse Odds: {float(tx0_inverse_odds)}; Tx1 Odds: {float(tx1_odds)}") if tx0_inverse_odds > tx1_odds: logger.debug("Skipping: price mismatch.") else: logger.debug( - f"Potential forward quantities: {tx0_wager_remaining}, {int(ledger.ledger.price(tx1_wager_remaining, tx1_odds))}" + f"Potential forward quantities: {tx0_wager_remaining}, {int(ledger.price(tx1_wager_remaining, tx1_odds))}" ) forward_quantity = int( - min(tx0_wager_remaining, int(ledger.ledger.price(tx1_wager_remaining, tx1_odds))) + min(tx0_wager_remaining, int(ledger.price(tx1_wager_remaining, tx1_odds))) ) logger.debug(f"Forward Quantity: {forward_quantity}") backward_quantity = round(forward_quantity / tx0_odds) @@ -501,7 +501,7 @@ def match(db, tx): if tx0_wager_remaining <= 0 or tx0_counterwager_remaining <= 0: # Fill order, and recredit give_remaining. tx0_status = "filled" - ledger.ledger.credit( + ledger.credit( db, tx0["source"], config.XCP, @@ -516,7 +516,7 @@ def match(db, tx): "counterwager_remaining": tx0_counterwager_remaining, "status": tx0_status, } - ledger.ledger.update_bet(db, tx0["tx_hash"], set_data) + ledger.update_bet(db, tx0["tx_hash"], set_data) log_data = set_data | { "tx_hash": tx["tx_hash"], @@ -528,7 +528,7 @@ def match(db, tx): if tx1_wager_remaining <= 0 or tx1_counterwager_remaining <= 0: # Fill order, and recredit give_remaining. tx1_status = "filled" - ledger.ledger.credit( + ledger.credit( db, tx1["source"], config.XCP, @@ -543,7 +543,7 @@ def match(db, tx): "counterwager_remaining": tx1_counterwager_remaining, "status": tx1_status, } - ledger.ledger.update_bet(db, tx1["tx_hash"], set_data) + ledger.update_bet(db, tx1["tx_hash"], set_data) log_data = set_data | { "tx_hash": tx["tx_hash"], @@ -552,9 +552,7 @@ def match(db, tx): logger.info("Bet %(bet_hash)s updated (%(tx_hash)s) [%(status)s]", log_data) # Get last value of feed. - broadcasts = ledger.ledger.get_broadcasts_by_source( - db, feed_address, "valid", order_by="ASC" - ) + broadcasts = ledger.get_broadcasts_by_source(db, feed_address, "valid", order_by="ASC") initial_value = broadcasts[-1]["value"] # Record bet fulfillment. @@ -584,7 +582,7 @@ def match(db, tx): "fee_fraction_int": tx1["fee_fraction_int"], "status": "pending", } - ledger.ledger.insert_record(db, "bet_matches", bindings, "BET_MATCH") + ledger.insert_record(db, "bet_matches", bindings, "BET_MATCH") logger.info( "Bet match %(tx0_index)s for %(forward_quantity)s XCP against %(backward_quantity)s XCP on %(feed_address)s", bindings, @@ -597,7 +595,7 @@ def expire(db, block_index, block_time): cursor = db.cursor() # Expire bets and give refunds for the quantity wager_remaining. - for bet in ledger.ledger.get_bets_to_expire(db, block_index): + for bet in ledger.get_bets_to_expire(db, block_index): # use tx_index=0 for block actions cancel_bet(db, bet, "expired", block_index, 0) @@ -608,11 +606,11 @@ def expire(db, block_index, block_time): "source": bet["source"], "block_index": block_index, } - ledger.ledger.insert_record(db, "bet_expirations", bindings, "BET_EXPIRATION") + ledger.insert_record(db, "bet_expirations", bindings, "BET_EXPIRATION") logger.info("Bet Expiration %(bet_hash)s", bindings) # Expire bet matches whose deadline is more than two weeks before the current block time. - for bet_match in ledger.ledger.get_bet_matches_to_expire(db, block_time): + for bet_match in ledger.get_bet_matches_to_expire(db, block_time): # use tx_index=0 for block actions cancel_bet_match(db, bet_match, "expired", block_index, 0) @@ -623,7 +621,7 @@ def expire(db, block_index, block_time): "tx1_address": bet_match["tx1_address"], "block_index": block_index, } - ledger.ledger.insert_record(db, "bet_match_expirations", bindings, "BET_MATCH_EXPIRATION") + ledger.insert_record(db, "bet_match_expirations", bindings, "BET_MATCH_EXPIRATION") logger.info("Bet Match Expiration %(bet_match_id)s", bindings) cursor.close() diff --git a/counterparty-core/counterpartycore/lib/messages/broadcast.py b/counterparty-core/counterpartycore/lib/messages/broadcast.py index cf75c3a99..18f2c87f5 100644 --- a/counterparty-core/counterpartycore/lib/messages/broadcast.py +++ b/counterparty-core/counterpartycore/lib/messages/broadcast.py @@ -29,8 +29,8 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import helpers @@ -92,7 +92,7 @@ def validate(db, source, timestamp, value, fee_fraction_int, text, block_index): if not source: problems.append("null source address") # Check previous broadcast in this feed. - broadcasts = ledger.ledger.get_broadcasts_by_source(db, source, "valid", order_by="ASC") + broadcasts = ledger.get_broadcasts_by_source(db, source, "valid", order_by="ASC") if broadcasts: last_broadcast = broadcasts[-1] if last_broadcast["locked"]: @@ -234,7 +234,7 @@ def parse(db, tx, message): "status": status, } if "integer overflow" not in status: - ledger.ledger.insert_record(db, "broadcasts", bindings, "BROADCAST") + ledger.insert_record(db, "broadcasts", bindings, "BROADCAST") logger.info("Broadcast from %(source)s (%(tx_hash)s) [%(status)s]", bindings) @@ -251,11 +251,11 @@ def parse(db, tx, message): "address": tx["source"], "options": options, } - existing_address = ledger.ledger.get_addresses(db, address=tx["source"]) + existing_address = ledger.get_addresses(db, address=tx["source"]) if len(existing_address) == 0: - ledger.ledger.insert_record(db, "addresses", op_bindings, "NEW_ADDRESS_OPTIONS") + ledger.insert_record(db, "addresses", op_bindings, "NEW_ADDRESS_OPTIONS") else: - ledger.ledger.insert_update( + ledger.insert_update( db, "addresses", "address", tx["source"], op_bindings, "ADDRESS_OPTIONS_UPDATE" ) @@ -263,11 +263,11 @@ def parse(db, tx, message): if value is None or value < 0: # Cancel Open Bets? if value == -2: - for i in ledger.ledger.get_bet_by_feed(db, tx["source"], status="open"): + for i in ledger.get_bet_by_feed(db, tx["source"], status="open"): bet.cancel_bet(db, i, "dropped", tx["block_index"], tx["tx_index"]) # Cancel Pending Bet Matches? if value == -3: - for bet_match in ledger.ledger.get_pending_bet_matches(db, tx["source"]): + for bet_match in ledger.get_pending_bet_matches(db, tx["source"]): bet.cancel_bet_match(db, bet_match, "dropped", tx["block_index"], tx["tx_index"]) cursor.close() return @@ -278,7 +278,7 @@ def parse(db, tx, message): return # Handle bet matches that use this feed. - bet_matches = ledger.ledger.get_pending_bet_matches( + bet_matches = ledger.get_pending_bet_matches( db, tx["source"], order_by="tx1_index ASC, tx0_index ASC" ) for bet_match in bet_matches: @@ -334,7 +334,7 @@ def parse(db, tx, message): bull_credit = escrow_less_fee bear_credit = 0 bet_match_status = "settled: liquidated for bull" - ledger.ledger.credit( + ledger.credit( db, bull_address, config.XCP, @@ -347,7 +347,7 @@ def parse(db, tx, message): bull_credit = 0 bear_credit = escrow_less_fee bet_match_status = "settled: liquidated for bear" - ledger.ledger.credit( + ledger.credit( db, bear_address, config.XCP, @@ -358,7 +358,7 @@ def parse(db, tx, message): ) # Pay fee to feed. - ledger.ledger.credit( + ledger.credit( db, bet_match["feed_address"], config.XCP, @@ -380,16 +380,14 @@ def parse(db, tx, message): "escrow_less_fee": None, "fee": fee, } - ledger.ledger.insert_record( - db, "bet_match_resolutions", bindings, "BET_MATCH_RESOLUTON" - ) + ledger.insert_record(db, "bet_match_resolutions", bindings, "BET_MATCH_RESOLUTON") logger.debug("Bet Match %(bet_match_id)s resolved", bindings) # Settle (if not liquidated). elif timestamp >= bet_match["deadline"]: bet_match_status = "settled" - ledger.ledger.credit( + ledger.credit( db, bull_address, config.XCP, @@ -398,7 +396,7 @@ def parse(db, tx, message): action=f"bet {bet_match_status}", event=tx["tx_hash"], ) - ledger.ledger.credit( + ledger.credit( db, bear_address, config.XCP, @@ -409,7 +407,7 @@ def parse(db, tx, message): ) # Pay fee to feed. - ledger.ledger.credit( + ledger.credit( db, bet_match["feed_address"], config.XCP, @@ -431,9 +429,7 @@ def parse(db, tx, message): "escrow_less_fee": None, "fee": fee, } - ledger.ledger.insert_record( - db, "bet_match_resolutions", bindings, "BET_MATCH_RESOLUTON" - ) + ledger.insert_record(db, "bet_match_resolutions", bindings, "BET_MATCH_RESOLUTON") logger.debug("Bet Match %(bet_match_id)s resolved", bindings) # Equal[/NotEqual] bet. @@ -450,7 +446,7 @@ def parse(db, tx, message): if value == bet_match["target_value"]: winner = "Equal" bet_match_status = "settled: for equal" - ledger.ledger.credit( + ledger.credit( db, equal_address, config.XCP, @@ -462,7 +458,7 @@ def parse(db, tx, message): else: winner = "NotEqual" bet_match_status = "settled: for notequal" - ledger.ledger.credit( + ledger.credit( db, notequal_address, config.XCP, @@ -473,7 +469,7 @@ def parse(db, tx, message): ) # Pay fee to feed. - ledger.ledger.credit( + ledger.credit( db, bet_match["feed_address"], config.XCP, @@ -495,15 +491,13 @@ def parse(db, tx, message): "escrow_less_fee": escrow_less_fee, "fee": fee, } - ledger.ledger.insert_record( - db, "bet_match_resolutions", bindings, "BET_MATCH_RESOLUTON" - ) + ledger.insert_record(db, "bet_match_resolutions", bindings, "BET_MATCH_RESOLUTON") logger.debug("Bet Match %(bet_match_id)s resolved", bindings) # Update the bet match’s status. if bet_match_status: bet_match_id = helpers.make_id(bet_match["tx0_hash"], bet_match["tx1_hash"]) - ledger.ledger.update_bet_match_status(db, bet_match_id, bet_match_status) + ledger.update_bet_match_status(db, bet_match_id, bet_match_status) logger.info( "Bet Match %(id)s updated [%(status)s]", diff --git a/counterparty-core/counterpartycore/lib/messages/btcpay.py b/counterparty-core/counterpartycore/lib/messages/btcpay.py index 93c26fc3d..9465be8bb 100644 --- a/counterparty-core/counterpartycore/lib/messages/btcpay.py +++ b/counterparty-core/counterpartycore/lib/messages/btcpay.py @@ -5,8 +5,8 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import helpers @@ -21,7 +21,7 @@ def validate(db, source, order_match_id, block_index): problems = [] order_match = None - order_matches = ledger.ledger.get_order_match(db, id=order_match_id) + order_matches = ledger.get_order_match(db, id=order_match_id) if len(order_matches) == 0: problems.append(f"no such order match {order_match_id}") return None, None, None, None, order_match, problems @@ -139,7 +139,7 @@ def parse(db, tx, message): # BTC must be paid all at once. if tx["btc_amount"] >= btc_quantity: # Credit source address for the currency that he bought with the bitcoins. - ledger.ledger.credit( + ledger.credit( db, tx["source"], escrowed_asset, @@ -151,19 +151,17 @@ def parse(db, tx, message): status = "valid" # Update order match. - ledger.ledger.update_order_match_status(db, order_match_id, "completed") + ledger.update_order_match_status(db, order_match_id, "completed") # Update give and get order status as filled if order_match is completed if protocol.enabled("btc_order_filled"): - order_matches = ledger.ledger.get_pending_order_matches(db, tx0_hash, tx1_hash) + order_matches = ledger.get_pending_order_matches(db, tx0_hash, tx1_hash) if len(order_matches) == 0: # mark both btc get and give orders as filled when order_match is completed and give or get remaining = 0 - ledger.ledger.mark_order_as_filled(db, tx0_hash, tx1_hash) + ledger.mark_order_as_filled(db, tx0_hash, tx1_hash) else: # always mark btc get order as filled when order_match is completed and give or get remaining = 0 - ledger.ledger.mark_order_as_filled( - db, tx0_hash, tx1_hash, source=tx["destination"] - ) + ledger.mark_order_as_filled(db, tx0_hash, tx1_hash, source=tx["destination"]) # Add parsed transaction to message-type–specific table. bindings = { @@ -177,7 +175,7 @@ def parse(db, tx, message): "status": status, } if "integer overflow" not in status: - ledger.ledger.insert_record(db, "btcpays", bindings, "BTC_PAY") + ledger.insert_record(db, "btcpays", bindings, "BTC_PAY") logger.info("BTC Pay for order match %(order_match_id)s (%(tx_hash)s) [%(status)s]", bindings) cursor.close() diff --git a/counterparty-core/counterpartycore/lib/messages/burn.py b/counterparty-core/counterpartycore/lib/messages/burn.py index 434e9b0e8..5ac31117d 100644 --- a/counterparty-core/counterpartycore/lib/messages/burn.py +++ b/counterparty-core/counterpartycore/lib/messages/burn.py @@ -4,7 +4,8 @@ import os from fractions import Fraction -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import protocol @@ -57,7 +58,7 @@ def compose(db, source: str, quantity: int, overburn: bool = False, skip_validat raise exceptions.ComposeError(problems) # Check that a maximum of 1 BTC total is burned per address. - burns = ledger.ledger.get_burns(db, source) + burns = ledger.get_burns(db, source) already_burned = sum([burn["burned"] for burn in burns]) if quantity > (1 * config.UNIT - already_burned) and not overburn: @@ -93,7 +94,7 @@ def parse(db, tx, message=None): if status == "valid": # Calculate quantity of XCP earned. (Maximum 1 BTC in total, ever.) - burns = ledger.ledger.get_burns(db, tx["source"]) + burns = ledger.get_burns(db, tx["source"]) already_burned = sum([burn["burned"] for burn in burns]) one = 1 * config.UNIT max_burn = one - already_burned @@ -108,7 +109,7 @@ def parse(db, tx, message=None): earned = round(burned * multiplier) # Credit source address with earned XCP. - ledger.ledger.credit( + ledger.credit( db, tx["source"], config.XCP, @@ -134,7 +135,7 @@ def parse(db, tx, message=None): except KeyError: return - ledger.ledger.credit( + ledger.credit( db, line["source"], config.XCP, @@ -164,7 +165,7 @@ def parse(db, tx, message=None): "status": status, } if "integer overflow" not in status: - ledger.ledger.insert_record(db, "burns", bindings, "BURN") + ledger.insert_record(db, "burns", bindings, "BURN") logger.info( "%(source)s burned %(burned)s BTC for %(earned)s XCP (%(tx_hash)s) [%(status)s]", bindings diff --git a/counterparty-core/counterpartycore/lib/messages/cancel.py b/counterparty-core/counterpartycore/lib/messages/cancel.py index 324089902..2d716e02e 100644 --- a/counterparty-core/counterpartycore/lib/messages/cancel.py +++ b/counterparty-core/counterpartycore/lib/messages/cancel.py @@ -6,7 +6,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.parser import messagetype from . import bet, order @@ -22,8 +23,8 @@ def validate(db, source, offer_hash): problems = [] # TODO: make query only if necessary - orders = ledger.ledger.get_order(db, order_hash=offer_hash) - bets = ledger.ledger.get_bet(db, bet_hash=offer_hash) + orders = ledger.get_order(db, order_hash=offer_hash) + bets = ledger.get_bet(db, bet_hash=offer_hash) offer_type = None if orders: @@ -108,7 +109,7 @@ def parse(db, tx, message): } if "integer overflow" not in status: event_name = f"CANCEL_{offer_type.upper()}" if offer_type else "INVALID_CANCEL" - ledger.ledger.insert_record(db, "cancels", bindings, event_name) + ledger.insert_record(db, "cancels", bindings, event_name) log_data = bindings | { "offer_type": offer_type.capitalize() if offer_type else "Invalid", diff --git a/counterparty-core/counterpartycore/lib/messages/destroy.py b/counterparty-core/counterpartycore/lib/messages/destroy.py index 2eb220a90..4453df2d6 100644 --- a/counterparty-core/counterpartycore/lib/messages/destroy.py +++ b/counterparty-core/counterpartycore/lib/messages/destroy.py @@ -3,9 +3,10 @@ import logging import struct -from counterpartycore.lib import config, ledger +from counterpartycore.lib import config from counterpartycore.lib.exceptions import * # noqa: F403 from counterpartycore.lib.exceptions import AddressError +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype from counterpartycore.lib.utils import address @@ -29,7 +30,7 @@ def pack(db, asset, quantity, tag): data += struct.pack( FORMAT, - ledger.ledger.get_asset_id(db, asset, CurrentState().current_block_index()), + ledger.get_asset_id(db, asset, CurrentState().current_block_index()), quantity, ) data += tag @@ -40,7 +41,7 @@ def unpack(db, message, return_dict=False): try: asset_id, quantity = struct.unpack(FORMAT, message[0:16]) tag = message[16:] - asset = ledger.ledger.get_asset_name(db, asset_id, CurrentState().current_block_index()) + asset = ledger.get_asset_name(db, asset_id, CurrentState().current_block_index()) except struct.error: raise UnpackError("could not unpack") # noqa: B904, F405 @@ -55,7 +56,7 @@ def unpack(db, message, return_dict=False): def validate(db, source, destination, asset, quantity): try: - ledger.ledger.get_asset_id(db, asset, CurrentState().current_block_index()) + ledger.get_asset_id(db, asset, CurrentState().current_block_index()) except AssetError: # noqa: F405 raise ValidateError("asset invalid") # noqa: B904, F405 @@ -79,13 +80,13 @@ def validate(db, source, destination, asset, quantity): if quantity < 0: raise ValidateError("quantity negative") # noqa: F405 - if ledger.ledger.get_balance(db, source, asset) < quantity: + if ledger.get_balance(db, source, asset) < quantity: raise BalanceError("balance insufficient") # noqa: F405 def compose(db, source: str, asset: str, quantity: int, tag: str, skip_validation: bool = False): # resolve subassets - asset = ledger.ledger.resolve_subasset_longname(db, asset) + asset = ledger.resolve_subasset_longname(db, asset) if not skip_validation: validate(db, source, None, asset, quantity) @@ -102,9 +103,7 @@ def parse(db, tx, message): try: asset, quantity, tag = unpack(db, message) validate(db, tx["source"], tx["destination"], asset, quantity) - ledger.ledger.debit( - db, tx["source"], asset, quantity, tx["tx_index"], "destroy", tx["tx_hash"] - ) + ledger.debit(db, tx["source"], asset, quantity, tx["tx_index"], "destroy", tx["tx_hash"]) except UnpackError as e: # noqa: F405 status = "invalid: " + "".join(e.args) @@ -123,7 +122,7 @@ def parse(db, tx, message): "status": status, } if "integer overflow" not in status: - ledger.ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") logger.info( "Destroy of %(quantity)s %(asset)s by %(source)s (%(tx_hash)s) [%(status)s]", bindings diff --git a/counterparty-core/counterpartycore/lib/messages/detach.py b/counterparty-core/counterpartycore/lib/messages/detach.py index edc7e2551..7b0f13ed1 100644 --- a/counterparty-core/counterpartycore/lib/messages/detach.py +++ b/counterparty-core/counterpartycore/lib/messages/detach.py @@ -1,7 +1,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.parser import utxosinfo from counterpartycore.lib.utils import address @@ -70,17 +71,17 @@ def detach_assets(db, tx, source, destination=None): bindings = { "tx_index": tx["tx_index"], "tx_hash": tx["tx_hash"], - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]), "block_index": tx["block_index"], "status": status, "send_type": "detach", } - ledger.ledger.insert_record(db, "sends", bindings, "DETACH_FROM_UTXO") + ledger.insert_record(db, "sends", bindings, "DETACH_FROM_UTXO") # stop here to avoid further processing return # we detach all the assets from the source UTXO - balances = ledger.ledger.get_utxo_balances(db, source) + balances = ledger.get_utxo_balances(db, source) for balance in balances: if balance["quantity"] == 0: continue @@ -99,7 +100,7 @@ def detach_assets(db, tx, source, destination=None): if detach_destination is None: detach_destination = balance["utxo_address"] - source_address = ledger.ledger.debit( + source_address = ledger.debit( db, source, balance["asset"], @@ -108,7 +109,7 @@ def detach_assets(db, tx, source, destination=None): action=action, event=tx["tx_hash"], ) - ledger.ledger.credit( + ledger.credit( db, detach_destination, balance["asset"], @@ -120,7 +121,7 @@ def detach_assets(db, tx, source, destination=None): bindings = { "tx_index": tx["tx_index"], "tx_hash": tx["tx_hash"], - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]), "block_index": tx["block_index"], "status": "valid", "source": source, @@ -131,7 +132,7 @@ def detach_assets(db, tx, source, destination=None): "fee_paid": 0, "send_type": "detach", } - ledger.ledger.insert_record(db, "sends", bindings, "DETACH_FROM_UTXO") + ledger.insert_record(db, "sends", bindings, "DETACH_FROM_UTXO") logger.info( "Detach assets from %(source)s (%(tx_hash)s) [%(status)s]", diff --git a/counterparty-core/counterpartycore/lib/messages/dispense.py b/counterparty-core/counterpartycore/lib/messages/dispense.py index adc2b8104..19e457b38 100644 --- a/counterparty-core/counterpartycore/lib/messages/dispense.py +++ b/counterparty-core/counterpartycore/lib/messages/dispense.py @@ -2,7 +2,8 @@ import struct from math import floor -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import dispenser as dispenser_module from counterpartycore.lib.parser import protocol @@ -15,10 +16,8 @@ def get_must_give(db, dispenser, btc_amount, block_index=None): if (dispenser["oracle_address"] is not None) and protocol.enabled( # noqa: E711 "oracle_dispensers", block_index ): - last_price, _last_fee, _last_fiat_label, _last_updated = ( - ledger.ledger.get_oracle_last_price( - db, dispenser["oracle_address"], block_index or CurrentState().current_block_index() - ) + last_price, _last_fee, _last_fiat_label, _last_updated = ledger.get_oracle_last_price( + db, dispenser["oracle_address"], block_index or CurrentState().current_block_index() ) if last_price is None: raise exceptions.NoPriceError( @@ -40,7 +39,7 @@ def validate_compose(db, source, destination, quantity): if source == destination: raise exceptions.ComposeError("source and destination must be different") - dispensers = ledger.ledger.get_dispensers(db, address=destination) + dispensers = ledger.get_dispensers(db, address=destination) if len(dispensers) == 0: problems.append("address doesn't have any open dispenser") return problems @@ -94,7 +93,7 @@ def parse(db, tx): outs = [] if protocol.enabled("multiple_dispenses"): - outs = ledger.ledger.get_vouts(db, tx["tx_hash"]) + outs = ledger.get_vouts(db, tx["tx_hash"]) else: outs = [tx] @@ -108,7 +107,7 @@ def parse(db, tx): for next_out in outs: dispensers = [] if next_out["destination"] is not None: - dispensers = ledger.ledger.get_dispensers( + dispensers = ledger.get_dispensers( db, address=next_out["destination"], status_in=[0, 11], order_by="asset" ) @@ -130,7 +129,7 @@ def parse(db, tx): if protocol.enabled("zero_quantity_value_adjustment_1") and actually_given == 0: continue - ledger.ledger.credit( + ledger.credit( db, next_out["source"], dispenser["asset"], @@ -162,7 +161,7 @@ def parse(db, tx): dispenser["closing_reason"] = "no_more_to_give" # return the remaining to the owner - ledger.ledger.credit( + ledger.credit( db, dispenser["source"], dispenser["asset"], @@ -183,7 +182,7 @@ def parse(db, tx): "status": dispenser["status"], "dispense_count": dispenser["dispense_count"] + 1, } - ledger.ledger.update_dispenser( + ledger.update_dispenser( db, dispenser["rowid"], set_data, @@ -206,7 +205,7 @@ def parse(db, tx): "dispenser_tx_hash": dispenser["tx_hash"], "btc_amount": next_out["btc_amount"], } - ledger.ledger.insert_record(db, "dispenses", bindings, "DISPENSE") + ledger.insert_record(db, "dispenses", bindings, "DISPENSE") dispense_index += 1 logger.info( diff --git a/counterparty-core/counterpartycore/lib/messages/dispenser.py b/counterparty-core/counterpartycore/lib/messages/dispenser.py index 4132229b7..7b1786e0e 100644 --- a/counterparty-core/counterpartycore/lib/messages/dispenser.py +++ b/counterparty-core/counterpartycore/lib/messages/dispenser.py @@ -13,8 +13,8 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import helpers @@ -68,7 +68,7 @@ def validate( return None, problems # resolve subassets - asset = ledger.ledger.resolve_subasset_longname(db, asset) + asset = ledger.resolve_subasset_longname(db, asset) if status == STATUS_OPEN or status == STATUS_OPEN_EMPTY_ADDRESS: if give_quantity <= 0: @@ -81,7 +81,7 @@ def validate( problems.append(f"invalid status {status}") cursor = db.cursor() - available = ledger.ledger.get_balance(db, source, asset, return_list=True) + available = ledger.get_balance(db, source, asset, return_list=True) if len(available) == 0: problems.append(f"address doesn't have the asset {asset}") @@ -95,7 +95,7 @@ def validate( and source != open_address and status != STATUS_CLOSED and len( - ledger.ledger.get_dispensers( + ledger.get_dispensers( db, status_in=[0, 11], address=open_address if status == STATUS_OPEN_EMPTY_ADDRESS else source, @@ -125,12 +125,12 @@ def validate( and open_address and open_address != source ): - open_dispensers = ledger.ledger.get_dispensers( + open_dispensers = ledger.get_dispensers( db, status_in=[0, 11], address=open_address, asset=asset, origin=source ) else: query_address = open_address if status == STATUS_OPEN_EMPTY_ADDRESS else source - open_dispensers = ledger.ledger.get_dispensers( + open_dispensers = ledger.get_dispensers( db, status_in=[0, 11], address=query_address, asset=asset ) @@ -140,7 +140,7 @@ def validate( max_refills = protocol.get_value_by_block_index("max_refills", block_index) refilling_count = 0 if max_refills > 0: - refilling_count = ledger.ledger.get_refilling_count( + refilling_count = ledger.get_refilling_count( db, dispenser_tx_hash=open_dispensers[0]["tx_hash"] ) @@ -171,7 +171,7 @@ def validate( and (len(open_dispensers) > 0) and (open_dispensers[0]["origin"] == source) ): - dispensers_from_same_origin_count = ledger.ledger.get_dispensers_count( + dispensers_from_same_origin_count = ledger.get_dispensers_count( db, source=query_address, status=STATUS_CLOSED, origin=source ) @@ -180,7 +180,7 @@ def validate( and dispensers_from_same_origin_count > 0 ): # It means that the same origin has not opened other dispensers in this address - existing_balances = ledger.ledger.get_balances_count(db, query_address) + existing_balances = ledger.get_balances_count(db, query_address) if existing_balances[0]["cnt"] > 0: problems.append( @@ -201,7 +201,7 @@ def validate( ) if len(problems) == 0: - asset_id = ledger.ledger.generate_asset_id(asset, block_index) + asset_id = ledger.generate_asset_id(asset, block_index) if asset_id == 0: problems.append( f"cannot dispense {asset}" @@ -214,7 +214,7 @@ def validate( cursor.close() if oracle_address is not None and protocol.enabled("oracle_dispensers", block_index): - last_price, last_fee, last_label, last_updated = ledger.ledger.get_oracle_last_price( + last_price, last_fee, last_label, last_updated = ledger.get_oracle_last_price( db, oracle_address, block_index ) @@ -264,7 +264,7 @@ def compose( if not skip_validation: raise exceptions.ComposeError(problems) else: - assetid = ledger.ledger.generate_asset_id( + assetid = ledger.generate_asset_id( asset, block_index=CurrentState().current_block_index() ) @@ -298,7 +298,7 @@ def compose( def calculate_oracle_fee( db, escrow_quantity, give_quantity, mainchainrate, oracle_address, block_index ): - last_price, last_fee, last_fiat_label, last_updated = ledger.ledger.get_oracle_last_price( + last_price, last_fee, last_fiat_label, last_updated = ledger.get_oracle_last_price( db, oracle_address, block_index ) last_fee_multiplier = last_fee / config.UNIT @@ -332,7 +332,7 @@ def unpack(message, return_dict=False): read = LENGTH + 21 if len(message) > read: oracle_address = address_unpack(message[read : read + 21]) - asset = ledger.ledger.generate_asset_name(assetid, CurrentState().current_block_index()) + asset = ledger.generate_asset_name(assetid, CurrentState().current_block_index()) status = "valid" except (exceptions.UnpackError, struct.error) as e: # noqa: F841 ( @@ -409,7 +409,7 @@ def parse(db, tx, message): status = "invalid: " + "; ".join(problems) else: if dispenser_status == STATUS_OPEN or dispenser_status == STATUS_OPEN_EMPTY_ADDRESS: - existing = ledger.ledger.get_dispensers( + existing = ledger.get_dispensers( db, address=action_address, asset=asset, status=STATUS_OPEN ) @@ -435,12 +435,10 @@ def parse(db, tx, message): try: if dispenser_status == STATUS_OPEN_EMPTY_ADDRESS: is_empty_address = True - address_assets = ledger.ledger.get_address_assets( - db, action_address - ) + address_assets = ledger.get_address_assets(db, action_address) if len(address_assets) > 0: for asset_name in address_assets: - asset_balance = ledger.ledger.get_balance( + asset_balance = ledger.get_balance( db, action_address, asset_name["asset"] ) if asset_balance > 0: @@ -448,7 +446,7 @@ def parse(db, tx, message): break if is_empty_address: - ledger.ledger.debit( + ledger.debit( db, tx["source"], asset, @@ -457,7 +455,7 @@ def parse(db, tx, message): action="open dispenser empty addr", event=tx["tx_hash"], ) - ledger.ledger.credit( + ledger.credit( db, action_address, asset, @@ -466,7 +464,7 @@ def parse(db, tx, message): action="open dispenser empty addr", event=tx["tx_hash"], ) - ledger.ledger.debit( + ledger.debit( db, action_address, asset, @@ -478,7 +476,7 @@ def parse(db, tx, message): else: status = "invalid: address not empty" else: - ledger.ledger.debit( + ledger.debit( db, tx["source"], asset, @@ -487,7 +485,7 @@ def parse(db, tx, message): action="open dispenser", event=tx["tx_hash"], ) - except ledger.ledger.DebitError as e: # noqa: F841 + except ledger.DebitError as e: # noqa: F841 status = "invalid: insufficient funds" if status == "valid": @@ -510,7 +508,7 @@ def parse(db, tx, message): if protocol.enabled("dispenser_origin_permission_extended"): bindings["origin"] = tx["source"] - ledger.ledger.insert_record(db, "dispensers", bindings, "OPEN_DISPENSER") + ledger.insert_record(db, "dispensers", bindings, "OPEN_DISPENSER") # Add the address to the dispensable cache if not CurrentState().parsing_mempool(): DispensableCache(db).new_dispensable(action_address) @@ -551,7 +549,7 @@ def parse(db, tx, message): if status == "valid": # Refill the dispenser by the given amount try: - ledger.ledger.debit( + ledger.debit( db, tx["source"], asset, @@ -566,7 +564,7 @@ def parse(db, tx, message): + escrow_quantity, "dispense_count": 0, # reset the dispense count on refill } - ledger.ledger.update_dispenser( + ledger.update_dispenser( db, existing[0]["rowid"], set_data, @@ -583,7 +581,7 @@ def parse(db, tx, message): }, ) - dispenser_tx_hash = ledger.ledger.get_dispensers( + dispenser_tx_hash = ledger.get_dispensers( db, address=action_address, asset=asset, status=STATUS_OPEN )[0]["tx_hash"] bindings_refill = { @@ -596,7 +594,7 @@ def parse(db, tx, message): "dispense_quantity": escrow_quantity, "dispenser_tx_hash": dispenser_tx_hash, } - ledger.ledger.insert_record( + ledger.insert_record( db, "dispenser_refills", bindings_refill, "REFILL_DISPENSER" ) @@ -604,7 +602,7 @@ def parse(db, tx, message): "Refilled dispenser for %(asset)s at %(source)s (%(tx_hash)s) [valid]", bindings_refill, ) - except ledger.ledger.DebitError: + except ledger.DebitError: status = "insufficient funds" else: status = "invalid: can only refill dispenser from source or origin" @@ -622,7 +620,7 @@ def parse(db, tx, message): ) existing = [] if close_from_another_address: - existing = ledger.ledger.get_dispensers( + existing = ledger.get_dispensers( db, address=action_address, asset=asset, @@ -630,12 +628,12 @@ def parse(db, tx, message): origin=tx["source"], ) else: - existing = ledger.ledger.get_dispensers( + existing = ledger.get_dispensers( db, address=tx["source"], asset=asset, status=STATUS_OPEN ) if len(existing) == 1: if close_delay == 0: - ledger.ledger.credit( + ledger.credit( db, tx["source"], asset, @@ -657,7 +655,7 @@ def parse(db, tx, message): "close_block_index": tx["block_index"] + close_delay, } - ledger.ledger.update_dispenser( + ledger.update_dispenser( db, existing[0]["rowid"], set_data, @@ -700,7 +698,7 @@ def parse(db, tx, message): class DispensableCache(metaclass=helpers.SingletonMeta): def __init__(self, db): logger.debug("Initialising Dispensable Cache...") - self.dispensable = ledger.ledger.get_all_dispensables(db) + self.dispensable = ledger.get_all_dispensables(db) def could_be_dispensable(self, source): return self.dispensable.get(source, False) @@ -716,14 +714,12 @@ def is_dispensable(db, address, amount): if not DispensableCache(db).could_be_dispensable(address): return False - dispensers = ledger.ledger.get_dispensers(db, address=address, status_in=[0, 11]) + dispensers = ledger.get_dispensers(db, address=address, status_in=[0, 11]) for next_dispenser in dispensers: if next_dispenser["oracle_address"] != None: # noqa: E711 - last_price, last_fee, last_fiat_label, last_updated = ( - ledger.ledger.get_oracle_last_price( - db, next_dispenser["oracle_address"], CurrentState().current_block_index() - ) + last_price, last_fee, last_fiat_label, last_updated = ledger.get_oracle_last_price( + db, next_dispenser["oracle_address"], CurrentState().current_block_index() ) fiatrate = helpers.satoshirate_to_fiat(next_dispenser["satoshirate"]) if fiatrate == 0 or last_price == 0: @@ -741,11 +737,11 @@ def close_pending(db, block_index): block_delay = protocol.get_value_by_block_index("dispenser_close_delay", block_index) if block_delay > 0: - pending_dispensers = ledger.ledger.get_pending_dispensers(db, block_index=block_index) + pending_dispensers = ledger.get_pending_dispensers(db, block_index=block_index) for dispenser in pending_dispensers: # use tx_index=0 for block actions - ledger.ledger.credit( + ledger.credit( db, dispenser["last_status_tx_source"], dispenser["asset"], @@ -759,7 +755,7 @@ def close_pending(db, block_index): "give_remaining": 0, "status": STATUS_CLOSED, } - ledger.ledger.update_dispenser( + ledger.update_dispenser( db, dispenser["rowid"], set_data, diff --git a/counterparty-core/counterpartycore/lib/messages/dividend.py b/counterparty-core/counterpartycore/lib/messages/dividend.py index 37259c270..e23586088 100644 --- a/counterparty-core/counterpartycore/lib/messages/dividend.py +++ b/counterparty-core/counterpartycore/lib/messages/dividend.py @@ -7,8 +7,8 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol @@ -44,21 +44,21 @@ def validate(db, source, quantity_per_unit, asset, dividend_asset, block_index): # Examine asset. try: - divisible = ledger.ledger.is_divisible(db, asset) + divisible = ledger.is_divisible(db, asset) except exceptions.AssetError: problems.append(f"no such asset, {asset}.") return None, None, problems, 0 # Only issuer can pay dividends. if protocol.after_block_or_test_network(block_index, 320000): # Protocol change. - issuer = ledger.ledger.get_asset_issuer(db, asset) + issuer = ledger.get_asset_issuer(db, asset) if issuer != source: problems.append("only issuer can pay dividends") # Examine dividend asset. try: - dividend_divisible = ledger.ledger.is_divisible(db, dividend_asset) + dividend_divisible = ledger.is_divisible(db, dividend_asset) except exceptions.AssetError: problems.append(f"no such dividend asset, {dividend_asset}.") return None, None, problems, 0 @@ -67,7 +67,7 @@ def validate(db, source, quantity_per_unit, asset, dividend_asset, block_index): exclude_empty = False if protocol.enabled("zero_quantity_value_adjustment_1"): exclude_empty = True - holders = ledger.ledger.holders(db, asset, exclude_empty, block_index=block_index) + holders = ledger.holders(db, asset, exclude_empty, block_index=block_index) outputs = [] addresses = [] @@ -109,7 +109,7 @@ def validate(db, source, quantity_per_unit, asset, dividend_asset, block_index): problems.append("zero dividend") if dividend_asset != config.BTC: - dividend_balances = ledger.ledger.get_balance(db, source, dividend_asset) + dividend_balances = ledger.get_balance(db, source, dividend_asset) if dividend_balances < dividend_total: problems.append(f"insufficient funds ({dividend_asset})") @@ -119,7 +119,7 @@ def validate(db, source, quantity_per_unit, asset, dividend_asset, block_index): if protocol.after_block_or_test_network(block_index, 330000): # Protocol change. fee = int(0.0002 * config.UNIT * holder_count) if fee: - balance = ledger.ledger.get_balance(db, source, config.XCP) + balance = ledger.get_balance(db, source, config.XCP) if balance < fee: problems.append(f"insufficient funds ({config.XCP})") @@ -146,7 +146,7 @@ def validate(db, source, quantity_per_unit, asset, dividend_asset, block_index): def get_estimate_xcp_fee(db, asset, block_index): - holders = ledger.ledger.holders(db, asset, True, block_index=block_index) + holders = ledger.holders(db, asset, True, block_index=block_index) addresses = [holder["address"] for holder in holders] holder_count = len(set(addresses)) return int(0.0002 * config.UNIT * holder_count) @@ -161,8 +161,8 @@ def compose( skip_validation: bool = False, ): # resolve subassets - asset = ledger.ledger.resolve_subasset_longname(db, asset) - dividend_asset = ledger.ledger.resolve_subasset_longname(db, dividend_asset) + asset = ledger.resolve_subasset_longname(db, asset) + dividend_asset = ledger.resolve_subasset_longname(db, dividend_asset) dividend_total, outputs, problems, fee = validate( db, source, quantity_per_unit, asset, dividend_asset, CurrentState().current_block_index() @@ -170,7 +170,7 @@ def compose( if problems and not skip_validation: raise exceptions.ComposeError(problems) logger.info( - f"Total quantity to be distributed in dividends: {ledger.ledger.value_out(db, dividend_total, dividend_asset)} {dividend_asset}" + f"Total quantity to be distributed in dividends: {ledger.value_out(db, dividend_total, dividend_asset)} {dividend_asset}" ) if dividend_asset == config.BTC: @@ -180,8 +180,8 @@ def compose( None, ) - asset_id = ledger.ledger.get_asset_id(db, asset, CurrentState().current_block_index()) - dividend_asset_id = ledger.ledger.get_asset_id( + asset_id = ledger.get_asset_id(db, asset, CurrentState().current_block_index()) + dividend_asset_id = ledger.get_asset_id( db, dividend_asset, CurrentState().current_block_index() ) data = messagetype.pack(ID) @@ -193,12 +193,12 @@ def unpack(db, message, block_index, return_dict=False): try: if protocol.after_block_or_test_network(block_index, 288151) and len(message) == LENGTH_2: quantity_per_unit, asset_id, dividend_asset_id = struct.unpack(FORMAT_2, message) - asset = ledger.ledger.get_asset_name(db, asset_id, block_index) - dividend_asset = ledger.ledger.get_asset_name(db, dividend_asset_id, block_index) + asset = ledger.get_asset_name(db, asset_id, block_index) + dividend_asset = ledger.get_asset_name(db, dividend_asset_id, block_index) status = "valid" elif len(message) == LENGTH_1: quantity_per_unit, asset_id = struct.unpack(FORMAT_1, message) - asset = ledger.ledger.get_asset_name(db, asset_id, block_index) + asset = ledger.get_asset_name(db, asset_id, block_index) dividend_asset = config.XCP status = "valid" else: @@ -245,7 +245,7 @@ def parse(db, tx, message): if status == "valid": # Debit. - ledger.ledger.debit( + ledger.debit( db, tx["source"], dividend_asset, @@ -255,7 +255,7 @@ def parse(db, tx, message): event=tx["tx_hash"], ) if protocol.after_block_or_test_network(tx["block_index"], 330000): # Protocol change. - ledger.ledger.debit( + ledger.debit( db, tx["source"], config.XCP, @@ -268,7 +268,7 @@ def parse(db, tx, message): # Credit. for output in outputs: if not protocol.enabled("dont_credit_zero_dividend") or output["dividend_quantity"] > 0: - ledger.ledger.credit( + ledger.credit( db, output["address"], dividend_asset, @@ -292,7 +292,7 @@ def parse(db, tx, message): } if "integer overflow" not in status: - ledger.ledger.insert_record(db, "dividends", bindings, "ASSET_DIVIDEND") + ledger.insert_record(db, "dividends", bindings, "ASSET_DIVIDEND") logger.info( "Dividend of %(quantity_per_unit)s %(dividend_asset)s per unit of %(asset)s (%(tx_hash)s) [%(status)s]", diff --git a/counterparty-core/counterpartycore/lib/messages/fairmint.py b/counterparty-core/counterpartycore/lib/messages/fairmint.py index bdda3a6f3..5e6d2ca89 100644 --- a/counterparty-core/counterpartycore/lib/messages/fairmint.py +++ b/counterparty-core/counterpartycore/lib/messages/fairmint.py @@ -3,7 +3,8 @@ import math import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.messages import fairminter as fairminter_mod from counterpartycore.lib.parser import protocol @@ -27,7 +28,7 @@ def validate( if not isinstance(quantity, int): problems.append("quantity must be an integer") - fairminter = ledger.ledger.get_fairminter_by_asset(db, asset) + fairminter = ledger.get_fairminter_by_asset(db, asset) if not fairminter: problems.append(f"fairminter not found for asset: `{asset}`") return problems @@ -35,7 +36,7 @@ def validate( if fairminter["status"] != "open": problems.append(f"fairminter is not open for asset: `{asset}`") - asset_supply = ledger.ledger.asset_supply(db, fairminter["asset"]) + asset_supply = ledger.asset_supply(db, fairminter["asset"]) if fairminter["price"] > 0: # if the fairminter is not free the quantity is mandatory @@ -56,7 +57,7 @@ def validate( fairminter["price"] ) xcp_total_price = int(math.ceil(xcp_total_price)) - balance = ledger.ledger.get_balance(db, source, config.XCP) + balance = ledger.get_balance(db, source, config.XCP) if balance < xcp_total_price: problems.append("insufficient XCP balance") elif not protocol.enabled("partial_mint_to_reach_hard_cap"): @@ -76,7 +77,7 @@ def compose(db, source: str, asset: str, quantity: int = 0, skip_validation: boo raise exceptions.ComposeError(problems) if quantity != 0: - fairminter = ledger.ledger.get_fairminter_by_asset(db, asset) + fairminter = ledger.get_fairminter_by_asset(db, asset) if fairminter["price"] == 0: raise exceptions.ComposeError("quantity is not allowed for free fairminters") @@ -124,12 +125,12 @@ def parse(db, tx, message): "source": tx["source"], "status": status, } - ledger.ledger.insert_record(db, "fairmints", bindings, "NEW_FAIRMINT") + ledger.insert_record(db, "fairmints", bindings, "NEW_FAIRMINT") logger.info(f"Fairmint {tx['tx_hash']} is invalid: {status}") return # get corresponding fairminter - fairminter = ledger.ledger.get_fairminter_by_asset(db, asset) + fairminter = ledger.get_fairminter_by_asset(db, asset) # determine if the soft cap has been reached soft_cap_not_reached = ( @@ -165,7 +166,7 @@ def parse(db, tx, message): else: paid_quantity = 0 if protocol.enabled("partial_mint_to_reach_hard_cap"): - asset_supply = ledger.ledger.asset_supply(db, fairminter["asset"]) + asset_supply = ledger.asset_supply(db, fairminter["asset"]) if ( fairminter["hard_cap"] > 0 and asset_supply + fairminter["max_mint_per_tx"] > fairminter["hard_cap"] @@ -185,12 +186,12 @@ def parse(db, tx, message): if paid_quantity > 0: # we debit the user - ledger.ledger.debit( + ledger.debit( db, tx["source"], config.XCP, paid_quantity, tx["tx_index"], xcp_action, tx["tx_hash"] ) if xcp_destination: # we credit the destination if it exists (issuer or escrow) - ledger.ledger.credit( + ledger.credit( db, xcp_destination, config.XCP, @@ -211,11 +212,11 @@ def parse(db, tx, message): "tag": xcp_action, "status": "valid", } - ledger.ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") if asset_destination == config.UNSPENDABLE: # the minted amount and commission are escrowed - ledger.ledger.credit( + ledger.credit( db, asset_destination, asset, @@ -226,12 +227,12 @@ def parse(db, tx, message): ) else: # the minted amount is sent to the user - ledger.ledger.credit( + ledger.credit( db, asset_destination, asset, earn_quantity, tx["tx_index"], asset_action, tx["tx_hash"] ) if commission > 0: # the commission is sent to the issuer - ledger.ledger.credit( + ledger.credit( db, fairminter["source"], asset, @@ -254,10 +255,10 @@ def parse(db, tx, message): "commission": commission, "status": status, } - ledger.ledger.insert_record(db, "fairmints", bindings, "NEW_FAIRMINT") + ledger.insert_record(db, "fairmints", bindings, "NEW_FAIRMINT") # we prepare the new issuance - last_issuance = ledger.ledger.get_last_issuance(db, asset) + last_issuance = ledger.get_last_issuance(db, asset) bindings = last_issuance | { "tx_index": tx["tx_index"], "tx_hash": tx["tx_hash"], @@ -271,7 +272,7 @@ def parse(db, tx, message): # we check if the hard cap is reached and in this case... if fairminter["hard_cap"] > 0: - asset_supply = ledger.ledger.asset_supply(db, fairminter["asset"]) + asset_supply = ledger.asset_supply(db, fairminter["asset"]) alredy_minted = asset_supply + earn_quantity + commission if alredy_minted == fairminter["hard_cap"]: # ...we unlock the issuances for this assets @@ -287,10 +288,10 @@ def parse(db, tx, message): and fairminter["soft_cap_deadline_block"] >= tx["block_index"] ): fairminter_mod.soft_cap_deadline_reached(db, fairminter, tx["block_index"]) - ledger.ledger.update_fairminter(db, fairminter["tx_hash"], {"status": "closed"}) + ledger.update_fairminter(db, fairminter["tx_hash"], {"status": "closed"}) # we insert the new issuance - ledger.ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE") + ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE") # log logger.info( diff --git a/counterparty-core/counterpartycore/lib/messages/fairminter.py b/counterparty-core/counterpartycore/lib/messages/fairminter.py index 37feeb9e2..a8570917e 100644 --- a/counterparty-core/counterpartycore/lib/messages/fairminter.py +++ b/counterparty-core/counterpartycore/lib/messages/fairminter.py @@ -2,7 +2,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import protocol from counterpartycore.lib.utils import assetnames @@ -77,13 +78,13 @@ def validate( # check asset name format try: - ledger.ledger.generate_asset_id(asset, CurrentState().current_block_index()) + ledger.generate_asset_id(asset, CurrentState().current_block_index()) if asset_parent != "": - ledger.ledger.generate_asset_id(asset_parent, CurrentState().current_block_index()) + ledger.generate_asset_id(asset_parent, CurrentState().current_block_index()) except exceptions.AssetNameError as e: problems.append(f"Invalid asset name: {e}") - existing_asset = ledger.ledger.get_asset(db, asset) + existing_asset = ledger.get_asset(db, asset) if existing_asset and existing_asset["asset_longname"] and asset_parent == "": asset_parent, asset = existing_asset["asset_longname"].split(".") @@ -91,7 +92,7 @@ def validate( asset_name = asset if asset_parent != "": asset_name = f"{asset_parent}.{asset}" - existing_asset = ledger.ledger.get_asset(db, asset_name) + existing_asset = ledger.get_asset(db, asset_name) if existing_asset: # check if a fair minter is already opened for this asset @@ -126,12 +127,12 @@ def validate( if existing_asset is None: if asset_parent != "": # if the asset does not exist its parent must exist - existing_parent = ledger.ledger.get_asset(db, asset_parent) + existing_parent = ledger.get_asset(db, asset_parent) if existing_parent is None: problems.append("Asset parent does not exist") elif not asset.startswith("A"): fee = 0.5 * config.UNIT - balance = ledger.ledger.get_balance(db, source, config.XCP) + balance = ledger.get_balance(db, source, config.XCP) if balance < fee: problems.append("insufficient XCP balance to pay fee") @@ -366,7 +367,7 @@ def parse(db, tx, message): "source": tx["source"], "status": status, } - ledger.ledger.insert_record(db, "fairminters", bindings, "NEW_FAIRMINTER") + ledger.insert_record(db, "fairminters", bindings, "NEW_FAIRMINTER") logger.info(f"Fair minter {tx['tx_hash']} is invalid: {status}") return @@ -377,7 +378,7 @@ def parse(db, tx, message): if end_block > 0 and tx["block_index"] > end_block: status = "closed" - existing_asset = ledger.ledger.get_asset(db, asset) + existing_asset = ledger.get_asset(db, asset) if existing_asset and existing_asset["asset_longname"] and asset_parent == "": asset_parent, asset = existing_asset["asset_longname"].split(".") @@ -386,7 +387,7 @@ def parse(db, tx, message): if asset_parent != "": asset_longname = f"{asset_parent}.{asset}" - existing_asset = ledger.ledger.get_asset(db, asset_longname if asset_longname != "" else asset) + existing_asset = ledger.get_asset(db, asset_longname if asset_longname != "" else asset) fee = 0 asset_name = asset @@ -439,19 +440,19 @@ def parse(db, tx, message): "status": status, "pre_minted": pre_minted, } - ledger.ledger.insert_record(db, "fairminters", bindings, "NEW_FAIRMINTER") + ledger.insert_record(db, "fairminters", bindings, "NEW_FAIRMINTER") logger.info(f"Fair minter opened for {asset_name} by {tx['source']}.") if not existing_asset: # Add to table of assets if new asset - asset_id = ledger.ledger.generate_asset_id(asset_name, tx["block_index"]) + asset_id = ledger.generate_asset_id(asset_name, tx["block_index"]) bindings = { "asset_id": str(asset_id), "asset_name": asset_name, "block_index": tx["block_index"], "asset_longname": asset_longname if asset_longname != "" else None, } - ledger.ledger.insert_record(db, "assets", bindings, "ASSET_CREATION") + ledger.insert_record(db, "assets", bindings, "ASSET_CREATION") # insert issuance bindings = { @@ -476,11 +477,11 @@ def parse(db, tx, message): "fair_minting": True, "asset_events": "open_fairminter", } - ledger.ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE") + ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE") if pre_minted: # issuer is credited with the preminted quantity - ledger.ledger.credit( + ledger.credit( db, tx["source"], asset_name, @@ -491,7 +492,7 @@ def parse(db, tx, message): ) elif premint_quantity > 0: # This means that the fair mint is not yet open. In this case we escrow the premint. - ledger.ledger.credit( + ledger.credit( db, config.UNSPENDABLE, asset_name, @@ -503,7 +504,7 @@ def parse(db, tx, message): # debit fees if fee > 0: - ledger.ledger.debit( + ledger.debit( db, tx["source"], config.XCP, @@ -516,7 +517,7 @@ def parse(db, tx, message): def unescrow_premint(db, fairminter, destroy=False): # unescrow premint quantity... - ledger.ledger.debit( + ledger.debit( db, config.UNSPENDABLE, fairminter["asset"], @@ -527,7 +528,7 @@ def unescrow_premint(db, fairminter, destroy=False): ) # ...and send it to the issuer if not destroy: - ledger.ledger.credit( + ledger.credit( db, fairminter["source"], fairminter["asset"], @@ -541,7 +542,7 @@ def unescrow_premint(db, fairminter, destroy=False): # called each block def open_fairminters(db, block_index): # gets the fairminters with a `start_block` equal to `block_index` - fairminters = ledger.ledger.get_fairminters_to_open(db, block_index) + fairminters = ledger.get_fairminters_to_open(db, block_index) for fairminter in fairminters: assert fairminter["status"] != "open" # sanity check # update status to open @@ -552,14 +553,14 @@ def open_fairminters(db, block_index): unescrow_premint(db, fairminter) update_data["pre_minted"] = True # update fairminter - ledger.ledger.update_fairminter(db, fairminter["tx_hash"], update_data) + ledger.update_fairminter(db, fairminter["tx_hash"], update_data) def close_fairminter(db, fairminter, block_index): # update status to closed - ledger.ledger.update_fairminter(db, fairminter["tx_hash"], {"status": "closed"}) + ledger.update_fairminter(db, fairminter["tx_hash"], {"status": "closed"}) # unlock issuance when fair minter is closed - last_issuance = ledger.ledger.get_asset(db, fairminter["asset"]) + last_issuance = ledger.get_asset(db, fairminter["asset"]) last_issuance["quantity"] = 0 last_issuance["fair_minting"] = False last_issuance["block_index"] = block_index @@ -571,11 +572,11 @@ def close_fairminter(db, fairminter, block_index): last_issuance["description_locked"] = True last_issuance["asset_events"] = "close_fairminter" del last_issuance["supply"] - ledger.ledger.insert_record(db, "issuances", last_issuance, "ASSET_ISSUANCE") + ledger.insert_record(db, "issuances", last_issuance, "ASSET_ISSUANCE") def close_fairminters(db, block_index): - fairminters = ledger.ledger.get_fairminters_to_close(db, block_index) + fairminters = ledger.get_fairminters_to_close(db, block_index) for fairminter in fairminters: assert fairminter["status"] != "closed" # sanity check close_fairminter(db, fairminter, block_index) @@ -601,7 +602,7 @@ def perform_fairmint_soft_cap_operations(db, fairmint, fairminter, fairmint_quan xcp_action = "fairmint payment" # credit paid quantity to issuer or minter... if xcp_destination: - ledger.ledger.credit( + ledger.credit( db, xcp_destination, config.XCP, @@ -622,7 +623,7 @@ def perform_fairmint_soft_cap_operations(db, fairmint, fairminter, fairmint_quan "tag": xcp_action, "status": "valid", } - ledger.ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") # the soft cap is reached: # - the assets are distributed to the miner, @@ -630,7 +631,7 @@ def perform_fairmint_soft_cap_operations(db, fairmint, fairminter, fairmint_quan # if not reached asset will be destroyed in `soft_cap_deadline_reached()` if fairmint_quantity >= fairminter["soft_cap"]: # send assets to minter - ledger.ledger.credit( + ledger.credit( db, fairmint["source"], fairminter["asset"], @@ -641,7 +642,7 @@ def perform_fairmint_soft_cap_operations(db, fairmint, fairminter, fairmint_quan ) # send commission to issuer if fairmint["commission"] > 0: - ledger.ledger.credit( + ledger.credit( db, fairminter["source"], fairminter["asset"], @@ -656,17 +657,15 @@ def soft_cap_deadline_reached(db, fairminter, block_index): """ Performs necessary operations for a fairminter whose soft cap deadline has been reached. """ - fairmint_quantity, paid_quantity = ledger.ledger.get_fairmint_quantities( - db, fairminter["tx_hash"] - ) + fairmint_quantity, paid_quantity = ledger.get_fairmint_quantities(db, fairminter["tx_hash"]) fairminter_supply = fairmint_quantity + fairminter["premint_quantity"] - fairmints = ledger.ledger.get_valid_fairmints(db, fairminter["tx_hash"]) + fairmints = ledger.get_valid_fairmints(db, fairminter["tx_hash"]) # until the soft cap is reached, payments, commissions and assets # are escrowed at the config.UNSPENDABLE address. When the soft cap deadline is reached, # we start by unescrow all the assets and payments for this fairminter... if fairminter_supply > 0 or not protocol.enabled("partial_mint_to_reach_hard_cap"): - ledger.ledger.debit( + ledger.debit( db, config.UNSPENDABLE, fairminter["asset"], @@ -676,7 +675,7 @@ def soft_cap_deadline_reached(db, fairminter, block_index): event=fairminter["tx_hash"], ) if paid_quantity > 0: - ledger.ledger.debit( + ledger.debit( db, config.UNSPENDABLE, config.XCP, @@ -709,10 +708,10 @@ def soft_cap_deadline_reached(db, fairminter, block_index): "tag": "soft cap not reached", "status": "valid", } - ledger.ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") elif fairminter["premint_quantity"] > 0: # the premint is sent to the issuer - ledger.ledger.credit( + ledger.credit( db, fairminter["source"], fairminter["asset"], @@ -725,7 +724,7 @@ def soft_cap_deadline_reached(db, fairminter, block_index): def perform_fairminter_soft_cap_operations(db, block_index): # get fairminters with `soft_cap_deadline_block` equal to `block_index` - fairminters = ledger.ledger.get_fairminters_by_soft_cap_deadline(db, block_index) + fairminters = ledger.get_fairminters_by_soft_cap_deadline(db, block_index) for fairminter in fairminters: soft_cap_deadline_reached(db, fairminter, block_index) diff --git a/counterparty-core/counterpartycore/lib/messages/gas.py b/counterparty-core/counterpartycore/lib/messages/gas.py index 405ddfc74..e042a8bf6 100644 --- a/counterparty-core/counterpartycore/lib/messages/gas.py +++ b/counterparty-core/counterpartycore/lib/messages/gas.py @@ -2,7 +2,8 @@ import math from decimal import Decimal as D -from counterpartycore.lib import config, ledger +from counterpartycore.lib import config +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.parser import protocol logger = logging.getLogger(config.LOGGER_NAME) @@ -34,7 +35,7 @@ def increment_counter(db, transaction_id, block_index): "transaction_id": transaction_id, "count": new_count, } - ledger.ledger.insert_record(db, "transaction_count", bindings, "INCREMENT_TRANSACTION_COUNT") + ledger.insert_record(db, "transaction_count", bindings, "INCREMENT_TRANSACTION_COUNT") def get_transaction_count_for_last_period(db, transaction_id, block_index): diff --git a/counterparty-core/counterpartycore/lib/messages/issuance.py b/counterparty-core/counterpartycore/lib/messages/issuance.py index 6be17c381..7e496533f 100644 --- a/counterparty-core/counterpartycore/lib/messages/issuance.py +++ b/counterparty-core/counterpartycore/lib/messages/issuance.py @@ -6,7 +6,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import assetnames @@ -101,7 +102,7 @@ def validate( problems.append("call price for non‐callable asset") # Valid re-issuance? - issuances = ledger.ledger.get_issuances( + issuances = ledger.get_issuances( db, asset=asset, status="valid", first=True, current_block_index=block_index ) reissued_asset_longname = None @@ -161,7 +162,7 @@ def validate( # validate parent ownership for subasset if subasset_longname is not None and not reissuance: - parent_issuances = ledger.ledger.get_issuances( + parent_issuances = ledger.get_issuances( db, asset=subasset_parent, status="valid", first=True, current_block_index=block_index ) if parent_issuances: @@ -173,7 +174,7 @@ def validate( # validate subasset issuance is not a duplicate if subasset_longname is not None and not reissuance: - assets = ledger.ledger.get_assets_by_longname(db, subasset_longname) + assets = ledger.get_assets_by_longname(db, subasset_longname) if len(assets) > 0: problems.append("subasset already exists") @@ -187,7 +188,7 @@ def validate( block_index < 310000 and not protocol.is_test_network() ): # Pay fee only upon first issuance. (Protocol change.) cursor = db.cursor() - balance = ledger.ledger.get_balance(db, source, config.XCP) + balance = ledger.get_balance(db, source, config.XCP) cursor.close() if protocol.enabled("numeric_asset_names"): # Protocol change. if subasset_longname is not None and protocol.enabled( @@ -228,10 +229,10 @@ def validate( if protocol.enabled("cip03", block_index) and reset and issuances: # Checking that all supply are held by the owner of the asset - balances = ledger.ledger.get_asset_balances(db, asset) + balances = ledger.get_asset_balances(db, asset) if len(balances) == 0: - if ledger.ledger.asset_supply(db, asset) > 0: + if ledger.asset_supply(db, asset) > 0: problems.append("Cannot reset an asset with no holder") elif len(balances) > 1: problems.append("Cannot reset an asset with many holders") @@ -276,7 +277,7 @@ def compose( ): # Callability is deprecated, so for re‐issuances set relevant parameters # to old values; for first issuances, make uncallable. - issuances = ledger.ledger.get_issuances( + issuances = ledger.get_issuances( db, asset=asset, status="valid", @@ -302,7 +303,7 @@ def compose( ) if subasset_longname is not None: # try to find an existing subasset - assets = ledger.ledger.get_assets_by_longname(db, subasset_longname) + assets = ledger.get_assets_by_longname(db, subasset_longname) if len(assets) > 0: # this is a reissuance asset = assets[0]["asset_name"] @@ -311,8 +312,8 @@ def compose( # generate a random numeric asset id which will map to this subasset asset = assetnames.generate_random_asset(subasset_longname) - asset_id = ledger.ledger.generate_asset_id(asset, CurrentState().current_block_index()) - asset_name = ledger.ledger.generate_asset_name( + asset_id = ledger.generate_asset_id(asset, CurrentState().current_block_index()) + asset_name = ledger.generate_asset_name( asset_id, CurrentState().current_block_index() ) # This will remove leading zeros in the numeric assets @@ -624,18 +625,18 @@ def unpack(db, message, message_type_id, block_index, return_dict=False): "", ) try: - asset = ledger.ledger.generate_asset_name(asset_id, block_index) + asset = ledger.generate_asset_name(asset_id, block_index) ##This is for backwards compatibility with assets names longer than 12 characters if asset.startswith("A"): - named_asset = ledger.ledger.get_asset_name(db, asset_id, block_index) + named_asset = ledger.get_asset_name(db, asset_id, block_index) if named_asset != 0: asset = named_asset if description == None: # noqa: E711 try: - description = ledger.ledger.get_asset_description(db, asset) + description = ledger.get_asset_description(db, asset) except exceptions.AssetError: description = "" @@ -703,7 +704,7 @@ def unpack(db, message, message_type_id, block_index, return_dict=False): def _get_last_description(db, asset, default, block_index): - issuances = ledger.ledger.get_issuances( + issuances = ledger.get_issuances( db, asset=asset, status="valid", first=True, current_block_index=block_index ) if len(issuances) > 0: @@ -782,11 +783,11 @@ def parse(db, tx, message, message_type_id): # Reset? if (status == "valid") and reset and protocol.enabled("cip03", tx["block_index"]): - balances_result = ledger.ledger.get_asset_balances(db, asset) + balances_result = ledger.get_asset_balances(db, asset) if len(balances_result) <= 1: if len(balances_result) == 0: - issuances_result = ledger.ledger.get_issuances( + issuances_result = ledger.get_issuances( db, asset=asset, last=True, current_block_index=tx["block_index"] ) @@ -798,7 +799,7 @@ def parse(db, tx, message, message_type_id): if owner_address == tx["source"]: if owner_balance > 0: - ledger.ledger.debit( + ledger.debit( db, tx["source"], asset, @@ -818,7 +819,7 @@ def parse(db, tx, message, message_type_id): "tag": "reset", "status": "valid", } - ledger.ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", bindings, "ASSET_DESTRUCTION") bindings = { "tx_index": tx["tx_index"], @@ -842,13 +843,13 @@ def parse(db, tx, message, message_type_id): "asset_events": "reset", } - ledger.ledger.insert_record(db, "issuances", bindings, "RESET_ISSUANCE") + ledger.insert_record(db, "issuances", bindings, "RESET_ISSUANCE") logger.info("Reset issuance of %(asset)s [%(tx_hash)s] (%(status)s)", bindings) # Credit. if quantity: - ledger.ledger.credit( + ledger.credit( db, tx["source"], asset, @@ -871,7 +872,7 @@ def parse(db, tx, message, message_type_id): # Debit fee. if status == "valid": - ledger.ledger.debit( + ledger.debit( db, tx["source"], config.XCP, @@ -908,7 +909,7 @@ def parse(db, tx, message, message_type_id): "block_index": tx["block_index"], "asset_longname": subasset_longname, } - ledger.ledger.insert_record(db, "assets", bindings, "ASSET_CREATION") + ledger.insert_record(db, "assets", bindings, "ASSET_CREATION") asset_events.append("creation") if status == "valid" and reissuance: @@ -950,7 +951,7 @@ def parse(db, tx, message, message_type_id): if "cannot issue during fair minting" in status: bindings["fair_minting"] = True if "integer overflow" not in status: - ledger.ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE") + ledger.insert_record(db, "issuances", bindings, "ASSET_ISSUANCE") logger.info( "Issuance of %(quantity)s %(asset)s by %(source)s [%(tx_hash)s] (%(status)s)", bindings @@ -958,7 +959,7 @@ def parse(db, tx, message, message_type_id): # Credit. if status == "valid" and quantity: - ledger.ledger.credit( + ledger.credit( db, tx["source"], asset, diff --git a/counterparty-core/counterpartycore/lib/messages/move.py b/counterparty-core/counterpartycore/lib/messages/move.py index 276e5af99..cf404f992 100644 --- a/counterparty-core/counterpartycore/lib/messages/move.py +++ b/counterparty-core/counterpartycore/lib/messages/move.py @@ -1,6 +1,7 @@ import logging -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.messages.detach import detach_assets from counterpartycore.lib.parser import protocol, utxosinfo from counterpartycore.lib.utils import address @@ -13,7 +14,7 @@ def compose(db, source, destination, utxo_value=None, skip_validation=False): if not utxosinfo.is_utxo_format(source): raise exceptions.ComposeError("Invalid source utxo format") - balances = ledger.ledger.get_utxo_balances(db, source) + balances = ledger.get_utxo_balances(db, source) if not balances: raise exceptions.ComposeError("No assets attached to the source utxo") @@ -34,13 +35,13 @@ def compose(db, source, destination, utxo_value=None, skip_validation=False): def move_balances(db, tx, source, destination): action = "utxo move" - msg_index = ledger.ledger.get_send_msg_index(db, tx["tx_hash"]) - balances = ledger.ledger.get_utxo_balances(db, source) + msg_index = ledger.get_send_msg_index(db, tx["tx_hash"]) + balances = ledger.get_utxo_balances(db, source) for balance in balances: if balance["quantity"] == 0: continue # debit asset from source - source_address = ledger.ledger.debit( + source_address = ledger.debit( db, source, balance["asset"], @@ -50,7 +51,7 @@ def move_balances(db, tx, source, destination): event=tx["tx_hash"], ) # credit asset to destination - destination_address = ledger.ledger.credit( + destination_address = ledger.credit( db, destination, balance["asset"], @@ -75,7 +76,7 @@ def move_balances(db, tx, source, destination): "send_type": "move", } - ledger.ledger.insert_record(db, "sends", bindings, "UTXO_MOVE") + ledger.insert_record(db, "sends", bindings, "UTXO_MOVE") msg_index += 1 # log the move diff --git a/counterparty-core/counterpartycore/lib/messages/order.py b/counterparty-core/counterpartycore/lib/messages/order.py index ff5f3940a..ade412705 100644 --- a/counterparty-core/counterpartycore/lib/messages/order.py +++ b/counterparty-core/counterpartycore/lib/messages/order.py @@ -7,8 +7,8 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import helpers @@ -27,13 +27,13 @@ def exact_penalty(db, address, block_index, order_match_id, tx_index): cursor = db.cursor() # Orders. - bad_orders = ledger.ledger.get_open_btc_orders(db, address) + bad_orders = ledger.get_open_btc_orders(db, address) for bad_order in bad_orders: cancel_order(db, bad_order, "expired", block_index, tx_index) if not protocol.after_block_or_test_network(block_index, 314250): # Protocol change. # Order matches. - bad_order_matches = ledger.ledger.get_pending_btc_order_matches(db, address) + bad_order_matches = ledger.get_pending_btc_order_matches(db, address) for bad_order_match in bad_order_matches: cancel_order_match(db, bad_order_match, "expired", block_index, tx_index) @@ -48,10 +48,10 @@ def cancel_order(db, order, status, block_index, tx_index): set_data = { "status": status, } - ledger.ledger.update_order(db, order["tx_hash"], set_data) + ledger.update_order(db, order["tx_hash"], set_data) if order["give_asset"] != config.BTC: # Can’t credit BTC. - ledger.ledger.credit( + ledger.credit( db, order["source"], order["give_asset"], @@ -68,7 +68,7 @@ def cancel_order(db, order, status, block_index, tx_index): "source": order["source"], "block_index": block_index, } - ledger.ledger.insert_record(db, "order_expirations", bindings, "ORDER_EXPIRATION") + ledger.insert_record(db, "order_expirations", bindings, "ORDER_EXPIRATION") logger.info( "Order cancelled %(give_asset)s / %(get_asset)s (%(order_hash)s) [%(status)s]", @@ -87,21 +87,21 @@ def cancel_order_match(db, order_match, status, block_index, tx_index): """The only cancelling is an expiration.""" # Skip order matches just expired as a penalty. (Not very efficient.) if not protocol.after_block_or_test_network(block_index, 314250): # Protocol change. - order_matches = ledger.ledger.get_order_match(db, id=order_match["id"]) + order_matches = ledger.get_order_match(db, id=order_match["id"]) if order_matches and order_matches[0]["status"] == "expired": return # Update status of order match. - ledger.ledger.update_order_match_status(db, order_match["id"], status) + ledger.update_order_match_status(db, order_match["id"], status) # If tx0 is dead, credit address directly; if not, replenish give remaining, get remaining, and fee required remaining. - orders = ledger.ledger.get_order(db, order_hash=order_match["tx0_hash"]) + orders = ledger.get_order(db, order_hash=order_match["tx0_hash"]) assert len(orders) == 1 tx0_order = orders[0] if tx0_order["status"] in ("expired", "cancelled"): tx0_order_status = tx0_order["status"] if order_match["forward_asset"] != config.BTC: - ledger.ledger.credit( + ledger.credit( db, order_match["tx0_address"], order_match["forward_asset"], @@ -135,16 +135,16 @@ def cancel_order_match(db, order_match, status, block_index, tx_index): "status": tx0_order_status, "fee_required_remaining": tx0_fee_required_remaining, } - ledger.ledger.update_order(db, order_match["tx0_hash"], set_data) + ledger.update_order(db, order_match["tx0_hash"], set_data) # If tx1 is dead, credit address directly; if not, replenish give remaining, get remaining, and fee required remaining. - orders = ledger.ledger.get_order(db, order_hash=order_match["tx1_hash"]) + orders = ledger.get_order(db, order_hash=order_match["tx1_hash"]) assert len(orders) == 1 tx1_order = orders[0] if tx1_order["status"] in ("expired", "cancelled"): tx1_order_status = tx1_order["status"] if order_match["backward_asset"] != config.BTC: - ledger.ledger.credit( + ledger.credit( db, order_match["tx1_address"], order_match["backward_asset"], @@ -177,7 +177,7 @@ def cancel_order_match(db, order_match, status, block_index, tx_index): "status": tx1_order_status, "fee_required_remaining": tx1_fee_required_remaining, } - ledger.ledger.update_order(db, order_match["tx1_hash"], set_data) + ledger.update_order(db, order_match["tx1_hash"], set_data) if block_index < 286500: # Protocol change. # Sanity check: one of the two must have expired. @@ -195,12 +195,8 @@ def cancel_order_match(db, order_match, status, block_index, tx_index): # Re‐match. if protocol.after_block_or_test_network(block_index, 310000): # Protocol change. if not protocol.after_block_or_test_network(block_index, 315000): # Protocol change. - match( - db, ledger.ledger.get_transactions(db, tx_hash=tx0_order["tx_hash"])[0], block_index - ) - match( - db, ledger.ledger.get_transactions(db, tx_hash=tx1_order["tx_hash"])[0], block_index - ) + match(db, ledger.get_transactions(db, tx_hash=tx0_order["tx_hash"])[0], block_index) + match(db, ledger.get_transactions(db, tx_hash=tx1_order["tx_hash"])[0], block_index) if status == "expired": # Record order match expiration. @@ -211,9 +207,7 @@ def cancel_order_match(db, order_match, status, block_index, tx_index): "tx1_address": order_match["tx1_address"], "block_index": block_index, } - ledger.ledger.insert_record( - db, "order_match_expirations", bindings, "ORDER_MATCH_EXPIRATION" - ) + ledger.insert_record(db, "order_match_expirations", bindings, "ORDER_MATCH_EXPIRATION") logger.info( "Order match cancelled %(forward_asset)s / %(backward_asset)s (%(order_match_id)s) [%(status)s]", @@ -280,11 +274,11 @@ def validate( if not give_quantity or not get_quantity: problems.append("zero give or zero get") - if give_asset not in (config.BTC, config.XCP) and not ledger.ledger.get_issuances( + if give_asset not in (config.BTC, config.XCP) and not ledger.get_issuances( db, status="valid", asset=give_asset, current_block_index=block_index ): problems.append(f"no such asset to give ({give_asset})") - if get_asset not in (config.BTC, config.XCP) and not ledger.ledger.get_issuances( + if get_asset not in (config.BTC, config.XCP) and not ledger.get_issuances( db, status="valid", asset=get_asset, current_block_index=block_index ): problems.append(f"no such asset to get ({get_asset})") @@ -309,12 +303,12 @@ def compose( cursor = db.cursor() # resolve subassets - give_asset = ledger.ledger.resolve_subasset_longname(db, give_asset) - get_asset = ledger.ledger.resolve_subasset_longname(db, get_asset) + give_asset = ledger.resolve_subasset_longname(db, give_asset) + get_asset = ledger.resolve_subasset_longname(db, get_asset) # Check balance. if give_asset != config.BTC: - balance = ledger.ledger.get_balance(db, source, give_asset) + balance = ledger.get_balance(db, source, give_asset) if balance < give_quantity: raise exceptions.ComposeError("insufficient funds") @@ -332,8 +326,8 @@ def compose( if problems and not skip_validation: raise exceptions.ComposeError(problems) - give_id = ledger.ledger.get_asset_id(db, give_asset, CurrentState().current_block_index()) - get_id = ledger.ledger.get_asset_id(db, get_asset, CurrentState().current_block_index()) + give_id = ledger.get_asset_id(db, give_asset, CurrentState().current_block_index()) + get_id = ledger.get_asset_id(db, get_asset, CurrentState().current_block_index()) data = messagetype.pack(ID) data += struct.pack( FORMAT, give_id, give_quantity, get_id, get_quantity, expiration, fee_required @@ -349,8 +343,8 @@ def unpack(db, message, block_index, return_dict=False): give_id, give_quantity, get_id, get_quantity, expiration, fee_required = struct.unpack( FORMAT, message ) - give_asset = ledger.ledger.get_asset_name(db, give_id, block_index) - get_asset = ledger.ledger.get_asset_name(db, get_id, block_index) + give_asset = ledger.get_asset_name(db, give_id, block_index) + get_asset = ledger.get_asset_name(db, get_id, block_index) status = "open" except (exceptions.UnpackError, exceptions.AssetNameError, struct.error) as e: # noqa: F841 give_asset, give_quantity, get_asset, get_quantity, expiration, fee_required = ( @@ -387,12 +381,12 @@ def parse(db, tx, message): price = 0 if status == "open": try: - price = ledger.ledger.price(get_quantity, give_quantity) + price = ledger.price(get_quantity, give_quantity) except ZeroDivisionError: price = 0 # Overorder - balance = ledger.ledger.get_balance(db, tx["source"], give_asset) + balance = ledger.get_balance(db, tx["source"], give_asset) if give_asset != config.BTC: if balance == 0: give_quantity = 0 @@ -433,7 +427,7 @@ def parse(db, tx, message): # Debit give quantity. (Escrow.) if status == "open": if give_asset != config.BTC: # No need (or way) to debit BTC. - ledger.ledger.debit( + ledger.debit( db, tx["source"], give_asset, @@ -464,7 +458,7 @@ def parse(db, tx, message): "status": status, } if "integer overflow" not in status: - ledger.ledger.insert_order(db, bindings) + ledger.insert_order(db, bindings) logger.info( "Order opened for %(give_quantity)s %(give_asset)s at %(source)s (%(tx_hash)s) [%(status)s]", @@ -482,7 +476,7 @@ def match(db, tx, block_index=None): cursor = db.cursor() # Get order in question. - orders = ledger.ledger.get_order(db, order_hash=tx["tx_hash"]) + orders = ledger.get_order(db, order_hash=tx["tx_hash"]) if not orders: cursor.close() return @@ -497,7 +491,7 @@ def match(db, tx, block_index=None): tx1_give_remaining = tx1["give_remaining"] tx1_get_remaining = tx1["get_remaining"] - order_matches = ledger.ledger.get_matching_orders( + order_matches = ledger.get_matching_orders( db, tx1["tx_hash"], give_asset=tx1["give_asset"], get_asset=tx1["get_asset"] ) @@ -506,7 +500,7 @@ def match(db, tx, block_index=None): order_matches, key=lambda x: x["tx_index"] ) # Sort by tx index second. order_matches = sorted( - order_matches, key=lambda x: ledger.ledger.price(x["get_quantity"], x["give_quantity"]) + order_matches, key=lambda x: ledger.price(x["get_quantity"], x["give_quantity"]) ) # Sort by price first. # Get fee remaining. @@ -521,8 +515,8 @@ def match(db, tx, block_index=None): order_match_id = helpers.make_id(tx0["tx_hash"], tx1["tx_hash"]) if not block_index: block_index = max( - ledger.ledger.get_order_first_block_index(cursor, tx0["tx_hash"]), - ledger.ledger.get_order_first_block_index(cursor, tx1["tx_hash"]), + ledger.get_order_first_block_index(cursor, tx0["tx_hash"]), + ledger.get_order_first_block_index(cursor, tx1["tx_hash"]), ) if tx1_status != "open": break @@ -532,10 +526,10 @@ def match(db, tx, block_index=None): tx0_get_remaining = tx0["get_remaining"] # Ignore previous matches. (Both directions, just to be sure.) - if ledger.ledger.get_order_match(db, id=helpers.make_id(tx0["tx_hash"], tx1["tx_hash"])): + if ledger.get_order_match(db, id=helpers.make_id(tx0["tx_hash"], tx1["tx_hash"])): logger.trace("Skipping: previous match") continue - if ledger.ledger.get_order_match(db, id=helpers.make_id(tx1["tx_hash"], tx0["tx_hash"])): + if ledger.get_order_match(db, id=helpers.make_id(tx1["tx_hash"], tx0["tx_hash"])): logger.trace("Skipping: previous match") continue @@ -571,13 +565,13 @@ def match(db, tx, block_index=None): # If the prices agree, make the trade. The found order sets the price, # and they trade as much as they can. - tx0_price = ledger.ledger.price(tx0["get_quantity"], tx0["give_quantity"]) - tx1_price = ledger.ledger.price(tx1["get_quantity"], tx1["give_quantity"]) - tx1_inverse_price = ledger.ledger.price(tx1["give_quantity"], tx1["get_quantity"]) + tx0_price = ledger.price(tx0["get_quantity"], tx0["give_quantity"]) + tx1_price = ledger.price(tx1["get_quantity"], tx1["give_quantity"]) + tx1_inverse_price = ledger.price(tx1["give_quantity"], tx1["get_quantity"]) # Protocol change. if tx["block_index"] < 286000: - tx1_inverse_price = ledger.ledger.price(1, tx1_price) + tx1_inverse_price = ledger.price(1, tx1_price) logger.trace( f"Tx0 Price: {float(tx0_price)}; Tx1 Inverse Price: {float(tx1_inverse_price)}" @@ -586,10 +580,10 @@ def match(db, tx, block_index=None): logger.trace("Skipping: price mismatch.") else: logger.trace( - f"Potential forward quantities: {tx0_give_remaining}, {int(ledger.ledger.price(tx1_give_remaining, tx0_price))}" + f"Potential forward quantities: {tx0_give_remaining}, {int(ledger.price(tx1_give_remaining, tx0_price))}" ) forward_quantity = int( - min(tx0_give_remaining, int(ledger.ledger.price(tx1_give_remaining, tx0_price))) + min(tx0_give_remaining, int(ledger.price(tx1_give_remaining, tx0_price))) ) logger.trace(f"Forward Quantity: {forward_quantity}") backward_quantity = round(forward_quantity * tx0_price) @@ -627,12 +621,12 @@ def match(db, tx, block_index=None): ): # Protocol change. fee = int( tx1["fee_required"] - * ledger.ledger.price(backward_quantity, tx1["give_quantity"]) + * ledger.price(backward_quantity, tx1["give_quantity"]) ) else: fee = int( tx1["fee_required_remaining"] - * ledger.ledger.price(forward_quantity, tx1_get_remaining) + * ledger.price(forward_quantity, tx1_get_remaining) ) logger.trace( @@ -654,12 +648,12 @@ def match(db, tx, block_index=None): ): # Protocol change. fee = int( tx0["fee_required"] - * ledger.ledger.price(backward_quantity, tx0["give_quantity"]) + * ledger.price(backward_quantity, tx0["give_quantity"]) ) else: fee = int( tx0["fee_required_remaining"] - * ledger.ledger.price(backward_quantity, tx0_get_remaining) + * ledger.price(backward_quantity, tx0_get_remaining) ) logger.trace( @@ -688,7 +682,7 @@ def match(db, tx, block_index=None): else: status = "completed" # Credit. - ledger.ledger.credit( + ledger.credit( db, tx1["source"], tx1["get_asset"], @@ -697,7 +691,7 @@ def match(db, tx, block_index=None): action="order match", event=order_match_id, ) - ledger.ledger.credit( + ledger.credit( db, tx0["source"], tx0["get_asset"], @@ -724,7 +718,7 @@ def match(db, tx, block_index=None): if tx0["give_asset"] != config.BTC and tx0["get_asset"] != config.BTC: # Fill order, and recredit give_remaining. tx0_status = "filled" - ledger.ledger.credit( + ledger.credit( db, tx0["source"], tx0["give_asset"], @@ -740,7 +734,7 @@ def match(db, tx, block_index=None): "fee_provided_remaining": tx0_fee_provided_remaining, "status": tx0_status, } - ledger.ledger.update_order(db, tx0["tx_hash"], set_data) + ledger.update_order(db, tx0["tx_hash"], set_data) # tx1 if tx1_give_remaining <= 0 or ( @@ -749,7 +743,7 @@ def match(db, tx, block_index=None): if tx1["give_asset"] != config.BTC and tx1["get_asset"] != config.BTC: # Fill order, and recredit give_remaining. tx1_status = "filled" - ledger.ledger.credit( + ledger.credit( db, tx1["source"], tx1["give_asset"], @@ -765,7 +759,7 @@ def match(db, tx, block_index=None): "fee_provided_remaining": tx1_fee_provided_remaining, "status": tx1_status, } - ledger.ledger.update_order(db, tx1["tx_hash"], set_data) + ledger.update_order(db, tx1["tx_hash"], set_data) # Calculate when the match will expire. if protocol.after_block_or_test_network(block_index, 308000): # Protocol change. @@ -797,7 +791,7 @@ def match(db, tx, block_index=None): "fee_paid": fee, "status": status, } - ledger.ledger.insert_record(db, "order_matches", bindings, "ORDER_MATCH") + ledger.insert_record(db, "order_matches", bindings, "ORDER_MATCH") logger.info( "Order match for %(forward_quantity)s %(forward_asset)s against %(backward_quantity)s %(backward_asset)s (%(id)s) [%(status)s]", @@ -813,18 +807,18 @@ def match(db, tx, block_index=None): def expire_orders(db, block_index): # Expire orders and give refunds for the quantity give_remaining (if non-zero; if not BTC). - orders = ledger.ledger.get_orders_to_expire(db, block_index) + orders = ledger.get_orders_to_expire(db, block_index) # Edge case: filled orders, and therefore not expired in the previous block, # re-ropened by order_match expiration in the previous block. # TODO: protocol change: expire order matches then orders. - orders += ledger.ledger.get_orders_to_expire(db, block_index - 1) + orders += ledger.get_orders_to_expire(db, block_index - 1) for order in orders: cancel_order(db, order, "expired", block_index, 0) # tx_index=0 for block action def expire_order_matches(db, block_index): # Expire order_matches for BTC with no BTC. - order_matches = ledger.ledger.get_order_matches_to_expire(db, block_index) + order_matches = ledger.get_order_matches_to_expire(db, block_index) for order_match in order_matches: cancel_order_match( db, order_match, "expired", block_index, 0 @@ -833,7 +827,7 @@ def expire_order_matches(db, block_index): # Expire btc sell order if match expires if protocol.enabled("btc_sell_expire_on_match_expire"): # Check for other pending order matches involving either tx0_hash or tx1_hash - order_matches_pending = ledger.ledger.get_pending_order_matches( + order_matches_pending = ledger.get_pending_order_matches( db, tx0_hash=order_match["tx0_hash"], tx1_hash=order_match["tx1_hash"] ) # Set BTC sell order status as expired only if there are no pending order matches @@ -841,14 +835,14 @@ def expire_order_matches(db, block_index): if order_match["backward_asset"] == "BTC" and order_match["status"] == "expired": cancel_order( db, - ledger.ledger.get_order(db, order_hash=order_match["tx1_hash"])[0], + ledger.get_order(db, order_hash=order_match["tx1_hash"])[0], "expired", block_index, ) if order_match["forward_asset"] == "BTC" and order_match["status"] == "expired": cancel_order( db, - ledger.ledger.get_order(db, order_hash=order_match["tx0_hash"])[0], + ledger.get_order(db, order_hash=order_match["tx0_hash"])[0], "expired", block_index, ) @@ -856,12 +850,8 @@ def expire_order_matches(db, block_index): if protocol.after_block_or_test_network(block_index, 315000): # Protocol change. # Re‐match. for order_match in order_matches: - match( - db, ledger.ledger.get_order(db, order_hash=order_match["tx0_hash"])[0], block_index - ) - match( - db, ledger.ledger.get_order(db, order_hash=order_match["tx1_hash"])[0], block_index - ) + match(db, ledger.get_order(db, order_hash=order_match["tx0_hash"])[0], block_index) + match(db, ledger.get_order(db, order_hash=order_match["tx1_hash"])[0], block_index) def expire(db, block_index): diff --git a/counterparty-core/counterpartycore/lib/messages/rps.py b/counterparty-core/counterpartycore/lib/messages/rps.py index bea4d6bf2..d950e6d61 100644 --- a/counterparty-core/counterpartycore/lib/messages/rps.py +++ b/counterparty-core/counterpartycore/lib/messages/rps.py @@ -21,7 +21,8 @@ import logging import os -from counterpartycore.lib import config, ledger +from counterpartycore.lib import config +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.parser import protocol logger = logging.getLogger(config.LOGGER_NAME) @@ -39,7 +40,7 @@ def replay_events(db, key): return events = RPS_EVENTS.get(key) if events: - ledger.ledger.replay_events(db, events) + ledger.replay_events(db, events) def parse(db, tx, message): diff --git a/counterparty-core/counterpartycore/lib/messages/sweep.py b/counterparty-core/counterpartycore/lib/messages/sweep.py index 5deed409b..3d9f2e944 100644 --- a/counterparty-core/counterpartycore/lib/messages/sweep.py +++ b/counterparty-core/counterpartycore/lib/messages/sweep.py @@ -4,9 +4,9 @@ from counterpartycore.lib import ( config, exceptions, - ledger, ) from counterpartycore.lib.exceptions import * # noqa: F403 +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import address @@ -31,8 +31,8 @@ def get_total_fee(db, source, block_index): total_fee = ANTISPAM_FEE antispamfee = protocol.get_value_by_block_index("sweep_antispam_fee", block_index) * config.UNIT if antispamfee > 0: - balances_count = ledger.ledger.get_balances_count(db, source)[0]["cnt"] - issuances_count = ledger.ledger.get_issuances_count(db, source) + balances_count = ledger.get_balances_count(db, source)[0]["cnt"] + issuances_count = ledger.get_issuances_count(db, source) total_fee = int(balances_count * antispamfee * 2 + issuances_count * antispamfee * 4) return total_fee @@ -45,7 +45,7 @@ def validate(db, source, destination, flags, memo, block_index): cursor = db.cursor() - result = ledger.ledger.get_balance(db, source, "XCP") + result = ledger.get_balance(db, source, "XCP") total_fee = get_total_fee(db, source, block_index) @@ -161,7 +161,7 @@ def parse(db, tx, message): ) if antispamfee > 0: - ledger.ledger.debit( + ledger.debit( db, tx["source"], "XCP", @@ -171,7 +171,7 @@ def parse(db, tx, message): event=tx["tx_hash"], ) else: - ledger.ledger.debit( + ledger.debit( db, tx["source"], "XCP", @@ -185,11 +185,11 @@ def parse(db, tx, message): status = "invalid: insufficient balance for antispam fee for sweep" if status == "valid": - balances = ledger.ledger.get_address_balances(db, tx["source"]) + balances = ledger.get_address_balances(db, tx["source"]) if flags & FLAG_BALANCES: for balance in balances: - ledger.ledger.debit( + ledger.debit( db, tx["source"], balance["asset"], @@ -198,7 +198,7 @@ def parse(db, tx, message): action="sweep", event=tx["tx_hash"], ) - ledger.ledger.credit( + ledger.credit( db, destination, balance["asset"], @@ -213,10 +213,10 @@ def parse(db, tx, message): assets_issued = balances if protocol.enabled("zero_balance_ownership_sweep_fix", tx["block_index"]): - assets_issued = ledger.ledger.get_asset_issued(db, tx["source"]) + assets_issued = ledger.get_asset_issued(db, tx["source"]) for next_asset_issued in assets_issued: - issuances = ledger.ledger.get_issuances( + issuances = ledger.get_issuances( db, asset=next_asset_issued["asset"], status="valid", @@ -248,7 +248,7 @@ def parse(db, tx, message): "reset": False, "asset_events": "transfer", } - ledger.ledger.insert_record(db, "issuances", bindings, "ASSET_TRANSFER") + ledger.insert_record(db, "issuances", bindings, "ASSET_TRANSFER") sweep_pos += 1 bindings = { @@ -262,7 +262,7 @@ def parse(db, tx, message): "memo": memo_bytes, "fee_paid": total_fee if antispamfee > 0 else fee_paid, } - ledger.ledger.insert_record(db, "sweeps", bindings, "SWEEP") + ledger.insert_record(db, "sweeps", bindings, "SWEEP") logger.info("Sweep from %(source)s to %(destination)s (%(tx_hash)s) [%(status)s]", bindings) diff --git a/counterparty-core/counterpartycore/lib/messages/utxo.py b/counterparty-core/counterpartycore/lib/messages/utxo.py index da642d85a..e7cf62385 100644 --- a/counterparty-core/counterpartycore/lib/messages/utxo.py +++ b/counterparty-core/counterpartycore/lib/messages/utxo.py @@ -3,7 +3,8 @@ import logging import struct -from counterpartycore.lib import backend, config, exceptions, ledger +from counterpartycore.lib import backend, config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import gas from counterpartycore.lib.parser import utxosinfo @@ -67,7 +68,7 @@ def validate(db, source, destination, asset, quantity, block_index=None): fee = 0 # check if source has enough funds - asset_balance = ledger.ledger.get_balance(db, source, asset) + asset_balance = ledger.get_balance(db, source, asset) if asset == config.XCP: # fee is always paid in XCP if asset_balance < quantity + fee: @@ -76,7 +77,7 @@ def validate(db, source, destination, asset, quantity, block_index=None): if asset_balance < quantity: problems.append("insufficient funds for transfer") if source_is_address: - xcp_balance = ledger.ledger.get_balance(db, source, config.XCP) + xcp_balance = ledger.get_balance(db, source, config.XCP) if xcp_balance < fee: problems.append("insufficient funds for fee") @@ -190,7 +191,7 @@ def parse(db, tx, message): bindings = { "tx_index": tx["tx_index"], "tx_hash": tx["tx_hash"], - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]), "block_index": tx["block_index"], "status": status, } @@ -208,7 +209,7 @@ def parse(db, tx, message): else: fee_payer = recipient # debit fee from the fee payer - ledger.ledger.debit( + ledger.debit( db, fee_payer, config.XCP, @@ -228,12 +229,12 @@ def parse(db, tx, message): "tag": f"{action} fee", "status": "valid", } - ledger.ledger.insert_record(db, "destructions", destroy_bindings, "ASSET_DESTRUCTION") + ledger.insert_record(db, "destructions", destroy_bindings, "ASSET_DESTRUCTION") # debit asset from source and credit to recipient - ledger.ledger.debit( + ledger.debit( db, source, asset, quantity, tx["tx_index"], action=action, event=tx["tx_hash"] ) - ledger.ledger.credit( + ledger.credit( db, recipient, asset, @@ -255,7 +256,7 @@ def parse(db, tx, message): if action == "attach to utxo": gas.increment_counter(db, ID, tx["block_index"]) - ledger.ledger.insert_record(db, "sends", bindings, event) + ledger.insert_record(db, "sends", bindings, event) # log valid transactions if status == "valid": diff --git a/counterparty-core/counterpartycore/lib/messages/versions/enhancedsend.py b/counterparty-core/counterpartycore/lib/messages/versions/enhancedsend.py index bac6ca453..59044cfd3 100644 --- a/counterparty-core/counterpartycore/lib/messages/versions/enhancedsend.py +++ b/counterparty-core/counterpartycore/lib/messages/versions/enhancedsend.py @@ -3,7 +3,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages.versions import send1 from counterpartycore.lib.parser import messagetype, protocol @@ -35,7 +36,7 @@ def unpack(message, block_index): full_address = address.unpack(short_address_bytes) # asset id to name - asset = ledger.ledger.generate_asset_name(asset_id, block_index) + asset = ledger.generate_asset_name(asset_id, block_index) if asset == config.BTC: raise exceptions.AssetNameError(f"{config.BTC} not allowed") @@ -87,7 +88,7 @@ def validate(db, source, destination, asset, quantity, memo_bytes, block_index): if protocol.enabled("options_require_memo"): cursor = db.cursor() try: - results = ledger.ledger.get_addresses(db, address=destination) + results = ledger.get_addresses(db, address=destination) if results: result = results[0] if result and helpers.active_options( @@ -119,14 +120,14 @@ def compose( return send1.compose_send_btc(db, source, destination, quantity) # resolve subassets - asset = ledger.ledger.resolve_subasset_longname(db, asset) + asset = ledger.resolve_subasset_longname(db, asset) # quantity must be in int satoshi (not float, string, etc) if not isinstance(quantity, int): raise exceptions.ComposeError("quantity must be an int (in satoshi)") # Only for outgoing (incoming will overburn). - balance = ledger.ledger.get_balance(db, source, asset) + balance = ledger.get_balance(db, source, asset) if balance < quantity and not skip_validation: raise exceptions.ComposeError("insufficient funds") @@ -146,9 +147,9 @@ def compose( raise exceptions.ComposeError(problems) if not skip_validation: - asset_id = ledger.ledger.get_asset_id(db, asset, block_index) + asset_id = ledger.get_asset_id(db, asset, block_index) else: - asset_id = ledger.ledger.generate_asset_id(asset, block_index) + asset_id = ledger.generate_asset_id(asset, block_index) short_address_bytes = address.pack(destination) @@ -198,15 +199,15 @@ def parse(db, tx, message): if status == "valid": # verify balance is present - balance = ledger.ledger.get_balance(db, tx["source"], asset) + balance = ledger.get_balance(db, tx["source"], asset) if balance == 0 or balance < quantity: status = "invalid: insufficient funds" if status == "valid": - ledger.ledger.debit( + ledger.debit( db, tx["source"], asset, quantity, tx["tx_index"], action="send", event=tx["tx_hash"] ) - ledger.ledger.credit( + ledger.credit( db, destination, asset, quantity, tx["tx_index"], action="send", event=tx["tx_hash"] ) @@ -221,11 +222,11 @@ def parse(db, tx, message): "quantity": quantity, "status": status, "memo": memo_bytes, - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "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.ledger.insert_record(db, "sends", bindings, "ENHANCED_SEND") + ledger.insert_record(db, "sends", bindings, "ENHANCED_SEND") logger.info( "Send (Enhanced) %(asset)s from %(source)s to %(destination)s (%(tx_hash)s) [%(status)s]", diff --git a/counterparty-core/counterpartycore/lib/messages/versions/mpma.py b/counterparty-core/counterpartycore/lib/messages/versions/mpma.py index 62ef8598b..5519df928 100644 --- a/counterparty-core/counterpartycore/lib/messages/versions/mpma.py +++ b/counterparty-core/counterpartycore/lib/messages/versions/mpma.py @@ -5,7 +5,8 @@ from bitstring import ReadError -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import messagetype, protocol from counterpartycore.lib.utils import helpers @@ -92,7 +93,7 @@ def validate(db, source, asset_dest_quant_list, block_index): problems.append(f"destination is required for {asset}") if protocol.enabled("options_require_memo"): - results = ledger.ledger.get_addresses(db, address=destination) if destination else None + results = ledger.get_addresses(db, address=destination) if destination else None if results: result = results[0] if ( @@ -134,12 +135,12 @@ def compose( for asset, quantity in out_balances: if protocol.enabled("mpma_subasset_support"): # resolve subassets - asset = ledger.ledger.resolve_subasset_longname(db, asset) # noqa: PLW2901 + asset = ledger.resolve_subasset_longname(db, asset) # noqa: PLW2901 if not isinstance(quantity, int): raise exceptions.ComposeError(f"quantities must be an int (in satoshis) for {asset}") - balance = ledger.ledger.get_balance(db, source, asset) + balance = ledger.get_balance(db, source, asset) if balance < quantity and not skip_validation: raise exceptions.ComposeError(f"insufficient funds for {asset}") @@ -182,12 +183,12 @@ def parse(db, tx, message): if status == "valid": for asset_id in unpacked: try: - asset = ledger.ledger.get_asset_name(db, asset_id, tx["block_index"]) # noqa: F841 + asset = ledger.get_asset_name(db, asset_id, tx["block_index"]) # noqa: F841 except exceptions.AssetNameError as e: # noqa: F841 status = f"invalid: asset {asset_id} invalid at block index {tx['block_index']}" break - balance = ledger.ledger.get_balance(db, tx["source"], asset_id) + balance = ledger.get_balance(db, tx["source"], asset_id) if not balance: status = f"invalid: insufficient funds for asset {asset_id}, address {tx['source']} has no balance" break @@ -215,7 +216,7 @@ def parse(db, tx, message): if status == "valid": for op in all_credits: - ledger.ledger.credit( + ledger.credit( db, op["destination"], op["asset"], @@ -226,7 +227,7 @@ def parse(db, tx, message): ) for op in all_debits: - ledger.ledger.debit( + ledger.debit( db, tx["source"], op["asset"], @@ -254,11 +255,11 @@ def parse(db, tx, message): "quantity": op[2], "status": status, "memo": memo_bytes, - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "msg_index": ledger.get_send_msg_index(db, tx["tx_hash"]), "send_type": "send", } - ledger.ledger.insert_record(db, "sends", bindings, "MPMA_SEND") + ledger.insert_record(db, "sends", bindings, "MPMA_SEND") logger.info( "Send (MPMA) %(asset)s from %(source)s to %(destination)s (%(tx_hash)s) [%(status)s]", diff --git a/counterparty-core/counterpartycore/lib/messages/versions/send1.py b/counterparty-core/counterpartycore/lib/messages/versions/send1.py index e4eb1ab61..f906d02c0 100644 --- a/counterparty-core/counterpartycore/lib/messages/versions/send1.py +++ b/counterparty-core/counterpartycore/lib/messages/versions/send1.py @@ -3,7 +3,8 @@ import logging import struct -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import dispense from counterpartycore.lib.parser import messagetype, protocol @@ -20,7 +21,7 @@ def unpack(db, message, block_index): # Only used for `unpack` API call at the moment. try: asset_id, quantity = struct.unpack(FORMAT, message) - asset = ledger.ledger.get_asset_name(db, asset_id, block_index) + asset = ledger.get_asset_name(db, asset_id, block_index) except struct.error: raise exceptions.UnpackError("could not unpack") # noqa: B904 @@ -57,7 +58,7 @@ def validate(db, source, destination, asset, quantity, block_index): # Check destination address options cursor = db.cursor() - results = ledger.ledger.get_addresses(db, address=destination) + results = ledger.get_addresses(db, address=destination) if results: result = results[0] if result and helpers.active_options( @@ -90,14 +91,14 @@ def compose( return compose_send_btc(db, source, destination, quantity) # resolve subassets - asset = ledger.ledger.resolve_subasset_longname(db, asset) + asset = ledger.resolve_subasset_longname(db, asset) # quantity must be in int satoshi (not float, string, etc) if not isinstance(quantity, int): raise exceptions.ComposeError("quantity must be an int (in satoshi)") # Only for outgoing (incoming will overburn). - balance = ledger.ledger.get_balance(db, source, asset) + balance = ledger.get_balance(db, source, asset) if balance < quantity and not skip_validation: raise exceptions.ComposeError("insufficient funds") @@ -107,7 +108,7 @@ def compose( if problems and not skip_validation: raise exceptions.ComposeError(problems) - asset_id = ledger.ledger.get_asset_id(db, asset, block_index) + asset_id = ledger.get_asset_id(db, asset, block_index) data = messagetype.pack(ID) data += struct.pack(FORMAT, asset_id, quantity) @@ -123,7 +124,7 @@ def parse(db, tx, message): if len(message) != LENGTH: raise exceptions.UnpackError asset_id, quantity = struct.unpack(FORMAT, message) - asset = ledger.ledger.get_asset_name(db, asset_id, tx["block_index"]) + asset = ledger.get_asset_name(db, asset_id, tx["block_index"]) status = "valid" except (exceptions.UnpackError, exceptions.AssetNameError, struct.error) as e: # noqa: F841 asset, quantity = None, None @@ -133,9 +134,7 @@ def parse(db, tx, message): # Oversend # doesn't make sense (0 and no balance should be the same) but let's not break the protocol try: - balance = ledger.ledger.get_balance( - db, tx["source"], asset, raise_error_if_no_balance=True - ) + balance = ledger.get_balance(db, tx["source"], asset, raise_error_if_no_balance=True) if balance < quantity: quantity = min(balance, quantity) except exceptions.BalanceError: @@ -151,10 +150,10 @@ def parse(db, tx, message): status = "invalid: " + "; ".join(problems) if status == "valid": - ledger.ledger.debit( + ledger.debit( db, tx["source"], asset, quantity, tx["tx_index"], action="send", event=tx["tx_hash"] ) - ledger.ledger.credit( + ledger.credit( db, tx["destination"], asset, @@ -174,11 +173,11 @@ def parse(db, tx, message): "asset": asset, "quantity": quantity, "status": status, - "msg_index": ledger.ledger.get_send_msg_index(db, tx["tx_hash"]), + "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.ledger.insert_record(db, "sends", bindings, "SEND") + ledger.insert_record(db, "sends", bindings, "SEND") logger.info( "Send %(asset)s from %(source)s to %(destination)s (%(tx_hash)s) [%(status)s]", bindings diff --git a/counterparty-core/counterpartycore/lib/monitors/telemetry/collectors/base.py b/counterparty-core/counterpartycore/lib/monitors/telemetry/collectors/base.py index eb1bac24b..ad9926d78 100644 --- a/counterparty-core/counterpartycore/lib/monitors/telemetry/collectors/base.py +++ b/counterparty-core/counterpartycore/lib/monitors/telemetry/collectors/base.py @@ -2,7 +2,8 @@ import os import counterpartycore.lib.monitors.telemetry.util as util -from counterpartycore.lib import config, ledger +from counterpartycore.lib import config +from counterpartycore.lib.ledger import ledger from .interface import TelemetryCollectorI @@ -31,7 +32,7 @@ def collect(self): force_enabled = util.is_force_enabled() platform = util.get_system() - block_index = ledger.ledger.last_message(self.db)["block_index"] + block_index = ledger.last_message(self.db)["block_index"] cursor = self.db.cursor() last_block = cursor.execute( "SELECT * FROM blocks where block_index = ?", [block_index] diff --git a/counterparty-core/counterpartycore/lib/parser/blocks.py b/counterparty-core/counterpartycore/lib/parser/blocks.py index e4e731323..3324e4f12 100644 --- a/counterparty-core/counterpartycore/lib/parser/blocks.py +++ b/counterparty-core/counterpartycore/lib/parser/blocks.py @@ -17,10 +17,10 @@ backend, config, exceptions, - ledger, ) from counterpartycore.lib.backend import rsfetcher from counterpartycore.lib.cli import log +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import ( attach, @@ -92,7 +92,7 @@ def update_transaction(db, tx, supported): - ledger.ledger.add_to_journal( + ledger.add_to_journal( db, tx["block_index"], "parse", @@ -270,7 +270,7 @@ def replay_transactions_events(db, transactions): "utxos_info": tx["utxos_info"], "transaction_type": tx["transaction_type"], } - ledger.ledger.add_to_journal( + ledger.add_to_journal( db, tx["block_index"], "insert", @@ -291,7 +291,7 @@ def replay_transactions_events(db, transactions): "destination": next_out["destination"], "btc_amount": next_out["btc_amount"], } - ledger.ledger.add_to_journal( + ledger.add_to_journal( db, tx["block_index"], "insert", @@ -329,7 +329,7 @@ def parse_block( if reparsing: replay_transactions_events(db, transactions) - ledger.ledger.ConsensusHashBuilder().reset() + ledger.ConsensusHashBuilder().reset() if block_index != config.MEMPOOL_BLOCK_INDEX: assert block_index == CurrentState().current_block_index() @@ -370,13 +370,13 @@ def parse_block( db, "ledger_hash", previous_ledger_hash, - ledger.ledger.ConsensusHashBuilder().block_ledger(), + ledger.ConsensusHashBuilder().block_ledger(), ) new_messages_hash, found_messages_hash = check.consensus_hash( db, "messages_hash", previous_messages_hash, - ledger.ledger.ConsensusHashBuilder().block_journal(), + ledger.ConsensusHashBuilder().block_journal(), ) update_block_query = """ @@ -398,7 +398,7 @@ def parse_block( cursor.execute(update_block_query, update_block_bindings) # trigger BLOCK_PARSED event - ledger.ledger.add_to_journal( + ledger.add_to_journal( db, block_index, "parse", @@ -434,7 +434,7 @@ def list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index, decoded_ if block_hash is None or block_hash == config.MEMPOOL_BLOCK_HASH: block_hash = config.MEMPOOL_BLOCK_HASH block_index = config.MEMPOOL_BLOCK_INDEX - existing_tx = ledger.ledger.get_transaction(db, tx_hash) + existing_tx = ledger.get_transaction(db, tx_hash) if existing_tx: CurrentState().set_current_tx_hash(None) return tx_index @@ -471,7 +471,7 @@ def list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index, decoded_ data, destination, utxos_info, block_index ), } - ledger.ledger.insert_record(db, "transactions", transaction_bindings, "NEW_TRANSACTION") + ledger.insert_record(db, "transactions", transaction_bindings, "NEW_TRANSACTION") if dispensers_outs: for next_out in dispensers_outs: @@ -483,7 +483,7 @@ def list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index, decoded_ "destination": next_out["destination"], "btc_amount": next_out["btc_amount"], } - ledger.ledger.insert_record( + ledger.insert_record( db, "transaction_outputs", transaction_outputs_bindings, @@ -619,7 +619,7 @@ def reparse(db, block_index=0): CurrentState().set_current_block_index(block["block_index"]) # Add event manually to journal because block already exists - ledger.ledger.add_to_journal( + ledger.add_to_journal( db, block["block_index"], "insert", @@ -637,7 +637,7 @@ def reparse(db, block_index=0): previous_txlist_hash = None previous_messages_hash = None if CurrentState().current_block_index() > config.BLOCK_FIRST: - previous_block = ledger.ledger.get_block(db, block["block_index"] - 1) + previous_block = ledger.get_block(db, block["block_index"] - 1) previous_ledger_hash = previous_block["ledger_hash"] previous_txlist_hash = previous_block["txlist_hash"] previous_messages_hash = previous_block["messages_hash"] @@ -693,7 +693,7 @@ def handle_reorg(db): previous_block_index -= 1 continue - if previous_block_hash != ledger.ledger.get_block_hash(db, previous_block_index): + if previous_block_hash != ledger.get_block_hash(db, previous_block_index): # hashes don't match logger.debug(f"Hashes don't match ({previous_block_index}).") previous_block_index -= 1 @@ -735,7 +735,7 @@ def parse_new_block(db, decoded_block, tx_index=None): } else: # get previous block - previous_block = ledger.ledger.get_block(db, CurrentState().current_block_index() - 1) + previous_block = ledger.get_block(db, CurrentState().current_block_index() - 1) # check if reorg is needed if decoded_block["hash_prev"] != previous_block["block_hash"]: logger.warning( @@ -761,7 +761,7 @@ def parse_new_block(db, decoded_block, tx_index=None): "previous_block_hash": decoded_block["hash_prev"], "difficulty": decoded_block["bits"], } - ledger.ledger.insert_record(db, "blocks", block_bindings, "NEW_BLOCK") + ledger.insert_record(db, "blocks", block_bindings, "NEW_BLOCK") # save transactions for transaction in decoded_block["transactions"]: @@ -832,7 +832,7 @@ def check_database_version(db): elif e.required_action == "reparse": reparse(db, block_index=e.from_block_index) # refresh the current block index - CurrentState().set_current_block_index(ledger.ledger.last_db_index(db)) + CurrentState().set_current_block_index(ledger.last_db_index(db)) # update the database version database.update_version(db) @@ -859,7 +859,7 @@ def catch_up(db, check_asset_conservation=True): try: CurrentState().set_block_parser_status("catching up") # update the current block index - current_block_index = ledger.ledger.last_db_index(db) + current_block_index = ledger.last_db_index(db) if current_block_index == 0: logger.info("New database.") current_block_index = config.BLOCK_FIRST - 1 diff --git a/counterparty-core/counterpartycore/lib/parser/check.py b/counterparty-core/counterpartycore/lib/parser/check.py index 610f3fb39..37fde2336 100644 --- a/counterparty-core/counterpartycore/lib/parser/check.py +++ b/counterparty-core/counterpartycore/lib/parser/check.py @@ -7,7 +7,8 @@ import requests -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.utils import database @@ -978,8 +979,8 @@ def consensus_hash(db, field, previous_consensus_hash, content): def asset_conservation(db, stop_event=None): logger.debug("Checking for conservation of assets.") with db: - supplies = ledger.ledger.supplies(db) - held = ledger.ledger.held(db) + supplies = ledger.supplies(db) + held = ledger.held(db) for asset in supplies.keys(): if stop_event is not None and stop_event.is_set(): logger.debug("Stop event received. Exiting asset conservation check...") @@ -989,15 +990,15 @@ def asset_conservation(db, stop_event=None): if asset_issued != asset_held: raise exceptions.SanityError( "{} {} issued ≠ {} {} held".format( - ledger.ledger.value_out(db, asset_issued, asset), + ledger.value_out(db, asset_issued, asset), asset, - ledger.ledger.value_out(db, asset_held, asset), + ledger.value_out(db, asset_held, asset), asset, ) ) logger.trace( "{} has been conserved ({} {} both issued and held)".format( - asset, ledger.ledger.value_out(db, asset_issued, asset), asset + asset, ledger.value_out(db, asset_issued, asset), asset ) ) logger.debug("All assets have been conserved.") diff --git a/counterparty-core/counterpartycore/lib/parser/follow.py b/counterparty-core/counterpartycore/lib/parser/follow.py index 463cced86..a6c173bc3 100644 --- a/counterparty-core/counterpartycore/lib/parser/follow.py +++ b/counterparty-core/counterpartycore/lib/parser/follow.py @@ -13,8 +13,8 @@ backend, config, exceptions, - ledger, ) +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.monitors import sentry from counterpartycore.lib.monitors.telemetry.oneshot import TelemetryOneShot @@ -127,9 +127,9 @@ def receive_rawblock(self, body): block_index=CurrentState().current_block_index() + 1, ) # check if already parsed by block.catch_up() - existing_block = ledger.ledger.get_block_by_hash(self.db, decoded_block["block_hash"]) + existing_block = ledger.get_block_by_hash(self.db, decoded_block["block_hash"]) if existing_block is None: - previous_block = ledger.ledger.get_block_by_hash(self.db, decoded_block["hash_prev"]) + previous_block = ledger.get_block_by_hash(self.db, decoded_block["hash_prev"]) if previous_block is None: # catch up with rpc if previous block is missing logger.debug("Previous block is missing. Catching up...") @@ -244,7 +244,7 @@ async def receive_multipart(self, socket, topic_name): raise e def is_late(self): - last_parsed_block = ledger.ledger.get_last_block(self.db) + last_parsed_block = ledger.get_last_block(self.db) if last_parsed_block: last_parsed_block_index = last_parsed_block["block_index"] bitcoind_block_index = backend.bitcoind.getblockcount() diff --git a/counterparty-core/counterpartycore/lib/parser/gettxinfo.py b/counterparty-core/counterpartycore/lib/parser/gettxinfo.py index 41c18b50c..359135604 100644 --- a/counterparty-core/counterpartycore/lib/parser/gettxinfo.py +++ b/counterparty-core/counterpartycore/lib/parser/gettxinfo.py @@ -6,8 +6,9 @@ from arc4 import ARC4 from bitcoinutils.keys import PublicKey -from counterpartycore.lib import backend, config, exceptions, ledger +from counterpartycore.lib import backend, config, exceptions from counterpartycore.lib.exceptions import BTCOnlyError, DecodeError +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import dispenser from counterpartycore.lib.parser import gettxinfolegacy, messagetype, p2sh, protocol @@ -482,7 +483,7 @@ def get_inputs_with_balance(db, decoded_tx): # we check that each vin does not contain assets.. for vin in decoded_tx["vin"]: utxo = vin["hash"] + ":" + str(vin["n"]) - if ledger.ledger.utxo_has_balance(db, utxo): + if ledger.utxo_has_balance(db, utxo): sources.append(utxo) return sources @@ -536,13 +537,13 @@ def update_utxo_balances_cache(db, utxos_info, data, destination, block_index): ) if utxos_info[0] != "": # always remove from cache inputs with balance - ledger.ledger.UTXOBalancesCache(db).remove_balance(utxos_info[0]) + ledger.UTXOBalancesCache(db).remove_balance(utxos_info[0]) # add to cache the destination if it's not a detach if utxos_info[1] != "" and transaction_type != "detach": - ledger.ledger.UTXOBalancesCache(db).add_balance(utxos_info[1]) + ledger.UTXOBalancesCache(db).add_balance(utxos_info[1]) elif utxos_info[1] != "" and transaction_type == "attach": # add to cache the destination if it's an attach - ledger.ledger.UTXOBalancesCache(db).add_balance(utxos_info[1]) + ledger.UTXOBalancesCache(db).add_balance(utxos_info[1]) def get_tx_info(db, decoded_tx, block_index, composing=False): diff --git a/counterparty-core/counterpartycore/lib/parser/mempool.py b/counterparty-core/counterpartycore/lib/parser/mempool.py index a7467bc13..944d61812 100644 --- a/counterparty-core/counterpartycore/lib/parser/mempool.py +++ b/counterparty-core/counterpartycore/lib/parser/mempool.py @@ -2,8 +2,9 @@ import logging import time -from counterpartycore.lib import backend, config, exceptions, ledger +from counterpartycore.lib import backend, config, exceptions from counterpartycore.lib.api.apiwatcher import EVENTS_ADDRESS_FIELDS +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import blocks, deserialize @@ -48,7 +49,7 @@ def parse_mempool_transactions(db, raw_tx_list, timestamps=None): decoded_tx_count = 0 for raw_tx in raw_tx_list: decoded_tx = deserialize.deserialize_tx(raw_tx, parse_vouts=True) - existing_tx = ledger.ledger.get_transaction(db, decoded_tx["tx_hash"]) + existing_tx = ledger.get_transaction(db, decoded_tx["tx_hash"]) not_supported_txs.append(decoded_tx["tx_hash"]) if existing_tx: logger.trace(f"Transaction {decoded_tx['tx_hash']} already in the database") @@ -127,7 +128,7 @@ def clean_mempool(db): mempool_events = cursor.fetchall() for event in mempool_events: # print(event) - tx = ledger.ledger.get_transaction(db, event["tx_hash"]) + tx = ledger.get_transaction(db, event["tx_hash"]) if tx: clean_transaction_events(db, event["tx_hash"]) # remove transactions removed from the mempool diff --git a/counterparty-core/counterpartycore/lib/utils/mpmaencoding.py b/counterparty-core/counterpartycore/lib/utils/mpmaencoding.py index 1dbfc5eee..24651fa6c 100644 --- a/counterparty-core/counterpartycore/lib/utils/mpmaencoding.py +++ b/counterparty-core/counterpartycore/lib/utils/mpmaencoding.py @@ -3,7 +3,8 @@ import struct from bitstring import BitArray, ConstBitStream -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.utils import address logger = logging.getLogger(config.LOGGER_NAME) @@ -85,8 +86,8 @@ def _encode_construct_send_list(send_asset, lut, sends): def _solve_asset(db, asset_name, block_index): - asset = ledger.ledger.resolve_subasset_longname(db, asset_name) - return ledger.ledger.get_asset_id(db, asset, block_index) + asset = ledger.resolve_subasset_longname(db, asset_name) + return ledger.get_asset_id(db, asset, block_index) def _encode_compress_send_list(db, nbits, send, block_index): @@ -187,7 +188,7 @@ def _decode_decode_send_list(stream, nbits, lut, block_index): num_recipients = 1 range_limit = num_recipients send_list = [] - asset = ledger.ledger.generate_asset_name(asset_id, block_index) + asset = ledger.generate_asset_name(asset_id, block_index) for i in range(0, range_limit): # noqa: B007 if nbits > 0: idx = stream.read(f"uint:{nbits}") diff --git a/counterparty-core/counterpartycore/test/complex_unit_test.py b/counterparty-core/counterpartycore/test/complex_unit_test.py index ce7a5763d..18d90e238 100644 --- a/counterparty-core/counterpartycore/test/complex_unit_test.py +++ b/counterparty-core/counterpartycore/test/complex_unit_test.py @@ -4,8 +4,8 @@ import pytest from apsw import ConstraintError -from counterpartycore.lib import ledger from counterpartycore.lib.api import apiv1 +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.parser import blocks # this is require near the top to do setup of the test suite @@ -36,8 +36,8 @@ def test_alice_bob(server_db, cp_server, apiserver): assert utxos[0]["confirmations"] == 74 # balance before send - alice_balance = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance = ledger.get_balance(server_db, alice, "XCP") + bob_balance = ledger.get_balance(server_db, bob, "XCP") assert alice_balance == 91674999900 assert bob_balance == 0 @@ -63,8 +63,8 @@ def test_alice_bob(server_db, cp_server, apiserver): # time.sleep(10) # balances after send - alice_balance2 = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance2 = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance2 = ledger.get_balance(server_db, alice, "XCP") + bob_balance2 = ledger.get_balance(server_db, bob, "XCP") assert alice_balance2 == alice_balance - v assert bob_balance2 == bob_balance + v @@ -95,8 +95,8 @@ def test_alice_bob(server_db, cp_server, apiserver): assert utxos[0]["confirmations"] == 1 # balances before send - alice_balance = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance = ledger.get_balance(server_db, alice, "XCP") + bob_balance = ledger.get_balance(server_db, bob, "XCP") assert alice_balance == alice_balance2 assert bob_balance == bob_balance2 @@ -121,8 +121,8 @@ def test_alice_bob(server_db, cp_server, apiserver): tx2hash, tx2 = util_test.insert_raw_transaction(send2hex, server_db) # balances after send - alice_balance2 = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance2 = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance2 = ledger.get_balance(server_db, alice, "XCP") + bob_balance2 = ledger.get_balance(server_db, bob, "XCP") assert alice_balance2 == alice_balance - v assert bob_balance2 == bob_balance + v @@ -137,8 +137,8 @@ def test_alice_bob(server_db, cp_server, apiserver): assert utxos[0]["confirmations"] == 1 # balances before send - alice_balance = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance = ledger.get_balance(server_db, alice, "XCP") + bob_balance = ledger.get_balance(server_db, bob, "XCP") assert alice_balance == alice_balance2 assert bob_balance == bob_balance2 @@ -163,8 +163,8 @@ def test_alice_bob(server_db, cp_server, apiserver): tx3 = util_test.insert_unconfirmed_raw_transaction(send3hex, server_db) # balances after send, unaffected - alice_balance2 = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance2 = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance2 = ledger.get_balance(server_db, alice, "XCP") + bob_balance2 = ledger.get_balance(server_db, bob, "XCP") assert alice_balance2 == alice_balance assert bob_balance2 == bob_balance @@ -197,8 +197,8 @@ def test_alice_bob(server_db, cp_server, apiserver): tx3bhash, tx3b = util_test.insert_raw_transaction(send3hex, server_db) # balances after send - alice_balance2 = ledger.ledger.get_balance(server_db, alice, "XCP") - bob_balance2 = ledger.ledger.get_balance(server_db, bob, "XCP") + alice_balance2 = ledger.get_balance(server_db, alice, "XCP") + bob_balance2 = ledger.get_balance(server_db, bob, "XCP") assert alice_balance2 == alice_balance - v assert bob_balance2 == bob_balance + v diff --git a/counterparty-core/counterpartycore/test/conftest.py b/counterparty-core/counterpartycore/test/conftest.py index eb89e0a09..40781155d 100644 --- a/counterparty-core/counterpartycore/test/conftest.py +++ b/counterparty-core/counterpartycore/test/conftest.py @@ -17,11 +17,12 @@ import requests from bitcoinutils.keys import PublicKey -from counterpartycore.lib import config, exceptions, ledger +from counterpartycore.lib import config, exceptions from counterpartycore.lib.api import apiserver as api_v2 from counterpartycore.lib.api import apiv1 as api from counterpartycore.lib.api import dbbuilder from counterpartycore.lib.cli import log, server +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.parser import gettxinfo, protocol, utxosinfo from counterpartycore.lib.utils import assetnames, base58, database, multisig, opcodes, script @@ -699,7 +700,7 @@ def convert_to_psbt(tx_hex): monkeypatch.setattr( "counterpartycore.lib.ledger.ledger.get_matching_orders", - ledger.ledger.get_matching_orders_no_cache, + ledger.get_matching_orders_no_cache, ) monkeypatch.setattr( @@ -711,13 +712,13 @@ def convert_to_psbt(tx_hex): monkeypatch.setattr( "counterpartycore.lib.ledger.ledger.asset_issued_total", - ledger.ledger.asset_issued_total_no_cache, + ledger.asset_issued_total_no_cache, ) monkeypatch.setattr( "counterpartycore.lib.ledger.ledger.get_last_issuance", - ledger.ledger.get_last_issuance_no_cache, + ledger.get_last_issuance_no_cache, ) monkeypatch.setattr( "counterpartycore.lib.ledger.ledger.asset_destroyed_total", - ledger.ledger.asset_destroyed_total_no_cache, + ledger.asset_destroyed_total_no_cache, ) diff --git a/counterparty-core/counterpartycore/test/telemetry_test.py b/counterparty-core/counterpartycore/test/telemetry_test.py index fd055fc3c..bc73c197b 100644 --- a/counterparty-core/counterpartycore/test/telemetry_test.py +++ b/counterparty-core/counterpartycore/test/telemetry_test.py @@ -40,7 +40,7 @@ class TestTelemetryCollectorBase: @patch("counterpartycore.lib.monitors.telemetry.collectors.base.ledger") def test_collect(self, mock_ledger, mock_config): mock_db = MagicMock() - mock_ledger.ledger.last_message.return_value = {"block_index": 12345} + mock_ledger.last_message.return_value = {"block_index": 12345} mock_config.__version__ = "1.2.3" mock_config.TESTNET = False mock_config.TESTNET4 = False @@ -52,7 +52,7 @@ def test_collect(self, mock_ledger, mock_config): print("\n\n\n", data) - mock_ledger.ledger.last_message.assert_called_with(mock_db) + mock_ledger.last_message.assert_called_with(mock_db) mock_db.cursor().execute.assert_called_with( "SELECT * FROM blocks where block_index = ?", [12345] ) @@ -68,7 +68,7 @@ def test_collect(self, mock_ledger, mock_config): def test_collect_with_docker(self, mock_exists, mock_ledger): mock_db = MagicMock() mock_exists.return_value = True - mock_ledger.ledger.last_message.return_value = {"block_index": 12345} + mock_ledger.last_message.return_value = {"block_index": 12345} collector = TelemetryCollectorBase(mock_db) data = collector.collect() assert data["dockerized"] == True # noqa: E712 diff --git a/counterparty-core/counterpartycore/test/util_test.py b/counterparty-core/counterpartycore/test/util_test.py index 447f55853..72b0a62aa 100644 --- a/counterparty-core/counterpartycore/test/util_test.py +++ b/counterparty-core/counterpartycore/test/util_test.py @@ -30,11 +30,11 @@ from counterpartycore.lib import ( config, exceptions, - ledger, messages, ) from counterpartycore.lib.api import composer from counterpartycore.lib.cli import server +from counterpartycore.lib.ledger import ledger from counterpartycore.lib.ledger.currentstate import CurrentState from counterpartycore.lib.messages import dispenser, fairminter, utxo # noqa from counterpartycore.lib.parser import blocks, check, deserialize, gettxinfo @@ -161,7 +161,7 @@ def insert_block(db, block_index, parse_block=True): "difficulty": None, } CurrentState().set_current_block_index(block_index) - ledger.ledger.insert_record(db, "blocks", bindings, "NEW_BLOCK") + ledger.insert_record(db, "blocks", bindings, "NEW_BLOCK") if parse_block: blocks.parse_block(db, block_index, block_time) @@ -226,7 +226,7 @@ def insert_raw_transaction(raw_transaction, db): "supported": True, "utxos_info": " ".join(utxos_info), } - ledger.ledger.insert_record(db, "transactions", bindings, "NEW_TRANSACTION") + ledger.insert_record(db, "transactions", bindings, "NEW_TRANSACTION") tx = list(cursor.execute("""SELECT * FROM transactions WHERE tx_index = ?""", (tx_index,)))[ 0 @@ -335,8 +335,8 @@ def insert_transaction(transaction, db): "previous_block_hash": None, "difficulty": None, } - ledger.ledger.insert_record(db, "blocks", block_bindings, "NEW_BLOCK") - ledger.ledger.insert_record(db, "transactions", transaction, "NEW_TRANSACTION") + ledger.insert_record(db, "blocks", block_bindings, "NEW_BLOCK") + ledger.insert_record(db, "transactions", transaction, "NEW_TRANSACTION") # `dispenser.dispense()` needs some vouts. Let's say one vout per transaction. transaction_outputs_bindings = { @@ -347,7 +347,7 @@ def insert_transaction(transaction, db): "destination": transaction["destination"], "btc_amount": transaction["btc_amount"], } - ledger.ledger.insert_record( + ledger.insert_record( db, "transaction_outputs", transaction_outputs_bindings, "NEW_TRANSACTION_OUTPUT" ) @@ -618,7 +618,7 @@ def _getrawtransaction_batch(txhash_list, verbose=False): def initialise_db(db): """Initialise blockchain in the db and insert first block.""" insert_block(db, config.BLOCK_FIRST - 1, parse_block=True) - CurrentState().set_current_block_index(ledger.ledger.last_db_index(db)) + CurrentState().set_current_block_index(ledger.last_db_index(db)) def run_scenario(scenario): @@ -648,7 +648,7 @@ def run_scenario(scenario): db.execute(sql_file.read()) initialise_db(db) - ledger.ledger.AssetCache(db).init(db) + ledger.AssetCache(db).init(db) raw_transactions = [] @@ -1045,7 +1045,7 @@ def compare_strings(string1, string2): def get_block_ledger(db, block_index): - """Return the block's ledger.ledger.""" + """Return the block's ledger.""" cursor = db.cursor() debits = list(cursor.execute("""SELECT * FROM debits WHERE block_index = ?""", (block_index,))) credits = list(