Skip to content

Commit 799ca85

Browse files
mempool: rename MempoolAddFail to TxValidationFail
1 parent c567868 commit 799ca85

File tree

7 files changed

+59
-43
lines changed

7 files changed

+59
-43
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 {
@@ -144,9 +144,8 @@ ntcApps
144144
, MonadSTM m
145145
, Crypto crypto
146146
, Aeson.ToJSON ntcAddr
147-
, Aeson.ToJSON (MempoolAddFail (Sig crypto))
148-
, Show (MempoolAddFail (Sig crypto))
149-
, ShowProxy (MempoolAddFail (Sig crypto))
147+
, Aeson.ToJSON (TxValidationFail (Sig crypto))
148+
, ShowProxy (TxValidationFail (Sig crypto))
150149
, ShowProxy (Sig crypto)
151150
, Typeable crypto
152151
)

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)
@@ -59,27 +59,27 @@ localMsgSubmissionServer getMsgId tracer MempoolWriter { mempoolAddTxs } =
5959
data TraceLocalMsgSubmission msg msgid =
6060
TraceReceivedMsg msgid
6161
-- ^ A signature was received.
62-
| TraceSubmitFailure msgid (MempoolAddFail msg)
62+
| TraceSubmitFailure msgid (TxValidationFail msg)
6363
| TraceSubmitAccept msgid
6464

6565
deriving instance
66-
(Show msg, Show msgid, Show (MempoolAddFail msg))
66+
(Show msg, Show msgid, Show (TxValidationFail msg))
6767
=> Show (TraceLocalMsgSubmission msg msgid)
6868

6969

7070

7171
data MsgSubmissionServerException msgid msg =
72-
MsgValidationException msgid (MempoolAddFail msg)
72+
MsgValidationException msgid (TxValidationFail msg)
7373
| TooManyMessages
7474

75-
deriving instance (Show (MempoolAddFail msg), Show msgid)
75+
deriving instance (Show (TxValidationFail msg), Show msgid)
7676
=> Show (MsgSubmissionServerException msgid msg)
7777

78-
instance (Typeable msgid, Typeable msg, Show (MempoolAddFail msg), Show msgid)
78+
instance (Typeable msgid, Typeable msg, Show (TxValidationFail msg), Show msgid)
7979
=> Exception (MsgSubmissionServerException msgid msg) where
8080

8181

82-
instance (ToJSON msgid, ToJSON (MempoolAddFail msg))
82+
instance (ToJSON msgid, ToJSON (TxValidationFail msg))
8383
=> ToJSON (TraceLocalMsgSubmission msg msgid) where
8484
toJSON (TraceReceivedMsg msgid) =
8585
-- 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
@@ -64,7 +64,7 @@ encodeReject = \case
6464
SigResultOther reason
6565
-> CBOR.encodeListLen 2 <> CBOR.encodeWord8 3 <> CBOR.encodeString reason
6666

