| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
Hydra.Chain.Direct.State
Description
Contains the a stateful interface to transaction construction and observation.
It defines the 'ChainStateType tx' to be used in the Direct
layer and it's constituents.
Synopsis
- class HasKnownUTxO a where
- getKnownUTxO :: a -> UTxO
- data ChainStateAt = ChainStateAt {
- spendableUTxO :: UTxO
- recordedAt :: Maybe ChainPoint
- chainSlotFromPoint :: ChainPoint -> ChainSlot
- data ChainState
- initialChainState :: ChainStateType Tx
- data ChainContext = ChainContext {
- networkId :: NetworkId
- ownVerificationKey :: VerificationKey PaymentKey
- ownParty :: Party
- scriptRegistry :: ScriptRegistry
- data OpenState = OpenState {}
- data ClosedState = ClosedState {
- closedUTxO :: UTxO
- headId :: HeadId
- seedTxIn :: TxIn
- contestationDeadline :: UTCTime
- initialize :: ChainContext -> PParams LedgerEra -> TxIn -> [OnChainId] -> HeadParameters -> Tx
- data IncrementTxError
- = InvalidHeadSeedInIncrement {
- headSeed :: HeadSeed
- | InvalidHeadIdInIncrement {
- headId :: HeadId
- | CannotFindHeadOutputInIncrement
- | CannotFindDepositOutputInIncrement {
- depositTxId :: TxId
- | SnapshotMissingIncrementUTxO
- | SnapshotIncrementUTxOIsNull
- | CannotObserveDraftedDeposit
- | CannotDecodeHeadDatumInIncrement
- = InvalidHeadSeedInIncrement {
- increment :: ChainContext -> UTxO -> (HeadSeed, HeadId) -> HeadParameters -> ConfirmedSnapshot Tx -> SlotNo -> Either IncrementTxError Tx
- dryRunIncrementTx :: ChainContext -> UTxO -> HeadId -> ConfirmedSnapshot Tx -> Tx -> SlotNo -> Either IncrementTxError Tx
- decodeOpenDatum :: TxOut CtxUTxO -> Either IncrementTxError (HeadSeed, HeadParameters)
- data DecrementTxError
- = InvalidHeadSeedInDecrement {
- headSeed :: HeadSeed
- | InvalidHeadIdInDecrement {
- headId :: HeadId
- | CannotFindHeadOutputInDecrement
- | DecrementValueNegative
- | SnapshotDecrementUTxOIsNull
- = InvalidHeadSeedInDecrement {
- decrement :: ChainContext -> UTxO -> (HeadSeed, HeadId) -> HeadParameters -> ConfirmedSnapshot Tx -> Either DecrementTxError Tx
- data CloseTxError
- = InvalidHeadIdInClose {
- headId :: HeadId
- | CannotFindHeadOutputToClose
- | BothCommitAndDecommitInClose
- = InvalidHeadIdInClose {
- data RecoverTxError
- = InvalidHeadIdInRecover {
- headId :: HeadId
- | CannotFindDepositOutputToRecover {
- depositTxId :: TxId
- | CannotFindDepositedOutputToRecover {
- depositedTxId :: TxId
- = InvalidHeadIdInRecover {
- recover :: ChainContext -> HeadId -> TxId -> UTxO -> SlotNo -> Either RecoverTxError Tx
- close :: ChainContext -> UTxO -> HeadId -> HeadParameters -> SnapshotVersion -> ConfirmedSnapshot Tx -> SlotNo -> PointInTime -> Either CloseTxError Tx
- data ContestTxError
- contest :: ChainContext -> UTxO -> HeadId -> ContestationPeriod -> SnapshotVersion -> ConfirmedSnapshot Tx -> PointInTime -> Either ContestTxError Tx
- data FanoutTxError
- fanout :: ChainContext -> UTxO -> TxIn -> UTxO -> Maybe UTxO -> Maybe UTxO -> UTxO -> SlotNo -> Either FanoutTxError Tx
- data PartialFanoutError
- partialFanout :: ChainContext -> UTxO -> TxIn -> Int -> UTxO -> UTxO -> SlotNo -> Either PartialFanoutError Tx
- finalPartialFanout :: ChainContext -> UTxO -> TxIn -> UTxO -> UTxO -> SlotNo -> Either PartialFanoutError Tx
- readHeadState :: (TxIn, TxOut CtxUTxO) -> Either PartialFanoutError State
- buildAndVerifyAccumulator :: FanoutProgressDatum -> UTxO -> Either PartialFanoutError HydraAccumulator
- utxoOfThisHead :: PolicyId -> UTxO -> UTxO
- data HydraContext = HydraContext {
- ctxVerificationKeys :: [VerificationKey PaymentKey]
- ctxHydraSigningKeys :: [Secret (SigningKey HydraKey)]
- ctxNetworkId :: NetworkId
- ctxContestationPeriod :: ContestationPeriod
- ctxDepositPeriod :: DepositPeriod
- ctxScriptRegistry :: ScriptRegistry
Documentation
class HasKnownUTxO a where Source #
A class for accessing the known UTxO set in a type. This is useful to get
all the relevant UTxO for resolving transaction inputs.
Methods
getKnownUTxO :: a -> UTxO Source #
Instances
| HasKnownUTxO ChainContext Source # | |
Defined in Hydra.Chain.Direct.State Methods getKnownUTxO :: ChainContext -> UTxO Source # | |
| HasKnownUTxO ChainState Source # | |
Defined in Hydra.Chain.Direct.State Methods getKnownUTxO :: ChainState -> UTxO Source # | |
| HasKnownUTxO ClosedState Source # | |
Defined in Hydra.Chain.Direct.State Methods getKnownUTxO :: ClosedState -> UTxO Source # | |
| HasKnownUTxO OpenState Source # | |
Defined in Hydra.Chain.Direct.State Methods getKnownUTxO :: OpenState -> UTxO Source # | |
States & transitions
data ChainStateAt Source #
The chain state used by the Hydra.Chain.Direct implementation. It records
the actual ChainState paired with a ChainSlot (used to know up to which
point to rewind on rollbacks).
XXX: could move this into IsChainState and use UTxOType tx instead of ChainStateType tx
Constructors
| ChainStateAt | |
Fields
| |
Instances
chainSlotFromPoint :: ChainPoint -> ChainSlot Source #
Get a generic ChainSlot from a Cardano ChainPoint. Slot 0 is used for
the genesis point.
data ChainState Source #
An enumeration of all possible on-chain states of a Hydra Head, where each case stores the relevant information to construct & observe transactions to other states.
Constructors
| Idle | The idle state does not contain any head-specific information and exists to be used as a starting and terminal state. |
| Open OpenState | |
| Closed ClosedState |
Instances
| Generic ChainState Source # | |
Defined in Hydra.Chain.Direct.State | |
| Show ChainState Source # | |
Defined in Hydra.Chain.Direct.State | |
| Eq ChainState Source # | |
Defined in Hydra.Chain.Direct.State Methods (==) :: ChainState -> ChainState -> Bool Source # (/=) :: ChainState -> ChainState -> Bool Source # | |
| HasKnownUTxO ChainState Source # | |
Defined in Hydra.Chain.Direct.State Methods getKnownUTxO :: ChainState -> UTxO Source # | |
| type Rep ChainState Source # | |
Defined in Hydra.Chain.Direct.State type Rep ChainState = D1 ('MetaData "ChainState" "Hydra.Chain.Direct.State" "hydra-node-2.3.0-1cgalYNmLJQC1YXlYVq7mp" 'False) (C1 ('MetaCons "Idle" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "Open" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 OpenState)) :+: C1 ('MetaCons "Closed" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 ClosedState)))) | |
initialChainState :: ChainStateType Tx Source #
Defines the starting state of the direct chain layer.
data ChainContext Source #
Read-only chain-specific data. This is different to HydraContext as it
only contains data known to single peer.
Constructors
| ChainContext | |
Fields
| |
Instances
Instances
| Generic OpenState Source # | |
| Show OpenState Source # | |
| Eq OpenState Source # | |
| HasKnownUTxO OpenState Source # | |
Defined in Hydra.Chain.Direct.State Methods getKnownUTxO :: OpenState -> UTxO Source # | |
| type Rep OpenState Source # | |
Defined in Hydra.Chain.Direct.State type Rep OpenState = D1 ('MetaData "OpenState" "Hydra.Chain.Direct.State" "hydra-node-2.3.0-1cgalYNmLJQC1YXlYVq7mp" 'False) (C1 ('MetaCons "OpenState" 'PrefixI 'True) (S1 ('MetaSel ('Just "openUTxO") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 UTxO) :*: (S1 ('MetaSel ('Just "headId") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 HeadId) :*: S1 ('MetaSel ('Just "seedTxIn") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 TxIn)))) | |
data ClosedState Source #
Constructors
| ClosedState | |
Fields
| |
Instances
Constructing transactions
Arguments
| :: ChainContext | |
| -> PParams LedgerEra | |
| -> TxIn | Seed input. |
| -> [OnChainId] | Verification key hashes of all participants. |
| -> HeadParameters | |
| -> Tx |
Construct an init transaction given some general ChainContext, the
HeadParameters and a seed TxIn which will be spent.
data IncrementTxError Source #
Constructors
Instances
| Show IncrementTxError Source # | |
Defined in Hydra.Chain.Direct.State | |
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head and deposit outputs |
| -> (HeadSeed, HeadId) | |
| -> HeadParameters | |
| -> ConfirmedSnapshot Tx | Snapshot to increment with. Also names the deposit to claim. |
| -> SlotNo | Valid until, must be before deadline. |
| -> Either IncrementTxError Tx |
Construct a increment transaction spending the head and deposit outputs in given UTxO,
and producing single head output for pending $sel:utxoToCommit:Snapshot of given Snapshot.
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing the current head output. |
| -> HeadId | |
| -> ConfirmedSnapshot Tx | Current confirmed snapshot, basis for the incrementing snapshot. |
| -> Tx | Drafted (unbalanced) deposit transaction. |
| -> SlotNo | Upper validity slot. |
| -> Either IncrementTxError Tx |
Build an increment transaction claiming a drafted (not yet submitted)
deposit transaction against the current head output. The incrementing
snapshot is based on the given current confirmed snapshot, as the next
snapshot which would commit the drafted deposit. The result can never
validate on chain (for an InitialSnapshot base the multi-signature is
fabricated, otherwise the current snapshot's signatures do not cover the
fabricated snapshot), but is byte-accurate in every component that matters
for size estimation: script witnesses, datum layout, one 64-byte signature
per party in the redeemer, and the merged head output value. The head seed,
parties and periods are decoded from the current head output's inline datum.
decodeOpenDatum :: TxOut CtxUTxO -> Either IncrementTxError (HeadSeed, HeadParameters) Source #
Decode head seed and parameters from the inline datum of a head output.
data DecrementTxError Source #
Possible errors when trying to construct decrement tx
Constructors
| InvalidHeadSeedInDecrement | |
Fields
| |
| InvalidHeadIdInDecrement | |
Fields
| |
| CannotFindHeadOutputInDecrement | |
| DecrementValueNegative | |
| SnapshotDecrementUTxOIsNull | |
Instances
| Show DecrementTxError Source # | |
Defined in Hydra.Chain.Direct.State | |
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head, initial and commit outputs |
| -> (HeadSeed, HeadId) | |
| -> HeadParameters | |
| -> ConfirmedSnapshot Tx | Snapshot to decrement with. |
| -> Either DecrementTxError Tx |
Construct a decrement transaction spending the head output in given UTxO,
and producing outputs for all pending $sel:utxoToDecommit:Snapshot of given Snapshot.
data CloseTxError Source #
Constructors
| InvalidHeadIdInClose | |
Fields
| |
| CannotFindHeadOutputToClose | |
| BothCommitAndDecommitInClose | |
Instances
| Show CloseTxError Source # | |
Defined in Hydra.Chain.Direct.State | |
data RecoverTxError Source #
Constructors
| InvalidHeadIdInRecover | |
Fields
| |
| CannotFindDepositOutputToRecover | |
Fields
| |
| CannotFindDepositedOutputToRecover | |
Fields
| |
Instances
| Show RecoverTxError Source # | |
Defined in Hydra.Chain.Direct.State | |
Arguments
| :: ChainContext | |
| -> HeadId | |
| -> TxId | Deposit TxId |
| -> UTxO | Spendable UTxO |
| -> SlotNo | |
| -> Either RecoverTxError Tx |
Construct a recover transaction spending the deposit output and producing outputs the user initially deposited.
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head, initial and commit outputs |
| -> HeadId | Head id to close. |
| -> HeadParameters | Parameters of the head to close. |
| -> SnapshotVersion | Last known version of the open head. NOTE: We deliberately require a
|
| -> ConfirmedSnapshot Tx | Snapshot to close with. |
| -> SlotNo |
|
| -> PointInTime |
|
| -> Either CloseTxError Tx |
Construct a close transaction spending the head output in given UTxO,
head parameters, and a confirmed snapshot. NOTE: Lower and upper bound slot
difference should not exceed contestation period.
data ContestTxError Source #
Constructors
| InvalidHeadIdInContest | |
Fields
| |
| CannotFindHeadOutputToContest | |
| MissingHeadDatumInContest | |
| MissingHeadRedeemerInContest | |
| WrongDatumInContest | |
| FailedToConvertFromScriptDataInContest | |
Instances
| Show ContestTxError Source # | |
Defined in Hydra.Chain.Direct.State | |
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head, initial and commit outputs |
| -> HeadId | |
| -> ContestationPeriod | |
| -> SnapshotVersion | Last known version of the open head. NOTE: We deliberately require a
|
| -> ConfirmedSnapshot Tx | Snapshot to contest with. |
| -> PointInTime | Current slot and posix time to be used as the contestation time. |
| -> Either ContestTxError Tx |
Construct a contest transaction based on the ClosedState and a confirmed
snapshot. The given PointInTime will be used as an upper validity bound and
needs to be before the deadline.
data FanoutTxError Source #
Constructors
| CannotFindHeadOutputToFanout | |
| MissingHeadDatumInFanout | |
| WrongDatumInFanout | |
| FailedToConvertFromScriptDataInFanout | |
| BothCommitAndDecommitInFanout | |
| FailedToCreateFanoutProof Text |
Instances
| Show FanoutTxError Source # | |
Defined in Hydra.Chain.Direct.State | |
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head, initial and commit outputs |
| -> TxIn | Seed TxIn |
| -> UTxO | Snapshot UTxO to fanout |
| -> Maybe UTxO | Snapshot UTxO to commit to fanout |
| -> Maybe UTxO | Snapshot UTxO to decommit to fanout |
| -> UTxO | Full snapshot UTxO for accumulator (matches closed datum) |
| -> SlotNo | Contestation deadline as SlotNo, used to set lower tx validity bound. |
| -> Either FanoutTxError Tx |
Construct a fanout transaction based on the ClosedState and off-chain
agreed UTxO set to fan out.
data PartialFanoutError Source #
Errors that can occur when constructing partial or final-partial fanout transactions.
Constructors
| CannotFindHeadOutput | |
| MissingHeadDatum | |
| WrongDatum | |
| FailedToConvertFromScriptData | |
| StaleChainState | The on-chain accumulator no longer matches the UTxOs we want to distribute. This happens when another node already posted a partial fanout and the chain state moved forward. |
| CannotCreateProof Text | Membership proof generation failed (e.g. subset element not in accumulator or CRS too short). Indicates a programming error in the caller. |
Instances
| Show PartialFanoutError Source # | |
Defined in Hydra.Chain.Direct.State | |
| Eq PartialFanoutError Source # | |
Defined in Hydra.Chain.Direct.State Methods (==) :: PartialFanoutError -> PartialFanoutError -> Bool Source # (/=) :: PartialFanoutError -> PartialFanoutError -> Bool Source # | |
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head output |
| -> TxIn | Seed TxIn |
| -> Int | Number of UTxOs to distribute in this step |
| -> UTxO | UTxO used to verify the on-chain accumulator commitment. For the first fanout step this is utxoForProof (the snapshot's full set, including any decommit UTxOs that may already have been removed from the head by a DecrementTx). For subsequent FanoutProgress steps it equals remainingUTxO. |
| -> UTxO | Remaining UTxOs to distribute (will be split into distribute + new remaining) |
| -> SlotNo | Contestation deadline as SlotNo |
| -> Either PartialFanoutError Tx |
Construct a partial fanout transaction that distributes a subset of UTxOs.
Handles both first step (Closed → FanoutProgress) and intermediate steps
(FanoutProgress → FanoutProgress) by detecting the current on-chain datum type.
The first chunkSize UTxOs from remainingUTxO are distributed; the rest become
the new remaining set.
Arguments
| :: ChainContext | |
| -> UTxO | Spendable UTxO containing head output |
| -> TxIn | Seed TxIn |
| -> UTxO | All remaining UTxOs to distribute |
| -> UTxO | Pre-settled UTxO: elements in the snapshot accumulator that are never distributed (e.g. a decommit UTxO paid out before close). mempty in normal case. |
| -> SlotNo | Contestation deadline as SlotNo |
| -> Either PartialFanoutError Tx |
Construct the final partial fanout transaction that distributes all remaining UTxOs and burns all head tokens. Reads FanoutProgressDatum from the head output.
readHeadState :: (TxIn, TxOut CtxUTxO) -> Either PartialFanoutError State Source #
Read and decode the head state from a head script output.
buildAndVerifyAccumulator :: FanoutProgressDatum -> UTxO -> Either PartialFanoutError HydraAccumulator Source #
Build an accumulator from the given UTxO and verify its commitment matches
the one in the on-chain datum. Returns the accumulator for reuse by the caller.
Fails with StaleChainState if the commitments differ.
Helpers
utxoOfThisHead :: PolicyId -> UTxO -> UTxO Source #
Generators
data HydraContext Source #
Define some global context from which generators can pick
values for generation. This allows to write fairly independent generators
which however still make sense with one another within the context of a head.
For example, one can generate a head's _party_ from that global list, whereas other functions may rely on all parties and thus, we need both generation to be coherent.
Do not use this in production code, but only for generating test data.
Constructors
| HydraContext | |
Fields
| |
Instances
| Show HydraContext Source # | |
Defined in Hydra.Chain.Direct.State | |