Skip to content

Commit

Permalink
let starbase handle rebroadcast IPNS (#89)
Browse files Browse the repository at this point in the history
* let starbase handle rebroadcast IPNS

* move edit fx options to interface.go

* fix golint

* Update interface.go
  • Loading branch information
jzhu-eth authored Dec 6, 2023
1 parent c00b885 commit f6ce140
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 71 deletions.
21 changes: 14 additions & 7 deletions cmd/node/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"time"

multierror "github.com/hashicorp/go-multierror"

options "github.com/ipfs/boxo/coreiface/options"
cmds "github.com/ipfs/go-ipfs-cmds"
mprome "github.com/ipfs/go-metrics-prometheus"
version "github.com/ipfs/kubo"
utilmain "github.com/ipfs/kubo/cmd/ipfs/util"
oldcmds "github.com/ipfs/kubo/commands"
Expand All @@ -30,14 +32,10 @@ import (
fsrepo "github.com/ipfs/kubo/repo/fsrepo"
"github.com/ipfs/kubo/repo/fsrepo/migrations"
"github.com/ipfs/kubo/repo/fsrepo/migrations/ipfsfetcher"
goprocess "github.com/jbenet/goprocess"
p2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
pnet "github.com/libp2p/go-libp2p/core/pnet"
sockets "github.com/libp2p/go-socket-activation"

options "github.com/ipfs/boxo/coreiface/options"
cmds "github.com/ipfs/go-ipfs-cmds"
mprome "github.com/ipfs/go-metrics-prometheus"
goprocess "github.com/jbenet/goprocess"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
prometheus "github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -397,6 +395,16 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
ipnsps = cfg.Ipns.UsePubsub.WithDefault(false)
}

if ipnsps {
// Libp2p's PubSub will be initialized if the "pubsub" flag is enabled or
// the "ipnsps" flag is enabled. Falcon replaces the IPNS PubsubRouter with
// our custom one and always disables the "ipnsps" flag to prevent the Kubo
// level from creating the IPNS PubsubRouter. Therefore, we need to enable
// the "pubsub" flag to ensure that the PubSub object will be initially
// initialized by fx.
pubsub = true
}

// Start assembling node config
ncfg := &core.BuildCfg{
Repo: repo,
Expand All @@ -405,7 +413,6 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
DisableEncryptedConnections: unencrypted,
ExtraOpts: map[string]bool{
"pubsub": pubsub,
"ipnsps": ipnsps,
},
//TODO(Kubuxu): refactor Online vs Offline by adding Permanent vs Ephemeral
}
Expand Down
61 changes: 61 additions & 0 deletions node/com/routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com

import (
"time"

"github.com/ipfs/kubo/core/node/helpers"
libp2p "github.com/ipfs/kubo/core/node/libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
namesys "github.com/libp2p/go-libp2p-pubsub-router"
record "github.com/libp2p/go-libp2p-record"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/host"
"go.uber.org/fx"
)

var (
neverDuration = 100 * 365 * 24 * time.Hour
)

type p2pPSRoutingIn struct {
fx.In

Validator record.Validator
Host host.Host
PubSub *pubsub.PubSub
}

type p2pRouterOut struct {
fx.Out
Router libp2p.Router `group:"routers"`
}

func PubsubRouter(
mctx helpers.MetricsCtx,
lc fx.Lifecycle,
in p2pPSRoutingIn,
) (p2pRouterOut, *namesys.PubsubValueStore, error) {
psRouter, err := namesys.NewPubsubValueStore(
helpers.LifecycleCtx(mctx, lc),
in.Host,
in.PubSub,
in.Validator,
namesys.WithRebroadcastInitialDelay(neverDuration),
)

if err != nil {
return p2pRouterOut{}, nil, err
}

return p2pRouterOut{
Router: libp2p.Router{
Routing: &routinghelpers.Compose{
ValueStore: &routinghelpers.LimitedValueStore{
ValueStore: psRouter,
Namespaces: []string{"ipns"},
},
},
Priority: 100,
},
}, psRouter, nil
}
141 changes: 77 additions & 64 deletions node/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,73 +30,74 @@ import (
//
// Upgrade procedure (as of Kubo v1.9.0):
//
// 1. Clean up files in cmd/falcon/, keeping config/ and e2e/ directories.
// cp -rf github.com/ipfs/kubo/cmd/ipfs/* cmd/falcon/
//
// 2. Add falcon.ConfigOption() to daemonCmd.Options to receive falcon config
// path from CLI flag.
//
// 3. Locate node creation code core.NewNode(...) in daemonFunc() from
// cmd/falcon/daemon.go and add the following code before it:
//
// //////////////////// Falcon ////////////////////
// if err := falcon.InitFalconBeforeNodeConstruction(req, repo); err != nil {
// fmt.Printf("Error initializing falcon before IPFS node construction: %v", err)
// return err
// }
// //////////////////// Falcon ////////////////////
//
// 4. Locate node creation code serveHTTPApi(...) in daemonFunc() from
// cmd/falcon/daemon.go and add the following code before it:
//
// //////////////////// Falcon ////////////////////
// falconErrc, err := falcon.InitFalconAfterNodeConstruction(req, cctx, node)
// if err != nil {
// fmt.Printf("Error initializing falcon after IPFS node construction: %v", err)
// return err
// }
// //////////////////// Falcon ////////////////////
//
// 5. Update error channel merge loop to include falconErrc in daemonFunc()
// from cmd/falcon/daemon.go:
//
// for err := range merge(apiErrc, gwErrc, gcErrc, falconErrc) {
// ...
// }
//
// 6. Disable default RPC API initialization in cmd/falcon/daemon.go by adding
// the following code to the beginning of serveHTTPApi() from
// from cmd/falcon/daemon.go:
//
// //////////////////// Falcon ////////////////////
// if true {
// return nil, nil
// }
// //////////////////// Falcon ////////////////////
//
// 7. Disable default gateway initialization in cmd/falcon/daemon.go by adding
// the following code to the beginning of serveHTTPGateway() from
// from cmd/falcon/daemon.go:
//
// //////////////////// Falcon ////////////////////
// if true {
// return nil, nil
// }
// //////////////////// Falcon ////////////////////
//
// 8. Disable debug handler in cmd/falcon/debug.go
//
// 9. Run `go mod tidy` to update go.mod with whats required by the new kubo
// version. There might be a conflict with the otel package when building
// falcon. Use `go get` to force the version used in kubo/go.mod.
// For example:
// go get go.opentelemetry.io/[email protected]
//
// 10. Make sure corenode.Core = fx.Options() assignments in
// 1. Clean up files in cmd/falcon/, keeping config/ and e2e/ directories.
// cp -rf github.com/ipfs/kubo/cmd/ipfs/* cmd/falcon/
//
// 2. Add falcon.ConfigOption() to daemonCmd.Options to receive falcon config
// path from CLI flag.
//
// 3. Locate node creation code core.NewNode(...) in daemonFunc() from
// cmd/falcon/daemon.go and add the following code before it:
//
// //////////////////// Falcon ////////////////////
// if err := falcon.InitFalconBeforeNodeConstruction(req, repo); err != nil {
// fmt.Printf("Error initializing falcon before IPFS node construction: %v", err)
// return err
// }
// //////////////////// Falcon ////////////////////
//
// 4. Locate node creation code serveHTTPApi(...) in daemonFunc() from
// cmd/falcon/daemon.go and add the following code before it:
//
// //////////////////// Falcon ////////////////////
// falconErrc, err := falcon.InitFalconAfterNodeConstruction(req, cctx, node)
// if err != nil {
// fmt.Printf("Error initializing falcon after IPFS node construction: %v", err)
// return err
// }
// //////////////////// Falcon ////////////////////
//
// 5. Update error channel merge loop to include falconErrc in daemonFunc()
// from cmd/falcon/daemon.go:
//
// for err := range merge(apiErrc, gwErrc, gcErrc, falconErrc) {
// ...
// }
//
// 6. Disable default RPC API initialization in cmd/falcon/daemon.go by adding
// the following code to the beginning of serveHTTPApi() from
// from cmd/falcon/daemon.go:
//
// //////////////////// Falcon ////////////////////
// if true {
// return nil, nil
// }
// //////////////////// Falcon ////////////////////
//
// 7. Disable default gateway initialization in cmd/falcon/daemon.go by adding
// the following code to the beginning of serveHTTPGateway() from
// from cmd/falcon/daemon.go:
//
// //////////////////// Falcon ////////////////////
// if true {
// return nil, nil
// }
// //////////////////// Falcon ////////////////////
//
// 8. Disable debug handler in cmd/falcon/debug.go
//
// 9. Run `go mod tidy` to update go.mod with whats required by the new kubo
// version. There might be a conflict with the otel package when building
// falcon. Use `go get` to force the version used in kubo/go.mod.
// For example:
// go get go.opentelemetry.io/[email protected]
//
// 10. Make sure corenode.Core = fx.Options() assignments in
// InitFalconBeforeNodeConstruction is consistent with Kubo.
//
// Example command to run falcon daemon:
// go run ./cmd/falcon/. daemon --falcon-config=./cmd/falcon/config/config_dev.yaml
//
// go run ./cmd/falcon/. daemon --falcon-config=./cmd/falcon/config/config_dev.yaml
const (
falconConfigFile = "falcon-config"
)
Expand Down Expand Up @@ -148,6 +149,18 @@ func InitFalconBeforeNodeConstruction(
fx.Provide(corenode.Files),
)

cfg, err := rpo.Config()
if err != nil {
return err
}

if cfg.Ipns.UsePubsub.WithDefault(false) {
// enable custom IPNS PubsubRouter
corenode.IPNS = fx.Options(
fx.Provide(corenode.RecordValidator),
fx.Provide(com.PubsubRouter),
)
}
return nil
}

Expand Down

0 comments on commit f6ce140

Please sign in to comment.