-
Notifications
You must be signed in to change notification settings - Fork 886
/
constructors.go
137 lines (121 loc) · 3.33 KB
/
constructors.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
package header
import (
"context"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
"go.uber.org/fx"
libfraud "github.com/celestiaorg/go-fraud"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/p2p"
"github.com/celestiaorg/go-header/store"
"github.com/celestiaorg/go-header/sync"
modfraud "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
modp2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/share/eds/byzantine"
)
// newP2PExchange constructs a new Exchange for headers.
func newP2PExchange[H libhead.Header[H]](
lc fx.Lifecycle,
cfg Config,
bpeers modp2p.Bootstrappers,
network modp2p.Network,
host host.Host,
conngater *conngater.BasicConnectionGater,
pidstore p2p.PeerIDStore,
) (libhead.Exchange[H], error) {
peers, err := cfg.trustedPeers(bpeers)
if err != nil {
return nil, err
}
ids := make([]peer.ID, len(peers))
for index, peer := range peers {
ids[index] = peer.ID
host.Peerstore().AddAddrs(peer.ID, peer.Addrs, peerstore.PermanentAddrTTL)
}
opts := []p2p.Option[p2p.ClientParameters]{
p2p.WithParams(cfg.Client),
p2p.WithNetworkID[p2p.ClientParameters](network.String()),
p2p.WithChainID(network.String()),
p2p.WithPeerIDStore[p2p.ClientParameters](pidstore),
}
if MetricsEnabled {
opts = append(opts, p2p.WithMetrics[p2p.ClientParameters]())
}
exchange, err := p2p.NewExchange[H](host, ids, conngater, opts...)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return exchange.Start(ctx)
},
OnStop: func(ctx context.Context) error {
return exchange.Stop(ctx)
},
})
return exchange, nil
}
// newSyncer constructs new Syncer for headers.
func newSyncer[H libhead.Header[H]](
ex libhead.Exchange[H],
store libhead.Store[H],
sub libhead.Subscriber[H],
cfg Config,
) (*sync.Syncer[H], error) {
opts := []sync.Option{sync.WithParams(cfg.Syncer), sync.WithBlockTime(modp2p.BlockTime)}
if MetricsEnabled {
opts = append(opts, sync.WithMetrics())
}
syncer, err := sync.NewSyncer[H](ex, store, sub, opts...)
if err != nil {
return nil, err
}
return syncer, nil
}
func newFraudedSyncer[H libhead.Header[H]](
fservice libfraud.Service[H],
syncer *sync.Syncer[H],
) *modfraud.ServiceBreaker[*sync.Syncer[H], H] {
return &modfraud.ServiceBreaker[*sync.Syncer[H], H]{
Service: syncer,
FraudType: byzantine.BadEncoding,
FraudServ: fservice,
}
}
// newInitStore constructs an initialized store
func newInitStore[H libhead.Header[H]](
lc fx.Lifecycle,
cfg Config,
net modp2p.Network,
ds datastore.Batching,
ex libhead.Exchange[H],
) (libhead.Store[H], error) {
opts := []store.Option{store.WithParams(cfg.Store)}
if MetricsEnabled {
opts = append(opts, store.WithMetrics())
}
s, err := store.NewStore[H](ds, opts...)
if err != nil {
return nil, err
}
trustedHash, err := cfg.trustedHash(net)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
err = store.Init[H](ctx, s, ex, trustedHash)
if err != nil {
return err
}
return s.Start(ctx)
},
OnStop: func(ctx context.Context) error {
return s.Stop(ctx)
},
})
return s, nil
}