Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Cooked.MockChain.BlockChain
Description
This modules provides a specification for our blockchain monads, in three layers:
- MonadBlockChainBalancing provides what's needing for balancing purposes
- MonadBlockChainWithoutValidation adds up remaining primitives without transaction validation
- 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
- data MockChainError
- = MCEValidationError ValidationPhase ValidationError
- | MCEUnbalanceable Wallet Value
- | MCEMissingBalancingWallet String
- | MCENoSuitableCollateral Integer Integer Value
- | MCEToCardanoError String ToCardanoError
- | MCEWrongReferenceScriptError TxOutRef ScriptHash (Maybe ScriptHash)
- | MCEUnknownOutRef TxOutRef
- | MCEPastSlot Slot Slot
- | MCEUnsupportedFeature String
- | FailWith String
- data MockChainLogEntry
- = MCLogSubmittedTxSkel TxSkel
- | MCLogAdjustedTxSkel TxSkel Integer (Maybe (Set TxOutRef, Wallet))
- | MCLogNewTx TxId Integer
- | MCLogDiscardedUtxos Integer String
- | MCLogUnusedCollaterals (Either Wallet (Set TxOutRef))
- | MCLogAddedReferenceScript TxSkelRedeemer TxOutRef ScriptHash
- | MCLogAdjustedTxSkelOut TxSkelOut Lovelace
- class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where
- class MonadBlockChainBalancing m => MonadBlockChainWithoutValidation m where
- allUtxos :: m [(TxOutRef, TxSkelOut)]
- setParams :: Params -> m ()
- waitNSlots :: Integral i => i -> m Slot
- define :: ToHash a => String -> a -> m a
- setConstitutionScript :: ToVersioned Script s => s -> m ()
- getConstitutionScript :: m (Maybe (Versioned Script))
- registerStakingCred :: ToCredential c => c -> Integer -> Integer -> m ()
- class MonadBlockChainWithoutValidation m => MonadBlockChain m where
- validateTxSkel :: TxSkel -> m CardanoTx
- newtype AsTrans t (m :: Type -> Type) a = AsTrans {
- getTrans :: t m a
- currentMSRange :: MonadBlockChainWithoutValidation m => m (POSIXTime, POSIXTime)
- utxosFromCardanoTx :: MonadBlockChainBalancing m => CardanoTx -> m [(TxOutRef, TxSkelOut)]
- unsafeTxOutByRef :: MonadBlockChainBalancing m => TxOutRef -> m TxSkelOut
- typedDatumFromTxOutRef :: (DatumConstrs a, MonadBlockChainBalancing m) => TxOutRef -> m (Maybe a)
- valueFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe Value)
- outputDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe OutputDatum)
- datumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe TxSkelOutDatum)
- currentSlot :: MonadBlockChainWithoutValidation m => m Slot
- awaitSlot :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m Slot
- getEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot
- awaitEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot
- waitNMSFromSlotLowerBound :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m Slot
- waitNMSFromSlotUpperBound :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m Slot
- slotRangeBefore :: MonadBlockChainWithoutValidation m => POSIXTime -> m SlotRange
- slotRangeAfter :: MonadBlockChainWithoutValidation m => POSIXTime -> m SlotRange
- slotToMSRange :: (MonadBlockChainWithoutValidation m, Integral i) => i -> m (POSIXTime, POSIXTime)
- txSkelInputValidators :: MonadBlockChainBalancing m => TxSkel -> m [Versioned Validator]
- txSkelInputValue :: MonadBlockChainBalancing m => TxSkel -> m Value
- lookupUtxos :: MonadBlockChainBalancing m => [TxOutRef] -> m (Map TxOutRef TxSkelOut)
- validateTxSkel' :: MonadBlockChain m => TxSkel -> m [TxOutRef]
- validateTxSkel_ :: MonadBlockChain m => TxSkel -> m ()
- txSkelProposalsDeposit :: MonadBlockChainBalancing m => TxSkel -> m Lovelace
- govActionDeposit :: MonadBlockChainBalancing m => m Lovelace
- defineM :: (MonadBlockChainWithoutValidation m, ToHash a) => String -> m a -> m a
- unsafeOutputDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m OutputDatum
- unsafeDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m TxSkelOutDatum
- unsafeTypedDatumFromTxOutRef :: (DatumConstrs a, MonadBlockChainBalancing m) => TxOutRef -> m (Maybe a)
- unsafeValueFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m Value
- txSkelAllScripts :: MonadBlockChainBalancing m => TxSkel -> m [Versioned Script]
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 |
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 |
Instances
Show MockChainLogEntry Source # | |
Defined in Cooked.MockChain.BlockChain Methods showsPrec :: Int -> MockChainLogEntry -> ShowS # show :: MockChainLogEntry -> String # showList :: [MockChainLogEntry] -> ShowS # | |
PrettyCooked (Contextualized MockChainLogEntry) Source # | This prints a |
Defined in Cooked.Pretty.MockChain | |
PrettyCooked (Contextualized [MockChainLogEntry]) Source # | |
Defined in Cooked.Pretty.MockChain Methods prettyCookedOpt :: PrettyCookedOpts -> Contextualized [MockChainLogEntry] -> DocCooked Source # prettyCooked :: Contextualized [MockChainLogEntry] -> DocCooked Source # |
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
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
Tweak
s 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
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.
Instances
MonadBlockChain StagedMockChain Source # | |
Defined in Cooked.MockChain.Staged Methods validateTxSkel :: TxSkel -> StagedMockChain CardanoTx Source # | |
Monad m => MonadBlockChain (MockChainT m) Source # | Most of the logic of the direct emulation happens here |
Defined in Cooked.MockChain.Direct Methods validateTxSkel :: TxSkel -> MockChainT m CardanoTx Source # | |
MonadBlockChain m => MonadBlockChain (ListT m) Source # | |
Defined in Cooked.MockChain.BlockChain | |
MonadBlockChain m => MonadBlockChain (ReaderT r m) Source # | |
Defined in Cooked.MockChain.BlockChain | |
MonadBlockChain m => MonadBlockChain (StateT s m) Source # | |
Defined in Cooked.MockChain.BlockChain | |
(Monoid w, MonadBlockChain m) => MonadBlockChain (WriterT w m) Source # | |
Defined in Cooked.MockChain.BlockChain | |
(MonadTrans t, MonadBlockChain m, MonadBlockChainWithoutValidation (AsTrans t m)) => MonadBlockChain (AsTrans t m) Source # | |
Defined in Cooked.MockChain.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.
Instances
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 #
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
outputDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe OutputDatum) Source #
Extracts a potential OutputDatum
from a given TxOutRef
datumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe TxSkelOutDatum) Source #
Extracts a potential TxSkelOutDatum
from a given TxOutRef
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 TxOutRef
s 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