-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpynanocoin.py
1166 lines (963 loc) · 40.1 KB
/
pynanocoin.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
from __future__ import annotations
import ipaddress
import os
import random
import socket
from typing import Iterable
import time
from hashlib import blake2b
import binascii
import base64
import dns.resolver
import ed25519_blake2
import ed25519_blake2b
import git
from typing import Optional, Union
from _logger import get_logger, VERBOSE
import pow_block
import acctools
from exceptions import *
from block import *
from net import *
from common import *
from peer import Peer, ip_addr
logger = get_logger()
# return a list of ipv4 mapped ipv6 strings
def get_all_dns_addresses(addr: str) -> list[str]:
result = dns.resolver.resolve(addr, 'A')
return ['::ffff:' + x.to_text() for x in result]
class message_type_enum:
invalid = 0x0
not_a_block = 0x1
keepalive = 0x2
publish = 0x3
confirm_req = 0x4
confirm_ack = 0x5
bulk_pull = 0x6
bulk_push = 0x7
frontier_req = 0x8
# deleted 0x9
node_id_handshake = 0x0a
bulk_pull_account = 0x0b
telemetry_req = 0x0c
telemetry_ack = 0x0d
asc_pull_req = 0x0e
asc_pull_ack = 0x0f
max = 0x0f
def message_type_enum_to_str(msg_type: int):
return next(name for name, value in vars(message_type_enum).items() if value == msg_type)
class network_id:
def __init__(self, rawbyte: int):
self.parse_header(int(rawbyte))
def parse_header(self, rawbyte: bytes) -> None:
if not (rawbyte in [ord('X'), ord('B'), ord('C')]):
raise ParseErrorBadNetworkId()
self.id = rawbyte
def __str__(self):
return chr(self.id)
def __eq__(self, other):
if not isinstance(other, network_id):
return False
elif self.id != other.id:
return False
return True
class message_type:
def __init__(self, num: int) -> None:
if not (num in range(0, message_type_enum.max + 1)):
raise ParseErrorBadMessageType()
self.type = num
def __str__(self):
return '%s(%s)' % (str(self.type), message_type_enum_to_str(self.type))
def __eq__(self, other):
if not isinstance(other, message_type):
return False
return self.type == other.type
class message_header:
def __init__(self, net_id: network_id, versions: list[int], msg_type: message_type, ext: int):
self.ext = ext
self.net_id = net_id
self.ver_max = versions[0]
self.ver_using = versions[1]
self.ver_min = versions[2]
self.msg_type = msg_type
assert isinstance(self.msg_type, message_type)
def serialise_header(self) -> bytes:
header = b""
header += ord('R').to_bytes(1, "big")
header += ord(str(self.net_id)).to_bytes(1, "big")
header += self.ver_max.to_bytes(1, "big")
header += self.ver_using.to_bytes(1, "big")
header += self.ver_min.to_bytes(1, "big")
header += self.msg_type.type.to_bytes(1, "big")
header += self.ext.to_bytes(2, "little")
return header
def is_query(self) -> bool:
return self.ext& 1
def is_response(self) -> bool:
return self.ext& 2
def set_is_query(self, bool: bool) -> None:
QUERY_MASK = 0x0001
self.ext = self.ext & 0xfffe
if bool:
self.ext = self.ext | QUERY_MASK
def set_is_response(self, bool: bool) -> None:
RESPONSE_MASK = 0x0002
self.ext = self.ext & 0xfffd
if bool:
self.ext = self.ext | RESPONSE_MASK
def count_v2_get(self):
count_v2_mask_left = 0xf000
count_v2_mask_right = 0x00f0
left = (self.ext & count_v2_mask_left) >> 12
right = (self.ext & count_v2_mask_right) >> 4
result = ((left << 4) | right) & 0xff
print("count_v2_get -> %s" % result)
return result
def count_get(self) -> int:
assert self.msg_type == message_type(message_type_enum.confirm_ack) or self.msg_type == message_type(message_type_enum.confirm_req)
if self.ext & 1 == 1:
print('v2 confirm_ack')
return self.count_v2_get()
else:
COUNT_MASK = 0xf000
return (self.ext & COUNT_MASK) >> 12
def block_type(self) -> int:
BLOCK_TYPE_MASK = 0x0f00
return (self.ext & BLOCK_TYPE_MASK) >> 8
def set_block_type(self, block_type: int) -> None:
assert(isinstance(block_type, int))
block_type = block_type << 8
self.ext = self.ext & 0xf0ff
self.ext = self.ext | block_type
def set_item_count(self, count: int) -> None:
assert(isinstance(count, int))
count = count << 12
self.ext = self.ext & 0x0fff
self.ext = self.ext | count
@classmethod
def parse_header(cls, data: bytes):
assert(len(data) == 8)
if data[0] != ord('R'):
raise ParseErrorBadMagicNumber()
net_id = network_id(data[1])
versions = [data[2], data[3], data[4]]
msg_type = message_type(data[5])
ext = int.from_bytes(data[6:], "little")
return message_header(net_id, versions, msg_type, ext)
def telemetry_ack_size(self) -> int:
telemetry_size_mask = 0x3ff
return self.ext & telemetry_size_mask
@classmethod
def from_json(self, json_hdr: dict):
return message_header(network_id(json_hdr['net_id']),
[json_hdr['ver_max'], json_hdr['ver_using'], json_hdr["ver_min"]],
message_type(json_hdr['msg_type']), json_hdr['ext'])
def confirm_req_size(self) -> int:
if self.block_type() == message_type_enum.not_a_block:
size = 64 * self.count_get()
else:
assert(self.count_get() == 1)
size = block_length_by_type.get(self.block_type())
return size
def confirm_ack_size(self) -> int:
size = 104
if self.block_type() == message_type_enum.not_a_block:
size += self.count_get() * 32
else:
assert(self.count_get() == 1)
size += block_length_by_type.get(self.block_type())
return size
def payload_length_bytes(self) -> Optional[int]:
if self.msg_type == message_type(message_type_enum.bulk_pull):
return None
if self.msg_type == message_type(message_type_enum.bulk_push):
return 0
elif self.msg_type == message_type(message_type_enum.telemetry_req):
return 0
elif self.msg_type == message_type(message_type_enum.frontier_req):
return 32 + 4 + 4
elif self.msg_type == message_type(message_type_enum.bulk_pull_account):
return 32 + 16 + 1
elif self.msg_type == message_type(message_type_enum.keepalive):
return 8 * (16 + 2)
elif self.msg_type == message_type(message_type_enum.publish):
return block_length_by_type(self.block_type())
elif self.msg_type == message_type(message_type_enum.confirm_ack):
return self.confirm_ack_size()
elif self.msg_type == message_type(message_type_enum.confirm_req):
return self.confirm_req_size()
elif self.msg_type == message_type(message_type_enum.node_id_handshake):
return node_id_handshake_size(self.is_query(), self.is_response())
elif self.msg_type == message_type(message_type_enum.telemetry_ack):
return self.telemetry_ack_size()
elif self.msg_type == message_type(message_type_enum.asc_pull_req):
return 1 + 8 + self.ext
elif self.msg_type == message_type(message_type_enum.asc_pull_ack):
return 1 + 8 + self.ext
else:
logger.debug(f"Unknown message type: {self.msg_type}")
return None
def __eq__(self, other):
if str(self) == str(other):
return True
def __str__(self):
str = "NetID: %s, " % self.net_id
str += "VerMaxUsingMin: %s/%s/%s, " % (self.ver_max, self.ver_using, self.ver_min)
str += "MsgType: %s, " % self.msg_type
str += "Extensions: %s" % hexlify(self.ext.to_bytes(2, "big"))
return str
class message_bulk_pull:
def __init__(self, ctx, start: str, end: str = None, count: int = None, ascending: bool = False):
self.hdr = message_header(ctx["net_id"], [18, 18, 18], message_type(message_type_enum.bulk_pull), 0)
self.count = count
if count is not None:
self.hdr.ext |= 1
if ascending:
self.hdr.ext |= 2
self.start = start
if end is not None:
self.end = end
else:
self.end = (0).to_bytes(32, "big")
def serialise(self) -> bytes:
data = self.hdr.serialise_header()
data += self.start
data += self.end
if self.count is not None:
data += self.generate_extended_params()
return data
@classmethod
def parse(cls, hdr: message_header, data: bytes):
start = data[0:32]
end = data[32:64]
bp = message_bulk_pull(hdr, start, end)
if hdr.ext == 1:
count = data[66:]
bp = message_bulk_pull(hdr, start, end, count=count)
return bp
def generate_extended_params(self) -> bytes:
assert(self.count is not None)
data = (0).to_bytes(1, "big")
data += self.count.to_bytes(4, "little")
data += (0).to_bytes(3, "big")
return data
class message_keepalive:
def __init__(self, hdr: message_header, peers: list[Peer] = None):
self.header = hdr
self.header.msg_type = message_type(message_type_enum.keepalive)
if peers is None:
self.peers = []
for i in range(0, 8):
self.peers.append(Peer())
else:
assert len(peers) == 8
self.peers = peers
def serialise(self) -> bytes:
assert len(self.peers) == 8
data = self.header.serialise_header()
for p in self.peers:
data += p.serialise()
return data
def __str__(self):
string = '%s\n' % self.header
for p in self.peers:
string += "%s\n" % str(p)
return string
def __eq__(self, other):
if str(self) == str(other):
return True
return False
@classmethod
def parse_payload(cls, hdr: message_header, rawdata: bytes):
assert(len(rawdata) % 18 == 0)
no_of_peers = int(len(rawdata) / 18)
start_index = 0
end_index = 18
peers_list = []
for i in range(0, no_of_peers):
p = Peer.parse_peer(rawdata[start_index:end_index])
p.last_seen = int(time.time())
peers_list.append(p)
start_index = end_index
end_index += 18
return message_keepalive(hdr, peers_list)
@classmethod
def make_packet(cls, peers: Iterable[Peer], net_id, version: int) -> bytes:
peers = list(peers)
for i in range(len(peers), 8):
peers.append(Peer())
hdr = message_header(net_id, [version, version, version], message_type(message_type_enum.keepalive), 0)
keepalive = message_keepalive(hdr, list(peers))
return keepalive.serialise()
class bulk_push:
def __init__(self, hdr: message_header, blocks: list):
self.hdr = hdr
self.blocks = blocks
def serialise(self) -> bytes:
data = b''
data += self.hdr.serialise_header()
for b in self.blocks:
data += b.serialise(True)
data += (1).to_bytes(1, 'big')
return data
@classmethod
def parse(cls, hdr, data):
blocks = []
ptr = 1
block_type = data[0]
# TODO: this should move into the Block class
while block_type != block_type_enum.not_a_block:
assert block_type in range(1, 7)
block = None
if block_type == 2:
block = block_send.parse(data[ptr: ptr + block_length_by_type(block_type)])
elif block_type == 3:
block = block_receive.parse(data[ptr: ptr + block_length_by_type(block_type)])
elif block_type == 4:
block = block_open.parse(data[ptr: ptr + block_length_by_type(block_type)])
elif block_type == 5:
block = block_change.parse(data[ptr: ptr + block_length_by_type(block_type)])
elif block_type == 6:
block = block_state.parse(data[ptr: ptr + block_length_by_type(block_type)])
elif block_type == 1:
break
ptr += block_length_by_type(block_type)
blocks.append(block)
block_type = data[ptr]
ptr += 1
return bulk_push(hdr, blocks)
def __eq__(self, other):
if not isinstance(other, bulk_push):
return False
for b in self.blocks:
if b not in other.blocks:
return False
return True
def __str__(self):
string = str(self.hdr) + '\n'
string += 'Blocks being pushed:\n'
for b in self.blocks:
string += str(b) + '\n'
return string
class block_manager:
def __init__(self, ctx: dict, workdir: str, gitrepo: git.Repo):
self.ctx = ctx
self.accounts = []
self.processed_blocks = []
self.unprocessed_blocks = set()
self.trust_open_blocks = True
self.workdir = workdir
self.gitrepo = gitrepo
# create genesis account and block
open_block = ctx["genesis_block"]
open_block.ancillary["balance"] = 0xffffffffffffffffffffffffffffffff
self.accounts.append(nano_account(self, open_block))
#TODO: Make a method which can get the next undiscovered account
def next_acc_iter(self):
for a in self.accounts:
for block_hash, b in a.blocks.items():
if not (isinstance(b, block_send) or isinstance(b, block_state)):
continue
elif isinstance(b, block_send):
if not self.account_exists(b.destination):
yield b.destination
elif isinstance(b, block_state):
if b.link == b'\x00' * 32:
continue
if not self.account_exists(b.link):
yield b.link
yield None
def process_one(self, block) -> bool:
success = False
if isinstance(block, block_open):
success = self.process_block_open(block)
elif isinstance(block, block_send):
success = self.process_block_send(block)
elif isinstance(block, block_change):
success = self.process_block_change(block)
elif isinstance(block, block_receive):
self.process_block_receive(block)
elif isinstance(block, block_state):
success = self.process_block_state(block)
else:
success = self.process_block(block)
return success
def process(self, block) -> bool:
success = self.process_one(block)
if success:
self.processed_blocks.append(block)
self.process_unprocessed_blocks()
return success
def process_block_state(self, block) -> bool:
#print('process_block_state %s' % hexlify(block.hash()))
# check block
# if not valid_block(block):
# return False
# is it open block and do we trust all open blocks
if block.previous == b'\x00' * 32 and self.trust_open_blocks:
# check if account exists
if self.account_exists(block.get_account()):
print('state open block (%s) for already opened account %s' %
(hexlify(block.hash()), acctools.to_account_addr(block.account)))
return True
# create the account
acc = nano_account(self, block)
self.accounts.append(acc)
print('Opened new account\n%s' % acc)
return True
# find the previous block
prevblk, acc = self.find_ledger_block_by_hash(block.previous)
if prevblk is None:
#print('cannot find previous block (%s) of state block (%s)' %
# (hexlify(block.previous), hexlify(block.hash())))
self.unprocessed_blocks.add(block)
return False
# check if it is an epoch block
if block.link.startswith(b'epoch') and prevblk.get_balance() == block.get_balance():
print('Epoch block')
print(block)
acc.add_block(block, previous=prevblk.hash())
return True
def process_block_open(self, block) -> bool:
# check block
# FIXME: this breaks with test network genesis open block
#if not valid_block(block):
# print('Invalid block with hash %s' % hexlify(block.hash()))
# return False
# check if account exists
if self.account_exists(block.get_account()):
print('open block (%s) for already opened account %s' %
(hexlify(block.hash()), acctools.to_account_addr(block.account)))
return True
# do we trust all open blocks?
if self.trust_open_blocks:
# with an open block, we do not know the balance and there is no way
# to know it without pulling an indeterminate number of blocks/accounts
# so setting it to zero for now since we are focused on forks when trusting open blocks
block.ancillary["balance"] = 0
# create the account
acc = nano_account(self, block)
self.accounts.append(acc)
print('Opened new account\n%s' % acc)
return True
# find the associated send block
srcblk, _ = self.find_ledger_block_by_hash(block.source)
if srcblk is None:
print('cannot find source block (%s) of open block (%s)' %
(hexlify(block.source), hexlify(block.hash())))
self.unprocessed_blocks.add(block)
return False
# we have a source block, set the opening balance
block.ancillary["balance"] = srcblk.ancillary["amount_sent"]
# create the account
acc = nano_account(self, block)
self.accounts.append(acc)
print('Opened new account\n%s' % acc)
return True
def process_block_send(self, block) -> bool:
assert block.previous
# check block
# if not valid_block(block):
# return False
# find the previous block
prevblk, acc = self.find_ledger_block_by_hash(block.previous)
if prevblk is None:
print('cannot find previous block (%s) of send block (%s)' %
(hexlify(block.previous), hexlify(block.hash())))
self.unprocessed_blocks.add(block)
return False
# we have a previous block, set the amount_sent and account
block.ancillary["amount_sent"] = prevblk.get_balance() - block.balance
block.ancillary["account"] = prevblk.get_account()
# add block to the account
acc.add_block(block, previous=prevblk.hash())
return True
def process_block_receive(self, block) -> bool:
assert(isinstance(block, block_receive))
prevblk, acc = self.find_ledger_block_by_hash(block.previous)
if prevblk is None:
print('cannot find previous block (%s) of receive block (%s)' %
(hexlify(block.previous), hexlify(block.hash())))
self.unprocessed_blocks.add(block)
return False
scrblk, _ = self.find_ledger_block_by_hash(block.source)
if scrblk is None:
print("cannot find source block (%s) of reveive block (%s)" %
(hexlify(block.source), hexlify(block.hash())))
self.unprocessed_blocks.add(block)
return False
block.ancillary["balance"] = prevblk.get_balance()
block.ancillary["balance"] += scrblk.ancillary["amount_sent"]
block.ancillary["account"] = prevblk.get_account()
acc.add_block(block, previous=prevblk.hash())
return True
def process_block_change(self, block) -> bool:
assert block.previous
# check block
# if not valid_block(block):
# return False
# find the previous block
prevblk, acc = self.find_ledger_block_by_hash(block.previous)
if prevblk is None:
print('cannot find previous block (%s) of send block (%s)' %
(hexlify(block.previous), hexlify(block.hash())))
self.unprocessed_blocks.add(block)
return False
# we have a previous block, set the balance and account
block.ancillary["account"] = prevblk.get_account()
block.ancillary["balance"] = prevblk.get_balance()
# add block to the account
acc.add_block(block, previous=prevblk.hash())
return True
# find a block by hash that is part of the local ledger
def find_ledger_block_by_hash(self, hsh: bytes) -> tuple:
for acc in self.accounts:
blk = acc.find_block_by_hash(hsh)
if blk: return blk, acc
return None, None
def process_block(self, block) -> bool:
assert not isinstance(block, block_send)
print('process block ', hexlify(block.hash()))
print(' prev:', hexlify(block.previous))
account_pk = self.find_blocks_account(block)
if account_pk is not None:
block.ancillary["account"] = account_pk
if not valid_block(block):
return False
self.find_prev_block(block).ancillary["next"] = block.hash()
else:
self.unprocessed_blocks.add(block)
print('process block no account_pk')
return False
n_account = self.find_nano_account(account_pk)
if n_account is None:
self.unprocessed_blocks.add(block)
print('process block no account')
return False
if isinstance(block, block_send):
amount = self.find_amount_sent(block)
if amount is not None:
block.ancillary["amount_sent"] = amount
else:
self.unprocessed_blocks.add(block)
print(block)
print('process block no amount')
return False
if block.get_balance() is None:
balance = self.find_balance(block)
if balance is not None:
block.ancillary["balance"] = balance
else:
self.unprocessed_blocks.add(block)
print('process block no balance')
return False
n_account.add_block(block)
print('process block done')
return True
def find_amount_sent(self, block) -> int or None:
for b in self.processed_blocks:
if b.hash() == block.get_previous():
if b.get_balance() is not None:
before = b.get_balance()
after = block.get_balance()
amount = before - after
return amount
else:
return None
def find_balance(self, block) -> int or None:
if isinstance(block, block_open):
assert False
for b in self.processed_blocks:
if b.hash() == block.get_previous():
return b.ancillary["amount_sent"]
elif isinstance(block, block_receive):
before = int.from_bytes(self.find_prev_block(block).get_balance(), "big")
for b in self.processed_blocks:
if b.hash() == block.source:
amount = b.ancillary["amount_sent"]
return before + amount
elif isinstance(block, block_change):
for b in self.processed_blocks:
if b.hash() == block.get_previous():
return b.get_balance()
return None
def account_exists(self, account: bytes) -> bool:
for a in self.accounts:
if a.account == account:
return True
return False
def find_blocks_account(self, block) -> bytes or None:
if block.get_account() is not None:
return block.get_account()
for b in self.processed_blocks:
if b.hash() == block.get_previous():
assert(b.get_account() is not None)
return b.get_account()
return None
def find_nano_account(self, account_pk):
for a in self.accounts:
if a.account == account_pk:
return a
return None
# try to process unprocessed blocks, if there is a success try again until there no more successes
def process_unprocessed_blocks(self) -> None:
blocks_processed = []
try_again = True
count = 0
while try_again:
try_again = False
# try to process each block
for blk in self.unprocessed_blocks:
if self.process_one(blk):
count += 1
try_again = True
blocks_processed.append(blk.hash())
# remove blocks that are successfully processed from unprocessed list
self.unprocessed_blocks = set(filter(
lambda blk: not (blk.hash() in blocks_processed),
self.unprocessed_blocks
))
if count > 0:
print('process_unprocessed_blocks] processed %s blocks, %s left' % (count, len(self.unprocessed_blocks)))
def find_prev_block(self, block):
hash = block.get_previous()
for b in self.processed_blocks:
if b.hash() == hash:
return b
def str_processed_blocks(self) -> str:
string = ""
for b in self.processed_blocks:
string += str(b)
string += "\n"
return string
def str_unprocessed_blocks(self) -> str:
string = ""
for b in self.unprocessed_blocks:
string += str(b)
string += "\n"
return string
def __str__(self):
string = "------------- Blocks Manager -------------\n"
string += "Blocks Processed: %d\n" % len(self.processed_blocks)
string += "Unprocessed Blocks: %d\n" % len(self.unprocessed_blocks)
string += "Accounts:\n\n"
for a in self.accounts:
string += " Public Key : %s\n" % hexlify(a.account)
string += " ID : %s\n\n" % acctools.to_account_addr(a.account)
return string
class nano_account:
# open_block can also be a block_state object
def __init__(self, blockman: block_manager, open_block: block_open):
self.first = open_block
self.workdir = blockman.workdir
self.gitrepo = blockman.gitrepo
# print(open_block)
self.account = open_block.get_account()
self.isforked = False
#self.heads = [open_blocks]
self.blocks = {}
self._add_block(open_block, None)
# add a block to account, if previous is set then check for forks
def add_block(self, block, previous: bytes) -> None:
if block.hash() in self.blocks:
if self.workdir:
merged_block = self.blocks[block.hash()]
merged_block.ancillary['peers'].update(block.ancillary['peers'])
hashstr = hexlify(merged_block.hash())
filename = '%s/%s' % (self.workdir, hashstr)
writefile(filename, str(merged_block) + '\n')
#print('block (%s) already exists in account %s' %
# (hexlify(block.hash()), acctools.to_account_addr(block.get_account())))
return
# if previous is none then it must be a starting block
if previous is None:
assert len(self.blocks) == 0
self._add_block(block)
return
# it is not a starting block, look for previous and check for forks
prevblk = self.blocks[previous]
assert prevblk
prev_next = prevblk.get_next()
if prev_next:
print('FORK DETECTED: block: %s previous: %s previous_next: %s' %
(hexlify(block.hash()), hexlify(previous), hexlify(prev_next)))
self.isforked = True
self._add_block(block, prevblk)
else:
print('added block: %s to account %s' %
(hexlify(block.hash()), acctools.to_account_addr(self.account)))
self._add_block(block, prevblk)
prevblk.ancillary['next'] = block.hash()
# block and prevblock are going to be one of the block objects
def _add_block(self, block, prevblk) -> None:
self.blocks[block.hash()] = block
hashstr = hexlify(block.hash())
if self.workdir:
filename = '%s/%s' % (self.workdir, hashstr)
writefile(filename, str(block) + '\n')
if self.gitrepo:
if prevblk is None:
self.gitrepo.git.checkout(orphan=hashstr)
else:
self.gitrepo.git.checkout('-m', '-b', hashstr, hexlify(prevblk.hash()))
self.gitrepo.git.add('.')
print('git commit')
self.gitrepo.git.commit('-m', '.')
print('git commit done')
def find_block_by_hash(self, hsh: bytes):
return self.blocks.get(hsh, None)
# # This method is used for debugging: checking order
# def traverse_backwards(self):
# block = self.blocks[-1]
# traversal = []
# while block is not None:
# traversal.append(self.blocks.index(block))
# block = self.find_prev(block)
# return traversal
# # This method is used for debugging: checking order
# def traverse_forwards(self):
# block = self.blocks[0]
# traversal = []
# while block is not None:
# traversal.append(self.blocks.index(block))
# block = self.find_next(block)
# return traversal
def find_prev(self, block):
prevhash = block.get_previous()
return self.blocks.get(prevhash, None)
def find_next(self, block):
if block.ancillary["next"] is None:
return None
nexthash = block.ancillary["next"]
return self.blocks.get(nexthash, None)
def get_last_block(self):
assert self.first
currblk = self.first
while True:
nexthash = currblk.get_next()
if nexthash is None:
break
nextblk = self.blocks.get(nexthash, None)
if nextblk is None:
break
currblk = nextblk
return currblk
def str_blocks(self) -> str:
string = ""
for b in self.blocks.values():
string += str(b)
string += "\n"
return string
# # Checks if itself is a subset of another account
# def is_subset(self, account):
# for b in self.blocks:
# if b not in account.blocks:
# return False
# return True
# def check_forks(self):
# for b1 in self.blocks:
# for b2 in self.blocks:
# if b1 == b2:
# continue
# elif b1.previous == b2.previous:
# return b1, b2
# return None, None
# def get_balance(self, block):
# return block.get_balance()
def __str__(self):
lastblk = self.get_last_block()
string = "------------- Nano Account -------------\n"
string += "Account : %s\n" % hexlify(self.account)
string += " : %s\n" % acctools.to_account_addr(self.account)
string += "Blocks : %d\n" % len(self.blocks)
string += "First : %s\n" % hexlify(self.first.hash())
string += "Last : %s\n" % hexlify(lastblk.hash())
string += "Balance : %f\n" % (lastblk.get_balance() / (10**30))
string += "isforked: %s\n" % self.isforked
return string
# return DNS adresses as Peer objects
def get_all_dns_addresses_as_peers(addr: str, peerport: int, score: int) -> list[Peer]:
addresses = get_all_dns_addresses(addr)
return [ Peer(ip_addr(ipaddress.IPv6Address(a)), peerport, score) for a in addresses ]
def read_bulk_pull_response(s: socket.socket) -> list:
blocks = []
while True:
block = Block.read_block_from_socket(s)
if block is None:
break
blocks.append(block)
return blocks
def readall(s: socket.socket) -> bytes:
data = b''
while True:
recvd = s.recv(10000)
if recvd == b'':
return data
data += recvd
def verify(data: bytes, signature: bytes, public_key: bytes) -> bool:
try:
ed25519_blake2.checkvalid(signature, data, public_key)
except ed25519_blake2.SignatureMismatch:
return False
return True
def valid_block(ctx, block, post_v2: bool = True) -> bool:
if isinstance(block, block_state):
if block.is_epoch_v2_block():
sig_valid = verify(block.hash(), block.signature, binascii.unhexlify(ctx["epoch_v2_signing_account"]))
elif block.is_epoch_v1_block():
sig_valid = verify(block.hash(), block.signature, binascii.unhexlify(ctx["genesis_pub"]))
else:
sig_valid = verify(block.hash(), block.signature, block.account)
else:
if block.get_account() is None:
raise VerificationErrorNoAccount()
sig_valid = verify(block.hash(), block.signature, block.get_account())
work_valid = pow_block.validate_block_pow(block, post_v2)
return work_valid and sig_valid
# wait for the next message, parse the header but not the payload
# the header is retruned as an object and the payload as raw bytes
def get_next_hdr_payload(s: socket.socket) -> tuple[message_header, bytes]:
# read and parse header
data = read_socket(s, 8)
if data is None:
raise CommsError()
header = message_header.parse_header(data)
# we can determine the size of the payload from the header
size = header.payload_length_bytes()
if size is None:
raise UnknownPacketType(header.msg_type.type)
# read and parse payload
data = read_socket(s, size)
return header, data
def extensions_to_count(extensions: int) -> int:
COUNT_MASK = 0xf000