github.com/MetalBlockchain/metalgo@v1.11.9/snow/validators/state.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package validators
     5  
     6  import (
     7  	"context"
     8  	"sync"
     9  
    10  	"github.com/MetalBlockchain/metalgo/ids"
    11  )
    12  
    13  var _ State = (*lockedState)(nil)
    14  
    15  // State allows the lookup of validator sets on specified subnets at the
    16  // requested P-chain height.
    17  type State interface {
    18  	// GetMinimumHeight returns the minimum height of the block still in the
    19  	// proposal window.
    20  	GetMinimumHeight(context.Context) (uint64, error)
    21  	// GetCurrentHeight returns the current height of the P-chain.
    22  	GetCurrentHeight(context.Context) (uint64, error)
    23  
    24  	// GetSubnetID returns the subnetID of the provided chain.
    25  	GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error)
    26  
    27  	// GetValidatorSet returns the validators of the provided subnet at the
    28  	// requested P-chain height.
    29  	// The returned map should not be modified.
    30  	GetValidatorSet(
    31  		ctx context.Context,
    32  		height uint64,
    33  		subnetID ids.ID,
    34  	) (map[ids.NodeID]*GetValidatorOutput, error)
    35  }
    36  
    37  type lockedState struct {
    38  	lock sync.Locker
    39  	s    State
    40  }
    41  
    42  func NewLockedState(lock sync.Locker, s State) State {
    43  	return &lockedState{
    44  		lock: lock,
    45  		s:    s,
    46  	}
    47  }
    48  
    49  func (s *lockedState) GetMinimumHeight(ctx context.Context) (uint64, error) {
    50  	s.lock.Lock()
    51  	defer s.lock.Unlock()
    52  
    53  	return s.s.GetMinimumHeight(ctx)
    54  }
    55  
    56  func (s *lockedState) GetCurrentHeight(ctx context.Context) (uint64, error) {
    57  	s.lock.Lock()
    58  	defer s.lock.Unlock()
    59  
    60  	return s.s.GetCurrentHeight(ctx)
    61  }
    62  
    63  func (s *lockedState) GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error) {
    64  	s.lock.Lock()
    65  	defer s.lock.Unlock()
    66  
    67  	return s.s.GetSubnetID(ctx, chainID)
    68  }
    69  
    70  func (s *lockedState) GetValidatorSet(
    71  	ctx context.Context,
    72  	height uint64,
    73  	subnetID ids.ID,
    74  ) (map[ids.NodeID]*GetValidatorOutput, error) {
    75  	s.lock.Lock()
    76  	defer s.lock.Unlock()
    77  
    78  	return s.s.GetValidatorSet(ctx, height, subnetID)
    79  }
    80  
    81  type noValidators struct {
    82  	State
    83  }
    84  
    85  func NewNoValidatorsState(state State) State {
    86  	return &noValidators{
    87  		State: state,
    88  	}
    89  }
    90  
    91  func (*noValidators) GetValidatorSet(context.Context, uint64, ids.ID) (map[ids.NodeID]*GetValidatorOutput, error) {
    92  	return nil, nil
    93  }