From 17d7eda377712c0bc18fc39f5280c07791a6b203 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 8 Jan 2024 10:51:38 -0500 Subject: [PATCH] fix: handle delay being nil --- service/p2p.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/service/p2p.go b/service/p2p.go index 44f32e4..f0577ad 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -180,10 +180,15 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool) error { p.logger.Error("failed to connect", zap.String("node", connectionUri.String()), zap.Error(err)) - delay := *p.reconnectDelay.GetInt(idString) - p.reconnectDelay.PutInt(idString, delay*2) + delay := p.reconnectDelay.GetInt(idString) + if delay == nil { + tmp := 1 + delay = &tmp + } + delayDeref := *delay + p.reconnectDelay.PutInt(idString, delayDeref*2) - time.Sleep(time.Duration(delay) * time.Second) + time.Sleep(time.Duration(delayDeref) * time.Second) return p.ConnectToNode(connectionUris, retried) } @@ -339,3 +344,33 @@ func (p *P2PImpl) SignMessageSimple(message []byte) ([]byte, error) { return result, nil } + +func (p *P2PImpl) AddPeer(peer net.Peer) error { + peerId, err := peer.Id().ToString() + if err != nil { + return err + } + p.peers.Put(peerId, peer) + p.reconnectDelay.Put(peerId, 1) + + return nil +} +func (p *P2PImpl) SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) error { + announceRequest := signed.NewAnnounceRequest(peer, peersToSend) + + message, err := msgpack.Marshal(announceRequest) + + if err != nil { + return err + } + + signedMessage, err := p.SignMessageSimple(message) + + if err != nil { + return err + } + + err = peer.SendMessage(signedMessage) + + return nil +}