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

Drop Pending Stakers 1 - introduced ScheduledStaker txs #2323

Merged
merged 41 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6079eb0
introduced pre durango staker tx
abi87 Nov 16, 2023
c6736b6
nit
abi87 Nov 16, 2023
944f309
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Nov 17, 2023
0106d75
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Nov 18, 2023
9a9897c
nit
abi87 Nov 18, 2023
d17f2a2
nits
abi87 Nov 18, 2023
4c4c011
nits to reduce future diffs
abi87 Nov 19, 2023
905e55e
nit
abi87 Nov 19, 2023
89094cd
improved e2e test
abi87 Nov 19, 2023
5dd8dd8
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 19, 2023
698af90
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 21, 2023
0c631c6
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 21, 2023
c3e3670
nits
abi87 Nov 21, 2023
9606b77
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 22, 2023
ce87dea
nits
abi87 Nov 22, 2023
fd33fde
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 22, 2023
6ea5add
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 23, 2023
b820cae
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 23, 2023
8260546
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 27, 2023
73d5c2f
nit
abi87 Nov 27, 2023
84321ef
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 28, 2023
c353c33
nits
abi87 Nov 28, 2023
d296e52
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 28, 2023
9bd9d60
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 28, 2023
a99a5f7
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 29, 2023
c07e2c8
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 29, 2023
5bb301b
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 29, 2023
e6463b7
Merge branch 'dev' into post_durango_nits_0
abi87 Nov 30, 2023
43839ee
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Nov 30, 2023
15d7871
Merge branch 'dev' into post_durango_nits_0
abi87 Dec 1, 2023
5e10688
nits
abi87 Dec 1, 2023
0e40da4
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Dec 1, 2023
a5b3875
Merge branch 'dev' into post_durango_nits_0
abi87 Dec 3, 2023
a7daf81
Merge branch 'post_durango_nits_0' into post_durango_staker_txs_0
abi87 Dec 3, 2023
fb85fe1
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Dec 4, 2023
bfc29a1
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Dec 5, 2023
9b97b59
fix
abi87 Dec 5, 2023
0684a8a
nits
StephenButtolph Dec 5, 2023
d914f34
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Dec 6, 2023
89ce3c2
fix error
abi87 Dec 6, 2023
fa9269c
simplify comment
StephenButtolph Dec 6, 2023
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: 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
22 changes: 3 additions & 19 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,38 +123,22 @@ func (b *builder) buildBlock() (block.Block, error) {
return nil, err
}
nextHeight := preferred.Height() + 1

preferredState, ok := b.blkManager.GetState(preferredID)
if !ok {
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)
nextTimeStamp, 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,
nextHeight,
timestamp,
nextTimeStamp,
timeWasCapped,
preferredState,
)
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/state/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *Staker) Less(than *Staker) bool {
return bytes.Compare(s.TxID[:], than.TxID[:]) == -1
}

