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 2 - Replace txs.ScheduledStaker with txs.Staker #2305

Merged
merged 74 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
17723e2
wip: introduced post durango staker tx
abi87 Nov 14, 2023
88130e4
TO BE SQUASHED
abi87 Nov 14, 2023
acc1517
nit
abi87 Nov 14, 2023
3ec3cd2
Merge branch 'dev' into post_durango_staker_txs
abi87 Nov 15, 2023
19a8318
nit
abi87 Nov 15, 2023
b213740
updated PR to acp proposal
abi87 Nov 15, 2023
9e7f851
Merge branch 'dev' into post_durango_staker_txs
abi87 Nov 16, 2023
13d150f
Merge branch 'dev' into post_durango_staker_txs
abi87 Nov 16, 2023
6079eb0
introduced pre durango staker tx
abi87 Nov 16, 2023
9e5cc17
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Nov 16, 2023
c6736b6
nit
abi87 Nov 16, 2023
6f34808
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Nov 16, 2023
a0fb538
nit
abi87 Nov 16, 2023
944f309
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Nov 17, 2023
7b79d43
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Nov 17, 2023
0106d75
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Nov 18, 2023
0ed0d1f
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Nov 18, 2023
9a9897c
nit
abi87 Nov 18, 2023
409008f
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Nov 18, 2023
d17f2a2
nits
abi87 Nov 18, 2023
6ab4d6e
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
6fe5652
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
8717574
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
4f06497
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
ef2443c
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
3d27b66
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
105f5f1
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
02ca462
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
a2115a4
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Dec 3, 2023
fb85fe1
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Dec 4, 2023
f0e490e
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Dec 4, 2023
bfc29a1
Merge branch 'dev' into post_durango_staker_txs_0
abi87 Dec 5, 2023
9b97b59
fix
abi87 Dec 5, 2023
02449eb
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
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
f0c55ef
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
abi87 Dec 6, 2023
fa9269c
simplify comment
StephenButtolph Dec 6, 2023
e73b312
Merge branch 'post_durango_staker_txs_0' into post_durango_staker_txs
StephenButtolph Dec 6, 2023
5330986
merged
StephenButtolph Dec 6, 2023
d3a6bf5
Merge branch 'dev' into post_durango_staker_txs
abi87 Dec 7, 2023
db4c048
fixed bad merge
abi87 Dec 7, 2023
b100a3d
explicit type casts
abi87 Dec 7, 2023
b265b3c
Merge branch 'dev' into post_durango_staker_txs
StephenButtolph Dec 7, 2023
85c41e6
reduce diff
StephenButtolph Dec 7, 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
32 changes: 24 additions & 8 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,11 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx := addStaker0.Unsigned.(*txs.AddValidatorTx)
staker0, err := state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -736,9 +738,11 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

