github.com/MetalBlockchain/metalgo@v1.11.9/subnets/config.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package subnets
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/snow/consensus/snowball"
    13  	"github.com/MetalBlockchain/metalgo/utils/set"
    14  )
    15  
    16  var errAllowedNodesWhenNotValidatorOnly = errors.New("allowedNodes can only be set when ValidatorOnly is true")
    17  
    18  type Config struct {
    19  	// ValidatorOnly indicates that this Subnet's Chains are available to only subnet validators.
    20  	// No chain related messages will go out to non-validators.
    21  	// Validators will drop messages received from non-validators.
    22  	// Also see [AllowedNodes] to allow non-validators to connect to this Subnet.
    23  	ValidatorOnly bool `json:"validatorOnly" yaml:"validatorOnly"`
    24  	// AllowedNodes is the set of node IDs that are explicitly allowed to connect to this Subnet when
    25  	// ValidatorOnly is enabled.
    26  	AllowedNodes        set.Set[ids.NodeID] `json:"allowedNodes"        yaml:"allowedNodes"`
    27  	ConsensusParameters snowball.Parameters `json:"consensusParameters" yaml:"consensusParameters"`
    28  
    29  	// ProposerMinBlockDelay is the minimum delay this node will enforce when
    30  	// building a snowman++ block.
    31  	//
    32  	// TODO: Remove this flag once all VMs throttle their own block production.
    33  	ProposerMinBlockDelay time.Duration `json:"proposerMinBlockDelay" yaml:"proposerMinBlockDelay"`
    34  	// ProposerNumHistoricalBlocks is the number of historical snowman++ blocks
    35  	// this node will index per chain. If set to 0, the node will index all
    36  	// snowman++ blocks.
    37  	//
    38  	// Note: The last accepted block is not considered a historical block. This
    39  	// prevents the user from only storing the last accepted block, which can
    40  	// never be safe due to the non-atomic commits between the proposervm
    41  	// database and the innerVM's database.
    42  	//
    43  	// Invariant: This value must be set such that the proposervm never needs to
    44  	// rollback more blocks than have been deleted. On startup, the proposervm
    45  	// rolls back its accepted chain to match the innerVM's accepted chain. If
    46  	// the innerVM is not persisting its last accepted block quickly enough, the
    47  	// database can become corrupted.
    48  	//
    49  	// TODO: Move this flag once the proposervm is configurable on a per-chain
    50  	// basis.
    51  	ProposerNumHistoricalBlocks uint64 `json:"proposerNumHistoricalBlocks" yaml:"proposerNumHistoricalBlocks"`
    52  }
    53  
    54  func (c *Config) Valid() error {
    55  	if err := c.ConsensusParameters.Verify(); err != nil {
    56  		return fmt.Errorf("consensus %w", err)
    57  	}
    58  	if !c.ValidatorOnly && c.AllowedNodes.Len() > 0 {
    59  		return errAllowedNodesWhenNotValidatorOnly
    60  	}
    61  	return nil
    62  }