Skip to content

Reintroduce P-chain block reindexing #3883

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

Merged
merged 6 commits into from
Apr 30, 2025
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
16 changes: 16 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.

183 changes: 182 additions & 1 deletion vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"

"github.com/google/btree"
Expand All @@ -23,14 +25,17 @@ import (
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/iterator"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/maybe"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/gas"
Expand Down Expand Up @@ -97,6 +102,7 @@ var (
LastAcceptedKey = []byte("last accepted")
HeightsIndexedKey = []byte("heights indexed")
InitializedKey = []byte("initialized")
BlocksReindexedKey = []byte("blocks reindexed.3")

emptyL1ValidatorCache = &cache.Empty[ids.ID, maybe.Maybe[L1Validator]]{}
)
Expand Down Expand Up @@ -215,6 +221,14 @@ type State interface {
// Discard uncommitted changes to the database.
Abort()

// ReindexBlocks converts any block indices using the legacy storage format
// to the new format. If this database has already updated the indices,
// this function will return immediately, without iterating over the
// database.
//
// TODO: Remove after v1.14.x is activated
ReindexBlocks(lock sync.Locker, log logging.Logger) error

// Commit changes to the base database.
Commit() error

Expand All @@ -227,6 +241,16 @@ type State interface {
Close() error
}

// Prior to https://github.com/ava-labs/avalanchego/pull/1719, blocks were
// stored as a map from blkID to stateBlk. Nodes synced prior to this PR may
// still have blocks partially stored using this legacy format.
//
// TODO: Remove after v1.14.x is activated
type stateBlk struct {
Bytes []byte `serialize:"true"`
Status choices.Status `serialize:"true"`
}

/*
* VMDB
* |-. validators
Expand Down Expand Up @@ -296,6 +320,7 @@ type State interface {
* | '-- timestamp + validationID -> nil
* '-. singletons
* |-- initializedKey -> nil
* |-- blocksReindexedKey -> nil
* |-- timestampKey -> timestamp
* |-- feeStateKey -> feeState
* |-- l1ValidatorExcessKey -> l1ValidatorExcess
Expand Down Expand Up @@ -2258,7 +2283,7 @@ func (s *state) GetStatelessBlock(blockID ids.ID) (block.Block, error) {
return nil, err
}

blk, err := block.Parse(block.GenesisCodec, blkBytes)
blk, _, err := parseStoredBlock(blkBytes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -3039,6 +3064,162 @@ func (s *state) writeMetadata() error {
return nil
}

// Returns the block and whether it is a [stateBlk].
// Invariant: blkBytes is safe to parse with blocks.GenesisCodec
//
// TODO: Remove after v1.14.x is activated
func parseStoredBlock(blkBytes []byte) (block.Block, bool, error) {
// Attempt to parse as blocks.Block
blk, err := block.Parse(block.GenesisCodec, blkBytes)
if err == nil {
return blk, false, nil
}

// Fallback to [stateBlk]
blkState := stateBlk{}
if _, err := block.GenesisCodec.Unmarshal(blkBytes, &blkState); err != nil {
return nil, false, err
}

blk, err = block.Parse(block.GenesisCodec, blkState.Bytes)
return blk, true, err
}

func (s *state) ReindexBlocks(lock sync.Locker, log logging.Logger) error {
has, err := s.singletonDB.Has(BlocksReindexedKey)
if err != nil {
return err
}
if has {
log.Info("blocks already reindexed")
return nil
}

// It is possible that new blocks are added after grabbing this iterator.
// New blocks are guaranteed to be persisted in the new format, so we don't
// need to check them.
blockIterator := s.blockDB.NewIterator()
// Releasing is done using a closure to ensure that updating blockIterator
// will result in having the most recent iterator released when executing
// the deferred function.
defer func() {
blockIterator.Release()
}()

log.Info("starting block reindexing")

var (
startTime = time.Now()
lastCommit = startTime
nextUpdate = startTime.Add(indexLogFrequency)
numIndicesChecked = 0
numIndicesUpdated = 0
)

for blockIterator.Next() {
valueBytes := blockIterator.Value()
blk, isStateBlk, err := parseStoredBlock(valueBytes)
if err != nil {
return fmt.Errorf("failed to parse block: %w", err)
}

blkID := blk.ID()

// This block was previously stored using the legacy format, update the
// index to remove the usage of stateBlk.
if isStateBlk {
blkBytes := blk.Bytes()
if err := s.blockDB.Put(blkID[:], blkBytes); err != nil {
return fmt.Errorf("failed to write block: %w", err)
}

numIndicesUpdated++
}

numIndicesChecked++

now := time.Now()
if now.After(nextUpdate) {
nextUpdate = now.Add(indexLogFrequency)

progress := timer.ProgressFromHash(blkID[:])
eta := timer.EstimateETA(
startTime,
progress,
math.MaxUint64,
)

log.Info("reindexing blocks",
zap.Int("numIndicesUpdated", numIndicesUpdated),
zap.Int("numIndicesChecked", numIndicesChecked),
zap.Duration("eta", eta),
)
}

if numIndicesChecked%indexIterationLimit == 0 {
// We must hold the lock during committing to make sure we don't
// attempt to commit to disk while a block is concurrently being
// accepted.
lock.Lock()
err := errors.Join(
s.Commit(),
blockIterator.Error(),
)
lock.Unlock()
if err != nil {
return err
}

// We release the iterator here to allow the underlying database to
// clean up deleted state.
blockIterator.Release()

// We take the minimum here because it's possible that the node is
// currently bootstrapping. This would mean that grabbing the lock
// could take an extremely long period of time; which we should not
// delay processing for.
indexDuration := now.Sub(lastCommit)
sleepDuration := min(
indexIterationSleepMultiplier*indexDuration,
indexIterationSleepCap,
)
time.Sleep(sleepDuration)

// Make sure not to include the sleep duration into the next index
// duration.
lastCommit = time.Now()

blockIterator = s.blockDB.NewIteratorWithStart(blkID[:])
}
}

// Ensure we fully iterated over all blocks before writing that indexing has
// finished.
//
// Note: This is needed because a transient read error could cause the
// iterator to stop early.
if err := blockIterator.Error(); err != nil {
return fmt.Errorf("failed to iterate over historical blocks: %w", err)
}

if err := s.singletonDB.Put(BlocksReindexedKey, nil); err != nil {
return fmt.Errorf("failed to put marked blocks as reindexed: %w", err)
}

// We must hold the lock during committing to make sure we don't attempt to
// commit to disk while a block is concurrently being accepted.
lock.Lock()
defer lock.Unlock()

log.Info("finished block reindexing",
zap.Int("numIndicesUpdated", numIndicesUpdated),
zap.Int("numIndicesChecked", numIndicesChecked),
zap.Duration("duration", time.Since(startTime)),
)

return s.Commit()
}

func (s *state) GetUptime(vdrID ids.NodeID) (time.Duration, time.Time, error) {
return s.validatorState.GetUptime(vdrID, constants.PrimaryNetworkID)
}
Expand Down
Loading