18. Single state in Hydra.Node.
· 4 min read
Status
Superseded by ADR 23 and ADR 26
Context
- Currently the
hydra-nodemaintains two pieces of state during the life-cycle of a Hydra Head:- A
HeadState txprovided by theHydraHead tx mhandle interface and part of theHydra.Nodemodule. It provides the basis for the mainhydra-nodebusiness logic inHydra.Node.processNextEventandHydra.HeadLogic.updateCreation, Usage SomeOnChainHeadStateis kept in theHydra.Chain.Directto keep track of the latest known head state, including notable transaction outputs and information how to spend it (e.g. scripts and datums) Code, Usage 1, Usage 2, Usage 3 (There are other unrelated things kept in memory like the event history in the API server or a peer map in the network heartbeat component.)
- A
- The interface between the
Hydra.Nodeand aHydra.Chaincomponent consists of- constructing certain Head protocol transactions given a description of it (
PostChainTx tx):postTx :: MonadThrow m => PostChainTx tx -> m () - a callback function when the
Hydra.Chaincomponent observed a new Head protocol transaction described byOnChainTx tx:type ChainCallback tx m = OnChainTx tx -> m ()
- constructing certain Head protocol transactions given a description of it (
- Given by the usage sites above, the
Hydra.Chain.Directmodule requires additional info to do both, construct protocol transactions withpostTxas well as observe potentialOnChainTx(here). Hence we see that, operation of theHydra.Chain.Directcomponent (and likely any implementing the interface fully) is inherently stateful. - We are looking at upcoming features to handle rollbacks and dealing with persisting the head state.
- Both could benefit from the idea, that the
HeadStateis just a result of pureEventprocessing (a.k.a event sourcing). - Right now the
HeadStatekept inHydra.Nodealone, is not enough to fully describe the state of thehydra-node. Hence it would not be enough to just persist all theEvents and replaying them to achieve persistence, nor resetting to some previousHeadStatein the presence of a rollback.
- Both could benefit from the idea, that the
Decision
- We define and keep a "blackbox"
ChainStateType txin theHeadState tx- It shall not be introspectable to the business logic in
HeadLogic - It shall contain chain-specific information about the current Hydra Head, which will naturally need to evolve once we have multiple Heads in our feature scope
- For example:
data HeadState tx
= IdleState
| InitialState
{ chainState :: ChainStateType tx
-- ...
}
| OpenState
{ chainState :: ChainStateType tx
-- ...
}
| ClosedState
{ chainState :: ChainStateType tx
-- ...
} - It shall not be introspectable to the business logic in
- We provide the latest
ChainStateType txtopostTx:postTx :: ChainStateType tx -> PostChainTx tx -> m () - We change the
ChainEvent txdata type and callback interface ofChainto:data ChainEvent tx
= Observation
{ observedTx :: OnChainTx tx
, newChainState :: ChainStateType tx
}
| Rollback ChainSlot
| Tick UTCTime
type ChainCallback tx m = (ChainStateType tx -> Maybe (ChainEvent tx)) -> m ()
with the meaning, that invocation of the callback indicates receival of a transaction which is Maybe observing a relevant ChainEvent tx, where an Observation may include a newChainState.
- We also decide to extend
OnChainEffectwith aChainState txto explicitly thread the usedchainStatein theHydra.HeadLogic.
Consequences
- We need to change the construction of
Chainhandles and the call sites ofpostTx - We need to extract the state handling (similar to the event queue) out of the
HydraNodehandle and shuffle the main ofhydra-nodea bit to be able to provide the latestChainStateto the chain callback as a continuation. - We need to make the
ChainStateserializable (ToJSON,FromJSON) as it will be part of theHeadState. - We can drop the
TVarof keepingOnChainHeadStatein theHydra.Chain.Directmodule. - We need to update
DirectChainSpecandBehaviorSpectest suites to mock/implement the callback & state handling. - We might be able to simplify the
ChainState txto be just aUTxOType txlater. - As
OnChainEffectandObservationvalues will contain aChainStateType tx, traces will automatically include the fullChainState, which might be helpful but also possible big.
Alternative
- We could extend
PostChainTx(likeObservation) withChainStateand keep the signatures:
postTx :: MonadThrow m => PostChainTx tx -> m ()
type ChainCallback tx m = (ChainState tx -> Maybe (ChainEvent tx) -> m ()
- Not implemented as it is less clear on the need for a
ChainStatein the signatures.

