@@ -15,11 +15,13 @@ package tests
1515import (
1616 "fmt"
1717
18+ "github.com/multiformats/go-multiaddr"
1819 "github.com/sourcenetwork/go-p2p"
1920 "github.com/sourcenetwork/immutable"
2021
2122 acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
2223 "github.com/sourcenetwork/defradb/crypto"
24+ "github.com/sourcenetwork/defradb/errors"
2325 "github.com/sourcenetwork/defradb/internal/db"
2426 "github.com/sourcenetwork/defradb/internal/kms"
2527 "github.com/sourcenetwork/defradb/node"
@@ -163,7 +165,36 @@ func setupNode(
163165
164166 addresses , err := nodeObj .DB .PeerInfo ()
165167 require .NoError (s .T , err )
168+ // The addresses returned by PeerInfo include the /p2p/<peerID> part, but
169+ // the libp2p.ListenAddrStrings cannot include it, so we need to remove it
170+ // before caching the addresses on the state.
171+ addresses , err = removePeerIDFromAddr (addresses )
172+ require .NoError (s .T , err )
166173 st .CachedAddresses = addresses
167174
168175 return st , nil
169176}
177+
178+ func removePeerIDFromAddr (addr []string ) ([]string , error ) {
179+ addrs := make ([]string , len (addr ))
180+ for i , a := range addr {
181+ justAddr , err := removePeerID (a )
182+ if err != nil {
183+ return nil , err
184+ }
185+ addrs [i ] = justAddr
186+ }
187+ return addrs , nil
188+ }
189+
190+ func removePeerID (addr string ) (string , error ) {
191+ maddrWithID , err := multiaddr .NewMultiaddr (addr )
192+ if err != nil {
193+ return "" , err
194+ }
195+ justAddr , p2ppart := multiaddr .SplitLast (maddrWithID )
196+ if p2ppart == nil || p2ppart .Protocol ().Code != multiaddr .P_P2P {
197+ return "" , errors .New ("address does not contain a /p2p/ part" )
198+ }
199+ return justAddr .String (), nil
200+ }
0 commit comments