-
Notifications
You must be signed in to change notification settings - Fork 212
/
recover.go
212 lines (194 loc) · 6.05 KB
/
recover.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
package tortoise
import (
"context"
"errors"
"fmt"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/beacons"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)
// Recover tortoise state from database.
func Recover(
ctx context.Context,
db sql.Executor,
atxdata *atxsdata.Data,
current types.LayerID,
opts ...Opt,
) (*Tortoise, error) {
trtl, err := New(atxdata, opts...)
if err != nil {
return nil, err
}
last, err := ballots.LatestLayer(db)
if err != nil {
return nil, fmt.Errorf("failed to load latest known layer: %w", err)
}
applied, err := layers.GetLastApplied(db)
if err != nil {
return nil, fmt.Errorf("get last applied: %w", err)
}
start := types.GetEffectiveGenesis() + 1
if size := trtl.cfg.WindowSizeLayers(applied); applied > size {
// we want to emulate the same condition as during genesis with one difference.
// genesis starts with zero opinion (aggregated hash) - see computeOpinion method.
// but in this case first processed layer should use non-zero opinion of the the previous layer.
window := applied - size
// we start tallying votes from the first layer of the epoch to guarantee that we load reference ballots.
// reference ballots track beacon and eligibilities
window = window.GetEpoch().FirstLayer()
if window > start {
prev, err1 := layers.GetAggregatedHash(db, window-1)
opinion, err2 := layers.GetAggregatedHash(db, window)
if err1 == nil && err2 == nil {
// tortoise will need reference to previous layer
trtl.RecoverFrom(window, opinion, prev)
start = window
}
}
}
malicious, err := identities.GetMalicious(db)
if err != nil {
return nil, fmt.Errorf("recover malicious %w", err)
}
for _, id := range malicious {
trtl.OnMalfeasance(id)
}
if types.GetEffectiveGenesis() != types.FirstEffectiveGenesis() {
// need to load the golden atxs after a checkpoint recovery
if err := recoverEpoch(types.GetEffectiveGenesis().Add(1).GetEpoch(), trtl, db, atxdata); err != nil {
return nil, err
}
}
valid, err := blocks.LastValid(db)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, fmt.Errorf("get last valid: %w", err)
}
if err == nil {
trtl.UpdateVerified(valid)
}
trtl.UpdateLastLayer(last)
for lid := start; !lid.After(last); lid = lid.Add(1) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if err := RecoverLayer(ctx, trtl, db, atxdata, lid, trtl.OnRecoveredBallot); err != nil {
return nil, fmt.Errorf("failed to load tortoise state at layer %d: %w", lid, err)
}
}
// load activations from future epochs that are not yet referenced by the ballots
atxsEpoch, err := atxs.LatestEpoch(db)
if err != nil {
return nil, fmt.Errorf("failed to load latest epoch: %w", err)
}
atxsEpoch++ // recoverEpoch expects target epoch
if last.GetEpoch() != atxsEpoch {
for eid := last.GetEpoch() + 1; eid <= atxsEpoch; eid++ {
if err := recoverEpoch(eid, trtl, db, atxdata); err != nil {
return nil, err
}
}
}
last = min(last, current)
if last < start {
return trtl, nil
}
trtl.TallyVotes(ctx, last)
// find topmost layer that was already applied with same result
// and reset pending so that result for that layer is not returned
for prev := valid; prev >= start; prev-- {
opinion, err := layers.GetAggregatedHash(db, prev)
if err == nil && opinion != types.EmptyLayerHash {
if trtl.OnApplied(prev, opinion) {
break
}
}
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, fmt.Errorf("check opinion %w", err)
}
}
return trtl, nil
}
func recoverEpoch(target types.EpochID, trtl *Tortoise, db sql.Executor, atxdata *atxsdata.Data) error {
atxdata.IterateInEpoch(target, func(id types.ATXID, atx *atxsdata.ATX) {
trtl.OnAtx(target, id, atx)
})
beacon, err := beacons.Get(db, target)
if err == nil && beacon != types.EmptyBeacon {
trtl.OnBeacon(target, beacon)
}
return nil
}
type ballotFunc func(*types.BallotTortoiseData)
func RecoverLayer(
ctx context.Context,
trtl *Tortoise,
db sql.Executor,
atxdata *atxsdata.Data,
lid types.LayerID,
onBallot ballotFunc,
) error {
if lid.FirstInEpoch() {
if err := recoverEpoch(lid.GetEpoch(), trtl, db, atxdata); err != nil {
return err
}
}
blocksrst, err := blocks.Layer(db, lid)
if err != nil {
return err
}
results := map[types.BlockHeader]bool{}
for _, block := range blocksrst {
valid, err := blocks.IsValid(db, block.ID())
if err != nil && errors.Is(err, sql.ErrNotFound) {
return err
}
results[block.ToVote()] = valid
}
var hareResult *types.BlockID
if trtl.WithinHdist(lid) {
hare, err := certificates.GetHareOutput(db, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return err
}
if err == nil {
hareResult = &hare
}
}
trtl.OnRecoveredBlocks(lid, results, hareResult)
// tortoise votes according to the hare only within hdist (protocol parameter).
// also node is free to prune certificates outside hdist to minimize space usage.
// NOTE(dshulyak) we loaded information about malicious identities earlier.
ballotsrst, err := ballots.LayerNoMalicious(db, lid)
if err != nil {
return err
}
// NOTE(dshulyak) it is done in two steps so that if ballot from the same layer was used
// as reference or base ballot we will be able to decode it.
// it might be possible to invalidate such ballots, but until then this is required
for _, ballot := range ballotsrst {
if ballot.EpochData != nil {
onBallot(ballot.ToTortoiseData())
}
}
for _, ballot := range ballotsrst {
if ballot.EpochData == nil {
onBallot(ballot.ToTortoiseData())
}
}
coin, err := layers.GetWeakCoin(db, lid)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return err
} else if err == nil {
trtl.OnWeakCoin(lid, coin)
}
return nil
}