-
Notifications
You must be signed in to change notification settings - Fork 1
/
life.js
1004 lines (465 loc) · 32.5 KB
/
life.js
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
import {LOG,SIG,BLOCKLOG,SYMBIOTE_ALIAS,PATH_RESOLVE,BLAKE3} from '../../KLY_Utils/utils.js'
import {symbiotes,hostchains,BROADCAST,DECRYPT_KEYS} from './utils.js'
import ControllerBlock from './blocks/controllerblock.js'
import {START_VERIFY_POLLING} from './verification.js'
import InstantBlock from './blocks/instantblock.js'
import fetch from 'node-fetch'
import ora from 'ora'
import l from 'level'
import fs from 'fs'
//______________________________________________________________VARIABLES POOL___________________________________________________________________
global.STOP_GEN_BLOCK={}
global.IN_PROCESS={VERIFY:false,GENERATE:false}
//________________________________________________________________INTERNAL_______________________________________________________________________
//TODO:Add more advanced logic(e.g number of txs,ratings,etc.)
let GET_TXS = symbiote => symbiotes.get(symbiote).MEMPOOL.splice(0,CONFIG.SYMBIOTES[symbiote].MANIFEST.EVENTS_LIMIT_PER_BLOCK),
//TODO:Add more advanced logic(e.g number of txs,ratings,etc.)
GET_CANDIDATES=async symbiote=>{
let limit=0,
promises=[],
state=symbiotes.get(symbiote).STATE
for (let [hash,creator] of symbiotes.get(symbiote).INSTANT_CANDIDATES.entries()){
symbiotes.get(symbiote).INSTANT_CANDIDATES.delete(hash)
//Get account of InstantBlock creator and check if he still has a stake and ability to generate block
promises.push(state.get(creator).then(acc=>
//If enough on balance-then pass hash.Otherwise-delete block from candidates and return "undefined"
acc.B>=CONFIG.SYMBIOTES[symbiote].MANIFEST.INSTANT_FREEZE
?
hash
:
symbiotes.get(symbiote).CANDIDATES.del(hash).catch(e=>
LOG(`Can't delete candidate \x1b[36;1m${hash}\x1b[33;1m on \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}`,'W')
)
).catch(e=>false))
//Limitation
if(limit++==CONFIG.SYMBIOTES[symbiote].MANIFEST.INSTANT_PORTION) break
}
//No "return await"
let readySet=await Promise.all(promises).then(arr=>arr.filter(Boolean)).catch(e=>
LOG(`Oops,set of instant blocks is empty on chain \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}\n${e}`,'W'),
[]
)
return readySet
},
//Tag:ExecMap
GEN_BLOCK_START=async(symbiote,type)=>{
if(!SIG_SIGNAL){
IN_PROCESS.GENERATE=true
await GEN_BLOCK(symbiote,type)
STOP_GEN_BLOCK[symbiote][type]=setTimeout(()=>GEN_BLOCK_START(symbiote,type),CONFIG.SYMBIOTES[symbiote][type+'_BLOCK_GENERATION_TIME'])
CONFIG.SYMBIOTES[symbiote]['STOP_GENERATE_BLOCK_'+type]
&&
clearTimeout(STOP_GEN_BLOCK[symbiote][type])
}else{
LOG(`Block generation for \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}\x1b[36;1m was stopped`,'I',symbiote)
SIG_PROCESS[symbiote].GENERATE=true
}
//leave function
IN_PROCESS.GENERATE=false
},
RUN_POLLING=async symbiote=>{
LOG(`Local state collapsed on \x1b[36;1m${symbiotes.get(symbiote).VERIFICATION_THREAD.COLLAPSED_INDEX}\x1b[32;1m for \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}`,'S')
START_VERIFY_POLLING(symbiote)
}
//________________________________________________________________EXTERNAL_______________________________________________________________________
export let GEN_BLOCK=async(symbiote,blockType)=>{
let hash,route,
symbioteRef=symbiotes.get(symbiote)
//!Here check the difference between VT and GT(VT_GT_NORMAL_DIFFERENCE)
if(blockType==='C' && symbioteRef.VERIFICATION_THREAD.COLLAPSED_INDEX+CONFIG.SYMBIOTES[symbiote].VT_GT_NORMAL_DIFFERENCE < symbioteRef.GENERATION_THREAD.NEXT_INDEX){
LOG(`Block generation for \u001b[38;5;m${SYMBIOTE_ALIAS(symbiote)}\x1b[36;1m skipped because GT is faster than VT. Increase \u001b[38;5;157m<VT_GT_NORMAL_DIFFERENCE>\x1b[36;1m if you need`,'I',symbiote)
return
}
if(blockType==='C'){
//_________________________________________GENERATE PORTION OF BLOCKS___________________________________________
//To fill ControllerBlocks for maximum
let phantomControllers=Math.ceil(symbiotes.get(symbiote).INSTANT_CANDIDATES.size/CONFIG.SYMBIOTES[symbiote].MANIFEST.INSTANT_PORTION),
genThread=symbiotes.get(symbiote).GENERATION_THREAD,
promises=[]//to push blocks to storage
//If nothing to generate-then no sense to generate block,so return
if(phantomControllers===0) return
for(let i=0;i<phantomControllers;i++){
let arr=await GET_CANDIDATES(symbiote),
conBlockCandidate=new ControllerBlock(symbiote,arr)
hash=ControllerBlock.genHash(symbiote,conBlockCandidate.a,conBlockCandidate.i,genThread.PREV_HASH)
conBlockCandidate.sig=await SIG(hash,PRIVATE_KEYS.get(symbiote))
route='/cb'
BLOCKLOG(`New \x1b[36m\x1b[41;1mControllerBlock\x1b[0m\x1b[32m generated ——│\x1b[36;1m`,'S',symbiote,hash,59,'\x1b[32m',conBlockCandidate)
genThread.PREV_HASH=hash
genThread.NEXT_INDEX++
promises.push(symbiotes.get(symbiote).CONTROLLER_BLOCKS.put(conBlockCandidate.i,conBlockCandidate).then(()=>conBlockCandidate).catch(e=>{
LOG(`Failed to store block ${conBlockCandidate.i} on ${SYMBIOTE_ALIAS(symbiote)}`,'F')
process.emit('SIGINT',122)
}))
}
//_______________________________________________COMMIT CHANGES___________________________________________________
//Commit group of blocks by setting hash and index of the last one
await Promise.all(promises).then(arr=>
symbiotes.get(symbiote).METADATA.put('GT',genThread).then(()=>
new Promise(resolve=>{
//And here we should broadcast blocks
arr.forEach(block=>
Promise.all(BROADCAST(route,block,symbiote))
)
//_____________________________________________PUSH TO HOSTCHAINS_______________________________________________
//Push to hostchains due to appropriate symbiote
Object.keys(CONFIG.SYMBIOTES[symbiote].MANIFEST.HOSTCHAINS).forEach(async ticker=>{
//TODO:Add more advanced logic
if(!CONFIG.SYMBIOTES[symbiote].STOP_HOSTCHAINS[ticker]){
let control=symbiotes.get(symbiote).HOSTCHAINS_WORKFLOW[ticker],
hostchain=hostchains.get(symbiote).get(ticker),
//If previous push is still not accepted-then no sense to push new symbiote update
isAlreadyAccepted=await hostchain.checkTx(control.HOSTCHAIN_HASH,control.INDEX,control.KLYNTAR_HASH,symbiote).catch(e=>false)
LOG(`Check if previous commit is accepted for \x1b[32;1m${SYMBIOTE_ALIAS(symbiote)}\x1b[36;1m on \x1b[32;1m${ticker}\x1b[36;1m ~~~> \x1b[32;1m${
control.KLYNTAR_HASH===''?'Just start':isAlreadyAccepted
}`,'I')
if(control.KLYNTAR_HASH===''||isAlreadyAccepted){
//If accpted-we can share to the rest
isAlreadyAccepted
&&
Promise.all(BROADCAST('/proof',{...control,symbiote,ticker},symbiote))
let index=symbiotes.get(symbiote).GENERATION_THREAD.NEXT_INDEX-1,
symbioticHash=await hostchain.sendTx(symbiote,index,genThread.PREV_HASH).catch(e=>{
LOG(`Error on \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}\u001b[38;5;3m with push to \x1b[36;1m${ticker} \n${e}`,'W')
return false
})
if(symbioticHash){
LOG(`Commit on ${SYMBIOTE_ALIAS(symbiote)}\x1b[32;1m to \x1b[36;1m${ticker}\x1b[32;1m for block \x1b[36;1m${index}\x1b[32;1m is \x1b[36;1m${symbioticHash}`,'S')
//Commit localy that we have send it
control.KLYNTAR_HASH=genThread.PREV_HASH
control.INDEX=index
control.HOSTCHAIN_HASH=symbioticHash
control.SIG=await SIG(control.KLYNTAR_HASH+control.INDEX+control.HOSTCHAIN_HASH+ticker,PRIVATE_KEYS.get(symbiote))
await symbiotes.get(symbiote).HOSTCHAINS_DATA.put(index+ticker,{KLYNTAR_HASH:control.KLYNTAR_HASH,HOSTCHAIN_HASH:control.HOSTCHAIN_HASH,SIG:control.SIG})
.then(()=>symbiotes.get(symbiote).HOSTCHAINS_DATA.put(ticker,control))//set such canary to avoid duplicates when quick reboot daemon
.then(()=>LOG(`Locally store pointer for \x1b[36;1m${index}\x1b[32;1m block of \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}\x1b[32;1m on \x1b[36;1m${ticker}`,'S'))
.catch(e=>LOG(`Error-impossible to store pointer for \x1b[36;1m${index}\u001b[38;5;3m block of \x1b[36;1m${SYMBIOTE_ALIAS(symbiote)}\u001b[38;5;3m on \x1b[36;1m${ticker}`,'W'))
}
LOG(`Balance of controller on hostchain \x1b[32;1m${ticker}\x1b[36;1m is \x1b[32;1m${await hostchain.getBalance(symbiote)}`,'I')
}
}
})
resolve()
})
).catch(e=>{
LOG(e,'F')
process.emit('SIGINT',114)
})
)
}
else{
//______________________________TEST__________________________________
let insBlockCandidate=new InstantBlock(symbiote,await GET_TXS(symbiote))
//_______________________________________________DELELTE________________________________
hash=InstantBlock.genHash(insBlockCandidate.c,insBlockCandidate.e,symbiote)
insBlockCandidate.sig=await SIG(hash,PRIVATE_KEYS.get(symbiote))
route='/ib'
//____________________________________TEST_____________________________
BLOCKLOG(`New \x1b[36;1m\x1b[44;1mInstantBlock\x1b[0m\x1b[32m generated ——│`,'S',symbiote,hash,56,'\x1b[32m',insBlockCandidate)
await symbiotes.get(symbiote).CANDIDATES.put(hash,insBlockCandidate)
Promise.all(BROADCAST(route,insBlockCandidate,symbiote))
//These blocks also will be included
symbiotes.get(symbiote).INSTANT_CANDIDATES.set(hash,CONFIG.SYMBIOTES[symbiote].PUB)
}
},
RELOAD_STATE=async(symbiote,symbioteRef)=>{
//Reset verification breakpoint
await symbioteRef.STATE.clear()
let promises=[],
//Try to load snapshot metadata to use as last collapsed
canary=await symbioteRef.SNAPSHOT.METADATA.get('CANARY').catch(e=>false),
snapshotVT=await symbioteRef.SNAPSHOT.METADATA.get('VT').catch(e=>false),
//Snapshot will never be OK if it's empty
snapshotIsOk = snapshotVT.CHECKSUM===BLAKE3(JSON.stringify(snapshotVT.DATA)+snapshotVT.COLLAPSED_INDEX+snapshotVT.COLLAPSED_HASH)//snapshot itself must be OK
&&
canary===snapshotVT.CHECKSUM//and we must be sure that no problems with staging zone,so snapshot is finally OK
&&
CONFIG.SYMBIOTES[symbiote].SNAPSHOTS.ALL//we have no remote shards(first releases don't have these features)
//Try to load from snapshot
//If it was initial run-then we'll start from genesis state
// If it's integrity problem - then we check if snapshot is OK-then we can load data from snapshot
//In case when snapshot is NOT ok - go to else branch and start sync from the genesis
if( snapshotIsOk ){
symbioteRef.VERIFICATION_THREAD=snapshotVT
let accs={},promises=[]
await new Promise(
resolve => symbioteRef.SNAPSHOT.STATE.createReadStream()
.on('data',data=>accs[data.key]=data.value)
.on('close',resolve)
)
Object.keys(accs).forEach(addr=>promises.push(symbioteRef.STATE.put(addr,accs[addr])))
await Promise.all(promises)
.then(()=>symbioteRef.METADATA.put('CANARY',canary))
.then(()=>symbioteRef.METADATA.put('VT',snapshotVT))
.catch(e=>{
LOG(`Problems with loading state from snapshot to state db \n${e}`,'F')
process.exit(138)
})
LOG(`Successfully recreated state from snapshot`,'I')
}else{
//Otherwise start rescan form height=0
symbioteRef.VERIFICATION_THREAD.COLLAPSED_INDEX==-1 && (!CONFIG.SYMBIOTES[symbiote].CONTROLLER.ME || symbioteRef.GENERATION_THREAD.NEXT_INDEX==0) ? LOG(`Initial run with no snapshot`,'I') : LOG(`Start sync from genesis`,'W')
symbioteRef.VERIFICATION_THREAD={COLLAPSED_HASH:'Poyekhali!@Y.A.Gagarin',COLLAPSED_INDEX:-1,DATA:{},CHECKSUM:''}
//Load all the configs
fs.readdirSync(process.env.GENESIS_PATH+`/${symbiote}`).forEach(file=>{
//Load genesis state or data from backups(not to load state from the beginning)
let genesis=JSON.parse(fs.readFileSync(process.env.GENESIS_PATH+`/${symbiote}/${file}`))
Object.keys(genesis).forEach(
address => promises.push(symbioteRef.STATE.put(address,genesis[address]))
)
})
await Promise.all(promises)
}
},
PREPARE_SYMBIOTE=async symbioteId=>{
//Loading spinner
let initSpinner
if(!CONFIG.PRELUDE.NO_SPINNERS){
initSpinner = ora({
color:'red',
prefixText:`\u001b[38;5;${process.env.KLY_MODE==='main'?'23':'202'}m [${new Date().toLocaleDateString()} at ${new Date().toLocaleTimeString()}]\u001b[38;5;99m(pid:${process.pid}) \x1b[36;1mPreparing symbiote \x1b[32;1m${SYMBIOTE_ALIAS(symbioteId)}\x1b[0m`
}).start()
}
//____________________________________________Prepare structures_________________________________________________
let symbioteConfig=CONFIG.SYMBIOTES[symbioteId]
//Contains default set of properties for major part of potential use-cases on symbiote
symbiotes.set(symbioteId,{
MEMPOOL:[],
//Сreate mapping to optimize processes while we check blocks-not to read/write to db many times
ACCOUNTS:new Map(),// ADDRESS => { ACCOUNT_STATE , NONCE_SET , NONCE_DUPLICATES , OUT , TYPE }
EVENTS_STATE:new Map(),// EVENT_KEY(on symbiote) => EVENT_VALUE
BLACKLIST:new Set(),//To sift addresses which spend more than has when we check another ControllerBlock
//Peers to exchange data with
NEAR:[]
})
let symbioteRef=symbiotes.get(symbioteId)
//Open writestream in append mode
SYMBIOTES_LOGS_STREAMS.set(symbioteId,fs.createWriteStream(process.env.LOGS_PATH+`/${symbioteId}.txt`),{flags:'a+'});
//OnlyLinuxFans.Due to incapsulation level we need to create sub-level directory for each symbiote
[process.env.CHAINDATA_PATH,process.env.SNAPSHOTS_PATH].forEach(
name => !fs.existsSync(`${name}/${symbioteId}`) && fs.mkdirSync(`${name}/${symbioteId}`)
)
//___________________________Load functionality to verify/filter/transform events_______________________________
//Importnat and must be the same for symbiote at appropriate chunks of time
await import(`./verifiers.js`).then(mod=>{
symbioteRef.VERIFIERS=mod.VERIFIERS
symbioteRef.SPENDERS=mod.SPENDERS
})
//Might be individual for each node
symbioteRef.FILTERS=(await import(`./filters.js`)).default;
//______________________________________Prepare databases and storages___________________________________________
//Create subdirs due to rational solutions
[
'METADATA',//important dir-cointains canaries,pointer to VERIFICATION_THREAD and GENERATION_THREADS
'CONTROLLER_BLOCKS',//For Controller's blocks(key is index)
'INSTANT_BLOCKS',//For Instant(key is hash)
'HOSTCHAINS_DATA',//To store external flow of commits for ControllerBlocks
'CANDIDATES'//For candidates(key is a hash(coz it's also InstantBlocks,but yet not included to chain))
].forEach(
dbName => symbioteRef[dbName]=l(process.env.CHAINDATA_PATH+`/${symbioteId}/${dbName}`,{valueEncoding:'json'})
)
/*
___________________________________________________State of symbiote___________________________________________________
*********************************************************************
* THE MOST IMPORTANT STORAGE-basis for each symbiote *
*********************************************************************
Holds accounts state,balances,aliases,services & conveyors metadata and so on
*Examples:
0)Aliases of accounts & groups & contracts & services & conveyors & domains & social media usernames. Some hint to Web1337.Read more on our sources https://klyntar.org
Single emoji refers to address and domain:❤️ => 0xd1ffa2d57241b01174db76b3b7123c3f707a12b91ddda00ea971741c94ab3578(Polygon contract,https://charity.health.com)
Combo:🔥😈🔥 => PQTJJR4FZIDBLLKOUVAD7FUYYGL66TJUPDERHBTJUUTTIDPYPGGQ(Algorand address by Klyntar)
Emoji ref to special signature type🌌 => aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(Root of hashes tree mapped to conveyor set of addresses protected by hash-based post quantum signatures)
Usernames(Twitter in this case) @jack => bc1qsmljf8cmfhul2tuzcljc2ylxrqhwf7qxpstj2a
1)
*/
symbioteRef.STATE=l(process.env.CHAINDATA_PATH+`/${symbioteId}/STATE`,{valueEncoding:'json'})
//...and separate dirs for state and metadata snapshots
symbioteRef.SNAPSHOT={
METADATA:l(process.env.SNAPSHOTS_PATH+`/${symbioteId}/METADATA`,{valueEncoding:'json'}),
STATE:l(process.env.SNAPSHOTS_PATH+`/${symbioteId}/STATE`,{valueEncoding:'json'})
}
//________________Load metadata about symbiote-current hight,collaped height,height for export,etc.___________________
symbioteRef.VERIFICATION_THREAD = await symbioteRef.METADATA.get('VT').catch(e=>
e.notFound
?
{COLLAPSED_HASH:'Poyekhali!@Y.A.Gagarin',COLLAPSED_INDEX:-1,DATA:{},CHECKSUM:''}//initial
:
(LOG(`Some problem with loading metadata of verification thread\nSymbiote:${symbioteId}\nError:${e}`,'F'),process.exit(124))
)
//_____Load security stuff-check if stop was graceful,canary is present,should we reload the state and so on_____
//These options only for Controller
//Due to phantom blocks,we'll generate blocks faster than state become verified,that's why we need two extra properties
if(symbioteConfig.CONTROLLER.ME){
symbioteRef.GENERATION_THREAD = await symbioteRef.METADATA.get('GT').catch(e=>
e.notFound
?
{
PREV_HASH:`Poyekhali!@Y.A.Gagarin`,//Genesis hash
NEXT_INDEX:0//So the first block will be with index 0
}
:
(LOG(`Some problem with loading metadata of generation thread\nSymbiote:${symbioteId}\nError:${e}`,'F'),process.exit(125))
)
let nextIsPresent = await symbioteRef.CONTROLLER_BLOCKS.get(symbioteRef.GENERATION_THREAD.NEXT_INDEX).catch(e=>false),//OK is in case of absence of next block
previous=await symbioteRef.CONTROLLER_BLOCKS.get(symbioteRef.GENERATION_THREAD.NEXT_INDEX-1).catch(e=>false)//but current block should present at least locally
if(nextIsPresent || !(symbioteRef.GENERATION_THREAD.NEXT_INDEX===0 || symbioteRef.GENERATION_THREAD.PREV_HASH === BLAKE3( JSON.stringify(previous.a) + symbioteId + previous.i + previous.p))){
initSpinner?.stop()
LOG(`Something wrong with a sequence of generation thread on \x1b[36;1m${SYMBIOTE_ALIAS(symbioteId)}`,'F')
process.exit(125)
}
}
//If we just start verification thread, there is no sense to do following logic
if(symbioteRef.VERIFICATION_THREAD.COLLAPSED_INDEX!==-1){
await symbioteRef.METADATA.get('CANARY').then(async canary=>{
let verifThread=symbioteRef.VERIFICATION_THREAD
//If staging zone is OK
if(verifThread.CHECKSUM===BLAKE3(JSON.stringify(verifThread.DATA)+verifThread.COLLAPSED_INDEX+verifThread.COLLAPSED_HASH)){
//This is the signal that we should rewrite state changes from the staging zone
if(canary!==verifThread.CHECKSUM){
initSpinner?.stop()
LOG(`Load state data from staging zone on \x1b[32;1m${SYMBIOTE_ALIAS(symbioteId)}`,'I')
let promises=[];
['ACCOUNTS','EVENTS'].forEach(
type => Object.keys(verifThread.DATA[type]).forEach(
key => promise.push(symbioteRef.STATE.put(key,verifThread.DATA[type][key]))
)
)
await Promise.all(promises).catch(e=>{
LOG(`Problems with loading state from staging zone of verification thread on \x1b[36;1m${SYMBIOTE_ALIAS(symbioteId)}\x1b[31;1m\n${e}`,'F')
process.exit(133)
})
}
}else{
initSpinner?.stop()
LOG(`Problems with staging zone of verification thread on \x1b[36;1m${SYMBIOTE_ALIAS(symbioteId)}`,'W')
await RELOAD_STATE(symbioteId,symbioteRef)
}
}).catch(async err=>{
initSpinner?.stop()
LOG(fs.readFileSync(PATH_RESOLVE('images/events/canaryDied.txt')).toString(),'CD')
LOG(`Problems with canary on \x1b[36;1m${SYMBIOTE_ALIAS(symbioteId)}\n${err}`,'W')
//Reset verification breakpoint
await RELOAD_STATE(symbioteId,symbioteRef)
})
}else {
initSpinner?.stop()
//Clear previous state to avoid mistakes
symbioteRef.STATE.clear()
//Load data from genesis state(initial values)
await RELOAD_STATE(symbioteId,symbioteRef)
}
symbioteRef.INSTANT_CANDIDATES=new Map()//mapping(hash=>creator)
//Clear,to not store OUT-OF-CHAIN blocks
//*UPD:Node operators should run cleaning time by time
//chainRef.CANDIDATES.clear()
//__________________________________Load modules to work with hostchains_________________________________________
//...and push template to global HOSTCHAINS_DATA object to control the flow
let tickers=Object.keys(symbioteConfig.MANIFEST.HOSTCHAINS),EvmHostChain,hostchainmap=new Map()
symbioteRef.HOSTCHAINS_WORKFLOW={}
//Add hostchains to mapping
//Load way to communicate with hostchain via appropriate type
for(let i=0,l=tickers.length;i<l;i++){
let way=symbioteConfig.MANIFEST.HOSTCHAINS[tickers[i]].TYPE
//Depending on TYPE load appropriate module
if(CONFIG.EVM.includes(tickers[i])){
EvmHostChain=(await import(`../../KLY_Hostchains/connectors/${way}/evm.js`)).default
hostchainmap.set(tickers[i],new EvmHostChain(symbioteId,tickers[i]))
}else hostchainmap.set(tickers[i],(await import(`../../KLY_Hostchains/connectors/${way}/${tickers[i]}.js`)).default)
hostchains.set(symbioteId,hostchainmap)
//hostchains.set(controllerAddr,tickers[i],(await import(`./KLY_Hostchains/${tickers[i]}.js`)).default)//load module
//Load canary
symbioteRef.HOSTCHAINS_WORKFLOW[tickers[i]]=await symbioteRef.HOSTCHAINS_DATA.get(tickers[i]).catch(e=>( {KLYNTAR_HASH:'',INDEX:0,HOSTCHAIN_HASH:'',SIG:''} ))
}
//___________________Decrypt all private keys(for KLYNTAR and hostchains) to memory of process___________________
await DECRYPT_KEYS(symbioteId,initSpinner,symbioteConfig.CONTROLLER.ME?'Controller':'Instant generator').then(()=>
//Print just first few bytes of keys to view that they were decrypted well.Looks like checksum
LOG(`Private key on \x1b[36;1m${SYMBIOTE_ALIAS(symbioteId)}\x1b[32;1m was decrypted successfully`,'S')
).catch(e=>{
LOG(`Keys decryption failed.Please,check your password carefully.In the worst case-use your decrypted keys from safezone and repeat procedure of encryption via CLI\n${e}`,'F')
process.exit(100)
})
//___________________________________________Load data from hostchains___________________________________________
//TODO:Add more advanced info
if(symbioteConfig.CONTROLLER.ME){
for(let i=0,l=tickers.length;i<l;i++){
let balance
if(CONFIG.SYMBIOTES[CURRENT_SYMBIOTE_ID].BALANCE_VIEW){
let spinner = ora({
color:'red',
prefixText:`\u001b[38;5;23m [${new Date().toLocaleDateString()} at ${new Date().toLocaleTimeString()}] \x1b[36;1mGetting balance for \x1b[32;1m${tickers[i]}\x1b[36;1m - keep waiting\x1b[0m`
}).start()
balance = await hostchains.get(symbioteId).get(tickers[i]).getBalance(symbioteId)
spinner.stop()
LOG(`Balance of controller on hostchain \x1b[32;1m${
tickers[i]
}\x1b[36;1m is \x1b[32;1m${
CONFIG.SYMBIOTES[CURRENT_SYMBIOTE_ID].BALANCE_VIEW?balance:'<disabled>'
} \x1b[36;1m[${symbioteConfig.STOP_HOSTCHAINS[tickers[i]]?'\x1b[31;1mSTOP':'\x1b[32;1mPUSH'}\x1b[36;1m]`,'I')
}
}
//____________________________________________GENERAL SYMBIOTE INFO____________________________________________
LOG(fs.readFileSync(PATH_RESOLVE('images/events/syminfo.txt')).toString(),'S')
LOG(`Canary is \x1b[32;1m<OK>`,'I')
LOG(`Collapsed on \x1b[32;1m${symbioteRef.VERIFICATION_THREAD.COLLAPSED_INDEX} \u001b[38;5;168m}———{\x1b[32;1m ${symbioteRef.VERIFICATION_THREAD.COLLAPSED_HASH}`,'I')
//Ask to approve current set of hostchains
!CONFIG.PRELUDE.OPTIMISTIC
&&
await new Promise(resolve=>
readline.createInterface({input:process.stdin, output:process.stdout, terminal:false})
.question(`\n ${'\u001b[38;5;23m'}[${new Date().toLocaleDateString()} at ${new Date().toLocaleTimeString()}]${'\x1b[36;1m'} Do you agree with the current set of hostchains? Enter \x1b[32;1mYES\x1b[36;1m to continue ———> \x1b[0m`,resolve)
).then(answer=>answer!=='YES'&& process.exit(126))
}
SIG_PROCESS[symbioteId]={VERIFY:false,GENERATE:false}//we should track events in both threads-as in verification,as in generation
},
RENAISSANCE=async symbioteID=>{
//_____________________________________________________Connect with CONTROLLER & NODES___________________________________________________________
let promises=[]
!CONFIG.SYMBIOTES[symbioteID].STOP_WORK
&&
promises.push(
//Controller doesn't need to load state coz without him there are no progress in chain.At least-in the first versions
RUN_POLLING(symbioteID).then(()=>
/*
Get current nodeset due to region.NOTE-each controller may has different depth of nodeset
Someone can give u list of nodes from region(e.g EU(Europe),NA(Nothern America),etc.)
Some controllers will have better localization(e.g EU_FR(Europe,France),NA_US_CA(United States,California))
Every controller may use own labels,follow ISO-3166 formats or even digits(e.g 0-Europe,1-USA,01-Europe,Germany,etc.)
Format defined by Controller and become public for Instant generators and other members to let them to find the best&fastest options
*/
!CONFIG.SYMBIOTES[symbioteID].CONTROLLER.ME
&&
fetch(CONFIG.SYMBIOTES[symbioteID].CONTROLLER.ADDR+'/nodes/'+symbioteID+'/'+CONFIG.SYMBIOTES[symbioteID].REGION).then(r=>r.json()).then(
async nodesArr=>{
LOG(`Received ${nodesArr.length} addresses from ${SYMBIOTE_ALIAS(symbioteID)}...`,'I')
let answers=[]
//Ask if these nodes are available and ready to share data with us
nodesArr.forEach(
addr => answers.push(
fetch(addr+'/addnode',{method:'POST',body:JSON.stringify([symbioteID,CONFIG.SYMBIOTES[symbioteID].MY_ADDR])})
.then(res=>res.text())
.then(val=>val==='OK'&&symbiotes.get(symbioteID).NEAR.push(addr))
.catch(e=>'')
)
)
await Promise.all(answers)
LOG(`Total nodeset ${SYMBIOTE_ALIAS(symbioteID)}...\x1b[36;1m has ${symbiotes.get(symbioteID).NEAR.length} addresses`,'I')
}
).catch(e=>LOG(`Controller of \x1b[36;1m${SYMBIOTE_ALIAS(symbioteID)}\x1b[31;1m is offline or some error has been occured\n${e}\n`,'F'))
))
await Promise.all(promises.splice(0))
//______________________________________________________RUN BLOCKS GENERATION PROCESS____________________________________________________________
let symbioteRef=CONFIG.SYMBIOTES[symbioteID]
if(!symbioteRef.STOP_WORK){
//Start generate ControllerBlocks if you're controller(obviously)
!symbioteRef.STOP_GENERATE_BLOCK_C && symbioteRef.CONTROLLER.ME && setTimeout(()=>{
STOP_GEN_BLOCK[symbioteID]={C:''}
//Tag:ExecMap - run generation workflow for ControllerBlocks
GEN_BLOCK_START(symbioteID,'C')
},symbioteRef.BLOCK_С_INIT_DELAY)
!symbioteRef.STOP_GENERATE_BLOCK_I && setTimeout(()=>{
STOP_GEN_BLOCK[symbioteID] ? STOP_GEN_BLOCK[symbioteID]['I']='' : STOP_GEN_BLOCK[symbioteID]={C:'',I:''}
//Tag:ExecMap - run generation workflow for InstantBlocks
GEN_BLOCK_START(symbioteID,'I')
},symbioteRef.BLOCK_I_INIT_DELAY)