Skip to content

Commit 9438a2b

Browse files
Add signature validation tracer
1 parent e12cb0c commit 9438a2b

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

dmq-node/app/Main.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ runDMQ commandLineConfig = do
8383
dmqcPrettyLog = I prettyLog,
8484
dmqcTopologyFile = I topologyFile,
8585
dmqcHandshakeTracer = I handshakeTracer,
86+
dmqcValidationTracer = I validationTracer,
8687
dmqcLocalHandshakeTracer = I localHandshakeTracer,
8788
dmqcCardanoNodeSocket = I snocketPath,
8889
dmqcVersion = I version
@@ -132,11 +133,14 @@ runDMQ commandLineConfig = do
132133
let sigSize :: Sig StandardCrypto -> SizeInBytes
133134
sigSize = fromIntegral . BSL.length . sigRawBytes
134135
mempoolReader = Mempool.getReader sigId sigSize (mempool nodeKernel)
136+
ntnValidationTracer = if validationTracer
137+
then WithEventType "NtN Validation" >$< tracer
138+
else nullTracer
135139
dmqNtNApps =
136140
let ntnMempoolWriter = Mempool.writerAdapter $
137141
Mempool.getWriter sigId
138142
(poolValidationCtx $ stakePools nodeKernel)
139-
(validateSig (hashKey . VKey))
143+
(validateSig ntnValidationTracer (hashKey . VKey))
140144
SigDuplicate
141145
(mempool nodeKernel)
142146
in ntnApps tracer
@@ -152,11 +156,14 @@ runDMQ commandLineConfig = do
152156
(decodeRemoteAddress (maxBound @NodeToNodeVersion)))
153157
dmqLimitsAndTimeouts
154158
defaultSigDecisionPolicy
159+
ntcValidationTracer = if validationTracer
160+
then WithEventType "NtC Validation" >$< tracer
161+
else nullTracer
155162
dmqNtCApps =
156163
let ntcMempoolWriter =
157164
Mempool.getWriter sigId
158165
(poolValidationCtx $ stakePools nodeKernel)
159-
(validateSig (hashKey . VKey))
166+
(validateSig ntcValidationTracer (hashKey . VKey))
160167
SigDuplicate
161168
(mempool nodeKernel)
162169
in NtC.ntcApps tracer dmqConfig

dmq-node/src/DMQ/Configuration.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ data Configuration' f =
135135
dmqcLocalServerTracer :: f Bool,
136136
dmqcLocalInboundGovernorTracer :: f Bool,
137137
dmqcDnsTracer :: f Bool,
138+
dmqcValidationTracer :: f Bool,
138139