67-
decodeReject :: CBOR.Decoder s (MempoolAddFail (Sig crypto))
67+
decodeReject :: CBOR.Decoder s (TxValidationFail (Sig crypto))
6868
decodeReject = do
6969
len <- CBOR.decodeListLen
7070
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: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
{-# LANGUAGE FlexibleInstances #-}
33
{-# LANGUAGE MultiWayIf #-}
44
{-# LANGUAGE OverloadedStrings #-}
5-
{-# LANGUAGE StandaloneDeriving #-}
5+
{-# LANGUAGE PatternSynonyms #-}
66
{-# LANGUAGE TupleSections #-}
77
{-# LANGUAGE TypeFamilies #-}
88
{-# LANGUAGE TypeOperators #-}
9+
{-# LANGUAGE ViewPatterns #-}
10+
11+
{-# OPTIONS_GHC -fno-warn-orphans #-}
912

1013
-- | Encapsulates signature validation utilities leveraged by the mempool writer
1114
--
1215
module DMQ.Protocol.SigSubmission.Validate where
1316

1417
import Control.Monad
1518
import Control.Concurrent.Class.MonadSTM.Strict
19+
import Control.Exception (Exception)
1620
import Control.Monad.Class.MonadTime.SI
1721
import Control.Monad.Trans.Class
1822
import Control.Monad.Trans.Except
@@ -45,16 +49,18 @@ import Ouroboros.Network.Util.ShowProxy
4549
-- | The type of non-fatal failures reported by the mempool writer
4650
-- for invalid messages
4751
--
48-
data instance MempoolAddFail (Sig crypto) =
52+
data instance TxValidationFail (Sig crypto) =
4953
SigInvalid SigValidationError
5054
| SigDuplicate
5155
| SigExpired
5256
| SigResultOther Text
5357
deriving (Eq, Show)
5458

55-
instance (Typeable crypto) => ShowProxy (MempoolAddFail (Sig crypto))
59+
instance (Typeable crypto) => ShowProxy (TxValidationFail (Sig crypto))
60+
61+
instance (Typeable crypto) => Exception (TxValidationFail (Sig crypto))
5662

57-
instance ToJSON (MempoolAddFail (Sig crypto)) where
63+
instance ToJSON (TxValidationFail (Sig crypto)) where
5864
toJSON SigDuplicate = String "duplicate"
5965
toJSON SigExpired = String "expired"
6066
toJSON (SigInvalid e) = object
@@ -84,6 +90,21 @@ data SigValidationError =
8490
deriving (Eq, Show)
8591

8692

93+
c_MAX_CLOCK_SKEW_SEC :: NominalDiffTime
94+
c_MAX_CLOCK_SKEW_SEC = 5
95+
96+
pattern NotZeroSetSnapshot :: StakeSnapshot
97+
pattern NotZeroSetSnapshot <- (isZero . ssSetPool -> False)
98+
99+
pattern NotZeroMarkSnapshot :: StakeSnapshot
100+
pattern NotZeroMarkSnapshot <- (isZero . ssMarkPool -> False)
101+
102+
pattern ZeroSetSnapshot :: StakeSnapshot
103+
pattern ZeroSetSnapshot <- (isZero . ssSetPool -> True)
104+
105+
{-# COMPLETE NotZeroSetSnapshot, NotZeroMarkSnapshot, ZeroSetSnapshot #-}
106+
107+
87108
-- TODO:
88109
-- We don't validate ocert numbers, since we might not have necessary
89110
-- information to do so, but we can validate that they are growing.
@@ -99,9 +120,9 @@ validateSig :: forall crypto m.
99120
-> [Sig crypto]
100121
-> PoolValidationCtx m
101122
-- ^ cardano pool id verification
102-
-> ExceptT (Sig crypto, MempoolAddFail (Sig crypto)) m
103-
[(Sig crypto, Either (MempoolAddFail (Sig crypto)) ())]
104-
validateSig _ec verKeyHashingFn sigs ctx = traverse process' sigs
123+
-> ExceptT (Sig crypto, TxValidationFail (Sig crypto)) m
124+
[(Sig crypto, Either (TxValidationFail (Sig crypto)) ())]
125+
validateSig verKeyHashingFn sigs ctx = traverse process' sigs
105126
where
106127
DMQPoolValidationCtx now mNextEpoch pools ocertCountersVar = ctx
107128

@@ -123,33 +144,31 @@ validateSig _ec verKeyHashingFn sigs ctx = traverse process' sigs
123144
?! KESBeforeStartOCERT startKESPeriod sigKESPeriod
124145
e <- case Map.lookup (verKeyHashingFn coldKey) pools of
125146
Nothing | isNothing mNextEpoch
126-
-> invalid SigResultOther $ Text.pack "not initialized yet"
147+
-> right . Left . SigResultOther $ Text.pack "not initialized yet"
127148
| otherwise
128149
-> left $ SigInvalid UnrecognizedPool
129-
-- TODO make 5 a constant
130-
Just ss | not (isZero (ssSetPool ss)) ->
150+
Just ss | NotZeroSetSnapshot <- ss ->
131151
if | now < nextEpoch -> success
132152
-- localstatequery is late, but the pool is about to expire
133153
| isZero (ssMarkPool ss)
134-
, now > addUTCTime 5 nextEpoch -> left SigExpired
154+
, now > addUTCTime c_MAX_CLOCK_SKEW_SEC nextEpoch -> left SigExpired
135155
-- we bound the time we're willing to approve a message
136156
-- in case smth happened to localstatequery and it's taking
137157
-- too long to update our state
138-
| now <= addUTCTime 5 nextEpoch -> success
139-
| otherwise -> left $ SigInvalid ClockSkew
140-
| not (isZero (ssMarkPool ss)) ->
158+
| now <= addUTCTime c_MAX_CLOCK_SKEW_SEC nextEpoch -> success
159+
| otherwise -> right . Left $ SigInvalid ClockSkew
160+
| NotZeroMarkSnapshot <- ss ->
141161
-- we take abs time in case we're late with our own
142162
-- localstatequery update, and/or the other side's clock
143163
-- is ahead, and we're just about or have just crossed the epoch
144164
-- and the pool is expected to move into the set mark
145-
if | abs (diffUTCTime nextEpoch now) <= 5 -> success
146-
| diffUTCTime nextEpoch now > 5 ->
165+
if | abs (diffUTCTime nextEpoch now) <= c_MAX_CLOCK_SKEW_SEC -> success
166+
| diffUTCTime nextEpoch now > c_MAX_CLOCK_SKEW_SEC ->
147167
left . SigResultOther $ Text.pack "pool not eligible yet"
148168
| otherwise -> right . Left $ SigInvalid ClockSkew
149169
-- pool is deregistered and ineligible to mint blocks
150-
| isZero (ssSetPool ss) ->
170+
| ZeroSetSnapshot <- ss ->
151171
left SigExpired
152-
| otherwise -> error "validateSig: impossible pool validation error"
153172
where
154173
-- mNextEpoch and pools are initialized in one STM transaction
155174
-- and fromJust will not fail here
@@ -167,15 +186,14 @@ validateSig _ec verKeyHashingFn sigs ctx = traverse process' sigs
167186
let f = \case
168187
Nothing -> Right $ Just ocertN
169188
Just n | n <= ocertN -> Right $ Just ocertN
170-
| otherwise -> Left . throwE . SigInvalid $ InvalidOCertCounter n ocertN
189+
| otherwise -> Left $ InvalidOCertCounter n ocertN
171190
in case Map.alterF f (verKeyHashingFn coldKey) ocertCounters of
172191
Right ocertCounters' -> (void success, ocertCounters')
173-
Left err -> (err, ocertCounters)
192+
Left err -> (throwE (SigInvalid err), ocertCounters)
174193
-- for eg. remember to run all results with possibly non-fatal errors
175194
right e
176195
where
177196
success = right $ Right ()
178-
invalid tag = right . Left . tag
179197

180198
startKESPeriod, endKESPeriod :: KESPeriod
181199

@@ -187,12 +205,12 @@ validateSig _ec verKeyHashingFn sigs ctx = traverse process' sigs
187205

188206
(?!:) :: Either e1 ()
189207
-> (e1 -> SigValidationError)
190-
-> ExceptT (MempoolAddFail (Sig crypto)) m ()
208+
-> ExceptT (TxValidationFail (Sig crypto)) m ()
191209
(?!:) result f = firstExceptT (SigInvalid . f) . hoistEither $ result
192210

193211
(?!) :: Bool
194212
-> SigValidationError
195-
-> ExceptT (MempoolAddFail (Sig crypto)) m ()
213+
-> ExceptT (TxValidationFail (Sig crypto)) m ()
196214
(?!) flag sve = if flag then void success else left (SigInvalid sve)
197215

198216
infix 1 ?!

0 commit comments

Comments
 (0)