Skip to content

Commit

Permalink
lint progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Feb 26, 2025
1 parent 79a6a96 commit 606141a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 75 deletions.
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/apiv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ def get_dispenser_info(tx_hash=None, tx_index=None):
oracle_price_last_updated = ""
oracle_fiat_label = ""

if dispenser_info["oracle_address"] != None: # noqa: E711
if dispenser_info["oracle_address"] is not None: # noqa: E711
fiat_price = helpers.satoshirate_to_fiat(dispenser_info["satoshirate"])

Check warning on line 1000 in counterparty-core/counterpartycore/lib/api/apiv1.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/api/apiv1.py#L999-L1000

Added lines #L999 - L1000 were not covered by tests
(
oracle_price,
Expand Down
10 changes: 5 additions & 5 deletions counterparty-core/counterpartycore/lib/ledger/issuances.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def resolve_subasset_longname(db, asset_name):
_subasset_parent, subasset_longname = assetnames.parse_subasset_from_asset_name(
asset_name, protocol.enabled("allow_subassets_on_numerics")
)
except Exception as e: # pylint: disable=broad-except # noqa: F841
logger.warning(f"Invalid subasset {asset_name}")
except Exception: # pylint: disable=broad-except # noqa: F841
logger.warning("Invalid subasset %s", asset_name)
subasset_longname = None

if subasset_longname is not None:
Expand Down Expand Up @@ -230,13 +230,13 @@ def value_output(quantity, asset, divisible):


def value_out(db, quantity, asset, divisible=None):
if asset not in ["leverage", "value", "fraction", "price", "odds"] and divisible == None: # noqa: E711
if asset not in ["leverage", "value", "fraction", "price", "odds"] and divisible is None:
divisible = is_divisible(db, asset)
return value_output(quantity, asset, divisible)


def value_in(db, quantity, asset, divisible=None):
if asset not in ["leverage", "value", "fraction", "price", "odds"] and divisible == None: # noqa: E711
if asset not in ["leverage", "value", "fraction", "price", "odds"] and divisible is None:
divisible = is_divisible(db, asset)
return value_input(quantity, asset, divisible)

Expand Down Expand Up @@ -447,7 +447,7 @@ def get_asset(db, asset):
break

asset = issuances[0]
asset["supply"] = asset_supply(db, issuance["asset"])
asset["supply"] = asset_supply(db, issuances[0]["asset"])
asset["locked"] = locked
return asset

Expand Down
10 changes: 4 additions & 6 deletions counterparty-core/counterpartycore/lib/messages/bet.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ def get_fee_fraction(db, feed_address):
fee_fraction_int = last_broadcast["fee_fraction_int"]
if fee_fraction_int:
return fee_fraction_int / 1e8
else:
return 0
else:
return 0
return 0


def validate(
Expand Down Expand Up @@ -424,7 +422,7 @@ def match(db, tx):
if tx1_status != "open":
break

logger.debug("Considering: " + tx0["tx_hash"])
logger.debug("Considering: %s", tx0["tx_hash"])
tx0_wager_remaining = tx0["wager_remaining"]
tx0_counterwager_remaining = tx0["counterwager_remaining"]

Expand Down Expand Up @@ -585,7 +583,7 @@ def match(db, tx):
}
ledger.events.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",
"Bet Match: tx0_index=%(tx0_index)s, forward=%(forward_quantity)s XCP, backward=%(backward_quantity)s XCP, feed=%(feed_address)s",
bindings,
)

Expand All @@ -608,7 +606,7 @@ def expire(db, block_index, block_time):
"block_index": block_index,
}
ledger.events.insert_record(db, "bet_expirations", bindings, "BET_EXPIRATION")
logger.info("Bet Expiration %(bet_hash)s", bindings)
logger.info("Bet expired: %(bet_hash)s", bindings)

# Expire bet matches whose deadline is more than two weeks before the current block time.
for bet_match in ledger.other.get_bet_matches_to_expire(db, block_time):
Expand Down
12 changes: 9 additions & 3 deletions counterparty-core/counterpartycore/lib/messages/issuance.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ def unpack(db, message, message_type_id, block_index, return_dict=False):
# Unpack message.
try:
subasset_longname = None
asset_id = None
quantity = None
divisible = None
callable_ = None
call_date = None
if message_type_id == LR_SUBASSET_ID or message_type_id == SUBASSET_ID:
if not protocol.enabled("subassets", block_index=block_index):
logger.warning(f"subassets are not enabled at block {block_index}")
Expand All @@ -532,7 +537,7 @@ def unpack(db, message, message_type_id, block_index, return_dict=False):

