Skip to content

Commit

Permalink
tx-submission: use SizeInBytes
Browse files Browse the repository at this point in the history
Deprecated TxSizeInBytes.
  • Loading branch information
coot committed May 6, 2024
1 parent bea4653 commit 8d42239
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 29 deletions.
2 changes: 2 additions & 0 deletions ouroboros-network-protocols/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Refactored CBOR mini-protocols codecs to a more modular structure
* Added `deepseq` dependency and implemented `NFData` for `testlib` types.
* Added miniprotocols codec benchmarks
* Use `SizeInBytes` newtype instead of the `TxSizeInBytes` type aliase.
`TxSizeInBytes` is now deprecated.

## 0.8.0.0 -- 2024-02-21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ data ClientStIdle txid tx m a = ClientStIdle {
}

data ClientStTxIds blocking txid tx m a where
SendMsgReplyTxIds :: BlockingReplyList blocking (txid, TxSizeInBytes)
SendMsgReplyTxIds :: BlockingReplyList blocking (txid, SizeInBytes)
-> ClientStIdle txid tx m a
-> ClientStTxIds blocking txid tx m a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ encodeTxSubmission2 encodeTxId encodeTx = encode
CBOR.encodeListLen 2
<> CBOR.encodeWord 1
<> CBOR.encodeListLenIndef
<> foldr (\(txid, sz) r -> CBOR.encodeListLen 2
<> foldr (\(txid, SizeInBytes sz) r ->
CBOR.encodeListLen 2
<> encodeTxId txid
<> CBOR.encodeWord32 sz
<> r)
CBOR.encodeBreak
txids'
where
txids' :: [(txid, TxSizeInBytes)]
txids' :: [(txid, SizeInBytes)]
txids' = case txids of
BlockingReply xs -> NonEmpty.toList xs
NonBlockingReply xs -> xs
Expand Down Expand Up @@ -168,9 +169,10 @@ decodeTxSubmission2 decodeTxId decodeTx = decode
blocking <- CBOR.decodeBool
ackNo <- CBOR.decodeWord16
reqNo <- CBOR.decodeWord16
return $! case blocking of
True -> SomeMessage (MsgRequestTxIds TokBlocking ackNo reqNo)
False -> SomeMessage (MsgRequestTxIds TokNonBlocking ackNo reqNo)
return $!
if blocking
then SomeMessage (MsgRequestTxIds TokBlocking ackNo reqNo)
else SomeMessage (MsgRequestTxIds TokNonBlocking ackNo reqNo)

(ClientAgency (TokTxIds b), 2, 1) -> do
CBOR.decodeListLenIndef
Expand All @@ -179,7 +181,7 @@ decodeTxSubmission2 decodeTxId decodeTx = decode
(do CBOR.decodeListLenOf 2
txid <- decodeTxId
sz <- CBOR.decodeWord32
return (txid, sz))
return (txid, SizeInBytes sz))
case (b, txids) of
(TokBlocking, t:ts) ->
return $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ module Ouroboros.Network.Protocol.TxSubmission2.Server
TxSubmissionServerPipelined (..)
, ServerStIdle (..)
, Collect (..)
, TxSizeInBytes
-- * Execution as a typed protocol
, txSubmissionServerPeerPipelined
-- * deprecated API
, TxSizeInBytes
) where

import Data.List.NonEmpty (NonEmpty)
Expand All @@ -44,7 +45,7 @@ data TxSubmissionServerPipelined txid tx m a where
data Collect txid tx =
-- | The result of 'SendMsgRequestTxIdsPipelined'. It also carries
-- the number of txids originally requested.
CollectTxIds Word16 [(txid, TxSizeInBytes)]
CollectTxIds Word16 [(txid, SizeInBytes)]

