Skip to content

Commit

Permalink
cleaned up fork switch in UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Feb 18, 2024
1 parent 441b3a4 commit a4746ce
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 91 deletions.
18 changes: 9 additions & 9 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
func TestBuildBlockBasic(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -75,7 +75,7 @@ func TestBuildBlockBasic(t *testing.T) {
func TestBuildBlockDoesNotBuildWithEmptyMempool(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand All @@ -92,7 +92,7 @@ func TestBuildBlockDoesNotBuildWithEmptyMempool(t *testing.T) {
func TestBuildBlockShouldReward(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -194,7 +194,7 @@ func TestBuildBlockShouldReward(t *testing.T) {
func TestBuildBlockAdvanceTime(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -227,7 +227,7 @@ func TestBuildBlockAdvanceTime(t *testing.T) {
func TestBuildBlockForceAdvanceTime(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -282,7 +282,7 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) {
func TestBuildBlockDropExpiredStakerTxs(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -388,7 +388,7 @@ func TestBuildBlockDropExpiredStakerTxs(t *testing.T) {
func TestBuildBlockInvalidStakingDurations(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -474,7 +474,7 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) {
func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down Expand Up @@ -518,7 +518,7 @@ func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) {
func TestNoErrorOnUnexpectedSetPreferenceDuringBootstrapping(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down
52 changes: 46 additions & 6 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package builder

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -57,8 +58,18 @@ import (
const (
defaultWeight = 10000
trackChecksum = false

apricotPhase3 activeFork = iota
apricotPhase5
banffFork
cortinaFork
durangoFork

latestFork activeFork = durangoFork
)

type activeFork uint8

var (
defaultMinStakingDuration = 24 * time.Hour
defaultMaxStakingDuration = 365 * 24 * time.Hour
Expand Down Expand Up @@ -110,12 +121,12 @@ type environment struct {
backend txexecutor.Backend
}

func newEnvironment(t *testing.T) *environment {
func newEnvironment(t *testing.T, fork activeFork) *environment { //nolint:unparam
require := require.New(t)

res := &environment{
isBootstrapped: &utils.Atomic[bool]{},
config: defaultConfig(),
config: defaultConfig(t, fork),
clk: defaultClock(),
}
res.isBootstrapped.Set(true)
Expand Down Expand Up @@ -293,7 +304,34 @@ func defaultState(
return state
}

func defaultConfig() *config.Config {
func defaultConfig(t *testing.T, fork activeFork) *config.Config {
var (
apricotPhase3Time = mockable.MaxTime
apricotPhase5Time = mockable.MaxTime
banffTime = mockable.MaxTime
cortinaTime = mockable.MaxTime
durangoTime = mockable.MaxTime
)

switch fork {
case durangoFork:
durangoTime = time.Time{} // neglecting fork ordering this for package tests
fallthrough
case cortinaFork:
cortinaTime = time.Time{} // neglecting fork ordering this for package tests
fallthrough
case banffFork:
banffTime = time.Time{} // neglecting fork ordering this for package tests
fallthrough
case apricotPhase5:
apricotPhase5Time = defaultValidateEndTime
fallthrough
case apricotPhase3:
apricotPhase3Time = defaultValidateEndTime
default:
require.NoError(t, fmt.Errorf("unhandled fork %d", fork))
}

return &config.Config{
Chains: chains.TestManager,
UptimeLockedCalculator: uptime.NewLockedCalculator(),
Expand All @@ -312,9 +350,11 @@ func defaultConfig() *config.Config {
MintingPeriod: 365 * 24 * time.Hour,
SupplyCap: 720 * units.MegaAvax,
},
ApricotPhase3Time: defaultValidateEndTime,
ApricotPhase5Time: defaultValidateEndTime,
BanffTime: time.Time{}, // neglecting fork ordering this for package tests
ApricotPhase3Time: apricotPhase3Time,
ApricotPhase5Time: apricotPhase5Time,
BanffTime: banffTime,
CortinaTime: cortinaTime,
DurangoTime: durangoTime,
}
}

Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/builder/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func TestAtomicTxImports(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env := newEnvironment(t, latestFork)
env.ctx.Lock.Lock()
defer env.ctx.Lock.Unlock()

Expand Down
49 changes: 43 additions & 6 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ const (

defaultWeight = 10000
trackChecksum = false

apricotPhase3 activeFork = iota
apricotPhase5
banffFork
cortinaFork
durangoFork
)

type activeFork uint8

var (
defaultMinStakingDuration = 24 * time.Hour
defaultMaxStakingDuration = 365 * 24 * time.Hour
Expand Down Expand Up @@ -124,10 +132,10 @@ type environment struct {
backend *executor.Backend
}

func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
func newEnvironment(t *testing.T, ctrl *gomock.Controller, fork activeFork) *environment {
res := &environment{
isBootstrapped: &utils.Atomic[bool]{},
config: defaultConfig(),
config: defaultConfig(t, fork),
clk: defaultClock(),
}
res.isBootstrapped.Set(true)
Expand Down Expand Up @@ -320,7 +328,34 @@ func defaultState(
return state
}

func defaultConfig() *config.Config {
func defaultConfig(t *testing.T, fork activeFork) *config.Config {
var (
apricotPhase3Time = mockable.MaxTime
apricotPhase5Time = mockable.MaxTime
banffTime = mockable.MaxTime
cortinaTime = mockable.MaxTime
durangoTime = mockable.MaxTime
)

switch fork {
case durangoFork:
durangoTime = time.Time{} // neglecting fork ordering this for package tests
fallthrough
case cortinaFork:
cortinaTime = time.Time{} // neglecting fork ordering this for package tests
fallthrough
case banffFork:
banffTime = time.Time{} // neglecting fork ordering this for package tests
fallthrough
case apricotPhase5:
apricotPhase5Time = defaultValidateEndTime
fallthrough
case apricotPhase3:
apricotPhase3Time = defaultValidateEndTime
default:
require.NoError(t, fmt.Errorf("unhandled fork %d", fork))
}

return &config.Config{
Chains: chains.TestManager,
UptimeLockedCalculator: uptime.NewLockedCalculator(),
Expand All @@ -339,9 +374,11 @@ func defaultConfig() *config.Config {
MintingPeriod: 365 * 24 * time.Hour,
SupplyCap: 720 * units.MegaAvax,
},
ApricotPhase3Time: defaultValidateEndTime,
ApricotPhase5Time: defaultValidateEndTime,
BanffTime: mockable.MaxTime,
ApricotPhase3Time: apricotPhase3Time,
ApricotPhase5Time: apricotPhase5Time,
BanffTime: banffTime,
CortinaTime: cortinaTime,
DurangoTime: durangoTime,
}
}

Expand Down
16 changes: 8 additions & 8 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
env := newEnvironment(t, ctrl, apricotPhase5)

// create apricotParentBlk. It's a standard one for simplicity
parentHeight := uint64(2022)
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
env := newEnvironment(t, ctrl, banffFork)
env.clk.Set(defaultGenesisTime)
env.config.BanffTime = time.Time{} // activate Banff
env.config.DurangoTime = mockable.MaxTime // deactivate Durango
Expand Down Expand Up @@ -549,7 +549,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -702,7 +702,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {

func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -845,7 +845,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
for _, tracked := range []bool{true, false} {
t.Run(fmt.Sprintf("tracked %t", tracked), func(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -950,7 +950,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {

func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

// Case: Timestamp is after next validator start time
Expand Down Expand Up @@ -1135,7 +1135,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {

func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

// Case: Timestamp is after next validator start time
Expand Down Expand Up @@ -1320,7 +1320,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {

func TestAddValidatorProposalBlock(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, apricotPhase5)
env.config.BanffTime = time.Time{} // activate Banff
env.config.DurangoTime = time.Time{} // activate Durango

Expand Down
14 changes: 7 additions & 7 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestApricotStandardBlockTimeVerification(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
env := newEnvironment(t, ctrl, apricotPhase5)

// setup and store parent block
// it's a standard block for simplicity
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
env := newEnvironment(t, ctrl, banffFork)
now := env.clk.Time()
env.clk.Set(now)
env.config.BanffTime = time.Time{} // activate Banff
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) {
func TestBanffStandardBlockUpdatePrimaryNetworkStakers(t *testing.T) {
require := require.New(t)

env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

// Case: Timestamp is after next validator start time
Expand Down Expand Up @@ -492,7 +492,7 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {
// is after the new timestamp
func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -690,7 +690,7 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) {
for _, tracked := range []bool{true, false} {
t.Run(fmt.Sprintf("tracked %t", tracked), func(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -751,7 +751,7 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) {

func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) {
require := require.New(t)
env := newEnvironment(t, nil)
env := newEnvironment(t, nil, banffFork)
env.config.BanffTime = time.Time{} // activate Banff

// Case: Timestamp is after next validator start time
Expand Down
Loading

0 comments on commit a4746ce

Please sign in to comment.