Skip to content

Commit 5700427

Browse files
committed
send new block tasks
1 parent f42dafd commit 5700427

File tree

24 files changed

+192
-52
lines changed

24 files changed

+192
-52
lines changed

node/blockchain/facade.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ def get_next_block_identifier(self) -> Optional[BlockIdentifier]:
137137
# TODO(dmu) HIGH: Implement method via write-through cache
138138
# https://thenewboston.atlassian.net/browse/BC-175
139139
last_block = self.get_last_block()
140-
if not last_block:
141-
return None
140+
return HashableStringWrapper(last_block.body).make_hash() if last_block else None
142141

143-
return HashableStringWrapper(last_block.body).make_hash()
142+
@staticmethod
143+
def get_block_by_number(number) -> Block:
144+
block = get_block_model().objects.get_block_by_number(_id=number)
145+
return block.get_block() if block else None
144146

145147
@staticmethod
146148
def get_account_lock(account_number) -> AccountLock:
@@ -253,3 +255,8 @@ def get_primary_validator(self) -> Optional[Node]:
253255
logger.warning('Primary validator %s is in PV schedule but not declared as a node', node_identifier)
254256

255257
return node
258+
259+
@staticmethod
260+
def yield_nodes(roles=None):
261+
for node in ORMNode.objects.filter_by_roles(roles):
262+
yield node.get_node()

node/blockchain/models/block.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def create(self, *args, **kwargs):
1414
def get_last_block(self):
1515
return self.order_by('-_id').first()
1616

17+
def get_block_by_number(self, number):
18+
return self.get_or_none(_id=number)
19+
1720
def get_next_block_number(self):
1821
last_block = self.get_last_block()
1922
return last_block._id + 1 if last_block else 0

node/blockchain/models/node.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ class NodeQuerySet(CustomQuerySet):
1515

1616
def filter_by_roles(self, roles: Collection[NodeRole]):
1717
# self.annotate() does not work with Djongo
18-
roles = set(roles or ())
19-
if not roles or set(roles) == NODE_ROLES:
18+
if not roles:
19+
return self.none()
20+
21+
roles = set(roles)
22+
if roles == NODE_ROLES:
2023
return self
2124

25+
assert not (roles - NODE_ROLES)
26+
2227
from node.blockchain.models import Schedule
2328

2429
assert {NodeRole.REGULAR_NODE, NodeRole.CONFIRMATION_VALIDATOR, NodeRole.PRIMARY_VALIDATOR} == NODE_ROLES
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
import logging
2+
13
from celery import shared_task
24

5+
from node.blockchain.facade import BlockchainFacade
6+
from node.blockchain.models.block import Block as ORMBlock
7+
from node.blockchain.types import NodeRole
8+
from node.core.clients.node import NodeClient
9+
10+
logger = logging.getLogger(__name__)
11+
312

413
@shared_task
5-
def send_new_block_to_node_task():
6-
raise NotImplementedError
14+
def send_new_block_to_node_task(block_number, node_identifier):
15+
node = BlockchainFacade.get_instance().get_node_by_identifier(node_identifier)
16+
block = ORMBlock.objects.get_block_by_number(block_number)
17+
NodeClient.get_instance().send_block(node, block.body)
718

819

920
@shared_task
1021
def send_new_block_task(block_number):
11-
raise NotImplementedError
22+
logger.debug('Sending block number %s to all CVs', block_number)
23+
for node in BlockchainFacade.get_instance().yield_nodes(roles={NodeRole.CONFIRMATION_VALIDATOR}):
24+
logger.debug('Sending block number %s to %s', block_number, node)
25+
send_new_block_to_node_task.delay(block_number, node.identifier)
1226

1327

14-
def start_send_new_block_task():
15-
send_new_block_task.delay()
28+
def start_send_new_block_task(block_number):
29+
send_new_block_task.delay(block_number)

node/blockchain/tests/fixtures/account.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def confirmation_validator_key_pair() -> KeyPair:
2828
)
2929

3030

