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
|
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.
|
Compute the distance between a given block hash and the best chain tip.
|
||||||
|
|
||||||
@@ -114,10 +115,10 @@ class BlockProcessor:
|
|||||||
|
|
||||||
distance = None
|
distance = None
|
||||||
|
|
||||||
chain_tip = self.get_best_block_hash()
|
chain_tip = BlockProcessor.get_best_block_hash()
|
||||||
chain_tip_height = self.get_block(chain_tip).get("height")
|
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:
|
if target_block is not None:
|
||||||
target_block_height = target_block.get("height")
|
target_block_height = target_block.get("height")
|
||||||
@@ -126,7 +127,8 @@ class BlockProcessor:
|
|||||||
|
|
||||||
return distance
|
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``).
|
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.
|
# 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.
|
# 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
|
# 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
|
# 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 = []
|
missed_blocks = []
|
||||||
|
|
||||||
while current_block_hash != last_common_ancestor and current_block_hash is not None:
|
while current_block_hash != last_common_ancestor and current_block_hash is not None:
|
||||||
missed_blocks.append(current_block_hash)
|
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")
|
current_block_hash = current_block.get("previousblockhash")
|
||||||
|
|
||||||
return missed_blocks[::-1]
|
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.
|
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.
|
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:
|
if block is None:
|
||||||
# This should never happen as long as we are using the same node, since bitcoind never drops orphan blocks
|
# 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:
|
else:
|
||||||
return False
|
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).
|
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
|
target_block_hash = last_known_block_hash
|
||||||
dropped_txs = []
|
dropped_txs = []
|
||||||
|
|
||||||
while not self.is_block_in_best_chain(target_block_hash):
|
while not BlockProcessor.is_block_in_best_chain(target_block_hash):
|
||||||
block = self.get_block(target_block_hash)
|
block = BlockProcessor.get_block(target_block_hash)
|
||||||
dropped_txs.extend(block.get("tx"))
|
dropped_txs.extend(block.get("tx"))
|
||||||
target_block_hash = block.get("previousblockhash")
|
target_block_hash = block.get("previousblockhash")
|
||||||
|
|
||||||
|
|||||||
@@ -57,8 +57,7 @@ def test_decode_raw_transaction_invalid():
|
|||||||
|
|
||||||
|
|
||||||
def test_get_missed_blocks():
|
def test_get_missed_blocks():
|
||||||
block_processor = BlockProcessor()
|
target_block = BlockProcessor.get_best_block_hash()
|
||||||
target_block = block_processor.get_best_block_hash()
|
|
||||||
|
|
||||||
# Generate some blocks and store the hash in a list
|
# Generate some blocks and store the hash in a list
|
||||||
missed_blocks = []
|
missed_blocks = []
|
||||||
@@ -67,47 +66,44 @@ def test_get_missed_blocks():
|
|||||||
missed_blocks.append(BlockProcessor.get_best_block_hash())
|
missed_blocks.append(BlockProcessor.get_best_block_hash())
|
||||||
|
|
||||||
# Check what we've missed
|
# Check what we've missed
|
||||||
assert block_processor.get_missed_blocks(target_block) == missed_blocks
|
assert BlockProcessor.get_missed_blocks(target_block) == missed_blocks
|
||||||
|
|
||||||
# We can see how it does not work if we replace the target by the first element in the list
|
# We can see how it does not work if we replace the target by the first element in the list
|
||||||
block_tip = missed_blocks[0]
|
block_tip = missed_blocks[0]
|
||||||
assert block_processor.get_missed_blocks(block_tip) != missed_blocks
|
assert BlockProcessor.get_missed_blocks(block_tip) != missed_blocks
|
||||||
|
|
||||||
# But it does again if we skip that block
|
# But it does again if we skip that block
|
||||||
assert block_processor.get_missed_blocks(block_tip) == missed_blocks[1:]
|
assert BlockProcessor.get_missed_blocks(block_tip) == missed_blocks[1:]
|
||||||
|
|
||||||
|
|
||||||
def test_get_distance_to_tip():
|
def test_get_distance_to_tip():
|
||||||
target_distance = 5
|
target_distance = 5
|
||||||
|
|
||||||
block_processor = BlockProcessor()
|
target_block = BlockProcessor.get_best_block_hash()
|
||||||
target_block = block_processor.get_best_block_hash()
|
|
||||||
|
|
||||||
# Mine some blocks up to the target distance
|
# Mine some blocks up to the target distance
|
||||||
generate_blocks(target_distance)
|
generate_blocks(target_distance)
|
||||||
|
|
||||||
# Check if the distance is properly computed
|
# Check if the distance is properly computed
|
||||||
assert block_processor.get_distance_to_tip(target_block) == target_distance
|
assert BlockProcessor.get_distance_to_tip(target_block) == target_distance
|
||||||
|
|
||||||
|
|
||||||
def test_is_block_in_best_chain():
|
def test_is_block_in_best_chain():
|
||||||
block_processor = BlockProcessor()
|
best_block_hash = BlockProcessor.get_best_block_hash()
|
||||||
best_block_hash = block_processor.get_best_block_hash()
|
best_block = BlockProcessor.get_block(best_block_hash)
|
||||||
best_block = block_processor.get_block(best_block_hash)
|
|
||||||
|
|
||||||
assert block_processor.is_block_in_best_chain(best_block_hash)
|
assert BlockProcessor.is_block_in_best_chain(best_block_hash)
|
||||||
|
|
||||||
fork(best_block.get("previousblockhash"))
|
fork(best_block.get("previousblockhash"))
|
||||||
generate_blocks(2)
|
generate_blocks(2)
|
||||||
|
|
||||||
assert not block_processor.is_block_in_best_chain(best_block_hash)
|
assert not BlockProcessor.is_block_in_best_chain(best_block_hash)
|
||||||
|
|
||||||
|
|
||||||
def test_find_last_common_ancestor():
|
def test_find_last_common_ancestor():
|
||||||
block_processor = BlockProcessor()
|
ancestor = BlockProcessor.get_best_block_hash()
|
||||||
ancestor = block_processor.get_best_block_hash()
|
|
||||||
generate_blocks(3)
|
generate_blocks(3)
|
||||||
best_block_hash = block_processor.get_best_block_hash()
|
best_block_hash = BlockProcessor.get_best_block_hash()
|
||||||
|
|
||||||
# Create a fork (forking creates a block if the mock is set by events)
|
# Create a fork (forking creates a block if the mock is set by events)
|
||||||
fork(ancestor)
|
fork(ancestor)
|
||||||
@@ -116,6 +112,6 @@ def test_find_last_common_ancestor():
|
|||||||
generate_blocks(5)
|
generate_blocks(5)
|
||||||
|
|
||||||
# The last common ancestor between the old best and the new best should be the "ancestor"
|
# The last common ancestor between the old best and the new best should be the "ancestor"
|
||||||
last_common_ancestor, dropped_txs = block_processor.find_last_common_ancestor(best_block_hash)
|
last_common_ancestor, dropped_txs = BlockProcessor.find_last_common_ancestor(best_block_hash)
|
||||||
assert last_common_ancestor == ancestor
|
assert last_common_ancestor == ancestor
|
||||||
assert len(dropped_txs) == 3
|
assert len(dropped_txs) == 3
|
||||||
|
|||||||
Reference in New Issue
Block a user