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--
1213module 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 )) () )]
105106validateSig 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 ?!
0 commit comments