diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index 35ab81dd7..07d66d001 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -3751,13 +3751,13 @@ processSMPTransmissions c@AgentClient {subQ, smpServersStats} (tSess@(userId, sr notify $ MSG msgMeta msgFlags body pure ACKPending A_RCVD rcpts -> qDuplex conn'' "RCVD" $ messagesRcvd rcpts msgMeta - A_QCONT addr -> qDuplexAckDel conn'' "QCONT" $ continueSending srvMsgId addr - QADD qs -> qDuplexAckDel conn'' "QADD" $ qAddMsg srvMsgId qs - QKEY qs -> qDuplexAckDel conn'' "QKEY" $ qKeyMsg srvMsgId qs - QUSE qs -> qDuplexAckDel conn'' "QUSE" $ qUseMsg srvMsgId qs + A_QCONT addr -> notifyIntegrity msgMeta >> qDuplexAckDel conn'' "QCONT" (continueSending srvMsgId addr) + QADD qs -> notifyIntegrity msgMeta >> qDuplexAckDel conn'' "QADD" (qAddMsg srvMsgId qs) + QKEY qs -> notifyIntegrity msgMeta >> qDuplexAckDel conn'' "QKEY" (qKeyMsg srvMsgId qs) + QUSE qs -> notifyIntegrity msgMeta >> qDuplexAckDel conn'' "QUSE" (qUseMsg srvMsgId qs) -- no action needed for QTEST -- any message in the new queue will mark it active and trigger deletion of the old queue - QTEST _ -> logServer "<--" c srv rId ("MSG :" <> logSecret' (SMP.unMsgId srvMsgId)) >> ackDel msgId + QTEST _ -> notifyIntegrity msgMeta >> logServer "<--" c srv rId ("MSG :" <> logSecret' (SMP.unMsgId srvMsgId)) >> ackDel msgId QEND addrs -> qDuplexAckDel conn'' "QEND" $ qEndMsg srvMsgId addrs EREADY _ -> qDuplexAckDel conn'' "EREADY" $ ereadyMsg rcPrev where @@ -3899,6 +3899,9 @@ processSMPTransmissions c@AgentClient {subQ, smpServersStats} (tSess@(userId, sr logError $ "prohibited: " <> s notify . ERR . AGENT $ A_PROHIBITED $ T.unpack s + notifyIntegrity :: MsgMeta -> AM () + notifyIntegrity mm = when (integrity mm /= MsgOk) $ notify $ MSGERR mm + enqueueCmd :: InternalCommand -> AM () enqueueCmd = enqueueCommand c "" connId (Just srv) . AInternalCommand @@ -4085,7 +4088,7 @@ processSMPTransmissions c@AgentClient {subQ, smpServersStats} (tSess@(userId, sr rs <- forM rcpts $ \rcpt -> clientReceipt rcpt `catchAllErrors` \e -> notify (ERR e) $> Nothing case L.nonEmpty . catMaybes $ L.toList rs of Just rs' -> notify (RCVD msgMeta rs') $> ACKPending - Nothing -> ack + Nothing -> notifyIntegrity msgMeta >> ack where ack :: AM ACKd ack = enqueueCmd (ICAck rId srvMsgId) $> ACKd diff --git a/src/Simplex/Messaging/Agent/Protocol.hs b/src/Simplex/Messaging/Agent/Protocol.hs index e7cf26969..35ae55b5e 100644 --- a/src/Simplex/Messaging/Agent/Protocol.hs +++ b/src/Simplex/Messaging/Agent/Protocol.hs @@ -438,6 +438,7 @@ data AEvent (e :: AEntity) where MSG :: MsgMeta -> MsgFlags -> MsgBody -> AEvent AEConn MSGNTF :: MsgId -> Maybe UTCTime -> AEvent AEConn RCVD :: MsgMeta -> NonEmpty MsgReceipt -> AEvent AEConn + MSGERR :: MsgMeta -> AEvent AEConn QCONT :: AEvent AEConn DEL_RCVQS :: NonEmpty (ConnId, SMPServer, SMP.RecipientId, Maybe AgentErrorType) -> AEvent AEConn DEL_CONNS :: NonEmpty ConnId -> AEvent AEConn @@ -522,6 +523,7 @@ data AEventTag (e :: AEntity) where MSG_ :: AEventTag AEConn MSGNTF_ :: AEventTag AEConn RCVD_ :: AEventTag AEConn + MSGERR_ :: AEventTag AEConn QCONT_ :: AEventTag AEConn DEL_RCVQS_ :: AEventTag AEConn DEL_CONNS_ :: AEventTag AEConn @@ -588,6 +590,7 @@ aEventTag = \case MSG {} -> MSG_ MSGNTF {} -> MSGNTF_ RCVD {} -> RCVD_ + MSGERR _ -> MSGERR_ QCONT -> QCONT_ DEL_RCVQS _ -> DEL_RCVQS_ DEL_CONNS _ -> DEL_CONNS_ diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index a036a7e2e..dc0037da5 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -472,6 +472,7 @@ functionalAPITests ps = do it "should expire multiple messages" $ testExpireManyMessages ps it "should expire one message if quota is exceeded" $ testExpireMessageQuota ps it "should expire multiple messages if quota is exceeded" $ testExpireManyMessagesQuota ps + it "should report expired message when the next message is QCONT" $ testExpireMessageBeforeQCONT ps describe "joining a full contact address" $ do it "should retry joining until the address has space" $ testJoinFullContactAsync ps it "should fail joining after quota exceeded timeout" $ testJoinFullContactAsyncExpire ps @@ -2724,6 +2725,40 @@ testExpireManyMessagesQuota (t, msType) = withSmpServerConfigOn t cfg' testPort where cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {msgQueueQuota = 1} +testExpireMessageBeforeQCONT :: (HasCallStack, HasTestEnv) => (ASrvTransport, AStoreType) -> IO () +testExpireMessageBeforeQCONT (t, msType) = withSmpServerConfigOn t cfg' testPort $ \_ -> do + a <- getSMPAgentClient' 1 agentCfg {quotaExceededTimeout = 1, messageRetryInterval = fastMessageRetryInterval} initAgentServers testDB + b <- getSMPAgentClient' 2 agentCfg initAgentServers testDB2 + (aId, bId) <- runRight $ do + (aId, bId) <- makeConnection a b + liftIO $ threadDelay 500000 >> disposeAgentClient b + 2 <- sendMessage a bId SMP.noMsgFlags "1" + get a ##> ("", bId, SENT 2) + 3 <- sendMessage a bId SMP.noMsgFlags "2" + get a =##> \case ("", c, MERR 3 (SMP _ QUOTA)) -> bId == c; _ -> False + pure (aId, bId) + withAgent 3 agentCfg {messageRetryInterval = fastMessageRetryInterval} initAgentServers testDB2 $ \b' -> runRight_ $ do + subscribeConnection b' aId + get b' =##> \case ("", c, Msg "1") -> c == aId; _ -> False + 3 <- sendMessage b' aId SMP.noMsgFlags "hello" + get b' ##> ("", aId, SENT 3) + get a =##> \case ("", c, Msg "hello") -> c == bId; _ -> False + 4 <- sendMessage b' aId SMP.noMsgFlags "hello 2" + pGet' b' False =##> \case ("", c, AEvt _ (MWARN 4 (SMP _ QUOTA))) -> aId == c; _ -> False + ackMessage b' aId 2 Nothing + ackMessage a bId 4 Nothing + liftIO . getInAnyOrder b' $ + [ \case ("", c, AEvt SAEConn (MSGERR MsgMeta {integrity = MsgError (MsgSkipped 3 3)})) -> c == aId; _ -> False, + \case ("", c, AEvt SAEConn QCONT) -> c == aId; _ -> False, + \case ("", c, AEvt SAEConn (SENT 4)) -> c == aId; _ -> False + ] + get a =##> \case ("", c, Msg "hello 2") -> c == bId; _ -> False + ackMessage a bId 6 Nothing + get a =##> \case ("", c, QCONT) -> c == bId; _ -> False + disposeAgentClient a + where + cfg' = updateCfg (cfgMS msType) $ \cfg_ -> cfg_ {msgQueueQuota = 1} + testJoinFullContactAsync :: (HasCallStack, HasTestEnv) => (ASrvTransport, AStoreType) -> IO () testJoinFullContactAsync (t, msType) = withSmpServerConfigOn t cfg' testPort $ \_ -> do (contactId, qInfo) <- fillContactAddress