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 GenerateTxError
- data MockChainError
- = MCEValidationError ValidationPhase ValidationError
- | MCEUnbalanceable Wallet Value TxSkel
- | MCENoSuitableCollateral Integer Integer Value
- | MCEGenerationError GenerateTxError
- | MCEUnknownOutRefError String TxOutRef
- | MCEUnknownValidator String ValidatorHash
- | MCEUnknownDatum String DatumHash
- | FailWith String
- data MockChainLogEntry
- = MCLogSubmittedTxSkel (Map TxOutRef TxOut) (Map DatumHash TxSkelOutDatum) TxSkel
- | MCLogAdjustedTxSkel (Map TxOutRef TxOut) (Map DatumHash TxSkelOutDatum) TxSkel Integer (Maybe (Set TxOutRef, Wallet))
- | MCLogNewTx TxId
- | MCLogDiscardedUtxos Integer String
- | MCLogUnusedCollaterals (Either Wallet (Set TxOutRef))
- | MCLogAddedReferenceScript TxSkelRedeemer TxOutRef ScriptHash
- | MCLogAdjustedTxSkelOut TxSkelOut Lovelace
- class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where
- getParams :: m Params
- utxosAt :: Address -> m [(TxOutRef, TxOut)]
- datumFromHash :: DatumHash -> m (Maybe Datum)
- scriptFromHash :: ScriptHash -> m (Maybe (Versioned Script))
- txOutByRef :: TxOutRef -> m (Maybe TxOut)
- logEvent :: MockChainLogEntry -> m ()
- class MonadBlockChainBalancing m => MonadBlockChainWithoutValidation m where
- class MonadBlockChainWithoutValidation m => MonadBlockChain m where
- validateTxSkel :: TxSkel -> m CardanoTx
- newtype AsTrans t (m :: Type -> Type) a = AsTrans {
- getTrans :: t m a
- currentTime :: MonadBlockChainWithoutValidation m => m (POSIXTime, POSIXTime)
- waitNSlots :: MonadBlockChainWithoutValidation m => Integer -> m Slot
- utxosFromCardanoTx :: CardanoTx -> [(TxOutRef, TxOut)]
- typedDatumFromTxOutRef :: (FromData 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 Datum)
- resolveDatum :: (IsAbstractOutput out, ToOutputDatum (DatumType out), MonadBlockChainBalancing m) => out -> m (Maybe (ConcreteOutput (OwnerType out) Datum (ValueType out) (ReferenceScriptType out)))
- resolveTypedDatum :: (IsAbstractOutput out, ToOutputDatum (DatumType out), MonadBlockChainBalancing m, FromData a) => out -> m (Maybe (ConcreteOutput (OwnerType out) a (ValueType out) (ReferenceScriptType out)))
- resolveValidator :: (IsAbstractOutput out, ToCredential (OwnerType out), MonadBlockChainBalancing m) => out -> m (Maybe (ConcreteOutput (Versioned Validator) (DatumType out) (ValueType out) (ReferenceScriptType out)))
- resolveReferenceScript :: (IsAbstractOutput out, ToScriptHash (ReferenceScriptType out), MonadBlockChainBalancing m) => out -> m (Maybe (ConcreteOutput (OwnerType out) (DatumType out) (ValueType out) (Versioned Validator)))
- getEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot
- awaitEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot
- awaitDurationFromLowerBound :: MonadBlockChainWithoutValidation m => Integer -> m Slot
- awaitDurationFromUpperBound :: MonadBlockChainWithoutValidation m => Integer -> m Slot
- slotRangeBefore :: MonadBlockChainWithoutValidation m => POSIXTime -> m SlotRange
- slotRangeAfter :: MonadBlockChainWithoutValidation m => POSIXTime -> m SlotRange
- slotToTimeInterval :: MonadBlockChainWithoutValidation m => Slot -> m (POSIXTime, POSIXTime)
- txSkelInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map TxOutRef TxOut)
- txSkelReferenceInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map TxOutRef TxOut)
- txSkelInputValidators :: MonadBlockChainBalancing m => TxSkel -> m (Map ValidatorHash (Versioned Validator))
- txSkelInputValue :: MonadBlockChainBalancing m => TxSkel -> m Value
- txSkelInputDataAsHashes :: MonadBlockChainBalancing m => TxSkel -> m [DatumHash]
- lookupUtxos :: MonadBlockChainBalancing m => [TxOutRef] -> m (Map TxOutRef TxOut)
- validateTxSkel' :: MonadBlockChain m => TxSkel -> m [TxOutRef]
- validateTxSkel_ :: MonadBlockChain m => TxSkel -> m ()
- txSkelProposalsDeposit :: MonadBlockChainBalancing m => TxSkel -> m Lovelace
- govActionDeposit :: MonadBlockChainBalancing m => m Lovelace
- txOutRefToTxSkelOut :: MonadBlockChainBalancing m => TxOutRef -> Bool -> Bool -> m TxSkelOut
- txOutRefToTxSkelOut' :: MonadBlockChainBalancing m => TxOutRef -> m TxSkelOut
- defineM :: (MonadBlockChainWithoutValidation m, ToHash a) => String -> m a -> m a
Documentation
data GenerateTxError Source #
Errors that can arise during transaction generation
Constructors
ToCardanoError String ToCardanoError | Error when translating a skeleton element to its Cardano counterpart |
TxBodyError String TxBodyError | Error when generating a Cardano transaction body |
GenerateTxErrorGeneral String | Other generation error |
Instances
Show GenerateTxError Source # | |
Defined in Cooked.MockChain.BlockChain Methods showsPrec :: Int -> GenerateTxError -> ShowS # show :: GenerateTxError -> String # showList :: [GenerateTxError] -> ShowS # | |
Eq GenerateTxError Source # | |
Defined in Cooked.MockChain.BlockChain Methods (==) :: GenerateTxError -> GenerateTxError -> Bool # (/=) :: GenerateTxError -> GenerateTxError -> Bool # |
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 TxSkel | Thrown when the balancing wallet does not have enough funds |
MCENoSuitableCollateral Integer Integer Value | Thrown when not enough collateral are provided. Built upon the fee, the percentage and the expected minimal collateral value. |
MCEGenerationError GenerateTxError | Thrown when an error occured during transaction generation |
MCEUnknownOutRefError String TxOutRef | Thrown when an output reference is missing from the mockchain state |
MCEUnknownValidator String ValidatorHash | Same as |
MCEUnknownDatum String DatumHash | Same as |
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 (Map TxOutRef TxOut) (Map DatumHash TxSkelOutDatum) TxSkel | Logging a Skeleton as it is submitted by the user. |
MCLogAdjustedTxSkel (Map TxOutRef TxOut) (Map DatumHash TxSkelOutDatum) 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 | Logging the appearance of a new transaction, after a skeleton has been successfully sent for validation. |
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
PrettyCooked MockChainLogEntry Source # | This prints a |
Defined in Cooked.Pretty.MockChain Methods prettyCookedOpt :: PrettyCookedOpts -> MockChainLogEntry -> DocCooked Source # | |
PrettyCooked [MockChainLogEntry] Source # | |
Defined in Cooked.Pretty.MockChain Methods prettyCookedOpt :: PrettyCookedOpts -> [MockChainLogEntry] -> DocCooked Source # prettyCooked :: [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 :: Address -> m [(TxOutRef, TxOut)] Source #
Returns a list of all UTxOs at a certain address.
datumFromHash :: DatumHash -> m (Maybe Datum) Source #
Returns the datum with the given hash if present.
scriptFromHash :: ScriptHash -> m (Maybe (Versioned Script)) Source #
Returns the full validator corresponding to hash, if that validator owns something or if it is stored in the reference script field of some UTxO.
txOutByRef :: TxOutRef -> m (Maybe TxOut) 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 block, 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, TxOut)] Source #
Returns a list of all currently known outputs.
setParams :: Params -> m () Source #
Updates parameters
currentSlot :: m Slot Source #
Returns the current slot number
awaitSlot :: Slot -> m Slot Source #
Waits until the current slot becomes greater or equal to the given slot, and returns the current slot after waiting.
Note that it might not wait for anything if the current slot is large enough.
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.
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
currentTime :: MonadBlockChainWithoutValidation m => m (POSIXTime, POSIXTime) Source #
Returns the closed ms interval corresponding to the current slot
waitNSlots :: MonadBlockChainWithoutValidation m => Integer -> m Slot Source #
Moves n slots fowards
typedDatumFromTxOutRef :: (FromData 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 Datum) Source #
resolveDatum :: (IsAbstractOutput out, ToOutputDatum (DatumType out), MonadBlockChainBalancing m) => out -> m (Maybe (ConcreteOutput (OwnerType out) Datum (ValueType out) (ReferenceScriptType out))) Source #
Try to resolve the datum on the output: If there's an inline datum, take
that; if there's a datum hash, look the corresponding datum up (with
datumFromHash
), returning Nothing
if it can't be found; if there's no
datum or hash at all, return Nothing
.
resolveTypedDatum :: (IsAbstractOutput out, ToOutputDatum (DatumType out), MonadBlockChainBalancing m, FromData a) => out -> m (Maybe (ConcreteOutput (OwnerType out) a (ValueType out) (ReferenceScriptType out))) Source #
Like resolveDatum
, but also tries to use fromBuiltinData
to extract
a datum of the suitable type.
resolveValidator :: (IsAbstractOutput out, ToCredential (OwnerType out), MonadBlockChainBalancing m) => out -> m (Maybe (ConcreteOutput (Versioned Validator) (DatumType out) (ValueType out) (ReferenceScriptType out))) Source #
Tries to resolve the validator that owns an output: If the output is owned by
a public key, or if the validator's hash is not known (i.e. if
scriptFromHash
returns Nothing
) return Nothing
.
resolveReferenceScript :: (IsAbstractOutput out, ToScriptHash (ReferenceScriptType out), MonadBlockChainBalancing m) => out -> m (Maybe (ConcreteOutput (OwnerType out) (DatumType out) (ValueType out) (Versioned Validator))) Source #
Tries to resolve the reference script on an output: If the output has no
reference script, or if the reference script's hash is not known (i.e. if
scriptFromHash
returns Nothing
), this function will return Nothing
.
getEnclosingSlot :: MonadBlockChainWithoutValidation m => POSIXTime -> m Slot Source #
Return the slot that contains the given time. See slotToTimeInterval
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.
awaitDurationFromLowerBound :: MonadBlockChainWithoutValidation m => Integer -> m Slot Source #
Wait a given number of ms from the lower bound of the current slot and returns the current slot after waiting.
awaitDurationFromUpperBound :: MonadBlockChainWithoutValidation m => Integer -> 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
slotToTimeInterval :: MonadBlockChainWithoutValidation m => Slot -> m (POSIXTime, POSIXTime) Source #
Returns the closed ms interval corresponding to the slot with the given number. It holds that
slotToTimeInterval (getEnclosingSlot t) == (a, b) ==> a <= t <= b
and
slotToTimeInterval n == (a, b) ==> getEnclosingSlot a == n && getEnclosingSlot b == n
and
slotToTimeInterval n == (a, b) ==> getEnclosingSlot (a-1) == n-1 && getEnclosingSlot (b+1) == n+1
txSkelInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map TxOutRef TxOut) Source #
Resolves all the inputs of a given TxSkel
txSkelReferenceInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map TxOutRef TxOut) Source #
Resolves all the reference inputs of a given TxSkel
txSkelInputValidators :: MonadBlockChainBalancing m => TxSkel -> m (Map ValidatorHash (Versioned Validator)) Source #
Returns all validators which protect transaction inputs
txSkelInputValue :: MonadBlockChainBalancing m => TxSkel -> m Value Source #
look up the UTxOs the transaction consumes, and sum their values.
txSkelInputDataAsHashes :: MonadBlockChainBalancing m => TxSkel -> m [DatumHash] Source #
Looks up the data on UTxOs the transaction consumes and returns their hashes.
lookupUtxos :: MonadBlockChainBalancing m => [TxOutRef] -> m (Map TxOutRef TxOut) 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.
Arguments
:: MonadBlockChainBalancing m | |
=> TxOutRef | The UTXO to translate |
-> Bool | Whether to include the datum in the transaction |
-> Bool | Whether to allow further adjustment of the Ada value |
-> m TxSkelOut |
This creates a payment from an existing UTXO
txOutRefToTxSkelOut' :: MonadBlockChainBalancing m => TxOutRef -> m TxSkelOut Source #
A default version of txOutRefToTxSkelOut
where we both include the datum
in the transaction if it was hashed in the TxOut
, and allow further ADA
adjustment in case changes in the output require it.