Skip to content

Commit

Permalink
Separate IPNI publisher host from blox host
Browse files Browse the repository at this point in the history
To avoid GraphSync protocol interfering with the one instantiated by fx
exchange, use a separate libp2p host to publish to IPNI.
  • Loading branch information
masih committed Jul 11, 2023
1 parent 91aebc9 commit dd13411
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
42 changes: 39 additions & 3 deletions cmd/blox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
IpniPublishDisabled bool `yaml:"ipniPublishDisabled"`
IpniPublishInterval time.Duration `yaml:"ipniPublishInterval"`
IpniPublishDirectAnnounce []string `yaml:"IpniPublishDirectAnnounce"`
IpniPublisherIdentity string `yaml:"ipniPublisherIdentity"`

listenAddrs cli.StringSlice
staticRelays cli.StringSlice
Expand Down Expand Up @@ -142,6 +143,11 @@ func init() {
Destination: &app.config.directAnnounce,
Value: cli.NewStringSlice("https://cid.contact/ingest/announce"),
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "ipniPublisherIdentity",
DefaultText: "Randomly generated lib2p identity.",
Destination: &app.config.IpniPublisherIdentity,
}),
&cli.BoolFlag{
Name: "initOnly",
Usage: "Weather to only initialise config and quit.",
Expand Down Expand Up @@ -210,6 +216,17 @@ func before(ctx *cli.Context) error {
}
app.config.Authorizer = pid.String()
}
if app.config.IpniPublisherIdentity == "" {
k, _, err := crypto.GenerateEd25519Key(nil)
if err != nil {
return err
}
km, err := crypto.MarshalPrivateKey(k)
if err != nil {
return err
}
app.config.IpniPublisherIdentity = base64.StdEncoding.EncodeToString(km)
}
// Initialize store directory if not set.
if app.config.StoreDir == "" {
app.config.StoreDir = path.Join(homeDir, ".fula", "blox", "store")
Expand Down Expand Up @@ -247,18 +264,31 @@ func action(ctx *cli.Context) error {
return err
}

ipnikm, err := base64.StdEncoding.DecodeString(app.config.IpniPublisherIdentity)
if err != nil {
return err
}
ipnik, err := crypto.UnmarshalPrivateKey(ipnikm)
if err != nil {
return err
}

if app.initOnly {
fmt.Printf("Application config initialised at: %s\n", app.configPath)
pid, err := peer.IDFromPrivateKey(k)
if err != nil {
return err
}
ipnipid, err := peer.IDFromPrivateKey(ipnik)
if err != nil {
return err
}
fmt.Printf(" blox peer ID: %s\n", pid.String())
fmt.Printf(" blox IPNI publisher peer ID: %s\n", ipnipid.String())
return nil
}

hopts := []libp2p.Option{
libp2p.Identity(k),
libp2p.ListenAddrStrings(app.config.ListenAddrs...),
libp2p.EnableNATService(),
libp2p.NATPortMap(),
Expand Down Expand Up @@ -286,7 +316,13 @@ func action(ctx *cli.Context) error {
hopts = append(hopts, libp2p.EnableAutoRelayWithStaticRelays(sr, autorelay.WithNumRelays(1)))
}

h, err := libp2p.New(hopts...)
bopts := append(hopts, libp2p.Identity(k))
h, err := libp2p.New(bopts...)
if err != nil {
return err
}
iopts := append(hopts, libp2p.Identity(ipnik))
ipnih, err := libp2p.New(iopts...)
if err != nil {
return err
}
Expand All @@ -304,7 +340,7 @@ func action(ctx *cli.Context) error {
exchange.WithIpniPublishDisabled(app.config.IpniPublishDisabled),
exchange.WithIpniPublishInterval(app.config.IpniPublishInterval),
exchange.WithIpniProviderEngineOptions(
engine.WithHost(h),
engine.WithHost(ipnih),
engine.WithDatastore(namespace.Wrap(ds, datastore.NewKey("ipni/ads"))),
engine.WithPublisherKind(engine.DataTransferPublisher),
engine.WithDirectAnnounce(app.config.IpniPublishDirectAnnounce...),
Expand Down
8 changes: 7 additions & 1 deletion exchange/ipni_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ func (p *ipniPublisher) Start(ctx context.Context) error {
}

func (p *ipniPublisher) notifyReceivedLink(l ipld.Link) {
if link, ok := l.(cidlink.Link); ok && !cid.Undef.Equals(link.Cid) {
if l == nil {
return
}
link, ok := l.(cidlink.Link)
if ok &&
!cid.Undef.Equals(link.Cid) &&
link.Cid.Prefix().MhType != multihash.IDENTITY {
p.buffer <- link.Cid
}
}
Expand Down

0 comments on commit dd13411

Please sign in to comment.