139140
-- low level verbose traces which trace protocol messages
140141
-- TODO: pref
@@ -256,6 +257,7 @@ defaultConfiguration = Configuration {
256257
dmqcLocalServerTracer = I False,
257258
dmqcLocalInboundGovernorTracer = I False,
258259
dmqcDnsTracer = I False,
260+
dmqcValidationTracer = I False,
259261

260262
dmqcSigSubmissionClientProtocolTracer = I False,
261263
dmqcSigSubmissionServerProtocolTracer = I False,
@@ -349,6 +351,7 @@ instance FromJSON PartialConfig where
349351
dmqcLocalServerTracer <- Last <$> v .:? "LocalServerTracer"
350352
dmqcLocalInboundGovernorTracer <- Last <$> v .:? "LocalInboundGovernorTracer"
351353
dmqcDnsTracer <- Last <$> v .:? "DnsTracer"
354+
dmqcValidationTracer <- Last <$> v .:? "ValidationTracer"
352355

353356
dmqcSigSubmissionClientProtocolTracer <- Last <$> v .:? "SigSubmissionClientProtocolTracer"
354357
dmqcSigSubmissionServerProtocolTracer <- Last <$> v .:? "SigSubmissionServerProtocolTracer"
@@ -427,6 +430,7 @@ instance ToJSON Configuration where
427430
, "LocalServerTracer" .= unI dmqcLocalServerTracer
428431
, "LocalInboundGovernorTracer" .= unI dmqcLocalInboundGovernorTracer
429432
, "DnsTracer" .= unI dmqcDnsTracer
433+
, "ValidationTracer" .= unI dmqcValidationTracer
430434
, "SigSubmissionClientProtocolTracer" .= unI dmqcSigSubmissionClientProtocolTracer
431435
, "SigSubmissionServerProtocolTracer" .= unI dmqcSigSubmissionServerProtocolTracer
432436
, "KeepAliveClientProtocolTracer" .= unI dmqcKeepAliveClientProtocolTracer

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE MultiWayIf #-}
44
{-# LANGUAGE OverloadedStrings #-}
55
{-# LANGUAGE PatternSynonyms #-}
6+
{-# LANGUAGE RankNTypes #-}
67
{-# LANGUAGE TupleSections #-}
78
{-# LANGUAGE TypeFamilies #-}
89
{-# LANGUAGE TypeOperators #-}
@@ -21,6 +22,7 @@ import Control.Monad.Class.MonadTime.SI
2122
import Control.Monad.Trans.Class
2223
import Control.Monad.Trans.Except
2324
import Control.Monad.Trans.Except.Extra
25+
import Control.Tracer (Tracer, traceWith)
2426
import Data.Aeson
2527
import Data.ByteString (ByteString)
2628
import Data.ByteString.Lazy qualified as LBS
@@ -116,27 +118,31 @@ validateSig :: forall crypto m.
116118
, Signable (KES crypto) ByteString
117119
, MonadSTM m
118120
)
119-
=> (DSIGN.VerKeyDSIGN (DSIGN crypto) -> KeyHash StakePool)
121+
=> Tracer m (Sig crypto, TxValidationFail (Sig crypto))
122+
-> (DSIGN.VerKeyDSIGN (DSIGN crypto) -> KeyHash StakePool)
120123
-> [Sig crypto]
121124
-> PoolValidationCtx m
122125
-- ^ cardano pool id verification
123126
-> ExceptT (Sig crypto, TxValidationFail (Sig crypto)) m
124127
[(Sig crypto, Either (TxValidationFail (Sig crypto)) ())]
125-
validateSig verKeyHashingFn sigs ctx = traverse process' sigs
128+
validateSig tracer verKeyHashingFn sigs ctx = traverse process' sigs
126129
where
127130
DMQPoolValidationCtx now mNextEpoch pools ocertCountersVar = ctx
128131

129-
process' sig = bimapExceptT (sig,) (sig,) $ process sig
130-
131-
process Sig { sigSignedBytes = signedBytes,
132-
sigKESPeriod,
133-
sigOpCertificate = SigOpCertificate ocert@OCert {
134-
ocertKESPeriod,
135-
ocertVkHot,
136-
ocertN
137-
},
138-
sigColdKey = SigColdKey coldKey,
139-
sigKESSignature = SigKESSignature kesSig
132+
process' sig =
133+
let result = process sig
134+
in bimapExceptT (sig,) (sig,) $
135+
result `catchLeftT` \e -> result <* lift (traceWith tracer (sig, e))
136+
137+
process sig@Sig { sigSignedBytes = signedBytes,
138+
sigKESPeriod,
139+
sigOpCertificate = SigOpCertificate ocert@OCert {
140+
ocertKESPeriod,
141+
ocertVkHot,
142+
ocertN
143+
},
144+
sigColdKey = SigColdKey coldKey,
145+
sigKESSignature = SigKESSignature kesSig
140146
} = do
141147
sigKESPeriod < endKESPeriod
142148
?! KESAfterEndOCERT endKESPeriod sigKESPeriod
@@ -191,7 +197,11 @@ validateSig verKeyHashingFn sigs ctx = traverse process' sigs
191197
Right ocertCounters' -> (void success, ocertCounters')
192198
Left err -> (throwE (SigInvalid err), ocertCounters)
193199
-- for eg. remember to run all results with possibly non-fatal errors
194-
right e
200+
let result = e
201+
case result of
202+
Left e' -> lift $ traceWith tracer (sig, e')
203+
Right _ -> pure ()
204+
right result
195205
where
196206
success = right $ Right ()
197207

0 commit comments

Comments
 (0)