Skip to content

Commit 3175ae3

Browse files
split async accept too
1 parent 6d6fe2c commit 3175ae3

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/Simplex/Messaging/Agent.hs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,20 @@ getConnShortLinkAsync c = withAgentEnv c .:: getConnShortLinkAsync' c
379379
{-# INLINE getConnShortLinkAsync #-}
380380

381381
-- | Enqueue JOIN command for a prepared connection.
382-
joinConnectionAsync :: ConnectionModeI c => AgentClient -> ACorrId -> ConnId -> Bool -> ConnectionRequestUri c -> ConnInfo -> PQSupport -> SubscriptionMode -> AE ()
383-
joinConnectionAsync c aCorrId connId enableNtfs cReqUri cInfo pqSup subMode =
384-
withAgentEnv c $ joinConnAsync c aCorrId connId enableNtfs cReqUri cInfo pqSup subMode
382+
joinConnectionAsync :: ConnectionModeI c => AgentClient -> ACorrId -> Bool -> ConnId -> Bool -> ConnectionRequestUri c -> ConnInfo -> PQSupport -> SubscriptionMode -> AE ()
383+
joinConnectionAsync c aCorrId updateConn connId enableNtfs cReqUri cInfo pqSup subMode =
384+
withAgentEnv c $ joinConnAsync c aCorrId updateConn connId enableNtfs cReqUri cInfo pqSup subMode
385385
{-# INLINE joinConnectionAsync #-}
386386

387387
-- | Allow connection to continue after CONF notification (LET command), no synchronous response
388388
allowConnectionAsync :: AgentClient -> ACorrId -> ConnId -> ConfirmationId -> ConnInfo -> AE ()
389389
allowConnectionAsync c = withAgentEnv c .:: allowConnectionAsync' c
390390
{-# INLINE allowConnectionAsync #-}
391391

392-
-- | Accept contact after REQ notification (ACPT command) asynchronously, synchronous response is new connection id
393-
acceptContactAsync :: AgentClient -> UserId -> ACorrId -> Bool -> ConfirmationId -> ConnInfo -> PQSupport -> SubscriptionMode -> AE ConnId
394-
acceptContactAsync c userId aCorrId enableNtfs = withAgentEnv c .:: acceptContactAsync' c userId aCorrId enableNtfs
392+
-- | Accept contact after REQ notification (ACPT command) asynchronously, for a prepared connection.
393+
acceptContactAsync :: AgentClient -> ACorrId -> ConnId -> Bool -> ConfirmationId -> ConnInfo -> PQSupport -> SubscriptionMode -> AE ()
394+
acceptContactAsync c aCorrId connId enableNtfs invId ownConnInfo pqSupport subMode =
395+
withAgentEnv c $ acceptContactAsync' c aCorrId connId enableNtfs invId ownConnInfo pqSupport subMode
395396
{-# INLINE acceptContactAsync #-}
396397

397398
-- | Acknowledge message (ACK command) asynchronously, no synchronous response
@@ -856,17 +857,19 @@ newConnNoQueues c userId enableNtfs cMode pqSupport = do
856857
let cData = ConnData {userId, connId = "", connAgentVersion, enableNtfs, lastExternalSndId = 0, deleted = False, ratchetSyncState = RSOk, pqSupport}
857858
withStore c $ \db -> createNewConn db g cData cMode
858859

859-
joinConnAsync :: ConnectionModeI c => AgentClient -> ACorrId -> ConnId -> Bool -> ConnectionRequestUri c -> ConnInfo -> PQSupport -> SubscriptionMode -> AM ()
860-
joinConnAsync c corrId connId enableNtfs cReqUri@CRInvitationUri {} cInfo pqSup subMode =
860+
joinConnAsync :: ConnectionModeI c => AgentClient -> ACorrId -> Bool -> ConnId -> Bool -> ConnectionRequestUri c -> ConnInfo -> PQSupport -> SubscriptionMode -> AM ()
861+
joinConnAsync c corrId updateConn connId enableNtfs cReqUri@CRInvitationUri {} cInfo pqSup subMode =
861862
lift (compatibleInvitationUri cReqUri) >>= \case
862863
Just (_, Compatible (CR.E2ERatchetParams v _ _ _), Compatible connAgentVersion) -> do
863864
let pqSupport = pqSup `CR.pqSupportAnd` versionPQSupport_ connAgentVersion (Just v)
865+
when updateConn $ withStore' c $ \db -> updateNewConnJoin db connId connAgentVersion pqSupport enableNtfs
864866
enqueueCommand c corrId connId Nothing $ AClientCommand $ JOIN enableNtfs (ACR sConnectionMode cReqUri) pqSupport subMode cInfo
865867
Nothing -> throwE $ AGENT A_VERSION
866-
joinConnAsync c corrId connId enableNtfs cReqUri@(CRContactUri _) cInfo pqSup subMode =
868+
joinConnAsync c corrId updateConn connId enableNtfs cReqUri@(CRContactUri _) cInfo pqSup subMode =
867869
lift (compatibleContactUri cReqUri) >>= \case
868870
Just (_, Compatible connAgentVersion) -> do
869871
let pqSupport = pqSup `CR.pqSupportAnd` versionPQSupport_ connAgentVersion Nothing
872+
when updateConn $ withStore' c $ \db -> updateNewConnJoin db connId connAgentVersion pqSupport enableNtfs
870873
enqueueCommand c corrId connId Nothing $ AClientCommand $ JOIN enableNtfs (ACR sConnectionMode cReqUri) pqSupport subMode cInfo
871874
Nothing -> throwE $ AGENT A_VERSION
872875

@@ -883,15 +886,13 @@ allowConnectionAsync' c corrId connId confId ownConnInfo =
883886
-- and also it can't be triggered by user concurrently several times in a row. It could be improved similarly to
884887
-- `acceptContact` by creating a new map for invitation locks and taking lock here, and removing `unacceptInvitation`
885888
-- while marking invitation as accepted inside "lock level transaction" after successful `joinConnAsync`.
886-
acceptContactAsync' :: AgentClient -> UserId -> ACorrId -> Bool -> InvitationId -> ConnInfo -> PQSupport -> SubscriptionMode -> AM ConnId
887-
acceptContactAsync' c userId corrId enableNtfs invId ownConnInfo pqSupport subMode = do
889+
acceptContactAsync' :: AgentClient -> ACorrId -> ConnId -> Bool -> InvitationId -> ConnInfo -> PQSupport -> SubscriptionMode -> AM ()
890+
acceptContactAsync' c corrId connId enableNtfs invId ownConnInfo pqSupport subMode = do
888891
Invitation {connReq} <- withStore c $ \db -> getInvitation db "acceptContactAsync'" invId
889-
connId <- newConnToJoin c userId "" enableNtfs connReq pqSupport
890892
withStore' c $ \db -> acceptInvitation db invId ownConnInfo
891-
joinConnAsync c corrId connId enableNtfs connReq ownConnInfo pqSupport subMode `catchAllErrors` \err -> do
893+
joinConnAsync c corrId False connId enableNtfs connReq ownConnInfo pqSupport subMode `catchAllErrors` \err -> do
892894
withStore' c (`unacceptInvitation` invId)
893895
throwE err
894-
pure connId
895896

896897
ackMessageAsync' :: AgentClient -> ACorrId -> ConnId -> AgentMsgId -> Maybe MsgReceiptInfo -> AM ()
897898
ackMessageAsync' c corrId connId msgId rcptInfo_ = do

tests/AgentTests/FunctionalAPITests.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ testInvitationShortLinkAsync viaProxy a b = do
14261426
linkUserData connData' `shouldBe` userData
14271427
runRight $ do
14281428
aId <- A.prepareConnectionToJoin b 1 True connReq PQSupportOn
1429-
A.joinConnectionAsync b "123" aId True connReq "bob's connInfo" PQSupportOn SMSubscribe
1429+
A.joinConnectionAsync b "123" False aId True connReq "bob's connInfo" PQSupportOn SMSubscribe
14301430
get b =##> \case ("123", c, JOINED sndSecure) -> c == aId && sndSecure; _ -> False
14311431
("", _, CONF confId _ "bob's connInfo") <- get a
14321432
allowConnection a bId confId "alice's connInfo"
@@ -2691,7 +2691,7 @@ testAsyncCommands sqSecured alice bob baseId =
26912691
("1", bobId', INV (ACR _ qInfo)) <- get alice
26922692
liftIO $ bobId' `shouldBe` bobId
26932693
aliceId <- prepareConnectionToJoin bob 1 True qInfo PQSupportOn
2694-
joinConnectionAsync bob "2" aliceId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
2694+
joinConnectionAsync bob "2" False aliceId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
26952695
("2", aliceId', JOINED sqSecured') <- get bob
26962696
liftIO $ do
26972697
aliceId' `shouldBe` aliceId
@@ -2782,7 +2782,7 @@ testGetConnShortLinkAsync ps = withAgentClients2 $ \alice bob ->
27822782
liftIO $ qInfo' `shouldBe` qInfo
27832783
liftIO $ userCtData' `shouldBe` userCtData
27842784
-- join connection async using connId from getConnShortLinkAsync
2785-
joinConnectionAsync bob "2" newId True qInfo' "bob's connInfo" PQSupportOn SMSubscribe
2785+
joinConnectionAsync bob "2" True newId True qInfo' "bob's connInfo" PQSupportOn SMSubscribe
27862786
let aliceId = newId
27872787
("2", aliceId', JOINED False) <- get bob
27882788
liftIO $ aliceId' `shouldBe` aliceId
@@ -2818,7 +2818,8 @@ testAcceptContactAsync sqSecured alice bob baseId =
28182818
(aliceId, sqSecuredJoin) <- joinConnection bob 1 True qInfo "bob's connInfo" SMSubscribe
28192819
liftIO $ sqSecuredJoin `shouldBe` False -- joining via contact address connection
28202820
("", _, REQ invId _ "bob's connInfo") <- get alice
2821-
bobId <- acceptContactAsync alice 1 "1" True invId "alice's connInfo" PQSupportOn SMSubscribe
2821+
bobId <- prepareConnectionToAccept alice 1 True invId PQSupportOn
2822+
acceptContactAsync alice "1" bobId True invId "alice's connInfo" PQSupportOn SMSubscribe
28222823
get alice =##> \case ("1", c, JOINED sqSecured') -> c == bobId && sqSecured' == sqSecured; _ -> False
28232824
("", _, CONF confId _ "alice's connInfo") <- get bob
28242825
allowConnection bob aliceId confId "bob's connInfo"
@@ -3094,7 +3095,7 @@ testJoinConnectionAsyncReplyErrorV8 ps@(t, ASType qsType _) = do
30943095
("1", bId', INV (ACR _ qInfo)) <- get a
30953096
liftIO $ bId' `shouldBe` bId
30963097
aId <- prepareConnectionToJoin b 1 True qInfo PQSupportOn
3097-
joinConnectionAsync b "2" aId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
3098+
joinConnectionAsync b "2" False aId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
30983099
liftIO $ threadDelay 500000
30993100
ConnectionStats {rcvQueuesInfo = [], sndQueuesInfo = [SndQueueInfo {}]} <- getConnectionServers b aId
31003101
pure (aId, bId)
@@ -3141,7 +3142,7 @@ testJoinConnectionAsyncReplyError ps@(t, ASType qsType _) = do
31413142
("1", bId', INV (ACR _ qInfo)) <- get a
31423143
liftIO $ bId' `shouldBe` bId
31433144
aId <- prepareConnectionToJoin b 1 True qInfo PQSupportOn
3144-
joinConnectionAsync b "2" aId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
3145+
joinConnectionAsync b "2" False aId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
31453146
liftIO $ threadDelay 500000
31463147
ConnectionStats {rcvQueuesInfo = [], sndQueuesInfo = [SndQueueInfo {}]} <- getConnectionServers b aId
31473148
pure (aId, bId)

0 commit comments

Comments
 (0)