Skip to content

Commit

Permalink
fix import, start to split ledger.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 16, 2025
1 parent 95221bc commit 0207d6a
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 167 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 @@ -835,7 +835,7 @@ def get_running_info():
else:
caught_up = True

last_block = ledger.ledger.get_last_block(db)
last_block = ledger.blocks.get_last_block(db)

try:
last_message = ledger.ledger.last_message(db)
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/healthz.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def check_last_parsed_block(db, blockcount):
"""Checks database to see if is caught up with backend."""
last_block = ledger.ledger.get_last_block(db)
last_block = ledger.blocks.get_last_block(db)
if last_block is None:
raise exceptions.DatabaseError(
f"{config.XCP_NAME} database is behind backend."
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def inject_issuances_and_block_times(ledger_db, state_db, result_list):
issuance_by_asset = ledger.ledger.get_assets_last_issuance(state_db, asset_list)

# get block_time for each block_index
block_times = ledger.ledger.get_blocks_time(ledger_db, block_indexes)
block_times = ledger.blocks.get_blocks_time(ledger_db, block_indexes)

# inject issuance and block_time
for result_item in result_list:
Expand Down
14 changes: 7 additions & 7 deletions counterparty-core/counterpartycore/lib/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialise(*args, **kwargs):
)
initialise_config(*args, **kwargs)
db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
CurrentState().set_current_block_index(ledger.blocks.last_db_index(db))
return db


Expand Down Expand Up @@ -673,7 +673,7 @@ def start_all(args):
# Initialise database
database.apply_outstanding_migration(config.DATABASE, config.LEDGER_DB_MIGRATIONS_DIR)
db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
CurrentState().set_current_block_index(ledger.blocks.last_db_index(db))
blocks.check_database_version(db)
database.optimize(db)

Expand Down Expand Up @@ -771,9 +771,9 @@ def start_all(args):

def reparse(block_index):
ledger_db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(ledger_db))
CurrentState().set_current_block_index(ledger.blocks.last_db_index(ledger_db))

last_block = ledger.ledger.get_last_block(ledger_db)
last_block = ledger.blocks.get_last_block(ledger_db)
if last_block is None or block_index > last_block["block_index"]:
print(colored("Block index is higher than current block index. No need to reparse.", "red"))
ledger_db.close()
Expand All @@ -792,9 +792,9 @@ def reparse(block_index):

def rollback(block_index=None):
ledger_db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(ledger_db))
CurrentState().set_current_block_index(ledger.blocks.last_db_index(ledger_db))

