Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check P-chain ShouldPrune during Initialize #1836

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines -2346 to -2359
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check was moved into vm.Initialize


// 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By setting pruned here - we guaranteed that we will return nil from VerifyHeightIndex if the P-chain has already been pruned (even if it is called immediately after Initialize).

return nil
}

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