-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathstate_change.py
More file actions
433 lines (324 loc) · 12.6 KB
/
state_change.py
File metadata and controls
433 lines (324 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# pylint: disable=too-few-public-methods,too-many-arguments,too-many-instance-attributes
from dataclasses import dataclass, field
from raiden.constants import EMPTY_SECRETHASH
from raiden.settings import MediationFeeConfig
from raiden.transfer.architecture import (
AuthenticatedSenderStateChange,
ContractReceiveStateChange,
StateChange,
)
from raiden.transfer.identifiers import CanonicalIdentifier
from raiden.transfer.state import (
BalanceProofSignedState,
NettingChannelState,
TokenNetworkRegistryState,
TokenNetworkState,
TransactionChannelDeposit,
)
from raiden.utils.secrethash import sha256_secrethash
from raiden.utils.typing import (
Address,
AddressMetadata,
BlockExpiration,
BlockGasLimit,
BlockHash,
BlockNumber,
BlockTimeout,
ChainID,
ChannelID,
Locksroot,
MessageID,
Nonce,
Optional,
PaymentID,
Secret,
SecretHash,
SecretRegistryAddress,
Signature,
T_Address,
T_BlockNumber,
T_Secret,
T_SecretHash,
T_SecretRegistryAddress,
TokenAmount,
TokenNetworkAddress,
TokenNetworkRegistryAddress,
WithdrawAmount,
typecheck,
)
@dataclass(frozen=True)
class BalanceProofStateChange(AuthenticatedSenderStateChange):
"""Marker used for state changes which contain a balance proof."""
balance_proof: BalanceProofSignedState
def __post_init__(self) -> None:
typecheck(self.balance_proof, BalanceProofSignedState)
@dataclass(frozen=True)
class Block(StateChange):
"""Transition used when a new block is mined.
Args:
block_number: The current block_number.
"""
block_number: BlockNumber
gas_limit: BlockGasLimit
block_hash: BlockHash
def __post_init__(self) -> None:
typecheck(self.block_number, T_BlockNumber)
@dataclass(frozen=True)
class ActionCancelPayment(StateChange):
"""The user requests the transfer to be cancelled.
This state change can fail, it depends on the node's role and the current
state of the transfer.
"""
payment_identifier: PaymentID
@dataclass(frozen=True)
class ActionChannelClose(StateChange):
"""User is closing an existing channel."""
canonical_identifier: CanonicalIdentifier
@property
def chain_identifier(self) -> ChainID:
return self.canonical_identifier.chain_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@dataclass(frozen=True)
class ActionChannelWithdraw(StateChange):
"""Withdraw funds from channel."""
canonical_identifier: CanonicalIdentifier
total_withdraw: WithdrawAmount
recipient_metadata: Optional[AddressMetadata] = None
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@dataclass(frozen=True)
class ActionChannelCoopSettle(StateChange):
"""Cooperatively withdraw funds from channel back to both parties
and close the channel in a single operation."""
canonical_identifier: CanonicalIdentifier
recipient_metadata: Optional[AddressMetadata] = None
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@dataclass(frozen=True)
class ContractReceiveChannelNew(ContractReceiveStateChange):
"""A new channel was created and this node IS a participant."""
channel_state: NettingChannelState
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.channel_state.canonical_identifier.token_network_address
@property
def channel_identifier(self) -> ChannelID:
return self.channel_state.canonical_identifier.channel_identifier
@dataclass(frozen=True)
class ContractReceiveChannelClosed(ContractReceiveStateChange):
"""A channel to which this node IS a participant was closed."""
transaction_from: Address
canonical_identifier: CanonicalIdentifier
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ContractReceiveChannelDeposit(ContractReceiveStateChange):
"""A channel to which this node IS a participant had a deposit."""
canonical_identifier: CanonicalIdentifier
deposit_transaction: TransactionChannelDeposit
fee_config: MediationFeeConfig
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ContractReceiveChannelWithdraw(ContractReceiveStateChange):
"""A channel to which this node IS a participant had a withdraw."""
canonical_identifier: CanonicalIdentifier
participant: Address
total_withdraw: WithdrawAmount
fee_config: MediationFeeConfig
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ContractReceiveChannelSettled(ContractReceiveStateChange):
"""A channel to which this node IS a participant was settled."""
canonical_identifier: CanonicalIdentifier
our_onchain_locksroot: Locksroot
our_transferred_amount: TokenAmount
partner_onchain_locksroot: Locksroot
partner_transferred_amount: TokenAmount
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ActionChannelSetRevealTimeout(StateChange):
"""Change the reveal timeout value of a given channel."""
canonical_identifier: CanonicalIdentifier
reveal_timeout: BlockTimeout
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ContractReceiveNewTokenNetworkRegistry(ContractReceiveStateChange):
"""Registers a new token network registry.
A token network registry corresponds to a registry smart contract.
"""
token_network_registry: TokenNetworkRegistryState
def __post_init__(self) -> None:
super().__post_init__()
typecheck(self.token_network_registry, TokenNetworkRegistryState)
@dataclass(frozen=True)
class ContractReceiveNewTokenNetwork(ContractReceiveStateChange):
"""A new token was registered with the token network registry."""
token_network_registry_address: TokenNetworkRegistryAddress
token_network: TokenNetworkState
def __post_init__(self) -> None:
super().__post_init__()
typecheck(self.token_network, TokenNetworkState)
@dataclass(frozen=True)
class ContractReceiveSecretReveal(ContractReceiveStateChange):
"""A new secret was registered with the SecretRegistry contract."""
secret_registry_address: SecretRegistryAddress
secrethash: SecretHash
secret: Secret
def __post_init__(self) -> None:
super().__post_init__()
typecheck(self.secret_registry_address, T_SecretRegistryAddress)
typecheck(self.secrethash, T_SecretHash)
typecheck(self.secret, T_Secret)
@dataclass(frozen=True)
class ContractReceiveChannelBatchUnlock(ContractReceiveStateChange):
"""All the locks were claimed via the blockchain.
Used when all the hash time locks were unlocked and a log ChannelUnlocked is emitted
by the token network contract.
Note:
For this state change the contract caller is not important but only the
receiving address. `receiver` is the address to which the `unlocked_amount`
was transferred. `returned_tokens` was transferred to the channel partner.
"""
canonical_identifier: CanonicalIdentifier
receiver: Address
sender: Address
locksroot: Locksroot
unlocked_amount: TokenAmount
returned_tokens: TokenAmount
def __post_init__(self) -> None:
super().__post_init__()
typecheck(self.receiver, T_Address)
typecheck(self.sender, T_Address)
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ContractReceiveRouteNew(ContractReceiveStateChange):
"""New channel was created and this node is NOT a participant."""
canonical_identifier: CanonicalIdentifier
participant1: Address
participant2: Address
def __post_init__(self) -> None:
super().__post_init__()
typecheck(self.participant1, T_Address)
typecheck(self.participant2, T_Address)
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
# FIXME: remove on next breaking release
# This needs to be kept, so that the state changes that are already in the DB
# continue to be decodable.
@dataclass(frozen=True)
class ContractReceiveUpdateTransfer(ContractReceiveStateChange):
canonical_identifier: CanonicalIdentifier
nonce: Nonce
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ReceiveUnlock(BalanceProofStateChange):
message_identifier: MessageID
secret: Secret
secrethash: SecretHash = field(default=EMPTY_SECRETHASH)
def __post_init__(self) -> None:
super().__post_init__()
object.__setattr__(self, "secrethash", sha256_secrethash(self.secret))
@dataclass(frozen=True)
class ReceiveDelivered(AuthenticatedSenderStateChange):
sender: Address
message_identifier: MessageID
@dataclass(frozen=True)
class ReceiveProcessed(AuthenticatedSenderStateChange):
sender: Address
message_identifier: MessageID
@dataclass(frozen=True)
class ReceiveWithdrawRequest(AuthenticatedSenderStateChange):
"""A Withdraw message received."""
message_identifier: MessageID
canonical_identifier: CanonicalIdentifier
total_withdraw: WithdrawAmount
nonce: Nonce
expiration: BlockExpiration
signature: Signature
participant: Address
coop_settle: Optional[bool] = False
sender_metadata: Optional[AddressMetadata] = None
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ReceiveWithdrawConfirmation(AuthenticatedSenderStateChange):
"""A Withdraw message was received."""
message_identifier: MessageID
canonical_identifier: CanonicalIdentifier
total_withdraw: WithdrawAmount
nonce: Nonce
expiration: BlockExpiration
signature: Signature
participant: Address
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class ReceiveWithdrawExpired(AuthenticatedSenderStateChange):
"""A WithdrawExpired message was received."""
message_identifier: MessageID
canonical_identifier: CanonicalIdentifier
total_withdraw: WithdrawAmount
expiration: BlockExpiration
nonce: Nonce
participant: Address
@property
def channel_identifier(self) -> ChannelID:
return self.canonical_identifier.channel_identifier
@property
def token_network_address(self) -> TokenNetworkAddress:
return self.canonical_identifier.token_network_address
@dataclass(frozen=True)
class UpdateServicesAddressesStateChange(StateChange):
"""A `RegisteredService` contract event was received."""
service: Address
valid_till: int