cooked-validators
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cooked.MockChain.BlockChain

Description

This modules provides a specification for our blockchain monads, in three layers:

  1. MonadBlockChainBalancing provides what's needing for balancing purposes
  2. MonadBlockChainWithoutValidation adds up remaining primitives without transaction validation
  3. MonadBlockChain concludes with the addition of transaction validation, thus modifying the internal index of outputs

In addition, you will find here many helpers functions which can be derived from the core definition of our blockchain.

Synopsis

Documentation

data MockChainError Source #

Errors that can be produced by the blockchain

Constructors

MCEValidationError ValidationPhase ValidationError

Validation errors, either in Phase 1 or Phase 2

MCEUnbalanceable Wallet Value

The balancing wallet does not have enough funds

MCEMissingBalancingWallet String

The balancing wallet is required but missing

MCENoSuitableCollateral Integer Integer Value

No suitable collateral could be associated with a skeleton

MCEToCardanoError String ToCardanoError

Translating a skeleton element to its Cardano counterpart failed

MCEWrongReferenceScriptError TxOutRef ScriptHash (Maybe ScriptHash)

The required reference script is missing from a witness utxo

MCEUnknownOutRef TxOutRef

A UTxO is missing from the mockchain state

MCEPastSlot Slot Slot

A jump in time would result in a past slot

MCEUnsupportedFeature String

An attempt to invoke an unsupported feature has been made

FailWith String

Used to provide MonadFail instances.

data MockChainLogEntry Source #

This represents the specific events that should be logged when processing transactions. If a new kind of event arises, then a new constructor should be provided here.

Constructors

MCLogSubmittedTxSkel TxSkel

Logging a Skeleton as it is submitted by the user.

MCLogAdjustedTxSkel TxSkel Integer (Maybe (Set TxOutRef, Wallet))

Logging a Skeleton as it has been adjusted by the balancing mechanism, alongside fee, and possible collateral utxos and return collateral wallet.

MCLogNewTx TxId Integer

Logging the successful validation of a new transaction, with its id and number of produced outputs.

MCLogDiscardedUtxos Integer String

Logging the fact that utxos provided by the user for balancing have to be discarded for a specific reason.

MCLogUnusedCollaterals (Either Wallet (Set TxOutRef))

Logging the fact that utxos provided as collaterals will not be used because the transaction does not involve scripts. There are 2 cases, depending on whether the user has provided an explicit wallet or a set of utxos to be used as collaterals.

MCLogAddedReferenceScript TxSkelRedeemer TxOutRef ScriptHash

Logging the automatic addition of a reference script

MCLogAdjustedTxSkelOut TxSkelOut Lovelace

Logging the automatic adjusment of a min ada amount

class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where Source #

This is the first layer of our blockchain, which provides the minimal subset of primitives required to perform balancing.

Methods

getParams :: m Params Source #

Returns the emulator parameters, including protocol parameters

utxosAt :: ToAddress a => a -> m [(TxOutRef, TxSkelOut)] Source #

Returns a list of all UTxOs at a certain address.

txOutByRef :: TxOutRef -> m (Maybe TxSkelOut) Source #

Returns an output given a reference to it

logEvent :: MockChainLogEntry -> m () Source #

Logs an event that occured during a BlockChain run

Instances

Instances details
MonadBlockChainBalancing StagedMockChain Source # 
Instance details

Defined in Cooked.MockChain.Staged

Monad m => MonadBlockChainBalancing (MockChainT m) Source # 
Instance details

Defined in Cooked.MockChain.Direct

