From 1e05216b7c9a8ce60556bb8f5bbe016b6ef3a5d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Roman=20Smr=C5=BE?= <roman.smrz@seznam.cz>
Date: Thu, 18 Jul 2024 20:34:31 +0200
Subject: Network: avoid error message on superseded channel request

---
 src/Erebos/Network.hs | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/Erebos/Network.hs b/src/Erebos/Network.hs
index 402e163..442285a 100644
--- a/src/Erebos/Network.hs
+++ b/src/Erebos/Network.hs
@@ -535,8 +535,12 @@ handlePacket identity secure peer chanSvc svcs (TransportHeader headers) prefs =
                         liftSTM $ finalizedChannel peer ch identity
                     _ -> return ()
 
-            Rejected dgst -> do
-                logd $ "rejected by peer: " ++ show dgst
+            Rejected dgst
+                | peerRequest : _ <- mapMaybe (\case TrChannelRequest d -> Just d; _ -> Nothing) headers
+                , peerRequest < dgst
+                -> return () -- Our request was rejected due to lower priority
+
+                | otherwise -> logd $ "rejected by peer: " ++ show dgst
 
             DataRequest dgst
                 | secure || dgst `elem` plaintextRefs -> do
@@ -607,9 +611,15 @@ handlePacket identity secure peer chanSvc svcs (TransportHeader headers) prefs =
                     ChannelCookieWait {} -> return ()
                     ChannelCookieReceived {} -> process
                     ChannelCookieConfirmed {} -> process
-                    ChannelOurRequest our | dgst < refDigest (storedRef our) -> process
-                                          | otherwise -> reject
-                    ChannelPeerRequest {} -> process
+                    ChannelOurRequest our
+                        | dgst < refDigest (storedRef our) -> process
+                        | otherwise -> do
+                            -- Reject peer channel request with lower priority
+                            addHeader $ TrChannelRequest $ refDigest $ storedRef our
+                            reject
+                    ChannelPeerRequest prev
+                        | dgst == wrDigest prev -> addHeader $ Acknowledged dgst
+                        | otherwise -> process
                     ChannelOurAccept {} -> reject
                     ChannelEstablished {} -> process
                     ChannelClosed {} -> return ()
-- 
cgit v1.2.3