From 5aae98182332a6bb21492aed88aa8e4a37f43bb8 Mon Sep 17 00:00:00 2001 From: Alberto Benegiamo Date: Tue, 13 Jun 2023 10:37:51 +0200 Subject: [PATCH] cleanup --- .../txs/executor/proposal_tx_executor.go | 18 --------- .../txs/executor/staker_tx_verification.go | 40 +++++++++++++++---- vms/platformvm/txs/validator.go | 9 ++--- vms/platformvm/txs/validator_test.go | 28 ++++++------- 4 files changed, 50 insertions(+), 45 deletions(-) diff --git a/vms/platformvm/txs/executor/proposal_tx_executor.go b/vms/platformvm/txs/executor/proposal_tx_executor.go index e81976100117..a4c2e953cbd3 100644 --- a/vms/platformvm/txs/executor/proposal_tx_executor.go +++ b/vms/platformvm/txs/executor/proposal_tx_executor.go @@ -700,24 +700,6 @@ func GetValidator(state state.Chain, subnetID ids.ID, nodeID ids.NodeID) (*state return state.GetPendingValidator(subnetID, nodeID) } -// fitsValidationWindow returns true if [delegator]'s period is inside [validator]'s period. -// -// A [delegator]'s period is inside if: -// - [delegator]'s start time is not before [validator]'s start time -// - [delegator]'s end time is not after [validator]'s end time -func fitsValidationWindow( - validator *state.Staker, - delegator *state.Staker, -) bool { - if delegator.StartTime.Before(validator.StartTime) { - return false - } - if delegator.EndTime.After(validator.EndTime) { - return false - } - return true -} - // overDelegated returns true if [validator] will be overdelegated when adding [delegator]. // // A [validator] would become overdelegated if: diff --git a/vms/platformvm/txs/executor/staker_tx_verification.go b/vms/platformvm/txs/executor/staker_tx_verification.go index 91c9f2e8bfea..28dd210339e5 100644 --- a/vms/platformvm/txs/executor/staker_tx_verification.go +++ b/vms/platformvm/txs/executor/staker_tx_verification.go @@ -217,7 +217,12 @@ func verifyAddSubnetValidatorTx( // Ensure that the period this validator validates the specified subnet // is a subset of the time they validate the primary network. - if !tx.Validator.BoundedBy(primaryNetworkValidator.StartTime, primaryNetworkValidator.EndTime) { + if !txs.BoundedBy( + tx.StartTime(), + tx.EndTime(), + primaryNetworkValidator.StartTime, + primaryNetworkValidator.EndTime, + ) { return ErrValidatorSubset } @@ -388,15 +393,22 @@ func verifyAddDelegatorTx( } txID := sTx.ID() - newStaker, err := state.NewPendingStaker(txID, tx) + newDelegator, err := state.NewPendingStaker(txID, tx) if err != nil { return nil, err } - if !fitsValidationWindow(primaryNetworkValidator, newStaker) { + // check that [delegator]'s period is inside [validator]'s period. + if !txs.BoundedBy( + newDelegator.StartTime, + newDelegator.EndTime, + primaryNetworkValidator.StartTime, + primaryNetworkValidator.EndTime, + ) { return nil, ErrPeriodMismatch } - overDelegated, err := overDelegated(chainState, primaryNetworkValidator, maximumWeight, newStaker) + + overDelegated, err := overDelegated(chainState, primaryNetworkValidator, maximumWeight, newDelegator) if err != nil { return nil, err } @@ -526,7 +538,12 @@ func verifyAddPermissionlessValidatorTx( // Ensure that the period this validator validates the specified subnet // is a subset of the time they validate the primary network. - if !tx.Validator.BoundedBy(primaryNetworkValidator.StartTime, primaryNetworkValidator.EndTime) { + if !txs.BoundedBy( + tx.StartTime(), + tx.EndTime(), + primaryNetworkValidator.StartTime, + primaryNetworkValidator.EndTime, + ) { return ErrValidatorSubset } @@ -685,15 +702,22 @@ func verifyAddPermissionlessDelegatorTx( maximumWeight = math.Min(maximumWeight, delegatorRules.maxValidatorStake) txID := sTx.ID() - newStaker, err := state.NewPendingStaker(txID, tx) + newDelegator, err := state.NewPendingStaker(txID, tx) if err != nil { return err } - if !fitsValidationWindow(validator, newStaker) { + // check that [delegator]'s period is inside [validator]'s period. + if !txs.BoundedBy( + newDelegator.StartTime, + newDelegator.EndTime, + validator.StartTime, + validator.EndTime, + ) { return ErrPeriodMismatch } - overDelegated, err := overDelegated(chainState, validator, maximumWeight, newStaker) + + overDelegated, err := overDelegated(chainState, validator, maximumWeight, newDelegator) if err != nil { return err } diff --git a/vms/platformvm/txs/validator.go b/vms/platformvm/txs/validator.go index 79163392fee7..13ee43a9f581 100644 --- a/vms/platformvm/txs/validator.go +++ b/vms/platformvm/txs/validator.go @@ -60,9 +60,8 @@ func (v *Validator) Verify() error { } } -// BoundedBy returns true iff the period that [validator] validates is a -// (non-strict) subset of the time that [other] validates. -// Namely, startTime <= v.StartTime() <= v.EndTime() <= endTime -func (v *Validator) BoundedBy(startTime, endTime time.Time) bool { - return !v.StartTime().Before(startTime) && !v.EndTime().After(endTime) +// BoundedBy returns true iff staker start and end are a +// (non-strict) subset of the provided time bound +func BoundedBy(stakerStart, stakerEnd, lowerBound, upperBound time.Time) bool { + return !stakerStart.Before(lowerBound) && !stakerEnd.After(upperBound) } diff --git a/vms/platformvm/txs/validator_test.go b/vms/platformvm/txs/validator_test.go index 047c7180193b..500129dfaa9d 100644 --- a/vms/platformvm/txs/validator_test.go +++ b/vms/platformvm/txs/validator_test.go @@ -38,54 +38,54 @@ func TestValidatorBoundedBy(t *testing.T) { End: bEndTime, Wght: defaultWeight, } - require.False(a.BoundedBy(b.StartTime(), b.EndTime())) - require.False(b.BoundedBy(a.StartTime(), a.EndTime())) + require.False(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.False(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) // case 2: a starts, b starts, a finishes, b finishes a.Start = 0 b.Start = 1 a.End = 2 b.End = 3 - require.False(a.BoundedBy(b.StartTime(), b.EndTime())) - require.False(b.BoundedBy(a.StartTime(), a.EndTime())) + require.False(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.False(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) // case 3: a starts, b starts, b finishes, a finishes a.Start = 0 b.Start = 1 b.End = 2 a.End = 3 - require.False(a.BoundedBy(b.StartTime(), b.EndTime())) - require.True(b.BoundedBy(a.StartTime(), a.EndTime())) + require.False(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.True(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) // case 4: b starts, a starts, a finishes, b finishes b.Start = 0 a.Start = 1 a.End = 2 b.End = 3 - require.True(a.BoundedBy(b.StartTime(), b.EndTime())) - require.False(b.BoundedBy(a.StartTime(), a.EndTime())) + require.True(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.False(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) // case 5: b starts, b finishes, a starts, a finishes b.Start = 0 b.End = 1 a.Start = 2 a.End = 3 - require.False(a.BoundedBy(b.StartTime(), b.EndTime())) - require.False(b.BoundedBy(a.StartTime(), a.EndTime())) + require.False(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.False(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) // case 6: b starts, a starts, b finishes, a finishes b.Start = 0 a.Start = 1 b.End = 2 a.End = 3 - require.False(a.BoundedBy(b.StartTime(), b.EndTime())) - require.False(b.BoundedBy(a.StartTime(), a.EndTime())) + require.False(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.False(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) // case 3: a starts, b starts, b finishes, a finishes a.Start = 0 b.Start = 0 b.End = 1 a.End = 1 - require.True(a.BoundedBy(b.StartTime(), b.EndTime())) - require.True(b.BoundedBy(a.StartTime(), a.EndTime())) + require.True(BoundedBy(a.StartTime(), a.EndTime(), b.StartTime(), b.EndTime())) + require.True(BoundedBy(b.StartTime(), b.EndTime(), a.StartTime(), a.EndTime())) }