Skip to content

Commit

Permalink
Drop Pending Stakers 1 - introduced ScheduledStaker txs (#2323)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
abi87 and StephenButtolph authored Dec 6, 2023
1 parent 7df1f3a commit b36416d
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 49 deletions.
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) {
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
4 changes: 2 additions & 2 deletions vms/platformvm/state/staker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestNewCurrentStaker(t *testing.T) {
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
18 changes: 11 additions & 7 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,9 +1320,13 @@ func (s *state) syncGenesis(genesisBlk block.Block, genesis *genesis.Genesis) er

// Persist primary network validator set at genesis
for _, vdrTx := range genesis.Validators {
validatorTx, ok := vdrTx.Unsigned.(txs.ValidatorTx)
// We expect genesis validator txs to be either AddValidatorTx or
// AddPermissionlessValidatorTx.
//
// TODO: Enforce stricter type check
validatorTx, ok := vdrTx.Unsigned.(txs.ScheduledStaker)
if !ok {
return fmt.Errorf("expected tx type txs.ValidatorTx but got %T", vdrTx.Unsigned)
return fmt.Errorf("expected a scheduled staker but got %T", vdrTx.Unsigned)
}

stakeAmount := validatorTx.Weight()
Expand Down Expand Up @@ -1451,7 +1455,7 @@ func (s *state) loadCurrentValidators() error {
return fmt.Errorf("failed loading validator transaction txID %s, %w", txID, 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 @@ -1492,7 +1496,7 @@ 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)
}
Expand Down Expand Up @@ -1539,7 +1543,7 @@ 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)
}
Expand Down Expand Up @@ -1596,7 +1600,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 @@ -1631,7 +1635,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)

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
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/standard_tx_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func (e *StandardTxExecutor) BaseTx(tx *txs.BaseTx) error {
}

// Creates the staker as defined in [stakerTx] and adds it to [e.State].
func (e *StandardTxExecutor) putStaker(stakerTx txs.Staker) error {
func (e *StandardTxExecutor) putStaker(stakerTx txs.ScheduledStaker) error {
txID := e.Tx.ID()
staker, err := state.NewPendingStaker(txID, stakerTx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (m *mempool) DropExpiredStakerTxs(minStartTime time.Time) []ids.ID {
txIter := m.unissuedTxs.NewIterator()
for txIter.Next() {
tx := txIter.Value()
stakerTx, ok := tx.Unsigned.(txs.Staker)
stakerTx, ok := tx.Unsigned.(txs.ScheduledStaker)
if !ok {
continue
}
Expand Down
151 changes: 151 additions & 0 deletions vms/platformvm/txs/mock_scheduled_staker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 0 additions & 28 deletions vms/platformvm/txs/mock_staker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions vms/platformvm/txs/staker_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ type Staker interface {
// PublicKey returns the BLS public key registered by this transaction. If
// there was no key registered by this transaction, it will return false.
PublicKey() (*bls.PublicKey, bool, error)
StartTime() time.Time
EndTime() time.Time
Weight() uint64
PendingPriority() Priority
CurrentPriority() Priority
}

type ScheduledStaker interface {
Staker
StartTime() time.Time
PendingPriority() Priority
}
2 changes: 1 addition & 1 deletion vms/platformvm/validator_set_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ func addPrimaryValidatorWithoutBLSKey(vm *VM, data *validatorInputData) (*state.
}

func internalAddValidator(vm *VM, signedTx *txs.Tx) (*state.Staker, error) {
stakerTx := signedTx.Unsigned.(txs.StakerTx)
if err := vm.Network.IssueTx(context.Background(), signedTx); err != nil {
return nil, fmt.Errorf("could not add tx to mempool: %w", err)
}
Expand All @@ -393,6 +392,7 @@ func internalAddValidator(vm *VM, signedTx *txs.Tx) (*state.Staker, error) {
}

// move time ahead, promoting the validator to current
stakerTx := signedTx.Unsigned.(txs.ScheduledStaker)
currentTime := stakerTx.StartTime()
vm.clock.Set(currentTime)
vm.state.SetTimestamp(currentTime)
Expand Down

0 comments on commit b36416d

Please sign in to comment.