description_length = len(message) - subasset_format_length - compacted_subasset_length
if description_length < 0:
logger.warning(f"invalid subasset length: {compacted_subasset_length}")
logger.warning("invalid subasset length: %s", compacted_subasset_length)
raise exceptions.UnpackError
messages_format = f">{compacted_subasset_length}s{description_length}s"
compacted_subasset_longname, description = struct.unpack(
Expand Down Expand Up @@ -641,7 +646,7 @@ def unpack(db, message, message_type_id, block_index, return_dict=False):
except exceptions.AssetIDError:
asset = None
status = "invalid: bad asset name"
except exceptions.UnpackError as e: # noqa: F841
except exceptions.UnpackError:
(
asset_id,
asset,
Expand Down Expand Up @@ -727,14 +732,15 @@ def parse(db, tx, message, message_type_id):
) = unpack(db, message, message_type_id, tx["block_index"])
# parse and validate the subasset from the message
subasset_parent = None
reissued_asset_longname = None
if status == "valid" and subasset_longname is not None: # Protocol change.
try:
# ensure the subasset_longname is valid
assetnames.validate_subasset_longname(subasset_longname)
subasset_parent, subasset_longname = assetnames.parse_subasset_from_asset_name(
subasset_longname, protocol.enabled("allow_subassets_on_numerics")
)
except exceptions.AssetNameError as e: # noqa: F841
except exceptions.AssetNameError:

Check warning on line 743 in counterparty-core/counterpartycore/lib/messages/issuance.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/issuance.py#L743

Added line #L743 was not covered by tests
asset = None
status = "invalid: bad subasset name"

Expand Down
22 changes: 11 additions & 11 deletions counterparty-core/counterpartycore/lib/messages/versions/mpma.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def py34_tuple_append(first_elem, t):
def unpack(message):
try:
unpacked = _decode_mpma_send_decode(message)
except struct.error as e: # noqa: F841
raise exceptions.UnpackError("could not unpack") # noqa: B904
except (exceptions.AssetNameError, exceptions.AssetIDError) as e: # noqa: F841
raise exceptions.UnpackError("invalid asset in mpma send") # noqa: B904
except ReadError as e: # noqa: F841
raise exceptions.UnpackError("truncated data") # noqa: B904
except struct.error as e:
raise exceptions.UnpackError("could not unpack") from e
except (exceptions.AssetNameError, exceptions.AssetIDError) as e:
raise exceptions.UnpackError("invalid asset in mpma send") from e

Check warning on line 38 in counterparty-core/counterpartycore/lib/messages/versions/mpma.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/versions/mpma.py#L38

Added line #L38 was not covered by tests
except ReadError as e:
raise exceptions.UnpackError("truncated data") from e

return unpacked

Expand Down Expand Up @@ -162,9 +162,9 @@ def parse(db, tx, message):
try:
unpacked = unpack(message)
status = "valid"
except struct.error as e: # noqa: F841
except struct.error:

Check warning on line 165 in counterparty-core/counterpartycore/lib/messages/versions/mpma.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/versions/mpma.py#L165

Added line #L165 was not covered by tests
status = "invalid: truncated message"
except (exceptions.AssetNameError, exceptions.AssetIDError) as e: # noqa: F841
except (exceptions.AssetNameError, exceptions.AssetIDError):

Check warning on line 167 in counterparty-core/counterpartycore/lib/messages/versions/mpma.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/versions/mpma.py#L167

Added line #L167 was not covered by tests
status = "invalid: invalid asset name/id"
except Exception as e: # pylint: disable=broad-except

Check warning on line 169 in counterparty-core/counterpartycore/lib/messages/versions/mpma.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/versions/mpma.py#L169

Added line #L169 was not covered by tests
status = f"invalid: couldn't unpack; {e}"
Expand All @@ -175,10 +175,10 @@ def parse(db, tx, message):
all_debits = []
all_credits = []
if status == "valid":
for asset_id in unpacked:
for asset_id in unpacked.keys():

Check warning

Code scanning / pylint

Consider iterating with .items(). Warning

Consider iterating with .items().

Check warning

Code scanning / pylint

Consider iterating the dictionary directly instead of calling .keys(). Warning

Consider iterating the dictionary directly instead of calling .keys().
try:
asset = ledger.issuances.get_asset_name(db, asset_id) # noqa: F841
except exceptions.AssetNameError as e: # noqa: F841
ledger.issuances.get_asset_name(db, asset_id)
except exceptions.AssetNameError:

Check warning on line 181 in counterparty-core/counterpartycore/lib/messages/versions/mpma.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/messages/versions/mpma.py#L181

Added line #L181 was not covered by tests
status = f"invalid: asset {asset_id} invalid at block index {tx['block_index']}"
break

Expand Down
51 changes: 29 additions & 22 deletions counterparty-core/counterpartycore/lib/parser/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def parse_tx(db, tx):
elif message_type_id == cancel.ID:
cancel.parse(db, tx, message)
elif message_type_id == rps.ID and rps_enabled:
rps.parse(db, tx, message)
rps.parse(db, tx)

Check warning on line 199 in counterparty-core/counterpartycore/lib/parser/blocks.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/parser/blocks.py#L199

Added line #L199 was not covered by tests
elif message_type_id == rpsresolve.ID and rps_enabled:
rpsresolve.parse(db, tx, message)
rpsresolve.parse(db, tx)

Check warning on line 201 in counterparty-core/counterpartycore/lib/parser/blocks.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/parser/blocks.py#L201

Added line #L201 was not covered by tests
elif message_type_id == destroy.ID and protocol.enabled(
"destroy_reactivated", block_index=tx["block_index"]
):
Expand Down Expand Up @@ -353,7 +353,7 @@ def parse_block(
f"{tx['tx_hash']}{tx['source']}{tx['destination']}{tx['btc_amount']}{tx['fee']}{data}"
)
except exceptions.ParseTransactionError as e:
logger.warning(f"ParseTransactionError for tx {tx['tx_hash']}: {e}")
logger.warning("ParseTransactionError for tx %s: %s", tx["tx_hash"], e)

Check warning on line 356 in counterparty-core/counterpartycore/lib/parser/blocks.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/parser/blocks.py#L356

Added line #L356 was not covered by tests
raise e
# pass

Expand All @@ -362,16 +362,16 @@ def parse_block(

if block_index != config.MEMPOOL_BLOCK_INDEX:
# Calculate consensus hashes.
new_txlist_hash, found_txlist_hash = check.consensus_hash(
new_txlist_hash, _found_txlist_hash = check.consensus_hash(
db, "txlist_hash", previous_txlist_hash, txlist
)
new_ledger_hash, found_ledger_hash = check.consensus_hash(
new_ledger_hash, _found_ledger_hash = check.consensus_hash(
db,
"ledger_hash",
previous_ledger_hash,
ledger.currentstate.ConsensusHashBuilder().block_ledger(),
)
new_messages_hash, found_messages_hash = check.consensus_hash(
new_messages_hash, _found_messages_hash = check.consensus_hash(
db,
"messages_hash",
previous_messages_hash,
Expand Down Expand Up @@ -499,7 +499,7 @@ def list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index, decoded_


def clean_table_from(cursor, table, block_index):
logger.debug(f"Rolling back table `{table}`...")
logger.debug("Rolling back table `%s`...", table)
# internal function, no sql injection here
cursor.execute(f"""DELETE FROM {table} WHERE block_index >= ?""", (block_index,)) # nosec B608 # noqa: S608 # nosec B608

Expand Down Expand Up @@ -535,7 +535,9 @@ def rebuild_database(db, include_transactions=True):
cursor.execute(f"DROP TABLE IF EXISTS {table}") # nosec B608
cursor.execute("""PRAGMA foreign_keys=ON""")
for file in ["0001.initial_migration.sql", "0002.create_mempool_transactions_table.sql"]:
with open(os.path.join(config.LEDGER_DB_MIGRATIONS_DIR, file), "r") as sql_file:
with open(
os.path.join(config.LEDGER_DB_MIGRATIONS_DIR, file), "r", encoding="utf-8"
) as sql_file:
db.execute(sql_file.read())


Expand Down Expand Up @@ -692,13 +694,13 @@ def handle_reorg(db):
current_block_hash = backend.bitcoind.getblockhash(previous_block_index + 1)
except exceptions.BlockOutOfRange:
# current block is not in the blockchain
logger.debug(f"Current block is not in the blockchain ({previous_block_index + 1}).")
logger.debug("Current block is not in the blockchain (%s).", previous_block_index + 1)
previous_block_index -= 1
continue

if previous_block_hash != ledger.blocks.get_block_hash(db, previous_block_index):
# hashes don't match
logger.debug(f"Hashes don't match ({previous_block_index}).")
logger.debug("Hashes don't match (%s).", previous_block_index)
previous_block_index -= 1
continue

Expand Down Expand Up @@ -758,7 +760,7 @@ def parse_new_block(db, decoded_block, tx_index=None):
assert previous_block["block_index"] == decoded_block["block_index"] - 1

with db: # ensure all the block or nothing
logger.info(f"Block {decoded_block['block_index']}", extra={"bold": True})
logger.info("Block %s", decoded_block["block_index"], extra={"bold": True})
# insert block
block_bindings = {
"block_index": decoded_block["block_index"],
Expand Down Expand Up @@ -818,7 +820,7 @@ def rollback_empty_block(db):
block = cursor.fetchone()
if block:
logger.warning(
f"Ledger hashes are empty from block {block['block_index']}. Rolling back..."
"Ledger hashes are empty from block %s. Rolling back...", block["block_index"]
)
rollback(db, block_index=block["block_index"], force=True)

Expand All @@ -845,16 +847,15 @@ def start_rsfetcher():
fetcher = rsfetcher.RSFetcher()
try:
fetcher.start(CurrentState().current_block_index() + 1)
except exceptions.InvalidVersion as e:
logger.error(e)
raise e
except Exception as e: # pylint: disable=broad-except
logger.warning(f"Failed to start RSFetcher ({e}). Retrying in 5 seconds...")
except exceptions.InvalidVersion as e1:
logger.error(e1)
raise e1
except Exception as e2: # pylint: disable=broad-except
logger.warning("Failed to start RSFetcher (%s). Retrying in 5 seconds...", e2)

Check warning on line 854 in counterparty-core/counterpartycore/lib/parser/blocks.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/parser/blocks.py#L850-L854

Added lines #L850 - L854 were not covered by tests
try:
fetcher.stop()
except Exception as e: # pylint: disable=broad-except
logger.debug(f"Failed to stop RSFetcher ({e}).")
pass
except Exception as e3: # pylint: disable=broad-except
logger.debug("Failed to stop RSFetcher (%s).", e3)

Check warning on line 858 in counterparty-core/counterpartycore/lib/parser/blocks.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/parser/blocks.py#L857-L858

Added lines #L857 - L858 were not covered by tests
time.sleep(5)
return start_rsfetcher()
return fetcher
Expand Down Expand Up @@ -931,7 +932,7 @@ def catch_up(db):
block_height = decoded_block.get("height")
fetch_time_end = time.time()
fetch_duration = fetch_time_end - fetch_time_start
logger.debug(f"Block {block_height} fetched. ({fetch_duration:.6f}s)")
logger.debug("Block %s fetched. (%.6fs)", block_height, fetch_duration)

Check warning on line 935 in counterparty-core/counterpartycore/lib/parser/blocks.py

View check run for this annotation

Codecov / codecov/patch

counterparty-core/counterpartycore/lib/parser/blocks.py#L935

Added line #L935 was not covered by tests

# Check for gaps in the blockchain
assert block_height <= CurrentState().current_block_index() + 1
Expand All @@ -949,7 +950,13 @@ def catch_up(db):
parsed_blocks += 1
formatted_duration = helpers.format_duration(time.time() - start_time)
logger.debug(
f"Block {CurrentState().current_block_index()}/{block_count} parsed, for {parsed_blocks} blocks in {formatted_duration}."
"Block %(current)s/%(total)s parsed, for %(parsed)s blocks in %(duration)s.",
{
"current": CurrentState().current_block_index(),
"total": block_count,
"parsed": parsed_blocks,
"duration": formatted_duration,
},
)

# Refresh block count.
Expand Down
17 changes: 7 additions & 10 deletions counterparty-core/counterpartycore/lib/parser/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,14 @@ def asset_conservation(db, stop_event=None):
asset_held = held[asset] if asset in held and held[asset] != None else 0 # noqa: E711
if asset_issued != asset_held:
raise exceptions.SanityError(
"{} {} issued ≠ {} {} held".format(
ledger.issuances.value_out(db, asset_issued, asset),
asset,
ledger.issuances.value_out(db, asset_held, asset),
asset,
)
f"{ledger.issuances.value_out(db, asset_issued, asset)} {asset} issued ≠ "
f"{ledger.issuances.value_out(db, asset_held, asset)} {asset} held"
)
logger.trace(
"{} has been conserved ({} {} both issued and held)".format(
asset, ledger.issuances.value_out(db, asset_issued, asset), asset
)
"%s has been conserved (%s %s both issued and held)",
asset,
ledger.issuances.value_out(db, asset_issued, asset),
asset,
)
logger.debug("All assets have been conserved.")

Expand Down Expand Up @@ -204,4 +201,4 @@ def check_database_version(db, upgrade_actions_callback, database_name):
# update the database version
database.update_version(db)
else:
logger.debug(f"{database_name} database is up to date.")
logger.debug("%s database is up to date.", database_name)
Loading

0 comments on commit 606141a

Please sign in to comment.