From 2691264dbe5d3ec3b59aa22db887bfd183f335f4 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 25 Oct 2018 21:39:42 +0200 Subject: [PATCH 1/4] Emit Block state change for confirmed block number Fix #2894 --- raiden/raiden_service.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/raiden/raiden_service.py b/raiden/raiden_service.py index 8c9813b971..b7b8ebf40e 100644 --- a/raiden/raiden_service.py +++ b/raiden/raiden_service.py @@ -530,6 +530,7 @@ def _callback_new_block(self, latest_block): latest_block_number = latest_block['number'] confirmation_blocks = self.config['blockchain']['confirmation_blocks'] confirmed_block_number = latest_block_number - confirmation_blocks + confirmed_block = self.chain.client.web3.eth.getBlock(confirmed_block_number) # handle testing private chains confirmed_block_number = max(GENESIS_BLOCK_NUMBER, confirmed_block_number) @@ -550,9 +551,9 @@ def _callback_new_block(self, latest_block): # been processed but the Block state change has not been # dispatched. state_change = Block( - block_number=latest_block_number, - gas_limit=latest_block['gasLimit'], - block_hash=bytes(latest_block['hash']), + block_number=confirmed_block_number, + gas_limit=confirmed_block['gasLimit'], + block_hash=bytes(confirmed_block['hash']), ) self.handle_state_change(state_change) From e3ab76327405550af400b6a318c1ce111a1c4f39 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 25 Oct 2018 22:00:40 +0200 Subject: [PATCH 2/4] Test for emitting only confirmed block numbers --- raiden/tests/integration/test_service.py | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/raiden/tests/integration/test_service.py b/raiden/tests/integration/test_service.py index 57475f4ee3..cd33a94495 100644 --- a/raiden/tests/integration/test_service.py +++ b/raiden/tests/integration/test_service.py @@ -1,7 +1,11 @@ import pytest from raiden.messages import Ping +from raiden.settings import DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS +from raiden.tests.utils.events import must_contain_entry +from raiden.tests.utils.geth import wait_until_block from raiden.transfer import state, views +from raiden.transfer.state_change import Block @pytest.mark.parametrize('number_of_nodes', [2]) @@ -57,3 +61,33 @@ def test_udp_ping_pong_unreachable_node(raiden_network, skip_if_not_udp): app1.raiden.address, ) assert network_state is state.NODE_NETWORK_UNREACHABLE + + +@pytest.mark.parametrize('number_of_nodes', [1]) +@pytest.mark.parametrize('channels_per_node', [0]) +@pytest.mark.parametrize('number_of_tokens', [1]) +def test_raiden_service_callback_new_block(raiden_network): + """ Regression test for: https://github.com/raiden-network/raiden/issues/2894 """ + app0 = raiden_network[0] + + app0.raiden.alarm.stop() + target_block_num = app0.raiden.chain.block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1 + wait_until_block( + app0.raiden.chain, + target_block_num, + ) + + latest_block = app0.raiden.chain.get_block(block_identifier='latest') + app0.raiden._callback_new_block(latest_block=latest_block) + + app0_state_changes = app0.raiden.wal.storage.get_statechanges_by_identifier( + from_identifier=0, + to_identifier='latest', + ) + + assert must_contain_entry(app0_state_changes, Block, { + 'block_number': target_block_num - DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, + }) + assert not must_contain_entry(app0_state_changes, Block, { + 'block_number': latest_block['number'] + }) From 778c95c0bde356cb5e20b678a99fb123d2a64446 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 25 Oct 2018 22:16:24 +0200 Subject: [PATCH 3/4] Changelog entry for 2894 --- docs/changelog.rst | 1 + raiden/tests/integration/test_service.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 75efeeae95..5b6ea8c301 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -2,6 +2,7 @@ Changelog ========= +* :bug:`2894` Raiden will no longer miss confirmation blocks at restart and will emit the block state change only for confirmed blocks. * :feature:`2857` Respect the ``--environment-type`` for private chain setup. * :feature:`2858` Changed contract address argument names to be consistent with the names of the contracts in the contracts repository. diff --git a/raiden/tests/integration/test_service.py b/raiden/tests/integration/test_service.py index cd33a94495..50fec2b254 100644 --- a/raiden/tests/integration/test_service.py +++ b/raiden/tests/integration/test_service.py @@ -89,5 +89,5 @@ def test_raiden_service_callback_new_block(raiden_network): 'block_number': target_block_num - DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS, }) assert not must_contain_entry(app0_state_changes, Block, { - 'block_number': latest_block['number'] + 'block_number': latest_block['number'], }) From e738730b6f073e18ab430e79558ef53dbd49ff36 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 26 Oct 2018 00:01:19 +0200 Subject: [PATCH 4/4] Fix few tests to adjust for emitting confirmed block --- raiden/tests/integration/api/test_pythonapi.py | 3 ++- raiden/tests/integration/test_integration_events.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/raiden/tests/integration/api/test_pythonapi.py b/raiden/tests/integration/api/test_pythonapi.py index 9be6f32d87..7ef4042d81 100644 --- a/raiden/tests/integration/api/test_pythonapi.py +++ b/raiden/tests/integration/api/test_pythonapi.py @@ -3,6 +3,7 @@ from raiden.api.python import RaidenAPI from raiden.exceptions import DepositMismatch, UnknownTokenAddress +from raiden.settings import DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS from raiden.tests.utils.events import must_contain_entry from raiden.tests.utils.geth import wait_until_block from raiden.tests.utils.transfer import get_channelstate @@ -159,7 +160,7 @@ def test_channel_lifecycle(raiden_network, token_addresses, deposit, transport_c channel12.settle_timeout + 10 # arbitrary number of additional blocks, used to wait for the settle() call ) - wait_until_block(node1.raiden.chain, settlement_block) + wait_until_block(node1.raiden.chain, settlement_block + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS) state_changes = node1.raiden.wal.storage.get_statechanges_by_identifier( from_identifier=0, diff --git a/raiden/tests/integration/test_integration_events.py b/raiden/tests/integration/test_integration_events.py index 8a27a0e7ba..f04899d4e4 100644 --- a/raiden/tests/integration/test_integration_events.py +++ b/raiden/tests/integration/test_integration_events.py @@ -16,6 +16,7 @@ ) from raiden.constants import GENESIS_BLOCK_NUMBER from raiden.network.blockchain_service import BlockChainService +from raiden.settings import DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS from raiden.tests.utils.events import must_have_event from raiden.tests.utils.geth import wait_until_block from raiden.tests.utils.network import CHAIN @@ -538,7 +539,11 @@ def test_secret_revealed(raiden_chain, deposit, settle_timeout, token_addresses) channel_state2_1.partner_state.balance_proof, ) - settle_expiration = app0.raiden.chain.block_number() + settle_timeout + settle_expiration = ( + app0.raiden.chain.block_number() + + settle_timeout + + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + ) wait_until_block(app0.raiden.chain, settle_expiration) assert_synced_channel_state(