From a8c3f5c6b5b33332190fb4ecb3394d955c20fc23 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 25 Aug 2022 21:42:58 +0100 Subject: [PATCH] rename columns --- src/Simplex/Messaging/Agent.hs | 22 +++++++++------- src/Simplex/Messaging/Agent/Client.hs | 8 +++--- src/Simplex/Messaging/Agent/Store.hs | 6 +++-- src/Simplex/Messaging/Agent/Store/SQLite.hs | 26 +++++++++---------- .../Migrations/M20220822_queue_rotation.hs | 8 +++--- .../Store/SQLite/Migrations/agent_schema.sql | 4 +-- tests/AgentTests/SQLiteTests.hs | 6 +++-- 7 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/Simplex/Messaging/Agent.hs b/src/Simplex/Messaging/Agent.hs index e74d7b57d..74228e427 100644 --- a/src/Simplex/Messaging/Agent.hs +++ b/src/Simplex/Messaging/Agent.hs @@ -305,7 +305,7 @@ newConn :: AgentMonad m => AgentClient -> ConnId -> Bool -> SConnectionMode c -> newConn c connId enableNtfs cMode = do srv <- getSMPServer c clientVRange <- asks $ smpClientVRange . config - (rq, qUri) <- newRcvQueue c srv clientVRange False + (rq, qUri) <- newRcvQueue c srv clientVRange True g <- asks idsDrg connAgentVersion <- asks $ maxVersion . smpAgentVRange . config let cData = ConnData {connId, connAgentVersion, enableNtfs, duplexHandshake = Nothing} -- connection mode is determined by the accepting agent @@ -335,7 +335,7 @@ joinConn c connId enableNtfs (CRInvitationUri (ConnReqUriData _ agentVRange (qUr (pk1, pk2, e2eSndParams) <- liftIO . CR.generateE2EParams $ version e2eRcvParams (_, rcDHRs) <- liftIO C.generateKeyPair' let rc = CR.initSndRatchet rcDHRr rcDHRs $ CR.x3dhSnd pk1 pk2 e2eRcvParams - sq <- newSndQueue qInfo + sq <- newSndQueue qInfo True g <- asks idsDrg let duplexHS = connAgentVersion /= 1 cData = ConnData {connId, connAgentVersion, enableNtfs, duplexHandshake = Just duplexHS} @@ -368,7 +368,7 @@ joinConn c connId enableNtfs (CRContactUri (ConnReqUriData _ agentVRange (qUri : createReplyQueue :: AgentMonad m => AgentClient -> ConnData -> SndQueue -> m SMPQueueInfo createReplyQueue c ConnData {connId, enableNtfs} SndQueue {smpClientVersion} = do srv <- getSMPServer c - (rq, qUri) <- newRcvQueue c srv (versionToRange smpClientVersion) False + (rq, qUri) <- newRcvQueue c srv (versionToRange smpClientVersion) True let qInfo = toVersionT qUri smpClientVersion addSubscription c rq connId withStore c $ \db -> upgradeSndConnToDuplex db connId rq @@ -459,7 +459,7 @@ createNextRcvQueue c cData rq@RcvQueue {server, sndId} sq = do pure SMPQueueUri {clientVRange, queueAddress} _ -> do srv <- getSMPServer c - (rq', qUri) <- newRcvQueue c srv clientVRange True + (rq', qUri) <- newRcvQueue c srv clientVRange False withStore' c $ \db -> dbCreateNextRcvQueue db rq rq' pure qUri void $ enqueueMessage c cData sq SMP.noMsgFlags QNEW {currentAddress = (server, sndId), nextQueueUri} @@ -1310,7 +1310,7 @@ processSMPTransmission c@AgentClient {smpClients, subQ} (srv, v, sessId, rId, cm clientVRange <- asks $ smpClientVRange . config case (nextQUri `compatibleVersion` clientVRange) of Just qInfo@(Compatible nextQInfo) -> do - sq'@SndQueue {sndPublicKey, e2ePubKey} <- newSndQueue qInfo + sq'@SndQueue {sndPublicKey, e2ePubKey} <- newSndQueue qInfo False withStore' c $ \db -> dbCreateNextSndQueue db sq sq' case (sndPublicKey, e2ePubKey) of (Just nextSenderKey, Just dhPublicKey) -> do @@ -1409,7 +1409,7 @@ connectReplyQueues c cData@ConnData {connId} ownConnInfo (qInfo :| _) = do case qInfo `proveCompatible` clientVRange of Nothing -> throwError $ AGENT A_VERSION Just qInfo' -> do - sq <- newSndQueue qInfo' + sq <- newSndQueue qInfo' True withStore c $ \db -> upgradeRcvConnToDuplex db connId sq enqueueConfirmation c cData sq ownConnInfo Nothing @@ -1468,17 +1468,18 @@ agentRatchetDecrypt db connId encAgentMsg = do liftIO $ updateRatchet db connId rc' skippedDiff liftEither $ first (SEAgentError . cryptoError) agentMsgBody_ -newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> m SndQueue -newSndQueue qInfo = +newSndQueue :: (MonadUnliftIO m, MonadReader Env m) => Compatible SMPQueueInfo -> Bool -> m SndQueue +newSndQueue qInfo current = asks (cmdSignAlg . config) >>= \case - C.SignAlg a -> newSndQueue_ a qInfo + C.SignAlg a -> newSndQueue_ a qInfo current newSndQueue_ :: (C.SignatureAlgorithm a, C.AlgorithmI a, MonadUnliftIO m) => C.SAlgorithm a -> Compatible SMPQueueInfo -> + Bool -> m SndQueue -newSndQueue_ a (Compatible (SMPQueueInfo smpClientVersion SMPQueueAddress {smpServer, senderId, dhPublicKey = rcvE2ePubDhKey})) = do +newSndQueue_ a (Compatible (SMPQueueInfo smpClientVersion SMPQueueAddress {smpServer, senderId, dhPublicKey = rcvE2ePubDhKey})) current = do -- this function assumes clientVersion is compatible - it was tested before (sndPublicKey, sndPrivateKey) <- liftIO $ C.generateSignatureKeyPair a (e2ePubKey, e2ePrivKey) <- liftIO C.generateKeyPair' @@ -1492,6 +1493,7 @@ newSndQueue_ a (Compatible (SMPQueueInfo smpClientVersion SMPQueueAddress {smpSe e2eDhSecret = C.dh' rcvE2ePubDhKey e2ePrivKey, e2ePubKey = Just e2ePubKey, status = New, + currSndQueue = current, dbNextSndQueueId = Nothing, sndQueueAction = Nothing, smpClientVersion, diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 9043bcb40..024ee9218 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -472,9 +472,9 @@ protocolClientError protocolError_ = \case e@PCEIOError {} -> INTERNAL $ show e newRcvQueue :: AgentMonad m => AgentClient -> SMPServer -> VersionRange -> Bool -> m (RcvQueue, SMPQueueUri) -newRcvQueue c srv vRange next = +newRcvQueue c srv vRange current = asks (cmdSignAlg . config) >>= \case - C.SignAlg a -> newRcvQueue_ a c srv vRange next + C.SignAlg a -> newRcvQueue_ a c srv vRange current newRcvQueue_ :: (C.SignatureAlgorithm a, C.AlgorithmI a, AgentMonad m) => @@ -484,7 +484,7 @@ newRcvQueue_ :: VersionRange -> Bool -> m (RcvQueue, SMPQueueUri) -newRcvQueue_ a c srv vRange next = do +newRcvQueue_ a c srv vRange current = do (recipientKey, rcvPrivateKey) <- liftIO $ C.generateSignatureKeyPair a (dhKey, privDhKey) <- liftIO C.generateKeyPair' (e2eDhKey, e2ePrivKey) <- liftIO C.generateKeyPair' @@ -505,7 +505,7 @@ newRcvQueue_ a c srv vRange next = do sndPublicKey = Nothing, status = New, rcvQueueAction = Nothing, - nextRcvQueue = next, + currRcvQueue = current, dbNextRcvQueueId = Nothing, clientNtfCreds = Nothing, smpClientVersion = maxVersion vRange, diff --git a/src/Simplex/Messaging/Agent/Store.hs b/src/Simplex/Messaging/Agent/Store.hs index ad78e5cb3..cf5e32997 100644 --- a/src/Simplex/Messaging/Agent/Store.hs +++ b/src/Simplex/Messaging/Agent/Store.hs @@ -61,8 +61,8 @@ data RcvQueue = RcvQueue status :: QueueStatus, -- | action to perform, to be done on connection subscription, if it fails and not reset rcvQueueAction :: Maybe (RcvQueueAction, UTCTime), - -- | True if this is the queue the connection is switching to, rather than the current queue - nextRcvQueue :: Bool, + -- | True for the current receive queue + currRcvQueue :: Bool, -- | database ID of the new queue created for this queue to switch to (queue rotation) dbNextRcvQueueId :: Maybe Int64, -- | credentials used in context of notifications @@ -103,6 +103,8 @@ data SndQueue = SndQueue status :: QueueStatus, -- | action to perform, to be done on connection subscription, if it fails and not reset sndQueueAction :: Maybe (SndQueueAction, UTCTime), + -- | True for the current send queue + currSndQueue :: Bool, -- | database ID of the new queue created for this queue to switch to (queue rotation) dbNextSndQueueId :: Maybe Int64, -- | SMP client version diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index fb42d77bd..302268eaf 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -394,14 +394,14 @@ getNextRcvQueue db = \case [sql| SELECT q.host, q.port, s.key_hash, q.rcv_id, q.rcv_private_key, q.rcv_dh_secret, q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.snd_key, q.status, - q.rcv_queue_action, q.rcv_queue_action_ts, q.next_rcv_queue, q.next_rcv_queue_id, + q.rcv_queue_action, q.rcv_queue_action_ts, q.curr_rcv_queue, q.next_rcv_queue_id, q.ntf_public_key, q.ntf_private_key, q.ntf_id, q.rcv_ntf_dh_secret, q.smp_client_version, q.created_at, q.updated_at FROM rcv_queues q INNER JOIN servers s ON q.host = s.host AND q.port = s.port - WHERE q.rcv_queue_id = ? AND q.next_rcv_queue = ? + WHERE q.rcv_queue_id = ? AND q.curr_rcv_queue = ? |] - (rqId, True) + (rqId, False) _ -> pure Nothing getNextSndQueue :: DB.Connection -> Maybe Int64 -> IO (Maybe SndQueue) @@ -1203,12 +1203,12 @@ type ServerRow = (NonEmpty TransportHost, String, C.KeyHash) type NtfCredsRow = (Maybe SMP.NtfPublicVerifyKey, Maybe SMP.NtfPrivateSignKey, Maybe SMP.NotifierId, Maybe RcvNtfDhSecret) toRcvQueue :: RcvQueueRow -> RcvQueue -toRcvQueue (srvRow :. (rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, sndPublicKey, status) :. (rqAction_, rqActionTs_, nextRcvQueue, dbNextRcvQueueId) :. ntfCredsRow :. (smpClientVersion_, createdAt, updatedAt)) = +toRcvQueue (srvRow :. (rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, sndPublicKey, status) :. (rqAction_, rqActionTs_, currRcvQueue, dbNextRcvQueueId) :. ntfCredsRow :. (smpClientVersion_, createdAt, updatedAt)) = let server = toSMPServer srvRow smpClientVersion = fromMaybe 1 smpClientVersion_ rcvQueueAction = (,) <$> rqAction_ <*> rqActionTs_ clientNtfCreds = toNtfCreds ntfCredsRow - in RcvQueue {server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, sndPublicKey, status, rcvQueueAction, nextRcvQueue, dbNextRcvQueueId, smpClientVersion, clientNtfCreds, createdAt, updatedAt} + in RcvQueue {server, rcvId, rcvPrivateKey, rcvDhSecret, e2ePrivKey, e2eDhSecret, sndId, sndPublicKey, status, rcvQueueAction, currRcvQueue, dbNextRcvQueueId, smpClientVersion, clientNtfCreds, createdAt, updatedAt} toSMPServer :: ServerRow -> SMPServer toSMPServer (host, port, keyHash) = SMPServer host port keyHash @@ -1225,14 +1225,14 @@ getRcvQueueByConnId_ dbConn connId = [sql| SELECT q.host, q.port, s.key_hash, q.rcv_id, q.rcv_private_key, q.rcv_dh_secret, q.e2e_priv_key, q.e2e_dh_secret, q.snd_id, q.snd_key, q.status, - q.rcv_queue_action, q.rcv_queue_action_ts, q.next_rcv_queue, q.next_rcv_queue_id, + q.rcv_queue_action, q.rcv_queue_action_ts, q.curr_rcv_queue, q.next_rcv_queue_id, q.ntf_public_key, q.ntf_private_key, q.ntf_id, q.rcv_ntf_dh_secret, q.smp_client_version, q.created_at, q.updated_at FROM rcv_queues q INNER JOIN servers s ON q.host = s.host AND q.port = s.port - WHERE q.conn_id = ? AND q.next_rcv_queue = ? + WHERE q.conn_id = ? AND q.curr_rcv_queue = ? |] - (connId, False) + (connId, True) getSndQueueByConnId_ :: DB.Connection -> ConnId -> IO (Maybe SndQueue) getSndQueueByConnId_ dbConn connId = @@ -1242,18 +1242,18 @@ getSndQueueByConnId_ dbConn connId = [sql| SELECT q.host, q.port, s.key_hash, q.snd_id, q.snd_public_key, q.snd_private_key, q.e2e_pub_key, q.e2e_dh_secret, q.status, - q.snd_queue_action, q.snd_queue_action_ts, q.next_snd_queue_id, + q.snd_queue_action, q.snd_queue_action_ts, q.curr_snd_queue, q.next_snd_queue_id, q.smp_client_version, q.created_at, q.updated_at FROM snd_queues q INNER JOIN servers s ON q.host = s.host AND q.port = s.port - WHERE q.conn_id = ? AND q.next_snd_queue = ? + WHERE q.conn_id = ? AND q.curr_snd_queue = ? |] - (connId, False) + (connId, True) where - sndQueue (srvRow :. (sndId, sndPublicKey, sndPrivateKey, e2ePubKey, e2eDhSecret, status, sqAction_, sqActionTs_, dbNextSndQueueId) :. (smpClientVersion, createdAt, updatedAt)) = + sndQueue (srvRow :. (sndId, sndPublicKey, sndPrivateKey, e2ePubKey, e2eDhSecret, status, sqAction_, sqActionTs_, currSndQueue, dbNextSndQueueId) :. (smpClientVersion, createdAt, updatedAt)) = let server = toSMPServer srvRow sndQueueAction = (,) <$> sqAction_ <*> sqActionTs_ - in SndQueue {server, sndId, sndPublicKey, sndPrivateKey, e2ePubKey, e2eDhSecret, status, sndQueueAction, dbNextSndQueueId, smpClientVersion, createdAt, updatedAt} + in SndQueue {server, sndId, sndPublicKey, sndPrivateKey, e2ePubKey, e2eDhSecret, status, sndQueueAction, currSndQueue, dbNextSndQueueId, smpClientVersion, createdAt, updatedAt} -- * updateRcvIds helpers diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220822_queue_rotation.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220822_queue_rotation.hs index 2853d928c..893806a37 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220822_queue_rotation.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220822_queue_rotation.hs @@ -14,8 +14,8 @@ ALTER TABLE rcv_queues ADD COLUMN rcv_queue_id INTEGER NULL; ALTER TABLE rcv_queues ADD COLUMN rcv_queue_action TEXT NULL; ALTER TABLE rcv_queues ADD COLUMN rcv_queue_action_ts TEXT NULL; -ALTER TABLE rcv_queues ADD COLUMN next_rcv_queue INTEGER DEFAULT 0 CHECK (next_rcv_queue NOT NULL); -UPDATE rcv_queues SET next_rcv_queue = 0; +ALTER TABLE rcv_queues ADD COLUMN curr_rcv_queue INTEGER DEFAULT 1 CHECK (curr_rcv_queue NOT NULL); +UPDATE rcv_queues SET curr_rcv_queue = 1; ALTER TABLE rcv_queues ADD COLUMN next_rcv_queue_id INTEGER NULL; -- REFERENCES rcv_queues (rcv_queue_id) ON DELETE SET NULL; @@ -37,8 +37,8 @@ ALTER TABLE snd_queues ADD COLUMN snd_queue_id INTEGER NULL; ALTER TABLE snd_queues ADD COLUMN snd_queue_action TEXT NULL; ALTER TABLE snd_queues ADD COLUMN snd_queue_action_ts TEXT NULL; -ALTER TABLE snd_queues ADD COLUMN next_snd_queue INTEGER DEFAULT 0 CHECK (next_snd_queue NOT NULL); -UPDATE snd_queues SET next_snd_queue = 0; +ALTER TABLE snd_queues ADD COLUMN curr_snd_queue INTEGER DEFAULT 1 CHECK (curr_snd_queue NOT NULL); +UPDATE snd_queues SET curr_snd_queue = 1; ALTER TABLE snd_queues ADD COLUMN next_snd_queue_id INTEGER NULL; -- REFERENCES snd_queues (snd_queue_id) ON DELETE SET NULL; diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index 7bcb3985e..ffbf88e7f 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -44,7 +44,7 @@ CREATE TABLE rcv_queues( rcv_queue_id INTEGER NULL, rcv_queue_action TEXT NULL, rcv_queue_action_ts TEXT NULL, - next_rcv_queue INTEGER DEFAULT 0 CHECK(next_rcv_queue NOT NULL), + curr_rcv_queue INTEGER DEFAULT 1 CHECK(curr_rcv_queue NOT NULL), next_rcv_queue_id INTEGER NULL, created_at TEXT CHECK(created_at NOT NULL), updated_at TEXT CHECK(updated_at NOT NULL), @@ -68,7 +68,7 @@ CREATE TABLE snd_queues( snd_queue_id INTEGER NULL, snd_queue_action TEXT NULL, snd_queue_action_ts TEXT NULL, - next_snd_queue INTEGER DEFAULT 0 CHECK(next_snd_queue NOT NULL), + curr_snd_queue INTEGER DEFAULT 1 CHECK(curr_snd_queue NOT NULL), next_snd_queue_id INTEGER NULL, created_at TEXT CHECK(created_at NOT NULL), updated_at TEXT CHECK(updated_at NOT NULL), diff --git a/tests/AgentTests/SQLiteTests.hs b/tests/AgentTests/SQLiteTests.hs index cd6b97255..4a6566482 100644 --- a/tests/AgentTests/SQLiteTests.hs +++ b/tests/AgentTests/SQLiteTests.hs @@ -166,7 +166,7 @@ rcvQueue1 = sndId = "2345", sndPublicKey = Nothing, status = New, - nextRcvQueue = False, + currRcvQueue = True, dbNextRcvQueueId = Nothing, rcvQueueAction = Nothing, clientNtfCreds = Nothing, @@ -185,6 +185,7 @@ sndQueue1 = e2ePubKey = Nothing, e2eDhSecret = testDhSecret, status = New, + currSndQueue = True, dbNextSndQueueId = Nothing, sndQueueAction = Nothing, smpClientVersion = 1, @@ -322,6 +323,7 @@ testUpgradeRcvConnToDuplex = e2ePubKey = Nothing, e2eDhSecret = testDhSecret, status = New, + currSndQueue = True, dbNextSndQueueId = Nothing, sndQueueAction = Nothing, smpClientVersion = 1, @@ -350,7 +352,7 @@ testUpgradeSndConnToDuplex = sndId = "4567", sndPublicKey = Nothing, status = New, - nextRcvQueue = False, + currRcvQueue = False, dbNextRcvQueueId = Nothing, rcvQueueAction = Nothing, clientNtfCreds = Nothing,