Skip to content

Commit

Permalink
Drop Pending Stakers 0 - De-duplicate staking tx verification (#2335)
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 authored Dec 4, 2023
1 parent 05ce366 commit c11accd
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 166 deletions.
18 changes: 12 additions & 6 deletions tests/e2e/p/staking_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,18 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
betaNodeID, betaPOP, err := betaInfoClient.GetNodeID(e2e.DefaultContext())
require.NoError(err)

pvmClient := platformvm.NewClient(alphaNode.GetProcessContext().URI)

const (
delegationPercent = 0.10 // 10%
delegationShare = reward.PercentDenominator * delegationPercent
weight = 2_000 * units.Avax
)

ginkgo.By("retrieving supply before inserting validators")
supplyAtValidatorsStart, _, err := pvmClient.GetCurrentSupply(e2e.DefaultContext(), constants.PrimaryNetworkID)
require.NoError(err)

alphaValidatorStartTime := time.Now().Add(e2e.DefaultValidatorStartTimeDiff)
alphaValidatorEndTime := alphaValidatorStartTime.Add(validationPeriod)
tests.Outf("alpha node validation period starting at: %v\n", alphaValidatorStartTime)
Expand Down Expand Up @@ -171,6 +177,10 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
require.NoError(err)
})

ginkgo.By("retrieving supply before inserting delegators")
supplyAtDelegatorsStart, _, err := pvmClient.GetCurrentSupply(e2e.DefaultContext(), constants.PrimaryNetworkID)
require.NoError(err)

gammaDelegatorStartTime := time.Now().Add(e2e.DefaultValidatorStartTimeDiff)
tests.Outf("gamma delegation period starting at: %v\n", gammaDelegatorStartTime)

Expand Down Expand Up @@ -227,8 +237,6 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
// delegation periods are shorter than the validation periods.
time.Sleep(time.Until(betaValidatorEndTime))

pvmClient := platformvm.NewClient(alphaNode.GetProcessContext().URI)

ginkgo.By("waiting until the alpha and beta nodes are no longer validators")
e2e.Eventually(func() bool {
validators, err := pvmClient.GetCurrentValidators(e2e.DefaultContext(), constants.PrimaryNetworkID, nil)
Expand Down Expand Up @@ -270,11 +278,9 @@ var _ = ginkgo.Describe("[Staking Rewards]", func() {
require.Len(rewardBalances, len(rewardKeys))

ginkgo.By("determining expected validation and delegation rewards")
currentSupply, _, err := pvmClient.GetCurrentSupply(e2e.DefaultContext(), constants.PrimaryNetworkID)
require.NoError(err)
calculator := reward.NewCalculator(rewardConfig)
expectedValidationReward := calculator.Calculate(validationPeriod, weight, currentSupply)
potentialDelegationReward := calculator.Calculate(delegationPeriod, weight, currentSupply)
expectedValidationReward := calculator.Calculate(validationPeriod, weight, supplyAtValidatorsStart)
potentialDelegationReward := calculator.Calculate(delegationPeriod, weight, supplyAtDelegatorsStart)
expectedDelegationFee, expectedDelegatorReward := reward.Split(potentialDelegationReward, delegationShare)

ginkgo.By("checking expected rewards against actual rewards")
Expand Down
19 changes: 1 addition & 18 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,11 @@ func (b *builder) buildBlock() (block.Block, error) {
return nil, fmt.Errorf("%w: %s", state.ErrMissingParentState, preferredID)
}

timestamp := b.txExecutorBackend.Clk.Time()
if parentTime := preferred.Timestamp(); parentTime.After(timestamp) {
timestamp = parentTime
}
// [timestamp] = max(now, parentTime)

nextStakerChangeTime, err := txexecutor.GetNextStakerChangeTime(preferredState)
timestamp, timeWasCapped, err := txexecutor.NextBlockTime(preferredState, b.txExecutorBackend.Clk)
if err != nil {
return nil, fmt.Errorf("could not calculate next staker change time: %w", err)
}

// timeWasCapped means that [timestamp] was reduced to
// [nextStakerChangeTime]. It is used as a flag for [buildApricotBlock] to
// be willing to issue an advanceTimeTx. It is also used as a flag for
// [buildBanffBlock] to force the issuance of an empty block to advance
// the time forward; if there are no available transactions.
timeWasCapped := !timestamp.Before(nextStakerChangeTime)
if timeWasCapped {
timestamp = nextStakerChangeTime
}
// [timestamp] = min(max(now, parentTime), nextStakerChangeTime)

return buildBlock(
b,
preferredID,
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/state/staker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestNewCurrentStaker(t *testing.T) {
subnetID := ids.GenerateTestID()
weight := uint64(12345)
startTime := time.Now()
endTime := time.Now()
endTime := startTime.Add(time.Hour)
potentialReward := uint64(54321)
currentPriority := txs.SubnetPermissionedValidatorCurrentPriority

Expand Down
25 changes: 13 additions & 12 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,12 @@ func (s *state) loadCurrentValidators() error {
}
tx, _, err := s.GetTx(txID)
if err != nil {
return err
return fmt.Errorf("failed loading validator transaction txID %s, %w", txID, err)
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

metadataBytes := validatorIt.Value()
Expand All @@ -1461,11 +1466,6 @@ func (s *state) loadCurrentValidators() error {
return err
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

staker, err := NewCurrentStaker(txID, stakerTx, metadata.PotentialReward)
if err != nil {
return err
Expand Down Expand Up @@ -1498,11 +1498,12 @@ func (s *state) loadCurrentValidators() error {
}

metadataBytes := subnetValidatorIt.Value()
startTime := stakerTx.StartTime()
metadata := &validatorMetadata{
txID: txID,
// use the start time as the fallback value
// in case it's not stored in the database
LastUpdated: uint64(stakerTx.StartTime().Unix()),
LastUpdated: uint64(startTime.Unix()),
}
if err := parseValidatorMetadata(metadataBytes, metadata); err != nil {
return err
Expand Down Expand Up @@ -1538,6 +1539,11 @@ func (s *state) loadCurrentValidators() error {
return err
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

metadata := &delegatorMetadata{
txID: txID,
}
Expand All @@ -1546,11 +1552,6 @@ func (s *state) loadCurrentValidators() error {
return err
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

staker, err := NewCurrentStaker(txID, stakerTx, metadata.PotentialReward)
if err != nil {
return err
Expand Down
Loading

0 comments on commit c11accd

Please sign in to comment.