Skip to content

Commit

Permalink
Merge pull request #704 from iotaledger/feat/time-provider-current-sl…
Browse files Browse the repository at this point in the history
…ot-epoch

Add `CurrentSlot` and `CurrentEpoch` to `TimeProvider`
  • Loading branch information
muXxer authored Mar 5, 2024
2 parents dde4a52 + 1b87975 commit 2dae7ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func createBlockWithParents(t *testing.T, strongParents, weakParents, shallowLik
WeakParents(weakParents).
ShallowLikeParents(shallowLikeParent).
IssuingTime(time.Now()).
SlotCommitmentID(iotago.NewCommitment(apiForSlot.Version(), apiForSlot.TimeProvider().SlotFromTime(time.Now())-apiForSlot.ProtocolParameters().MinCommittableAge(), iotago.CommitmentID{}, iotago.Identifier{}, 0, 0).MustID()).
SlotCommitmentID(iotago.NewCommitment(apiForSlot.Version(), apiForSlot.TimeProvider().CurrentSlot()-apiForSlot.ProtocolParameters().MinCommittableAge(), iotago.CommitmentID{}, iotago.Identifier{}, 0, 0).MustID()).
Build()
require.NoError(t, err)

Expand Down
10 changes: 10 additions & 0 deletions timeprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (t *TimeProvider) SlotFromTime(targetTime time.Time) SlotIndex {
return t.genesisSlot + SlotIndex(int64(elapsed/time.Second)/t.slotDurationSeconds) + 1
}

// CurrentSlot calculates the current slot based on the current time.
func (t *TimeProvider) CurrentSlot() SlotIndex {
return t.SlotFromTime(time.Now())
}

// SlotStartTime calculates the start time of the given slot.
func (t *TimeProvider) SlotStartTime(slot SlotIndex) time.Time {
if slot <= t.genesisSlot {
Expand Down Expand Up @@ -130,6 +135,11 @@ func (t *TimeProvider) EpochFromSlot(slot SlotIndex) EpochIndex {
return EpochIndex((slot - t.genesisSlot) >> t.slotsPerEpochExponent)
}

// CurrentEpoch calculates the current epoch based on the current time.
func (t *TimeProvider) CurrentEpoch() EpochIndex {
return t.EpochFromSlot(t.CurrentSlot())
}

// EpochStart calculates the start slot of the given epoch.
func (t *TimeProvider) EpochStart(epoch EpochIndex) SlotIndex {
return t.genesisSlot + SlotIndex(epoch<<t.slotsPerEpochExponent)
Expand Down

0 comments on commit 2dae7ac

Please sign in to comment.