Skip to content

Commit

Permalink
wip 3: rename checkSecurityParamConsistency to configAllSecurityParam…
Browse files Browse the repository at this point in the history
…s but add a checkSecurityParamConsistency that actually checks security params are consistent
  • Loading branch information
fraser-iohk committed Feb 7, 2024
1 parent 438121b commit ff8db93
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ instance BlockSupportsMetrics ByronBlock where
isSelfIssued = isSelfIssuedConstUnknown

instance BlockSupportsSanityCheck ByronBlock where
checkSecurityParamConsistency =
configAllSecurityParams =
pure . pbftSecurityParam . pbftParams . topLevelConfigProtocol

instance RunNode ByronBlock
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ instance ShelleyCompatible proto era => BlockSupportsMetrics (ShelleyBlock proto

instance ProtocolConfigHasSecurityParam proto
=> BlockSupportsSanityCheck (ShelleyBlock proto era) where
checkSecurityParamConsistency = pure . protocolConfigSecurityParam . topLevelConfigProtocol
configAllSecurityParams = pure . protocolConfigSecurityParam . topLevelConfigProtocol

instance
( ShelleyCompatible proto era
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,6 @@ instance BlockSupportsMetrics DualByronBlock where
isSelfIssued = isSelfIssuedConstUnknown

instance BlockSupportsSanityCheck DualByronBlock where
checkSecurityParamConsistency = pure . protocolConfigSecurityParam . topLevelConfigProtocol
configAllSecurityParams = pure . protocolConfigSecurityParam . topLevelConfigProtocol

instance RunNode DualByronBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Patch
- A bullet item for the Patch category.
-->

### Non-Breaking

- Adds a Tracer for startup sanity check warnings in Ouroboros.Consensus.Node.Tracers (see BlockSupportsSanityCheck in ouroboros-consensus)


<!--
### Breaking
- A bullet item for the Breaking category.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ library
Ouroboros.Consensus.Node.ExitPolicy
Ouroboros.Consensus.Node.Recovery
Ouroboros.Consensus.Node.RethrowPolicy
Ouroboros.Consensus.Node.StartupWarning
Ouroboros.Consensus.Node.Tracers
Ouroboros.Consensus.NodeKernel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Encoding as CBOR
import Codec.Serialise (DeserialiseFailure)
import Control.DeepSeq (NFData)
import Control.Monad (forM_)
import Control.Monad.Class.MonadTime.SI (MonadTime)
import Control.Monad.Class.MonadTimer.SI (MonadTimer)
import Control.Tracer (Tracer, contramap, traceWith)
import Data.ByteString.Lazy (ByteString)
import Data.Functor.Contravariant (Predicate (..))
import Data.Hashable (Hashable)
import Data.List.NonEmpty (NonEmpty(..))
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, isNothing)
Expand All @@ -88,7 +88,6 @@ import Ouroboros.Consensus.Node.ProtocolInfo
import Ouroboros.Consensus.Node.Recovery
import Ouroboros.Consensus.Node.RethrowPolicy
import Ouroboros.Consensus.Node.Run
import Ouroboros.Consensus.Node.StartupWarning
import Ouroboros.Consensus.Node.Tracers
import Ouroboros.Consensus.NodeKernel
import Ouroboros.Consensus.Storage.ChainDB (ChainDB, ChainDbArgs)
Expand Down Expand Up @@ -366,11 +365,8 @@ runWith RunNodeArgs{..} encAddrNtN decAddrNtN LowLevelRunNodeArgs{..} =
, ChainDB.cdbVolatileDbValidation = ValidateAll
}

case checkSecurityParamConsistency cfg of
_ :| [] -> pure ()
ks@(_ :| _) ->
traceWith (consensusSanityCheckTracer rnTraceConsensus) $
InconsistentSecurityParam ks
forM_ (sanityCheckConfig cfg) $ \issue ->
traceWith (consensusSanityCheckTracer rnTraceConsensus) issue

chainDB <- openChainDB registry inFuture cfg initLedger
llrnChainDbArgsDefaults customiseChainDbArgs'
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ import Ouroboros.Network.TxSubmission.Inbound
(TraceTxSubmissionInbound)
import Ouroboros.Network.TxSubmission.Outbound
(TraceTxSubmissionOutbound)
import Ouroboros.Consensus.Node.StartupWarning
(StartupWarning)

