github.com/MetalBlockchain/metalgo@v1.11.9/node/insecure_validator_manager.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package node
     5  
     6  import (
     7  	"go.uber.org/zap"
     8  
     9  	"github.com/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/snow/networking/router"
    11  	"github.com/MetalBlockchain/metalgo/snow/validators"
    12  	"github.com/MetalBlockchain/metalgo/utils/constants"
    13  	"github.com/MetalBlockchain/metalgo/utils/logging"
    14  	"github.com/MetalBlockchain/metalgo/version"
    15  )
    16  
    17  type insecureValidatorManager struct {
    18  	router.Router
    19  	log    logging.Logger
    20  	vdrs   validators.Manager
    21  	weight uint64
    22  }
    23  
    24  func (i *insecureValidatorManager) Connected(vdrID ids.NodeID, nodeVersion *version.Application, subnetID ids.ID) {
    25  	if constants.PrimaryNetworkID == subnetID {
    26  		// Sybil protection is disabled so we don't have a txID that added the
    27  		// peer as a validator. Because each validator needs a txID associated
    28  		// with it, we hack one together by padding the nodeID with zeroes.
    29  		dummyTxID := ids.Empty
    30  		copy(dummyTxID[:], vdrID.Bytes())
    31  
    32  		err := i.vdrs.AddStaker(constants.PrimaryNetworkID, vdrID, nil, dummyTxID, i.weight)
    33  		if err != nil {
    34  			i.log.Error("failed to add validator",
    35  				zap.Stringer("nodeID", vdrID),
    36  				zap.Stringer("subnetID", constants.PrimaryNetworkID),
    37  				zap.Error(err),
    38  			)
    39  		}
    40  	}
    41  	i.Router.Connected(vdrID, nodeVersion, subnetID)
    42  }
    43  
    44  func (i *insecureValidatorManager) Disconnected(vdrID ids.NodeID) {
    45  	// RemoveWeight will only error here if there was an error reported during
    46  	// Add.
    47  	err := i.vdrs.RemoveWeight(constants.PrimaryNetworkID, vdrID, i.weight)
    48  	if err != nil {
    49  		i.log.Error("failed to remove weight",
    50  			zap.Stringer("nodeID", vdrID),
    51  			zap.Stringer("subnetID", constants.PrimaryNetworkID),
    52  			zap.Error(err),
    53  		)
    54  	}
    55  	i.Router.Disconnected(vdrID)
    56  }