-
Notifications
You must be signed in to change notification settings - Fork 886
/
service.go
143 lines (122 loc) · 3.99 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package header
import (
"context"
"errors"
"fmt"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/p2p"
"github.com/celestiaorg/go-header/sync"
"github.com/celestiaorg/celestia-node/header"
modfraud "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
)
// Service represents the header Service that can be started / stopped on a node.
// Service's main function is to manage its sub-services. Service can contain several
// sub-services, such as Exchange, ExchangeServer, Syncer, and so forth.
type Service struct {
ex libhead.Exchange[*header.ExtendedHeader]
syncer syncer
sub libhead.Subscriber[*header.ExtendedHeader]
p2pServer *p2p.ExchangeServer[*header.ExtendedHeader]
store libhead.Store[*header.ExtendedHeader]
}
// syncer bare minimum Syncer interface for testing
type syncer interface {
libhead.Head[*header.ExtendedHeader]
State() sync.State
SyncWait(ctx context.Context) error
}
// newHeaderService creates a new instance of header Service.
func newHeaderService(
// getting Syncer wrapped in ServiceBreaker so we ensure service breaker is constructed
syncer *modfraud.ServiceBreaker[*sync.Syncer[*header.ExtendedHeader], *header.ExtendedHeader],
sub libhead.Subscriber[*header.ExtendedHeader],
p2pServer *p2p.ExchangeServer[*header.ExtendedHeader],
ex libhead.Exchange[*header.ExtendedHeader],
store libhead.Store[*header.ExtendedHeader],
) Module {
return &Service{
syncer: syncer.Service,
sub: sub,
p2pServer: p2pServer,
ex: ex,
store: store,
}
}
func (s *Service) GetByHash(ctx context.Context, hash libhead.Hash) (*header.ExtendedHeader, error) {
return s.store.Get(ctx, hash)
}
func (s *Service) GetRangeByHeight(
ctx context.Context,
from *header.ExtendedHeader,
to uint64,
) ([]*header.ExtendedHeader, error) {
return s.store.GetRangeByHeight(ctx, from, to)
}
func (s *Service) GetByHeight(ctx context.Context, height uint64) (*header.ExtendedHeader, error) {
head, err := s.syncer.Head(ctx)
switch {
case err != nil:
return nil, err
case head.Height() == height:
return head, nil
case head.Height()+1 < height:
return nil, fmt.Errorf("header: given height is from the future: "+
"networkHeight: %d, requestedHeight: %d", head.Height(), height)
}
// TODO(vgonkivs): remove after https://github.com/celestiaorg/go-header/issues/32 is
// implemented and fetch header from HeaderEx if missing locally
head, err = s.store.Head(ctx)
switch {
case err != nil:
return nil, err
case head.Height() == height:
return head, nil
// `+1` allows for one header network lag, e.g. user request header that is milliseconds away
case head.Height()+1 < height:
return nil, fmt.Errorf("header: syncing in progress: "+
"localHeadHeight: %d, requestedHeight: %d", head.Height(), height)
default:
return s.store.GetByHeight(ctx, height)
}
}
func (s *Service) WaitForHeight(ctx context.Context, height uint64) (*header.ExtendedHeader, error) {
return s.store.GetByHeight(ctx, height)
}
func (s *Service) LocalHead(ctx context.Context) (*header.ExtendedHeader, error) {
return s.store.Head(ctx)
}
func (s *Service) SyncState(context.Context) (sync.State, error) {
return s.syncer.State(), nil
}
func (s *Service) SyncWait(ctx context.Context) error {
return s.syncer.SyncWait(ctx)
}
func (s *Service) NetworkHead(ctx context.Context) (*header.ExtendedHeader, error) {
return s.syncer.Head(ctx)
}
func (s *Service) Subscribe(ctx context.Context) (<-chan *header.ExtendedHeader, error) {
subscription, err := s.sub.Subscribe()
if err != nil {
return nil, err
}
headerCh := make(chan *header.ExtendedHeader)
go func() {
defer close(headerCh)
defer subscription.Cancel()
for {
h, err := subscription.NextHeader(ctx)
if err != nil {
if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) {
log.Errorw("fetching header from subscription", "err", err)
}
return
}
select {
case <-ctx.Done():
return
case headerCh <- h:
}
}
}()
return headerCh, nil
}