-
Notifications
You must be signed in to change notification settings - Fork 210
/
recovery.go
449 lines (423 loc) · 12.3 KB
/
recovery.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
package checkpoint
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"path/filepath"
"github.com/spf13/afero"
"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/accounts"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/localsql"
"github.com/spacemeshos/go-spacemesh/sql/localsql/nipost"
"github.com/spacemeshos/go-spacemesh/sql/poets"
"github.com/spacemeshos/go-spacemesh/sql/recovery"
)
const recoveryDir = "recovery"
type Config struct {
Uri string `mapstructure:"recovery-uri"`
Restore uint32 `mapstructure:"recovery-layer"`
// set to false if atxs are not compatible before and after the checkpoint recovery.
PreserveOwnAtx bool `mapstructure:"preserve-own-atx"`
}
func DefaultConfig() Config {
return Config{
PreserveOwnAtx: true,
}
}
type RecoverConfig struct {
GoldenAtx types.ATXID
DataDir string
DbFile string
LocalDbFile string
PreserveOwnAtx bool
NodeID types.NodeID
Uri string
Restore types.LayerID
}
func RecoveryDir(dataDir string) string {
return filepath.Join(dataDir, recoveryDir)
}
func RecoveryFilename(dataDir, base string, restore types.LayerID) string {
return filepath.Join(RecoveryDir(dataDir), fmt.Sprintf("%s-restore-%d", base, restore.Uint32()))
}
func copyToLocalFile(
ctx context.Context,
logger log.Log,
fs afero.Fs,
dataDir, uri string,
restore types.LayerID,
) (string, error) {
parsed, err := url.Parse(uri)
if err != nil {
return "", fmt.Errorf("%w: parse recovery URI %v", err, uri)
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return "", fmt.Errorf("%w: %s", ErrUrlSchemeNotSupported, uri)
}
if bdir, err := backupRecovery(fs, RecoveryDir(dataDir)); err != nil {
return "", err
} else if bdir != "" {
logger.With().Info("old recovery data backed up",
log.Context(ctx),
log.String("dir", bdir),
)
}
dst := RecoveryFilename(dataDir, filepath.Base(parsed.String()), restore)
if err = httpToLocalFile(ctx, parsed, fs, dst); err != nil {
return "", err
}
logger.With().Info("checkpoint data persisted",
log.Context(ctx),
log.String("file", dst),
)
return dst, nil
}
type PreservedData struct {
Deps []*types.VerifiedActivationTx
Proofs []*types.PoetProofMessage
}
func Recover(
ctx context.Context,
logger log.Log,
fs afero.Fs,
cfg *RecoverConfig,
) (*PreservedData, error) {
db, err := sql.Open("file:" + filepath.Join(cfg.DataDir, cfg.DbFile))
if err != nil {
return nil, fmt.Errorf("open old database: %w", err)
}
defer db.Close()
localDB, err := localsql.Open("file:" + filepath.Join(cfg.DataDir, cfg.LocalDbFile))
if err != nil {
return nil, fmt.Errorf("open old local database: %w", err)
}
defer localDB.Close()
preserve, err := RecoverWithDb(ctx, logger, db, localDB, fs, cfg)
switch {
case errors.Is(err, ErrCheckpointNotFound):
logger.With().Info("no checkpoint file available. not recovering",
log.String("uri", cfg.Uri),
)
return nil, nil
case err != nil:
return nil, err
}
return preserve, nil
}
func RecoverWithDb(
ctx context.Context,
logger log.Log,
db *sql.Database,
localDB *localsql.Database,
fs afero.Fs,
cfg *RecoverConfig,
) (*PreservedData, error) {
oldRestore, err := recovery.CheckpointInfo(db)
if err != nil {
return nil, fmt.Errorf("get last checkpoint: %w", err)
}
if oldRestore >= cfg.Restore {
types.SetEffectiveGenesis(oldRestore.Uint32() - 1)
return nil, nil
}
if err = fs.RemoveAll(filepath.Join(cfg.DataDir, bootstrap.DirName)); err != nil {
return nil, fmt.Errorf("remove old bootstrap data: %w", err)
}
logger.With().Info("recover from uri", log.String("uri", cfg.Uri))
cpfile, err := copyToLocalFile(ctx, logger, fs, cfg.DataDir, cfg.Uri, cfg.Restore)
if err != nil {
return nil, err
}
return recoverFromLocalFile(ctx, logger, db, localDB, fs, cfg, cpfile)
}
type recoverydata struct {
accounts []*types.Account
atxs []*atxs.CheckpointAtx
}
func recoverFromLocalFile(
ctx context.Context,
logger log.Log,
db *sql.Database,
localDB *localsql.Database,
fs afero.Fs,
cfg *RecoverConfig,
file string,
) (*PreservedData, error) {
logger.With().Info("recovering from checkpoint file", log.String("file", file))
newGenesis := cfg.Restore - 1
data, err := checkpointData(fs, file, newGenesis)
if err != nil {
return nil, err
}
logger.With().Info("recovery data contains",
log.Int("num accounts", len(data.accounts)),
log.Int("num atxs", len(data.atxs)),
)
deps, proofs, err := collectOwnAtxDeps(logger, db, localDB, cfg, data)
if err != nil {
logger.With().Error("failed to collect deps for own atx", log.Err(err))
// continue to recover from checkpoint despite failure to preserve own atx
} else if len(deps) > 0 {
logger.With().Info("collected own atx deps",
log.Context(ctx),
log.Int("own atx deps", len(deps)),
)
}
if err := db.Close(); err != nil {
return nil, fmt.Errorf("close old db: %w", err)
}
// all is ready. backup the old data and create new.
backupDir, err := backupOldDb(fs, cfg.DataDir, cfg.DbFile)
if err != nil {
return nil, err
}
logger.With().Info("backed up old database",
log.Context(ctx),
log.String("backup dir", backupDir),
)
newdb, err := sql.Open("file:" + filepath.Join(cfg.DataDir, cfg.DbFile))
if err != nil {
return nil, fmt.Errorf("open sqlite db %w", err)
}
defer newdb.Close()
logger.With().Info("populating new database",
log.Context(ctx),
log.Int("num accounts", len(data.accounts)),
log.Int("num atxs", len(data.atxs)),
)
if err = newdb.WithTx(ctx, func(tx *sql.Tx) error {
for _, acct := range data.accounts {
if err = accounts.Update(tx, acct); err != nil {
return fmt.Errorf("restore account snapshot: %w", err)
}
logger.With().Info("account stored",
log.Context(ctx),
acct.Address,
log.Uint64("nonce", acct.NextNonce),
log.Uint64("balance", acct.Balance),
)
}
for _, catx := range data.atxs {
if err = atxs.AddCheckpointed(tx, catx); err != nil {
return fmt.Errorf("add checkpoint atx %s: %w", catx.ID.String(), err)
}
logger.With().Info("checkpoint atx saved",
log.Context(ctx),
catx.ID,
catx.SmesherID,
)
}
if err = recovery.SetCheckpoint(tx, cfg.Restore); err != nil {
return fmt.Errorf("save checkppoint info: %w", err)
}
return nil
}); err != nil {
return nil, err
}
if _, err = backupRecovery(fs, RecoveryDir(cfg.DataDir)); err != nil {
return nil, err
}
types.SetEffectiveGenesis(newGenesis.Uint32())
logger.With().Info("effective genesis reset for recovery",
log.Context(ctx),
types.GetEffectiveGenesis(),
)
var preserve *PreservedData
if len(deps) > 0 {
preserve = &PreservedData{Deps: deps, Proofs: proofs}
}
return preserve, nil
}
func checkpointData(fs afero.Fs, file string, newGenesis types.LayerID) (*recoverydata, error) {
data, err := afero.ReadFile(fs, file)
if err != nil {
return nil, fmt.Errorf("%w: read recovery file %v", err, file)
}
if err = ValidateSchema(data); err != nil {
return nil, err
}
var checkpoint types.Checkpoint
if err = json.Unmarshal(data, &checkpoint); err != nil {
return nil, fmt.Errorf("%w: unmarshal checkpoint from %v", err, file)
}
if checkpoint.Version != SchemaVersion {
return nil, fmt.Errorf("expected version %v, got %v", SchemaVersion, checkpoint.Version)
}
allAccts := make([]*types.Account, 0, len(checkpoint.Data.Accounts))
for _, acct := range checkpoint.Data.Accounts {
a := types.Account{
Layer: newGenesis,
NextNonce: acct.Nonce,
Balance: acct.Balance,
State: acct.State,
}
copy(a.Address[:], acct.Address)
if acct.Template != nil {
var tmplAddr types.Address
copy(tmplAddr[:], acct.Template[:])
a.TemplateAddress = &tmplAddr
}
allAccts = append(allAccts, &a)
}
allAtxs := make([]*atxs.CheckpointAtx, 0, len(checkpoint.Data.Atxs))
for _, atx := range checkpoint.Data.Atxs {
var catx atxs.CheckpointAtx
catx.ID = types.ATXID(types.BytesToHash(atx.ID))
catx.Epoch = types.EpochID(atx.Epoch)
catx.CommitmentATX = types.ATXID(types.BytesToHash(atx.CommitmentAtx))
catx.SmesherID = types.BytesToNodeID(atx.PublicKey)
catx.NumUnits = atx.NumUnits
catx.VRFNonce = types.VRFPostIndex(atx.VrfNonce)
catx.BaseTickHeight = atx.BaseTickHeight
catx.TickCount = atx.TickCount
catx.Sequence = atx.Sequence
copy(catx.Coinbase[:], atx.Coinbase)
allAtxs = append(allAtxs, &catx)
}
return &recoverydata{
accounts: allAccts,
atxs: allAtxs,
}, nil
}
func collectOwnAtxDeps(
logger log.Log,
db *sql.Database,
localDB *localsql.Database,
cfg *RecoverConfig,
data *recoverydata,
) ([]*types.VerifiedActivationTx, []*types.PoetProofMessage, error) {
if !cfg.PreserveOwnAtx {
return nil, nil, nil
}
atxid, err := atxs.GetLastIDByNodeID(db, cfg.NodeID)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, nil, fmt.Errorf("query own last atx id: %w", err)
}
var ref types.ATXID
var own bool
if atxid != types.EmptyATXID {
ref = atxid
logger.With().Debug("found own atx", log.Stringer("own atx", ref))
own = true
}
// check for if miner is building any atx
nipostCh, _ := nipost.Challenge(localDB, cfg.NodeID)
if ref == types.EmptyATXID {
if nipostCh == nil {
return nil, nil, nil
}
if nipostCh.CommitmentATX != nil {
ref = *nipostCh.CommitmentATX
}
}
all := map[types.ATXID]struct{}{cfg.GoldenAtx: {}, types.EmptyATXID: {}}
for _, catx := range data.atxs {
all[catx.ID] = struct{}{}
}
var (
deps []*types.VerifiedActivationTx
proofs []*types.PoetProofMessage
)
if ref != types.EmptyATXID {
logger.With().Info("collecting atx and deps",
ref,
log.Bool("own", own),
)
deps, proofs, err = collectDeps(db, cfg.GoldenAtx, ref, all)
if err != nil {
return nil, nil, err
}
}
if nipostCh != nil {
logger.With().Info("collecting pending atx and deps",
log.Object("nipost", nipostCh),
)
// any previous atx in nipost should already be captured earlier
// we only care about positioning atx here
deps2, proofs2, err := collectDeps(db, cfg.GoldenAtx, nipostCh.PositioningATX, all)
if err != nil {
return nil, nil, fmt.Errorf("deps from nipost positioning atx (%v): %w", nipostCh.PositioningATX, err)
}
deps = append(deps, deps2...)
proofs = append(proofs, proofs2...)
}
return deps, proofs, nil
}
func collectDeps(
db *sql.Database,
goldenAtxId types.ATXID,
ref types.ATXID,
all map[types.ATXID]struct{},
) ([]*types.VerifiedActivationTx, []*types.PoetProofMessage, error) {
var deps []*types.VerifiedActivationTx
if err := collect(db, goldenAtxId, ref, all, &deps); err != nil {
return nil, nil, err
}
proofs, err := poetProofs(db, deps)
if err != nil {
return nil, nil, err
}
return deps, proofs, nil
}
func collect(
db *sql.Database,
goldenAtxID types.ATXID,
ref types.ATXID,
all map[types.ATXID]struct{},
deps *[]*types.VerifiedActivationTx,
) error {
if _, ok := all[ref]; ok {
return nil
}
atx, err := atxs.Get(db, ref)
if err != nil {
return fmt.Errorf("get ref atx: %w", err)
}
if atx.Golden() {
return fmt.Errorf("atx %v belong to previous snapshot. cannot be preserved", ref)
}
if atx.CommitmentATX != nil {
if err = collect(db, goldenAtxID, *atx.CommitmentATX, all, deps); err != nil {
return err
}
} else {
commitment, err := atxs.CommitmentATX(db, atx.SmesherID)
if err != nil {
return fmt.Errorf("get commitment for ref atx %v: %w", ref, err)
}
if err := collect(db, goldenAtxID, commitment, all, deps); err != nil {
return err
}
}
if err = collect(db, goldenAtxID, atx.PrevATXID, all, deps); err != nil {
return err
}
if err = collect(db, goldenAtxID, atx.PositioningATX, all, deps); err != nil {
return err
}
*deps = append(*deps, atx)
all[ref] = struct{}{}
return nil
}
func poetProofs(db *sql.Database, vatxs []*types.VerifiedActivationTx) ([]*types.PoetProofMessage, error) {
var proofs []*types.PoetProofMessage
for _, vatx := range vatxs {
proof, err := poets.Get(db, types.PoetProofRef(vatx.GetPoetProofRef()))
if err != nil {
return nil, fmt.Errorf("get poet proof (%v): %w", vatx.ID(), err)
}
var msg types.PoetProofMessage
if err := codec.Decode(proof, &msg); err != nil {
return nil, fmt.Errorf("decode poet proof (%v): %w", vatx.ID(), err)
}
proofs = append(proofs, &msg)
}
return proofs, nil
}