Skip to content

Commit

Permalink
Implement --save option in ipfs swarm peering add/rm
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelbacaner authored and ajnavarro committed Nov 21, 2022
1 parent 039badf commit 80beead
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
swarmLatencyOptionName = "latency"
swarmDirectionOptionName = "direction"
swarmResetLimitsOptionName = "reset"
swarmSaveOptionName = "save"
swarmUsedResourcesPercentageName = "min-used-limit-perc"
)

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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{},
Expand Down Expand Up @@ -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...)
}
29 changes: 29 additions & 0 deletions test/sharness/t0140-swarm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
'
Expand Down

0 comments on commit 80beead

Please sign in to comment.