{-------------------------------------------------------------------------------
All tracers of a node bundled together
Expand All @@ -63,7 +61,7 @@ data Tracers' remotePeer localPeer blk f = Tracers
, blockchainTimeTracer :: f (TraceBlockchainTimeEvent UTCTime)
, forgeStateInfoTracer :: f (TraceLabelCreds (ForgeStateInfo blk))
, keepAliveClientTracer :: f (TraceKeepAliveClient remotePeer)
, consensusSanityCheckTracer :: f StartupWarning
, consensusSanityCheckTracer :: f SanityCheckIssue
, consensusErrorTracer :: f SomeException
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import Test.ThreadNet.Util.NodeToNodeVersion
import Test.ThreadNet.Util.NodeTopology
import Test.ThreadNet.Util.Seed
import Test.Util.HardFork.Future
import Test.Util.SanityCheck (prop_sanityChecks)
import Test.Util.Slots (NumSlots (..))
import Test.Util.Time (dawnOfTime)

Expand Down Expand Up @@ -138,6 +139,7 @@ prop_simple_hfc_convergence testSetup@TestSetup{..} =
counterexample ("eraSizeA: " <> show eraSizeA) $
tabulate "epochs in era A" [labelEraSizeA] $
prop_general args testOutput .&&.
prop_sanityChecks (topLevelConfig (CoreNodeId 0)) .&&.
prop_allExpectedBlocks
where
k :: SecurityParam
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Patch
- A bullet item for the Patch category.
-->
### Non-Breaking

- Add BlockSupportsSanityCheck to check for common configuration issues which may manifest themselves in unusual but not necessarily immediately obvious ways

<!--
### Breaking
- A bullet item for the Breaking category.
-->
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
module Ouroboros.Consensus.Block.SupportsSanityCheck
( BlockSupportsSanityCheck(..)
( SanityCheckIssue(..)
, BlockSupportsSanityCheck(..)
, checkSecurityParamConsistency
, sanityCheckConfig
, ProtocolConfigHasSecurityParam(..)
) where

import Control.Exception
import Data.Maybe (catMaybes)
import Ouroboros.Consensus.Config (TopLevelConfig)
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty (NonEmpty(..))
import Ouroboros.Consensus.Config.SecurityParam
import Ouroboros.Consensus.Protocol.Abstract

data SanityCheckIssue
= InconsistentSecurityParam (NonEmpty SecurityParam)
deriving (Show, Eq)

instance Exception SanityCheckIssue

class BlockSupportsSanityCheck blk where
checkSecurityParamConsistency
configAllSecurityParams
:: TopLevelConfig blk
-> NonEmpty SecurityParam

checkSecurityParamConsistency
:: BlockSupportsSanityCheck blk
=> TopLevelConfig blk
-> Maybe SanityCheckIssue
checkSecurityParamConsistency cfg = do
let allParams = configAllSecurityParams cfg
if allSame allParams
then Nothing
else Just (InconsistentSecurityParam allParams)

allSame :: Eq a => NonEmpty a -> Bool
allSame (x :| xs) = all (x ==) xs

sanityCheckConfig
:: BlockSupportsSanityCheck blk
=> TopLevelConfig blk
-> [SanityCheckIssue]
sanityCheckConfig cfg =
catMaybes [checkSecurityParamConsistency cfg]

class ProtocolConfigHasSecurityParam p where
protocolConfigSecurityParam :: ConsensusConfig p -> SecurityParam
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import Data.SOP.Strict
import Ouroboros.Consensus.HardFork.History.EpochInfo

instance CanHardFork xs => BlockSupportsSanityCheck (HardForkBlock xs) where
checkSecurityParamConsistency tlc = do
configAllSecurityParams tlc = do
let configProtocol = topLevelConfigProtocol tlc
hardForkConsensusConfigK configProtocol :|
perEraConsensusConfigSecurityParams (hardForkConsensusConfigPerEra configProtocol)

perEraConsensusConfigSecurityParams :: All SingleEraBlock xs
=> PerEraConsensusConfig xs -> [SecurityParam]
perEraConsensusConfigSecurityParams
:: All SingleEraBlock xs
=> PerEraConsensusConfig xs -> [SecurityParam]
perEraConsensusConfigSecurityParams (PerEraConsensusConfig xs) =
unK $ hctraverse_ (Proxy @SingleEraBlock) go xs
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import Ouroboros.Consensus.Block.SupportsSanityCheck
import Ouroboros.Consensus.Config
import Test.Util.Orphans.Arbitrary ()
import Test.Tasty.QuickCheck
import Data.List.NonEmpty (nub)

prop_sanityChecks
:: BlockSupportsSanityCheck blk
=> TopLevelConfig blk -> Property
prop_sanityChecks cfg =
prop_securityParamConsistent cfg
sanityCheckConfig cfg === []

prop_securityParamConsistent
:: BlockSupportsSanityCheck blk
=> TopLevelConfig blk -> Property
prop_securityParamConsistent cfg =
length (nub (checkSecurityParamConsistency cfg)) === 1
checkSecurityParamConsistency cfg === Nothing

Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ import Codec.Serialise (Serialise)
import qualified Data.Map.Strict as Map
import Data.Void (Void)
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Block.SupportsSanityCheck
import Ouroboros.Consensus.Config
import Ouroboros.Consensus.Ledger.SupportsMempool (txForgetValidated)
import Ouroboros.Consensus.Ledger.SupportsProtocol
import Ouroboros.Consensus.Mock.Ledger
import Ouroboros.Consensus.Mock.Ledger.Block.BFT
import Ouroboros.Consensus.Mock.Ledger.Block.PBFT
import Ouroboros.Consensus.Mock.Node.Abstract
import Ouroboros.Consensus.Mock.Node.Serialisation ()
import Ouroboros.Consensus.Mock.Protocol.Praos
import Ouroboros.Consensus.Node.InitStorage
import Ouroboros.Consensus.Node.NetworkProtocolVersion
import Ouroboros.Consensus.Node.Run
Expand Down Expand Up @@ -61,7 +57,7 @@ instance BlockSupportsMetrics (SimpleBlock c ext) where

instance ProtocolConfigHasSecurityParam (BlockProtocol (SimpleBlock c ext))
=> BlockSupportsSanityCheck (SimpleBlock c ext) where
checkSecurityParamConsistency =
configAllSecurityParams =
pure . protocolConfigSecurityParam . topLevelConfigProtocol

instance ( LedgerSupportsProtocol (SimpleBlock SimpleMockCrypto ext)
Expand Down

0 comments on commit ff8db93

Please sign in to comment.