Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Contains the a state-ful 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 ChainTransition
- data ChainState
- initialChainState :: ChainStateType Tx
- data ChainContext = ChainContext {
- networkId :: NetworkId
- ownVerificationKey :: VerificationKey PaymentKey
- ownParty :: Party
- scriptRegistry :: ScriptRegistry
- data InitialState = InitialState {
- initialThreadOutput :: InitialThreadOutput
- initialInitials :: [(TxIn, TxOut CtxUTxO)]
- initialCommits :: [(TxIn, TxOut CtxUTxO)]
- headId :: HeadId
- seedTxIn :: TxIn
- data OpenState = OpenState {
- openThreadOutput :: OpenThreadOutput
- headId :: HeadId
- seedTxIn :: TxIn
- openUtxoHash :: UTxOHash
- data ClosedState = ClosedState {
- closedThreadOutput :: ClosedThreadOutput
- headId :: HeadId
- seedTxIn :: TxIn
- initialize :: ChainContext -> TxIn -> [OnChainId] -> HeadParameters -> Tx
- commit :: ChainContext -> HeadId -> UTxO -> UTxO -> Either (PostTxError Tx) Tx
- commit' :: ChainContext -> HeadId -> UTxO -> CommitBlueprintTx Tx -> Either (PostTxError Tx) Tx
- rejectByronAddress :: UTxO -> Either (PostTxError Tx) ()
- rejectMoreThanMainnetLimit :: NetworkId -> UTxO -> Either (PostTxError Tx) ()
- abort :: ChainContext -> TxIn -> UTxO -> UTxO -> Either AbortTxError Tx
- data CollectTxError
- = InvalidHeadIdInCollect {
- headId :: HeadId
- | CannotFindHeadOutputToCollect
- = InvalidHeadIdInCollect {
- collect :: ChainContext -> HeadId -> HeadParameters -> UTxO -> UTxO -> Either CollectTxError Tx
- data DecrementTxError
- decrement :: ChainContext -> UTxO -> HeadId -> HeadParameters -> ConfirmedSnapshot Tx -> Either DecrementTxError Tx
- data CloseTxError
- = InvalidHeadIdInClose {
- headId :: HeadId
- | CannotFindHeadOutputToClose
- = InvalidHeadIdInClose {
- 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 -> SlotNo -> Either FanoutTxError Tx
- utxoOfThisHead :: PolicyId -> UTxO -> UTxO
- observeInit :: ChainContext -> [VerificationKey PaymentKey] -> Tx -> Either NotAnInitReason (OnChainTx Tx, InitialState)
- observeCommit :: ChainContext -> InitialState -> Tx -> Maybe (OnChainTx Tx, InitialState)
- observeCollect :: InitialState -> Tx -> Maybe (OnChainTx Tx, OpenState)
- observeClose :: OpenState -> Tx -> Maybe (OnChainTx Tx, ClosedState)
- maxGenParties :: Int
- maxGenAssets :: Int
- genChainState :: Gen ChainState
- genChainStateWithTx :: Gen (ChainContext, ChainState, Tx, ChainTransition)
- data HydraContext = HydraContext {
- ctxVerificationKeys :: [VerificationKey PaymentKey]
- ctxHydraSigningKeys :: [SigningKey HydraKey]
- ctxNetworkId :: NetworkId
- ctxContestationPeriod :: ContestationPeriod
- ctxScriptRegistry :: ScriptRegistry
- ctxParties :: HydraContext -> [Party]
- ctxParticipants :: HydraContext -> [OnChainId]
- ctxHeadParameters :: HydraContext -> HeadParameters
- genHydraContext :: Int -> Gen HydraContext
- genHydraContextFor :: Int -> Gen HydraContext
- deriveChainContexts :: HydraContext -> Gen [ChainContext]
- pickChainContext :: HydraContext -> Gen ChainContext
- genStInitial :: HydraContext -> Gen (ChainContext, InitialState)
- genInitTx :: HydraContext -> Gen Tx
- genCommits :: HydraContext -> Tx -> Gen [Tx]
- genCommits' :: Gen UTxO -> HydraContext -> Tx -> Gen [Tx]
- genCommitFor :: VerificationKey PaymentKey -> Gen UTxO
- genCommit :: Gen UTxO
- genCollectComTx :: Gen (ChainContext, [UTxO], InitialState, Tx)
- genDecrementTx :: Int -> Gen (ChainContext, [TxOut CtxUTxO], OpenState, Tx)
- genCloseTx :: Int -> Gen (ChainContext, OpenState, Tx, ConfirmedSnapshot Tx)
- genContestTx :: Gen (HydraContext, PointInTime, ClosedState, Tx)
- genFanoutTx :: Int -> Int -> Gen (HydraContext, ClosedState, Tx)
- getContestationDeadline :: ClosedState -> UTCTime
- genStOpen :: HydraContext -> Gen (UTxO, OpenState)
- genStClosed :: HydraContext -> UTxO -> Maybe UTxO -> Gen (SnapshotNumber, UTxO, Maybe UTxO, ClosedState)
- unsafeCommit :: HasCallStack => ChainContext -> HeadId -> UTxO -> UTxO -> Tx
- unsafeAbort :: HasCallStack => ChainContext -> TxIn -> UTxO -> UTxO -> Tx
- unsafeDecrement :: HasCallStack => ChainContext -> UTxO -> HeadId -> HeadParameters -> ConfirmedSnapshot Tx -> Tx
- unsafeClose :: HasCallStack => ChainContext -> UTxO -> HeadId -> HeadParameters -> SnapshotVersion -> ConfirmedSnapshot Tx -> SlotNo -> PointInTime -> Tx
- unsafeCollect :: ChainContext -> HeadId -> HeadParameters -> UTxO -> UTxO -> Tx
- unsafeContest :: HasCallStack => ChainContext -> UTxO -> HeadId -> ContestationPeriod -> SnapshotVersion -> ConfirmedSnapshot Tx -> PointInTime -> Tx
- unsafeFanout :: HasCallStack => ChainContext -> UTxO -> TxIn -> UTxO -> Maybe UTxO -> SlotNo -> Tx
- unsafeObserveInit :: HasCallStack => ChainContext -> [VerificationKey PaymentKey] -> Tx -> InitialState
- unsafeObserveInitAndCommits :: HasCallStack => ChainContext -> [VerificationKey PaymentKey] -> Tx -> [Tx] -> ([UTxO], InitialState)
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.
getKnownUTxO :: a -> UTxO Source #
Instances
HasKnownUTxO ChainContext Source # | |
Defined in Hydra.Chain.Direct.State getKnownUTxO :: ChainContext -> UTxO Source # | |
HasKnownUTxO ChainState Source # | |
Defined in Hydra.Chain.Direct.State getKnownUTxO :: ChainState -> UTxO Source # | |
HasKnownUTxO ClosedState Source # | |
Defined in Hydra.Chain.Direct.State getKnownUTxO :: ClosedState -> UTxO Source # | |
HasKnownUTxO InitialState Source # | |
Defined in Hydra.Chain.Direct.State getKnownUTxO :: InitialState -> UTxO Source # | |
HasKnownUTxO OpenState Source # | |
Defined in Hydra.Chain.Direct.State 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).
ChainStateAt | |
|
Instances
chainSlotFromPoint :: ChainPoint -> ChainSlot Source #
Get a generic ChainSlot
from a Cardano ChainPoint
. Slot 0 is used for
the genesis point.
data ChainTransition Source #
A definition of all transitions between ChainState
s. Enumerable and
bounded to be used as labels for checking coverage.
Instances
Bounded ChainTransition Source # | |
Defined in Hydra.Chain.Direct.State | |
Enum ChainTransition Source # | |
Defined in Hydra.Chain.Direct.State succ :: ChainTransition -> ChainTransition Source # pred :: ChainTransition -> ChainTransition Source # toEnum :: Int -> ChainTransition Source # fromEnum :: ChainTransition -> Int Source # enumFrom :: ChainTransition -> [ChainTransition] Source # enumFromThen :: ChainTransition -> ChainTransition -> [ChainTransition] Source # enumFromTo :: ChainTransition -> ChainTransition -> [ChainTransition] Source # enumFromThenTo :: ChainTransition -> ChainTransition -> ChainTransition -> [ChainTransition] Source # | |
Show ChainTransition Source # | |
Defined in Hydra.Chain.Direct.State | |
Eq ChainTransition Source # | |
Defined in Hydra.Chain.Direct.State (==) :: ChainTransition -> ChainTransition -> Bool Source # (/=) :: ChainTransition -> ChainTransition -> Bool Source # |
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.
Idle | The idle state does not contain any head-specific information and exists to be used as a starting and terminal state. |
Initial InitialState | |
Open OpenState | |
Closed ClosedState |
Instances
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.
ChainContext | |
|
Instances
data InitialState Source #
InitialState | |
|
Instances
OpenState | |
|
Instances
data ClosedState Source #
ClosedState | |
|
Instances
Constructing transactions
:: ChainContext | |
-> 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.
:: ChainContext | |
-> HeadId | |
-> UTxO | Spendable |
-> UTxO |
|
-> Either (PostTxError Tx) Tx |
Construct a commit transaction based on known, spendable UTxO and some
arbitrary UTxOs to commit. This does look for "our initial output" to spend
and check the given UTxO'
to be compatible. Hence, this function does fail
if already committed or if the head is not initializing.
NOTE: This version of commit
does only commit outputs which are held by
payment keys. For a variant which supports committing scripts, see commit'
.
:: ChainContext | |
-> HeadId | |
-> UTxO | Spendable |
-> CommitBlueprintTx Tx | |
-> Either (PostTxError Tx) Tx |
Construct a commit transaction based on known, spendable UTxO and some
user UTxO inputs to commit. This does look for "our initial output" to spend
and check the given UTxO'
to be compatible. Hence, this function does fail
if already committed or if the head is not initializing.
NOTE: A simpler variant only supporting pubkey outputs is commit
.
rejectByronAddress :: UTxO -> Either (PostTxError Tx) () Source #
rejectMoreThanMainnetLimit :: NetworkId -> UTxO -> Either (PostTxError Tx) () Source #
:: ChainContext | |
-> TxIn | Seed TxIn |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> UTxO | Committed UTxOs to reimburse. |
-> Either AbortTxError Tx |
Construct a abort transaction based on known, spendable UTxO. This function looks for head, initial and commit outputs to spend and it will fail if we can't find the head output.
data CollectTxError Source #
Instances
Show CollectTxError Source # | |
Defined in Hydra.Chain.Direct.State |
:: ChainContext | |
-> HeadId | |
-> HeadParameters | |
-> UTxO | UTxO to be used to collect. Should match whatever is recorded in the commit inputs. |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> Either CollectTxError Tx |
Construct a collect transaction based on known, spendable UTxO. This function looks for head output and commit outputs to spend and it will fail if we can't find the head output.
data DecrementTxError Source #
Possible errors when trying to construct decrement tx
InvalidHeadIdInDecrement | |
| |
CannotFindHeadOutputInDecrement | |
SnapshotMissingDecrementUTxO | |
SnapshotDecrementUTxOIsNull |
Instances
Show DecrementTxError Source # | |
Defined in Hydra.Chain.Direct.State |
:: ChainContext | |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> 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 #
Instances
Show CloseTxError Source # | |
Defined in Hydra.Chain.Direct.State |
:: 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 #
InvalidHeadIdInContest | |
| |
CannotFindHeadOutputToContest | |
MissingHeadDatumInContest | |
MissingHeadRedeemerInContest | |
WrongDatumInContest | |
FailedToConvertFromScriptDataInContest |
Instances
Show ContestTxError Source # | |
Defined in Hydra.Chain.Direct.State |
:: 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 #
CannotFindHeadOutputToFanout | |
MissingHeadDatumInFanout | |
WrongDatumInFanout | |
FailedToConvertFromScriptDataInFanout |
Instances
Show FanoutTxError Source # | |
Defined in Hydra.Chain.Direct.State |
:: ChainContext | |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> TxIn | Seed TxIn |
-> UTxO | Snapshot UTxO to fanout |
-> Maybe UTxO | Snapshot UTxO to decommit to fanout |
-> 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.
Helpers
utxoOfThisHead :: PolicyId -> UTxO -> UTxO Source #
Observing Transitions
IdleState transitions
observeInit :: ChainContext -> [VerificationKey PaymentKey] -> Tx -> Either NotAnInitReason (OnChainTx Tx, InitialState) Source #
Observe an init transition using a InitialState
and observeInitTx
.
InitialState transitions
observeCommit :: ChainContext -> InitialState -> Tx -> Maybe (OnChainTx Tx, InitialState) Source #
Observe an commit transition using a InitialState
and observeCommitTx
.
observeCollect :: InitialState -> Tx -> Maybe (OnChainTx Tx, OpenState) Source #
Observe an collect transition using a InitialState
and observeCollectComTx
.
This function checks the head id and ignores if not relevant.
OpenState transitions
observeClose :: OpenState -> Tx -> Maybe (OnChainTx Tx, ClosedState) Source #
Observe a close transition using a OpenState
and observeCloseTx
.
This function checks the head id and ignores if not relevant.
Generators
maxGenParties :: Int Source #
Maximum number of parties used in the generators.
maxGenAssets :: Int Source #
Maximum number of assets (ADA or other tokens) used in the generators.
genChainState :: Gen ChainState Source #
Generate a ChainState
within known limits above.
genChainStateWithTx :: Gen (ChainContext, ChainState, Tx, ChainTransition) Source #
Generate a ChainContext
and ChainState
within the known limits above, along with a
transaction that results in a transition away from it.
Warning zone
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.
HydraContext | |
|
Instances
Show HydraContext Source # | |
Defined in Hydra.Chain.Direct.State |
ctxParties :: HydraContext -> [Party] Source #
ctxParticipants :: HydraContext -> [OnChainId] Source #
ctxHeadParameters :: HydraContext -> HeadParameters Source #
genHydraContext :: Int -> Gen HydraContext Source #
Generate a HydraContext
for a arbitrary number of parties, bounded by
given maximum.
genHydraContextFor :: Int -> Gen HydraContext Source #
Generate a HydraContext
for a given number of parties.
deriveChainContexts :: HydraContext -> Gen [ChainContext] Source #
Get all peer-specific ChainContext
s from a HydraContext
. NOTE: This
assumes that HydraContext
has same length $sel:ctxVerificationKeys:HydraContext
and
$sel:ctxHydraSigningKeys:HydraContext
.
XXX: This is actually a non-monadic function.
pickChainContext :: HydraContext -> Gen ChainContext Source #
Pick one of the participants and derive the peer-specific ChainContext
from a HydraContext
. NOTE: This assumes that HydraContext
has same length
$sel:ctxVerificationKeys:HydraContext
and $sel:ctxHydraSigningKeys:HydraContext
.
genStInitial :: HydraContext -> Gen (ChainContext, InitialState) Source #
genInitTx :: HydraContext -> Gen Tx Source #
genCommits :: HydraContext -> Tx -> Gen [Tx] Source #
genCommits' :: Gen UTxO -> HydraContext -> Tx -> Gen [Tx] Source #
genCommitFor :: VerificationKey PaymentKey -> Gen UTxO Source #
genCollectComTx :: Gen (ChainContext, [UTxO], InitialState, Tx) Source #
genDecrementTx :: Int -> Gen (ChainContext, [TxOut CtxUTxO], OpenState, Tx) Source #
genCloseTx :: Int -> Gen (ChainContext, OpenState, Tx, ConfirmedSnapshot Tx) Source #
genContestTx :: Gen (HydraContext, PointInTime, ClosedState, Tx) Source #
genFanoutTx :: Int -> Int -> Gen (HydraContext, ClosedState, Tx) Source #
genStOpen :: HydraContext -> Gen (UTxO, OpenState) Source #
genStClosed :: HydraContext -> UTxO -> Maybe UTxO -> Gen (SnapshotNumber, UTxO, Maybe UTxO, ClosedState) Source #
Danger zone
:: HasCallStack | |
=> ChainContext | |
-> HeadId | |
-> UTxO | Spendable |
-> UTxO |
|
-> Tx |
:: HasCallStack | |
=> ChainContext | |
-> TxIn | Seed TxIn |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> UTxO | Committed UTxOs to reimburse. |
-> Tx |
:: HasCallStack | |
=> ChainContext | |
-> UTxO | Spendable |
-> HeadId | |
-> HeadParameters | |
-> ConfirmedSnapshot Tx | |
-> Tx |
:: HasCallStack | |
=> ChainContext | |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> HeadId | |
-> HeadParameters | |
-> SnapshotVersion | |
-> ConfirmedSnapshot Tx | |
-> SlotNo | |
-> PointInTime | |
-> Tx |
Unsafe version of close
that throws an error if the transaction fails to build.
:: ChainContext | |
-> HeadId | |
-> HeadParameters | |
-> UTxO | UTxO to be used to collect. Should match whatever is recorded in the commit inputs. |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> Tx |
:: HasCallStack | |
=> ChainContext | |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> HeadId | |
-> ContestationPeriod | |
-> SnapshotVersion | |
-> ConfirmedSnapshot Tx | |
-> PointInTime | |
-> Tx |
Unsafe version of contest
that throws an error if the transaction fails to build.
:: HasCallStack | |
=> ChainContext | |
-> UTxO | Spendable UTxO containing head, initial and commit outputs |
-> TxIn | Seed TxIn |
-> UTxO | Snapshot UTxO to fanout |
-> Maybe UTxO | Snapshot decommit UTxO to fanout |
-> SlotNo | Contestation deadline as SlotNo, used to set lower tx validity bound. |
-> Tx |
unsafeObserveInit :: HasCallStack => ChainContext -> [VerificationKey PaymentKey] -> Tx -> InitialState Source #
unsafeObserveInitAndCommits :: HasCallStack => ChainContext -> [VerificationKey PaymentKey] -> Tx -> [Tx] -> ([UTxO], InitialState) Source #