This repository has been archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#526] Add BlockValidationMode data type
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |