Skip to content

Commit b5f092c

Browse files
committed
NodeClient.send_block()
1 parent a08be10 commit b5f092c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

node/core/clients/node.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ def send_signed_change_request(self, /, address: str, signed_change_request: Sig
189189
# enums to basic types in dict while still having enums in model instance representation
190190
return self.http_post(address, 'signed-change-requests', data=signed_change_request.json())
191191

192+
@with_node
193+
def send_block(self, /, address: str, block: Block):
194+
logger.debug('Sending %s to %s', block, address)
195+
return self.http_post(address, 'blocks', data=block.json())
196+
192197
def yield_nodes(self, /, address: str) -> Generator[Node, None, None]:
193198
for item in self.yield_resource(address, 'nodes', by_limit=LIST_NODES_LIMIT):
194199
yield Node.parse_obj(item)

node/core/tests/test_node_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from node.blockchain.inner_models import Node as InnerNode
1010
from node.blockchain.models import Block as ORMBlock
1111
from node.blockchain.models import Node as ORMNode
12+
from node.blockchain.models import PendingBlock
13+
from node.blockchain.tests.factories.block import make_block
14+
from node.blockchain.tests.factories.block_message.node_declaration import make_node_declaration_block_message
1215
from node.blockchain.tests.factories.node import make_node
1316
from node.core.utils.cryptography import get_node_identifier
1417

@@ -87,6 +90,28 @@ def test_send_scr_to_node(
8790
))
8891

8992

93+
@pytest.mark.usefixtures('base_blockchain', 'as_confirmation_validator')
94+
def test_send_block_to_address_integration(
95+
test_server_address, primary_validator_key_pair, regular_node, regular_node_key_pair, smart_mocked_node_client
96+
):
97+
assert not PendingBlock.objects.exists()
98+
99+
facade = BlockchainFacade.get_instance()
100+
block_message = make_node_declaration_block_message(regular_node, regular_node_key_pair, facade)
101+
102+
assert facade.get_primary_validator().identifier == primary_validator_key_pair.public
103+
block = make_block(block_message, primary_validator_key_pair.private)
104+
105+
with patch('node.blockchain.views.block.start_process_pending_blocks_task') as mock:
106+
response = smart_mocked_node_client.send_block(test_server_address, block)
107+
108+
assert response.status_code == 204
109+
mock.assert_called()
110+
pending_block = PendingBlock.objects.get_or_none(number=block.get_block_number(), hash=block.make_hash())
111+
assert pending_block
112+
assert pending_block.body == block.json()
113+
114+
90115
@pytest.mark.django_db
91116
@pytest.mark.usefixtures('rich_blockchain')
92117
@pytest.mark.parametrize('block_identifier, block_number', (

0 commit comments

Comments
 (0)