Skip to content

Commit

Permalink
clean util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 14, 2025
1 parent cf47698 commit eea083b
Show file tree
Hide file tree
Showing 27 changed files with 134 additions and 124 deletions.
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
is_docker,
is_force_enabled,
)
from counterpartycore.lib.utils import helpers

D = decimal.Decimal

Expand Down Expand Up @@ -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

Check warning

Code scanning / pylint

Module 'counterpartycore.lib.utils.helpers' has no 'CURRENT_BLOCK_INDEX' member. Warning

Module 'counterpartycore.lib.utils.helpers' has no 'CURRENT_BLOCK_INDEX' member.
)

if oracle_price > 0:
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/api_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions counterparty-core/counterpartycore/lib/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -154,7 +155,7 @@ def get_backend_height():
return block_count + blocks_behind


class BackendHeight(metaclass=util.SingletonMeta):
class BackendHeight(metaclass=helpers.SingletonMeta):

Check warning

Code scanning / pylint

Too few public methods (1/2). Warning

Too few public methods (1/2).
def __init__(self):
self.backend_height = get_backend_height()
self.last_update = time.time()
Expand Down Expand Up @@ -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

Check warning

Code scanning / pylint

Module 'counterpartycore.lib.utils.helpers' has no 'CURRENT_BLOCK_INDEX' member. Warning

Module 'counterpartycore.lib.utils.helpers' has no 'CURRENT_BLOCK_INDEX' member.
)

if dispenser["oracle_price"] > 0:
Expand Down
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/backend/rsfetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):

Check warning

Code scanning / pylint

Too many instance attributes (9/7). Warning

Too many instance attributes (9/7).
thread_index_counter = 0 # Add a thread index counter

def __init__(self, indexer_config=None):
Expand Down
1 change: 0 additions & 1 deletion counterparty-core/counterpartycore/lib/cli/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 4 additions & 5 deletions counterparty-core/counterpartycore/lib/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")

Expand Down
8 changes: 4 additions & 4 deletions counterparty-core/counterpartycore/lib/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/messages/bet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
util,
)
from counterpartycore.lib.parser import message_type, protocol
from counterpartycore.lib.utils import helpers

D = decimal.Decimal

Expand Down Expand Up @@ -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

Check warning

Code scanning / pylint

Unused variable 'bet_match_id'. Warning

Unused variable 'bet_match_id'.

# Debit the order.
# Counterwager remainings may be negative.
Expand Down Expand Up @@ -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"],
Expand Down
7 changes: 4 additions & 3 deletions counterparty-core/counterpartycore/lib/messages/broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
util,
)
from counterpartycore.lib.parser import message_type, protocol
from counterpartycore.lib.utils import helpers

from . import bet

Expand Down Expand Up @@ -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")


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/messages/btcpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/messages/dispense.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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"]))
Expand Down
7 changes: 4 additions & 3 deletions counterparty-core/counterpartycore/lib/messages/dispenser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
Loading

0 comments on commit eea083b

Please sign in to comment.