-
Notifications
You must be signed in to change notification settings - Fork 19
/
snapshot.go
131 lines (113 loc) · 4.27 KB
/
snapshot.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package tools
import (
"encoding/json"
"errors"
"fmt"
"code.vegaprotocol.io/vega/core/config"
"code.vegaprotocol.io/vega/paths"
"code.vegaprotocol.io/vega/vegatools/snapshotdb"
tmconfig "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/store"
"github.com/spf13/viper"
)
type snapshotCmd struct {
config.OutputFlag
DBPath string `description:"path to snapshot state data" long:"db-path" short:"d"`
SnapshotContentsPath string `description:"path to file where to write the content of a snapshot" long:"snapshot-contents" short:"c"`
BlockHeight uint64 `description:"block-height of requested snapshot" long:"block-height" short:"b"`
SetProtocolUpgrade bool `description:"set protocol-upgrade flag to true in the latest snapshot" long:"set-pup" short:"p"`
TendermintHome string `description:"tendermint home directory, if set will print the last processed block height" long:"tendermint-home"`
}
func getLastProcessedBlock(homeDir string) (int64, error) {
conf := tmconfig.DefaultConfig()
if err := viper.Unmarshal(conf); err != nil {
return 0, err
}
conf.SetRoot(homeDir)
// lets get the last processed block from tendermint
blockStoreDB, err := tmconfig.DefaultDBProvider(&tmconfig.DBContext{ID: "blockstore", Config: conf})
if err != nil {
return 0, err
}
blockStore := store.NewBlockStore(blockStoreDB)
return blockStore.Height(), nil
}
func (opts *snapshotCmd) Execute(_ []string) error {
if opts.SnapshotContentsPath != "" && opts.BlockHeight == 0 {
return errors.New("must specify --block-height when using --write-payload")
}
db := opts.DBPath
if opts.DBPath == "" {
vegaPaths := paths.New(rootCmd.VegaHome)
db = vegaPaths.StatePathFor(paths.SnapshotStateHome)
}
if opts.SetProtocolUpgrade {
err := snapshotdb.SetProtocolUpgrade(paths.New(rootCmd.VegaHome))
return err
}
if opts.SnapshotContentsPath != "" {
fmt.Printf("finding payloads for block-height %d...\n", opts.BlockHeight)
err := snapshotdb.SavePayloadsToFile(db, opts.SnapshotContentsPath, opts.BlockHeight)
if err != nil {
return err
}
fmt.Printf("payloads saved to '%s'\n", opts.SnapshotContentsPath)
return nil
}
snapshots, invalid, err := snapshotdb.SnapshotData(db, opts.BlockHeight)
if err != nil {
return err
}
var lastProcessedBlock int64
if opts.TendermintHome != "" {
if lastProcessedBlock, err = getLastProcessedBlock(opts.TendermintHome); err != nil {
return err
}
}
if opts.Output.IsJSON() {
o := struct {
Snapshots []snapshotdb.Data `json:"snapshots"`
Invalid []snapshotdb.Data `json:"invalidSnapshots,omitempty"`
LastProcessedBlock int64 `json:"lastProcessedBlock,omitempty"`
}{
Snapshots: snapshots,
Invalid: invalid,
LastProcessedBlock: lastProcessedBlock,
}
b, err := json.Marshal(o)
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
if lastProcessedBlock != 0 {
fmt.Printf("\nLast processed block: %d\n", lastProcessedBlock)
}
fmt.Println("\nSnapshots available:", len(snapshots))
for _, snap := range snapshots {
fmt.Printf("\tHeight: %d, Version: %d, Size %d, Hash: %s\n", snap.Height, snap.Version, snap.Size, snap.Hash)
}
if len(invalid) == 0 {
return nil
}
fmt.Println("Invalid snapshots:", len(invalid))
for _, snap := range invalid {
fmt.Printf("\tVersion: %d, Size %d, Hash: %s\n", snap.Version, snap.Size, snap.Hash)
}
return nil
}