Skip to content

Commit 5b911ba

Browse files
hackaugustopalango
authored andcommitted
Fix token network clean up
The token network state machine was not properly cleaning up the channel state, if the channel state machine returns a new_state set to None the token network must remove the NettingChannelState from it's map. fix #1882
1 parent 1dc3c11 commit 5b911ba

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

raiden/tests/unit/test_tokennetwork.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from raiden.constants import UINT64_MAX
55
from raiden.tests.utils import factories
66
from raiden.transfer import channel, token_network
7-
from raiden.transfer.state_change import ContractReceiveChannelNew
7+
from raiden.transfer.state_change import (
8+
ContractReceiveChannelClosed,
9+
ContractReceiveChannelNew,
10+
ContractReceiveChannelSettled,
11+
)
812
from raiden.transfer.state import TokenNetworkState
913

1014

@@ -56,3 +60,58 @@ def test_contract_receive_channelnew_must_be_idempotent():
5660

5761
channelmap_by_address = iteration.new_state.partneraddresses_to_channels
5862
assert channelmap_by_address[channel_state1.partner_state.address] == channel_state1, msg
63+
64+
65+
def test_channel_settle_must_properly_cleanup():
66+
open_block_number = 10
67+
pseudo_random_generator = random.Random()
68+
69+
token_network_id = factories.make_address()
70+
token_id = factories.make_address()
71+
token_network_state = TokenNetworkState(token_network_id, token_id)
72+
73+
amount = 30
74+
our_balance = amount + 50
75+
channel_state = factories.make_channel(our_balance=our_balance)
76+
77+
channel_new_state_change = ContractReceiveChannelNew(token_network_id, channel_state)
78+
79+
channel_new_iteration = token_network.state_transition(
80+
token_network_state,
81+
channel_new_state_change,
82+
pseudo_random_generator,
83+
open_block_number,
84+
)
85+
86+
closed_block_number = open_block_number + 10
87+
channel_close_state_change = ContractReceiveChannelClosed(
88+
token_network_id,
89+
channel_state.identifier,
90+
channel_state.partner_state.address,
91+
closed_block_number,
92+
)
93+
94+
channel_closed_iteration = token_network.state_transition(
95+
channel_new_iteration.new_state,
96+
channel_close_state_change,
97+
pseudo_random_generator,
98+
closed_block_number,
99+
)
100+
101+
settle_block_number = closed_block_number + channel_state.settle_timeout + 1
102+
channel_settled_state_change = ContractReceiveChannelSettled(
103+
token_network_id,
104+
channel_state.identifier,
105+
settle_block_number,
106+
)
107+
108+
channel_settled_iteration = token_network.state_transition(
109+
channel_closed_iteration.new_state,
110+
channel_settled_state_change,
111+
pseudo_random_generator,
112+
closed_block_number,
113+
)
114+
115+
token_network_state_after_settle = channel_settled_iteration.new_state
116+
ids_to_channels = token_network_state_after_settle.channelidentifiers_to_channels
117+
assert channel_state.identifier not in ids_to_channels

raiden/transfer/token_network.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def subdispatch_to_channel_by_id(
3131
pseudo_random_generator,
3232
block_number,
3333
)
34+
35+
if result.new_state is None:
36+
del ids_to_channels[state_change.channel_identifier]
37+
else:
38+
ids_to_channels[state_change.channel_identifier] = result.new_state
39+
3440
events.extend(result.events)
3541

3642
return TransitionResult(token_network_state, events)

0 commit comments

Comments
 (0)