Skip to content

Commit

Permalink
Merge branch 'master' into update-versions-v1.11.11
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Sep 3, 2024
2 parents 383201e + 17ece5d commit ba57180
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 91 deletions.
8 changes: 4 additions & 4 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ func TestBuildBlockAdvanceTime(t *testing.T) {
)

// Add a staker to [env.state]
env.state.PutCurrentValidator(&state.Staker{
require.NoError(env.state.PutCurrentValidator(&state.Staker{
NextTime: nextTime,
Priority: txs.PrimaryNetworkValidatorCurrentPriority,
})
}))

// Advance wall clock to [nextTime]
env.backend.Clk.Set(nextTime)
Expand Down Expand Up @@ -278,10 +278,10 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) {
)

// Add a staker to [env.state]
env.state.PutCurrentValidator(&state.Staker{
require.NoError(env.state.PutCurrentValidator(&state.Staker{
NextTime: nextTime,
Priority: txs.PrimaryNetworkValidatorCurrentPriority,
})
}))

// Advance wall clock to [nextTime] + [txexecutor.SyncBound]
env.backend.Clk.Set(nextTime.Add(txexecutor.SyncBound))
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func addPendingValidator(
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(addValidatorTx, status.Committed)
env.state.SetHeight(1)
require.NoError(env.state.Commit())
Expand Down
24 changes: 12 additions & 12 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())
}
Expand Down Expand Up @@ -572,7 +572,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(subnetStaker)
require.NoError(env.state.PutPendingValidator(subnetStaker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())
}
Expand Down Expand Up @@ -611,7 +611,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker0)
require.NoError(env.state.PutCurrentValidator(staker0))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -714,7 +714,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())

Expand All @@ -741,7 +741,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -782,7 +782,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -867,7 +867,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -904,7 +904,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -996,7 +996,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -1085,7 +1085,7 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -1181,7 +1181,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -1270,7 +1270,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(addStaker0, status.Committed)
require.NoError(env.state.Commit())

Expand Down
8 changes: 4 additions & 4 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(tx, status.Committed)
}
env.state.SetHeight( /*dummyHeight*/ 1)
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

env.state.PutCurrentValidator(staker)
require.NoError(env.state.PutCurrentValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())

Expand All @@ -658,7 +658,7 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())

Expand Down Expand Up @@ -733,7 +733,7 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) {
)
require.NoError(err)

env.state.PutPendingValidator(staker)
require.NoError(env.state.PutPendingValidator(staker))
env.state.AddTx(tx, status.Committed)
require.NoError(env.state.Commit())

Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func TestGetStake(t *testing.T) {
)
require.NoError(err)

service.vm.state.PutPendingValidator(staker)
require.NoError(service.vm.state.PutPendingValidator(staker))
service.vm.state.AddTx(tx, status.Committed)
require.NoError(service.vm.state.Commit())

Expand Down
16 changes: 10 additions & 6 deletions vms/platformvm/state/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (d *diff) GetDelegateeReward(subnetID ids.ID, nodeID ids.NodeID) (uint64, e
return parentState.GetDelegateeReward(subnetID, nodeID)
}

func (d *diff) PutCurrentValidator(staker *Staker) {
d.currentStakerDiffs.PutValidator(staker)
func (d *diff) PutCurrentValidator(staker *Staker) error {
return d.currentStakerDiffs.PutValidator(staker)
}

func (d *diff) DeleteCurrentValidator(staker *Staker) {
Expand Down Expand Up @@ -243,8 +243,8 @@ func (d *diff) GetPendingValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker,
}
}

