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 SkelContext TxSkel
- | MCLogAdjustedTxSkel SkelContext TxSkel Integer (Maybe (Set TxOutRef, Wallet))
- | MCLogNewTx TxId
- | MCLogDiscardedUtxos Integer String
- | MCLogUnusedCollaterals (Either Wallet (Set TxOutRef))
- | MCLogAddedReferenceScript Redeemer TxOutRef ScriptHash
- | MCLogAdjustedTxSkelOut TxSkelOut Ada
- class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where
- getParams :: m Params
- utxosAt :: Address -> m [(TxOutRef, TxOut)]
- datumFromHash :: DatumHash -> m (Maybe Datum)
- validatorFromHash :: ValidatorHash -> m (Maybe (Versioned Validator))
- txOutByRef :: TxOutRef -> m (Maybe TxOut)
- logEvent :: MockChainLogEntry -> m ()
- class MonadBlockChainBalancing m => MonadBlockChainWithoutValidation m where
- class MonadBlockChainWithoutValidation m => MonadBlockChain m where
- validateTxSkel :: TxSkel -> m CardanoTx
- data SkelContext = SkelContext {}
- 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
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 MockChainT
monad
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 SkelContext TxSkel | Logging a Skeleton as it is submitted by the user. |
MCLogAdjustedTxSkel SkelContext 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 Redeemer TxOutRef ScriptHash | Logging the automatic addition of a reference script |
MCLogAdjustedTxSkelOut TxSkelOut Ada | Logging the automatic adjusment of a min ada amount |
Instances
class (MonadFail m, MonadError MockChainError m) => MonadBlockChainBalancing m where Source #
Contains methods needed for 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.
validatorFromHash :: ValidatorHash -> m (Maybe (Versioned Validator)) 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 #
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.
Instances
class MonadBlockChainWithoutValidation m => MonadBlockChain m where Source #
The main abstraction of the blockchain.
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. In MockChainT
, this means:
- deletes the consumed outputs from
mcstIndex
- adds the produced outputs to
msctIndex
- deletes the consumed datums from
mcstDatums
- adds the produced datums to
mcstDatums
- adds the validators on outputs to the
mcstValidators
.
Instances
MonadBlockChain StagedMockChain Source # | |
Defined in Cooked.MockChain.Staged Methods validateTxSkel :: TxSkel -> StagedMockChain CardanoTx Source # | |
Monad m => MonadBlockChain (MockChainT m) Source # | |
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 |
data SkelContext Source #
The missing information on a TxSkel
that can only be resolved by querying
the state of the blockchain.
Constructors
SkelContext | |
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 #
valueFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe Value) Source #
outputDatumFromTxOutRef :: MonadBlockChainBalancing m => TxOutRef -> m (Maybe OutputDatum) Source #
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 #
Try 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
validatorFromHash
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 #
Try 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
validatorFromHash
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 #
txSkelReferenceInputUtxos :: MonadBlockChainBalancing m => TxSkel -> m (Map TxOutRef TxOut) Source #
txSkelInputValidators :: MonadBlockChainBalancing m => TxSkel -> m (Map ValidatorHash (Versioned Validator)) Source #
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.