Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: http index announcements #1418

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 57 additions & 22 deletions indexprovider/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Wrapper struct {
// bitswapEnabled records whether to announce bitswap as an available
// protocol to the network indexer
bitswapEnabled bool
httpEnabled bool
stop context.CancelFunc
}

Expand All @@ -79,6 +80,8 @@ func NewWrapper(cfg *config.Boost) func(lc fx.Lifecycle, h host.Host, r repo.Loc

// bitswap is enabled if there is a bitswap peer id
bitswapEnabled := cfg.Dealmaking.BitswapPeerID != ""
// http is considered enabled if there is an http retrieval multiaddr set
httpEnabled := cfg.Dealmaking.HTTPRetrievalMultiaddr != ""

// setup bitswap extended provider if there is a public multi addr for bitswap
w := &Wrapper{
Expand All @@ -91,6 +94,7 @@ func NewWrapper(cfg *config.Boost) func(lc fx.Lifecycle, h host.Host, r repo.Loc
enabled: !isDisabled,
piecedirectory: piecedirectory,
bitswapEnabled: bitswapEnabled,
httpEnabled: httpEnabled,
ssm: ssm,
}
return w, nil
Expand All @@ -107,7 +111,7 @@ func (w *Wrapper) Start(_ context.Context) {
go func() {
err := w.AnnounceExtendedProviders(runCtx)
if err != nil {
log.Warnf("announcing extended providers: %w", err)
log.Warnf("announcing extended providers: %s", err)
}
}()

Expand Down Expand Up @@ -305,6 +309,41 @@ func (w *Wrapper) AnnounceExtendedProviders(ctx context.Context) error {
key := w.h.Peerstore().PrivKey(w.h.ID())
adBuilder := xproviders.NewAdBuilder(w.h.ID(), key, w.h.Addrs())

err := w.appendExtendedProviders(ctx, adBuilder, key)
if err != nil {
return err
}

last, _, err := w.prov.GetLatestAdv(ctx)
if err != nil {
return err
}
adBuilder.WithLastAdID(last)
ad, err := adBuilder.BuildAndSign()
if err != nil {
return err
}

// make sure we're connected to the mesh so that the message will go through
// pubsub and reach the indexer
err = w.meshCreator.Connect(ctx)
if err != nil {
log.Warnf("could not connect to pubsub mesh before announcing extended provider: %w", err)
}

// publish the extended providers announcement
adCid, err := w.prov.Publish(ctx, *ad)
if err != nil {
return err
}

log.Infof("announced endpoint to indexer with advertisement cid %s", adCid)

return nil
}

func (w *Wrapper) appendExtendedProviders(ctx context.Context, adBuilder *xproviders.AdBuilder, key crypto.PrivKey) error {

if !w.bitswapEnabled {
// If bitswap is completely disabled, publish an advertisement with empty extended providers
// which should override previously published extended providers associated to w.h.ID().
Expand Down Expand Up @@ -366,31 +405,27 @@ func (w *Wrapper) AnnounceExtendedProviders(ctx context.Context) error {
adBuilder.WithExtendedProviders(ep)
}

last, _, err := w.prov.GetLatestAdv(ctx)
if err != nil {
return err
}
adBuilder.WithLastAdID(last)
ad, err := adBuilder.BuildAndSign()
if err != nil {
return err
}
if !w.httpEnabled {
log.Info("Dealmaking.HTTPRetrievalMultiaddr is not set - announcing http disabled to Indexer")
} else {
// marshal http metadata
meta := metadata.Default.New(metadata.IpfsGatewayHttp{})
mbytes, err := meta.MarshalBinary()
if err != nil {
return err
}
var ep = xproviders.Info{
ID: w.h.ID().String(),
Addrs: []string{w.cfg.Dealmaking.HTTPRetrievalMultiaddr},
Metadata: mbytes,
Priv: key,
}

// make sure we're connected to the mesh so that the message will go through
// pubsub and reach the indexer
err = w.meshCreator.Connect(ctx)
if err != nil {
log.Warnf("could not connect to pubsub mesh before announcing extended provider: %w", err)
}
log.Infof("announcing http endpoint to indexer as extended provider: %s", ep.Addrs)

// publish the extended providers announcement
adCid, err := w.prov.Publish(ctx, *ad)
if err != nil {
return err
adBuilder.WithExtendedProviders(ep)
}

log.Infof("announced endpoint to indexer with advertisement cid %s", adCid)

return nil
}

Expand Down