Skip to content

Commit 10eaa57

Browse files
mempool: rename MempoolAddFail to TxValidationFail
1 parent f312ca0 commit 10eaa57

File tree

8 files changed

+41
-48
lines changed

8 files changed

+41
-48
lines changed

dmq-node/src/DMQ/NodeToClient.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ data Codecs crypto m =
101101
dmqCodecs :: ( MonadST m
102102
, Crypto crypto
103103
)
104-
=> (MempoolAddFail (Sig crypto) -> CBOR.Encoding)
105-
-> (forall s. CBOR.Decoder s (MempoolAddFail (Sig crypto)))
104+
=> (TxValidationFail (Sig crypto) -> CBOR.Encoding)
105+
-> (forall s. CBOR.Decoder s (TxValidationFail (Sig crypto)))
106106
-> Codecs crypto m
107107
dmqCodecs encodeReject' decodeReject' =
108108
Codecs {
@@ -139,9 +139,8 @@ ntcApps
139139
, MonadSTM m
140140
, Crypto crypto
141141
, Aeson.ToJSON ntcAddr
142-
, Aeson.ToJSON (MempoolAddFail (Sig crypto))
143-
, Show (MempoolAddFail (Sig crypto))
144-
, ShowProxy (MempoolAddFail (Sig crypto))
142+
, Aeson.ToJSON (TxValidationFail (Sig crypto))
143+
, ShowProxy (TxValidationFail (Sig crypto))
145144
, ShowProxy (Sig crypto)
146145
, Typeable crypto
147146
)

dmq-node/src/DMQ/NodeToClient/LocalMsgSubmission.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ localMsgSubmissionServer ::
2626
, Typeable msgid
2727
, Typeable msg
2828
, Show msgid
29-
, Show (MempoolAddFail msg))
29+
, Show (TxValidationFail msg))
3030
=> (msg -> msgid)
3131
-- ^ get message id
3232
-> Tracer m (TraceLocalMsgSubmission msg msgid)
@@ -57,27 +57,27 @@ localMsgSubmissionServer getMsgId tracer MempoolWriter { mempoolAddTxs } =
5757
data TraceLocalMsgSubmission msg msgid =
5858
TraceReceivedMsg msgid
5959
-- ^ A signature was received.
60-
| TraceSubmitFailure msgid (MempoolAddFail msg)
60+
| TraceSubmitFailure msgid (TxValidationFail msg)
6161
| TraceSubmitAccept msgid
6262

6363
deriving instance
64-
(Show msg, Show msgid, Show (MempoolAddFail msg))
64+
(Show msg, Show msgid, Show (TxValidationFail msg))
6565
=> Show (TraceLocalMsgSubmission msg msgid)
6666

6767

6868

6969
data MsgSubmissionServerException msgid msg =
70-
MsgValidationException msgid (MempoolAddFail msg)
70+
MsgValidationException msgid (TxValidationFail msg)
7171
| TooManyMessages
7272

73-
deriving instance (Show (MempoolAddFail msg), Show msgid)
73+
deriving instance (Show (TxValidationFail msg), Show msgid)
7474
=> Show (MsgSubmissionServerException msgid msg)
7575

76-
instance (Typeable msgid, Typeable msg, Show (MempoolAddFail msg), Show msgid)
76+
instance (Typeable msgid, Typeable msg, Show (TxValidationFail msg), Show msgid)
7777
=> Exception (MsgSubmissionServerException msgid msg) where
7878

7979

80-
instance (ToJSON msgid, ToJSON (MempoolAddFail msg))
80+
instance (ToJSON msgid, ToJSON (TxValidationFail msg))
8181
=> ToJSON (TraceLocalMsgSubmission msg msgid) where
8282
toJSON (TraceReceivedMsg msgid) =
8383
-- TODO: once we have verbosity levels, we could include the full tx, for

dmq-node/src/DMQ/Protocol/LocalMsgSubmission/Client.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Ouroboros.Network.TxSubmission.Mempool.Simple
2121

2222
-- | Type aliases for the high level client API
2323
--
24-
type LocalMsgSubmissionClient sig = LocalTxSubmissionClient sig (MempoolAddFail sig)
24+
type LocalMsgSubmissionClient sig = LocalTxSubmissionClient sig (TxValidationFail sig)
2525
type LocalMsgClientStIdle = LocalTxClientStIdle
2626

