Skip to content

Commit

Permalink
Fix timings in e2e tests (#5304)
Browse files Browse the repository at this point in the history
## Motivation
Fix flaky tests in `activation/e2e`. Test would randomly fail with the NIPostBuilder failing to fetch a poet proof.

## Changes
- ensure that the spawned PoET and the nipost builder use the same genesis time instead of `time.Now()`
- update PoET config to ensure that there is enough time to fetch a proof from PoET and retry in case PoET is slightly slow to generate the proof and the first request by NIPostBuilder fails

## Test Plan
n/a

## TODO
<!-- This section should be removed when all items are complete -->
- [x] Explain motivation or link existing issue(s)
- [x] Test changes and document test plan
- [x] Update documentation as needed
- [x] Update [changelog](../CHANGELOG.md) as needed
  • Loading branch information
fasmat committed Nov 27, 2023
1 parent 5c50473 commit dbc1183
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
24 changes: 12 additions & 12 deletions activation/e2e/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,20 @@ func TestNIPostBuilderWithClients(t *testing.T) {
opts.Scrypt.N = 2 // Speedup initialization in tests.
initPost(t, logger.Named("manager"), mgr, opts)

// ensure that genesis aligns with layer timings
genesis := time.Now().Add(layerDuration).Round(layerDuration)
epoch := layersPerEpoch * layerDuration
poetCfg := activation.PoetConfig{
PhaseShift: epoch / 2,
CycleGap: epoch / 5,
CycleGap: epoch / 4,
GracePeriod: epoch / 5,
RequestTimeout: epoch / 5,
RequestRetryDelay: epoch / 50,
MaxRequestRetries: 10,
}
poetProver := spawnPoet(
t,
WithGenesis(time.Now()),
WithGenesis(genesis),
WithEpochDuration(epoch),
WithPhaseShift(poetCfg.PhaseShift),
WithCycleGap(poetCfg.CycleGap),
Expand All @@ -154,8 +156,6 @@ func TestNIPostBuilderWithClients(t *testing.T) {
mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
func(got types.LayerID) time.Time {
// time.Now() ~= currentLayer
genesis := time.Now().Add(-time.Duration(postGenesisEpoch.FirstLayer()) * layerDuration)
return genesis.Add(layerDuration * time.Duration(got))
},
)
Expand Down Expand Up @@ -266,18 +266,20 @@ func TestNewNIPostBuilderNotInitialized(t *testing.T) {
mgr, err := activation.NewPostSetupManager(sig.NodeID(), cfg, logger, cdb, goldenATX)
require.NoError(t, err)

// ensure that genesis aligns with layer timings
genesis := time.Now().Add(layerDuration).Round(layerDuration)
epoch := layersPerEpoch * layerDuration
poetCfg := activation.PoetConfig{
PhaseShift: epoch / 5,
CycleGap: epoch / 10,
GracePeriod: epoch / 10,
RequestTimeout: epoch / 10,
RequestRetryDelay: epoch / 100,
PhaseShift: epoch / 2,
CycleGap: epoch / 4,
GracePeriod: epoch / 5,
RequestTimeout: epoch / 5,
RequestRetryDelay: epoch / 50,
MaxRequestRetries: 10,
}
poetProver := spawnPoet(
t,
WithGenesis(time.Now()),
WithGenesis(genesis),
WithEpochDuration(epoch),
WithPhaseShift(poetCfg.PhaseShift),
WithCycleGap(poetCfg.CycleGap),
Expand All @@ -286,8 +288,6 @@ func TestNewNIPostBuilderNotInitialized(t *testing.T) {
mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
func(got types.LayerID) time.Time {
// time.Now() ~= currentLayer
genesis := time.Now().Add(-time.Duration(postGenesisEpoch.FirstLayer()) * layerDuration)
return genesis.Add(layerDuration * time.Duration(got))
},
)
Expand Down
8 changes: 4 additions & 4 deletions activation/e2e/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ func TestValidator_Validate(t *testing.T) {
opts.Scrypt.N = 2 // Speedup initialization in tests.
initPost(t, logger.Named("manager"), mgr, opts)

// ensure that genesis aligns with layer timings
genesis := time.Now().Add(layerDuration).Round(layerDuration)
epoch := layersPerEpoch * layerDuration
poetCfg := activation.PoetConfig{
PhaseShift: epoch / 2,
CycleGap: epoch / 5,
CycleGap: epoch / 4,
GracePeriod: epoch / 5,
RequestTimeout: epoch / 5,
RequestRetryDelay: epoch / 50,
MaxRequestRetries: 10,
}
poetProver := spawnPoet(
t,
WithGenesis(time.Now()),
WithGenesis(genesis),
WithEpochDuration(epoch),
WithPhaseShift(poetCfg.PhaseShift),
WithCycleGap(poetCfg.CycleGap),
Expand All @@ -61,8 +63,6 @@ func TestValidator_Validate(t *testing.T) {
mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
func(got types.LayerID) time.Time {
// time.Now() ~= currentLayer
genesis := time.Now().Add(-time.Duration(postGenesisEpoch.FirstLayer()) * layerDuration)
return genesis.Add(layerDuration * time.Duration(got))
},
)
Expand Down
6 changes: 3 additions & 3 deletions common/types/poet.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func (p *PoetProofMessage) MarshalLogObject(encoder log.ObjectEncoder) error {
}

// Ref returns the reference to the PoET proof message. It's the blake3 sum of the entire proof message.
func (proofMessage *PoetProofMessage) Ref() (PoetProofRef, error) {
poetProofBytes, err := codec.Encode(&proofMessage.PoetProof)
func (p *PoetProofMessage) Ref() (PoetProofRef, error) {
poetProofBytes, err := codec.Encode(&p.PoetProof)
if err != nil {
return PoetProofRef{}, fmt.Errorf("failed to marshal poet proof for poetId %x round %v: %w",
proofMessage.PoetServiceID, proofMessage.RoundID, err)
p.PoetServiceID, p.RoundID, err)
}
h := CalcHash32(poetProofBytes)
return (PoetProofRef)(h), nil
Expand Down
1 change: 0 additions & 1 deletion config/presets/fastnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func fastnet() config.Config {

conf.BaseConfig.OptFilterThreshold = 90
conf.BaseConfig.DatabasePruneInterval = time.Minute
conf.ATXGradeDelay = 1 * time.Second

// set for systest TestEquivocation
conf.BaseConfig.MinerGoodAtxsPercent = 50
Expand Down

0 comments on commit dbc1183

Please sign in to comment.