Skip to content

Commit 418af43

Browse files
authored
Merge pull request #199 from thenewboston-developers/BC-272-add-block-to-blockchain-once-it-gets-enough-confirmations
One more unittest
2 parents 6dd453f + 93d02a1 commit 418af43

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

node/blockchain/tasks/process_block_confirmations.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ def get_next_block_confirmations(next_block_number) -> list[BlockConfirmation]:
2323
return list(BlockConfirmation.objects.filter(number=next_block_number, signer__in=cv_identifiers))
2424

2525

26-
def get_consensus_block_hash_with_confirmations(confirmations,
27-
minimum_consensus) -> Optional[tuple[Hash, list[BlockConfirmation]]]:
26+
def get_consensus_block_hash_with_confirmations(
27+
confirmations: list[BlockConfirmation], minimum_consensus: int
28+
) -> Optional[tuple[Hash, list[BlockConfirmation]]]:
29+
30+
assert len(set(confirmation.number for confirmation in confirmations)) <= 1
31+
2832
key_func = attrgetter('hash')
29-
grouped_confirmations = [(Hash(hash_), list(confirmations))
30-
for hash_, confirmations in groupby(sorted(confirmations, key=key_func), key=key_func)]
31-
finalizable_hashes = [(hash_, confirmations)
32-
for hash_, confirmations in grouped_confirmations
33-
if len(confirmations) >= minimum_consensus]
33+
grouped_confirmations = [(Hash(hash_), list(confirmations_))
34+
for hash_, confirmations_ in groupby(sorted(confirmations, key=key_func), key=key_func)]
35+
finalizable_hashes = [(hash_, confirmations_)
36+
for hash_, confirmations_ in grouped_confirmations
37+
if len(confirmations_) >= minimum_consensus]
3438

3539
if not finalizable_hashes:
3640
return None # No consensus, yet
@@ -44,7 +48,7 @@ def get_consensus_block_hash_with_confirmations(confirmations,
4448
return block_hash, consensus_confirmations
4549

4650

47-
def is_valid_consensus(confirmations: list[BlockConfirmation], minimum_consensus):
51+
def is_valid_consensus(confirmations: list[BlockConfirmation], minimum_consensus: int):
4852
# Validate confirmations, since they may have not been validated on API call because some of them were added
4953
# much earlier then the next block number become equal to confirmation block number
5054
assert len(set(confirmation.signer for confirmation in confirmations)) == len(confirmations)

node/blockchain/tests/test_tasks/test_process_block_confirmations.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
from model_bakery import baker
55

66
from node.blockchain.models import Node
7-
from node.blockchain.tasks.process_block_confirmations import get_next_block_confirmations
7+
from node.blockchain.tasks.process_block_confirmations import (
8+
get_consensus_block_hash_with_confirmations, get_next_block_confirmations
9+
)
810
from node.blockchain.types import NodeRole
911

1012

@@ -24,3 +26,20 @@ def test_get_next_block_confirmations():
2426
assert set(get_next_block_confirmations(3)) == {bc2, bc3}
2527

2628
mock.assert_called_once_with((NodeRole.CONFIRMATION_VALIDATOR,))
29+
30+
31+
@pytest.mark.django_db
32+
def test_get_consensus_block_hash_with_confirmations():
33+
bc0 = baker.make('blockchain.BlockConfirmation', number=3, signer='0' * 64, hash='a' * 128)
34+
bc1 = baker.make('blockchain.BlockConfirmation', number=3, signer='1' * 64, hash='a' * 128)
35+
bc2 = baker.make('blockchain.BlockConfirmation', number=3, signer='2' * 64, hash='b' * 128)
36+
37+
confirmations = [bc0, bc1, bc2]
38+
result = get_consensus_block_hash_with_confirmations(confirmations, 2)
39+
assert result
40+
hash_, consensus_confirmations = result
41+
assert hash_ == bc0.hash
42+
assert consensus_confirmations == [bc0, bc1]
43+
44+
result = get_consensus_block_hash_with_confirmations(confirmations, 3)
45+
assert result is None

0 commit comments

Comments
 (0)