|
1 | 1 | package libp2p
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + datastore "github.com/ipfs/go-datastore" |
4 | 9 | pubsub "github.com/libp2p/go-libp2p-pubsub"
|
5 | 10 | "github.com/libp2p/go-libp2p/core/discovery"
|
6 | 11 | "github.com/libp2p/go-libp2p/core/host"
|
| 12 | + "github.com/libp2p/go-libp2p/core/peer" |
7 | 13 | "go.uber.org/fx"
|
8 | 14 |
|
9 | 15 | "github.com/ipfs/kubo/core/node/helpers"
|
| 16 | + "github.com/ipfs/kubo/repo" |
10 | 17 | )
|
11 | 18 |
|
| 19 | +type P2PPubSubIn struct { |
| 20 | + fx.In |
| 21 | + |
| 22 | + Repo repo.Repo |
| 23 | + Host host.Host |
| 24 | + Discovery discovery.Discovery |
| 25 | +} |
| 26 | + |
12 | 27 | func FloodSub(pubsubOptions ...pubsub.Option) interface{} {
|
13 |
| - return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, disc discovery.Discovery) (service *pubsub.PubSub, err error) { |
14 |
| - return pubsub.NewFloodSub(helpers.LifecycleCtx(mctx, lc), host, append(pubsubOptions, pubsub.WithDiscovery(disc))...) |
| 28 | + return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PPubSubIn) (service *pubsub.PubSub, err error) { |
| 29 | + return pubsub.NewFloodSub( |
| 30 | + helpers.LifecycleCtx(mctx, lc), |
| 31 | + params.Host, |
| 32 | + append(pubsubOptions, |
| 33 | + pubsub.WithDiscovery(params.Discovery), |
| 34 | + pubsub.WithDefaultValidator(pubsub.NewBasicSeqnoValidator(makePubSubMetadataStore(params.Repo.Datastore()))))..., |
| 35 | + ) |
15 | 36 | }
|
16 | 37 | }
|
17 | 38 |
|
18 | 39 | func GossipSub(pubsubOptions ...pubsub.Option) interface{} {
|
19 |
| - return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, disc discovery.Discovery) (service *pubsub.PubSub, err error) { |
20 |
| - return pubsub.NewGossipSub(helpers.LifecycleCtx(mctx, lc), host, append( |
21 |
| - pubsubOptions, |
22 |
| - pubsub.WithDiscovery(disc), |
23 |
| - pubsub.WithFloodPublish(true))..., |
| 40 | + return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PPubSubIn) (service *pubsub.PubSub, err error) { |
| 41 | + return pubsub.NewGossipSub( |
| 42 | + helpers.LifecycleCtx(mctx, lc), |
| 43 | + params.Host, |
| 44 | + append( |
| 45 | + pubsubOptions, |
| 46 | + pubsub.WithDiscovery(params.Discovery), |
| 47 | + pubsub.WithFloodPublish(true), |
| 48 | + pubsub.WithDefaultValidator(pubsub.NewBasicSeqnoValidator(makePubSubMetadataStore(params.Repo.Datastore()))))..., |
24 | 49 | )
|
25 | 50 | }
|
26 | 51 | }
|
| 52 | + |
| 53 | +func makePubSubMetadataStore(ds datastore.Datastore) pubsub.PeerMetadataStore { |
| 54 | + return &pubsubMetadataStore{ds: ds} |
| 55 | +} |
| 56 | + |
| 57 | +type pubsubMetadataStore struct { |
| 58 | + ds datastore.Datastore |
| 59 | +} |
| 60 | + |
| 61 | +func (m *pubsubMetadataStore) Get(ctx context.Context, p peer.ID) ([]byte, error) { |
| 62 | + k := datastore.NewKey(fmt.Sprintf("/pubsub/seqno/%s", p)) |
| 63 | + |
| 64 | + v, err := m.ds.Get(ctx, k) |
| 65 | + if err != nil && errors.Is(err, datastore.ErrNotFound) { |
| 66 | + return nil, nil |
| 67 | + } |
| 68 | + |
| 69 | + return v, err |
| 70 | +} |
| 71 | + |
| 72 | +func (m *pubsubMetadataStore) Put(ctx context.Context, p peer.ID, v []byte) error { |
| 73 | + k := datastore.NewKey(fmt.Sprintf("/pubsub/seqno/%s", p)) |
| 74 | + return m.ds.Put(ctx, k, v) |
| 75 | +} |
0 commit comments