Skip to content

Commit

Permalink
[vms/platformvm] Remove GetPendingValidators API (#2817)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu committed Mar 6, 2024
1 parent 90a13f3 commit 639b9ca
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 140 deletions.
19 changes: 1 addition & 18 deletions vms/platformvm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ type Client interface {
GetStakingAssetID(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (ids.ID, error)
// GetCurrentValidators returns the list of current validators for subnet with ID [subnetID]
GetCurrentValidators(ctx context.Context, subnetID ids.ID, nodeIDs []ids.NodeID, options ...rpc.Option) ([]ClientPermissionlessValidator, error)
// GetPendingValidators returns the list of pending validators for subnet with ID [subnetID]
GetPendingValidators(ctx context.Context, subnetID ids.ID, nodeIDs []ids.NodeID, options ...rpc.Option) ([]interface{}, []interface{}, error)
// GetCurrentSupply returns an upper bound on the supply of AVAX in the system along with the P-chain height
GetCurrentSupply(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, uint64, error)
// SampleValidators returns the nodeIDs of a sample of [sampleSize] validators from the current validator set for subnet with ID [subnetID]
Expand Down Expand Up @@ -103,8 +101,7 @@ type Client interface {
// GetStake returns the amount of nAVAX that [addrs] have cumulatively
// staked on the Primary Network.
//
// Deprecated: Stake should be calculated using GetTx, GetCurrentValidators,
// and GetPendingValidators.
// Deprecated: Stake should be calculated using GetTx and GetCurrentValidators.
GetStake(
ctx context.Context,
addrs []ids.ShortID,
Expand Down Expand Up @@ -325,20 +322,6 @@ func (c *client) GetCurrentValidators(
return getClientPermissionlessValidators(res.Validators)
}

func (c *client) GetPendingValidators(
ctx context.Context,
subnetID ids.ID,
nodeIDs []ids.NodeID,
options ...rpc.Option,
) ([]interface{}, []interface{}, error) {
res := &GetPendingValidatorsReply{}
err := c.requester.SendRequest(ctx, "platform.getPendingValidators", &GetPendingValidatorsArgs{
SubnetID: subnetID,
NodeIDs: nodeIDs,
}, res, options...)
return res.Validators, res.Delegators, err
}

func (c *client) GetCurrentSupply(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, uint64, error) {
res := &GetCurrentSupplyReply{}
err := c.requester.SendRequest(ctx, "platform.getCurrentSupply", &GetCurrentSupplyArgs{
Expand Down
122 changes: 0 additions & 122 deletions vms/platformvm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,128 +932,6 @@ func (s *Service) GetCurrentValidators(_ *http.Request, args *GetCurrentValidato
return nil
}

// GetPendingValidatorsArgs are the arguments for calling GetPendingValidators
type GetPendingValidatorsArgs struct {
// Subnet we're getting the pending validators of
// If omitted, defaults to primary network
SubnetID ids.ID `json:"subnetID"`
// NodeIDs of validators to request. If [NodeIDs]
// is empty, it fetches all pending validators. If
// some requested nodeIDs are not pending validators,
// they are omitted from the response.
NodeIDs []ids.NodeID `json:"nodeIDs"`
}

// GetPendingValidatorsReply are the results from calling GetPendingValidators.
type GetPendingValidatorsReply struct {
Validators []interface{} `json:"validators"`
Delegators []interface{} `json:"delegators"`
}

// GetPendingValidators returns the lists of pending validators and delegators.
func (s *Service) GetPendingValidators(_ *http.Request, args *GetPendingValidatorsArgs, reply *GetPendingValidatorsReply) error {
s.vm.ctx.Log.Debug("API called",
zap.String("service", "platform"),
zap.String("method", "getPendingValidators"),
)

reply.Validators = []interface{}{}
reply.Delegators = []interface{}{}

// Create set of nodeIDs
nodeIDs := set.Of(args.NodeIDs...)

s.vm.ctx.Lock.Lock()
defer s.vm.ctx.Lock.Unlock()

numNodeIDs := nodeIDs.Len()
targetStakers := make([]*state.Staker, 0, numNodeIDs)
if numNodeIDs == 0 { // Include all nodes
pendingStakerIterator, err := s.vm.state.GetPendingStakerIterator()
if err != nil {
return err
}
for pendingStakerIterator.Next() { // Iterates in order of increasing stop time
staker := pendingStakerIterator.Value()
if args.SubnetID != staker.SubnetID {
continue
}
targetStakers = append(targetStakers, staker)
}
pendingStakerIterator.Release()
} else {
for nodeID := range nodeIDs {
staker, err := s.vm.state.GetPendingValidator(args.SubnetID, nodeID)
switch err {
case nil:
case database.ErrNotFound:
// nothing to do, continue
continue
default:
return err
}
targetStakers = append(targetStakers, staker)

delegatorsIt, err := s.vm.state.GetPendingDelegatorIterator(args.SubnetID, nodeID)
if err != nil {
return err
}
for delegatorsIt.Next() {
staker := delegatorsIt.Value()
targetStakers = append(targetStakers, staker)
}
delegatorsIt.Release()
}
}

for _, pendingStaker := range targetStakers {
nodeID := pendingStaker.NodeID
weight := avajson.Uint64(pendingStaker.Weight)
apiStaker := platformapi.Staker{
TxID: pendingStaker.TxID,
NodeID: nodeID,
StartTime: avajson.Uint64(pendingStaker.StartTime.Unix()),
EndTime: avajson.Uint64(pendingStaker.EndTime.Unix()),
Weight: weight,
StakeAmount: &weight,
}

switch pendingStaker.Priority {
case txs.PrimaryNetworkValidatorPendingPriority, txs.SubnetPermissionlessValidatorPendingPriority:
attr, err := s.loadStakerTxAttributes(pendingStaker.TxID)
if err != nil {
return err
}

shares := attr.shares
delegationFee := avajson.Float32(100 * float32(shares) / float32(reward.PercentDenominator))

connected := s.vm.uptimeManager.IsConnected(nodeID, args.SubnetID)
vdr := platformapi.PermissionlessValidator{
Staker: apiStaker,
DelegationFee: delegationFee,
Connected: connected,
Signer: attr.proofOfPossession,
}
reply.Validators = append(reply.Validators, vdr)

case txs.PrimaryNetworkDelegatorApricotPendingPriority, txs.PrimaryNetworkDelegatorBanffPendingPriority, txs.SubnetPermissionlessDelegatorPendingPriority:
reply.Delegators = append(reply.Delegators, apiStaker)

case txs.SubnetPermissionedValidatorPendingPriority:
connected := s.vm.uptimeManager.IsConnected(nodeID, args.SubnetID)
reply.Validators = append(reply.Validators, platformapi.PermissionedValidator{
Staker: apiStaker,
Connected: connected,
})

default:
return fmt.Errorf("unexpected staker priority %d", pendingStaker.Priority)
}
}
return nil
}

// GetCurrentSupplyArgs are the arguments for calling GetCurrentSupply
type GetCurrentSupplyArgs struct {
SubnetID ids.ID `json:"subnetID"`
Expand Down

0 comments on commit 639b9ca

Please sign in to comment.