-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repackaged NextBlockTime and GetNextStakerChangeTime (#3134)
- Loading branch information
Showing
8 changed files
with
76 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package state | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/utils/timer/mockable" | ||
) | ||
|
||
func NextBlockTime(state Chain, clk *mockable.Clock) (time.Time, bool, error) { | ||
var ( | ||
timestamp = clk.Time() | ||
parentTime = state.GetTimestamp() | ||
) | ||
if parentTime.After(timestamp) { | ||
timestamp = parentTime | ||
} | ||
// [timestamp] = max(now, parentTime) | ||
|
||
nextStakerChangeTime, err := GetNextStakerChangeTime(state) | ||
if err != nil { | ||
return time.Time{}, false, fmt.Errorf("failed getting next staker change time: %w", err) | ||
} | ||
|
||
// timeWasCapped means that [timestamp] was reduced to [nextStakerChangeTime] | ||
timeWasCapped := !timestamp.Before(nextStakerChangeTime) | ||
if timeWasCapped { | ||
timestamp = nextStakerChangeTime | ||
} | ||
// [timestamp] = min(max(now, parentTime), nextStakerChangeTime) | ||
return timestamp, timeWasCapped, nil | ||
} | ||
|
||
// GetNextStakerChangeTime returns the next time a staker will be either added | ||
// or removed to/from the current validator set. | ||
func GetNextStakerChangeTime(state Chain) (time.Time, error) { | ||
currentStakerIterator, err := state.GetCurrentStakerIterator() | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
defer currentStakerIterator.Release() | ||
|
||
pendingStakerIterator, err := state.GetPendingStakerIterator() | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
defer pendingStakerIterator.Release() | ||
|
||
hasCurrentStaker := currentStakerIterator.Next() | ||
hasPendingStaker := pendingStakerIterator.Next() | ||
switch { | ||
case hasCurrentStaker && hasPendingStaker: | ||
nextCurrentTime := currentStakerIterator.Value().NextTime | ||
nextPendingTime := pendingStakerIterator.Value().NextTime | ||
if nextCurrentTime.Before(nextPendingTime) { | ||
return nextCurrentTime, nil | ||
} | ||
return nextPendingTime, nil | ||
case hasCurrentStaker: | ||
return currentStakerIterator.Value().NextTime, nil | ||
case hasPendingStaker: | ||
return pendingStakerIterator.Value().NextTime, nil | ||
default: | ||
return time.Time{}, database.ErrNotFound | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters