github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/service.go (about)

     1  // Package attestations defines an attestation pool
     2  // service implementation which is used to manage the lifecycle
     3  // of aggregated, unaggregated, and fork-choice attestations.
     4  package attestations
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	lru "github.com/hashicorp/golang-lru"
    11  	"github.com/prysmaticlabs/prysm/shared/params"
    12  )
    13  
    14  var forkChoiceProcessedRootsSize = 1 << 16
    15  
    16  // Service of attestation pool operations.
    17  type Service struct {
    18  	cfg                      *Config
    19  	ctx                      context.Context
    20  	cancel                   context.CancelFunc
    21  	err                      error
    22  	forkChoiceProcessedRoots *lru.Cache
    23  	genesisTime              uint64
    24  }
    25  
    26  // Config options for the service.
    27  type Config struct {
    28  	Pool          Pool
    29  	pruneInterval time.Duration
    30  }
    31  
    32  // NewService instantiates a new attestation pool service instance that will
    33  // be registered into a running beacon node.
    34  func NewService(ctx context.Context, cfg *Config) (*Service, error) {
    35  	cache, err := lru.New(forkChoiceProcessedRootsSize)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	if cfg.pruneInterval == 0 {
    41  		// Prune expired attestations from the pool every slot interval.
    42  		cfg.pruneInterval = time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second
    43  	}
    44  
    45  	ctx, cancel := context.WithCancel(ctx)
    46  	return &Service{
    47  		cfg:                      cfg,
    48  		ctx:                      ctx,
    49  		cancel:                   cancel,
    50  		forkChoiceProcessedRoots: cache,
    51  	}, nil
    52  }
    53  
    54  // Start an attestation pool service's main event loop.
    55  func (s *Service) Start() {
    56  	go s.prepareForkChoiceAtts()
    57  	go s.pruneAttsPool()
    58  }
    59  
    60  // Stop the beacon block attestation pool service's main event loop
    61  // and associated goroutines.
    62  func (s *Service) Stop() error {
    63  	defer s.cancel()
    64  	return nil
    65  }
    66  
    67  // Status returns the current service err if there's any.
    68  func (s *Service) Status() error {
    69  	if s.err != nil {
    70  		return s.err
    71  	}
    72  	return nil
    73  }
    74  
    75  // SetGenesisTime sets genesis time for operation service to use.
    76  func (s *Service) SetGenesisTime(t uint64) {
    77  	s.genesisTime = t
    78  }