From a17830754ccb22ef1ae8a63f30643c41eb35f01d Mon Sep 17 00:00:00 2001 From: fengzie <526567244@qq.com> Date: Sun, 29 Sep 2024 11:58:24 +0800 Subject: [PATCH] Fix issue in ResourceManager and nopfsPlugin about repo path (#10492) --- core/node/groups.go | 2 +- core/node/libp2p/rcmgr.go | 7 +------ plugin/plugins/nopfs/nopfs.go | 39 ++++++++++++++++++----------------- repo/mock.go | 4 ++++ repo/repo.go | 3 +++ 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/core/node/groups.go b/core/node/groups.go index a12cc7b7f1d..d806e2ef6e0 100644 --- a/core/node/groups.go +++ b/core/node/groups.go @@ -132,7 +132,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config, userResourceOverrides rcmgr.Part fx.Provide(libp2p.UserAgent()), // Services (resource management) - fx.Provide(libp2p.ResourceManager(cfg.Swarm, userResourceOverrides)), + fx.Provide(libp2p.ResourceManager(bcfg.Repo.Path(), cfg.Swarm, userResourceOverrides)), fx.Provide(libp2p.AddrFilters(cfg.Swarm.AddrFilters)), fx.Provide(libp2p.AddrsFactory(cfg.Addresses.Announce, cfg.Addresses.AppendAnnounce, cfg.Addresses.NoAnnounce)), fx.Provide(libp2p.SmuxTransport(cfg.Swarm.Transports)), diff --git a/core/node/libp2p/rcmgr.go b/core/node/libp2p/rcmgr.go index 8b6d55a2cfc..80bfec34ae6 100644 --- a/core/node/libp2p/rcmgr.go +++ b/core/node/libp2p/rcmgr.go @@ -28,7 +28,7 @@ const NetLimitTraceFilename = "rcmgr.json.gz" var ErrNoResourceMgr = fmt.Errorf("missing ResourceMgr: make sure the daemon is running with Swarm.ResourceMgr.Enabled") -func ResourceManager(cfg config.SwarmConfig, userResourceOverrides rcmgr.PartialLimitConfig) interface{} { +func ResourceManager(repoPath string, cfg config.SwarmConfig, userResourceOverrides rcmgr.PartialLimitConfig) interface{} { return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo) (network.ResourceManager, Libp2pOpts, error) { var manager network.ResourceManager var opts Libp2pOpts @@ -46,11 +46,6 @@ func ResourceManager(cfg config.SwarmConfig, userResourceOverrides rcmgr.Partial if enabled { log.Debug("libp2p resource manager is enabled") - repoPath, err := config.PathRoot() - if err != nil { - return nil, opts, fmt.Errorf("opening IPFS_PATH: %w", err) - } - limitConfig, msg, err := LimitConfig(cfg, userResourceOverrides) if err != nil { return nil, opts, fmt.Errorf("creating final Resource Manager config: %w", err) diff --git a/plugin/plugins/nopfs/nopfs.go b/plugin/plugins/nopfs/nopfs.go index 64350830f94..c32d7533f55 100644 --- a/plugin/plugins/nopfs/nopfs.go +++ b/plugin/plugins/nopfs/nopfs.go @@ -6,7 +6,6 @@ import ( "github.com/ipfs-shipyard/nopfs" "github.com/ipfs-shipyard/nopfs/ipfs" - "github.com/ipfs/kubo/config" "github.com/ipfs/kubo/core" "github.com/ipfs/kubo/core/node" "github.com/ipfs/kubo/plugin" @@ -20,7 +19,10 @@ var Plugins = []plugin.Plugin{ // fxtestPlugin is used for testing the fx plugin. // It merely adds an fx option that logs a debug statement, so we can verify that it works in tests. -type nopfsPlugin struct{} +type nopfsPlugin struct { + // Path to the IPFS repo. + repo string +} var _ plugin.PluginFx = (*nopfsPlugin)(nil) @@ -33,29 +35,28 @@ func (p *nopfsPlugin) Version() string { } func (p *nopfsPlugin) Init(env *plugin.Environment) error { + p.repo = env.Repo + return nil } // MakeBlocker is a factory for the blocker so that it can be provided with Fx. -func MakeBlocker() (*nopfs.Blocker, error) { - ipfsPath, err := config.PathRoot() - if err != nil { - return nil, err - } +func MakeBlocker(repoPath string) func() (*nopfs.Blocker, error) { + return func() (*nopfs.Blocker, error) { + defaultFiles, err := nopfs.GetDenylistFiles() + if err != nil { + return nil, err + } - defaultFiles, err := nopfs.GetDenylistFiles() - if err != nil { - return nil, err - } + kuboFiles, err := nopfs.GetDenylistFilesInDir(filepath.Join(repoPath, "denylists")) + if err != nil { + return nil, err + } - kuboFiles, err := nopfs.GetDenylistFilesInDir(filepath.Join(ipfsPath, "denylists")) - if err != nil { - return nil, err - } - - files := append(defaultFiles, kuboFiles...) + files := append(defaultFiles, kuboFiles...) - return nopfs.NewBlocker(files) + return nopfs.NewBlocker(files) + } } // PathResolvers returns wrapped PathResolvers for Kubo. @@ -76,7 +77,7 @@ func (p *nopfsPlugin) Options(info core.FXNodeInfo) ([]fx.Option, error) { opts := append( info.FXOptions, - fx.Provide(MakeBlocker), + fx.Provide(MakeBlocker(p.repo)), fx.Decorate(ipfs.WrapBlockService), fx.Decorate(ipfs.WrapNameSystem), fx.Decorate(PathResolvers), diff --git a/repo/mock.go b/repo/mock.go index 46bb0cb4210..2ac60615d26 100644 --- a/repo/mock.go +++ b/repo/mock.go @@ -27,6 +27,10 @@ func (m *Mock) Config() (*config.Config, error) { return &m.C, nil // FIXME threadsafety } +func (m *Mock) Path() string { + return "" +} + func (m *Mock) UserResourceOverrides() (rcmgr.PartialLimitConfig, error) { return rcmgr.PartialLimitConfig{}, nil } diff --git a/repo/repo.go b/repo/repo.go index 9abdf867e47..f345e89b417 100644 --- a/repo/repo.go +++ b/repo/repo.go @@ -23,6 +23,9 @@ type Repo interface { // to the returned config are not automatically persisted. Config() (*config.Config, error) + // Path is the repo file-system path + Path() string + // UserResourceOverrides returns optional user resource overrides for the // libp2p resource manager. UserResourceOverrides() (rcmgr.PartialLimitConfig, error)