Skip to content

Commit d748611

Browse files
nfrisbypalas
authored andcommitted
Add MempoolTimeoutSoftPredicate, ought to live in Consensus Layer
1 parent 456f0ec commit d748611

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

cardano-node/src/Cardano/Node/TraceConstraints.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Cardano.Ledger.Keys
1313
import Cardano.Logging (LogFormatting)
1414
import Cardano.Node.Queries (ConvertTxId, GetKESInfo (..), HasKESInfo (..),
1515
HasKESMetricsData (..), LedgerQueries)
16+
import qualified Cardano.Node.Tracing.Tracers.Consensus as ConsensusTracers
1617
import Cardano.Protocol.Crypto (StandardCrypto)
1718
import Cardano.Tracing.HasIssuer (HasIssuer)
1819
import Ouroboros.Consensus.Block (BlockProtocol, CannotForge, ForgeStateUpdateError,
@@ -73,4 +74,6 @@ type TraceConstraints blk =
7374
, LogFormatting (ForgeStateUpdateError blk)
7475
, LogFormatting (Set (Credential 'Staking))
7576
, LogFormatting (NonEmpty.NonEmpty (KeyHash 'Staking))
77+
78+
, ConsensusTracers.MempoolTimeoutSoftPredicate blk
7679
)

cardano-node/src/Cardano/Node/Tracing/Tracers/Consensus.hs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE FlexibleInstances #-}
34
{-# LANGUAGE LambdaCase #-}
@@ -19,6 +20,8 @@ module Cardano.Node.Tracing.Tracers.Consensus
1920
, servedBlockLatest
2021
, ClientMetrics
2122
, txsMempoolTimeoutSoftCounterName
23+
, MempoolTimeoutSoftPredicate (..)
24+
, EraMempoolTimeoutSoftPredicate (..)
2225
, impliesMempoolTimeoutSoft
2326
) where
2427

@@ -80,12 +83,23 @@ import Data.Int (Int64)
8083
import Data.IntPSQ (IntPSQ)
8184
import qualified Data.IntPSQ as Pq
8285
import qualified Data.List as List
86+
import qualified Data.List.NonEmpty as NE
8387
import qualified Data.Text as Text
8488
import Data.Time (NominalDiffTime)
8589
import Data.Word (Word32, Word64)
8690
import Network.TypedProtocol.Core
8791

8892

93+
-- all for MempoolTimeoutSoftPredicate
94+
import qualified Cardano.Ledger.Conway.Rules as Conway
95+
import qualified Cardano.Ledger.Core as SL (EraRule)
96+
import qualified Cardano.Ledger.Shelley.API as SL (ApplyTxError (..))
97+
import qualified Data.SOP as SOP
98+
import Ouroboros.Consensus.Byron.Ledger.Block as Consensus
99+
import Ouroboros.Consensus.HardFork.Combinator as Consensus
100+
import Ouroboros.Consensus.Shelley.Ledger.Block as Consensus
101+
import Ouroboros.Consensus.TypeFamilyWrappers as Consensus
102+
89103
instance (LogFormatting adr, Show adr) => LogFormatting (ConnectionId adr) where
90104
forMachine _dtal (ConnectionId local' remote) =
91105
mconcat [ "connectionId" .= String (showT local'
@@ -1247,19 +1261,55 @@ txsMempoolTimeoutSoftCounterName :: Text.Text
12471261
txsMempoolTimeoutSoftCounterName = "txsMempoolTimeoutSoft"
12481262

12491263
impliesMempoolTimeoutSoft ::
1250-
LedgerSupportsMempool blk => TraceEventMempool blk -> Bool
1264+
forall blk. MempoolTimeoutSoftPredicate blk => TraceEventMempool blk -> Bool
12511265
impliesMempoolTimeoutSoft = \case
12521266
TraceMempoolRejectedTx _tx txApplyErr _mpSz ->
1253-
-- TODO export a proper predicate from Consensus
1254-
"ApplyTxError (ConwayMempoolFailure" `List.isPrefixOf` show txApplyErr
1267+
errImpliesMempoolTimeoutSoft (Proxy @blk) txApplyErr
12551268
_ -> False
12561269

1270+
class MempoolTimeoutSoftPredicate blk where
1271+
-- | Does the error indicate a mempool timeout
1272+
errImpliesMempoolTimeoutSoft :: proxy blk -> ApplyTxErr blk -> Bool
1273+
1274+
instance SOP.All MempoolTimeoutSoftPredicate xs => MempoolTimeoutSoftPredicate (Consensus.HardForkBlock xs) where
1275+
errImpliesMempoolTimeoutSoft _prx = \case
1276+
Consensus.HardForkApplyTxErrWrongEra{} -> False
1277+
Consensus.HardForkApplyTxErrFromEra (Consensus.OneEraApplyTxErr ns) ->
1278+
SOP.hcollapse $ SOP.hcmap (Proxy @MempoolTimeoutSoftPredicate) f ns
1279+
where
1280+
f :: forall x. MempoolTimeoutSoftPredicate x => Consensus.WrapApplyTxErr x -> SOP.K Bool x
1281+
f (Consensus.WrapApplyTxErr err) = SOP.K $ errImpliesMempoolTimeoutSoft (Proxy @x) err
1282+
1283+
instance MempoolTimeoutSoftPredicate Consensus.ByronBlock where
1284+
errImpliesMempoolTimeoutSoft = \_prx _err -> False
1285+
1286+
instance EraMempoolTimeoutSoftPredicate era => MempoolTimeoutSoftPredicate (Consensus.ShelleyBlock proto era) where
1287+
errImpliesMempoolTimeoutSoft _prx = \case
1288+
SL.ApplyTxError (err NE.:| errs) ->
1289+
null errs && eraImpliesMempoolTimeoutSoft (Proxy @era) err
1290+
1291+
class EraMempoolTimeoutSoftPredicate era where
1292+
-- | Does the error indicate a mempool timeout
1293+
eraImpliesMempoolTimeoutSoft :: proxy era -> Conway.PredicateFailure (SL.EraRule "LEDGER" era) -> Bool
1294+
1295+
instance EraMempoolTimeoutSoftPredicate ShelleyEra where eraImpliesMempoolTimeoutSoft _prx _err = False
1296+
instance EraMempoolTimeoutSoftPredicate AllegraEra where eraImpliesMempoolTimeoutSoft _prx _err = False
1297+
instance EraMempoolTimeoutSoftPredicate MaryEra where eraImpliesMempoolTimeoutSoft _prx _err = False
1298+
instance EraMempoolTimeoutSoftPredicate AlonzoEra where eraImpliesMempoolTimeoutSoft _prx _err = False
1299+
instance EraMempoolTimeoutSoftPredicate BabbageEra where eraImpliesMempoolTimeoutSoft _prx _err = False
1300+
instance EraMempoolTimeoutSoftPredicate ConwayEra where
1301+
eraImpliesMempoolTimeoutSoft _prx = \case
1302+
Conway.ConwayMempoolFailure txt -> Text.pack "MempoolTxTooSlow" `Text.isPrefixOf` txt
1303+
_ -> False
1304+
instance EraMempoolTimeoutSoftPredicate DijkstraEra where eraImpliesMempoolTimeoutSoft _prx _err = False
1305+
12571306
instance
12581307
( LogFormatting (ApplyTxErr blk)
12591308
, LogFormatting (GenTx blk)
12601309
, ToJSON (GenTxId blk)
12611310
, LedgerSupportsMempool blk
12621311
, ConvertRawHash blk
1312+
, MempoolTimeoutSoftPredicate blk
12631313
) => LogFormatting (TraceEventMempool blk) where
12641314
forMachine dtal (TraceMempoolAddedTx tx _mpSzBefore mpSzAfter) =
12651315
mconcat

cardano-node/src/Cardano/Tracing/Tracers.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ mkConsensusTracers
758758
, ToObject (ValidationErr (BlockProtocol blk))
759759
, ToObject (ForgeStateUpdateError blk)
760760
, Consensus.RunNode blk
761+
, ConsensusTracers.MempoolTimeoutSoftPredicate blk
761762
, HasKESMetricsData blk
762763
, HasKESInfo blk
763764
)
@@ -1272,7 +1273,7 @@ notifyBlockForging fStats tr = Tracer $ \case
12721273
--------------------------------------------------------------------------------
12731274

12741275
notifyTxsMempoolTimeoutSoft ::
1275-
LedgerSupportsMempool blk
1276+
ConsensusTracers.MempoolTimeoutSoftPredicate blk
12761277
=> Maybe EKGDirect
12771278
-> Tracer IO (TraceEventMempool blk)
12781279
notifyTxsMempoolTimeoutSoft mbEKGDirect = case mbEKGDirect of
@@ -1323,6 +1324,7 @@ mempoolTracer
13231324
, ToObject (ApplyTxErr blk)
13241325
, ToObject (GenTx blk)
13251326
, LedgerSupportsMempool blk
1327+
, ConsensusTracers.MempoolTimeoutSoftPredicate blk
13261328
, ConvertRawHash blk
13271329
)
13281330
=> Maybe EKGDirect

0 commit comments

Comments
 (0)