/
constructors.go
94 lines (82 loc) · 2.47 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
package share
import (
"context"
"errors"
"github.com/filecoin-project/dagstore"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/routing"
routingdisc "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"go.uber.org/fx"
"github.com/furyaxyz/elysium-app/pkg/da"
"github.com/furyaxyz/elysium-node/share"
"github.com/furyaxyz/elysium-node/share/availability/cache"
disc "github.com/furyaxyz/elysium-node/share/availability/discovery"
"github.com/furyaxyz/elysium-node/share/availability/light"
"github.com/furyaxyz/elysium-node/share/eds"
"github.com/furyaxyz/elysium-node/share/getters"
)
func newDiscovery(cfg Config) func(routing.ContentRouting, host.Host) *disc.Discovery {
return func(
r routing.ContentRouting,
h host.Host,
) *disc.Discovery {
return disc.NewDiscovery(
h,
routingdisc.NewRoutingDiscovery(r),
disc.WithPeersLimit(cfg.Discovery.PeersLimit),
disc.WithAdvertiseInterval(cfg.Discovery.AdvertiseInterval),
)
}
}
// cacheAvailability wraps light availability with a cache for result sampling.
func cacheAvailability(lc fx.Lifecycle, ds datastore.Batching, avail *light.ShareAvailability) share.Availability {
ca := cache.NewShareAvailability(avail, ds)
lc.Append(fx.Hook{
OnStop: ca.Close,
})
return ca
}
func newModule(getter share.Getter, avail share.Availability) Module {
return &module{getter, avail}
}
// ensureEmptyCARExists adds an empty EDS to the provided EDS store.
func ensureEmptyCARExists(ctx context.Context, store *eds.Store) error {
emptyEDS := share.EmptyExtendedDataSquare()
emptyDAH := da.NewDataAvailabilityHeader(emptyEDS)
err := store.Put(ctx, emptyDAH.Hash(), emptyEDS)
if errors.Is(err, dagstore.ErrShardExists) {
return nil
}
return err
}
func lightGetter(
shrexGetter *getters.ShrexGetter,
ipldGetter *getters.IPLDGetter,
cfg Config,
) share.Getter {
var cascade []share.Getter
if cfg.UseShareExchange {
cascade = append(cascade, shrexGetter)
}
cascade = append(cascade, ipldGetter)
return getters.NewCascadeGetter(cascade)
}
func fullGetter(
store *eds.Store,
storeGetter *getters.StoreGetter,
shrexGetter *getters.ShrexGetter,
ipldGetter *getters.IPLDGetter,
cfg Config,
) share.Getter {
var cascade []share.Getter
cascade = append(cascade, storeGetter)
if cfg.UseShareExchange {
cascade = append(cascade, shrexGetter)
}
cascade = append(cascade, ipldGetter)
return getters.NewTeeGetter(
getters.NewCascadeGetter(cascade),
store,
)
}