2727

dmq-node/src/DMQ/Protocol/LocalMsgSubmission/Codec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ codecLocalMsgSubmission
3030
( MonadST m
3131
, Crypto crypto
3232
)
33-
=> (MempoolAddFail (Sig crypto) -> CBOR.Encoding)
34-
-> (forall s. CBOR.Decoder s (MempoolAddFail (Sig crypto)))
33+
=> (TxValidationFail (Sig crypto) -> CBOR.Encoding)
34+
-> (forall s. CBOR.Decoder s (TxValidationFail (Sig crypto)))
3535
-> AnnotatedCodec (LocalMsgSubmission (Sig crypto)) CBOR.DeserialiseFailure m ByteString
3636
codecLocalMsgSubmission =
3737
LTX.anncodecLocalTxSubmission' SigWithBytes SigSubmission.encodeSig SigSubmission.decodeSig
3838

39-
encodeReject :: MempoolAddFail (Sig crypto) -> CBOR.Encoding
39+
encodeReject :: TxValidationFail (Sig crypto) -> CBOR.Encoding
4040
encodeReject = \case
4141
SigInvalid reason -> CBOR.encodeListLen 2 <> CBOR.encodeWord8 0 <> e
4242
where
@@ -65,7 +65,7 @@ encodeReject = \case
6565
SigResultOther reason
6666
-> CBOR.encodeListLen 2 <> CBOR.encodeWord8 3 <> CBOR.encodeString reason
6767

68-
decodeReject :: CBOR.Decoder s (MempoolAddFail (Sig crypto))
68+
decodeReject :: CBOR.Decoder s (TxValidationFail (Sig crypto))
6969
decodeReject = do
7070
len <- CBOR.decodeListLen
7171
tag <- CBOR.decodeWord8

dmq-node/src/DMQ/Protocol/LocalMsgSubmission/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Ouroboros.Network.TxSubmission.Mempool.Simple
2222

2323
-- | Type aliases for the high level client API
2424
--
25-
type LocalMsgSubmissionServer sig = LocalTxSubmissionServer sig (MempoolAddFail sig)
25+
type LocalMsgSubmissionServer sig = LocalTxSubmissionServer sig (TxValidationFail sig)
2626

2727

2828
-- | A non-pipelined 'Peer' representing the 'LocalMsgSubmissionServer'.

