Skip to content

Commit

Permalink
separately track blockstore rollback height
Browse files Browse the repository at this point in the history
use blockstore's height (not application state's) for tracking rollback
of blockstore & state.db
  • Loading branch information
pirtleshell committed Dec 12, 2023
1 parent 9f6ee42 commit 44428bc
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions cmd/kava/cmd/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ $ kava shard --home path/to/.kava --start 1000000 --end -1 --only-app-state`,
return fmt.Errorf("only sharding of rootmulti.Store type is supported")
}

// handle desired endblock being latest
latest := multistore.LatestVersion()
fmt.Printf("latest height: %d\n", latest)
// handle desired endblock being latestAppHeight
latestAppHeight := multistore.LatestVersion()
fmt.Printf("latest height: %d\n", latestAppHeight)
if endBlock == shardEndBlockLatest {
endBlock = latest
endBlock = latestAppHeight
}
shardSize := endBlock - startBlock + 1

// error if requesting block range the database does not have
if endBlock > latest {
return fmt.Errorf("data does not contain end block (%d): latest version is %d", endBlock, latest)
if endBlock > latestAppHeight {
return fmt.Errorf("data does not contain end block (%d): latest version is %d", endBlock, latestAppHeight)
}

fmt.Printf("pruning data in %s down to heights %d - %d (%d blocks)\n", clientCtx.HomeDir, startBlock, endBlock, shardSize)
Expand All @@ -129,18 +129,22 @@ $ kava shard --home path/to/.kava --start 1000000 --end -1 --only-app-state`,
return fmt.Errorf("failed to open cometbft dbs: %s", err)
}

// use blockstore's latest height in case this command was stopped pre-maturely
// and the application state is behind the blockstore.
latestBlockStore := blockStore.Height()

// prep for outputting progress repeatedly to same line
needsRollback := endBlock < latest
needsRollback := endBlock < latestBlockStore
progress := "rolling back blockstore & cometbft state to height %d"
numChars := len(fmt.Sprintf(progress, latest))
numChars := len(fmt.Sprintf(progress, latestBlockStore))
clearLine := fmt.Sprintf("\r%s\r", strings.Repeat(" ", numChars))
printRollbackProgress := func(h int64) {
fmt.Print(clearLine)
fmt.Printf(progress, h)
}

// rollback tendermint db
height := latest
height := latestBlockStore
for height > endBlock {
printRollbackProgress(height - 1)
height, _, err = tmstate.Rollback(blockStore, stateStore, true)
Expand All @@ -152,15 +156,15 @@ $ kava shard --home path/to/.kava --start 1000000 --end -1 --only-app-state`,
if needsRollback {
fmt.Println()
} else {
fmt.Printf("latest store height is already %d\n", latest)
fmt.Printf("latest store height is already %d\n", latestBlockStore)
}

//////////////////////////////
// Prune blocks to startBlock
//////////////////////////////

// enumerate all heights to prune
pruneHeights := make([]int64, 0, latest-shardSize)
pruneHeights := make([]int64, 0, latestAppHeight-shardSize)
for i := int64(1); i < startBlock; i++ {
pruneHeights = append(pruneHeights, i)
}
Expand Down

0 comments on commit 44428bc

Please sign in to comment.