-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathcasts.go
More file actions
93 lines (77 loc) · 2.07 KB
/
Copy pathcasts.go
File metadata and controls
93 lines (77 loc) · 2.07 KB
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
// Copyright (c) 2022 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package bft
import (
"bytes"
"sort"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/thor"
)
// casts stores the master's overall casts, maintaining the map of quality to checkpoint.
type casts map[thor.Bytes32]uint32
func (engine *Engine) newCasts() error {
c := make(casts)
finalized := engine.Finalized()
heads, err := engine.repo.ScanHeads(block.Number(finalized))
if err != nil {
return err
}
for _, head := range heads {
chain := engine.repo.NewChain(head)
cur := head
for {
sum, err := engine.repo.GetBlockSummary(cur)
if err != nil {
return err
}
signer, _ := sum.Header.Signer()
if signer == engine.master {
st, err := engine.computeState(sum)
if err != nil {
return err
}
checkpoint, err := chain.GetBlockID(getCheckPoint(sum.Header.Number()))
if err != nil {
return err
}
if quality, ok := c[checkpoint]; !ok || quality < st.Quality {
c[checkpoint] = st.Quality
}
break
}
if sum.Header.Number() <= block.Number(finalized) {
break
}
cur = sum.Header.ParentID()
}
}
engine.casts = c
return nil
}
// Slice dumps the casts that is after finalized into slice.
func (ca casts) Slice(finalized thor.Bytes32) []struct {
checkpoint thor.Bytes32
quality uint32
} {
list := make([]struct {
checkpoint thor.Bytes32
quality uint32
}, 0, len(ca))
for checkpoint, quality := range ca {
if block.Number(checkpoint) >= block.Number(finalized) {
list = append(list, struct {
checkpoint thor.Bytes32
quality uint32
}{checkpoint: checkpoint, quality: quality})
}
}
sort.Slice(list, func(i, j int) bool {
return bytes.Compare(list[i].checkpoint.Bytes(), list[j].checkpoint.Bytes()) > 0
})
return list
}
// Mark marks the master's cast.
func (ca casts) Mark(checkpoint thor.Bytes32, quality uint32) {
ca[checkpoint] = quality
}