Skip to content

Commit 532d466

Browse files
committed
agent: pass server when creating connection
Add Maybe SMPServerWithAuth parameter to createConnection and newConn so callers can specify the SMP server for the new queue instead of always using getSMPServer. When Nothing, falls back to existing behavior.
1 parent 1f173ab commit 532d466

3 files changed

Lines changed: 33 additions & 32 deletions

File tree

src/Simplex/Messaging/Agent.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,9 @@ deleteConnectionsAsync c waitDelivery = withAgentEnv c . deleteConnectionsAsync'
397397
{-# INLINE deleteConnectionsAsync #-}
398398

399399
-- | Create SMP agent connection (NEW command)
400-
createConnection :: ConnectionModeI c => AgentClient -> NetworkRequestMode -> UserId -> Bool -> Bool -> SConnectionMode c -> Maybe (UserConnLinkData c) -> Maybe CRClientData -> CR.InitialKeys -> SubscriptionMode -> AE (ConnId, (CreatedConnLink c, Maybe ClientServiceId))
401-
createConnection c nm userId enableNtfs checkNotices = withAgentEnv c .::. newConn c nm userId enableNtfs checkNotices
400+
createConnection :: ConnectionModeI c => AgentClient -> NetworkRequestMode -> UserId -> Bool -> Bool -> SConnectionMode c -> Maybe (UserConnLinkData c) -> Maybe CRClientData -> Maybe SMPServerWithAuth -> CR.InitialKeys -> SubscriptionMode -> AE (ConnId, (CreatedConnLink c, Maybe ClientServiceId))
401+
createConnection c nm userId enableNtfs checkNotices cMode linkData_ clientData srv_ pqInitKeys subMode =
402+
withAgentEnv c $ newConn c nm userId enableNtfs checkNotices cMode linkData_ clientData srv_ pqInitKeys subMode
402403
{-# INLINE createConnection #-}
403404

404405
-- | Prepare connection link for contact mode (no network call).
@@ -914,9 +915,9 @@ switchConnectionAsync' c corrId connId =
914915
connectionStats c $ DuplexConnection cData rqs' sqs
915916
_ -> throwE $ CMD PROHIBITED "switchConnectionAsync: not duplex"
916917

917-
newConn :: ConnectionModeI c => AgentClient -> NetworkRequestMode -> UserId -> Bool -> Bool -> SConnectionMode c -> Maybe (UserConnLinkData c) -> Maybe CRClientData -> CR.InitialKeys -> SubscriptionMode -> AM (ConnId, (CreatedConnLink c, Maybe ClientServiceId))
918-
newConn c nm userId enableNtfs checkNotices cMode linkData_ clientData pqInitKeys subMode = do
919-
srv <- getSMPServer c userId
918+
newConn :: ConnectionModeI c => AgentClient -> NetworkRequestMode -> UserId -> Bool -> Bool -> SConnectionMode c -> Maybe (UserConnLinkData c) -> Maybe CRClientData -> Maybe SMPServerWithAuth -> CR.InitialKeys -> SubscriptionMode -> AM (ConnId, (CreatedConnLink c, Maybe ClientServiceId))
919+
newConn c nm userId enableNtfs checkNotices cMode linkData_ clientData srv_ pqInitKeys subMode = do
920+
srv <- maybe (getSMPServer c userId) pure srv_
920921
when (checkNotices && connMode cMode == CMContact) $ checkClientNotices c srv
921922
connId <- newConnNoQueues c userId enableNtfs cMode (CR.connPQEncryption pqInitKeys)
922923
(connId,)

tests/AgentTests/FunctionalAPITests.hs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ inAnyOrder g rs = withFrozenCallStack $ do
286286

287287
createConnection :: ConnectionModeI c => AgentClient -> UserId -> Bool -> SConnectionMode c -> Maybe CRClientData -> SubscriptionMode -> AE (ConnId, ConnectionRequestUri c)
288288
createConnection c userId enableNtfs cMode clientData subMode = do
289-
(connId, (CCLink cReq _, Nothing)) <- A.createConnection c NRMInteractive userId enableNtfs True cMode Nothing clientData IKPQOn subMode
289+
(connId, (CCLink cReq _, Nothing)) <- A.createConnection c NRMInteractive userId enableNtfs True cMode Nothing clientData Nothing IKPQOn subMode
290290
pure (connId, cReq)
291291

292292
joinConnection :: AgentClient -> UserId -> Bool -> ConnectionRequestUri c -> ConnInfo -> SubscriptionMode -> AE (ConnId, SndQueueSecured)
@@ -718,7 +718,7 @@ runAgentClientTest pqSupport sqSecured viaProxy alice bob baseId =
718718
runAgentClientTestPQ :: HasCallStack => SndQueueSecured -> Bool -> (AgentClient, InitialKeys) -> (AgentClient, PQSupport) -> AgentMsgId -> IO ()
719719
runAgentClientTestPQ sqSecured viaProxy (alice, aPQ) (bob, bPQ) baseId =
720720
runRight_ $ do
721-
(bobId, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMInvitation Nothing Nothing aPQ SMSubscribe
721+
(bobId, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMInvitation Nothing Nothing Nothing aPQ SMSubscribe
722722
aliceId <- A.prepareConnectionToJoin bob 1 True qInfo bPQ
723723
(sqSecured', Nothing) <- A.joinConnection bob NRMInteractive 1 aliceId True qInfo "bob's connInfo" bPQ SMSubscribe
724724
liftIO $ sqSecured' `shouldBe` sqSecured
@@ -948,7 +948,7 @@ runAgentClientContactTest pqSupport sqSecured viaProxy alice bob baseId =
948948
runAgentClientContactTestPQ :: HasCallStack => SndQueueSecured -> Bool -> PQSupport -> (AgentClient, InitialKeys) -> (AgentClient, PQSupport) -> AgentMsgId -> IO ()
949949
runAgentClientContactTestPQ sqSecured viaProxy reqPQSupport (alice, aPQ) (bob, bPQ) baseId =
950950
runRight_ $ do
951-
(_, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing aPQ SMSubscribe
951+
(_, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing Nothing aPQ SMSubscribe
952952
aliceId <- A.prepareConnectionToJoin bob 1 True qInfo bPQ
953953
(sqSecuredJoin, Nothing) <- A.joinConnection bob NRMInteractive 1 aliceId True qInfo "bob's connInfo" bPQ SMSubscribe
954954
liftIO $ sqSecuredJoin `shouldBe` False -- joining via contact address connection
@@ -992,7 +992,7 @@ runAgentClientContactTestPQ sqSecured viaProxy reqPQSupport (alice, aPQ) (bob, b
992992

993993
runAgentClientContactTestPQ3 :: HasCallStack => Bool -> (AgentClient, InitialKeys) -> (AgentClient, PQSupport) -> (AgentClient, PQSupport) -> AgentMsgId -> IO ()
994994
runAgentClientContactTestPQ3 viaProxy (alice, aPQ) (bob, bPQ) (tom, tPQ) baseId = runRight_ $ do
995-
(_, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing aPQ SMSubscribe
995+
(_, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing Nothing aPQ SMSubscribe
996996
(bAliceId, bobId, abPQEnc) <- connectViaContact bob bPQ qInfo
997997
sentMessages abPQEnc alice bobId bob bAliceId
998998
(tAliceId, tomId, atPQEnc) <- connectViaContact tom tPQ qInfo
@@ -1045,7 +1045,7 @@ noMessages_ ingoreQCONT c err = tryGet `shouldReturn` ()
10451045
testRejectContactRequest :: HasCallStack => IO ()
10461046
testRejectContactRequest =
10471047
withAgentClients2 $ \alice bob -> runRight_ $ do
1048-
(_addrConnId, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
1048+
(_addrConnId, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
10491049
aliceId <- A.prepareConnectionToJoin bob 1 True qInfo PQSupportOn
10501050
(sqSecured, Nothing) <- A.joinConnection bob NRMInteractive 1 aliceId True qInfo "bob's connInfo" PQSupportOn SMSubscribe
10511051
liftIO $ sqSecured `shouldBe` False -- joining via contact address connection
@@ -1372,7 +1372,7 @@ testInvitationShortLink viaProxy a b =
13721372
withAgent 3 agentCfg initAgentServers testDB3 $ \c -> do
13731373
let userData = UserLinkData "some user data"
13741374
newLinkData = UserInvLinkData userData
1375-
(bId, (CCLink connReq (Just shortLink), Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing CR.IKUsePQ SMSubscribe
1375+
(bId, (CCLink connReq (Just shortLink), Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing Nothing CR.IKUsePQ SMSubscribe
13761376
(FixedLinkData {linkConnReq = connReq'}, connData') <- runRight $ getConnShortLink b 1 shortLink
13771377
strDecode (strEncode shortLink) `shouldBe` Right shortLink
13781378
connReq' `shouldBe` connReq
@@ -1408,14 +1408,14 @@ testInvitationShortLinkPrev viaProxy sndSecure a b = runRight_ $ do
14081408
let userData = UserLinkData "some user data"
14091409
newLinkData = UserInvLinkData userData
14101410
-- can't create short link with previous version
1411-
(bId, (CCLink connReq Nothing, Nothing)) <- A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing CR.IKPQOn SMSubscribe
1411+
(bId, (CCLink connReq Nothing, Nothing)) <- A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing Nothing CR.IKPQOn SMSubscribe
14121412
testJoinConn_ viaProxy sndSecure a bId b connReq
14131413

14141414
testInvitationShortLinkAsync :: HasCallStack => Bool -> AgentClient -> AgentClient -> IO ()
14151415
testInvitationShortLinkAsync viaProxy a b = do
14161416
let userData = UserLinkData "some user data"
14171417
newLinkData = UserInvLinkData userData
1418-
(bId, (CCLink connReq (Just shortLink), Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing CR.IKUsePQ SMSubscribe
1418+
(bId, (CCLink connReq (Just shortLink), Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing Nothing CR.IKUsePQ SMSubscribe
14191419
(FixedLinkData {linkConnReq = connReq'}, connData') <- runRight $ getConnShortLink b 1 shortLink
14201420
strDecode (strEncode shortLink) `shouldBe` Right shortLink
14211421
connReq' `shouldBe` connReq
@@ -1442,7 +1442,7 @@ testContactShortLink viaProxy a b =
14421442
let userData = UserLinkData "some user data"
14431443
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
14441444
newLinkData = UserContactLinkData userCtData
1445-
(contactId, (CCLink connReq0 (Just shortLink), Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing CR.IKPQOn SMSubscribe
1445+
(contactId, (CCLink connReq0 (Just shortLink), Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing Nothing CR.IKPQOn SMSubscribe
14461446
Right connReq <- pure $ smpDecode (smpEncode connReq0)
14471447
(FixedLinkData {linkConnReq = connReq'}, ContactLinkData _ userCtData') <- runRight $ getConnShortLink b 1 shortLink
14481448
strDecode (strEncode shortLink) `shouldBe` Right shortLink
@@ -1489,7 +1489,7 @@ testContactShortLink viaProxy a b =
14891489
testAddContactShortLink :: HasCallStack => Bool -> AgentClient -> AgentClient -> IO ()
14901490
testAddContactShortLink viaProxy a b =
14911491
withAgent 3 agentCfg initAgentServers testDB3 $ \c -> do
1492-
(contactId, (CCLink connReq0 Nothing, Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMContact Nothing Nothing CR.IKPQOn SMSubscribe
1492+
(contactId, (CCLink connReq0 Nothing, Nothing)) <- runRight $ A.createConnection a NRMInteractive 1 True True SCMContact Nothing Nothing Nothing CR.IKPQOn SMSubscribe
14931493
Right connReq <- pure $ smpDecode (smpEncode connReq0) --
14941494
let userData = UserLinkData "some user data"
14951495
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
@@ -1535,7 +1535,7 @@ testInvitationShortLinkRestart ps = withAgentClients2 $ \a b -> do
15351535
let userData = UserLinkData "some user data"
15361536
newLinkData = UserInvLinkData userData
15371537
(bId, (CCLink connReq (Just shortLink), Nothing)) <- withSmpServer ps $
1538-
runRight $ A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing CR.IKUsePQ SMOnlyCreate
1538+
runRight $ A.createConnection a NRMInteractive 1 True True SCMInvitation (Just newLinkData) Nothing Nothing CR.IKUsePQ SMOnlyCreate
15391539
withSmpServer ps $ do
15401540
runRight_ $ subscribeConnection a bId
15411541
(FixedLinkData {linkConnReq = connReq'}, connData') <- runRight $ getConnShortLink b 1 shortLink
@@ -1549,7 +1549,7 @@ testContactShortLinkRestart ps = withAgentClients2 $ \a b -> do
15491549
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
15501550
newLinkData = UserContactLinkData userCtData
15511551
(contactId, (CCLink connReq0 (Just shortLink), Nothing)) <- withSmpServer ps $
1552-
runRight $ A.createConnection a NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing CR.IKPQOn SMOnlyCreate
1552+
runRight $ A.createConnection a NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing Nothing CR.IKPQOn SMOnlyCreate
15531553
Right connReq <- pure $ smpDecode (smpEncode connReq0)
15541554
let updatedData = UserLinkData "updated user data"
15551555
updatedCtData = UserContactData {direct = False, owners = [], relays = [relayLink1, relayLink2], userData = updatedData}
@@ -1573,7 +1573,7 @@ testAddContactShortLinkRestart ps = withAgentClients2 $ \a b -> do
15731573
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
15741574
newLinkData = UserContactLinkData userCtData
15751575
((contactId, (CCLink connReq0 Nothing, Nothing)), shortLink) <- withSmpServer ps $ runRight $ do
1576-
r@(contactId, _) <- A.createConnection a NRMInteractive 1 True True SCMContact Nothing Nothing CR.IKPQOn SMOnlyCreate
1576+
r@(contactId, _) <- A.createConnection a NRMInteractive 1 True True SCMContact Nothing Nothing Nothing CR.IKPQOn SMOnlyCreate
15771577
(r,) <$> setConnShortLink a contactId SCMContact newLinkData Nothing
15781578
Right connReq <- pure $ smpDecode (smpEncode connReq0)
15791579
let updatedData = UserLinkData "updated user data"
@@ -1595,7 +1595,7 @@ testAddContactShortLinkRestart ps = withAgentClients2 $ \a b -> do
15951595
testOldContactQueueShortLink :: HasCallStack => (ASrvTransport, AStoreType) -> IO ()
15961596
testOldContactQueueShortLink ps@(_, msType) = withAgentClients2 $ \a b -> do
15971597
(contactId, (CCLink connReq Nothing, Nothing)) <- withSmpServer ps $ runRight $
1598-
A.createConnection a NRMInteractive 1 True True SCMContact Nothing Nothing CR.IKPQOn SMOnlyCreate
1598+
A.createConnection a NRMInteractive 1 True True SCMContact Nothing Nothing Nothing CR.IKPQOn SMOnlyCreate
15991599
-- make it an "old" queue
16001600
let updateStoreLog f = replaceSubstringInFile f " queue_mode=C" ""
16011601
#if defined(dbServerPostgres)
@@ -2402,7 +2402,7 @@ makeConnectionForUsers = makeConnectionForUsers_ PQSupportOn True
24022402

24032403
makeConnectionForUsers_ :: HasCallStack => PQSupport -> SndQueueSecured -> AgentClient -> UserId -> AgentClient -> UserId -> ExceptT AgentErrorType IO (ConnId, ConnId)
24042404
makeConnectionForUsers_ pqSupport sqSecured alice aliceUserId bob bobUserId = do
2405-
(bobId, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive aliceUserId True True SCMInvitation Nothing Nothing (IKLinkPQ pqSupport) SMSubscribe
2405+
(bobId, (CCLink qInfo Nothing, Nothing)) <- A.createConnection alice NRMInteractive aliceUserId True True SCMInvitation Nothing Nothing Nothing (IKLinkPQ pqSupport) SMSubscribe
24062406
aliceId <- A.prepareConnectionToJoin bob bobUserId True qInfo pqSupport
24072407
(sqSecured', Nothing) <- A.joinConnection bob NRMInteractive bobUserId aliceId True qInfo "bob's connInfo" pqSupport SMSubscribe
24082408
liftIO $ sqSecured' `shouldBe` sqSecured
@@ -2735,7 +2735,7 @@ testSetConnShortLinkAsync ps = withAgentClients2 $ \alice bob ->
27352735
let userData = UserLinkData "test user data"
27362736
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
27372737
newLinkData = UserContactLinkData userCtData
2738-
(cId, (CCLink qInfo (Just shortLink), _)) <- A.createConnection alice NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing IKPQOn SMSubscribe
2738+
(cId, (CCLink qInfo (Just shortLink), _)) <- A.createConnection alice NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing Nothing IKPQOn SMSubscribe
27392739
-- verify initial link data
27402740
(_, ContactLinkData _ userCtData') <- getConnShortLink bob 1 shortLink
27412741
liftIO $ userCtData' `shouldBe` userCtData
@@ -2767,7 +2767,7 @@ testGetConnShortLinkAsync ps = withAgentClients2 $ \alice bob ->
27672767
let userData = UserLinkData "test user data"
27682768
userCtData = UserContactData {direct = True, owners = [], relays = [], userData}
27692769
newLinkData = UserContactLinkData userCtData
2770-
(_, (CCLink qInfo (Just shortLink), _)) <- A.createConnection alice NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing IKPQOn SMSubscribe
2770+
(_, (CCLink qInfo (Just shortLink), _)) <- A.createConnection alice NRMInteractive 1 True True SCMContact (Just newLinkData) Nothing Nothing IKPQOn SMSubscribe
27712771
-- get link data async - creates new connection for bob
27722772
newId <- getConnShortLinkAsync bob 1 "1" Nothing shortLink
27732773
("1", newId', LDATA FixedLinkData {linkConnReq = qInfo'} (ContactLinkData _ userCtData')) <- get bob
@@ -4048,7 +4048,7 @@ testClientNotice :: HasCallStack => (ASrvTransport, AStoreType) -> IO ()
40484048
testClientNotice ps = do
40494049
withAgent 1 agentCfg initAgentServers testDB $ \c -> do
40504050
(cId, _) <- withSmpServerStoreLogOn ps testPort $ \_ -> runRight $
4051-
A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
4051+
A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
40524052
("", "", DOWN _ [_]) <- nGet c
40534053

40544054
addNotice c cId $ Just 1
@@ -4057,7 +4057,7 @@ testClientNotice ps = do
40574057
subscribedWithErrors c 1
40584058
testNotice c True
40594059
threadDelay 1000000
4060-
runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
4060+
runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
40614061
("", "", DOWN _ [_]) <- nGet c
40624062

40634063
addNotice c cId' $ Just 1
@@ -4068,7 +4068,7 @@ testClientNotice ps = do
40684068
threadDelay 1000000
40694069
testNotice c True
40704070
threadDelay 1000000
4071-
runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
4071+
runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
40724072

40734073
addNotice c cId'' $ Just 1
40744074

@@ -4080,7 +4080,7 @@ testClientNotice ps = do
40804080
threadDelay 2000000
40814081
testNotice c True
40824082
threadDelay 1000000
4083-
runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
4083+
runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
40844084
("", "", DOWN _ [_]) <- nGet c
40854085

40864086
addNotice c cId3 Nothing
@@ -4095,7 +4095,7 @@ testClientNotice ps = do
40954095
withSmpServerStoreLogOn ps testPort $ \_ -> do
40964096
runRight_ $ subscribeAllConnections c False Nothing
40974097
subscribedWithErrors c 4
4098-
void $ runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
4098+
void $ runRight $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
40994099
where
41004100
addNotice c cId ttl = logNotice c cId $ Just ClientNotice {ttl}
41014101
removeNotice c cId = logNotice c cId Nothing
@@ -4111,7 +4111,7 @@ testClientNotice ps = do
41114111
r -> expectationFailure $ "unexpected event: " <> show r
41124112
testNotice :: HasCallStack => AgentClient -> Bool -> IO ()
41134113
testNotice c willExpire = do
4114-
NOTICE "localhost" False expiresAt_ <- runLeft $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing IKPQOn SMSubscribe
4114+
NOTICE "localhost" False expiresAt_ <- runLeft $ A.createConnection c NRMInteractive 1 True True SCMContact Nothing Nothing Nothing IKPQOn SMSubscribe
41154115
isJust expiresAt_ `shouldBe` willExpire
41164116

41174117
noNetworkDelay :: AgentClient -> IO ()

0 commit comments

Comments
 (0)