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