func (d *diff) PutPendingValidator(staker *Staker) {
d.pendingStakerDiffs.PutValidator(staker)
func (d *diff) PutPendingValidator(staker *Staker) error {
return d.pendingStakerDiffs.PutValidator(staker)
}

func (d *diff) DeletePendingValidator(staker *Staker) {
Expand Down Expand Up @@ -444,7 +444,9 @@ func (d *diff) Apply(baseState Chain) error {
for _, validatorDiff := range subnetValidatorDiffs {
switch validatorDiff.validatorStatus {
case added:
baseState.PutCurrentValidator(validatorDiff.validator)
if err := baseState.PutCurrentValidator(validatorDiff.validator); err != nil {
return err
}
case deleted:
baseState.DeleteCurrentValidator(validatorDiff.validator)
}
Expand All @@ -471,7 +473,9 @@ func (d *diff) Apply(baseState Chain) error {
for _, validatorDiff := range subnetValidatorDiffs {
switch validatorDiff.validatorStatus {
case added:
baseState.PutPendingValidator(validatorDiff.validator)
if err := baseState.PutPendingValidator(validatorDiff.validator); err != nil {
return err
}
case deleted:
baseState.DeletePendingValidator(validatorDiff.validator)
}
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/state/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestDiffCurrentValidator(t *testing.T) {
SubnetID: ids.GenerateTestID(),
NodeID: ids.GenerateTestNodeID(),
}
d.PutCurrentValidator(currentValidator)
require.NoError(d.PutCurrentValidator(currentValidator))

// Assert that we get the current validator back
gotCurrentValidator, err := d.GetCurrentValidator(currentValidator.SubnetID, currentValidator.NodeID)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestDiffPendingValidator(t *testing.T) {
SubnetID: ids.GenerateTestID(),
NodeID: ids.GenerateTestNodeID(),
}
d.PutPendingValidator(pendingValidator)
require.NoError(d.PutPendingValidator(pendingValidator))

// Assert that we get the pending validator back
gotPendingValidator, err := d.GetPendingValidator(pendingValidator.SubnetID, pendingValidator.NodeID)
Expand Down
12 changes: 8 additions & 4 deletions vms/platformvm/state/mock_chain.go

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

12 changes: 8 additions & 4 deletions vms/platformvm/state/mock_diff.go

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

12 changes: 8 additions & 4 deletions vms/platformvm/state/mock_state.go

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

17 changes: 14 additions & 3 deletions vms/platformvm/state/stakers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
package state

import (
"errors"

"github.com/google/btree"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/iterator"
)

var ErrAddingStakerAfterDeletion = errors.New("attempted to add a staker after deleting it")

type Stakers interface {
CurrentStakers
PendingStakers
Expand All @@ -26,7 +30,7 @@ type CurrentStakers interface {
// staker set.
//
// Invariant: [staker] is not currently a CurrentValidator
PutCurrentValidator(staker *Staker)
PutCurrentValidator(staker *Staker) error

// DeleteCurrentValidator removes the [staker] describing a validator from
// the staker set.
Expand Down Expand Up @@ -72,7 +76,7 @@ type PendingStakers interface {

// PutPendingValidator adds the [staker] describing a validator to the
// staker set.
PutPendingValidator(staker *Staker)
PutPendingValidator(staker *Staker) error

// DeletePendingValidator removes the [staker] describing a validator from
// the staker set.
Expand Down Expand Up @@ -289,15 +293,22 @@ func (s *diffStakers) GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker,
return nil, validatorDiff.validatorStatus
}

func (s *diffStakers) PutValidator(staker *Staker) {
func (s *diffStakers) PutValidator(staker *Staker) error {
validatorDiff := s.getOrCreateDiff(staker.SubnetID, staker.NodeID)
if validatorDiff.validatorStatus == deleted {
// Enforce the invariant that a validator cannot be added after being
// deleted.
return ErrAddingStakerAfterDeletion
}

validatorDiff.validatorStatus = added
validatorDiff.validator = staker

if s.addedStakers == nil {
s.addedStakers = btree.NewG(defaultTreeDegree, (*Staker).Less)
}
s.addedStakers.ReplaceOrInsert(staker)
return nil
}

func (s *diffStakers) DeleteValidator(staker *Staker) {
Expand Down
Loading

0 comments on commit ba57180

Please sign in to comment.