github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/p2p/peers/peerdata/store.go (about)

     1  package peerdata
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"sync"
     7  	"time"
     8  
     9  	"github.com/ethereum/go-ethereum/p2p/enr"
    10  	"github.com/libp2p/go-libp2p-core/network"
    11  	"github.com/libp2p/go-libp2p-core/peer"
    12  	ma "github.com/multiformats/go-multiaddr"
    13  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
    14  	pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
    15  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    16  	"github.com/prysmaticlabs/prysm/shared/interfaces"
    17  )
    18  
    19  var (
    20  	// ErrPeerUnknown is returned when there is an attempt to obtain data from a peer that is not known.
    21  	ErrPeerUnknown = errors.New("peer unknown")
    22  )
    23  
    24  // PeerConnectionState is the state of the connection.
    25  type PeerConnectionState ethpb.ConnectionState
    26  
    27  // StoreConfig holds peer store parameters.
    28  type StoreConfig struct {
    29  	MaxPeers int
    30  }
    31  
    32  // Store is a container for various peer related data (both protocol and app level).
    33  // Container implements RWMutex, so data access can be restricted on the container level. This allows
    34  // different components rely on the very same peer map container.
    35  // Note: access to data is controlled by clients i.e. client code is responsible for locking/unlocking
    36  // the mutex when accessing data.
    37  type Store struct {
    38  	sync.RWMutex
    39  	ctx    context.Context
    40  	config *StoreConfig
    41  	peers  map[peer.ID]*PeerData
    42  }
    43  
    44  // PeerData aggregates protocol and application level info about a single peer.
    45  type PeerData struct {
    46  	// Network related data.
    47  	Address       ma.Multiaddr
    48  	Direction     network.Direction
    49  	ConnState     PeerConnectionState
    50  	Enr           *enr.Record
    51  	NextValidTime time.Time
    52  	// Chain related data.
    53  	MetaData                  interfaces.Metadata
    54  	ChainState                *pb.Status
    55  	ChainStateLastUpdated     time.Time
    56  	ChainStateValidationError error
    57  	// Scorers internal data.
    58  	BadResponses         int
    59  	ProcessedBlocks      uint64
    60  	BlockProviderUpdated time.Time
    61  	// Gossip Scoring data.
    62  	TopicScores      map[string]*pbrpc.TopicScoreSnapshot
    63  	GossipScore      float64
    64  	BehaviourPenalty float64
    65  }
    66  
    67  // NewStore creates new peer data store.
    68  func NewStore(ctx context.Context, config *StoreConfig) *Store {
    69  	return &Store{
    70  		ctx:    ctx,
    71  		config: config,
    72  		peers:  make(map[peer.ID]*PeerData),
    73  	}
    74  }
    75  
    76  // PeerData returns data associated with a given peer, if any.
    77  // Important: it is assumed that store mutex is locked when calling this method.
    78  func (s *Store) PeerData(pid peer.ID) (*PeerData, bool) {
    79  	peerData, ok := s.peers[pid]
    80  	return peerData, ok
    81  }
    82  
    83  // PeerDataGetOrCreate returns data associated with a given peer.
    84  // If no data has been associated yet, newly created and associated data object is returned.
    85  // Important: it is assumed that store mutex is locked when calling this method.
    86  func (s *Store) PeerDataGetOrCreate(pid peer.ID) *PeerData {
    87  	if peerData, ok := s.peers[pid]; ok {
    88  		return peerData
    89  	}
    90  	s.peers[pid] = &PeerData{}
    91  	return s.peers[pid]
    92  }
    93  
    94  // SetPeerData updates data associated with a given peer.
    95  // Important: it is assumed that store mutex is locked when calling this method.
    96  func (s *Store) SetPeerData(pid peer.ID, data *PeerData) {
    97  	s.peers[pid] = data
    98  }
    99  
   100  // DeletePeerData removes data associated with a given peer.
   101  // Important: it is assumed that store mutex is locked when calling this method.
   102  func (s *Store) DeletePeerData(pid peer.ID) {
   103  	delete(s.peers, pid)
   104  }
   105  
   106  // Peers returns map of peer data objects.
   107  // Important: it is assumed that store mutex is locked when calling this method.
   108  func (s *Store) Peers() map[peer.ID]*PeerData {
   109  	return s.peers
   110  }
   111  
   112  // Config exposes store configuration params.
   113  func (s *Store) Config() *StoreConfig {
   114  	return s.config
   115  }