Skip to content

Commit

Permalink
Check P-chain ShouldPrune during Initialize (#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Aug 10, 2023
1 parent 6a8c0bd commit c29ad76
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
15 changes: 15 additions & 0 deletions vms/platformvm/state/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 11 additions & 17 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ type State interface {
// Discard uncommitted changes to the database.
Abort()

// Returns if the state should be pruned and indexed to remove rejected
// blocks and generate the block height index.
//
// TODO: Remove after v1.11.x is activated
ShouldPrune() (bool, error)

// Removes rejected blocks from disk and indexes accepted blocks by height. This
// function supports being (and is recommended to be) called asynchronously.
//
Expand Down Expand Up @@ -467,11 +473,14 @@ func New(
// to be run.
//
// TODO: Cleanup after v1.11.x is activated
shouldPrune, err := s.shouldPrune()
shouldPrune, err := s.ShouldPrune()
if err != nil {
return nil, err
}
if shouldPrune {
// If the pruned key is on disk, we must delete it to ensure our disk
// can't get into a partially pruned state if the node restarts mid-way
// through pruning.
if err := s.singletonDB.Delete(prunedKey); err != nil {
return nil, fmt.Errorf("failed to remove prunedKey from singletonDB: %w", err)
}
Expand Down Expand Up @@ -738,7 +747,7 @@ func (s *state) doneInit() error {
return s.singletonDB.Put(initializedKey, nil)
}

func (s *state) shouldPrune() (bool, error) {
func (s *state) ShouldPrune() (bool, error) {
has, err := s.singletonDB.Has(prunedKey)
if err != nil {
return true, err
Expand Down Expand Up @@ -2343,21 +2352,6 @@ func parseStoredBlock(blkBytes []byte) (blocks.Block, choices.Status, bool, erro

func (s *state) PruneAndIndex(lock sync.Locker, log logging.Logger) error {
lock.Lock()
shouldPrune, err := s.shouldPrune()
if err != nil {
lock.Unlock()
return fmt.Errorf(
"failed to check if the database should be pruned: %w",
err,
)
}
if !shouldPrune {
lock.Unlock()

log.Info("state already pruned and indexed")
return nil
}

// It is possible that new blocks are added after grabbing this iterator. New
// blocks are guaranteed to be accepted and height-indexed, so we don't need to
// check them.
Expand Down
13 changes: 13 additions & 0 deletions vms/platformvm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ func (vm *VM) Initialize(
return err
}

shouldPrune, err := vm.state.ShouldPrune()
if err != nil {
return fmt.Errorf(
"failed to check if the database should be pruned: %w",
err,
)
}
if !shouldPrune {
chainCtx.Log.Info("state already pruned and indexed")
vm.pruned.Set(true)
return nil
}

go func() {
err := vm.state.PruneAndIndex(&vm.ctx.Lock, vm.ctx.Log)
if err != nil {
Expand Down

0 comments on commit c29ad76

Please sign in to comment.