Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
[#526] Add BlockValidationMode data type
Browse files Browse the repository at this point in the history
  • Loading branch information
intricate committed Jun 18, 2019
1 parent dafcecd commit 4b788bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions cardano-ledger/cardano-ledger.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ library
Cardano.Chain.Block.Header
Cardano.Chain.Block.Proof
Cardano.Chain.Block.Validation
Cardano.Chain.Block.ValidationMode

Cardano.Chain.Common.AddrAttributes
Cardano.Chain.Common.AddrSpendingData
Expand Down
50 changes: 50 additions & 0 deletions cardano-ledger/src/Cardano/Chain/Block/ValidationMode.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{-# LANGUAGE FlexibleContexts #-}

module Cardano.Chain.Block.ValidationMode
( BlockValidationMode (..)
, orThrowErrorBVM
, toTxValidationMode
, whenBlockValidation
) where

import Cardano.Prelude

import Cardano.Chain.UTxO.ValidationMode (TxValidationMode (..))

--------------------------------------------------------------------------------
-- BlockValidationMode
--------------------------------------------------------------------------------

-- | Indicates what sort of block validation should be performed.
data BlockValidationMode
= BlockValidation
-- ^ Perform all block validations.
| NoBlockValidation
-- ^ Perform no block validations.
deriving (Eq, Show)

-- | Perform an action only when in the 'BlockValidation' mode. Otherwise, do
-- nothing.
whenBlockValidation
:: MonadError err m
=> BlockValidationMode
-> m ()
-> m ()
whenBlockValidation BlockValidation action = action
whenBlockValidation _ _ = pure ()

orThrowErrorBVM
:: (MonadError e m, MonadReader BlockValidationMode m)
=> Bool
-> e
-> m ()
orThrowErrorBVM condition err = do
bvm <- ask
unless (bvm == NoBlockValidation || condition) (throwError err)

infix 1 `orThrowErrorBVM`

-- | Translate a 'BlockValidationMode' to an appropriate 'TxValidationMode'.
toTxValidationMode :: BlockValidationMode -> TxValidationMode
toTxValidationMode BlockValidation = TxValidation
toTxValidationMode NoBlockValidation = NoTxValidation

0 comments on commit 4b788bb

Please sign in to comment.