github.com/ethereum-optimism/optimism@v1.7.2/op-node/p2p/store/iface.go (about)

     1  package store
     2  
     3  import (
     4  	"errors"
     5  	"math"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/libp2p/go-libp2p/core/peer"
    10  	"github.com/libp2p/go-libp2p/core/peerstore"
    11  )
    12  
    13  type TopicScores struct {
    14  	TimeInMesh               float64 `json:"timeInMesh"` // in seconds
    15  	FirstMessageDeliveries   float64 `json:"firstMessageDeliveries"`
    16  	MeshMessageDeliveries    float64 `json:"meshMessageDeliveries"`
    17  	InvalidMessageDeliveries float64 `json:"invalidMessageDeliveries"`
    18  }
    19  
    20  type GossipScores struct {
    21  	Total              float64     `json:"total"`
    22  	Blocks             TopicScores `json:"blocks"` // fully zeroed if the peer has not been in the mesh on the topic
    23  	IPColocationFactor float64     `json:"IPColocationFactor"`
    24  	BehavioralPenalty  float64     `json:"behavioralPenalty"`
    25  }
    26  
    27  func (g GossipScores) Apply(rec *scoreRecord) {
    28  	rec.PeerScores.Gossip = g
    29  }
    30  
    31  type ReqRespScores struct {
    32  	ValidResponses   float64 `json:"validResponses"`
    33  	ErrorResponses   float64 `json:"errorResponses"`
    34  	RejectedPayloads float64 `json:"rejectedPayloads"`
    35  }
    36  
    37  type IncrementValidResponses struct {
    38  	Cap float64
    39  }
    40  
    41  func (i IncrementValidResponses) Apply(rec *scoreRecord) {
    42  	rec.PeerScores.ReqResp.ValidResponses = math.Min(rec.PeerScores.ReqResp.ValidResponses+1, i.Cap)
    43  }
    44  
    45  type IncrementErrorResponses struct {
    46  	Cap float64
    47  }
    48  
    49  func (i IncrementErrorResponses) Apply(rec *scoreRecord) {
    50  	rec.PeerScores.ReqResp.ErrorResponses = math.Min(rec.PeerScores.ReqResp.ErrorResponses+1, i.Cap)
    51  }
    52  
    53  type IncrementRejectedPayloads struct {
    54  	Cap float64
    55  }
    56  
    57  func (i IncrementRejectedPayloads) Apply(rec *scoreRecord) {
    58  	rec.PeerScores.ReqResp.RejectedPayloads = math.Min(rec.PeerScores.ReqResp.RejectedPayloads+1, i.Cap)
    59  }
    60  
    61  type DecayApplicationScores struct {
    62  	ValidResponseDecay   float64
    63  	ErrorResponseDecay   float64
    64  	RejectedPayloadDecay float64
    65  	DecayToZero          float64
    66  }
    67  
    68  func (d *DecayApplicationScores) Apply(rec *scoreRecord) {
    69  	decay := func(value float64, decay float64) float64 {
    70  		value *= decay
    71  		if value < d.DecayToZero {
    72  			return 0
    73  		}
    74  		return value
    75  	}
    76  	rec.PeerScores.ReqResp.ValidResponses = decay(rec.PeerScores.ReqResp.ValidResponses, d.ValidResponseDecay)
    77  	rec.PeerScores.ReqResp.ErrorResponses = decay(rec.PeerScores.ReqResp.ErrorResponses, d.ErrorResponseDecay)
    78  	rec.PeerScores.ReqResp.RejectedPayloads = decay(rec.PeerScores.ReqResp.RejectedPayloads, d.RejectedPayloadDecay)
    79  }
    80  
    81  type PeerScores struct {
    82  	Gossip  GossipScores  `json:"gossip"`
    83  	ReqResp ReqRespScores `json:"reqResp"`
    84  }
    85  
    86  // ScoreDatastore defines a type-safe API for getting and setting libp2p peer score information
    87  type ScoreDatastore interface {
    88  	// GetPeerScores returns the current scores for the specified peer
    89  	GetPeerScores(id peer.ID) (PeerScores, error)
    90  
    91  	// GetPeerScore returns the current combined score for the specified peer
    92  	GetPeerScore(id peer.ID) (float64, error)
    93  
    94  	// SetScore applies the given store diff to the specified peer
    95  	SetScore(id peer.ID, diff ScoreDiff) (PeerScores, error)
    96  }
    97  
    98  // ScoreDiff defines a type-safe batch of changes to apply to the peer-scoring record of the peer.
    99  // The scoreRecord the diff is applied to is private: diffs can only be defined in this package,
   100  // to ensure changes to the record are non-breaking.
   101  type ScoreDiff interface {
   102  	Apply(score *scoreRecord)
   103  }
   104  
   105  var UnknownBanErr = errors.New("unknown ban")
   106  
   107  type PeerBanStore interface {
   108  	// SetPeerBanExpiration create the peer ban with expiration time.
   109  	// If expiry == time.Time{} then the ban is deleted.
   110  	SetPeerBanExpiration(id peer.ID, expiry time.Time) error
   111  	// GetPeerBanExpiration gets the peer ban expiration time, or UnknownBanErr error if none exists.
   112  	GetPeerBanExpiration(id peer.ID) (time.Time, error)
   113  }
   114  
   115  type IPBanStore interface {
   116  	// SetIPBanExpiration create the IP ban with expiration time.
   117  	// If expiry == time.Time{} then the ban is deleted.
   118  	SetIPBanExpiration(ip net.IP, expiry time.Time) error
   119  	// GetIPBanExpiration gets the IP ban expiration time, or UnknownBanErr error if none exists.
   120  	GetIPBanExpiration(ip net.IP) (time.Time, error)
   121  }
   122  
   123  type MetadataStore interface {
   124  	// SetPeerMetadata sets the metadata for the specified peer
   125  	SetPeerMetadata(id peer.ID, md PeerMetadata) (PeerMetadata, error)
   126  	// GetPeerMetadata returns the metadata for the specified peer
   127  	GetPeerMetadata(id peer.ID) (PeerMetadata, error)
   128  }
   129  
   130  // ExtendedPeerstore defines a type-safe API to work with additional peer metadata based on a libp2p peerstore.Peerstore
   131  type ExtendedPeerstore interface {
   132  	peerstore.Peerstore
   133  	ScoreDatastore
   134  	peerstore.CertifiedAddrBook
   135  	PeerBanStore
   136  	IPBanStore
   137  	MetadataStore
   138  }