hydra-node
Safe HaskellSafe-Inferred
LanguageGHC2021

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

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

Instances details
HasKnownUTxO ChainContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: ChainContext -> UTxO Source #

HasKnownUTxO ChainState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: ChainState -> UTxO Source #

HasKnownUTxO ClosedState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: ClosedState -> UTxO Source #

HasKnownUTxO OpenState Source # 
Instance details

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

Instances details
FromJSON ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

parseJSON :: Value -> Parser ChainStateAt

parseJSONList :: Value -> Parser [ChainStateAt]

omittedField :: Maybe ChainStateAt

ToJSON ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

toJSON :: ChainStateAt -> Value

toEncoding :: ChainStateAt -> Encoding

toJSONList :: [ChainStateAt] -> Value

toEncodingList :: [ChainStateAt] -> Encoding

omitField :: ChainStateAt -> Bool

Generic ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Associated Types

type Rep ChainStateAt :: Type -> Type Source #

Show ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

FromCBOR ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

fromCBOR :: Decoder s ChainStateAt

label :: Proxy ChainStateAt -> Text

ToCBOR ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

toCBOR :: ChainStateAt -> Encoding

encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy ChainStateAt -> Size

encodedListSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy [ChainStateAt] -> Size

Eq ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

type Rep ChainStateAt Source # 
Instance details

Defined in Hydra.Chain.Direct.State

type Rep ChainStateAt = D1 ('MetaData "ChainStateAt" "Hydra.Chain.Direct.State" "hydra-node-2.3.0-1cgalYNmLJQC1YXlYVq7mp" 'False) (C1 ('MetaCons "ChainStateAt" 'PrefixI 'True) (S1 ('MetaSel ('Just "spendableUTxO") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 UTxO) :*: S1 ('MetaSel ('Just "recordedAt") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (Maybe ChainPoint))))

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

Instances details
Generic ChainState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Associated Types

type Rep ChainState :: Type -> Type Source #

Show ChainState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Eq ChainState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

HasKnownUTxO ChainState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: ChainState -> UTxO Source #

type Rep ChainState Source # 
Instance details

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 details
Generic ChainContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Associated Types

type Rep ChainContext :: Type -> Type Source #

Show ChainContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Eq ChainContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

HasKnownUTxO ChainContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: ChainContext -> UTxO Source #

type Rep ChainContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

type Rep ChainContext = D1 ('MetaData "ChainContext" "Hydra.Chain.Direct.State" "hydra-node-2.3.0-1cgalYNmLJQC1YXlYVq7mp" 'False) (C1 ('MetaCons "ChainContext" 'PrefixI 'True) ((S1 ('MetaSel ('Just "networkId") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 NetworkId) :*: S1 ('MetaSel ('Just "ownVerificationKey") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 (VerificationKey PaymentKey))) :*: (S1 ('MetaSel ('Just "ownParty") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Party) :*: S1 ('MetaSel ('Just "scriptRegistry") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 ScriptRegistry))))

data OpenState Source #

Constructors

OpenState 

Fields

Instances

Instances details
Generic OpenState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Associated Types

type Rep OpenState :: Type -> Type Source #

Show OpenState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Eq OpenState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

HasKnownUTxO OpenState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: OpenState -> UTxO Source #

type Rep OpenState Source # 
Instance details

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

Instances details
Generic ClosedState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Associated Types

type Rep ClosedState :: Type -> Type Source #

Show ClosedState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Eq ClosedState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

HasKnownUTxO ClosedState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Methods

getKnownUTxO :: ClosedState -> UTxO Source #

type Rep ClosedState Source # 
Instance details

Defined in Hydra.Chain.Direct.State

type Rep ClosedState = D1 ('MetaData "ClosedState" "Hydra.Chain.Direct.State" "hydra-node-2.3.0-1cgalYNmLJQC1YXlYVq7mp" 'False) (C1 ('MetaCons "ClosedState" 'PrefixI 'True) ((S1 ('MetaSel ('Just "closedUTxO") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 UTxO) :*: S1 ('MetaSel ('Just "headId") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 HeadId)) :*: (S1 ('MetaSel ('Just "seedTxIn") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 TxIn) :*: S1 ('MetaSel ('Just "contestationDeadline") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 UTCTime))))

Constructing transactions

initialize Source #

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.

increment Source #

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.

dryRunIncrementTx Source #

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

decrement Source #

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.

recover Source #

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.

close Source #

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 SnapshotVersion to be passed in, even though it could be extracted from the open head output in the spendable UTxO, to stay consistent with the way parameters are handled.

-> ConfirmedSnapshot Tx

Snapshot to close with.

-> SlotNo

Tx validity lower bound

-> PointInTime

Tx validity upper bound

-> 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.

contest Source #

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 SnapshotVersion to be passed in, even though it could be extracted from the open head output in the spendable UTxO, to stay consistent with the way parameters are handled.

-> 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.

fanout Source #

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.

partialFanout 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.

finalPartialFanout Source #

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

Instances details
Show HydraContext Source # 
Instance details

Defined in Hydra.Chain.Direct.State

Orphan instances

IsChainState Tx Source # 
Instance details

Associated Types

type ChainPointType Tx = (p :: Type)

type ChainStateType Tx = (c :: Type)

Methods

chainStatePoint :: ChainStateType Tx -> ChainPointType Tx

chainPointSlot :: ChainPointType Tx -> ChainSlot