-
Notifications
You must be signed in to change notification settings - Fork 170
/
cmd.go
73 lines (55 loc) · 2.01 KB
/
cmd.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
package snapshot
import (
"os"
"path/filepath"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/onflow/flow-go/cmd/util/cmd/common"
"github.com/onflow/flow-go/engine/common/rpc/convert"
)
var (
flagDatadir string
flagHeight uint64
)
// This command can be used to retrieve a snapshot of the protocol state at any finalized height.
// The resulting snapshot file can then be used to bootstrap another node's protocol state.
// This can be useful for recovering a node which is very far behind, or has a corrupted database
// that cannot be recovered otherwise.
//
// The recommended usage is to use a height which is just before the most recent epoch transition.
// This way the node will have a root block below the current epoch, and will sync all blocks
// from the current epoch.
var Cmd = &cobra.Command{
Use: "snapshot",
Short: "Retrieves a protocol state snapshot from the database, which can be used to instantiate another node",
Run: run,
}
func init() {
Cmd.Flags().StringVar(&flagDatadir, "datadir", "",
"directory that stores the protocol state")
_ = Cmd.MarkFlagRequired("datadir")
Cmd.Flags().Uint64Var(&flagHeight, "height", 0, "the height of the snapshot to retrieve")
_ = Cmd.MarkFlagRequired("height")
}
func run(*cobra.Command, []string) {
db := common.InitStorage(flagDatadir)
defer db.Close()
storages := common.InitStorages(db)
state, err := common.InitProtocolState(db, storages)
if err != nil {
log.Fatal().Err(err).Msg("could not init protocol state")
}
log := log.With().Uint64("block_height", flagHeight).Logger()
snap := state.AtHeight(flagHeight)
encoded, err := convert.SnapshotToBytes(snap)
if err != nil {
log.Fatal().Err(err).Msg("failed to encode snapshot")
}
dir := filepath.Join(".", "root-protocol-state-snapshot.json")
log.Info().Msgf("going to write snapshot to %s", dir)
err = os.WriteFile(dir, encoded, 0600)
if err != nil {
log.Fatal().Err(err).Msg("failed to write snapshot")
}
log.Info().Msgf("successfully wrote snapshot to %s", dir)
}