dmq-node/src/DMQ/Protocol/LocalMsgSubmission/Type.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleInstances #-}
3-
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE PolyKinds #-}
54
{-# LANGUAGE TypeFamilies #-}
65

@@ -19,4 +18,4 @@ import Ouroboros.Network.TxSubmission.Mempool.Simple
1918

2019
-- | The LocalMsgSubmission protocol is an alias for the LocalTxSubmission
2120
--
22-
type LocalMsgSubmission sig = Ouroboros.LocalTxSubmission sig (MempoolAddFail sig)
21+
type LocalMsgSubmission sig = Ouroboros.LocalTxSubmission sig (TxValidationFail sig)

dmq-node/src/DMQ/Protocol/SigSubmission/Validate.hs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
{-# LANGUAGE FlexibleInstances #-}
33
{-# LANGUAGE MultiWayIf #-}
44
{-# LANGUAGE OverloadedStrings #-}
5-
{-# LANGUAGE StandaloneDeriving #-}
65
{-# LANGUAGE TupleSections #-}
76
{-# LANGUAGE TypeFamilies #-}
87
{-# LANGUAGE TypeOperators #-}
98

9+
{-# OPTIONS_GHC -fno-warn-orphans #-}
10+
1011
-- | Encapsulates signature validation utilities leveraged by the mempool writer
1112
--
1213
module DMQ.Protocol.SigSubmission.Validate where
@@ -45,16 +46,16 @@ import Ouroboros.Network.Util.ShowProxy
4546
-- | The type of non-fatal failures reported by the mempool writer
4647
-- for invalid messages
4748
--
48-
data instance MempoolAddFail (Sig crypto) =
49+
data instance TxValidationFail (Sig crypto) =
4950
SigInvalid SigValidationError
5051
| SigDuplicate
5152
| SigExpired
5253
| SigResultOther Text
5354
deriving (Eq, Show)
5455

55-
instance (Typeable crypto) => ShowProxy (MempoolAddFail (Sig crypto))
56+
instance (Typeable crypto) => ShowProxy (TxValidationFail (Sig crypto))
5657

57-
instance ToJSON (MempoolAddFail (Sig crypto)) where
58+
instance ToJSON (TxValidationFail (Sig crypto)) where
5859
toJSON SigDuplicate = String "duplicate"
5960
toJSON SigExpired = String "expired"
6061
toJSON (SigInvalid e) = object
@@ -100,8 +101,8 @@ validateSig :: forall crypto m.
100101
-> [Sig crypto]
101102
-> PoolValidationCtx m
102103
-- ^ cardano pool id verification
103-
-> ExceptT (Sig crypto, MempoolAddFail (Sig crypto)) m
104-
[(Sig crypto, Either (MempoolAddFail (Sig crypto)) ())]
104+
-> ExceptT (Sig crypto, TxValidationFail (Sig crypto)) m
105+
[(Sig crypto, Either (TxValidationFail (Sig crypto)) ())]
105106
validateSig verKeyHashingFn sigs ctx = traverse process' sigs
106107
where
107108
DMQPoolValidationCtx now mNextEpoch pools ocertCountersVar = ctx
@@ -124,22 +125,20 @@ validateSig verKeyHashingFn sigs ctx = traverse process' sigs
124125
?! KESBeforeStartOCERT startKESPeriod sigKESPeriod
125126
e <- case Map.lookup (verKeyHashingFn coldKey) pools of
126127
Nothing | isNothing mNextEpoch
127-
-> invalid SigResultOther $ Text.pack "not initialized yet"
128+
-> right . Left . SigResultOther $ Text.pack "not initialized yet"
128129
| otherwise
129130
-> left $ SigInvalid UnrecognizedPool
130131
-- TODO make 5 a constant
131132
Just ss | not (isZero (ssSetPool ss)) ->
132133
if | now < nextEpoch -> success
133134
-- localstatequery is late, but the pool is about to expire
134-
| isZero (ssMarkPool ss) ->
135-
if now <= addUTCTime 5 nextEpoch
136-
then invalid SigInvalid ClockSkew
137-
else left $ SigInvalid ExpiredPool
135+
| isZero (ssMarkPool ss)
136+
, now > addUTCTime 5 nextEpoch -> left $ SigInvalid ExpiredPool
138137
-- we bound the time we're willing to approve a message
139138
-- in case smth happened to localstatequery and it's taking
140139
-- too long to update our state
141140
| now <= addUTCTime 5 nextEpoch -> success
142-
| otherwise -> left $ SigInvalid ClockSkew
141+
| otherwise -> right . Left $ SigInvalid ClockSkew
143142
| not (isZero (ssMarkPool ss)) ->
144143
-- we take abs time in case we're late with our own
145144
-- localstatequery update, and/or the other side's clock
@@ -169,15 +168,14 @@ validateSig verKeyHashingFn sigs ctx = traverse process' sigs
169168
let f = \case
170169
Nothing -> Right $ Just ocertN
171170
Just n | n <= ocertN -> Right $ Just ocertN
172-
| otherwise -> Left . throwE . SigInvalid $ InvalidOCertCounter n ocertN
171+
| otherwise -> Left $ InvalidOCertCounter n ocertN
173172
in case Map.alterF f (verKeyHashingFn coldKey) ocertCounters of
174173
Right ocertCounters' -> (void success, ocertCounters')
175-
Left err -> (err, ocertCounters)
174+
Left err -> (throwE (SigInvalid err), ocertCounters)
176175
-- for eg. remember to run all results with possibly non-fatal errors
177176
right e
178177
where
179178
success = right $ Right ()
180-
invalid tag = right . Left . tag
181179

182180
startKESPeriod, endKESPeriod :: KESPeriod
183181

@@ -189,12 +187,12 @@ validateSig verKeyHashingFn sigs ctx = traverse process' sigs
189187

190188
(?!:) :: Either e1 ()
191189
-> (e1 -> SigValidationError)
192-
-> ExceptT (MempoolAddFail (Sig crypto)) m ()
190+
-> ExceptT (TxValidationFail (Sig crypto)) m ()
193191
(?!:) result f = firstExceptT (SigInvalid . f) . hoistEither $ result
194192

195193
(?!) :: Bool
196194
-> SigValidationError
197-
-> ExceptT (MempoolAddFail (Sig crypto)) m ()
195+
-> ExceptT (TxValidationFail (Sig crypto)) m ()
198196
(?!) flag sve = if flag then void success else left (SigInvalid sve)
199197

200198
infix 1 ?!

ouroboros-network/lib/Ouroboros/Network/TxSubmission/Mempool/Simple.hs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
{-# LANGUAGE GADTs #-}
77
{-# LANGUAGE NamedFieldPuns #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
9-
{-# LANGUAGE StandaloneDeriving #-}
109
{-# LANGUAGE TupleSections #-}
1110
{-# LANGUAGE TypeFamilies #-}
1211

1312
-- | The module should be imported qualified.
1413
--
1514
module Ouroboros.Network.TxSubmission.Mempool.Simple
16-
( MempoolAddFail
15+
( TxValidationFail
1716
, Mempool (..)
1817
, MempoolSeq (..)
1918
, MempoolWriter (..)
@@ -28,7 +27,6 @@ module Ouroboros.Network.TxSubmission.Mempool.Simple
2827
import Prelude hiding (read, seq)
2928

3029
import Control.Concurrent.Class.MonadSTM.Strict
31-
import Control.DeepSeq
3230
import Control.Exception (assert)
3331
import Control.Monad.Trans.Except
3432
import Data.Bifunctor (bimap, first, second)
@@ -116,7 +114,7 @@ getReader getTxId getTxSize (Mempool mempool) =
116114

117115
-- | type of mempool validation errors which are non-fatal
118116
--
119-
data family MempoolAddFail tx
117+
data family TxValidationFail tx
120118

121119
-- | A mempool writer which generalizes the tx submission mempool writer
122120
-- TODO: We could replace TxSubmissionMempoolWriter with this at some point
@@ -138,7 +136,7 @@ data MempoolWriter txid tx idx m =
138136
-- returned.
139137
mempoolAddTxs
140138
:: [tx]
141-
-> m (Either (txid, MempoolAddFail tx) [(txid, SubmitResult (MempoolAddFail tx))])
139+
-> m (Either (txid, TxValidationFail tx) [(txid, SubmitResult (TxValidationFail tx))])
142140
}
143141

144142

@@ -147,10 +145,9 @@ data MempoolWriter txid tx idx m =
147145
--
148146
getWriter :: forall tx txid ctx m.
149147
( MonadSTM m
150-
-- TODO:
151148
-- , NFData txid
152149
-- , NFData tx
153-
-- , NFData (MempoolAddFail tx)
150+
-- , NFData (TxValidationFail tx)
154151
, Ord txid
155152
)
156153
=> (tx -> txid)
@@ -159,11 +156,11 @@ getWriter :: forall tx txid ctx m.
159156
-- ^ acquire validation context
160157
-> ( [tx]
161158
-> ctx
162-
-> ExceptT (tx, MempoolAddFail tx) m
163-
[(tx, Either (MempoolAddFail tx) ())])
159+
-> ExceptT (tx, TxValidationFail tx) m
160+
[(tx, Either (TxValidationFail tx) ())])
164161
-- ^ validation function which should evaluate its result to normal form
165162
-- esp. if it is 'expensive'
166-
-> MempoolAddFail tx
163+
-> TxValidationFail tx
167164
-- ^ replace duplicates
168165
-> Mempool m txid tx
169166
-> MempoolWriter txid tx Int m
@@ -184,7 +181,7 @@ getWriter getTxId acquireCtx validateTxs duplicateFail (Mempool mempool) =
184181
[if duplicate then
185182
Left (txid, duplicateFail)
186183
else
187-
bimap ((txid,)) (const (txid, tx)) eResult
184+
bimap (txid,) (const (txid, tx)) eResult
188185
| (txid, (tx, eResult)) <- vTxs
189186
, let duplicate = txid `Set.member` mempoolSet
190187
]

0 commit comments

Comments
 (0)