Skip to content

Commit

Permalink
dot/state: update rewind to forcefully set blocktree root, update fla…
Browse files Browse the repository at this point in the history
…g param to be block to rewind to (#1443)
  • Loading branch information
noot committed Mar 8, 2021
1 parent aecd73a commit 8ee8ded
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/gossamer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ var (
Name: "roles",
Usage: "Roles of the gossamer node",
}
// RewindFlag rewinds the head of the chain by the given number of blocks. Useful for development
// RewindFlag rewinds the head of the chain to the given block number. Useful for development
RewindFlag = cli.IntFlag{
Name: "rewind",
Usage: "Rewind head of chain by given number of blocks",
Usage: "Rewind head of chain to the given block number",
}
)

Expand Down
2 changes: 1 addition & 1 deletion dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func createStateService(cfg *Config) (*state.Service, error) {
}

if cfg.State.Rewind != 0 {
err = stateSrvc.Rewind(cfg.State.Rewind)
err = stateSrvc.Rewind(int64(cfg.State.Rewind))
if err != nil {
return nil, fmt.Errorf("failed to rewind state: %w", err)
}
Expand Down
18 changes: 14 additions & 4 deletions dot/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"bytes"
"fmt"
"math/big"
"os"
"path/filepath"

Expand Down Expand Up @@ -321,13 +322,22 @@ func (s *Service) Start() error {
return nil
}

// Rewind rewinds the chain by the given number of blocks.
// Rewind rewinds the chain to the given block number.
// If the given number of blocks is greater than the chain height, it will rewind to genesis.
func (s *Service) Rewind(numBlocks int) error {
func (s *Service) Rewind(toBlock int64) error {
num, _ := s.Block.BestBlockNumber()
if toBlock > num.Int64() {
return fmt.Errorf("cannot rewind, given height is higher than our current height")
}

logger.Info("rewinding state...", "current height", num, "desired height", toBlock)

root, err := s.Block.GetBlockByNumber(big.NewInt(toBlock))
if err != nil {
return err
}

logger.Info("rewinding state...", "current height", num, "to rewind", numBlocks)
s.Block.bt.Rewind(numBlocks)
s.Block.bt = blocktree.NewBlockTreeFromRoot(root.Header, s.db)
newHead := s.Block.BestBlockHash()

header, _ := s.Block.BestBlockHeader()
Expand Down

0 comments on commit 8ee8ded

Please sign in to comment.