forked from raiden-network/raiden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraiden_service.py
1124 lines (930 loc) · 39.4 KB
/
raiden_service.py
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# pylint: disable=too-many-lines
from future import standard_library
standard_library.install_aliases()
import os
import sys
import itertools
import pickle as pickle
import random
from collections import defaultdict
import filelock
import gevent
from gevent.event import AsyncResult
from coincurve import PrivateKey
from ethereum import slogging
from ethereum.utils import encode_hex
from raiden.constants import (
UINT64_MAX,
NETTINGCHANNEL_SETTLE_TIMEOUT_MIN,
NETTINGCHANNEL_SETTLE_TIMEOUT_MAX,
ROPSTEN_REGISTRY_ADDRESS,
)
from raiden.blockchain.events import (
get_relevant_proxies,
BlockchainEvents,
)
from raiden.event_handler import StateMachineEventHandler
from raiden.message_handler import RaidenMessageHandler
from raiden.tasks import (
AlarmTask,
)
from raiden.token_swap import GreenletTasksDispatcher
from raiden.transfer.architecture import StateManager
from raiden.transfer.state_change import Block
from raiden.transfer.state import (
RoutesState,
CHANNEL_STATE_SETTLED,
)
from raiden.transfer.mediated_transfer import (
initiator,
mediator,
)
from raiden.transfer.mediated_transfer import target as target_task
from raiden.transfer.mediated_transfer.state import (
lockedtransfer_from_message,
LockedTransferState,
)
from raiden.transfer.state_change import (
ActionTransferDirect,
)
from raiden.transfer.mediated_transfer.state_change import (
ActionInitInitiator,
ActionInitMediator,
ActionInitTarget,
)
from raiden.transfer.events import (
EventTransferSentSuccess,
)
from raiden.transfer.log import (
StateChangeLog,
StateChangeLogSQLiteBackend,
)
from raiden.channel import (
ChannelEndState,
ChannelExternalState,
)
from raiden.channel.netting_channel import (
ChannelSerialization,
)
from raiden.exceptions import InvalidAddress, AddressWithoutCode
from raiden.network.channelgraph import (
get_best_routes,
channel_to_routestate,
ChannelGraph,
ChannelDetails,
)
from raiden.messages import (
RevealSecret,
SignedMessage,
)
from raiden.transfer.state import MerkleTreeState
from raiden.transfer.merkle_tree import (
EMPTY_MERKLE_TREE,
compute_layers,
)
from raiden.network.protocol import (
RaidenProtocol,
)
from raiden.connection_manager import ConnectionManager
from raiden.utils import (
isaddress,
pex,
privatekey_to_address,
sha3,
)
log = slogging.get_logger(__name__) # pylint: disable=invalid-name
def create_default_identifier():
""" Generates a random identifier. """
return random.randint(0, UINT64_MAX)
def load_snapshot(serialization_file):
if os.path.exists(serialization_file):
with open(serialization_file, 'rb') as handler:
return pickle.load(handler)
def save_snapshot(serialization_file, raiden):
all_channels = [
ChannelSerialization(channel)
for network in raiden.token_to_channelgraph.values()
for channel in network.address_to_channel.values()
]
all_queues = list()
for key, queue in raiden.protocol.channel_queue.iteritems():
queue_data = {
'receiver_address': key[0],
'token_address': key[1],
'messages': queue.copy(),
}
all_queues.append(queue_data)
data = {
'channels': all_channels,
'queues': all_queues,
'receivedhashes_to_acks': raiden.protocol.receivedhashes_to_acks,
'nodeaddresses_to_nonces': raiden.protocol.nodeaddresses_to_nonces,
'transfers': raiden.identifier_to_statemanagers,
'registry_address': ROPSTEN_REGISTRY_ADDRESS,
}
with open(serialization_file, 'wb') as handler:
# __slots__ without __getstate__ require `-1`
pickle.dump(
data,
handler,
protocol=-1,
)
def endpoint_registry_exception_handler(greenlet):
try:
greenlet.get()
except Exception as e: # pylint: disable=broad-except
rpc_unreachable = (
e.args[0] == 'timeout when polling for transaction'
)
if rpc_unreachable:
log.fatal(
'Endpoint registry failed: %s. '
'Ethereum RPC API might be unreachable.',
repr(e),
)
else:
log.fatal('Endpoint registry failed: %s. ', repr(e))
sys.exit(1)
class RandomSecretGenerator(object): # pylint: disable=too-few-public-methods
def __next__(self): # pylint: disable=no-self-use
return os.urandom(32)
next = __next__
class RaidenService(object):
""" A Raiden node. """
# pylint: disable=too-many-instance-attributes,too-many-public-methods
def __init__(self, chain, default_registry, private_key_bin, transport, discovery, config):
if not isinstance(private_key_bin, bytes) or len(private_key_bin) != 32:
raise ValueError('invalid private_key')
invalid_timeout = (
config['settle_timeout'] < NETTINGCHANNEL_SETTLE_TIMEOUT_MIN or
config['settle_timeout'] > NETTINGCHANNEL_SETTLE_TIMEOUT_MAX
)
if invalid_timeout:
raise ValueError('settle_timeout must be in range [{}, {}]'.format(
NETTINGCHANNEL_SETTLE_TIMEOUT_MIN, NETTINGCHANNEL_SETTLE_TIMEOUT_MAX
))
self.token_to_channelgraph = dict()
self.tokens_to_connectionmanagers = dict()
self.manager_to_token = dict()
self.swapkey_to_tokenswap = dict()
self.swapkey_to_greenlettask = dict()
self.identifier_to_statemanagers = defaultdict(list)
self.identifier_to_results = defaultdict(list)
# This is a map from a hashlock to a list of channels, the same
# hashlock can be used in more than one token (for tokenswaps), a
# channel should be removed from this list only when the lock is
# released/withdrawn but not when the secret is registered.
self.token_to_hashlock_to_channels = defaultdict(lambda: defaultdict(list))
self.chain = chain
self.default_registry = default_registry
self.config = config
self.privkey = private_key_bin
self.address = privatekey_to_address(private_key_bin)
endpoint_registration_event = gevent.spawn(
discovery.register,
self.address,
config['external_ip'],
config['external_port'],
)
endpoint_registration_event.link_exception(endpoint_registry_exception_handler)
self.private_key = PrivateKey(private_key_bin)
self.pubkey = self.private_key.public_key.format(compressed=False)
self.protocol = RaidenProtocol(
transport,
discovery,
self,
config['protocol']['retry_interval'],
config['protocol']['retries_before_backoff'],
config['protocol']['nat_keepalive_retries'],
config['protocol']['nat_keepalive_timeout'],
config['protocol']['nat_invitation_timeout'],
)
# TODO: remove this cyclic dependency
transport.protocol = self.protocol
self.message_handler = RaidenMessageHandler(self)
self.state_machine_event_handler = StateMachineEventHandler(self)
self.blockchain_events = BlockchainEvents()
self.greenlet_task_dispatcher = GreenletTasksDispatcher()
self.on_message = self.message_handler.on_message
self.alarm = AlarmTask(chain)
self.shutdown_timeout = config['shutdown_timeout']
self._block_number = None
self.transaction_log = StateChangeLog(
storage_instance=StateChangeLogSQLiteBackend(
database_path=config['database_path']
)
)
if config['database_path'] != ':memory:':
self.database_dir = os.path.dirname(config['database_path'])
self.lock_file = os.path.join(self.database_dir, '.lock')
self.snapshot_dir = os.path.join(self.database_dir, 'snapshots')
self.serialization_file = os.path.join(self.snapshot_dir, 'data.pickle')
if not os.path.exists(self.snapshot_dir):
os.makedirs(self.snapshot_dir)
# Prevent concurrent acces to the same db
self.db_lock = filelock.FileLock(self.lock_file)
else:
self.database_dir = None
self.lock_file = None
self.snapshot_dir = None
self.serialization_file = None
self.db_lock = None
# If the endpoint registration fails the node will quit, this must
# finish before starting the protocol
endpoint_registration_event.join()
self.start()
def start(self):
""" Start the node. """
self.alarm.start()
# Prime the block number cache and set the callbacks
self._block_number = self.alarm.last_block_number
self.alarm.register_callback(self.poll_blockchain_events)
self.alarm.register_callback(self.set_block_number)
# Registry registration must start *after* the alarm task, this avoid
# corner cases were the registry is queried in block A, a new block B
# is mined, and the alarm starts polling at block C.
self.register_registry(self.default_registry.address)
# Restore from snapshot must come after registering the registry as we
# need to know the registered tokens to populate `token_to_channelgraph`
if self.database_dir is not None:
self.db_lock.acquire(timeout=0)
assert self.db_lock.is_locked
self.restore_from_snapshots()
# Start the protocol after the registry is queried to avoid warning
# about unknown channels.
self.protocol.start()
# Health check needs the protocol layer
self.start_neighbours_healthcheck()
def start_neighbours_healthcheck(self):
for graph in self.token_to_channelgraph.values():
for neighbour in graph.get_neighbours():
if neighbour != ConnectionManager.BOOTSTRAP_ADDR:
self.start_health_check_for(neighbour)
def stop(self):
""" Stop the node. """
self.alarm.stop_async()
self.protocol.stop_and_wait()
wait_for = [self.alarm]
wait_for.extend(self.protocol.greenlets)
wait_for.extend(self.greenlet_task_dispatcher.stop())
# We need a timeout to prevent an endless loop from trying to
# contact the disconnected client
gevent.wait(wait_for, timeout=self.shutdown_timeout)
# Filters must be uninstalled after the alarm task has stopped. Since
# the events are polled by a alarm task callback, if the filters are
# uninstalled before the alarm task is fully stopped the callback
# `poll_blockchain_events` will fail.
#
# We need a timeout to prevent an endless loop from trying to
# contact the disconnected client
try:
with gevent.Timeout(self.shutdown_timeout):
self.blockchain_events.uninstall_all_event_listeners()
except gevent.timeout.Timeout:
pass
# save the state after all tasks are done
if self.serialization_file:
save_snapshot(self.serialization_file, self)
if self.db_lock is not None:
self.db_lock.release()
def __repr__(self):
return '<{} {}>'.format(self.__class__.__name__, pex(self.address))
def restore_from_snapshots(self):
data = load_snapshot(self.serialization_file)
data_exists_and_is_recent = (
data is not None and
'registry_address' in data and
data['registry_address'] == ROPSTEN_REGISTRY_ADDRESS
)
if data_exists_and_is_recent:
first_channel = True
for channel in data['channels']:
try:
self.restore_channel(channel)
first_channel = False
except AddressWithoutCode as e:
log.warn(
'Channel without code while restoring. Must have been '
'already settled while we were offline.',
error=str(e)
)
except AttributeError as e:
if first_channel:
log.warn(
'AttributeError during channel restoring. If code has changed'
' then this is fine. If not then please report a bug.',
error=str(e)
)
break
else:
raise
for restored_queue in data['queues']:
self.restore_queue(restored_queue)
self.protocol.receivedhashes_to_acks = data['receivedhashes_to_acks']
self.protocol.nodeaddresses_to_nonces = data['nodeaddresses_to_nonces']
self.restore_transfer_states(data['transfers'])
def set_block_number(self, block_number):
state_change = Block(block_number)
self.state_machine_event_handler.log_and_dispatch_to_all_tasks(state_change)
for graph in self.token_to_channelgraph.itervalues():
for channel in graph.address_to_channel.itervalues():
channel.state_transition(state_change)
# To avoid races, only update the internal cache after all the state
# tasks have been updated.
self._block_number = block_number
def set_node_network_state(self, node_address, network_state):
for graph in self.token_to_channelgraph.itervalues():
channel = graph.partneraddress_to_channel.get(node_address)
if channel:
channel.network_state = network_state
def start_health_check_for(self, node_address):
self.protocol.start_health_check(node_address)
def get_block_number(self):
return self._block_number
def poll_blockchain_events(self, current_block=None):
# pylint: disable=unused-argument
on_statechange = self.state_machine_event_handler.on_blockchain_statechange
for state_change in self.blockchain_events.poll_state_change(self._block_number):
on_statechange(state_change)
def find_channel_by_address(self, netting_channel_address_bin):
for graph in self.token_to_channelgraph.itervalues():
channel = graph.address_to_channel.get(netting_channel_address_bin)
if channel is not None:
return channel
raise ValueError('unknown channel {}'.format(encode_hex(netting_channel_address_bin)))
def sign(self, message):
""" Sign message inplace. """
if not isinstance(message, SignedMessage):
raise ValueError('{} is not signable.'.format(repr(message)))
message.sign(self.private_key, self.address)
def send_async(self, recipient, message):
""" Send `message` to `recipient` using the raiden protocol.
The protocol will take care of resending the message on a given
interval until an Acknowledgment is received or a given number of
tries.
"""
if not isaddress(recipient):
raise ValueError('recipient is not a valid address.')
if recipient == self.address:
raise ValueError('programming error, sending message to itself')
return self.protocol.send_async(recipient, message)
def send_and_wait(self, recipient, message, timeout):
""" Send `message` to `recipient` and wait for the response or `timeout`.
Args:
recipient (address): The address of the node that will receive the
message.
message: The transfer message.
timeout (float): How long should we wait for a response from `recipient`.
Returns:
None: If the wait timed out
object: The result from the event
"""
if not isaddress(recipient):
raise ValueError('recipient is not a valid address.')
self.protocol.send_and_wait(recipient, message, timeout)
def register_secret(self, secret):
""" Register the secret with any channel that has a hashlock on it.
This must search through all channels registered for a given hashlock
and ignoring the tokens. Useful for refund transfer, split transfer,
and token swaps.
Raises:
TypeError: If secret is unicode data.
"""
if isinstance(secret, unicode):
raise TypeError('secret must be binary')
hashlock = sha3(secret)
revealsecret_message = RevealSecret(secret)
self.sign(revealsecret_message)
for hash_channel in self.token_to_hashlock_to_channels.itervalues():
for channel in hash_channel[hashlock]:
channel.register_secret(secret)
# The protocol ignores duplicated messages.
self.send_async(
channel.partner_state.address,
revealsecret_message,
)
def register_channel_for_hashlock(self, token_address, channel, hashlock):
channels_registered = self.token_to_hashlock_to_channels[token_address][hashlock]
if channel not in channels_registered:
channels_registered.append(channel)
def handle_secret( # pylint: disable=too-many-arguments
self,
identifier,
token_address,
secret,
partner_secret_message,
hashlock):
""" Unlock/Witdraws locks, register the secret, and send Secret
messages as necessary.
This function will:
- Unlock the locks created by this node and send a Secret message to
the corresponding partner so that she can withdraw the token.
- Withdraw the lock from sender.
- Register the secret for the locks received and reveal the secret
to the senders
Note:
The channel needs to be registered with
`raiden.register_channel_for_hashlock`.
"""
# handling the secret needs to:
# - unlock the token for all `forward_channel` (the current one
# and the ones that failed with a refund)
# - send a message to each of the forward nodes allowing them
# to withdraw the token
# - register the secret for the `originating_channel` so that a
# proof can be made, if necessary
# - reveal the secret to the `sender` node (otherwise we
# cannot withdraw the token)
channels_list = self.token_to_hashlock_to_channels[token_address][hashlock]
channels_to_remove = list()
revealsecret_message = RevealSecret(secret)
self.sign(revealsecret_message)
messages_to_send = []
for channel in channels_list:
# unlock a pending lock
if channel.our_state.is_known(hashlock):
secret = channel.create_secret(identifier, secret)
self.sign(secret)
channel.register_transfer(
self.get_block_number(),
secret,
)
messages_to_send.append((
channel.partner_state.address,
secret,
))
channels_to_remove.append(channel)
# withdraw a pending lock
elif channel.partner_state.is_known(hashlock):
if partner_secret_message:
is_balance_proof = (
partner_secret_message.sender == channel.partner_state.address and
partner_secret_message.channel == channel.channel_address
)
if is_balance_proof:
channel.register_transfer(
self.get_block_number(),
partner_secret_message,
)
channels_to_remove.append(channel)
else:
channel.register_secret(secret)
messages_to_send.append((
channel.partner_state.address,
revealsecret_message,
))
else:
channel.register_secret(secret)
messages_to_send.append((
channel.partner_state.address,
revealsecret_message,
))
else:
log.error(
'Channel is registered for a given lock but the lock is not contained in it.'
)
for channel in channels_to_remove:
channels_list.remove(channel)
if not channels_list:
del self.token_to_hashlock_to_channels[token_address][hashlock]
# send the messages last to avoid races
for recipient, message in messages_to_send:
self.send_async(
recipient,
message,
)
def get_channel_details(self, token_address, netting_channel):
channel_details = netting_channel.detail()
our_state = ChannelEndState(
channel_details['our_address'],
channel_details['our_balance'],
None,
EMPTY_MERKLE_TREE,
)
partner_state = ChannelEndState(
channel_details['partner_address'],
channel_details['partner_balance'],
None,
EMPTY_MERKLE_TREE,
)
def register_channel_for_hashlock(channel, hashlock):
self.register_channel_for_hashlock(
token_address,
channel,
hashlock,
)
channel_address = netting_channel.address
reveal_timeout = self.config['reveal_timeout']
settle_timeout = channel_details['settle_timeout']
external_state = ChannelExternalState(
register_channel_for_hashlock,
netting_channel,
)
channel_detail = ChannelDetails(
channel_address,
our_state,
partner_state,
external_state,
reveal_timeout,
settle_timeout,
)
return channel_detail
def restore_channel(self, serialized_channel):
token_address = serialized_channel.token_address
netting_channel = self.chain.netting_channel(
serialized_channel.channel_address,
)
# restoring balances from the blockchain since the serialized
# value could be falling behind.
channel_details = netting_channel.detail()
# our_address is checked by detail
assert channel_details['partner_address'] == serialized_channel.partner_address
if serialized_channel.our_leaves:
our_layers = compute_layers(serialized_channel.our_leaves)
our_tree = MerkleTreeState(our_layers)
else:
our_tree = EMPTY_MERKLE_TREE
our_state = ChannelEndState(
channel_details['our_address'],
channel_details['our_balance'],
serialized_channel.our_balance_proof,
our_tree,
)
if serialized_channel.partner_leaves:
partner_layers = compute_layers(serialized_channel.partner_leaves)
partner_tree = MerkleTreeState(partner_layers)
else:
partner_tree = EMPTY_MERKLE_TREE
partner_state = ChannelEndState(
channel_details['partner_address'],
channel_details['partner_balance'],
serialized_channel.partner_balance_proof,
partner_tree,
)
def register_channel_for_hashlock(channel, hashlock):
self.register_channel_for_hashlock(
token_address,
channel,
hashlock,
)
external_state = ChannelExternalState(
register_channel_for_hashlock,
netting_channel,
)
details = ChannelDetails(
serialized_channel.channel_address,
our_state,
partner_state,
external_state,
serialized_channel.reveal_timeout,
channel_details['settle_timeout'],
)
graph = self.token_to_channelgraph[token_address]
graph.add_channel(details)
channel = graph.address_to_channel.get(
serialized_channel.channel_address,
)
channel.our_state.balance_proof = serialized_channel.our_balance_proof
channel.partner_state.balance_proof = serialized_channel.partner_balance_proof
def restore_queue(self, serialized_queue):
receiver_address = serialized_queue['receiver_address']
token_address = serialized_queue['token_address']
queue = self.protocol.get_channel_queue(
receiver_address,
token_address,
)
for messagedata in serialized_queue['messages']:
queue.put(messagedata)
def restore_transfer_states(self, transfer_states):
self.identifier_to_statemanagers = transfer_states
def register_registry(self, registry_address):
proxies = get_relevant_proxies(
self.chain,
self.address,
registry_address,
)
# Install the filters first to avoid missing changes, as a consequence
# some events might be applied twice.
self.blockchain_events.add_proxies_listeners(proxies)
for manager in proxies.channel_managers:
token_address = manager.token_address()
manager_address = manager.address
channels_detail = list()
netting_channels = proxies.channelmanager_nettingchannels[manager_address]
for channel in netting_channels:
detail = self.get_channel_details(token_address, channel)
channels_detail.append(detail)
edge_list = manager.channels_addresses()
graph = ChannelGraph(
self.address,
manager_address,
token_address,
edge_list,
channels_detail,
)
self.manager_to_token[manager_address] = token_address
self.token_to_channelgraph[token_address] = graph
self.tokens_to_connectionmanagers[token_address] = ConnectionManager(
self,
token_address,
graph
)
def channel_manager_is_registered(self, manager_address):
return manager_address in self.manager_to_token
def register_channel_manager(self, manager_address):
manager = self.default_registry.manager(manager_address)
netting_channels = [
self.chain.netting_channel(channel_address)
for channel_address in manager.channels_by_participant(self.address)
]
# Install the filters first to avoid missing changes, as a consequence
# some events might be applied twice.
self.blockchain_events.add_channel_manager_listener(manager)
for channel in netting_channels:
self.blockchain_events.add_netting_channel_listener(channel)
token_address = manager.token_address()
edge_list = manager.channels_addresses()
channels_detail = [
self.get_channel_details(token_address, channel)
for channel in netting_channels
]
graph = ChannelGraph(
self.address,
manager_address,
token_address,
edge_list,
channels_detail,
)
self.manager_to_token[manager_address] = token_address
self.token_to_channelgraph[token_address] = graph
self.tokens_to_connectionmanagers[token_address] = ConnectionManager(
self,
token_address,
graph
)
def register_netting_channel(self, token_address, channel_address):
netting_channel = self.chain.netting_channel(channel_address)
self.blockchain_events.add_netting_channel_listener(netting_channel)
detail = self.get_channel_details(token_address, netting_channel)
graph = self.token_to_channelgraph[token_address]
graph.add_channel(detail)
def connection_manager_for_token(self, token_address):
if not isaddress(token_address):
raise InvalidAddress('token address is not valid.')
if token_address in self.tokens_to_connectionmanagers.keys():
manager = self.tokens_to_connectionmanagers[token_address]
else:
raise InvalidAddress('token is not registered.')
return manager
def leave_all_token_networks_async(self):
token_addresses = self.token_to_channelgraph.keys()
leave_results = []
for token_address in token_addresses:
try:
connection_manager = self.connection_manager_for_token(token_address)
except InvalidAddress:
pass
leave_results.append(connection_manager.leave_async())
combined_result = AsyncResult()
gevent.spawn(gevent.wait, leave_results).link(combined_result)
return combined_result
def close_and_settle(self):
log.info('raiden will close and settle all channels now')
connection_managers = [
self.connection_manager_for_token(token_address) for
token_address in self.token_to_channelgraph
]
def blocks_to_wait():
return max(
connection_manager.min_settle_blocks
for connection_manager in connection_managers
)
all_channels = list(
itertools.chain.from_iterable(
[connection_manager.open_channels for connection_manager in connection_managers]
)
)
leaving_greenlet = self.leave_all_token_networks_async()
# using the un-cached block number here
last_block = self.chain.block_number()
earliest_settlement = last_block + blocks_to_wait()
# TODO: estimate and set a `timeout` parameter in seconds
# based on connection_manager.min_settle_blocks and an average
# blocktime from the past
current_block = last_block
avg_block_time = self.chain.estimate_blocktime()
wait_blocks_left = blocks_to_wait()
while current_block < earliest_settlement:
gevent.sleep(self.alarm.wait_time)
last_block = self.chain.block_number()
if last_block != current_block:
current_block = last_block
avg_block_time = self.chain.estimate_blocktime()
wait_blocks_left = blocks_to_wait()
not_settled = sum(
1 for channel in all_channels
if not channel.state == CHANNEL_STATE_SETTLED
)
if not_settled == 0:
log.debug('nothing left to settle')
break
log.info(
'waiting at least %s more blocks (~%s sec) for settlement'
'(%s channels not yet settled)' % (
wait_blocks_left,
wait_blocks_left * avg_block_time,
not_settled
)
)
leaving_greenlet.wait(timeout=blocks_to_wait() * self.chain.estimate_blocktime() * 1.5)
if any(channel.state != CHANNEL_STATE_SETTLED for channel in all_channels):
log.error(
'Some channels were not settled!',
channels=[
pex(channel.channel_address) for channel in all_channels
if channel.state != CHANNEL_STATE_SETTLED
]
)
def mediated_transfer_async(self, token_address, amount, target, identifier):
""" Transfer `amount` between this node and `target`.
This method will start an asyncronous transfer, the transfer might fail
or succeed depending on a couple of factors:
- Existence of a path that can be used, through the usage of direct
or intermediary channels.
- Network speed, making the transfer sufficiently fast so it doesn't
expire.
"""
async_result = self.start_mediated_transfer(
token_address,
amount,
identifier,
target,
)
return async_result
def direct_transfer_async(self, token_address, amount, target, identifier):
""" Do a direct tranfer with target.
Direct transfers are non cancellable and non expirable, since these
transfers are a signed balance proof with the transferred amount
incremented.
Because the transfer is non cancellable, there is a level of trust with
the target. After the message is sent the target is effectively paid
and then it is not possible to revert.
The async result will be set to False iff there is no direct channel
with the target or the payer does not have balance to complete the
transfer, otherwise because the transfer is non expirable the async
result *will never be set to False* and if the message is sent it will
hang until the target node acknowledge the message.
This transfer should be used as an optimization, since only two packets
are required to complete the transfer (from the payer's perspective),
whereas the mediated transfer requires 6 messages.
"""
graph = self.token_to_channelgraph[token_address]
direct_channel = graph.partneraddress_to_channel.get(target)
direct_channel_with_capacity = (
direct_channel and
direct_channel.can_transfer and
amount <= direct_channel.distributable
)
if direct_channel_with_capacity:
direct_transfer = direct_channel.create_directtransfer(amount, identifier)
self.sign(direct_transfer)
direct_channel.register_transfer(
self.get_block_number(),
direct_transfer,
)
direct_transfer_state_change = ActionTransferDirect(
identifier,
amount,
token_address,
direct_channel.partner_state.address,
)
# TODO: add the transfer sent event
state_change_id = self.transaction_log.log(direct_transfer_state_change)
# TODO: This should be set once the direct transfer is acknowledged
transfer_success = EventTransferSentSuccess(
identifier,
amount,
target,
)
self.transaction_log.log_events(
state_change_id,
[transfer_success],
self.get_block_number()
)
async_result = self.protocol.send_async(
direct_channel.partner_state.address,
direct_transfer,
)
else:
async_result = AsyncResult()
async_result.set(False)
return async_result
def start_mediated_transfer(self, token_address, amount, identifier, target):
# pylint: disable=too-many-locals
async_result = AsyncResult()
graph = self.token_to_channelgraph[token_address]
available_routes = get_best_routes(
graph,
self.protocol.nodeaddresses_networkstatuses,