github.com/cgcardona/r-subnet-evm@v0.1.5/warp/validators/state.go (about)

     1  // (c) 2023, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package validators
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/ava-labs/avalanchego/ids"
    10  	"github.com/ava-labs/avalanchego/snow"
    11  	"github.com/ava-labs/avalanchego/snow/validators"
    12  	"github.com/ava-labs/avalanchego/utils/constants"
    13  )
    14  
    15  var _ validators.State = (*State)(nil)
    16  
    17  // State provides a special case used to handle Avalanche Warp Message verification for messages sent
    18  // from the Primary Network. Subnets have strictly fewer validators than the Primary Network, so we require
    19  // signatures from a threshold of the RECEIVING subnet validator set rather than the full Primary Network
    20  // since the receiving subnet already relies on a majority of its validators being correct.
    21  type State struct {
    22  	chainContext *snow.Context
    23  	validators.State
    24  }
    25  
    26  // NewState returns a wrapper of [validators.State] which special cases the handling of the Primary Network.
    27  //
    28  // The wrapped state will return the chainContext's Subnet validator set instead of the Primary Network when
    29  // the Primary Network SubnetID is passed in.
    30  func NewState(chainContext *snow.Context) *State {
    31  	return &State{
    32  		chainContext: chainContext,
    33  		State:        chainContext.ValidatorState,
    34  	}
    35  }
    36  
    37  func (s *State) GetValidatorSet(
    38  	ctx context.Context,
    39  	height uint64,
    40  	subnetID ids.ID,
    41  ) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
    42  	// If the subnetID is anything other than the Primary Network, this is a direct
    43  	// passthrough
    44  	if subnetID != constants.PrimaryNetworkID {
    45  		return s.State.GetValidatorSet(ctx, height, subnetID)
    46  	}
    47  
    48  	// If the requested subnet is the primary network, then we return the validator
    49  	// set for the Subnet that is receiving the message instead.
    50  	return s.State.GetValidatorSet(ctx, height, s.chainContext.SubnetID)
    51  }