github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/alsp/model/record.go (about)

     1  package model
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  )
     6  
     7  // ProtocolSpamRecord is a record of a misbehaving node. It is used to keep track of the Penalty value of the node
     8  // and the number of times it has been slashed due to its Penalty value dropping below the disallow-listing threshold.
     9  type ProtocolSpamRecord struct {
    10  	// OriginId is the node id of the misbehaving node. It is assumed an authorized (i.e., staked) node at the
    11  	// time of the misbehavior report creation (otherwise, the networking layer should not have dispatched the
    12  	// message to the Flow protocol layer in the first place).
    13  	OriginId flow.Identifier
    14  
    15  	// Decay speed of Penalty for this misbehaving node. Each node may have a different Decay speed based on its behavior.
    16  	// Subsequent disallow listings of the node will decrease the Decay speed of the node so it will take longer to be allow-listed.
    17  	Decay float64
    18  
    19  	// CutoffCounter is a counter that is used to determine how many times the connections to the node has been cut due to
    20  	// its Penalty value dropping below the disallow-listing threshold.
    21  	// Note that the cutoff connections are recovered after a certain amount of time.
    22  	CutoffCounter uint64
    23  
    24  	// DisallowListed indicates whether the node is currently disallow-listed or not. When a node is in the disallow-list,
    25  	// the existing connections to the node are cut and no new connections are allowed to be established, neither incoming
    26  	// nor outgoing.
    27  	DisallowListed bool
    28  
    29  	// total Penalty value of the misbehaving node. Should be a negative value.
    30  	Penalty float64
    31  }
    32  
    33  // RecordAdjustFunc is a function that is used to adjust the fields of a ProtocolSpamRecord.
    34  // The function is called with the current record and should return the adjusted record.
    35  // Returned error indicates that the adjustment is not applied, and the record should not be updated.
    36  // In BFT setup, the returned error should be treated as a fatal error.
    37  type RecordAdjustFunc func(ProtocolSpamRecord) (ProtocolSpamRecord, error)
    38  
    39  // SpamRecordFactoryFunc is a function that creates a new protocol spam record with the given origin id and initial values.
    40  // Args:
    41  // - originId: the origin id of the spam record.
    42  // Returns:
    43  // - ProtocolSpamRecord, the created record.
    44  type SpamRecordFactoryFunc func(flow.Identifier) ProtocolSpamRecord
    45  
    46  // SpamRecordFactory returns the default factory function for creating a new protocol spam record.
    47  // Returns:
    48  // - SpamRecordFactoryFunc, the default factory function.
    49  // Note that the default factory function creates a new record with the initial values.
    50  func SpamRecordFactory() SpamRecordFactoryFunc {
    51  	return func(originId flow.Identifier) ProtocolSpamRecord {
    52  		return ProtocolSpamRecord{
    53  			OriginId:       originId,
    54  			Decay:          InitialDecaySpeed,
    55  			DisallowListed: false,
    56  			CutoffCounter:  uint64(0),
    57  			Penalty:        float64(0),
    58  		}
    59  	}
    60  }