Skip to content

Commit 8fea152

Browse files
authored
smp server: remove dependency of message size on the version (#1627)
1 parent 42a2c1f commit 8fea152

File tree

5 files changed

+16
-23
lines changed

5 files changed

+16
-23
lines changed

src/Simplex/Messaging/Protocol.hs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,13 @@ currentSMPClientVersion = VersionSMPC 4
292292
supportedSMPClientVRange :: VersionRangeSMPC
293293
supportedSMPClientVRange = mkVersionRange initialSMPClientVersion currentSMPClientVersion
294294

295-
-- TODO v6.0 remove dependency on version
296-
maxMessageLength :: VersionSMP -> Int
297-
maxMessageLength v
298-
| v >= encryptedBlockSMPVersion = 16048 -- max 16048
299-
| v >= sendingProxySMPVersion = 16064 -- max 16067
300-
| otherwise = 16088 -- 16048 - always use this size to determine allowed ranges
295+
maxMessageLength :: Int
296+
maxMessageLength = 16048
301297

302298
paddedProxiedTLength :: Int
303299
paddedProxiedTLength = 16226 -- 16225 .. 16227
304300

305-
-- TODO v7.0 change to 16048
306-
type MaxMessageLen = 16088
301+
type MaxMessageLen = 16048
307302

308303
-- 16 extra bytes: 8 for timestamp and 8 for flags (7 flags and the space, only 1 flag is currently used)
309304
type MaxRcvMessageLen = MaxMessageLen + 16 -- 16104, the padded size is 16106
@@ -1477,7 +1472,7 @@ data ErrorType
14771472
STORE {storeErr :: Text}
14781473
| -- | ACK command is sent without message to be acknowledged
14791474
NO_MSG
1480-
| -- | sent message is too large (> maxMessageLength = 16088 bytes)
1475+
| -- | sent message is too large (> maxMessageLength = 16048 bytes)
14811476
LARGE_MSG
14821477
| -- | relay public key is expired
14831478
EXPIRED

src/Simplex/Messaging/Server.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,10 @@ client
13491349
ms
13501350
clnt@Client {clientId, ntfSubscriptions, ntfServiceSubscribed, serviceSubsCount = _todo', ntfServiceSubsCount, rcvQ, sndQ, clientTHParams = thParams'@THandleParams {sessionId}, procThreads} = do
13511351
labelMyThread . B.unpack $ "client $" <> encode sessionId <> " commands"
1352-
let THandleParams {thVersion} = thParams'
1353-
clntServiceId = (\THClientService {serviceId} -> serviceId) <$> (peerClientService =<< thAuth thParams')
1352+
let clntServiceId = (\THClientService {serviceId} -> serviceId) <$> (peerClientService =<< thAuth thParams')
13541353
process t acc@(rs, msgs) =
13551354
(maybe acc (\(!r, !msg_) -> (r : rs, maybe msgs (: msgs) msg_)))
1356-
<$> processCommand clntServiceId thVersion t
1355+
<$> processCommand clntServiceId t
13571356
forever $
13581357
atomically (readTBQueue rcvQ)
13591358
>>= foldrM process ([], [])
@@ -1439,8 +1438,8 @@ client
14391438
mkIncProxyStats ps psOwn own sel = do
14401439
incStat $ sel ps
14411440
when own $ incStat $ sel psOwn
1442-
processCommand :: Maybe ServiceId -> VersionSMP -> VerifiedTransmission s -> M s (Maybe ResponseAndMessage)
1443-
processCommand clntServiceId clntVersion (q_, (corrId, entId, cmd)) = case cmd of
1441+
processCommand :: Maybe ServiceId -> VerifiedTransmission s -> M s (Maybe ResponseAndMessage)
1442+
processCommand clntServiceId (q_, (corrId, entId, cmd)) = case cmd of
14441443
Cmd SProxiedClient command -> processProxiedCmd (corrId, entId, command)
14451444
Cmd SSender command -> case command of
14461445
SKEY k -> withQueue $ \q qr -> checkMode QMMessaging qr $ secureQueue_ q k
@@ -1829,7 +1828,7 @@ client
18291828

18301829
sendMessage :: MsgFlags -> MsgBody -> StoreQueue s -> QueueRec -> M s (Transmission BrokerMsg)
18311830
sendMessage msgFlags msgBody q qr
1832-
| B.length msgBody > maxMessageLength clntVersion = do
1831+
| B.length msgBody > maxMessageLength = do
18331832
stats <- asks serverStats
18341833
incStat $ msgSentLarge stats
18351834
pure $ err LARGE_MSG
@@ -1982,7 +1981,7 @@ client
19821981
-- rejectOrVerify filters allowed commands, no need to repeat it here.
19831982
-- INTERNAL is used because processCommand never returns Nothing for sender commands (could be extracted for better types).
19841983
-- `fst` removes empty message that is only returned for `SUB` command
1985-
Right t''@(_, (corrId', entId', _)) -> maybe (corrId', entId', ERR INTERNAL) fst <$> lift (processCommand Nothing fwdVersion t'')
1984+
Right t''@(_, (corrId', entId', _)) -> maybe (corrId', entId', ERR INTERNAL) fst <$> lift (processCommand Nothing t'')
19861985
-- encode response
19871986
r' <- case batchTransmissions clntTHParams [Right (Nothing, encodeTransmission clntTHParams r)] of
19881987
[] -> throwE INTERNAL -- at least 1 item is guaranteed from NonEmpty/Right

tests/CoreTests/BatchingTests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ randomMSG = do
389389
corrId <- atomically $ C.randomBytes 24 g
390390
rId <- atomically $ C.randomBytes 24 g
391391
msgId <- atomically $ C.randomBytes 24 g
392-
msg <- atomically $ C.randomBytes (maxMessageLength currentClientSMPRelayVersion) g
392+
msg <- atomically $ C.randomBytes maxMessageLength g
393393
pure (CorrId corrId, EntityId rId, MSG RcvMessage {msgId, msgBody = EncRcvMsgBody msg})
394394

395395
randomSENDv6 :: ByteString -> Int -> IO (Either TransportError (Maybe TAuthorizations, ByteString))

tests/SMPProxyTests.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ smpProxyTests = do
6969
let srv1 = SMPServer testHost testPort testKeyHash
7070
srv2 = SMPServer testHost2 testPort2 testKeyHash
7171
describe "client API" $ do
72-
let maxLen = maxMessageLength encryptedBlockSMPVersion
7372
describe "one server" $ do
7473
it "deliver via proxy" . oneServer $ do
7574
deliverMessageViaProxy srv1 srv1 C.SEd448 "hello 1" "hello 2"
@@ -78,7 +77,7 @@ smpProxyTests = do
7877
relayServ = srv2
7978
(msg1, msg2) <- runIO $ do
8079
g <- C.newRandom
81-
atomically $ (,) <$> C.randomBytes maxLen g <*> C.randomBytes maxLen g
80+
atomically $ (,) <$> C.randomBytes maxMessageLength g <*> C.randomBytes maxMessageLength g
8281
it "deliver via proxy" . twoServersFirstProxy $
8382
deliverMessageViaProxy proxyServ relayServ C.SEd448 "hello 1" "hello 2"
8483
it "max message size, Ed448 keys" . twoServersFirstProxy $

tests/ServerTests.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ testCreateSecure =
232232
Resp "dabc" _ err5 <- sendRecv s ("", "dabc", sId, _SEND "hello")
233233
(err5, ERR AUTH) #== "rejects unsigned SEND"
234234

235-
let maxAllowedMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion) '-'
235+
let maxAllowedMessage = B.replicate maxMessageLength '-'
236236
Resp "bcda" _ OK <- signSendRecv s sKey ("bcda", sId, _SEND maxAllowedMessage)
237237
Resp "" _ (Msg mId3 msg3) <- tGet1 r
238238
(dec mId3 msg3, Right maxAllowedMessage) #== "delivers message of max size"
239239

240-
let biggerMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion + 1) '-'
240+
let biggerMessage = B.replicate (maxMessageLength + 1) '-'
241241
Resp "bcda" _ (ERR LARGE_MSG) <- signSendRecv s sKey ("bcda", sId, _SEND biggerMessage)
242242
pure ()
243243

@@ -279,12 +279,12 @@ testCreateSndSecure =
279279
Resp "dabc" _ err5 <- sendRecv s ("", "dabc", sId, _SEND "hello")
280280
(err5, ERR AUTH) #== "rejects unsigned SEND"
281281

282-
let maxAllowedMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion) '-'
282+
let maxAllowedMessage = B.replicate maxMessageLength '-'
283283
Resp "bcda" _ OK <- signSendRecv s sKey ("bcda", sId, _SEND maxAllowedMessage)
284284
Resp "" _ (Msg mId3 msg3) <- tGet1 r
285285
(dec mId3 msg3, Right maxAllowedMessage) #== "delivers message of max size"
286286

287-
let biggerMessage = B.replicate (maxMessageLength currentClientSMPRelayVersion + 1) '-'
287+
let biggerMessage = B.replicate (maxMessageLength + 1) '-'
288288
Resp "bcda" _ (ERR LARGE_MSG) <- signSendRecv s sKey ("bcda", sId, _SEND biggerMessage)
289289
pure ()
290290

0 commit comments

Comments
 (0)