Makes all BLockProcessor methods static

This commit is contained in:
Sergi Delgado Segura
2020-01-02 11:21:01 +01:00
parent 5ce5936fc0
commit 3ff10ea1bf
2 changed files with 30 additions and 30 deletions

View File

@@ -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")

View File

@@ -57,8 +57,7 @@ def test_decode_raw_transaction_invalid():
def test_get_missed_blocks():
block_processor = BlockProcessor()
target_block = block_processor.get_best_block_hash()
target_block = BlockProcessor.get_best_block_hash()
# Generate some blocks and store the hash in a list
missed_blocks = []
@@ -67,47 +66,44 @@ def test_get_missed_blocks():
missed_blocks.append(BlockProcessor.get_best_block_hash())
# 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
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
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():
target_distance = 5
block_processor = BlockProcessor()
target_block = block_processor.get_best_block_hash()
target_block = BlockProcessor.get_best_block_hash()
# Mine some blocks up to the target distance
generate_blocks(target_distance)
# 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():
block_processor = BlockProcessor()
best_block_hash = block_processor.get_best_block_hash()
best_block = block_processor.get_block(best_block_hash)
best_block_hash = BlockProcessor.get_best_block_hash()
best_block = BlockProcessor.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"))
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():
block_processor = BlockProcessor()
ancestor = block_processor.get_best_block_hash()
ancestor = BlockProcessor.get_best_block_hash()
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)
fork(ancestor)
@@ -116,6 +112,6 @@ def test_find_last_common_ancestor():
generate_blocks(5)
# 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 len(dropped_txs) == 3