func NewCurrentStaker(txID ids.ID, staker txs.Staker, potentialReward uint64) (*Staker, error) {
func NewCurrentStaker(txID ids.ID, staker txs.ScheduledStaker, potentialReward uint64) (*Staker, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in next PR we'll change the signature of this function to use txs.Staker instead of txs.ScheduledStaker.

publicKey, _, err := staker.PublicKey()
if err != nil {
return nil, err
Expand All @@ -103,7 +103,7 @@ func NewCurrentStaker(txID ids.ID, staker txs.Staker, potentialReward uint64) (*
}, nil
}

func NewPendingStaker(txID ids.ID, staker txs.Staker) (*Staker, error) {
func NewPendingStaker(txID ids.ID, staker txs.ScheduledStaker) (*Staker, error) {
publicKey, _, err := staker.PublicKey()
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions vms/platformvm/state/staker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func TestNewCurrentStaker(t *testing.T) {
subnetID := ids.GenerateTestID()
weight := uint64(12345)
startTime := time.Now()
endTime := time.Now()
endTime := startTime.Add(time.Hour)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a nit, not strictly relevant to this PR, but I'd rather take it out of the way

potentialReward := uint64(54321)
currentPriority := txs.SubnetPermissionedValidatorCurrentPriority

stakerTx := txs.NewMockStaker(ctrl)
stakerTx := txs.NewMockScheduledStaker(ctrl)
stakerTx.EXPECT().NodeID().Return(nodeID)
stakerTx.EXPECT().PublicKey().Return(publicKey, true, nil)
stakerTx.EXPECT().SubnetID().Return(subnetID)
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestNewPendingStaker(t *testing.T) {
endTime := time.Now()
pendingPriority := txs.SubnetPermissionedValidatorPendingPriority

stakerTx := txs.NewMockStaker(ctrl)
stakerTx := txs.NewMockScheduledStaker(ctrl)
stakerTx.EXPECT().NodeID().Return(nodeID)
stakerTx.EXPECT().PublicKey().Return(publicKey, true, nil)
stakerTx.EXPECT().SubnetID().Return(subnetID)
Expand Down
32 changes: 16 additions & 16 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ func (s *state) syncGenesis(genesisBlk block.Block, genesis *genesis.Genesis) er
return fmt.Errorf("expected tx type *txs.AddValidatorTx but got %T", vdrTx.Unsigned)
}

stakeAmount := tx.Validator.Wght
stakeAmount := tx.Weight()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a nit, not strictly relevant to this PR, but I'd rather take it out of the way

stakeDuration := tx.Validator.Duration()
currentSupply, err := s.GetCurrentSupply(constants.PrimaryNetworkID)
if err != nil {
Expand Down Expand Up @@ -1447,7 +1447,11 @@ func (s *state) loadCurrentValidators() error {
}
tx, _, err := s.GetTx(txID)
if err != nil {
return err
return fmt.Errorf("failed loading validator transaction txID %v, %w", txID, err)
}
stakerTx, ok := tx.Unsigned.(txs.ScheduledStaker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

metadataBytes := validatorIt.Value()
Expand All @@ -1460,11 +1464,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 All @@ -1491,17 +1490,18 @@ func (s *state) loadCurrentValidators() error {
return err
}

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

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 @@ -1537,6 +1537,11 @@ func (s *state) loadCurrentValidators() error {
return err
}

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

metadata := &delegatorMetadata{
txID: txID,
}
Expand All @@ -1545,11 +1550,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 @@ -1594,7 +1594,7 @@ func (s *state) loadPendingValidators() error {
return err
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
stakerTx, ok := tx.Unsigned.(txs.ScheduledStaker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}
Expand Down Expand Up @@ -1629,7 +1629,7 @@ func (s *state) loadPendingValidators() error {
return err
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
stakerTx, ok := tx.Unsigned.(txs.ScheduledStaker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}
Expand Down
3 changes: 2 additions & 1 deletion vms/platformvm/txs/add_delegator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var (
_ DelegatorTx = (*AddDelegatorTx)(nil)
_ DelegatorTx = (*AddDelegatorTx)(nil)
_ ScheduledStaker = (*AddDelegatorTx)(nil)
Comment on lines +22 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no change to existing tx format


errDelegatorWeightMismatch = errors.New("delegator weight is not equal to total stake weight")
errStakeMustBeAVAX = errors.New("stake must be AVAX")
Expand Down
5 changes: 4 additions & 1 deletion vms/platformvm/txs/add_permissionless_delegator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var _ DelegatorTx = (*AddPermissionlessDelegatorTx)(nil)
var (
_ DelegatorTx = (*AddPermissionlessDelegatorTx)(nil)
_ ScheduledStaker = (*AddPermissionlessDelegatorTx)(nil)
)

// AddPermissionlessDelegatorTx is an unsigned addPermissionlessDelegatorTx
type AddPermissionlessDelegatorTx struct {
Expand Down
3 changes: 2 additions & 1 deletion vms/platformvm/txs/add_permissionless_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

var (
_ ValidatorTx = (*AddPermissionlessValidatorTx)(nil)
_ ValidatorTx = (*AddPermissionlessValidatorTx)(nil)
_ ScheduledStaker = (*AddPermissionlessDelegatorTx)(nil)

errEmptyNodeID = errors.New("validator nodeID cannot be empty")
errNoStake = errors.New("no stake")
Expand Down
3 changes: 2 additions & 1 deletion vms/platformvm/txs/add_subnet_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

var (
_ StakerTx = (*AddSubnetValidatorTx)(nil)
_ StakerTx = (*AddSubnetValidatorTx)(nil)
_ ScheduledStaker = (*AddSubnetValidatorTx)(nil)

errAddPrimaryNetworkValidator = errors.New("can't add primary network validator with AddSubnetValidatorTx")
)
Expand Down
3 changes: 2 additions & 1 deletion vms/platformvm/txs/add_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var (
_ ValidatorTx = (*AddValidatorTx)(nil)
_ ValidatorTx = (*AddValidatorTx)(nil)
_ ScheduledStaker = (*AddValidatorTx)(nil)

errTooManyShares = fmt.Errorf("a staker can only require at most %d shares from delegators", reward.PercentDenominator)
)
Expand Down
Loading
Loading