Skip to content

Commit 8ee8ded

Browse files
authored
dot/state: update rewind to forcefully set blocktree root, update flag param to be block to rewind to (#1443)
1 parent aecd73a commit 8ee8ded

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

cmd/gossamer/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ var (
4343
Name: "roles",
4444
Usage: "Roles of the gossamer node",
4545
}
46-
// RewindFlag rewinds the head of the chain by the given number of blocks. Useful for development
46+
// RewindFlag rewinds the head of the chain to the given block number. Useful for development
4747
RewindFlag = cli.IntFlag{
4848
Name: "rewind",
49-
Usage: "Rewind head of chain by given number of blocks",
49+
Usage: "Rewind head of chain to the given block number",
5050
}
5151
)
5252

dot/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func createStateService(cfg *Config) (*state.Service, error) {
6464
}
6565

6666
if cfg.State.Rewind != 0 {
67-
err = stateSrvc.Rewind(cfg.State.Rewind)
67+
err = stateSrvc.Rewind(int64(cfg.State.Rewind))
6868
if err != nil {
6969
return nil, fmt.Errorf("failed to rewind state: %w", err)
7070
}

dot/state/service.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package state
1919
import (
2020
"bytes"
2121
"fmt"
22+
"math/big"
2223
"os"
2324
"path/filepath"
2425

@@ -321,13 +322,22 @@ func (s *Service) Start() error {
321322
return nil
322323
}
323324

324-
// Rewind rewinds the chain by the given number of blocks.
325+
// Rewind rewinds the chain to the given block number.
325326
// If the given number of blocks is greater than the chain height, it will rewind to genesis.
326-
func (s *Service) Rewind(numBlocks int) error {
327+
func (s *Service) Rewind(toBlock int64) error {
327328
num, _ := s.Block.BestBlockNumber()
329+
if toBlock > num.Int64() {
330+
return fmt.Errorf("cannot rewind, given height is higher than our current height")
331+
}
332+
333+
logger.Info("rewinding state...", "current height", num, "desired height", toBlock)
334+
335+
root, err := s.Block.GetBlockByNumber(big.NewInt(toBlock))
336+
if err != nil {
337+
return err
338+
}
328339

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

333343
header, _ := s.Block.BestBlockHeader()

0 commit comments

Comments
 (0)