-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use GetTipset(cert.Head()) to compute startup delay
Resolves #720 Signed-off-by: Jakub Sztandera <oss@kubuxu.com>
- Loading branch information
Showing
4 changed files
with
119 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package f3 |
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,16 @@ | ||
package f3 | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/filecoin-project/go-f3/ec" | ||
) | ||
|
||
func computeTipsetTimestampAtEpoch(validTipset ec.TipSet, epoch int64, ECPeriod time.Duration) time.Time { | ||
// validTipset is base and epoch is the head | ||
// timestamp(head) = genesis + epoch(head) * period | ||
// timestamp(base) = genesis + epoch(base) * period + timestamp(head) - timestamp(head) | ||
// timestamp(base) = timestamp(head) + genesis + epoch(base) * period - genesis - epoch(head) * period | ||
// timestamp(base) = timestamp(head) + (epoch(base) - epoch(head)) * period | ||
return validTipset.Timestamp().Add(time.Duration(epoch-validTipset.Epoch()) * ECPeriod) | ||
} |
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,90 @@ | ||
package f3 | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/filecoin-project/go-f3/gpbft" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type tipset struct { | ||
genesis time.Time | ||
period time.Duration | ||
epoch int64 | ||
} | ||
|
||
func (ts *tipset) String() string { | ||
panic("not implemented") | ||
} | ||
|
||
func (ts *tipset) Key() gpbft.TipSetKey { | ||
panic("not implemented") | ||
} | ||
|
||
func (ts *tipset) Beacon() []byte { | ||
panic("not implemented") | ||
} | ||
|
||
func (ts *tipset) Epoch() int64 { | ||
return ts.epoch | ||
} | ||
|
||
func (ts *tipset) Timestamp() time.Time { | ||
return ts.genesis.Add(time.Duration(ts.epoch) * ts.period) | ||
} | ||
|
||
func tipsetGenerator(genesis time.Time, period time.Duration) func(epoch int64) *tipset { | ||
return func(epoch int64) *tipset { | ||
return &tipset{ | ||
genesis: genesis, | ||
period: period, | ||
epoch: epoch, | ||
} | ||
} | ||
} | ||
|
||
func TestComputeTipsetTimestampAtEpoch(t *testing.T) { | ||
genesis := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) | ||
period := 30 * time.Second | ||
|
||
generateTipset := tipsetGenerator(genesis, period) | ||
tipset := generateTipset(10) | ||
|
||
t.Run("Basic Functionality", func(t *testing.T) { | ||
targetEpoch := int64(15) | ||
expected := generateTipset(targetEpoch).Timestamp() | ||
actual := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Zero Epoch", func(t *testing.T) { | ||
targetEpoch := int64(0) | ||
expected := generateTipset(targetEpoch).Timestamp() | ||
actual := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Large Epoch", func(t *testing.T) { | ||
largeEpoch := int64(1e6) | ||
expected := generateTipset(largeEpoch).Timestamp() | ||
actual := computeTipsetTimestampAtEpoch(tipset, largeEpoch, period) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Boundary Condition", func(t *testing.T) { | ||
boundaryEpoch := int64(1e3) | ||
expected := generateTipset(boundaryEpoch).Timestamp() | ||
actual := computeTipsetTimestampAtEpoch(tipset, boundaryEpoch, period) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("Consistency", func(t *testing.T) { | ||
targetEpoch := int64(20) | ||
expected := generateTipset(targetEpoch).Timestamp() | ||
actual1 := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period) | ||
actual2 := computeTipsetTimestampAtEpoch(tipset, targetEpoch, period) | ||
assert.Equal(t, expected, actual1) | ||
assert.Equal(t, expected, actual2) | ||
}) | ||
} |