last_block = ledger.ledger.get_last_block(ledger_db)
last_block = ledger.blocks.get_last_block(ledger_db)
if last_block is None or block_index > last_block["block_index"]:
print(
colored("Block index is higher than current block index. No need to rollback.", "red")
Expand Down Expand Up @@ -822,7 +822,7 @@ def vacuum():

def check_database():
db = database.initialise_db()
CurrentState().set_current_block_index(ledger.ledger.last_db_index(db))
CurrentState().set_current_block_index(ledger.blocks.last_db_index(db))

start_all_time = time.time()

Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/ledger/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import blocks # noqa F401
from . import blocks, ledger # noqa F401
110 changes: 110 additions & 0 deletions counterparty-core/counterpartycore/lib/ledger/blocks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from counterpartycore.lib import config


def get_block(db, block_index: int):
"""
Return the information of a block
Expand All @@ -11,3 +14,110 @@ def get_block(db, block_index: int):
cursor = db.cursor()
cursor.execute(query, bindings)
return cursor.fetchone()


def last_db_index(db):
cursor = db.cursor()
query = "SELECT name FROM sqlite_master WHERE type='table' AND name='blocks'"
if len(list(cursor.execute(query))) == 0:
return 0

query = "SELECT block_index FROM blocks WHERE ledger_hash IS NOT NULL ORDER BY block_index DESC LIMIT 1"
blocks = list(cursor.execute(query))
if len(blocks) == 0:
return 0

return blocks[0]["block_index"]


def get_block_by_hash(db, block_hash: str):
"""
Return the information of a block
:param int block_hash: The hash of the block to return (e.g. 00000000000000000001158f52eae43aa7fede1bb675736f105ccb545edcf5dd)
"""
query = """
SELECT * FROM blocks
WHERE block_hash = ?
"""
bindings = (block_hash,)
cursor = db.cursor()
cursor.execute(query, bindings)
return cursor.fetchone()


def get_last_block(db):
cursor = db.cursor()
query = "SELECT * FROM blocks WHERE block_index != ? ORDER BY block_index DESC LIMIT 1"
cursor.execute(query, (config.MEMPOOL_BLOCK_INDEX,))
block = cursor.fetchone()
return block


def get_block_hash(db, block_index):
query = """
SELECT block_hash FROM blocks
WHERE block_index = ?
"""
bindings = (block_index,)
cursor = db.cursor()
cursor.execute(query, bindings)
block = cursor.fetchone()
if block is None:
return None
return block["block_hash"]


def get_blocks_time(db, block_indexes):
cursor = db.cursor()
query = f"""
SELECT block_index, block_time
FROM blocks
WHERE block_index IN ({",".join(["?" for e in range(0, len(block_indexes))])})
""" # nosec B608 # noqa: S608
cursor.execute(query, block_indexes)
blocks = cursor.fetchall()
result = {}
for block in blocks:
result[block["block_index"]] = block["block_time"]
return result


def get_vouts(db, tx_hash):
cursor = db.cursor()
query = """
SELECT txs.source AS source, txs_outs.*
FROM transaction_outputs txs_outs
LEFT JOIN transactions txs ON txs.tx_hash = txs_outs.tx_hash
WHERE txs_outs.tx_hash=:tx_hash
ORDER BY txs_outs.out_index
"""
bindings = {"tx_hash": tx_hash}
cursor.execute(query, bindings)
return cursor.fetchall()


def get_transactions(db, tx_hash=None, tx_index=None):
cursor = db.cursor()
where = []
bindings = []
if tx_hash is not None:
where.append("tx_hash = ?")
bindings.append(tx_hash)
if tx_index is not None:
where.append("tx_index = ?")
bindings.append(tx_index)
# no sql injection here
query = f"""SELECT * FROM transactions WHERE ({" AND ".join(where)})""" # nosec B608 # noqa: S608
cursor.execute(query, tuple(bindings))
return cursor.fetchall()


def get_transaction(db, tx_hash: str):
"""
Returns the information of a transaction
:param str tx_hash: The hash of the transaction to return (e.g. 876a6cfbd4aa22ba4fa85c2e1953a1c66649468a43a961ad16ea4d5329e3e4c5)
"""
transactions = get_transactions(db, tx_hash)
if transactions:
return transactions[0]
return None
142 changes: 0 additions & 142 deletions counterparty-core/counterpartycore/lib/ledger/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,148 +1273,6 @@ def get_burns(db, address: str = None, status: str = "valid"):
######################################


def get_blocks(db, last: int = None, limit: int = 10):
"""
Returns the list of the last ten blocks
:param int last: The index of the most recent block to return (e.g. 840000)
:param int limit: The number of blocks to return (e.g. 2)
"""
cursor = db.cursor()
bindings = []
query = """
SELECT * FROM blocks
"""
if last is not None:
query += "WHERE block_index <= ?"
bindings.append(last)
query += " ORDER BY block_index DESC LIMIT ?"
bindings.append(limit)
cursor.execute(query, tuple(bindings))
return cursor.fetchall()


def last_db_index(db):
cursor = db.cursor()
query = "SELECT name FROM sqlite_master WHERE type='table' AND name='blocks'"
if len(list(cursor.execute(query))) == 0:
return 0

query = "SELECT block_index FROM blocks WHERE ledger_hash IS NOT NULL ORDER BY block_index DESC LIMIT 1"
blocks = list(cursor.execute(query))
if len(blocks) == 0:
return 0

return blocks[0]["block_index"]


def get_block_by_hash(db, block_hash: str):
"""
Return the information of a block
:param int block_hash: The hash of the block to return (e.g. 00000000000000000001158f52eae43aa7fede1bb675736f105ccb545edcf5dd)
"""
query = """
SELECT * FROM blocks
WHERE block_hash = ?
"""
bindings = (block_hash,)
cursor = db.cursor()
cursor.execute(query, bindings)
return cursor.fetchone()


def get_last_block(db):
cursor = db.cursor()
query = "SELECT * FROM blocks WHERE block_index != ? ORDER BY block_index DESC LIMIT 1"
cursor.execute(query, (config.MEMPOOL_BLOCK_INDEX,))
block = cursor.fetchone()
return block


def get_block_hash(db, block_index):
query = """
SELECT block_hash FROM blocks
WHERE block_index = ?
"""
bindings = (block_index,)
cursor = db.cursor()
cursor.execute(query, bindings)
block = cursor.fetchone()
if block is None:
return None
return block["block_hash"]


def get_blocks_time(db, block_indexes):
cursor = db.cursor()
query = f"""
SELECT block_index, block_time
FROM blocks
WHERE block_index IN ({",".join(["?" for e in range(0, len(block_indexes))])})
""" # nosec B608 # noqa: S608
cursor.execute(query, block_indexes)
blocks = cursor.fetchall()
result = {}
for block in blocks:
result[block["block_index"]] = block["block_time"]
return result


def get_current_block_index(db):
cursor = db.cursor()
query = "SELECT COALESCE(MAX(block_index), 0) AS block_index FROM blocks"
cursor.execute(query)
return cursor.fetchall()[0]["block_index"]


def get_vouts(db, tx_hash):
cursor = db.cursor()
query = """
SELECT txs.source AS source, txs_outs.*
FROM transaction_outputs txs_outs
LEFT JOIN transactions txs ON txs.tx_hash = txs_outs.tx_hash
WHERE txs_outs.tx_hash=:tx_hash
ORDER BY txs_outs.out_index
"""
bindings = {"tx_hash": tx_hash}
cursor.execute(query, bindings)
return cursor.fetchall()


def get_transactions(db, tx_hash=None, tx_index=None):
cursor = db.cursor()
where = []
bindings = []
if tx_hash is not None:
where.append("tx_hash = ?")
bindings.append(tx_hash)
if tx_index is not None:
where.append("tx_index = ?")
bindings.append(tx_index)
# no sql injection here
query = f"""SELECT * FROM transactions WHERE ({" AND ".join(where)})""" # nosec B608 # noqa: S608
cursor.execute(query, tuple(bindings))
return cursor.fetchall()


def get_transaction(db, tx_hash: str):
"""
Returns the information of a transaction
:param str tx_hash: The hash of the transaction to return (e.g. 876a6cfbd4aa22ba4fa85c2e1953a1c66649468a43a961ad16ea4d5329e3e4c5)
"""
transactions = get_transactions(db, tx_hash)
if transactions:
return transactions[0]
return None


def get_transaction_source(db, tx_hash):
cursor = db.cursor()
query = """SELECT source FROM transactions WHERE tx_hash = ?"""
bindings = (tx_hash,)
cursor.execute(query, tuple(bindings))
return cursor.fetchone()["source"]


def get_addresses(db, address=None):
cursor = db.cursor()
where = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def parse(db, tx):

outs = []
if protocol.enabled("multiple_dispenses"):
outs = ledger.ledger.get_vouts(db, tx["tx_hash"])
outs = ledger.blocks.get_vouts(db, tx["tx_hash"])
else:
outs = [tx]

Expand Down
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/messages/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ def cancel_order_match(db, order_match, status, block_index, tx_index):
if protocol.after_block_or_test_network(block_index, 310000): # Protocol change.
if not protocol.after_block_or_test_network(block_index, 315000): # Protocol change.
match(
db, ledger.ledger.get_transactions(db, tx_hash=tx0_order["tx_hash"])[0], block_index
db, ledger.blocks.get_transactions(db, tx_hash=tx0_order["tx_hash"])[0], block_index
)
match(
db, ledger.ledger.get_transactions(db, tx_hash=tx1_order["tx_hash"])[0], block_index
db, ledger.blocks.get_transactions(db, tx_hash=tx1_order["tx_hash"])[0], block_index
)

if status == "expired":
Expand Down
Loading

0 comments on commit 0207d6a

Please sign in to comment.