MonadBlockChainBalancing m => MonadBlockChainBalancing (ListT m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

MonadBlockChainBalancing m => MonadBlockChainBalancing (ReaderT r m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

MonadBlockChainBalancing m => MonadBlockChainBalancing (StateT s m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

(Monoid w, MonadBlockChainBalancing m) => MonadBlockChainBalancing (WriterT w m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

(MonadTrans t, MonadBlockChainBalancing m, Monad (t m), MonadError MockChainError (AsTrans t m)) => MonadBlockChainBalancing (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

class MonadBlockChainBalancing m => MonadBlockChainWithoutValidation m where Source #

This is the second layer of our blockchain, which provides all the other blockchain primitives not needed for balancing, except transaction validation. This layers is the one where Tweaks are plugged to.

Methods

allUtxos :: m [(TxOutRef, TxSkelOut)] Source #

Returns a list of all currently known outputs.

setParams :: Params -> m () Source #

Updates parameters

waitNSlots :: Integral i => i -> m Slot Source #

Wait a certain amount of slot. Throws MCEPastSlot if the input integer is negative. Returns the slot after jumping in time.

define :: ToHash a => String -> a -> m a Source #

Binds a hashable quantity of type a to a variable in the mockchain, while registering its alias for printing purposes.

setConstitutionScript :: ToVersioned Script s => s -> m () Source #

Sets the current script to act as the official constitution script

getConstitutionScript :: m (Maybe (Versioned Script)) Source #

Gets the current official constitution script

registerStakingCred :: ToCredential c => c -> Integer -> Integer -> m () Source #

Registers a staking credential with a given reward and deposit

Instances

Instances details
MonadBlockChainWithoutValidation StagedMockChain Source # 
Instance details

Defined in Cooked.MockChain.Staged

Monad m => MonadBlockChainWithoutValidation (MockChainT m) Source # 
Instance details

Defined in Cooked.MockChain.Direct

MonadBlockChainWithoutValidation m => MonadBlockChainWithoutValidation (ListT m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

MonadBlockChainWithoutValidation m => MonadBlockChainWithoutValidation (ReaderT r m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

MonadBlockChainWithoutValidation m => MonadBlockChainWithoutValidation (StateT s m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

(Monoid w, MonadBlockChainWithoutValidation m) => MonadBlockChainWithoutValidation (WriterT w m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

(MonadTrans t, MonadBlockChainWithoutValidation m, Monad (t m), MonadError MockChainError (AsTrans t m)) => MonadBlockChainWithoutValidation (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

class MonadBlockChainWithoutValidation m => MonadBlockChain m where Source #

The final layer of our blockchain, adding transaction validation to the mix. This is the only primitive that actually modifies the ledger state.

Methods

validateTxSkel :: TxSkel -> m CardanoTx Source #

Generates, balances and validates a transaction from a skeleton. It returns the validated transaction and updates the state of the blockchain.

newtype AsTrans t (m :: Type -> Type) a Source #

A newtype wrapper to be used with '-XDerivingVia' to derive instances of MonadBlockChain for any MonadTransControl.

For example, to derive 'MonadBlockChain m => MonadBlockChain (ReaderT r m)', you'd write

deriving via (AsTrans (ReaderT r) m) instance MonadBlockChain m => MonadBlockChain (ReaderT r m)

and avoid the trouble of defining all the class methods yourself.

Constructors

AsTrans 

Fields

Instances

Instances details
(MonadTransControl t, MonadError MockChainError m, Monad (t m)) => MonadError MockChainError (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Methods

throwError :: MockChainError -> AsTrans t m a #

catchError :: AsTrans t m a -> (MockChainError -> AsTrans t m a) -> AsTrans t m a #

MonadTransControl t => MonadTransControl (AsTrans t) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Associated Types

type StT (AsTrans t) a #

Methods

liftWith :: Monad m => (Run (AsTrans t) -> m a) -> AsTrans t m a #

restoreT :: Monad m => m (StT (AsTrans t) a) -> AsTrans t m a #

MonadTrans t => MonadTrans (AsTrans t) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Methods

lift :: Monad m => m a -> AsTrans t m a #

(MonadTrans t, MonadFail m, Monad (t m)) => MonadFail (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Methods

fail :: String -> AsTrans t m a #

Applicative (t m) => Applicative (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Methods

pure :: a -> AsTrans t m a #

(<*>) :: AsTrans t m (a -> b) -> AsTrans t m a -> AsTrans t m b #

liftA2 :: (a -> b -> c) -> AsTrans t m a -> AsTrans t m b -> AsTrans t m c #

(*>) :: AsTrans t m a -> AsTrans t m b -> AsTrans t m b #

(<*) :: AsTrans t m a -> AsTrans t m b -> AsTrans t m a #

Functor (t m) => Functor (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Methods

fmap :: (a -> b) -> AsTrans t m a -> AsTrans t m b #

(<$) :: a -> AsTrans t m b -> AsTrans t m a #

Monad (t m) => Monad (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

Methods

(>>=) :: AsTrans t m a -> (a -> AsTrans t m b) -> AsTrans t m b #

(>>) :: AsTrans t m a -> AsTrans t m b -> AsTrans t m b #

return :: a -> AsTrans t m a #

(MonadTrans t, MonadBlockChain m, MonadBlockChainWithoutValidation (AsTrans t m)) => MonadBlockChain (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

(MonadTrans t, MonadBlockChainBalancing m, Monad (t m), MonadError MockChainError (AsTrans t m)) => MonadBlockChainBalancing (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

(MonadTrans t, MonadBlockChainWithoutValidation m, Monad (t m), MonadError MockChainError (AsTrans t m)) => MonadBlockChainWithoutValidation (AsTrans t m) Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

type StT (AsTrans t) a Source # 
Instance details

Defined in Cooked.MockChain.BlockChain

type StT (AsTrans t) a = StT t a

currentMSRange :: MonadBlockChainWithoutValidation m => m (POSIXTime, POSIXTime) Source #

Returns the closed ms interval corresponding to the current slot

utxosFromCardanoTx :: MonadBlockChainBalancing m => CardanoTx -> m [(TxOutRef, TxSkelOut)] Source #

Retrieves the ordered list of outputs of the given CardanoTx.

This is useful when writing endpoints and/or traces to fetch utxos of interest right from the start and avoid querying the chain for them afterwards using allUtxos or similar functions.

unsafeTxOutByRef :: MonadBlockChainBalancing m => TxOutRef -> m TxSkelOut Source #

Same as txOutByRef but throws an error in case of missing utxo. Use this when you know the utxo is present, or when you want an error to be throw when it's not.

typedDatumFromTxOutRef :: (DatumConstrs a, MonadBlockChainBalancing m) => TxOutRef -> m (Maybe a) Source #

Like datumFromTxOutRef, but uses fromBuiltinData to attempt to deserialize this datum into a given type

valueFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe Value) Source #

Resolves an TxOutRef and extracts the value it contains

currentSlot :: MonadBlockChainWithoutValidation m => m Slot Source #

Returns the current slot number

awaitSlot :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m Slot Source #

Wait for a certain slot, or throws an error if the slot is already past

getEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot Source #

Return the slot that contains the given time. See slotToMSRange for some satisfied equational properties.

awaitEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot Source #

Waits until the current slot becomes greater or equal to the slot containing the given POSIX time. Note that that it might not wait for anything if the current slot is large enough.

waitNMSFromSlotLowerBound :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m Slot Source #

Wait a given number of ms from the lower bound of the current slot and returns the current slot after waiting.

waitNMSFromSlotUpperBound :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m Slot Source #

Wait a given number of ms from the upper bound of the current slot and returns the current slot after waiting.

slotRangeBefore :: MonadBlockChainWithoutValidation m => POSIXTime -> m SlotRange Source #

The infinite range of slots ending before or at the given time

slotRangeAfter :: MonadBlockChainWithoutValidation m => POSIXTime -> m SlotRange Source #

The infinite range of slots starting after or at the given time

slotToMSRange :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m (POSIXTime, POSIXTime) Source #

Returns the closed ms interval corresponding to the slot with the given number. It holds that

slotToMSRange (getEnclosingSlot t) == (a, b)    ==>   a <= t <= b

and

slotToMSRange n == (a, b)   ==>   getEnclosingSlot a == n && getEnclosingSlot b == n

and

slotToMSRange n == (a, b)   ==>   getEnclosingSlot (a-1) == n-1 && getEnclosingSlot (b+1) == n+1

txSkelInputValidators :: MonadBlockChainBalancing m => TxSkel -> m [Versioned Validator] Source #

Returns all validators which guard transaction inputs

txSkelInputValue :: MonadBlockChainBalancing m => TxSkel -> m Value Source #

look up the UTxOs the transaction consumes, and sum their values.

lookupUtxos :: MonadBlockChainBalancing m => [TxOutRef] -> m (Map TxOutRef TxSkelOut) Source #

Go through all of the TxOutRefs in the list and look them up in the state of the blockchain, throwing an error if one of them cannot be resolved.

validateTxSkel' :: MonadBlockChain m => TxSkel -> m [TxOutRef] Source #

Validates a skeleton, and retuns the ordered list of produced output references

validateTxSkel_ :: MonadBlockChain m => TxSkel -> m () Source #

Validates a skeleton, and erases the outputs

txSkelProposalsDeposit :: MonadBlockChainBalancing m => TxSkel -> m Lovelace Source #

Retrieves the total amount of lovelace deposited in proposals in this skeleton (equal to govActionDeposit times the number of proposals).

govActionDeposit :: MonadBlockChainBalancing m => m Lovelace Source #

Retrieves the required deposit amount for issuing governance actions.

defineM :: (MonadBlockChainWithoutValidation m, ToHash a) => String -> m a -> m a Source #

Like define, but binds the result of a monadic computation instead

unsafeOutputDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m OutputDatum Source #

Same as outputDatumFromTxOutRef but throws an error when the utxo is missing

unsafeDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m TxSkelOutDatum Source #

Same as datumFromTxOutRef but throws an error when the utxo is missing

unsafeTypedDatumFromTxOutRef :: (DatumConstrs a, MonadBlockChainBalancing m) => TxOutRef -> m (Maybe a) Source #

Like typedDatumFromTxOutRef but throws an error when the utxo is missing

unsafeValueFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m Value Source #

Same as valueFromTxOutRef but throws an error when the utxo is missing

txSkelAllScripts :: MonadBlockChainBalancing m => TxSkel -> m [Versioned Script] Source #

Returns all scripts involved in this TxSkel