Skip to content
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

P-chain - Cleaned up fork switch in UTs #2746

Merged
merged 7 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved

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
abi87 marked this conversation as resolved.
Show resolved Hide resolved
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
marun marked this conversation as resolved.
Show resolved Hide resolved
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
27 changes: 8 additions & 19 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
Expand All @@ -35,7 +34,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,10 +137,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)

env := newEnvironment(t, ctrl)
env.clk.Set(defaultGenesisTime)
env.config.BanffTime = time.Time{} // activate Banff
env.config.DurangoTime = mockable.MaxTime // deactivate Durango
env := newEnvironment(t, ctrl, banffFork)

// create parentBlock. It's a standard one for simplicity
parentTime := defaultGenesisTime
Expand Down Expand Up @@ -549,8 +545,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.config.BanffTime = time.Time{} // activate Banff
env := newEnvironment(t, nil, banffFork)

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

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

subnetID := testSubnet1.ID()
env.config.TrackedSubnets.Add(subnetID)
Expand Down Expand Up @@ -845,8 +839,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.config.BanffTime = time.Time{} // activate Banff
env := newEnvironment(t, nil, banffFork)

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

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

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

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

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

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

now := env.clk.Time()

Expand Down
Loading
Loading