31+
@pytest.fixture
32+
def confirmation_validator_key_pair_2() -> KeyPair:
33+
return KeyPair(
34+
public=AccountNumber('5db9a262236cc148fd2adf841dbe0967e9bfe77e5e482dc7e0ef0c59d7fb56cf'),
35+
private=SigningKey('67eb1838664e12d0773988618a1c69d90757dae5b9ea4e7c7e543b118f31aa55'),
36+
)
37+
38+
3139
@pytest.fixture
3240
def regular_node_key_pair() -> KeyPair:
3341
return KeyPair(

node/blockchain/tests/fixtures/blockchain.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ def rich_blockchain(
2525
base_blockchain,
2626
primary_validator_key_pair,
2727
confirmation_validator_key_pair,
28+
confirmation_validator_key_pair_2,
2829
regular_node_declaration_signed_change_request,
2930
self_node_declaration_signed_change_request,
3031
confirmation_validator_declaration_signed_change_request,
32+
confirmation_validator_2_declaration_signed_change_request,
3133
):
3234
blockchain_facade = BlockchainFacade.get_instance()
3335

@@ -49,10 +51,17 @@ def rich_blockchain(
4951
validate=False,
5052
)
5153

54+
blockchain_facade.add_block_from_signed_change_request(
55+
signed_change_request=confirmation_validator_2_declaration_signed_change_request,
56+
signing_key=primary_validator_key_pair.private,
57+
validate=False,
58+
)
59+
5260
blockchain_facade.add_block_from_signed_change_request(
5361
signed_change_request=make_pv_schedule_update_signed_change_request({
5462
'0': primary_validator_key_pair.public,
5563
'10000': confirmation_validator_key_pair.public,
64+
'20000': confirmation_validator_key_pair_2.public,
5665
}, primary_validator_key_pair),
5766
signing_key=primary_validator_key_pair.private,
5867
validate=False,

node/blockchain/tests/fixtures/blockchain_structure/node_declaration.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def confirmation_validator_declaration_signed_change_request(
3232
return make_node_declaration_signed_change_request(confirmation_validator_node, confirmation_validator_key_pair)
3333

3434

35+
@pytest.fixture
36+
def confirmation_validator_2_declaration_signed_change_request(
37+
confirmation_validator_node_2, confirmation_validator_key_pair_2, db
38+
):
39+
return make_node_declaration_signed_change_request(
40+
confirmation_validator_node_2, confirmation_validator_key_pair_2
41+
)
42+
43+
3544
@pytest.fixture
3645
def node_declaration_block_message(
3746
node_declaration_signed_change_request_message, regular_node_key_pair, base_blockchain, db

node/blockchain/tests/fixtures/misc.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ def clean_up_block_lock(lock_collection):
2929

3030

3131
@pytest.fixture(autouse=True)
32-
def test_settings(settings):
33-
with override_settings(
34-
USE_ON_COMMIT_HOOK=False,
35-
LOCK_DEFAULT_TIMEOUT_SECONDS=0.001,
36-
SECRET_KEY='b27c612c6cbeac10c8788fbc95b29f563cc0ea2eb7d6be08',
37-
NODE_SIGNING_KEY='a025da120a1c95b27f17bb9442af9c27d3a357733aa150b458f21682a2d539a9',
38-
CELERY_BROKER_URL='memory://',
39-
CELERY_TASK_ALWAYS_EAGER=True,
40-
):
32+
def blockchain_test_settings(settings):
33+
with override_settings(LOCK_DEFAULT_TIMEOUT_SECONDS=0.001,):
4134
yield

node/blockchain/tests/fixtures/mocks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
def mock_get_primary_validator(primary_validator_node):
88
with patch('node.blockchain.facade.BlockchainFacade.get_primary_validator', return_value=primary_validator_node):
99
yield
10+
11+
12+
@pytest.fixture(autouse=True)
13+
def start_send_new_block_task_mock():
14+
with patch('node.blockchain.views.signed_change_request.start_send_new_block_task') as mock:
15+
yield mock

node/blockchain/tests/fixtures/node.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ def confirmation_validator_node(confirmation_validator_key_pair):
2929
)
3030

3131

32+
@pytest.fixture
33+
def confirmation_validator_node_2(confirmation_validator_key_pair_2):
34+
return make_node(
35+
confirmation_validator_key_pair_2,
36+
['http://not-existing-confirmation-validator-2-address-674898923.com:8555/']
37+
)
38+
39+
3240
@pytest.fixture
3341
def self_node(self_node_key_pair, test_server_address):
3442
return make_node(self_node_key_pair, ['http://not-existing-self-address-674898923.com:8555/', test_server_address])

0 commit comments

Comments
 (0)