-
Notifications
You must be signed in to change notification settings - Fork 146
fix(lib/babe): use current system time to yield a new slot #3133
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
EclesioMeloJunior
merged 34 commits into
development
from
eclesio/babe-timestamp-slot-window
Mar 21, 2023
Merged
Changes from 11 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
7266f6f
feat: remove slot timers and use a similar substrate approach
EclesioMeloJunior 15361a4
chore: create integration test
EclesioMeloJunior 15ff45d
chore: remove unneeded print lines
EclesioMeloJunior ac5433f
chore: make sure we wait just the remaining time and not a full slot …
EclesioMeloJunior c8ced58
chore: remove unneeded `untilNextSlot` prop
EclesioMeloJunior 5eba8ef
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 0e57ec5
chore: remove unneeded diffs
EclesioMeloJunior 31d3f73
Merge branch 'eclesio/babe-timestamp-slot-window' of github.com:Chain…
EclesioMeloJunior a038c62
chore: remove unneeded else branch
EclesioMeloJunior 65fa6d0
chore: remove unneeded diffs
EclesioMeloJunior 8cca3fe
chore: remove unneeded `continue` keyword
EclesioMeloJunior f55f863
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior a3a17e4
chore: include substrate permalink + rename variables
EclesioMeloJunior 4312fc2
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 5e7d7e1
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 67ddc9a
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 8ec67bc
chore: fix check lint CI
EclesioMeloJunior ef63ac9
Merge branch 'eclesio/babe-timestamp-slot-window' of github.com:Chain…
EclesioMeloJunior a711c52
chore: add license header to new files
EclesioMeloJunior a5d84d3
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 6c22c94
chore: address comments
EclesioMeloJunior 1feaf89
chore: make `waitForNextSlot` context aware
EclesioMeloJunior 610f255
chore: fix `TestSlotHandlerConstructor` test
EclesioMeloJunior 311a3da
chore: add integration build flag
EclesioMeloJunior f3d38da
chore: solve context dependency + resolve slot mismatch error
EclesioMeloJunior 0700c70
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior e985d3f
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 167128a
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 98fd9eb
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 5dd32ac
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 2c09c5a
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior 2d41cde
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior f98efc9
Merge branch 'development' into eclesio/babe-timestamp-slot-window
EclesioMeloJunior e417fe4
chore: return any error
EclesioMeloJunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
| package babe | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/ChainSafe/gossamer/dot/types" | ||
| "github.com/ChainSafe/gossamer/lib/crypto/sr25519" | ||
| "github.com/ChainSafe/gossamer/pkg/scale" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEpochHandler_run(t *testing.T) { | ||
EclesioMeloJunior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const authorityIndex uint32 = 0 | ||
| aliceKeyPair := keyring.Alice().(*sr25519.Keypair) | ||
| epochData := &epochData{ | ||
| threshold: scale.MaxUint128, | ||
| authorityIndex: authorityIndex, | ||
| authorities: []types.Authority{ | ||
| *types.NewAuthority(aliceKeyPair.Public(), 1), | ||
| }, | ||
| } | ||
|
|
||
| const slotDuration = 6 * time.Second | ||
| const epochLength uint64 = 100 | ||
|
|
||
| constants := constants{ //nolint:govet | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| slotDuration: slotDuration, | ||
| epochLength: epochLength, | ||
| } | ||
|
|
||
| const expectedEpoch = 1 | ||
| startSlot := getCurrentSlot(slotDuration) | ||
| handleSlotFunc := testHandleSlotFunc(t, authorityIndex, expectedEpoch, startSlot) | ||
|
|
||
| epochHandler, err := newEpochHandler(1, startSlot, epochData, constants, handleSlotFunc, aliceKeyPair) | ||
| require.NoError(t, err) | ||
| require.Equal(t, epochLength, uint64(len(epochHandler.slotToPreRuntimeDigest))) | ||
|
|
||
| timeoutCtx, cancel := context.WithTimeout(context.Background(), slotDuration*10) | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| defer cancel() | ||
|
|
||
| errCh := make(chan error) | ||
| go epochHandler.run(timeoutCtx, errCh) | ||
|
|
||
| err = <-errCh | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func testHandleSlotFunc(t *testing.T, expectedAuthorityIndex uint32, | ||
| expectedEpoch, startSlot uint64) handleSlotFunc { | ||
| currentSlot := startSlot | ||
|
|
||
| return func(epoch uint64, slot Slot, authorityIndex uint32, | ||
| preRuntimeDigest *types.PreRuntimeDigest) error { | ||
|
|
||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| require.NotNil(t, preRuntimeDigest) | ||
| require.Equal(t, expectedEpoch, epoch) | ||
| require.Equal(t, expectedAuthorityIndex, authorityIndex) | ||
|
|
||
| require.Equal(t, slot.number, currentSlot, "%d != %d", slot.number, currentSlot) | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // increase the slot by one so we expect the next call | ||
| // to be exactly 1 slot greater than the previous call | ||
| currentSlot += 1 | ||
| return nil | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,55 @@ | ||
| package babe | ||
|
|
||
| import ( | ||
| "time" | ||
| ) | ||
|
|
||
| // timeUntilNextSlotInNanos calculates, based on the current system time, the remainng | ||
| // time to the next slot | ||
jimjbrettj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func timeUntilNextSlotInMilli(slotDuration time.Duration) time.Duration { | ||
| now := time.Now().UnixNano() | ||
| slotDurationInMilli := slotDuration.Nanoseconds() | ||
|
|
||
| nextSlot := (now + slotDurationInMilli) / slotDurationInMilli | ||
|
|
||
| remaining := nextSlot*slotDurationInMilli - now | ||
| return time.Duration(remaining) | ||
| } | ||
|
|
||
| type slotHandler struct { | ||
| slotDuration time.Duration | ||
| lastSlot *Slot | ||
| } | ||
|
|
||
| func newSlotHandler(slotDuration time.Duration) *slotHandler { | ||
| return &slotHandler{ | ||
| slotDuration: slotDuration, | ||
| } | ||
| } | ||
|
|
||
| func (s *slotHandler) waitForNextSlot() Slot { | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for { | ||
| // check if there is enough time to collaaborate | ||
| untilNextSlot := timeUntilNextSlotInMilli(s.slotDuration) | ||
| oneThird := s.slotDuration / 3 | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if untilNextSlot <= oneThird { | ||
| time.Sleep(untilNextSlot) | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| currentSystemTime := time.Now() | ||
| currentSlotNumber := uint64(currentSystemTime.UnixNano()) / uint64(s.slotDuration.Nanoseconds()) | ||
| currentSlot := Slot{ | ||
| start: currentSystemTime, | ||
| duration: s.slotDuration, | ||
| number: currentSlotNumber, | ||
| } | ||
|
|
||
| // Never yield the same slot twice | ||
| if s.lastSlot == nil || currentSlot.number > s.lastSlot.number { | ||
| s.lastSlot = ¤tSlot | ||
| return currentSlot | ||
| } | ||
|
|
||
| time.Sleep(timeUntilNextSlotInMilli(s.slotDuration)) | ||
| } | ||
| } | ||
This file contains hidden or 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,27 @@ | ||
| package babe | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSlotHandeConstructor(t *testing.T) { | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expected := &slotHandler{ | ||
| slotDuration: time.Duration(6000), | ||
| } | ||
|
|
||
| handler := newSlotHandler(time.Duration(6000)) | ||
| require.Equal(t, expected, handler) | ||
| } | ||
|
|
||
| func TestSlotHandlerNextSlot(t *testing.T) { | ||
EclesioMeloJunior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| slotDuration := 2 * time.Second | ||
EclesioMeloJunior marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| handler := newSlotHandler(slotDuration) | ||
|
|
||
| firstIteration := handler.waitForNextSlot() | ||
| secondIteration := handler.waitForNextSlot() | ||
|
|
||
| require.Greater(t, secondIteration.number, firstIteration.number) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.