github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/txs/validator.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package txs
     5  
     6  import (
     7  	"errors"
     8  	"time"
     9  
    10  	"github.com/MetalBlockchain/metalgo/ids"
    11  )
    12  
    13  var (
    14  	ErrWeightTooSmall = errors.New("weight of this validator is too low")
    15  	errBadSubnetID    = errors.New("subnet ID can't be primary network ID")
    16  )
    17  
    18  // Validator is a validator.
    19  type Validator struct {
    20  	// Node ID of the validator
    21  	NodeID ids.NodeID `serialize:"true" json:"nodeID"`
    22  
    23  	// Unix time this validator starts validating
    24  	Start uint64 `serialize:"true" json:"start"`
    25  
    26  	// Unix time this validator stops validating
    27  	End uint64 `serialize:"true" json:"end"`
    28  
    29  	// Weight of this validator used when sampling
    30  	Wght uint64 `serialize:"true" json:"weight"`
    31  }
    32  
    33  // StartTime is the time that this validator will enter the validator set
    34  func (v *Validator) StartTime() time.Time {
    35  	return time.Unix(int64(v.Start), 0)
    36  }
    37  
    38  // EndTime is the time that this validator will leave the validator set
    39  func (v *Validator) EndTime() time.Time {
    40  	return time.Unix(int64(v.End), 0)
    41  }
    42  
    43  // Weight is this validator's weight when sampling
    44  func (v *Validator) Weight() uint64 {
    45  	return v.Wght
    46  }
    47  
    48  // Verify validates the ID for this validator
    49  func (v *Validator) Verify() error {
    50  	switch {
    51  	case v.Wght == 0: // Ensure the validator has some weight
    52  		return ErrWeightTooSmall
    53  	default:
    54  		return nil
    55  	}
    56  }
    57  
    58  // BoundedBy returns true iff staker start and end are a
    59  // (non-strict) subset of the provided time bound
    60  func BoundedBy(stakerStart, stakerEnd, lowerBound, upperBound time.Time) bool {
    61  	return !stakerStart.Before(lowerBound) && !stakerEnd.After(upperBound) && !stakerEnd.Before(stakerStart)
    62  }