Skip to content

Commit 616841b

Browse files
chore: small snapshot commands & docs improvement (backport #16404) (#16408)
Co-authored-by: Julien Robert <[email protected]>
1 parent 2cd72b7 commit 616841b

9 files changed

Lines changed: 51 additions & 26 deletions

File tree

client/snapshot/cmd.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ func Cmd(appCreator servertypes.AppCreator) *cobra.Command {
1010
cmd := &cobra.Command{
1111
Use: "snapshots",
1212
Short: "Manage local snapshots",
13-
Long: "Manage local snapshots",
1413
}
1514
cmd.AddCommand(
1615
ListSnapshotsCmd,

client/snapshot/export.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package snapshot
22

33
import (
4-
"fmt"
5-
64
"github.com/cosmos/cosmos-sdk/server"
75
servertypes "github.com/cosmos/cosmos-sdk/server/types"
86
"github.com/spf13/cobra"
@@ -33,15 +31,15 @@ func ExportSnapshotCmd(appCreator servertypes.AppCreator) *cobra.Command {
3331
height = app.CommitMultiStore().LastCommitID().Version
3432
}
3533

36-
fmt.Printf("Exporting snapshot for height %d\n", height)
34+
cmd.Printf("Exporting snapshot for height %d\n", height)
3735

3836
sm := app.SnapshotManager()
3937
snapshot, err := sm.Create(uint64(height))
4038
if err != nil {
4139
return err
4240
}
4341

44-
fmt.Printf("Snapshot created at height %d, format %d, chunks %d\n", snapshot.Height, snapshot.Format, snapshot.Chunks)
42+
cmd.Printf("Snapshot created at height %d, format %d, chunks %d\n", snapshot.Height, snapshot.Format, snapshot.Chunks)
4543
return nil
4644
},
4745
}

client/snapshot/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var ListSnapshotsCmd = &cobra.Command{
2222
return fmt.Errorf("failed to list snapshots: %w", err)
2323
}
2424
for _, snapshot := range snapshots {
25-
fmt.Println("height:", snapshot.Height, "format:", snapshot.Format, "chunks:", snapshot.Chunks)
25+
cmd.Println("height:", snapshot.Height, "format:", snapshot.Format, "chunks:", snapshot.Chunks)
2626
}
2727

2828
return nil

client/snapshot/load.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const SnapshotFileName = "_snapshot"
2222
func LoadArchiveCmd() *cobra.Command {
2323
return &cobra.Command{
2424
Use: "load <archive-file>",
25-
Short: "Load a snapshot archive file into snapshot store",
25+
Short: "Load a snapshot archive file (.tar.gz) into snapshot store",
2626
Args: cobra.ExactArgs(1),
2727
RunE: func(cmd *cobra.Command, args []string) error {
2828
ctx := server.GetServerContextFromCmd(cmd)
@@ -70,7 +70,7 @@ func LoadArchiveCmd() *cobra.Command {
7070

7171
savedSnapshot, err := snapshotStore.Save(snapshot.Height, snapshot.Format, chunks)
7272
if err != nil {
73-
fmt.Println("failed to save snapshot", err)
73+
cmd.Println("failed to save snapshot", err)
7474
return
7575
}
7676
quitChan <- savedSnapshot

docs/docs/run-node/01-run-node.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,41 @@ log_level: "state:info,p2p:info,consensus:info,x/staking:info,x/ibc:info,*error"
152152

153153
## State Sync
154154

155-
State sync is the act in which a node syncs the latest or close to the latest state of a blockchain. This is useful for users who don't want to sync all the blocks in history. You can read more here: https://docs.cometbft.com/v0.37/core/state-sync
155+
State sync is the act in which a node syncs the latest or close to the latest state of a blockchain. This is useful for users who don't want to sync all the blocks in history. Read more in [CometBFT documentation](https://docs.cometbft.com/v0.37/core/state-sync).
156+
157+
State sync works thanks to snapshots. Read how the SDK handles snapshots [here](https://github.com/cosmos/cosmos-sdk/blob/825245d/store/snapshots/README.md).
156158

157159
### Local State Sync
158160

159161
Local state sync work similar to normal state sync except that it works off a local snapshot of state instead of one provided via the p2p network. The steps to start local state sync are similar to normal state sync with a few different designs.
160162

161163
1. As mentioned in https://docs.cometbft.com/v0.37/core/state-sync, one must set a height and hash in the config.toml along with a few rpc servers (the afromentioned link has instructions on how to do this).
162-
2. Bootsrapping Comet state in order to start the node after the snapshot has been ingested. This can be done with the bootstrap command `<app> comet bootstrap-state`
163-
<!-- 3. TODO after https://github.com/cosmos/cosmos-sdk/pull/16060 is merged -->
164+
2. Run `<appd snapshot restore <height> <format>` to restore a local snapshot (note: first load it from a file with the *load* command).
165+
3. Bootsrapping Comet state in order to start the node after the snapshot has been ingested. This can be done with the bootstrap command `<app> comet bootstrap-state`
166+
167+
### Snapshots Commands
168+
169+
The Cosmos SDK provides commands for managing snapshots.
170+
These commands can be added in an app with the following snippet in `cmd/<app>/root.go`:
171+
172+
```go
173+
import (
174+
"github.com/cosmos/cosmos-sdk/client/snapshot"
175+
)
176+
177+
func initRootCmd(/* ... */) {
178+
// ...
179+
rootCmd.AddCommand(
180+
snapshot.Cmd(appCreator),
181+
)
182+
}
183+
```
184+
185+
Then following commands are available at `<appd> snapshots [command]`:
186+
187+
* **list**: list local snapshots
188+
* **load**: Load a snapshot archive file into snapshot store
189+
* **restore**: Restore app state from local snapshot
190+
* **export**: Export app state to snapshot store
191+
* **dump**: Dump the snapshot as portable archive format
192+
* **delete**: Delete a local snapshot

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ replace (
177177
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
178178
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
179179
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
180-
// Downgraded to avoid bugs in following commits which caused simulations to fail.
180+
// replace broken goleveldb
181181
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
182182
)
183183

server/tm_cmds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func BootstrapStateCmd(appCreator types.AppCreator) *cobra.Command {
209209
},
210210
}
211211

212-
cmd.Flags().Int64("height", 0, "Block height to bootstrap state at, if not provided will use the latest block height in app state")
212+
cmd.Flags().Int64("height", 0, "Block height to bootstrap state at, if not provided it uses the latest block height in app state")
213213

214214
return cmd
215215
}

simapp/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,6 @@ replace (
170170
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
171171
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
172172
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
173-
// Downgraded to avoid bugs in following commits which caused simulations to fail.
173+
// replace broken goleveldb
174174
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
175175
)

snapshots/README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ Snapshot settings are optional. However, if set, they have an effect on how prun
5555
persisting the heights that are multiples of `state-sync.snapshot-interval` until after the snapshot is complete.
5656

5757
If pruning is enabled (not `pruning = "nothing"`), we avoid pruning heights that are multiples of
58-
`state-sync.snapshot-interval` in the regular logic determined by the
59-
pruning settings and applied after every `Commit()`. This is done to prevent a
60-
height from being removed before a snapshot is complete. Therefore, we keep
61-
such heights until after a snapshot is done. At this point, the height is sent to
58+
`state-sync.snapshot-interval` in the regular logic determined by the
59+
pruning settings and applied after every `Commit()`. This is done to prevent a
60+
height from being removed before a snapshot is complete. Therefore, we keep
61+
such heights until after a snapshot is done. At this point, the height is sent to
6262
the `pruning.Manager` to be pruned according to the pruning settings after the next `Commit()`.
6363

6464
To illustrate, assume that we are currently at height 960 with `pruning-keep-recent = 50`,
6565
`pruning-interval = 10`, and `state-sync.snapshot-interval = 100`. Let's assume that
6666
the snapshot that was triggered at height `900` **just finishes**. Then, we can prune height
67-
`900` right away (that is, when we call `Commit()` at height 960 because 900 is less than `960 - 50 = 910`.
67+
`900` right away (that is, when we call `Commit()` at height 960 because 900 is less than `960 - 50 = 910`).
6868

6969
Let's now assume that all conditions stay the same but the snapshot at height 900 is **not complete yet**.
7070
Then, we cannot prune it to avoid deleting a height that is still being snapshotted. Therefore, we keep track
@@ -78,23 +78,22 @@ Note that in both examples, if we let current height = C, and previous height P
7878

7979
P - `pruning-keep-recent` - `pruning-interval` <= h <= P - `pruning-keep-recent`
8080

81-
we can prune height h. In our first example, all heights 899 - 909 fall in this range and are pruned at height 960 as long as
81+
we can prune height h. In our first example, all heights 899 - 909 fall in this range and are pruned at height 960 as long as
8282
h is not a snapshot height (E.g. 900).
8383

8484
That is, we always use current height to determine at which height to prune (960) while we use previous
8585
to determine which heights are to be pruned (959 - 50 - 10 = 899-909 = 959 - 50).
8686

87-
8887
## Configuration
8988

9089
* `state-sync.snapshot-interval`
91-
* the interval at which to take snapshots.
92-
* the value of 0 disables snapshots.
93-
* if pruning is enabled, it is done after a snapshot is complete for the heights that are multiples of this interval.
90+
* the interval at which to take snapshots.
91+
* the value of 0 disables snapshots.
92+
* if pruning is enabled, it is done after a snapshot is complete for the heights that are multiples of this interval.
9493

9594
* `state-sync.snapshot-keep-recent`:
96-
* the number of recent snapshots to keep.
97-
* 0 means keep all.
95+
* the number of recent snapshots to keep.
96+
* 0 means keep all.
9897

9998
## Snapshot Metadata
10099

0 commit comments

Comments
 (0)