diff --git a/counterparty-core/counterpartycore/lib/api/api_v1.py b/counterparty-core/counterpartycore/lib/api/api_v1.py index 7f96fc24a..ddde735b4 100644 --- a/counterparty-core/counterpartycore/lib/api/api_v1.py +++ b/counterparty-core/counterpartycore/lib/api/api_v1.py @@ -60,6 +60,7 @@ is_docker, is_force_enabled, ) +from counterpartycore.lib.utils import helpers D = decimal.Decimal @@ -1037,14 +1038,14 @@ def get_dispenser_info(tx_hash=None, tx_index=None): oracle_fiat_label = "" if dispenser["oracle_address"] != None: # noqa: E711 - fiat_price = util.satoshirate_to_fiat(dispenser["satoshirate"]) + fiat_price = helpers.satoshirate_to_fiat(dispenser["satoshirate"]) ( oracle_price, oracle_fee, oracle_fiat_label, oracle_price_last_updated, ) = ledger.get_oracle_last_price( - db, dispenser["oracle_address"], util.CURRENT_BLOCK_INDEX + db, dispenser["oracle_address"], helpers.CURRENT_BLOCK_INDEX ) if oracle_price > 0: diff --git a/counterparty-core/counterpartycore/lib/api/api_watcher.py b/counterparty-core/counterpartycore/lib/api/api_watcher.py index 67b1402c1..c5d7c211e 100644 --- a/counterparty-core/counterpartycore/lib/api/api_watcher.py +++ b/counterparty-core/counterpartycore/lib/api/api_watcher.py @@ -7,7 +7,7 @@ from counterpartycore.lib import config, database, exceptions from counterpartycore.lib.api import dbbuilder from counterpartycore.lib.parser import utxosinfo -from counterpartycore.lib.util import format_duration +from counterpartycore.lib.utils.helpers import format_duration logger = logging.getLogger(config.LOGGER_NAME) diff --git a/counterparty-core/counterpartycore/lib/api/util.py b/counterparty-core/counterpartycore/lib/api/util.py index 48caf50ad..d82a1e1c8 100644 --- a/counterparty-core/counterpartycore/lib/api/util.py +++ b/counterparty-core/counterpartycore/lib/api/util.py @@ -22,6 +22,7 @@ util, ) from counterpartycore.lib.api import compose +from counterpartycore.lib.utils import helpers D = decimal.Decimal logger = logging.getLogger(config.LOGGER_NAME) @@ -154,7 +155,7 @@ def get_backend_height(): return block_count + blocks_behind -class BackendHeight(metaclass=util.SingletonMeta): +class BackendHeight(metaclass=helpers.SingletonMeta): def __init__(self): self.backend_height = get_backend_height() self.last_update = time.time() @@ -638,14 +639,14 @@ def inject_fiat_price(ledger_db, dispenser): if "satoshirate" not in dispenser: return dispenser if dispenser["oracle_address"] != None: # noqa: E711 - dispenser["fiat_price"] = util.satoshirate_to_fiat(dispenser["satoshirate"]) + dispenser["fiat_price"] = helpers.satoshirate_to_fiat(dispenser["satoshirate"]) ( dispenser["oracle_price"], _oracle_fee, dispenser["fiat_unit"], dispenser["oracle_price_last_updated"], ) = ledger.get_oracle_last_price( - ledger_db, dispenser["oracle_address"], util.CURRENT_BLOCK_INDEX + ledger_db, dispenser["oracle_address"], helpers.CURRENT_BLOCK_INDEX ) if dispenser["oracle_price"] > 0: diff --git a/counterparty-core/counterpartycore/lib/backend/rsfetcher.py b/counterparty-core/counterpartycore/lib/backend/rsfetcher.py index 42c9eb969..c7102d2e8 100644 --- a/counterparty-core/counterpartycore/lib/backend/rsfetcher.py +++ b/counterparty-core/counterpartycore/lib/backend/rsfetcher.py @@ -9,8 +9,9 @@ from counterparty_rs import indexer -from counterpartycore.lib import config, exceptions, util +from counterpartycore.lib import config, exceptions from counterpartycore.lib.parser import protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -28,7 +29,7 @@ def delete_database_directory(): logger.debug(f"RSFetcher - Reset database at {config.FETCHER_DB}") -class RSFetcher(metaclass=util.SingletonMeta): +class RSFetcher(metaclass=helpers.SingletonMeta): thread_index_counter = 0 # Add a thread index counter def __init__(self, indexer_config=None): diff --git a/counterparty-core/counterpartycore/lib/cli/bootstrap.py b/counterparty-core/counterpartycore/lib/cli/bootstrap.py index a05d2e390..852b40d67 100644 --- a/counterparty-core/counterpartycore/lib/cli/bootstrap.py +++ b/counterparty-core/counterpartycore/lib/cli/bootstrap.py @@ -74,7 +74,6 @@ def verify_signature(public_key_data, signature_path, snapshot_path): verified = gpg.verify_file(s, snapshot_path, close_file=False) finally: pass - # shutil.rmtree(temp_dir) return verified diff --git a/counterparty-core/counterpartycore/lib/cli/log.py b/counterparty-core/counterpartycore/lib/cli/log.py index 15f1be83c..9117983a8 100644 --- a/counterparty-core/counterpartycore/lib/cli/log.py +++ b/counterparty-core/counterpartycore/lib/cli/log.py @@ -16,6 +16,7 @@ from counterpartycore.lib import config, util from counterpartycore.lib.api.util import to_json +from counterpartycore.lib.utils import helpers logging.TRACE = logging.DEBUG - 5 logging.addLevelName(logging.TRACE, "TRACE") @@ -372,7 +373,7 @@ def shutdown(): logging.shutdown() -class ZmqPublisher(metaclass=util.SingletonMeta): +class ZmqPublisher(metaclass=helpers.SingletonMeta): def __init__(self): self.context = zmq.Context() self.socket = self.context.socket(zmq.PUB) diff --git a/counterparty-core/counterpartycore/lib/cli/server.py b/counterparty-core/counterpartycore/lib/cli/server.py index dcad412f6..1c027aee2 100755 --- a/counterparty-core/counterpartycore/lib/cli/server.py +++ b/counterparty-core/counterpartycore/lib/cli/server.py @@ -21,12 +21,12 @@ database, exceptions, ledger, - util, ) from counterpartycore.lib.api import api_server as api_v2 from counterpartycore.lib.api import api_v1, dbbuilder from counterpartycore.lib.cli import bootstrap, log from counterpartycore.lib.parser import blocks, check, follow +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) D = decimal.Decimal @@ -523,7 +523,7 @@ def initialise_config( config.GUNICORN_WORKERS = gunicorn_workers if electrs_url: - if not util.is_url(electrs_url): + if not helpers.is_url(electrs_url): raise ConfigurationError("Invalid Electrs URL") config.ELECTRS_URL = electrs_url else: diff --git a/counterparty-core/counterpartycore/lib/composer.py b/counterparty-core/counterpartycore/lib/composer.py index c85e55384..715b6c218 100644 --- a/counterparty-core/counterpartycore/lib/composer.py +++ b/counterparty-core/counterpartycore/lib/composer.py @@ -18,10 +18,9 @@ config, exceptions, ledger, - util, ) from counterpartycore.lib.parser import deserialize, utxosinfo -from counterpartycore.lib.utils import multisig, opcodes, script +from counterpartycore.lib.utils import helpers, multisig, opcodes, script MAX_INPUTS_SET = 100 @@ -243,7 +242,7 @@ def data_to_pubkey_pairs(data, arc4_key): # Two pubkeys, minus length byte, minus prefix, minus two nonces, # minus two sign bytes. chunk_size = (33 * 2) - 1 - len(config.PREFIX) - 2 - 2 - data_array = util.chunkify(data, chunk_size) + data_array = helpers.chunkify(data, chunk_size) pubkey_pairs = [] for data_part in data_array: # Get data (fake) public key. @@ -326,7 +325,7 @@ def prepare_outputs(source, destinations, data, unspent_list, construct_params): ################ -class UTXOLocks(metaclass=util.SingletonMeta): +class UTXOLocks(metaclass=helpers.SingletonMeta): def __init__(self): self.init() @@ -369,7 +368,7 @@ def complete_unspent_list(unspent_list): # get missing data from Bitcoin Core if len(txhash_set) > 0: - txhash_list_chunks = util.chunkify(list(txhash_set), config.MAX_RPC_BATCH_SIZE) + txhash_list_chunks = helpers.chunkify(list(txhash_set), config.MAX_RPC_BATCH_SIZE) txs = {} for txhash_list in txhash_list_chunks: txs = txs | backend.bitcoind.getrawtransaction_batch( diff --git a/counterparty-core/counterpartycore/lib/database.py b/counterparty-core/counterpartycore/lib/database.py index 64598847e..90efd6bd3 100644 --- a/counterparty-core/counterpartycore/lib/database.py +++ b/counterparty-core/counterpartycore/lib/database.py @@ -9,6 +9,7 @@ from termcolor import cprint from counterpartycore.lib import config, exceptions, ledger, util +from counterpartycore.lib.utils import helpers apsw.bestpractice.apply(apsw.bestpractice.recommended) # includes WAL mode @@ -142,12 +143,12 @@ def close(self): db.close() -class LedgerDBConnectionPool(APSWConnectionPool, metaclass=util.SingletonMeta): +class LedgerDBConnectionPool(APSWConnectionPool, metaclass=helpers.SingletonMeta): def __init__(self): super().__init__(config.DATABASE, "Ledger DB") -class StateDBConnectionPool(APSWConnectionPool, metaclass=util.SingletonMeta): +class StateDBConnectionPool(APSWConnectionPool, metaclass=helpers.SingletonMeta): def __init__(self): super().__init__(config.STATE_DATABASE, "API DB") diff --git a/counterparty-core/counterpartycore/lib/ledger.py b/counterparty-core/counterpartycore/lib/ledger.py index 0844be301..c3800c913 100644 --- a/counterparty-core/counterpartycore/lib/ledger.py +++ b/counterparty-core/counterpartycore/lib/ledger.py @@ -9,7 +9,7 @@ from counterpartycore.lib import backend, config, database, exceptions, util from counterpartycore.lib.cli import log from counterpartycore.lib.parser import check, protocol, utxosinfo -from counterpartycore.lib.utils import assetnames +from counterpartycore.lib.utils import assetnames, helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -471,7 +471,7 @@ def get_balance(db, address, asset, raise_error_if_no_balance=False, return_list return balances[0]["quantity"] -class UTXOBalancesCache(metaclass=util.SingletonMeta): +class UTXOBalancesCache(metaclass=helpers.SingletonMeta): def __init__(self, db): logger.debug("Initialising utxo balances cache...") sql = "SELECT utxo, asset, quantity, MAX(rowid) FROM balances WHERE utxo IS NOT NULL GROUP BY utxo, asset" @@ -1044,7 +1044,7 @@ def asset_destroyed_total_no_cache(db, asset): return destroyed_total -class AssetCache(metaclass=util.SingletonMeta): +class AssetCache(metaclass=helpers.SingletonMeta): def __init__(self, db) -> None: self.assets = {} self.assets_total_issued = {} @@ -1899,7 +1899,7 @@ def get_open_btc_orders(db, address): return cursor.fetchall() -class OrdersCache(metaclass=util.SingletonMeta): +class OrdersCache(metaclass=helpers.SingletonMeta): def __init__(self, db): logger.debug("Initialising orders cache...") self.last_cleaning_block_index = 0 diff --git a/counterparty-core/counterpartycore/lib/messages/bet.py b/counterparty-core/counterpartycore/lib/messages/bet.py index 93da6eb39..1715f23a9 100644 --- a/counterparty-core/counterpartycore/lib/messages/bet.py +++ b/counterparty-core/counterpartycore/lib/messages/bet.py @@ -23,6 +23,7 @@ util, ) from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers D = decimal.Decimal @@ -659,7 +660,7 @@ def match(db, tx): logger.debug("Skipping: zero backward quantity.") continue - bet_match_id = util.make_id(tx0["tx_hash"], tx1["tx_hash"]) # noqa: F841 + bet_match_id = helpers.make_id(tx0["tx_hash"], tx1["tx_hash"]) # noqa: F841 # Debit the order. # Counterwager remainings may be negative. @@ -729,7 +730,7 @@ def match(db, tx): # Record bet fulfillment. bindings = { - "id": util.make_id(tx0["tx_hash"], tx["tx_hash"]), + "id": helpers.make_id(tx0["tx_hash"], tx["tx_hash"]), "tx0_index": tx0["tx_index"], "tx0_hash": tx0["tx_hash"], "tx0_address": tx0["source"], diff --git a/counterparty-core/counterpartycore/lib/messages/broadcast.py b/counterparty-core/counterpartycore/lib/messages/broadcast.py index 8be905e2b..952ea7bc0 100644 --- a/counterparty-core/counterpartycore/lib/messages/broadcast.py +++ b/counterparty-core/counterpartycore/lib/messages/broadcast.py @@ -34,6 +34,7 @@ util, ) from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers from . import bet @@ -108,7 +109,7 @@ def validate_address_options(options): raise exceptions.OptionsError("options integer overflow") elif options > config.ADDRESS_OPTION_MAX_VALUE: raise exceptions.OptionsError("options out of range") - elif not util.active_options(config.ADDRESS_OPTION_MAX_VALUE, options): + elif not helpers.active_options(config.ADDRESS_OPTION_MAX_VALUE, options): raise exceptions.OptionsError("options not possible") @@ -323,7 +324,7 @@ def parse(db, tx, message): ) for bet_match in bet_matches: broadcast_bet_match_cursor = db.cursor() - bet_match_id = util.make_id(bet_match["tx0_hash"], bet_match["tx1_hash"]) + bet_match_id = helpers.make_id(bet_match["tx0_hash"], bet_match["tx1_hash"]) bet_match_status = None # Calculate total funds held in escrow and total fee to be paid if @@ -536,7 +537,7 @@ def parse(db, tx, message): # Update the bet match’s status. if bet_match_status: - bet_match_id = util.make_id(bet_match["tx0_hash"], bet_match["tx1_hash"]) + bet_match_id = helpers.make_id(bet_match["tx0_hash"], bet_match["tx1_hash"]) ledger.update_bet_match_status(db, bet_match_id, bet_match_status) logger.info( diff --git a/counterparty-core/counterpartycore/lib/messages/btcpay.py b/counterparty-core/counterpartycore/lib/messages/btcpay.py index 4f38cc517..f0aded862 100644 --- a/counterparty-core/counterpartycore/lib/messages/btcpay.py +++ b/counterparty-core/counterpartycore/lib/messages/btcpay.py @@ -10,6 +10,7 @@ util, ) from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -141,7 +142,7 @@ def unpack(message, return_dict=False): binascii.hexlify(tx0_hash_bytes).decode("utf-8"), binascii.hexlify(tx1_hash_bytes).decode("utf-8"), ) - order_match_id = util.make_id(tx0_hash, tx1_hash) + order_match_id = helpers.make_id(tx0_hash, tx1_hash) status = "valid" except (exceptions.UnpackError, struct.error) as e: # noqa: F841 tx0_hash, tx1_hash, order_match_id = None, None, None diff --git a/counterparty-core/counterpartycore/lib/messages/dispense.py b/counterparty-core/counterpartycore/lib/messages/dispense.py index 1f57a895d..4a4389045 100644 --- a/counterparty-core/counterpartycore/lib/messages/dispense.py +++ b/counterparty-core/counterpartycore/lib/messages/dispense.py @@ -5,6 +5,7 @@ from counterpartycore.lib import config, exceptions, ledger, util from counterpartycore.lib.messages import dispenser as dispenser_module from counterpartycore.lib.parser import protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -20,7 +21,7 @@ def get_must_give(db, dispenser, btc_amount, block_index=None): raise exceptions.NoPriceError( f"No price available for this oracle {dispenser['oracle_address']} at block {block_index}" ) - fiatrate = util.satoshirate_to_fiat(dispenser["satoshirate"]) + fiatrate = helpers.satoshirate_to_fiat(dispenser["satoshirate"]) return int(floor(((btc_amount / config.UNIT) * last_price) / fiatrate)) return int(floor(btc_amount / dispenser["satoshirate"])) diff --git a/counterparty-core/counterpartycore/lib/messages/dispenser.py b/counterparty-core/counterpartycore/lib/messages/dispenser.py index 09d6e3197..0e3981688 100644 --- a/counterparty-core/counterpartycore/lib/messages/dispenser.py +++ b/counterparty-core/counterpartycore/lib/messages/dispenser.py @@ -20,6 +20,7 @@ from counterpartycore.lib.messages.utils.address import pack as address_pack from counterpartycore.lib.messages.utils.address import unpack as address_unpack from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -508,7 +509,7 @@ def calculate_oracle_fee( last_fee_multiplier = last_fee / config.UNIT # Format mainchainrate to ######.## - oracle_mainchainrate = util.satoshirate_to_fiat(mainchainrate) + oracle_mainchainrate = helpers.satoshirate_to_fiat(mainchainrate) oracle_mainchainrate_btc = oracle_mainchainrate / last_price # Calculate the total amount earned for dispenser and the fee @@ -899,7 +900,7 @@ def parse(db, tx, message): cursor.close() -class DispensableCache(metaclass=util.SingletonMeta): +class DispensableCache(metaclass=helpers.SingletonMeta): def __init__(self, db): logger.debug("Initialising Dispensable Cache...") self.dispensable = ledger.get_all_dispensables(db) @@ -925,7 +926,7 @@ def is_dispensable(db, address, amount): last_price, last_fee, last_fiat_label, last_updated = ledger.get_oracle_last_price( db, next_dispenser["oracle_address"], util.CURRENT_BLOCK_INDEX ) - fiatrate = util.satoshirate_to_fiat(next_dispenser["satoshirate"]) + fiatrate = helpers.satoshirate_to_fiat(next_dispenser["satoshirate"]) if fiatrate == 0 or last_price == 0: return False if amount >= fiatrate / last_price: diff --git a/counterparty-core/counterpartycore/lib/messages/order.py b/counterparty-core/counterpartycore/lib/messages/order.py index f4da3eee8..0bc5e51dc 100644 --- a/counterparty-core/counterpartycore/lib/messages/order.py +++ b/counterparty-core/counterpartycore/lib/messages/order.py @@ -12,6 +12,7 @@ util, ) from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) D = decimal.Decimal @@ -672,7 +673,7 @@ def match(db, tx, block_index=None): # Sanity check. Should never happen. if tx0["status"] != "open": raise Exception(f"Order match is not open: {tx0}.") - order_match_id = util.make_id(tx0["tx_hash"], tx1["tx_hash"]) + order_match_id = helpers.make_id(tx0["tx_hash"], tx1["tx_hash"]) if not block_index: block_index = max( ledger.get_order_first_block_index(cursor, tx0["tx_hash"]), @@ -686,10 +687,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.get_order_match(db, id=util.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.get_order_match(db, id=util.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 @@ -931,7 +932,7 @@ def match(db, tx, block_index=None): # Record order match. bindings = { - "id": util.make_id(tx0["tx_hash"], tx["tx_hash"]), + "id": helpers.make_id(tx0["tx_hash"], tx["tx_hash"]), "tx0_index": tx0["tx_index"], "tx0_hash": tx0["tx_hash"], "tx0_address": tx0["source"], diff --git a/counterparty-core/counterpartycore/lib/messages/send.py b/counterparty-core/counterpartycore/lib/messages/send.py index 3f9d1c210..ae5e31cbc 100644 --- a/counterparty-core/counterpartycore/lib/messages/send.py +++ b/counterparty-core/counterpartycore/lib/messages/send.py @@ -1,9 +1,10 @@ import logging import time -from counterpartycore.lib import config, database, exceptions, util +from counterpartycore.lib import config, database, exceptions from counterpartycore.lib.messages.versions import enhanced_send, mpma, send1 from counterpartycore.lib.parser import protocol +from counterpartycore.lib.utils import helpers ID = send1.ID @@ -253,7 +254,7 @@ def compose( return mpma.compose( db, source, - util.flat(zip(asset, destination, quantity, memo, memo_is_hex)), + helpers.flat(zip(asset, destination, quantity, memo, memo_is_hex)), None, None, skip_validation, @@ -281,7 +282,7 @@ def compose( return mpma.compose( db, source, - util.flat( + helpers.flat( zip(asset, destination, quantity, memo["list"], memo_is_hex["list"]) ), memo["msg_wide"], @@ -293,7 +294,7 @@ def compose( return mpma.compose( db, source, - util.flat(zip(asset, destination, quantity)), + helpers.flat(zip(asset, destination, quantity)), memo, memo_is_hex, skip_validation, diff --git a/counterparty-core/counterpartycore/lib/messages/versions/enhanced_send.py b/counterparty-core/counterpartycore/lib/messages/versions/enhanced_send.py index c14223204..0159c390f 100644 --- a/counterparty-core/counterpartycore/lib/messages/versions/enhanced_send.py +++ b/counterparty-core/counterpartycore/lib/messages/versions/enhanced_send.py @@ -7,6 +7,7 @@ from counterpartycore.lib.messages.utils import address from counterpartycore.lib.messages.versions import send1 from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -89,7 +90,7 @@ def validate(db, source, destination, asset, quantity, memo_bytes, block_index): results = ledger.get_addresses(db, address=destination) if results: result = results[0] - if result and util.active_options( + if result and helpers.active_options( result["options"], config.ADDRESS_OPTION_REQUIRE_MEMO ): if memo_bytes is None or (len(memo_bytes) == 0): diff --git a/counterparty-core/counterpartycore/lib/messages/versions/mpma.py b/counterparty-core/counterpartycore/lib/messages/versions/mpma.py index 2c02cb154..3e3cf629e 100644 --- a/counterparty-core/counterpartycore/lib/messages/versions/mpma.py +++ b/counterparty-core/counterpartycore/lib/messages/versions/mpma.py @@ -11,6 +11,7 @@ _encode_mpma_send, ) from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -128,7 +129,7 @@ def compose( if memo_is_hex and not isinstance(memo_is_hex, bool): raise exceptions.ComposeError("`memo_is_hex` must be a boolean") - out_balances = util.accumulate([(t[0], t[2]) for t in asset_dest_quant_list]) + out_balances = helpers.accumulate([(t[0], t[2]) for t in asset_dest_quant_list]) for asset, quantity in out_balances: if protocol.enabled("mpma_subasset_support"): # resolve subassets diff --git a/counterparty-core/counterpartycore/lib/messages/versions/send1.py b/counterparty-core/counterpartycore/lib/messages/versions/send1.py index 9fe6217ee..6f0780918 100644 --- a/counterparty-core/counterpartycore/lib/messages/versions/send1.py +++ b/counterparty-core/counterpartycore/lib/messages/versions/send1.py @@ -6,6 +6,7 @@ from counterpartycore.lib import config, exceptions, ledger, util from counterpartycore.lib.messages import dispense from counterpartycore.lib.parser import message_type, protocol +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -58,7 +59,7 @@ def validate(db, source, destination, asset, quantity, block_index): results = ledger.get_addresses(db, address=destination) if results: result = results[0] - if result and util.active_options( + if result and helpers.active_options( result["options"], config.ADDRESS_OPTION_REQUIRE_MEMO ): problems.append("destination requires memo") diff --git a/counterparty-core/counterpartycore/lib/parser/blocks.py b/counterparty-core/counterpartycore/lib/parser/blocks.py index 73c268270..36a25b507 100644 --- a/counterparty-core/counterpartycore/lib/parser/blocks.py +++ b/counterparty-core/counterpartycore/lib/parser/blocks.py @@ -12,7 +12,7 @@ import time from datetime import timedelta -from counterpartycore.lib import ( # noqa: E402 +from counterpartycore.lib import ( backend, config, database, @@ -22,7 +22,7 @@ ) from counterpartycore.lib.backend import rsfetcher from counterpartycore.lib.cli import log -from counterpartycore.lib.messages import ( # noqa: E402 +from counterpartycore.lib.messages import ( attach, bet, broadcast, @@ -46,9 +46,10 @@ sweep, utxo, ) -from counterpartycore.lib.messages.versions import enhanced_send, mpma # noqa: E402 +from counterpartycore.lib.messages.versions import enhanced_send, mpma from counterpartycore.lib.parser import check, deserialize, message_type, protocol -from counterpartycore.lib.parser.gettxinfo import get_tx_info # noqa: E402 +from counterpartycore.lib.parser.gettxinfo import get_tx_info +from counterpartycore.lib.utils import helpers D = decimal.Decimal logger = logging.getLogger(config.LOGGER_NAME) @@ -1494,7 +1495,7 @@ def catch_up(db, check_asset_conservation=True): assert parsed_block_index == block_height parsed_blocks += 1 - formatted_duration = util.format_duration(time.time() - start_time) + formatted_duration = helpers.format_duration(time.time() - start_time) logger.debug( f"Block {util.CURRENT_BLOCK_INDEX}/{block_count} parsed, for {parsed_blocks} blocks in {formatted_duration}." ) diff --git a/counterparty-core/counterpartycore/lib/parser/follow.py b/counterparty-core/counterpartycore/lib/parser/follow.py index 85a53b3fd..59bb27b7e 100644 --- a/counterparty-core/counterpartycore/lib/parser/follow.py +++ b/counterparty-core/counterpartycore/lib/parser/follow.py @@ -19,6 +19,7 @@ from counterpartycore.lib.parser import blocks, check, deserialize, mempool from counterpartycore.lib.tools import sentry from counterpartycore.lib.tools.telemetry.oneshot import TelemetryOneShot +from counterpartycore.lib.utils import helpers logger = logging.getLogger(config.LOGGER_NAME) @@ -335,7 +336,7 @@ def get_raw_mempool(db): txhash_list.append(txid) timestamps[txid] = tx_info["time"] - chunks = util.chunkify(txhash_list, config.MAX_RPC_BATCH_SIZE) + chunks = helpers.chunkify(txhash_list, config.MAX_RPC_BATCH_SIZE) logger.debug(f"Found {len(txhash_list)} transaction(s) in the raw mempool...") return chunks, timestamps @@ -372,7 +373,7 @@ def stop(self): self.join() -class NotSupportedTransactionsCache(metaclass=util.SingletonMeta): +class NotSupportedTransactionsCache(metaclass=helpers.SingletonMeta): def __init__(self): self.not_suppported_txs = [] self.cache_path = os.path.join( diff --git a/counterparty-core/counterpartycore/lib/tools/telemetry/oneshot.py b/counterparty-core/counterpartycore/lib/tools/telemetry/oneshot.py index da637e42b..eaa978cab 100644 --- a/counterparty-core/counterpartycore/lib/tools/telemetry/oneshot.py +++ b/counterparty-core/counterpartycore/lib/tools/telemetry/oneshot.py @@ -6,7 +6,7 @@ from counterpartycore.lib.tools.telemetry.collectors.influxdb import ( TelemetryCollectorInfluxDB, ) -from counterpartycore.lib.util import SingletonMeta +from counterpartycore.lib.utils.helpers import SingletonMeta from sentry_sdk import capture_exception logger = logging.getLogger(config.LOGGER_NAME) diff --git a/counterparty-core/counterpartycore/lib/util.py b/counterparty-core/counterpartycore/lib/util.py index 6db49e917..0c8362c81 100644 --- a/counterparty-core/counterpartycore/lib/util.py +++ b/counterparty-core/counterpartycore/lib/util.py @@ -1,76 +1,6 @@ -import itertools -import logging -from operator import itemgetter -from urllib.parse import urlparse - -from counterpartycore.lib import config - -logger = logging.getLogger(config.LOGGER_NAME) - CURRENT_BLOCK_INDEX = None CURRENT_TX_HASH = None PARSING_MEMPOOL = False BLOCK_PARSER_STATUS = "starting" CURRENT_BACKEND_HEIGHT = None CURRENT_BLOCK_TIME = None - - -def chunkify(l, n): # noqa: E741 - n = max(1, n) - return [l[i : i + n] for i in range(0, len(l), n)] - - -def flat(z): - return [x for x in z] - - -def accumulate(l): # noqa: E741 - it = itertools.groupby(l, itemgetter(0)) - for key, subiter in it: - yield key, sum(item[1] for item in subiter) - - -def active_options(config, options): - """Checks if options active in some given config.""" - return config & options == options - - -ID_SEPARATOR = "_" - - -def make_id(hash_1, hash_2): - return hash_1 + ID_SEPARATOR + hash_2 - - -# ORACLES -def satoshirate_to_fiat(satoshirate): - return round(satoshirate / 100.0, 2) - - -class SingletonMeta(type): - _instances = {} - - def __call__(cls, *args, **kwargs): - """ - Possible changes to the value of the `__init__` argument do not affect - the returned instance. - """ - if cls not in cls._instances: - instance = super().__call__(*args, **kwargs) - cls._instances[cls] = instance - return cls._instances[cls] - - -def format_duration(seconds): - duration_seconds = int(seconds) - hours, remainder = divmod(duration_seconds, 3600) - minutes, seconds = divmod(remainder, 60) - return f"{hours}h {minutes}m {seconds}s" - - -def is_url(url): - try: - result = urlparse(url) - return all(result.scheme, result.netloc) - except ValueError: - return False diff --git a/counterparty-core/counterpartycore/lib/utils/helpers.py b/counterparty-core/counterpartycore/lib/utils/helpers.py new file mode 100644 index 000000000..fb53dca95 --- /dev/null +++ b/counterparty-core/counterpartycore/lib/utils/helpers.py @@ -0,0 +1,64 @@ +import itertools +from operator import itemgetter +from urllib.parse import urlparse + + +def chunkify(l, n): # noqa: E741 + n = max(1, n) + return [l[i : i + n] for i in range(0, len(l), n)] + + +def flat(z): + return [x for x in z] + + +def accumulate(l): # noqa: E741 + it = itertools.groupby(l, itemgetter(0)) + for key, subiter in it: + yield key, sum(item[1] for item in subiter) + + +def active_options(config, options): + """Checks if options active in some given config.""" + return config & options == options + + +ID_SEPARATOR = "_" + + +def make_id(hash_1, hash_2): + return hash_1 + ID_SEPARATOR + hash_2 + + +# ORACLES +def satoshirate_to_fiat(satoshirate): + return round(satoshirate / 100.0, 2) + + +class SingletonMeta(type): + _instances = {} + + def __call__(cls, *args, **kwargs): + """ + Possible changes to the value of the `__init__` argument do not affect + the returned instance. + """ + if cls not in cls._instances: + instance = super().__call__(*args, **kwargs) + cls._instances[cls] = instance + return cls._instances[cls] + + +def format_duration(seconds): + duration_seconds = int(seconds) + hours, remainder = divmod(duration_seconds, 3600) + minutes, seconds = divmod(remainder, 60) + return f"{hours}h {minutes}m {seconds}s" + + +def is_url(url): + try: + result = urlparse(url) + return all(result.scheme, result.netloc) + except ValueError: + return False diff --git a/counterparty-core/counterpartycore/test/conftest.py b/counterparty-core/counterpartycore/test/conftest.py index d4edc4076..a25d81560 100644 --- a/counterparty-core/counterpartycore/test/conftest.py +++ b/counterparty-core/counterpartycore/test/conftest.py @@ -725,4 +725,4 @@ def convert_to_psbt(tx_hex): class MockSingletonMeta: pass - monkeypatch.setattr("counterpartycore.lib.util.SingletonMeta", MockSingletonMeta) + monkeypatch.setattr("counterpartycore.lib.utils.helpers.SingletonMeta", MockSingletonMeta) diff --git a/counterparty-core/counterpartycore/test/util_test.py b/counterparty-core/counterpartycore/test/util_test.py index 0efd044bc..55064f40a 100644 --- a/counterparty-core/counterpartycore/test/util_test.py +++ b/counterparty-core/counterpartycore/test/util_test.py @@ -40,6 +40,7 @@ from counterpartycore.lib.cli import server from counterpartycore.lib.messages import dispenser, fairminter, utxo # noqa from counterpartycore.lib.parser import blocks, check, deserialize, gettxinfo +from counterpartycore.lib.utils import helpers from counterpartycore.test.fixtures.params import DEFAULT_PARAMS as DP from counterpartycore.test.fixtures.scenarios import ( INTEGRATION_SCENARIOS, @@ -551,7 +552,7 @@ def extract_addresses_from_txlist(tx_hashes_tx, _getrawtransaction_batch): tx_inputs_hashes.update([vin["txid"] for vin in tx["vin"]]) # chunk txs to avoid huge memory spikes - for tx_inputs_hashes_chunk in util.chunkify( + for tx_inputs_hashes_chunk in helpers.chunkify( list(tx_inputs_hashes), config.BACKEND_RAW_TRANSACTIONS_CACHE_SIZE ): raw_transactions = _getrawtransaction_batch(tx_inputs_hashes_chunk, verbose=True)