Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Jun 13, 2023
1 parent f170f5d commit 5aae981
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
18 changes: 0 additions & 18 deletions vms/platformvm/txs/executor/proposal_tx_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 32 additions & 8 deletions vms/platformvm/txs/executor/staker_tx_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions vms/platformvm/txs/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
28 changes: 14 additions & 14 deletions vms/platformvm/txs/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

0 comments on commit 5aae981

Please sign in to comment.