/
processList.go
1305 lines (1098 loc) · 39.3 KB
/
processList.go
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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"bytes"
"fmt"
"log"
"sync"
"encoding/binary"
"os"
"github.com/FactomProject/factomd/common/adminBlock"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/directoryBlock"
"github.com/FactomProject/factomd/common/entryCreditBlock"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/messages"
"github.com/FactomProject/factomd/common/primitives"
//"github.com/FactomProject/factomd/database/databaseOverlay"
)
var _ = fmt.Print
var _ = log.Print
type Request struct {
vmIndex int // VM Index
vmheight uint32 // Height in the Process List where we are missing a message
wait int64 // How long to wait before we actually request
sent int64 // Last time sent (zero means none have been sent)
requestCnt int
}
var _ interfaces.IRequest = (*Request)(nil)
func (r *Request) key() (thekey [32]byte) {
binary.BigEndian.PutUint32(thekey[0:4], uint32(r.vmIndex))
binary.BigEndian.PutUint64(thekey[5:13], uint64(r.wait))
binary.BigEndian.PutUint64(thekey[14:22], uint64(r.vmheight))
return thekey
}
/*
func (r *Request) key() (thekey [20]byte) {
binary.BigEndian.PutUint32(thekey[0:4], uint32(r.vmIndex))
binary.BigEndian.PutUint64(thekey[4:12], uint64(r.wait))
binary.BigEndian.PutUint64(thekey[12:20], uint64(r.sent))
return
}
*/
type ProcessList struct {
DBHeight uint32 // The directory block height for these lists
// Temporary balances from updating transactions in real time.
FactoidBalancesT map[[32]byte]int64
FactoidBalancesTMutex sync.Mutex
ECBalancesT map[[32]byte]int64
ECBalancesTMutex sync.Mutex
State *State
VMs []*VM // Process list for each server (up to 32)
ServerMap [10][64]int // Map of FedServers to all Servers for each minute
System VM // System Faults and other system wide messages
SysHighest int
diffSigTally int /* Tally of how many VMs have provided different
Directory Block Signatures than what we have
(discard DBlock if > 1/2 have sig differences) */
// messages processed in this list
OldMsgs map[[32]byte]interfaces.IMsg
oldmsgslock *sync.Mutex
OldAcks map[[32]byte]interfaces.IMsg
oldackslock *sync.Mutex
// Entry Blocks added within 10 minutes (follower and leader)
NewEBlocks map[[32]byte]interfaces.IEntryBlock
neweblockslock *sync.Mutex
NewEntriesMutex sync.RWMutex
NewEntries map[[32]byte]interfaces.IEntry
// State information about the directory block while it is under construction. We may
// have to start building the next block while still building the previous block.
AdminBlock interfaces.IAdminBlock
EntryCreditBlock interfaces.IEntryCreditBlock
DirectoryBlock interfaces.IDirectoryBlock
// Number of Servers acknowledged by Factom
Matryoshka []interfaces.IHash // Reverse Hash
AuditServers []interfaces.IFctServer // List of Audit Servers
FedServers []interfaces.IFctServer // List of Federated Servers
// AmINegotiator is just used for displaying an "N" next to a node
// that is the assigned negotiator for a particular processList
// height
AmINegotiator bool
// DB Sigs
DBSignatures []DBSig
DBSigAlreadySent bool
Requests map[[32]byte]*Request
//Requests map[[20]byte]*Request
NextHeightToProcess [64]int
}
var _ interfaces.IProcessList = (*ProcessList)(nil)
func (p *ProcessList) GetAmINegotiator() bool {
return p.AmINegotiator
}
func (p *ProcessList) SetAmINegotiator(b bool) {
p.AmINegotiator = b
}
// Data needed to add to admin block
type DBSig struct {
ChainID interfaces.IHash
Signature interfaces.IFullSignature
VMIndex int
}
type VM struct {
List []interfaces.IMsg // Lists of acknowledged messages
ListAck []*messages.Ack // Acknowledgements
Height int // Height of messages that have been processed
LeaderMinute int // Where the leader is in acknowledging messages
Synced bool // Is this VM synced yet?
//faultingEOM int64 // Faulting for EOM because it is too late
heartBeat int64 // Just ping ever so often if we have heard nothing.
Signed bool // We have signed the previous block.
WhenFaulted int64 // WhenFaulted is a timestamp of when this VM was faulted
// vm.WhenFaulted serves as a bool flag (if > 0, the vm is currently considered faulted)
FaultFlag int // FaultFlag tracks what the VM was faulted for (0 = EOM missing, 1 = negotiation issue)
}
func (p *ProcessList) Clear() {
return
//p.State.AddStatus(fmt.Sprintf("PROCESSLIST.Clear dbht %d", p.DBHeight))
p.FactoidBalancesTMutex.Lock()
defer p.FactoidBalancesTMutex.Unlock()
p.FactoidBalancesT = nil
p.ECBalancesTMutex.Lock()
defer p.ECBalancesTMutex.Unlock()
p.ECBalancesT = nil
p.oldmsgslock.Lock()
defer p.oldmsgslock.Unlock()
p.OldMsgs = nil
p.oldackslock.Lock()
defer p.oldackslock.Unlock()
p.OldAcks = nil
p.neweblockslock.Lock()
defer p.neweblockslock.Unlock()
p.NewEBlocks = nil
p.NewEntriesMutex.Lock()
defer p.NewEntriesMutex.Unlock()
p.NewEntries = nil
p.AdminBlock = nil
p.EntryCreditBlock = nil
p.DirectoryBlock = nil
p.Matryoshka = nil
p.AuditServers = nil
p.FedServers = nil
p.DBSignatures = nil
p.Requests = nil
}
func (p *ProcessList) GetKeysNewEntries() (keys [][32]byte) {
keys = make([][32]byte, p.LenNewEntries())
if p == nil {
return
}
p.NewEntriesMutex.RLock()
defer p.NewEntriesMutex.RUnlock()
i := 0
for k := range p.NewEntries {
keys[i] = k
i++
}
return
}
func (p *ProcessList) GetNewEntry(key [32]byte) interfaces.IEntry {
p.NewEntriesMutex.RLock()
defer p.NewEntriesMutex.RUnlock()
return p.NewEntries[key]
}
func (p *ProcessList) LenNewEntries() int {
if p == nil {
return 0
}
p.NewEntriesMutex.RLock()
defer p.NewEntriesMutex.RUnlock()
return len(p.NewEntries)
}
func (p *ProcessList) Complete() bool {
if p == nil {
return false
}
if p.DBHeight <= p.State.GetHighestSavedBlk() {
return true
}
for i := 0; i < len(p.FedServers); i++ {
vm := p.VMs[i]
if vm.LeaderMinute < 10 {
return false
}
if vm.Height < len(vm.List) {
return false
}
}
return true
}
// Returns the Virtual Server index for this hash for the given minute
func (p *ProcessList) VMIndexFor(hash []byte) int {
if p.State.OneLeader {
return 0
}
v := uint64(0)
for _, b := range hash {
v += uint64(b)
}
r := int(v % uint64(len(p.FedServers)))
return r
}
func SortServers(servers []interfaces.IFctServer) []interfaces.IFctServer {
for i := 0; i < len(servers)-1; i++ {
done := true
for j := 0; j < len(servers)-1-i; j++ {
fs1 := servers[j].GetChainID().Bytes()
fs2 := servers[j+1].GetChainID().Bytes()
if bytes.Compare(fs1, fs2) > 0 {
tmp := servers[j]
servers[j] = servers[j+1]
servers[j+1] = tmp
done = false
}
}
if done {
return servers
}
}
return servers
}
func (p *ProcessList) SortFedServers() {
p.FedServers = SortServers(p.FedServers)
}
func (p *ProcessList) SortAuditServers() {
p.AuditServers = SortServers(p.AuditServers)
}
func (p *ProcessList) SortDBSigs() {
// Sort by VMIndex
for i := 0; i < len(p.DBSignatures)-1; i++ {
done := true
for j := 0; j < len(p.DBSignatures)-1-i; j++ {
if p.DBSignatures[j].VMIndex > p.DBSignatures[j+1].VMIndex {
tmp := p.DBSignatures[j]
p.DBSignatures[j] = p.DBSignatures[j+1]
p.DBSignatures[j+1] = tmp
done = false
}
}
if done {
return
}
}
/* Sort by ChainID
for i := 0; i < len(p.DBSignatures)-1; i++ {
done := true
for j := 0; j < len(p.DBSignatures)-1-i; j++ {
fs1 := p.DBSignatures[j].ChainID.Bytes()
fs2 := p.DBSignatures[j+1].ChainID.Bytes()
if bytes.Compare(fs1, fs2) > 0 {
tmp := p.DBSignatures[j]
p.DBSignatures[j] = p.DBSignatures[j+1]
p.DBSignatures[j+1] = tmp
done = false
}
}
if done {
return
}
}*/
}
// Returns the Federated Server responsible for this hash in this minute
func (p *ProcessList) FedServerFor(minute int, hash []byte) interfaces.IFctServer {
vs := p.VMIndexFor(hash)
if vs < 0 {
return nil
}
fedIndex := p.ServerMap[minute][vs]
return p.FedServers[fedIndex]
}
func (p *ProcessList) GetVirtualServers(minute int, identityChainID interfaces.IHash) (found bool, index int) {
found, fedIndex := p.GetFedServerIndexHash(identityChainID)
if !found {
return false, -1
}
p.MakeMap()
for i := 0; i < len(p.FedServers); i++ {
fedix := p.ServerMap[minute][i]
if fedix == fedIndex {
return true, i
}
}
return false, -1
}
// Returns true and the index of this server, or false and the insertion point for this server
func (p *ProcessList) GetFedServerIndexHash(identityChainID interfaces.IHash) (bool, int) {
if p == nil {
return false, 0
}
p.SortFedServers()
scid := identityChainID.Bytes()
for i, fs := range p.FedServers {
// Find and remove
comp := bytes.Compare(scid, fs.GetChainID().Bytes())
if comp == 0 {
return true, i
}
}
return false, len(p.FedServers)
}
// Returns true and the index of this server, or false and the insertion point for this server
func (p *ProcessList) GetAuditServerIndexHash(identityChainID interfaces.IHash) (bool, int) {
if p == nil {
return false, 0
}
p.SortAuditServers()
scid := identityChainID.Bytes()
for i, fs := range p.AuditServers {
// Find and remove
if bytes.Compare(scid, fs.GetChainID().Bytes()) == 0 {
return true, i
}
}
return false, len(p.AuditServers)
}
// This function will be replaced by a calculation from the Matryoshka hashes from the servers
// but for now, we are just going to make it a function of the dbheight.
func (p *ProcessList) MakeMap() {
n := len(p.FedServers)
if n > 0 {
indx := int(p.DBHeight*131) % n
for i := 0; i < 10; i++ {
indx = (indx + 1) % n
for j := 0; j < len(p.FedServers); j++ {
p.ServerMap[i][j] = indx
indx = (indx + 1) % n
}
}
}
}
// This function will be replaced by a calculation from the Matryoshka hashes from the servers
// but for now, we are just going to make it a function of the dbheight.
func (p *ProcessList) PrintMap() string {
n := len(p.FedServers)
prt := fmt.Sprintf("===PrintMapStart=== %d\n", p.DBHeight)
prt = prt + fmt.Sprintf("dddd %s minute map: s.LeaderVMIndex %d pl.dbht %d s.dbht %d s.EOM %v\ndddd ",
p.State.FactomNodeName, p.State.LeaderVMIndex, p.DBHeight, p.State.LLeaderHeight, p.State.EOM)
for i := 0; i < n; i++ {
prt = fmt.Sprintf("%s%3d", prt, i)
}
prt = prt + "\ndddd "
for i := 0; i < 10; i++ {
prt = fmt.Sprintf("%s%3d ", prt, i)
for j := 0; j < len(p.FedServers); j++ {
prt = fmt.Sprintf("%s%2d ", prt, p.ServerMap[i][j])
}
prt = prt + "\ndddd "
}
prt = prt + fmt.Sprintf("\n===PrintMapEnd=== %d\n", p.DBHeight)
return prt
}
// Add the given serverChain to this processlist as a Federated Server, and return
// the server index number of the added server
func (p *ProcessList) AddFedServer(identityChainID interfaces.IHash) int {
p.SortFedServers()
found, i := p.GetFedServerIndexHash(identityChainID)
if found {
//p.State.AddStatus(fmt.Sprintf("ProcessList.AddFedServer Server already there %x at height %d", identityChainID.Bytes()[2:6], p.DBHeight))
return i
}
// If an audit server, it gets promoted
auditFound, _ := p.GetAuditServerIndexHash(identityChainID)
if auditFound {
//p.State.AddStatus(fmt.Sprintf("ProcessList.AddFedServer Server %x was an audit server at height %d", identityChainID.Bytes()[2:6], p.DBHeight))
p.RemoveAuditServerHash(identityChainID)
}
p.FedServers = append(p.FedServers, nil)
copy(p.FedServers[i+1:], p.FedServers[i:])
p.FedServers[i] = &interfaces.Server{ChainID: identityChainID, Online: true}
//p.State.AddStatus(fmt.Sprintf("ProcessList.AddFedServer Server added at index %d %x at height %d", i, identityChainID.Bytes()[2:6], p.DBHeight))
p.MakeMap()
//p.State.AddStatus(fmt.Sprintf("PROCESSLIST.AddFedServer: Adding Server %x", identityChainID.Bytes()[3:8]))
return i
}
// Add the given serverChain to this processlist as an Audit Server, and return
// the server index number of the added server
func (p *ProcessList) AddAuditServer(identityChainID interfaces.IHash) int {
found, i := p.GetAuditServerIndexHash(identityChainID)
if found {
//p.State.AddStatus(fmt.Sprintf("ProcessList.AddAuditServer Server already there %x at height %d", identityChainID.Bytes()[2:6], p.DBHeight))
return i
}
// If a fed server, demote
fedFound, _ := p.GetFedServerIndexHash(identityChainID)
if fedFound {
//p.State.AddStatus(fmt.Sprintf("ProcessList.AddAuditServer Server %x was a fed server at height %d", identityChainID.Bytes()[2:6], p.DBHeight))
p.RemoveFedServerHash(identityChainID)
}
p.AuditServers = append(p.AuditServers, nil)
copy(p.AuditServers[i+1:], p.AuditServers[i:])
p.AuditServers[i] = &interfaces.Server{ChainID: identityChainID, Online: true}
//p.State.AddStatus(fmt.Sprintf("PROCESSLIST.AddAuditServer Server added at index %d %x at height %d", i, identityChainID.Bytes()[2:6], p.DBHeight))
return i
}
// Remove the given serverChain from this processlist's Federated Servers
func (p *ProcessList) RemoveFedServerHash(identityChainID interfaces.IHash) {
found, i := p.GetFedServerIndexHash(identityChainID)
if !found {
p.RemoveAuditServerHash(identityChainID) // SOF-201
return
}
p.FedServers = append(p.FedServers[:i], p.FedServers[i+1:]...)
p.MakeMap()
//p.State.AddStatus(fmt.Sprintf("PROCESSLIST.RemoveFedServer: Removing Server %x", identityChainID.Bytes()[3:8]))
}
// Remove the given serverChain from this processlist's Audit Servers
func (p *ProcessList) RemoveAuditServerHash(identityChainID interfaces.IHash) {
found, i := p.GetAuditServerIndexHash(identityChainID)
if !found {
return
}
p.AuditServers = append(p.AuditServers[:i], p.AuditServers[i+1:]...)
//p.State.AddStatus(fmt.Sprintf("PROCESSLIST.RemoveAuditServer: Removing Audit Server %x", identityChainID.Bytes()[3:8]))
}
// Given a server index, return the last Ack
func (p *ProcessList) GetAck(vmIndex int) *messages.Ack {
return p.GetAckAt(vmIndex, p.VMs[vmIndex].Height)
}
// Given a server index, return the last Ack
func (p *ProcessList) GetAckAt(vmIndex int, height int) *messages.Ack {
vm := p.VMs[vmIndex]
if height < 0 || height >= len(vm.ListAck) {
return nil
}
return vm.ListAck[height]
}
func (p ProcessList) HasMessage() bool {
for i := 0; i < len(p.FedServers); i++ {
if len(p.VMs[i].List) > 0 {
return true
}
}
return false
}
func (p *ProcessList) AddOldMsgs(m interfaces.IMsg) {
p.oldmsgslock.Lock()
defer p.oldmsgslock.Unlock()
p.OldMsgs[m.GetHash().Fixed()] = m
}
func (p *ProcessList) DeleteOldMsgs(key interfaces.IHash) {
p.oldmsgslock.Lock()
defer p.oldmsgslock.Unlock()
delete(p.OldMsgs, key.Fixed())
}
func (p *ProcessList) GetOldMsgs(key interfaces.IHash) interfaces.IMsg {
p.oldmsgslock.Lock()
defer p.oldmsgslock.Unlock()
return p.OldMsgs[key.Fixed()]
}
func (p *ProcessList) AddNewEBlocks(key interfaces.IHash, value interfaces.IEntryBlock) {
p.neweblockslock.Lock()
defer p.neweblockslock.Unlock()
p.NewEBlocks[key.Fixed()] = value
}
func (p *ProcessList) GetNewEBlocks(key interfaces.IHash) interfaces.IEntryBlock {
p.neweblockslock.Lock()
defer p.neweblockslock.Unlock()
return p.NewEBlocks[key.Fixed()]
}
func (p *ProcessList) DeleteEBlocks(key interfaces.IHash) {
p.neweblockslock.Lock()
defer p.neweblockslock.Unlock()
delete(p.NewEBlocks, key.Fixed())
}
func (p *ProcessList) AddNewEntry(key interfaces.IHash, value interfaces.IEntry) {
p.NewEntriesMutex.Lock()
defer p.NewEntriesMutex.Unlock()
p.NewEntries[key.Fixed()] = value
}
func (p *ProcessList) DeleteNewEntry(key interfaces.IHash) {
p.NewEntriesMutex.Lock()
defer p.NewEntriesMutex.Unlock()
delete(p.NewEntries, key.Fixed())
}
func (p *ProcessList) CurrentFault() *messages.FullServerFault {
if len(p.System.List) < 1 || len(p.System.List) <= p.System.Height {
return nil
}
return p.System.List[p.System.Height].(*messages.FullServerFault)
}
func (p *ProcessList) GetLeaderTimestamp() interfaces.Timestamp {
for _, msg := range p.VMs[0].List {
if msg.Type() == constants.DIRECTORY_BLOCK_SIGNATURE_MSG {
return msg.GetTimestamp()
}
}
return new(primitives.Timestamp)
}
func (p *ProcessList) ResetDiffSigTally() {
p.diffSigTally = 0
}
func (p *ProcessList) IncrementDiffSigTally() {
p.diffSigTally++
}
func (p *ProcessList) CheckDiffSigTally() bool {
// If the majority of VMs' signatures do not match our
// saved block, we discard that block from our database.
if p.diffSigTally > 0 && p.diffSigTally > (len(p.FedServers)/2) {
// p.State.DB.Delete([]byte(databaseOverlay.DIRECTORYBLOCK), p.State.ProcessLists.Lists[0].DirectoryBlock.GetKeyMR().Bytes())
return false
}
return true
}
func (p *ProcessList) GetRequest(now int64, vmIndex int, height int, waitSeconds int64) *Request {
r := new(Request)
r.wait = waitSeconds
r.vmIndex = vmIndex
r.vmheight = uint32(height)
if p.Requests[r.key()] == nil {
r.sent = now + 2000
p.Requests[r.key()] = r
} else {
r = p.Requests[r.key()]
}
return r
}
// Return the number of times we have tripped an ask for this request.
func (p *ProcessList) Ask(vmIndex int, height int, waitSeconds int64, tag int) int {
now := p.State.GetTimestamp().GetTimeMilli()
r := p.GetRequest(now, vmIndex, len(p.VMs[0].List), waitSeconds)
if r == nil {
return 0
}
if now-r.sent >= waitSeconds*1000+500 && p.State.inMsgQueue.Length() < constants.INMSGQUEUE_MED {
missingMsgRequest := messages.NewMissingMsg(p.State, r.vmIndex, p.DBHeight, r.vmheight)
// The System (handling full faults) is a special VM. Let's guess it first.
vm := &p.System
if vmIndex >= 0 {
// Ah, not the System VM, so let's look up the one we are really talking about.
vm = p.VMs[vmIndex]
}
for k := range p.Requests {
r2 := p.Requests[k]
if r2.vmIndex == vmIndex && int(r2.vmheight) < vm.Height {
delete(p.Requests, k)
}
}
missingMsgRequest.AddHeight(uint32(height))
// Okay, we are going to send one, so ask for all nil messages for this vm
for i := 0; i < len(vm.List); i++ {
if vm.List[i] == nil {
missingMsgRequest.AddHeight(uint32(i))
}
}
// Might as well as for the next message too. Won't hurt.
missingMsgRequest.AddHeight(uint32(len(vm.List)))
if vmIndex < 0 {
missingMsgRequest.SystemHeight = uint32(p.System.Height)
}
missingMsgRequest.SendOut(p.State, missingMsgRequest)
p.State.MissingRequestAskCnt++
r.sent = now
r.requestCnt++
}
return r.requestCnt
}
func getLeaderMin(p *ProcessList) int {
leaderMin := 0
for _, vm := range p.VMs {
if vm.LeaderMinute > leaderMin {
leaderMin = vm.LeaderMinute
}
}
if leaderMin >= 10 {
leaderMin = 0
}
leaderMin--
if leaderMin < 0 {
leaderMin = 0
}
return leaderMin
}
func (p *ProcessList) TrimVMList(height uint32, vmIndex int) {
if !(uint32(len(p.VMs[vmIndex].List)) > height) {
p.VMs[vmIndex].List = p.VMs[vmIndex].List[:height]
}
}
// Process messages and update our state.
func (p *ProcessList) Process(state *State) (progress bool) {
dbht := state.GetHighestSavedBlk()
if dbht >= p.DBHeight {
//p.State.AddStatus(fmt.Sprintf("ProcessList.Process: VM Height is %d and Saved height is %d", dbht, state.GetHighestSavedBlk()))
return true
}
state.PLProcessHeight = p.DBHeight
if len(p.System.List) >= p.System.Height {
systemloop:
for i, f := range p.System.List[p.System.Height:] {
if f == nil {
p.Ask(-1, i, 10, 100)
break systemloop
}
fault, ok := f.(*messages.FullServerFault)
if ok {
vm := p.VMs[fault.VMIndex]
if vm.Height < int(fault.Height) {
//p.State.AddStatus(fmt.Sprint("VM HEIGHT IS", vm.Height, "FH IS", fault.Height))
break systemloop
}
if !fault.Process(p.DBHeight, p.State) {
fault.SetAlreadyProcessed()
break systemloop
}
p.System.Height++
progress = true
}
}
}
for i := 0; i < len(p.FedServers); i++ {
vm := p.VMs[i]
if !p.State.Syncing {
markNoFault(p, i)
} else {
if !vm.Synced {
if vm.WhenFaulted == 0 {
markFault(p, i, 0)
}
} else {
if vm.FaultFlag == 0 {
markNoFault(p, i)
}
}
}
FaultCheck(p)
if vm.Height == len(vm.List) && p.State.Syncing && !vm.Synced {
// means that we are missing an EOM
p.Ask(i, vm.Height, 0, 1)
}
// If we haven't heard anything from a VM, ask for a message at the last-known height
if vm.Height == len(vm.List) {
p.Ask(i, vm.Height, 20, 2)
}
VMListLoop:
for j := vm.Height; j < len(vm.List); j++ {
if vm.List[j] == nil {
//p.State.AddStatus(fmt.Sprintf("ProcessList.go Process: Found nil list at vm %d vm height %d ", i, j))
p.Ask(i, j, 0, 3)
break VMListLoop
}
thisAck := vm.ListAck[j]
var expectedSerialHash interfaces.IHash
var err error
if vm.Height == 0 {
expectedSerialHash = thisAck.SerialHash
} else {
last := vm.ListAck[vm.Height-1]
expectedSerialHash, err = primitives.CreateHash(last.MessageHash, thisAck.MessageHash)
if err != nil {
vm.List[j] = nil
//p.State.AddStatus(fmt.Sprintf("ProcessList.go Process: Error computing serial hash at dbht: %d vm %d vm-height %d ", p.DBHeight, i, j))
p.Ask(i, j, 3, 4)
break VMListLoop
}
// compare the SerialHash of this acknowledgement with the
// expected serialHash (generated above)
if !expectedSerialHash.IsSameAs(thisAck.SerialHash) {
//p.State.AddStatus(fmt.Sprintf("processList.Process(): SerialHash fail: dbht: %d vm %d msg %s", p.DBHeight, i, vm.List[j]))
//fmt.Printf("dddd %20s %10s --- %10s %10x %10s %10x \n", "Conflict", p.State.FactomNodeName, "expected", expectedSerialHash.Bytes()[:3], "This", thisAck.Bytes()[:3])
//fmt.Printf("dddd Error detected on %s\nSerial Hash failure: Fed Server %d Leader ID %x List Ht: %d \nDetected on: %s\n",
// state.GetFactomNodeName(),
// i,
// p.FedServers[i].GetChainID().Bytes()[:3],
// j,
// vm.List[j].String())
//fmt.Printf("dddd Last Ack: %6x Last Serial: %6x\n", last.GetHash().Bytes()[:3], last.SerialHash.Bytes()[:3])
//fmt.Printf("dddd This Ack: %6x This Serial: %6x\n", thisAck.GetHash().Bytes()[:3], thisAck.SerialHash.Bytes()[:3])
//fmt.Printf("dddd Expected: %6x\n", expectedSerialHash.Bytes()[:3])
//fmt.Printf("dddd The message that didn't work: %s\n\n", vm.List[j].String())
// the SerialHash of this acknowledgment is incorrect
// according to this node's processList
//fault(p, i, 0, vm, 0, j, 2)
//p.State.AddStatus(fmt.Sprintf("ProcessList.go Process: SerialHash fails to match at dbht %d vm %d vm-height %d ", p.DBHeight, i, j))
p.State.Reset()
return
}
}
// So here is the deal. After we have processed a block, we have to allow the DirectoryBlockSignatures a chance to save
// to disk. Then we can insist on having the entry blocks.
diff := p.DBHeight - state.EntryDBHeightComplete
// Keep in mind, the process list is processing at a height one greater than the database. 1 is caught up. 2 is one behind.
// Until the first couple signatures are processed, we will be 2 behind.
if !p.State.WaitForEntries || (vm.LeaderMinute < 2 && diff <= 3) || diff <= 2 {
// If we can't process this entry (i.e. returns false) then we can't process any more.
p.NextHeightToProcess[i] = j + 1
msg := vm.List[j]
if msg.Process(p.DBHeight, state) { // Try and Process this entry
vm.heartBeat = 0
vm.Height = j + 1 // Don't process it again if the process worked.
progress = true
} else {
//p.State.AddStatus(fmt.Sprintf("processList.Process(): Could not process entry dbht: %d VM: %d msg: [[%s]]", p.DBHeight, i, msg.String()))
break VMListLoop // Don't process further in this list, go to the next.
}
} else {
// If we don't have the Entry Blocks (or we haven't processed the signatures) we can't do more.
// p.State.AddStatus(fmt.Sprintf("Can't do more: dbht: %d vm: %d vm-height: %d Entry Height: %d", p.DBHeight, i, j, state.EntryDBHeightComplete))
break VMListLoop
}
}
}
return
}
func (p *ProcessList) AddToSystemList(m interfaces.IMsg) bool {
// Make sure we have a list, and punt if we don't.
if p == nil {
p.State.Holding[m.GetRepeatHash().Fixed()] = m
return false
}
fullFault, ok := m.(*messages.FullServerFault)
if !ok {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Fail (not a FullFault) %s", m))
return false // Should never happen; Don't pass junk to be added to the System List
}
// If we have already processed past this fault, just ignore.
if p.System.Height > int(fullFault.SystemHeight) {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Fail (System.Height > fullFault.SystemHeight) (%d > %d) : %s",
// p.System.Height,
// int(fullFault.SystemHeight),
// fullFault.String()))
return false
}
// If the fault is in the future, hold it.
if p.System.Height < int(fullFault.SystemHeight) {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Holding (System.Height(%d) < fullFault.SystemHeight(%d)) : %s",
// p.System.Height,
// int(fullFault.SystemHeight),
// fullFault.String()))
p.State.Holding[m.GetRepeatHash().Fixed()] = m
return false
}
// If we are here, fullFault.SystemHeight == p.System.Height
if len(p.System.List) <= p.System.Height {
// Nothing in our list a this slot yet, so insert this FullFault message
p.System.List = append(p.System.List, fullFault)
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Success (append) : %s",
// fullFault.String()))
return true
}
// Something is in our SystemList at this height;
// We will prioritize the FullFault with the highest VMIndex
existingSystemFault, _ := p.System.List[p.System.Height].(*messages.FullServerFault)
if existingSystemFault.GetHash().IsSameAs(fullFault.GetHash()) {
if p.VMs[existingSystemFault.VMIndex].WhenFaulted > 0 {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Fail (already have) : %s",
// fullFault.String()))
return false
}
}
if existingSystemFault.HasEnoughSigs(p.State) && p.State.pledgedByAudit(existingSystemFault) {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Fail (existingFault is complete) : %s",
// existingSystemFault.String()))
return false
}
if fullFault.Priority(p.State) < existingSystemFault.Priority(p.State) {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Fail (priority %d < %d) :: Exist: %s /// New: %s",
// fullFault.Priority(p.State),
// existingSystemFault.Priority(p.State),
// existingSystemFault.String(),
// fullFault.String()))
return false
}
if existingSystemFault.SigTally(p.State) > fullFault.SigTally(p.State) {
if fullFault.GetCoreHash().IsSameAs(existingSystemFault.GetCoreHash()) {
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Fail (less sigs than existingFault's) (%d > %d) : %s",
// existingSystemFault.SigTally(p.State),
// fullFault.SigTally(p.State),
// fullFault.String()))
return false
}
}
//p.State.regularFullFaultExecution(fullFault, p)
p.System.List[p.System.Height] = fullFault
//p.State.AddStatus(fmt.Sprintf("FULL FAULT AddToSystemList Success (create) : %s sigs:%d",
// fullFault.String(), fullFault.SigTally(p.State)))
return true
}
func (p *ProcessList) AddToProcessList(ack *messages.Ack, m interfaces.IMsg) {
if p == nil {
return
}
m.PutAck(ack)
// If this is us, make sure we ignore (if old or in the ignore period) or die because two instances are running.
//
if !ack.Response && ack.LeaderChainID.IsSameAs(p.State.IdentityChainID) {
now := p.State.GetTimestamp()
if now.GetTimeSeconds()-ack.Timestamp.GetTimeSeconds() > 120 {
// Us and too old? Just ignore.
return
}
num := p.State.GetSalt(ack.Timestamp)
if num != ack.SaltNumber {
os.Stderr.WriteString(fmt.Sprintf("This ChainID %x\n", p.State.IdentityChainID.Bytes()))
os.Stderr.WriteString(fmt.Sprintf("This Salt %x\n", p.State.Salt.Bytes()[:8]))
os.Stderr.WriteString(fmt.Sprintf("This SaltNumber %x\n for this ack", num))
os.Stderr.WriteString(fmt.Sprintf("Ack ChainID %x\n", ack.LeaderChainID.Bytes()))
os.Stderr.WriteString(fmt.Sprintf("Ack Salt %x\n", ack.Salt))
os.Stderr.WriteString(fmt.Sprintf("Ack SaltNumber %x\n for this ack", ack.SaltNumber))
panic("There are two leaders configured with the same Identity in this network! This is a configuration problem!")
}
}
toss := func(hint string) {
fmt.Println("dddd TOSS in Process List", p.State.FactomNodeName, hint)
fmt.Println("dddd TOSS in Process List", p.State.FactomNodeName, ack.String())
fmt.Println("dddd TOSS in Process List", p.State.FactomNodeName, m.String())
delete(p.State.Holding, ack.GetHash().Fixed())
delete(p.State.Acks, ack.GetHash().Fixed())
}
now := p.State.GetTimestamp()
vm := p.VMs[ack.VMIndex]
if len(vm.List) > int(ack.Height) && vm.List[ack.Height] != nil {
_, isNew2 := p.State.Replay.Valid(constants.INTERNAL_REPLAY, m.GetRepeatHash().Fixed(), m.GetTimestamp(), now)
if !isNew2 {
toss("seen before, or too old")
return
}
}
if ack.DBHeight != p.DBHeight {
panic(fmt.Sprintf("Ack is wrong height. Expected: %d Ack: ", p.DBHeight))
return
}
if len(vm.List) > int(ack.Height) && vm.List[ack.Height] != nil {
if vm.List[ack.Height].GetMsgHash().IsSameAs(m.GetMsgHash()) {
fmt.Printf("dddd %-30s %10s %s\n", "xxxxxxxxx PL Duplicate ", p.State.GetFactomNodeName(), m.String())
fmt.Printf("dddd %-30s %10s %s\n", "xxxxxxxxx PL Duplicate ", p.State.GetFactomNodeName(), ack.String())
fmt.Printf("dddd %-30s %10s %s\n", "xxxxxxxxx PL Duplicate vm", p.State.GetFactomNodeName(), vm.List[ack.Height].String())
fmt.Printf("dddd %-30s %10s %s\n", "xxxxxxxxx PL Duplicate vm", p.State.GetFactomNodeName(), vm.ListAck[ack.Height].String())
toss("2")
return
}
vm.List[ack.Height] = nil
return
}
// From this point on, we consider the transaction recorded. If we detect it has already been
// recorded, then we still treat it as if we recorded it.
vm.heartBeat = 0 // We have heard from this VM
// We have already tested and found m to be a new message. We now record its hashes so later, we
// can detect that it has been recorded. We don't care about the results of IsTSValid_ at this point.
p.State.Replay.IsTSValid_(constants.INTERNAL_REPLAY, m.GetRepeatHash().Fixed(), m.GetTimestamp(), now)
p.State.Replay.IsTSValid_(constants.INTERNAL_REPLAY, m.GetMsgHash().Fixed(), m.GetTimestamp(), now)
delete(p.State.Acks, ack.GetHash().Fixed())
delete(p.State.Holding, m.GetMsgHash().Fixed())
// Both the ack and the message hash to the same GetHash()
m.SetLocal(false)
ack.SetLocal(false)
ack.SetPeer2Peer(false)