github.com/koko1123/flow-go-1@v0.29.6/network/slashing/consumer.go (about)

     1  package slashing
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog"
     7  
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/module"
    10  	"github.com/koko1123/flow-go-1/utils/logging"
    11  )
    12  
    13  const (
    14  	unknown                      = "unknown"
    15  	unExpectedValidationError    = "unexpected_validation_error"
    16  	unAuthorizedSenderViolation  = "unauthorized_sender"
    17  	unknownMsgTypeViolation      = "unknown_message_type"
    18  	invalidMsgViolation          = "invalid_message"
    19  	senderEjectedViolation       = "sender_ejected"
    20  	unauthorizedUnicastOnChannel = "unauthorized_unicast_on_channel"
    21  )
    22  
    23  // Consumer is a struct that logs a message for any slashable offenses.
    24  // This struct will be updated in the future when slashing is implemented.
    25  type Consumer struct {
    26  	log     zerolog.Logger
    27  	metrics module.NetworkSecurityMetrics
    28  }
    29  
    30  // NewSlashingViolationsConsumer returns a new Consumer.
    31  func NewSlashingViolationsConsumer(log zerolog.Logger, metrics module.NetworkSecurityMetrics) *Consumer {
    32  	return &Consumer{
    33  		log:     log.With().Str("module", "network_slashing_consumer").Logger(),
    34  		metrics: metrics,
    35  	}
    36  }
    37  
    38  func (c *Consumer) logOffense(networkOffense string, violation *Violation) {
    39  	// if violation fails before the message is decoded the violation.MsgType will be unknown
    40  	if len(violation.MsgType) == 0 {
    41  		violation.MsgType = unknown
    42  	}
    43  
    44  	// if violation fails for an unknown peer violation.Identity will be nil
    45  	role := unknown
    46  	nodeID := flow.ZeroID
    47  	if violation.Identity != nil {
    48  		role = violation.Identity.Role.String()
    49  		nodeID = violation.Identity.NodeID
    50  	}
    51  
    52  	e := c.log.Error().
    53  		Str("peer_id", violation.PeerID).
    54  		Str("networking_offense", networkOffense).
    55  		Str("message_type", violation.MsgType).
    56  		Str("channel", violation.Channel.String()).
    57  		Str("protocol", violation.Protocol.String()).
    58  		Bool(logging.KeySuspicious, true).
    59  		Str("role", role).
    60  		Hex("sender_id", logging.ID(nodeID))
    61  
    62  	e.Msg(fmt.Sprintf("potential slashable offense: %s", violation.Err))
    63  
    64  	// capture unauthorized message count metric
    65  	c.metrics.OnUnauthorizedMessage(role, violation.MsgType, violation.Channel.String(), networkOffense)
    66  }
    67  
    68  // OnUnAuthorizedSenderError logs an error for unauthorized sender error.
    69  func (c *Consumer) OnUnAuthorizedSenderError(violation *Violation) {
    70  	c.logOffense(unAuthorizedSenderViolation, violation)
    71  }
    72  
    73  // OnUnknownMsgTypeError logs an error for unknown message type error.
    74  func (c *Consumer) OnUnknownMsgTypeError(violation *Violation) {
    75  	c.logOffense(unknownMsgTypeViolation, violation)
    76  }
    77  
    78  // OnInvalidMsgError logs an error for messages that contained payloads that could not
    79  // be unmarshalled into the message type denoted by message code byte.
    80  func (c *Consumer) OnInvalidMsgError(violation *Violation) {
    81  	c.logOffense(invalidMsgViolation, violation)
    82  }
    83  
    84  // OnSenderEjectedError logs an error for sender ejected error.
    85  func (c *Consumer) OnSenderEjectedError(violation *Violation) {
    86  	c.logOffense(senderEjectedViolation, violation)
    87  }
    88  
    89  // OnUnauthorizedUnicastOnChannel logs an error for messages unauthorized to be sent via unicast.
    90  func (c *Consumer) OnUnauthorizedUnicastOnChannel(violation *Violation) {
    91  	c.logOffense(unauthorizedUnicastOnChannel, violation)
    92  }
    93  
    94  // OnUnexpectedError logs an error for unexpected errors. This indicates message validation
    95  // has failed for an unknown reason and could potentially be n slashable offense.
    96  func (c *Consumer) OnUnexpectedError(violation *Violation) {
    97  	c.logOffense(unExpectedValidationError, violation)
    98  }