github.com/MetalBlockchain/metalgo@v1.11.9/snow/validators/logger.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  	"go.uber.org/zap"
     8  
     9  	"github.com/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/utils/crypto/bls"
    11  	"github.com/MetalBlockchain/metalgo/utils/logging"
    12  	"github.com/MetalBlockchain/metalgo/utils/set"
    13  	"github.com/MetalBlockchain/metalgo/vms/types"
    14  )
    15  
    16  var _ SetCallbackListener = (*logger)(nil)
    17  
    18  type logger struct {
    19  	log      logging.Logger
    20  	subnetID ids.ID
    21  	nodeIDs  set.Set[ids.NodeID]
    22  }
    23  
    24  // NewLogger returns a callback listener that will log validator set changes for
    25  // the specified validators
    26  func NewLogger(
    27  	log logging.Logger,
    28  	subnetID ids.ID,
    29  	nodeIDs ...ids.NodeID,
    30  ) SetCallbackListener {
    31  	nodeIDSet := set.Of(nodeIDs...)
    32  	return &logger{
    33  		log:      log,
    34  		subnetID: subnetID,
    35  		nodeIDs:  nodeIDSet,
    36  	}
    37  }
    38  
    39  func (l *logger) OnValidatorAdded(
    40  	nodeID ids.NodeID,
    41  	pk *bls.PublicKey,
    42  	txID ids.ID,
    43  	weight uint64,
    44  ) {
    45  	if l.nodeIDs.Contains(nodeID) {
    46  		var pkBytes []byte
    47  		if pk != nil {
    48  			pkBytes = bls.PublicKeyToCompressedBytes(pk)
    49  		}
    50  		l.log.Info("node added to validator set",
    51  			zap.Stringer("subnetID", l.subnetID),
    52  			zap.Stringer("nodeID", nodeID),
    53  			zap.Reflect("publicKey", types.JSONByteSlice(pkBytes)),
    54  			zap.Stringer("txID", txID),
    55  			zap.Uint64("weight", weight),
    56  		)
    57  	}
    58  }
    59  
    60  func (l *logger) OnValidatorRemoved(
    61  	nodeID ids.NodeID,
    62  	weight uint64,
    63  ) {
    64  	if l.nodeIDs.Contains(nodeID) {
    65  		l.log.Info("node removed from validator set",
    66  			zap.Stringer("subnetID", l.subnetID),
    67  			zap.Stringer("nodeID", nodeID),
    68  			zap.Uint64("weight", weight),
    69  		)
    70  	}
    71  }
    72  
    73  func (l *logger) OnValidatorWeightChanged(
    74  	nodeID ids.NodeID,
    75  	oldWeight uint64,
    76  	newWeight uint64,
    77  ) {
    78  	if l.nodeIDs.Contains(nodeID) {
    79  		l.log.Info("validator weight changed",
    80  			zap.Stringer("subnetID", l.subnetID),
    81  			zap.Stringer("nodeID", nodeID),
    82  			zap.Uint64("previousWeight ", oldWeight),
    83  			zap.Uint64("newWeight ", newWeight),
    84  		)
    85  	}
    86  }