-- | The result of 'SendMsgRequestTxsPipelined'. The actual reply only
-- contains the transactions sent, but this pairs them up with the
Expand All @@ -61,7 +62,7 @@ data ServerStIdle (n :: N) txid tx m a where
:: Word16 -- ^ number of txids to acknowledge
-> Word16 -- ^ number of txids to request
-> m a -- ^ Result if done
-> (NonEmpty (txid, TxSizeInBytes)
-> (NonEmpty (txid, SizeInBytes)
-> m (ServerStIdle Z txid tx m a))
-> ServerStIdle Z txid tx m a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,35 @@
--
-- This is used to relay transactions between nodes.
--
module Ouroboros.Network.Protocol.TxSubmission2.Type where
module Ouroboros.Network.Protocol.TxSubmission2.Type
( TxSubmission2 (..)
, Message (..)
, ClientHasAgency (..)
, ServerHasAgency (..)
, NobodyHasAgency (..)
, TokBlockingStyle (..)
, StBlockingStyle (..)
, BlockingReplyList (..)
-- re-exports
, SizeInBytes (..)
-- deprecated API
, TxSizeInBytes
) where

import Control.DeepSeq
import Data.List.NonEmpty (NonEmpty)
import Data.Word (Word16, Word32)
import Data.Word (Word16)

import Network.TypedProtocol.Core

import Control.DeepSeq
import Ouroboros.Network.SizeInBytes (SizeInBytes (..))
import Ouroboros.Network.Util.ShowProxy

-- | Transactions are typically not big, but in principle in future we could
-- have ones over 64k large.
--
type TxSizeInBytes = Word32
type TxSizeInBytes = SizeInBytes
{-# DEPRECATED TxSizeInBytes "Use 'Ouroboros.Network.SizeInBytes.SizeInBytes' instead" #-}

-- | The kind of the transaction-submission protocol, and the types of the
-- states in the protocol state machine.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Network.TypedProtocol.Pipelined (N, Nat (..))

import Ouroboros.Network.Protocol.TxSubmission2.Client
import Ouroboros.Network.Protocol.TxSubmission2.Server
import Ouroboros.Network.SizeInBytes (SizeInBytes)


--
Expand Down Expand Up @@ -57,7 +58,7 @@ txSubmissionClient
(Ord txid, Show txid, Monad m)
=> Tracer m (TraceEventClient txid tx)
-> (tx -> txid)
-> (tx -> TxSizeInBytes)
-> (tx -> SizeInBytes)
-> Word16 -- ^ Maximum number of unacknowledged txids allowed
-> [tx]
-> TxSubmissionClient txid tx m ()
Expand Down Expand Up @@ -182,7 +183,7 @@ data ServerState txid tx = ServerState {
-- requested. This is not ordered to illustrate the fact that we can
-- request txs out of order. We also remember the sizes, though this
-- example does not make use of the size information.
availableTxids :: Map txid TxSizeInBytes,
availableTxids :: Map txid SizeInBytes,

-- | Transactions we have successfully downloaded but have not yet added
-- to the mempool or acknowledged. This is needed because we request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Ouroboros.Network.Protocol.TxSubmission2.Test
, TxId (..)
) where

import Data.Bifunctor (second)
import Data.ByteString.Lazy (ByteString)
import Data.List (nub)
import Data.List.NonEmpty qualified as NonEmpty
Expand Down Expand Up @@ -255,11 +256,12 @@ instance Arbitrary (AnyMessageAndAgency (TxSubmission2 TxId Tx)) where

, AnyMessageAndAgency (ClientAgency (TokTxIds TokBlocking)) <$>
MsgReplyTxIds <$> (BlockingReply . NonEmpty.fromList
. map (second SizeInBytes)
. QC.getNonEmpty
<$> arbitrary)

, AnyMessageAndAgency (ClientAgency (TokTxIds TokNonBlocking)) <$>
MsgReplyTxIds <$> (NonBlockingReply <$> arbitrary)
MsgReplyTxIds <$> (NonBlockingReply . map (second SizeInBytes) <$> arbitrary)

, AnyMessageAndAgency (ServerAgency TokIdle) <$>
MsgRequestTxs <$> arbitrary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tests = testGroup "TxSubmission"

data Tx txid = Tx {
getTxId :: txid,
getTxSize :: TxSizeInBytes,
getTxSize :: SizeInBytes,
-- | If false this means that when this tx will be submitted to a remote
-- mempool it will not be valid. The outbound mempool might contain
-- invalid tx's in this sense.
Expand All @@ -88,7 +88,7 @@ instance ShowProxy txid => ShowProxy (Tx txid) where
instance Arbitrary txid => Arbitrary (Tx txid) where
arbitrary =
Tx <$> arbitrary
<*> arbitrary
<*> (SizeInBytes <$> arbitrary)
<*> frequency [ (3, pure True)
, (1, pure False)
]
Expand Down Expand Up @@ -136,7 +136,7 @@ getMempoolReader (Mempool mempool) =
mempoolHasTx = \txid -> isJust $ find (\tx -> getTxId tx == txid) seq
}

f :: Int -> Tx txid -> (txid, Int, TxSizeInBytes)
f :: Int -> Tx txid -> (txid, Int, SizeInBytes)
f idx Tx {getTxId, getTxSize} = (getTxId, idx, getTxSize)


Expand Down Expand Up @@ -177,13 +177,13 @@ txSubmissionCodec2 =
encodeTx Tx {getTxId, getTxSize, getTxValid} =
CBOR.encodeListLen 3
<> CBOR.encodeInt getTxId
<> CBOR.encodeWord32 getTxSize
<> CBOR.encodeWord32 (getSizeInBytes getTxSize)
<> CBOR.encodeBool getTxValid

decodeTx = do
_ <- CBOR.decodeListLen
Tx <$> CBOR.decodeInt
<*> CBOR.decodeWord32
<*> (SizeInBytes <$> CBOR.decodeWord32)
<*> CBOR.decodeBool


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Network.TypedProtocol.Pipelined (N, Nat (..), natToInt)

import Ouroboros.Network.NodeToNode.Version (NodeToNodeVersion)
import Ouroboros.Network.Protocol.TxSubmission2.Server
import Ouroboros.Network.SizeInBytes (SizeInBytes)
import Ouroboros.Network.TxSubmission.Mempool.Reader (MempoolSnapshot (..),
TxSubmissionMempoolReader (..))

Expand Down Expand Up @@ -118,7 +119,7 @@ data ServerState txid tx = ServerState {
-- are a subset of the 'unacknowledgedTxIds' that we have not yet
-- requested. This is not ordered to illustrate the fact that we can
-- request txs out of order. We also remember the size.
availableTxids :: !(Map txid TxSizeInBytes),
availableTxids :: !(Map txid SizeInBytes),

-- | Transactions we have successfully downloaded but have not yet added
-- to the mempool or acknowledged. This needed because we can request
Expand Down Expand Up @@ -187,7 +188,7 @@ txSubmissionInbound tracer maxUnacked mpReader mpWriter _version =
continueWithStateM (serverIdle Zero) initialServerState
where
-- TODO #1656: replace these fixed limits by policies based on
-- TxSizeInBytes and delta-Q and the bandwidth/delay product.
-- SizeInBytes and delta-Q and the bandwidth/delay product.
-- These numbers are for demo purposes only, the throughput will be low.
maxTxIdsToRequest = 3 :: Word16
maxTxToRequest = 2 :: Word16
Expand Down Expand Up @@ -378,7 +379,7 @@ txSubmissionInbound tracer maxUnacked mpReader mpWriter _version =
--
acknowledgeTxIds :: ServerState txid tx
-> StrictSeq txid
-> Map txid TxSizeInBytes
-> Map txid SizeInBytes
-> MempoolSnapshot txid tx idx
-> ServerState txid tx
acknowledgeTxIds st txidsSeq _ _ | Seq.null txidsSeq = st
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ module Ouroboros.Network.TxSubmission.Mempool.Reader
) where

import Control.Monad.Class.MonadSTM (MonadSTM, STM)

import Ouroboros.Network.Protocol.TxSubmission2.Client (TxSizeInBytes)
import Ouroboros.Network.SizeInBytes (SizeInBytes)

-- | The consensus layer functionality that the inbound and outbound side of
-- the tx submission logic requires.
Expand Down Expand Up @@ -53,7 +52,7 @@ mapTxSubmissionMempoolReader f rdr =
--
data MempoolSnapshot txid tx idx =
MempoolSnapshot {
mempoolTxIdsAfter :: idx -> [(txid, idx, TxSizeInBytes)],
mempoolTxIdsAfter :: idx -> [(txid, idx, SizeInBytes)],
mempoolLookupTx :: idx -> Maybe tx,
mempoolHasTx :: txid -> Bool
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Ouroboros.Network.ControlMessage (ControlMessage, ControlMessageSTM,
timeoutWithControlMessage)
import Ouroboros.Network.NodeToNode.Version (NodeToNodeVersion)
import Ouroboros.Network.Protocol.TxSubmission2.Client
import Ouroboros.Network.SizeInBytes (SizeInBytes)
import Ouroboros.Network.TxSubmission.Mempool.Reader (MempoolSnapshot (..),
TxSubmissionMempoolReader (..))

Expand Down Expand Up @@ -119,7 +120,7 @@ txSubmissionOutbound tracer maxUnacked TxSubmissionMempoolReader{..} _version co
!lastIdx'
| null txs = lastIdx
| otherwise = idx where (_, idx, _) = last txs
txs' :: [(txid, TxSizeInBytes)]
txs' :: [(txid, SizeInBytes)]
txs' = [ (txid, size) | (txid, _, size) <- txs ]
client' = client unackedSeq'' lastIdx'
in (txs', client')
Expand Down

0 comments on commit 8d42239

Please sign in to comment.