addSubnetValTx := tx.Unsigned.(*txs.AddSubnetValidatorTx)
staker, err := state.NewCurrentStaker(
tx.ID(),
tx.Unsigned.(*txs.AddSubnetValidatorTx),
addSubnetValTx,
addSubnetValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -794,9 +798,11 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx := addStaker0.Unsigned.(*txs.AddValidatorTx)
staker, err = state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -907,9 +913,11 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx := addStaker0.Unsigned.(*txs.AddValidatorTx)
staker, err = state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -993,9 +1001,11 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx := addStaker0.Unsigned.(*txs.AddValidatorTx)
staker, err := state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -1084,9 +1094,11 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx = addStaker0.Unsigned.(*txs.AddValidatorTx)
staker, err = state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -1174,9 +1186,11 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx := addStaker0.Unsigned.(*txs.AddValidatorTx)
staker, err := state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -1264,9 +1278,11 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
require.NoError(err)

// store Staker0 to state
addValTx = addStaker0.Unsigned.(*txs.AddValidatorTx)
staker, err = state.NewCurrentStaker(
addStaker0.ID(),
addStaker0.Unsigned.(*txs.AddValidatorTx),
addValTx,
addValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down
4 changes: 3 additions & 1 deletion vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,11 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

addSubnetValTx := tx.Unsigned.(*txs.AddSubnetValidatorTx)
staker, err := state.NewCurrentStaker(
tx.ID(),
tx.Unsigned.(*txs.AddSubnetValidatorTx),
addSubnetValTx,
addSubnetValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down
8 changes: 6 additions & 2 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,11 @@ func TestGetStake(t *testing.T) {
)
require.NoError(err)

addDelTx := tx.Unsigned.(*txs.AddDelegatorTx)
staker, err := state.NewCurrentStaker(
tx.ID(),
tx.Unsigned.(*txs.AddDelegatorTx),
addDelTx,
addDelTx.StartTime(),
0,
)
require.NoError(err)
Expand Down Expand Up @@ -643,9 +645,11 @@ func TestGetCurrentValidators(t *testing.T) {
)
require.NoError(err)

addDelTx := delTx.Unsigned.(*txs.AddDelegatorTx)
staker, err := state.NewCurrentStaker(
delTx.ID(),
delTx.Unsigned.(*txs.AddDelegatorTx),
addDelTx,
addDelTx.StartTime(),
0,
)
require.NoError(err)
Expand Down
13 changes: 10 additions & 3 deletions vms/platformvm/state/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,26 @@ 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.PostDurangoStaker,
startTime time.Time,
Copy link
Contributor Author

@abi87 abi87 Nov 14, 2023

Choose a reason for hiding this comment

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

This is the main point of this PR. Upon drop of pending stakers, we'll explicitly pass chainTime here to create the staker object. In this PR we anticipate the structural change by spreading use of txs.Staker instead of txs.ScheduledStaker wherever possible

potentialReward uint64,
) (*Staker, error) {
publicKey, _, err := staker.PublicKey()
if err != nil {
return nil, err
}
endTime := staker.EndTime()

stakingDuration := staker.Duration()
endTime := startTime.Add(stakingDuration)
return &Staker{
TxID: txID,
NodeID: staker.NodeID(),
PublicKey: publicKey,
SubnetID: staker.SubnetID(),
Weight: staker.Weight(),
StartTime: staker.StartTime(),
StartTime: startTime,
EndTime: endTime,
PotentialReward: potentialReward,
NextTime: endTime,
Expand Down
12 changes: 6 additions & 6 deletions vms/platformvm/state/staker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ func TestNewCurrentStaker(t *testing.T) {
subnetID := ids.GenerateTestID()
weight := uint64(12345)
startTime := time.Now()
endTime := time.Now()
duration := time.Hour
endTime := startTime.Add(duration)
potentialReward := uint64(54321)
currentPriority := txs.SubnetPermissionedValidatorCurrentPriority

stakerTx := txs.NewMockStaker(ctrl)
stakerTx := txs.NewMockPostDurangoStaker(ctrl)
stakerTx.EXPECT().Duration().Return(duration)
stakerTx.EXPECT().NodeID().Return(nodeID)
stakerTx.EXPECT().PublicKey().Return(publicKey, true, nil)
stakerTx.EXPECT().SubnetID().Return(subnetID)
stakerTx.EXPECT().Weight().Return(weight)
stakerTx.EXPECT().StartTime().Return(startTime)
stakerTx.EXPECT().EndTime().Return(endTime)
stakerTx.EXPECT().CurrentPriority().Return(currentPriority)

staker, err := NewCurrentStaker(txID, stakerTx, potentialReward)
staker, err := NewCurrentStaker(txID, stakerTx, startTime, potentialReward)
require.NotNil(staker)
require.NoError(err)
require.Equal(txID, staker.TxID)
Expand All @@ -173,7 +173,7 @@ func TestNewCurrentStaker(t *testing.T) {

stakerTx.EXPECT().PublicKey().Return(nil, false, errCustom)

_, err = NewCurrentStaker(txID, stakerTx, potentialReward)
_, err = NewCurrentStaker(txID, stakerTx, startTime, potentialReward)
require.ErrorIs(err, errCustom)
}

Expand Down
48 changes: 31 additions & 17 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()
stakeDuration := tx.Validator.Duration()
currentSupply, err := s.GetCurrentSupply(constants.PrimaryNetworkID)
if err != nil {
Expand All @@ -1341,7 +1341,7 @@ func (s *state) syncGenesis(genesisBlk block.Block, genesis *genesis.Genesis) er
return err
}

staker, err := NewCurrentStaker(vdrTx.ID(), tx, potentialReward)
staker, err := NewCurrentStaker(vdrTx.ID(), tx, tx.StartTime(), potentialReward)
if err != nil {
return err
}
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.Staker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}

metadataBytes := validatorIt.Value()
Expand All @@ -1456,16 +1460,16 @@ func (s *state) loadCurrentValidators() error {
// Note: we don't provide [LastUpdated] here because we expect it to
// always be present on disk.
}
if err := parseValidatorMetadata(metadataBytes, metadata); err != nil {
err = parseValidatorMetadata(metadataBytes, metadata)
if err != nil {
return err
}

stakerTx, ok := tx.Unsigned.(txs.Staker)
if !ok {
return fmt.Errorf("expected tx type txs.Staker but got %T", tx.Unsigned)
}
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 moved up a fewl line. This should never happen, but always better to fail fast.


staker, err := NewCurrentStaker(txID, stakerTx, metadata.PotentialReward)
staker, err := NewCurrentStaker(
txID,
stakerTx.(txs.PostDurangoStaker),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is safe because all of our StakerTxs are also PostDurangoStakerTxs and viceversa. We type assert it for each transaction.
Still ain't pretty

stakerTx.StartTime(),
metadata.PotentialReward)
if err != nil {
return err
}
Expand Down Expand Up @@ -1507,7 +1511,12 @@ func (s *state) loadCurrentValidators() error {
return err
}

staker, err := NewCurrentStaker(txID, stakerTx, metadata.PotentialReward)
staker, err := NewCurrentStaker(
txID,
stakerTx.(txs.PostDurangoStaker),
stakerTx.StartTime(),
metadata.PotentialReward,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -1537,6 +1546,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 @@ -1545,12 +1559,12 @@ 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)
}
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 moved up a bit. Should never happen but always better to fail fast


staker, err := NewCurrentStaker(txID, stakerTx, metadata.PotentialReward)
staker, err := NewCurrentStaker(
txID,
stakerTx.(txs.PostDurangoStaker),
stakerTx.StartTime(),
metadata.PotentialReward,
)
if err != nil {
return err
}
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)
_ PostDurangoStaker = (*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)
_ PostDurangoStaker = (*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)
_ PostDurangoStaker = (*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)
_ PostDurangoStaker = (*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)
_ PostDurangoStaker = (*AddValidatorTx)(nil)

errTooManyShares = fmt.Errorf("a staker can only require at most %d shares from delegators", reward.PercentDenominator)
)
Expand Down
4 changes: 3 additions & 1 deletion vms/platformvm/txs/executor/advance_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,11 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

addSubnetValTx := tx.Unsigned.(*txs.AddSubnetValidatorTx)
staker, err := state.NewCurrentStaker(
tx.ID(),
tx.Unsigned.(*txs.AddSubnetValidatorTx),
addSubnetValTx,
addSubnetValTx.StartTime(),
0,
)
require.NoError(err)
Expand Down
Loading
Loading