/
forkless_cause.go
103 lines (89 loc) · 3.47 KB
/
forkless_cause.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
package vector
import (
"github.com/Fantom-foundation/lachesis-ex/hash"
"github.com/Fantom-foundation/lachesis-ex/inter/idx"
)
type kv struct {
a, b hash.Event
}
// ForklessCause calculates "sufficient coherence" between the events.
// The A.HighestBefore array remembers the sequence number of the last
// event by each validator that is an ancestor of A. The array for
// B.LowestAfter remembers the sequence number of the earliest
// event by each validator that is a descendant of B. Compare the two arrays,
// and find how many elements in the A.HighestBefore array are greater
// than or equal to the corresponding element of the B.LowestAfter
// array. If there are more than 2n/3 such matches, then the A and B
// have achieved sufficient coherency.
//
// If B1 and B2 are forks, then they cannot BOTH forkless-cause any specific event A,
// unless more than 1/3W are Byzantine.
// This great property is the reason why this function exists,
// providing the base for the BFT algorithm.
func (vi *Index) ForklessCause(aID, bID hash.Event) bool {
if res, ok := vi.cache.ForklessCause.Get(kv{aID, bID}); ok {
return res.(bool)
}
res := vi.forklessCause(aID, bID)
vi.cache.ForklessCause.Add(kv{aID, bID}, res)
return res
}
func (vi *Index) forklessCause(aID, bID hash.Event) bool {
vi.initBranchesInfo()
// get events by hash
a := vi.GetHighestBeforeSeq(aID)
if a == nil {
vi.Log.Crit("Event A not found", "event", aID.String())
return false
}
// check A doesn't observe any forks from B
if vi.atLeastOneFork() {
bBranchID := vi.getEventBranchID(bID)
if a.Get(bBranchID).IsForkDetected() { // B is observed as cheater by A
return false
}
}
// check A observes that {QUORUM} non-cheater-validators observe B
b := vi.GetLowestAfterSeq(bID)
if b == nil {
vi.Log.Crit("Event B not found", "event", bID.String())
return false
}
yes := vi.validators.NewCounter()
// calculate forkless causing using the indexes
for branchIDint, creatorIdx := range vi.bi.BranchIDCreatorIdxs {
branchID := idx.Validator(branchIDint)
// bLowestAfter := vi.GetLowestAfterSeq_(bID, branchID) // lowest event from creator on branchID, which observes B
bLowestAfter := b.Get(branchID) // lowest event from creator on branchID, which observes B
aHighestBefore := a.Get(branchID) // highest event from creator, observed by A
// if lowest event from branchID which observes B <= highest from branchID observed by A
// then {highest from branchID observed by A} observes B
if bLowestAfter <= aHighestBefore.Seq && bLowestAfter != 0 && !aHighestBefore.IsForkDetected() {
// we may count the same creator multiple times (on different branches)!
// so not every call increases the counter
yes.CountByIdx(creatorIdx)
}
}
return yes.HasQuorum()
}
// NoCheaters excludes events which are observed by selfParents as cheaters.
// Called by emitter to exclude cheater's events from potential parents list.
func (vi *Index) NoCheaters(selfParent *hash.Event, options hash.Events) hash.Events {
if selfParent == nil {
return options
}
vi.initBranchesInfo()
// no need to merge, because every branch is marked by IsForkDetected if fork is observed
highest := vi.GetHighestBeforeSeq(*selfParent)
filtered := make(hash.Events, 0, len(options))
for _, id := range options {
header := vi.getEvent(id)
if header == nil {
vi.Log.Crit("Event not found", "id", id.String())
}
if !highest.Get(vi.validators.GetIdx(header.Creator)).IsForkDetected() {
filtered.Add(id)
}
}
return filtered
}