From 85eb07127cb69edb08256f232acbc6c4a2fc433d Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Thu, 16 Jan 2025 12:05:26 +0000 Subject: [PATCH] Remove globals BLOCK_LEDGER and BLOCK_JOURNAL --- .../counterpartycore/lib/ledger/ledger.py | 30 +++++++++++++++---- .../counterpartycore/lib/parser/blocks.py | 13 +++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/counterparty-core/counterpartycore/lib/ledger/ledger.py b/counterparty-core/counterpartycore/lib/ledger/ledger.py index 9db6cd188..11c7389ee 100644 --- a/counterparty-core/counterpartycore/lib/ledger/ledger.py +++ b/counterparty-core/counterpartycore/lib/ledger/ledger.py @@ -14,15 +14,33 @@ logger = logging.getLogger(config.LOGGER_NAME) -BLOCK_LEDGER = [] -BLOCK_JOURNAL = [] - ############################### # UTIL FUNCTIONS # ############################### +class ConsensusHashBuilder(metaclass=helpers.SingletonMeta): + def __init__(self): + self.reset() + + def append_to_block_ledger(self, item): + self.ledger.append(item) + + def append_to_block_journal(self, item): + self.journal.append(item) + + def block_ledger(self): + return self.ledger + + def block_journal(self): + return self.journal + + def reset(self): + self.ledger = [] + self.journal = [] + + @contextmanager def get_cursor(db): cursor = db.cursor() @@ -213,7 +231,7 @@ def add_to_journal(db, block_index, command, category, event, bindings): cursor.execute(query, message_bindings) cursor.close() - BLOCK_JOURNAL.append(f"{command}{category}{bindings_string}") + ConsensusHashBuilder().append_to_block_journal(f"{command}{category}{bindings_string}") log.log_event(db, block_index, message_index, event, items) @@ -357,7 +375,7 @@ def debit(db, address, asset, quantity, tx_index, action=None, event=None): } insert_record(db, "debits", bindings, "DEBIT") - BLOCK_LEDGER.append(f"{block_index}{address}{asset}{quantity}") + ConsensusHashBuilder().append_to_block_ledger(f"{block_index}{address}{asset}{quantity}") return utxo_address @@ -439,7 +457,7 @@ def credit(db, address, asset, quantity, tx_index, action=None, event=None): } insert_record(db, "credits", bindings, "CREDIT") - BLOCK_LEDGER.append(f"{block_index}{address}{asset}{quantity}") + ConsensusHashBuilder().append_to_block_ledger(f"{block_index}{address}{asset}{quantity}") return utxo_address diff --git a/counterparty-core/counterpartycore/lib/parser/blocks.py b/counterparty-core/counterpartycore/lib/parser/blocks.py index 2187e72aa..e13914cf8 100644 --- a/counterparty-core/counterpartycore/lib/parser/blocks.py +++ b/counterparty-core/counterpartycore/lib/parser/blocks.py @@ -329,8 +329,7 @@ def parse_block( if reparsing: replay_transactions_events(db, transactions) - ledger.ledger.BLOCK_LEDGER = [] - ledger.ledger.BLOCK_JOURNAL = [] + ledger.ledger.ConsensusHashBuilder().reset() if block_index != config.MEMPOOL_BLOCK_INDEX: assert block_index == CurrentState().current_block_index() @@ -368,10 +367,16 @@ def parse_block( db, "txlist_hash", previous_txlist_hash, txlist ) new_ledger_hash, found_ledger_hash = check.consensus_hash( - db, "ledger_hash", previous_ledger_hash, ledger.ledger.BLOCK_LEDGER + db, + "ledger_hash", + previous_ledger_hash, + ledger.ledger.ConsensusHashBuilder().block_ledger(), ) new_messages_hash, found_messages_hash = check.consensus_hash( - db, "messages_hash", previous_messages_hash, ledger.ledger.BLOCK_JOURNAL + db, + "messages_hash", + previous_messages_hash, + ledger.ledger.ConsensusHashBuilder().block_journal(), ) update_block_query = """