Skip to content

Commit

Permalink
Remove globals BLOCK_LEDGER and BLOCK_JOURNAL
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 16, 2025
1 parent 1652446 commit 85eb071
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
30 changes: 24 additions & 6 deletions counterparty-core/counterpartycore/lib/ledger/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

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

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

Expand Down
13 changes: 9 additions & 4 deletions counterparty-core/counterpartycore/lib/parser/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = """
Expand Down

0 comments on commit 85eb071

Please sign in to comment.