Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 77ca2a5

Browse files
committedJul 30, 2024·
fix: DNS TTL not respected
1 parent 3aea3d1 commit 77ca2a5

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed
 

‎p2p/peer.go

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Peer interface {
3232
IsOutbound() bool // did we dial the peer
3333
IsPersistent() bool // do we redial this peer when we disconnect
3434

35+
IPHasChanged() bool // has the peer's IP changed
36+
3537
CloseConn() error // close original connection
3638

3739
NodeInfo() NodeInfo // peer's info
@@ -300,6 +302,13 @@ func (p *peer) IsPersistent() bool {
300302
return p.peerConn.persistent
301303
}
302304

305+
// IPHasChanged returns true and the new IP if the peer's IP has changed.
306+
func (p *peer) IPHasChanged() bool {
307+
oldIP := p.ip
308+
newIP := p.RemoteIP()
309+
return !oldIP.Equal(newIP)
310+
}
311+
303312
// NodeInfo returns a copy of the peer's NodeInfo.
304313
func (p *peer) NodeInfo() NodeInfo {
305314
return p.nodeInfo

‎p2p/peer_set_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (mp *mockPeer) NodeInfo() NodeInfo { return DefaultNodeInfo{}
2727
func (mp *mockPeer) Status() ConnectionStatus { return ConnectionStatus{} }
2828
func (mp *mockPeer) ID() ID { return mp.id }
2929
func (mp *mockPeer) IsOutbound() bool { return false }
30+
func (mp *mockPeer) IPHasChanged() bool { return true }
3031
func (mp *mockPeer) IsPersistent() bool { return true }
3132
func (mp *mockPeer) Get(s string) interface{} { return s }
3233
func (mp *mockPeer) Set(string, interface{}) {}

‎p2p/switch.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -381,22 +381,38 @@ func (sw *Switch) StopPeerForError(peer Peer, reason interface{}) {
381381
sw.stopAndRemovePeer(peer, reason)
382382

383383
if peer.IsPersistent() {
384-
var addr *NetAddress
385-
if peer.IsOutbound() { // socket address for outbound peers
386-
addr = peer.SocketAddr()
387-
} else { // self-reported address for inbound peers
388-
var err error
389-
addr, err = peer.NodeInfo().NetAddress()
390-
if err != nil {
391-
sw.Logger.Error("Wanted to reconnect to inbound peer, but self-reported address is wrong",
392-
"peer", peer, "err", err)
393-
return
394-
}
384+
addr, err := sw.getPeerAddress(peer)
385+
if err != nil {
386+
return
387+
}
388+
go sw.reconnectToPeer(addr)
389+
}
390+
391+
if peer.IPHasChanged() {
392+
addr, err := sw.getPeerAddress(peer)
393+
if err != nil {
394+
return
395395
}
396396
go sw.reconnectToPeer(addr)
397397
}
398398
}
399399

400+
// getPeerAddress returns the appropriate NetAddress for a given peer,
401+
// handling both outbound and inbound peers.
402+
func (sw *Switch) getPeerAddress(peer Peer) (*NetAddress, error) {
403+
if peer.IsOutbound() {
404+
return peer.SocketAddr(), nil
405+
}
406+
// For inbound peers, get the self-reported address
407+
addr, err := peer.NodeInfo().NetAddress()
408+
if err != nil {
409+
sw.Logger.Error("Failed to get address for inbound peer",
410+
"peer", peer, "err", err)
411+
return nil, err
412+
}
413+
return addr, nil
414+
}
415+
400416
// StopPeerGracefully disconnects from a peer gracefully.
401417
// TODO: handle graceful disconnects.
402418
func (sw *Switch) StopPeerGracefully(peer Peer) {

‎p2p/switch_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,37 @@ func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
566566
assert.Equal(t, 1, sw.Peers().Size())
567567
}
568568

569+
func TestSwitchReconnectsWhenOutboundPeerDnsChanges(t *testing.T) {
570+
sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
571+
err := sw.Start()
572+
require.NoError(t, err)
573+
t.Cleanup(func() {
574+
if err := sw.Stop(); err != nil {
575+
t.Error(err)
576+
}
577+
})
578+
579+
// change the DNS of the peer
580+
// Initial setup
581+
initialPrivKey := ed25519.GenPrivKey()
582+
583+
rp := &remotePeer{PrivKey: initialPrivKey, Config: cfg}
584+
rp.Start()
585+
586+
fmt.Println("Dialing to", rp.Addr().String())
587+
588+
// Now change the private key
589+
newPrivKey := ed25519.GenPrivKey()
590+
rp.PrivKey = newPrivKey
591+
rp.Start()
592+
593+
fmt.Println("Dialing to", rp.Addr().String())
594+
// get all peers
595+
peers := sw.Peers().List()
596+
fmt.Println("Peers", peers)
597+
// Add assertions or further logic to verify the effects of the key change
598+
}
599+
569600
func TestSwitchDialPeersAsync(t *testing.T) {
570601
if testing.Short() {
571602
return

0 commit comments

Comments
 (0)
Please sign in to comment.