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

Backport v0.33 limit get block height and checkpoint protocol snapshot #5658

Merged
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Jordan Schalm <jordan@dapperlabs.com>
  • Loading branch information
zhangchiqing and jordanschalm committed Apr 11, 2024
commit 7bd18aa5b97f16eb3092461f382b2898fc5be3f2
3 changes: 3 additions & 0 deletions cmd/util/common/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func findSealedHeightForCommits(

// iterate backwards from the end height to the start height
// to find the block that produces a state commitment in the given list
// It is safe to skip blocks in this linear search because we expect `stateCommitments` to hold commits
// for a contiguous range of blocks (for correct operation we assume `blocksToSkip` is smaller than this range).
// end height must be a sealed block
step := blocksToSkip + 1
for height := endHeight; height >= startHeight; height -= uint64(step) {
Expand Down Expand Up @@ -164,6 +166,7 @@ func GenerateProtocolSnapshotForCheckpointWithHeights(
blocksToSkip uint,
endHeight uint64,
) (protocol.Snapshot, uint64, flow.StateCommitment, error) {
// Stop searching after 10,000 iterations or upon reaching the minimum height, whichever comes first.
startHeight := uint64(0)
// preventing startHeight from being negative
length := uint64(blocksToSkip+1) * 10000
Expand Down
8 changes: 6 additions & 2 deletions state/protocol/snapshots/dynamic_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var ErrSnapshotPhaseMismatch = errors.New("snapshot does not contain a valid sealing segment")
var ErrSnapshotHistoryLimit = fmt.Errorf("reached the snapshot history limit")
var ErrSnapshotHistoryLimit = errors.New("reached the snapshot history limit")

// GetDynamicBootstrapSnapshot will return a valid snapshot for dynamic bootstrapping
// GetDynamicBootstrapSnapshot returns `refSnapshot` if it is valid for use in dynamic bootstrapping.
// Otherwise returns an error. (Effectively this validates that the input snapshot can be used in dynamic bootstrapping.)
// Expected error returns during normal operations:
// * ErrSnapshotPhaseMismatch - snapshot does not contain a valid sealing segment
// All other errors should be treated as exceptions.
Expand All @@ -31,6 +32,8 @@ func GetClosestDynamicBootstrapSnapshot(state protocol.State, refSnapshot protoc
return getValidSnapshot(state, refSnapshot, 0, true, snapshotHistoryLimit)
}

// GetCounterAndPhase returns the current epoch counter and phase, at `height`.
// No errors are expected during normal operation.
func GetCounterAndPhase(state protocol.State, height uint64) (uint64, flow.EpochPhase, error) {
snapshot := state.AtHeight(height)

Expand Down Expand Up @@ -59,6 +62,7 @@ func IsEpochOrPhaseDifferent(counter1, counter2 uint64, phase1, phase2 flow.Epoc
// where the transition happens.
// Expected error returns during normal operations:
// * ErrSnapshotPhaseMismatch - snapshot does not contain a valid sealing segment
// * ErrSnapshotHistoryLimit - failed to find a valid snapshot after checking `snapshotHistoryLimit` blocks
// All other errors should be treated as exceptions.
func getValidSnapshot(
state protocol.State,
Expand Down