Skip to content

Commit

Permalink
fix: deduplicate Swarm.Announce and AppendAnnounce
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Nov 30, 2021
1 parent 81aeec2 commit 9673571
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 11 additions & 4 deletions core/node/libp2p/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ func AddrFilters(filters []string) func() (*ma.Filters, Libp2pOpts, error) {
}

func makeAddrsFactory(announce []string, appendAnnouce []string, noAnnounce []string) (p2pbhost.AddrsFactory, error) {
var err error // To assign to the slice in the for loop
var err error // To assign to the slice in the for loop
existing := make(map[string]bool) // To avoid duplicates

annAddrs := make([]ma.Multiaddr, len(announce))
for i, addr := range announce {
annAddrs[i], err = ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
existing[addr] = true
}

appendAnnAddrs := make([]ma.Multiaddr, len(appendAnnouce))
for i, addr := range appendAnnouce {
appendAnnAddrs[i], err = ma.NewMultiaddr(addr)
var appendAnnAddrs []ma.Multiaddr
for _, addr := range appendAnnouce {
if existing[addr] {
// skip AppendAnnounce that is on the Announce list already
continue
}
appendAddr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}
appendAnnAddrs = append(appendAnnAddrs, appendAddr)
}

filters := ma.NewFilters()
Expand Down
13 changes: 10 additions & 3 deletions test/sharness/t0140-swarm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ test_expect_success 'Addresses.Announce affects addresses' '

test_kill_ipfs_daemon

announceCfg='["/dnsaddr/dynamic.example.com", "/ip4/10.20.30.40/tcp/4321"]'
test_expect_success "test_config_set AppendAnnounce succeeds" "
ipfs config --json Addresses.AppendAnnounce '$announceCfg'

announceCfg='["/ip4/127.0.0.1/tcp/4001", "/ip4/1.2.3.4/tcp/1234"]'
test_expect_success "test_config_set succeeds" "
ipfs config --json Addresses.Announce '$announceCfg'
"
# Include "/ip4/1.2.3.4/tcp/1234" to ensure we deduplicate addrs already present in Swarm.Announce
appendAnnounceCfg='["/dnsaddr/dynamic.example.com", "/ip4/10.20.30.40/tcp/4321", "/ip4/1.2.3.4/tcp/1234"]'
test_expect_success "test_config_set Announce and AppendAnnounce succeeds" "
ipfs config --json Addresses.Announce '$announceCfg' &&
ipfs config --json Addresses.AppendAnnounce '$appendAnnounceCfg'
"

test_launch_ipfs_daemon
Expand Down

0 comments on commit 9673571

Please sign in to comment.