From 2c9f85fbc328a96b0c62bc3721f131210f5c8e80 Mon Sep 17 00:00:00 2001 From: samuelbacaner Date: Sun, 14 Aug 2022 10:08:28 -0500 Subject: [PATCH 1/5] Implement --save option in ipfs swarm peering add/rm --- core/commands/swarm.go | 64 ++++++++++++++++++++++++++++++++++++ test/sharness/t0140-swarm.sh | 29 ++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 1508efcb8a8..1dc1111e527 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -68,6 +68,7 @@ const ( swarmLatencyOptionName = "latency" swarmDirectionOptionName = "direction" swarmResetLimitsOptionName = "reset" + swarmSaveOptionName = "save" swarmUsedResourcesPercentageName = "min-used-limit-perc" ) @@ -103,6 +104,9 @@ var swarmPeeringAddCmd = &cmds.Command{ Arguments: []cmds.Argument{ cmds.StringArg("address", true, true, "address of peer to add into the peering subsystem"), }, + Options: []cmds.Option{ + cmds.BoolOption(swarmSaveOptionName, "if set, address(es) will be saved to peering config"), + }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { addrs := make([]ma.Multiaddr, len(req.Arguments)) @@ -135,6 +139,30 @@ var swarmPeeringAddCmd = &cmds.Command{ return err } } + + save, _ := req.Options[swarmSaveOptionName].(bool) + if save { + r, err := fsrepo.Open(env.(*commands.Context).ConfigRoot) + if err != nil { + return err + } + defer r.Close() + cfg, err := r.Config() + if err != nil { + return err + } + + addrInfos, err := mergeAddrInfo(addInfos, cfg.Peering.Peers) + if err != nil { + return fmt.Errorf("error merging peers: %w", err) + } + + cfg.Peering.Peers = addrInfos + if err := r.SetConfig(cfg); err != nil { + return fmt.Errorf("error writing new peers to repo config: %w", err) + } + } + return nil }, Encoders: cmds.EncoderMap{ @@ -193,6 +221,9 @@ var swarmPeeringRmCmd = &cmds.Command{ Arguments: []cmds.Argument{ cmds.StringArg("ID", true, true, "ID of peer to remove from the peering subsystem"), }, + Options: []cmds.Option{ + cmds.BoolOption(swarmSaveOptionName, "if set, address(es) will be removed from peering config"), + }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { node, err := cmdenv.GetNode(env) if err != nil { @@ -213,6 +244,25 @@ var swarmPeeringRmCmd = &cmds.Command{ return err } } + + save, _ := req.Options[swarmSaveOptionName].(bool) + if save { + r, err := fsrepo.Open(env.(*commands.Context).ConfigRoot) + if err != nil { + return err + } + defer r.Close() + cfg, err := r.Config() + if err != nil { + return err + } + + cfg.Peering.Peers = node.Peering.ListPeers() + if err := r.SetConfig(cfg); err != nil { + return fmt.Errorf("error removing peers from repo config: %w", err) + } + } + return nil }, Type: peeringResult{}, @@ -1090,3 +1140,17 @@ func filtersRemove(r repo.Repo, cfg *config.Config, toRemoveFilters []string) ([ return removed, nil } + +func mergeAddrInfo(addrInfos ...[]peer.AddrInfo) ([]peer.AddrInfo, error) { + var addrs []ma.Multiaddr + for _, infos := range addrInfos { + for _, addrInfo := range infos { + addr, err := peer.AddrInfoToP2pAddrs(&addrInfo) + if err != nil { + return nil, err + } + addrs = append(addrs, addr...) + } + } + return peer.AddrInfosFromP2pAddrs(addrs...) +} diff --git a/test/sharness/t0140-swarm.sh b/test/sharness/t0140-swarm.sh index d65831d3e22..2851bb9ac08 100755 --- a/test/sharness/t0140-swarm.sh +++ b/test/sharness/t0140-swarm.sh @@ -160,6 +160,35 @@ test_expect_success 'peering is removed' ' test_kill_ipfs_daemon +test_launch_ipfs_daemon + +peeringID='QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N' +peeringID2='QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5K' +peeringAddr='/ip4/1.2.3.4/tcp/1234/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N' +peeringAddr2='/ip4/1.2.3.4/tcp/1234/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5K' + +test_expect_success "'ipfs swarm peering add' with save option mutates config" ' + ipfs config Peering.Peers > start-peers && + ipfs swarm peering add ${peeringAddr} ${peeringAddr2} --save && + ipfs config Peering.Peers > end-peers && + ! test_cmp start-peers end-peers && + test_should_contain "${peeringID}" end-peers && + test_should_contain ${peeringID2} end-peers && + rm start-peers end-peers +' + +test_expect_success "'ipfs swarm peering rm' with save option mutates config" ' + ipfs config Peering.Peers > start-peers && + ipfs swarm peering rm ${peeringID} --save && + ipfs config Peering.Peers > end-peers && + ! test_cmp start-peers end-peers && + test_should_not_contain "${peeringID}" end-peers && + test_should_contain ${peeringID2} end-peers && + rm start-peers end-peers +' + +test_kill_ipfs_daemon + test_expect_success "set up tcp testbed" ' iptb testbed create -type localipfs -count 2 -force -init ' From e3369a489db686168f4176f301ee22051db226b2 Mon Sep 17 00:00:00 2001 From: samuelbacaner Date: Thu, 25 Aug 2022 08:35:34 -0500 Subject: [PATCH 2/5] Factor our config update; simplify updating config in add case --- core/commands/swarm.go | 60 ++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 1dc1111e527..31eae22291c 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -142,24 +142,12 @@ var swarmPeeringAddCmd = &cmds.Command{ save, _ := req.Options[swarmSaveOptionName].(bool) if save { - r, err := fsrepo.Open(env.(*commands.Context).ConfigRoot) - if err != nil { - return err - } - defer r.Close() - cfg, err := r.Config() - if err != nil { - return err + update := func(cfg *config.Config) { + cfg.Peering.Peers = node.Peering.ListPeers() } - - addrInfos, err := mergeAddrInfo(addInfos, cfg.Peering.Peers) + err := updateAndPersistConfig(env, update) if err != nil { - return fmt.Errorf("error merging peers: %w", err) - } - - cfg.Peering.Peers = addrInfos - if err := r.SetConfig(cfg); err != nil { - return fmt.Errorf("error writing new peers to repo config: %w", err) + return fmt.Errorf("unable to update and persist config change: %w", err) } } @@ -247,19 +235,12 @@ var swarmPeeringRmCmd = &cmds.Command{ save, _ := req.Options[swarmSaveOptionName].(bool) if save { - r, err := fsrepo.Open(env.(*commands.Context).ConfigRoot) - if err != nil { - return err + update := func(cfg *config.Config) { + cfg.Peering.Peers = node.Peering.ListPeers() } - defer r.Close() - cfg, err := r.Config() + err := updateAndPersistConfig(env, update) if err != nil { - return err - } - - cfg.Peering.Peers = node.Peering.ListPeers() - if err := r.SetConfig(cfg); err != nil { - return fmt.Errorf("error removing peers from repo config: %w", err) + return fmt.Errorf("unable to update and persist config change: %w", err) } } @@ -1141,16 +1122,19 @@ func filtersRemove(r repo.Repo, cfg *config.Config, toRemoveFilters []string) ([ return removed, nil } -func mergeAddrInfo(addrInfos ...[]peer.AddrInfo) ([]peer.AddrInfo, error) { - var addrs []ma.Multiaddr - for _, infos := range addrInfos { - for _, addrInfo := range infos { - addr, err := peer.AddrInfoToP2pAddrs(&addrInfo) - if err != nil { - return nil, err - } - addrs = append(addrs, addr...) - } +func updateAndPersistConfig(env cmds.Environment, update func(*config.Config)) error { + r, err := fsrepo.Open(env.(*commands.Context).ConfigRoot) + if err != nil { + return fmt.Errorf("error opening repo: %w", err) + } + defer r.Close() + cfg, err := r.Config() + if err != nil { + return fmt.Errorf("error fetching config file: %w", err) + } + update(cfg) + if err := r.SetConfig(cfg); err != nil { + return fmt.Errorf("error removing peers from repo config: %w", err) } - return peer.AddrInfosFromP2pAddrs(addrs...) + return nil } From cc967e9bd804fbb75005f45d139f0b46e590aa68 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Mon, 21 Nov 2022 15:46:33 +0100 Subject: [PATCH 3/5] Fix error message Signed-off-by: Antonio Navarro Perez --- core/commands/swarm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 31eae22291c..073b4737110 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -1134,7 +1134,7 @@ func updateAndPersistConfig(env cmds.Environment, update func(*config.Config)) e } update(cfg) if err := r.SetConfig(cfg); err != nil { - return fmt.Errorf("error removing peers from repo config: %w", err) + return fmt.Errorf("error setting config: %w", err) } return nil } From 35efa59ee656069a08f2aef466c922d3847410c0 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Tue, 6 Dec 2022 15:51:46 +0100 Subject: [PATCH 4/5] Requested changes. --- core/commands/swarm.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index 073b4737110..e8af25a7db0 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -143,7 +143,7 @@ var swarmPeeringAddCmd = &cmds.Command{ save, _ := req.Options[swarmSaveOptionName].(bool) if save { update := func(cfg *config.Config) { - cfg.Peering.Peers = node.Peering.ListPeers() + cfg.Peering.Peers = append(cfg.Peering.Peers, addInfos...) } err := updateAndPersistConfig(env, update) if err != nil { @@ -231,16 +231,25 @@ var swarmPeeringRmCmd = &cmds.Command{ if err = res.Emit(peeringResult{id, "success"}); err != nil { return err } - } - save, _ := req.Options[swarmSaveOptionName].(bool) - if save { - update := func(cfg *config.Config) { - cfg.Peering.Peers = node.Peering.ListPeers() - } - err := updateAndPersistConfig(env, update) - if err != nil { - return fmt.Errorf("unable to update and persist config change: %w", err) + save, _ := req.Options[swarmSaveOptionName].(bool) + if save { + update := func(cfg *config.Config) { + var cfgOut []peer.AddrInfo + for _, p := range cfg.Peering.Peers { + if p.ID == id { + continue + } + cfgOut = append(cfgOut, p) + } + + cfg.Peering.Peers = cfgOut + } + + err := updateAndPersistConfig(env, update) + if err != nil { + return fmt.Errorf("unable to update and persist config change: %w", err) + } } } From 689f476a3b86a439c75135cc31da8d8700044851 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Tue, 6 Dec 2022 15:56:40 +0100 Subject: [PATCH 5/5] Improve delete from config code --- core/commands/swarm.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index e8af25a7db0..acbf7a8dbb2 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -221,35 +221,37 @@ var swarmPeeringRmCmd = &cmds.Command{ return ErrNotOnline } + pids := make(map[peer.ID]struct{}, len(req.Arguments)) for _, arg := range req.Arguments { id, err := peer.Decode(arg) if err != nil { return err } - + pids[id] = struct{}{} node.Peering.RemovePeer(id) if err = res.Emit(peeringResult{id, "success"}); err != nil { return err } + } - save, _ := req.Options[swarmSaveOptionName].(bool) - if save { - update := func(cfg *config.Config) { - var cfgOut []peer.AddrInfo - for _, p := range cfg.Peering.Peers { - if p.ID == id { - continue - } - cfgOut = append(cfgOut, p) + save, _ := req.Options[swarmSaveOptionName].(bool) + if save { + update := func(cfg *config.Config) { + var cfgOut []peer.AddrInfo + for _, p := range cfg.Peering.Peers { + if _, ok := pids[p.ID]; ok { + continue } - cfg.Peering.Peers = cfgOut + cfgOut = append(cfgOut, p) } - err := updateAndPersistConfig(env, update) - if err != nil { - return fmt.Errorf("unable to update and persist config change: %w", err) - } + cfg.Peering.Peers = cfgOut + } + + err := updateAndPersistConfig(env, update) + if err != nil { + return fmt.Errorf("unable to update and persist config change: %w", err) } }