mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Makes all BLockProcessor methods static
This commit is contained in:
@@ -98,7 +98,8 @@ class BlockProcessor:
|
||||
|
||||
return tx
|
||||
|
||||
def get_distance_to_tip(self, target_block_hash):
|
||||
@staticmethod
|
||||
def get_distance_to_tip(target_block_hash):
|
||||
"""
|
||||
Compute the distance between a given block hash and the best chain tip.
|
||||
|
||||
@@ -114,10 +115,10 @@ class BlockProcessor:
|
||||
|
||||
distance = None
|
||||
|
||||
chain_tip = self.get_best_block_hash()
|
||||
chain_tip_height = self.get_block(chain_tip).get("height")
|
||||
chain_tip = BlockProcessor.get_best_block_hash()
|
||||
chain_tip_height = BlockProcessor.get_block(chain_tip).get("height")
|
||||
|
||||
target_block = self.get_block(target_block_hash)
|
||||
target_block = BlockProcessor.get_block(target_block_hash)
|
||||
|
||||
if target_block is not None:
|
||||
target_block_height = target_block.get("height")
|
||||
@@ -126,7 +127,8 @@ class BlockProcessor:
|
||||
|
||||
return distance
|
||||
|
||||
def get_missed_blocks(self, last_know_block_hash):
|
||||
@staticmethod
|
||||
def get_missed_blocks(last_know_block_hash):
|
||||
"""
|
||||
Compute the blocks between the current best chain tip and a given block hash (``last_know_block_hash``).
|
||||
|
||||
@@ -142,22 +144,23 @@ class BlockProcessor:
|
||||
|
||||
# If last_known_block_hash is on the best chain, this will return last_know_block_hash and and empty list.
|
||||
# Otherwise we will get the last_common_ancestor and a list of dropped transactions.
|
||||
last_common_ancestor, dropped_txs = self.find_last_common_ancestor(last_know_block_hash)
|
||||
last_common_ancestor, dropped_txs = BlockProcessor.find_last_common_ancestor(last_know_block_hash)
|
||||
# TODO: 32-handle-reorgs-offline
|
||||
# Dropped txs is not used yet. It is necessary to manage the changes in the Watcher/Responder due to a reorg
|
||||
|
||||
current_block_hash = self.get_best_block_hash()
|
||||
current_block_hash = BlockProcessor.get_best_block_hash()
|
||||
missed_blocks = []
|
||||
|
||||
while current_block_hash != last_common_ancestor and current_block_hash is not None:
|
||||
missed_blocks.append(current_block_hash)
|
||||
|
||||
current_block = self.get_block(current_block_hash)
|
||||
current_block = BlockProcessor.get_block(current_block_hash)
|
||||
current_block_hash = current_block.get("previousblockhash")
|
||||
|
||||
return missed_blocks[::-1]
|
||||
|
||||
def is_block_in_best_chain(self, block_hash):
|
||||
@staticmethod
|
||||
def is_block_in_best_chain(block_hash):
|
||||
"""
|
||||
Checks whether or not a given block is on the best chain. Blocks are identified by block_hash.
|
||||
|
||||
@@ -174,7 +177,7 @@ class BlockProcessor:
|
||||
KeyError: If the block cannot be found in the blockchain.
|
||||
"""
|
||||
|
||||
block = self.get_block(block_hash)
|
||||
block = BlockProcessor.get_block(block_hash)
|
||||
|
||||
if block is None:
|
||||
# This should never happen as long as we are using the same node, since bitcoind never drops orphan blocks
|
||||
@@ -186,7 +189,8 @@ class BlockProcessor:
|
||||
else:
|
||||
return False
|
||||
|
||||
def find_last_common_ancestor(self, last_known_block_hash):
|
||||
@staticmethod
|
||||
def find_last_common_ancestor(last_known_block_hash):
|
||||
"""
|
||||
Finds the last common ancestor between the current best chain tip and the last block known by us (older block).
|
||||
|
||||
@@ -204,8 +208,8 @@ class BlockProcessor:
|
||||
target_block_hash = last_known_block_hash
|
||||
dropped_txs = []
|
||||
|
||||
while not self.is_block_in_best_chain(target_block_hash):
|
||||
block = self.get_block(target_block_hash)
|
||||
while not BlockProcessor.is_block_in_best_chain(target_block_hash):
|
||||
block = BlockProcessor.get_block(target_block_hash)
|
||||
dropped_txs.extend(block.get("tx"))
|
||||
target_block_hash = block.get("previousblockhash")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user