Skip to content

Restrict ProposerVM P-chain height advancement #3777

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

Merged
merged 15 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion vms/proposervm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (p *postForkCommonComponents) buildChild(

// The child's P-Chain height is proposed as the optimal P-Chain height that
// is at least the parent's P-Chain height
pChainHeight, err := p.vm.optimalPChainHeight(ctx, parentPChainHeight)
pChainHeight, err := p.vm.selectChildPChainHeight(ctx, parentPChainHeight)
if err != nil {
p.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to calculate optimal P-chain height"),
Expand Down
2 changes: 1 addition & 1 deletion vms/proposervm/pre_fork_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (b *preForkBlock) buildChild(ctx context.Context) (Block, error) {

// The child's P-Chain height is proposed as the optimal P-Chain height that
// is at least the minimum height
pChainHeight, err := b.vm.optimalPChainHeight(ctx, b.vm.Upgrades.ApricotPhase4MinPChainHeight)
pChainHeight, err := b.vm.selectChildPChainHeight(ctx, b.vm.Upgrades.ApricotPhase4MinPChainHeight)
if err != nil {
b.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to calculate optimal P-chain height"),
Expand Down
26 changes: 22 additions & 4 deletions vms/proposervm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,31 @@ func (vm *VM) notifyInnerBlockReady() {
}
}

func (vm *VM) optimalPChainHeight(ctx context.Context, minPChainHeight uint64) (uint64, error) {
minimumHeight, err := vm.ctx.ValidatorState.GetMinimumHeight(ctx)
// fujiOverridePChainHeightUntilHeight is the P-chain height at which the
// proposervm will no longer attempt to keep the P-chain height the same.
const fujiOverridePChainHeightUntilHeight = 200041

// fujiOverridePChainHeightUntilTimestamp is the timestamp at which the
// proposervm will no longer attempt to keep the P-chain height the same.
var fujiOverridePChainHeightUntilTimestamp = time.Date(2025, time.March, 7, 17, 0, 0, 0, time.UTC) // noon ET

func (vm *VM) selectChildPChainHeight(ctx context.Context, minPChainHeight uint64) (uint64, error) {
var (
now = vm.Clock.Time()
shouldOverride = vm.ctx.NetworkID == constants.FujiID &&
vm.ctx.SubnetID != constants.PrimaryNetworkID &&
now.Before(fujiOverridePChainHeightUntilTimestamp) &&
minPChainHeight < fujiOverridePChainHeightUntilHeight
)
if shouldOverride {
return minPChainHeight, nil
}

recommendedHeight, err := vm.ctx.ValidatorState.GetMinimumHeight(ctx)
if err != nil {
return 0, err
}

return max(minimumHeight, minPChainHeight), nil
return max(recommendedHeight, minPChainHeight), nil
}

// parseInnerBlock attempts to parse the provided bytes as an inner block. If
Expand Down
89 changes: 89 additions & 0 deletions vms/proposervm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/upgrade/upgradetest"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/proposervm/proposer"
Expand Down Expand Up @@ -2571,3 +2572,91 @@ func TestTimestampMetrics(t *testing.T) {
})
}
}

func TestSelectChildPChainHeight(t *testing.T) {
var (
activationTime = time.Unix(0, 0)
durangoTime = activationTime

beforeOverrideEnds = fujiOverridePChainHeightUntilTimestamp.Add(-time.Minute)
)
for _, test := range []struct {
name string
time time.Time
networkID uint32
subnetID ids.ID
currentPChainHeight uint64
minPChainHeight uint64
expectedPChainHeight uint64
}{
{
name: "no override - mainnet",
time: beforeOverrideEnds,
networkID: constants.MainnetID,
subnetID: ids.GenerateTestID(),
currentPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
minPChainHeight: fujiOverridePChainHeightUntilHeight - 5,
expectedPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
},
{
name: "no override - primary network",
time: beforeOverrideEnds,
networkID: constants.FujiID,
subnetID: constants.PrimaryNetworkID,
currentPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
minPChainHeight: fujiOverridePChainHeightUntilHeight - 5,
expectedPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
},
{
name: "no override - expired network",
time: fujiOverridePChainHeightUntilTimestamp,
networkID: constants.FujiID,
subnetID: ids.GenerateTestID(),
currentPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
minPChainHeight: fujiOverridePChainHeightUntilHeight - 5,
expectedPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
},
{
name: "no override - chain previously advanced",
time: beforeOverrideEnds,
networkID: constants.FujiID,
subnetID: ids.GenerateTestID(),
currentPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
minPChainHeight: fujiOverridePChainHeightUntilHeight + 1,
expectedPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
},
{
name: "override",
time: beforeOverrideEnds,
networkID: constants.FujiID,
subnetID: ids.GenerateTestID(),
currentPChainHeight: fujiOverridePChainHeightUntilHeight + 2,
minPChainHeight: fujiOverridePChainHeightUntilHeight - 5,
expectedPChainHeight: fujiOverridePChainHeightUntilHeight - 5,
},
} {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

_, vdrState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0)
defer func() {
require.NoError(proVM.Shutdown(context.Background()))
}()

proVM.Clock.Set(test.time)
proVM.ctx.NetworkID = test.networkID
proVM.ctx.SubnetID = test.subnetID

vdrState.GetMinimumHeightF = func(context.Context) (uint64, error) {
return test.currentPChainHeight, nil
}

actualPChainHeight, err := proVM.selectChildPChainHeight(
context.Background(),
test.minPChainHeight,
)
require.NoError(err)
require.Equal(test.expectedPChainHeight, actualPChainHeight)
})
}
}
Loading