+
<html lang="en" dir="ltr" class="blog-wrapper blog-post-page plugin-blog plugin-id-default">
+
<meta name="generator" content="Docusaurus v2.4.0">
+
<title data-rh="true">18. Single state in Hydra.Node. | Hydra: Head Protocol</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="https://input-output-hk.github.io/head-protocol/adr/18"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docusaurus_tag" content="default"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:docusaurus_tag" content="default"><meta data-rh="true" property="og:title" content="18. Single state in Hydra.Node. | Hydra: Head Protocol"><meta data-rh="true" name="description" content="Status"><meta data-rh="true" property="og:description" content="Status"><meta data-rh="true" property="og:type" content="article"><meta data-rh="true" property="article:published_time" content="2022-04-13T00:00:00.000Z"><meta data-rh="true" property="article:tag" content="Superseded"><link data-rh="true" rel="icon" href="/head-protocol/img/hydra.png"><link data-rh="true" rel="canonical" href="https://input-output-hk.github.io/head-protocol/adr/18"><link data-rh="true" rel="alternate" href="https://input-output-hk.github.io/head-protocol/adr/18" hreflang="en"><link data-rh="true" rel="alternate" href="https://input-output-hk.github.io/head-protocol/fr/adr/18" hreflang="fr"><link data-rh="true" rel="alternate" href="https://input-output-hk.github.io/head-protocol/ja/adr/18" hreflang="ja"><link data-rh="true" rel="alternate" href="https://input-output-hk.github.io/head-protocol/adr/18" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://YZTAF8IOVB-dsn.algolia.net" crossorigin="anonymous"><link rel="alternate" type="application/rss+xml" href="/head-protocol/adr/rss.xml" title="Hydra: Head Protocol RSS Feed">
+
<link rel="alternate" type="application/atom+xml" href="/head-protocol/adr/atom.xml" title="Hydra: Head Protocol Atom Feed">
+
<link rel="search" type="application/opensearchdescription+xml" title="Hydra: Head Protocol" href="/head-protocol/opensearch.xml">
+
<link rel="alternate" type="application/rss+xml" href="/head-protocol/monthly/rss.xml" title="Hydra: Head Protocol RSS Feed">
+
<link rel="alternate" type="application/atom+xml" href="/head-protocol/monthly/atom.xml" title="Hydra: Head Protocol Atom Feed">
+
<script src="https://plausible.io/js/script.js" defer="defer" data-domain="hydra.family"></script><link rel="stylesheet" href="/head-protocol/assets/css/styles.e0759140.css">
+
<link rel="preload" href="/head-protocol/assets/js/runtime~main.0387067f.js" as="script">
+
<link rel="preload" href="/head-protocol/assets/js/main.b2f9d7ab.js" as="script">
+
<body class="navigation-with-keyboard">
+
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
+
<div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/head-protocol/"><div class="navbar__logo"><img src="/head-protocol/img/hydra.png" alt="Hydra: Head Protocol" class="themedImage_ToTc themedImage--light_HNdA"><img src="/head-protocol/img/hydra-white.png" alt="Hydra: Head Protocol" class="themedImage_ToTc themedImage--dark_i4oU"></div><b class="navbar__title text--truncate">Hydra: Head Protocol</b></a><a class="navbar__item navbar__link" href="/head-protocol/docs/getting-started">User Manual</a><a class="navbar__item navbar__link" href="/head-protocol/use-cases">Use Cases</a><a class="navbar__item navbar__link" href="/head-protocol/core-concepts">Core Concepts</a><a class="navbar__item navbar__link" href="/head-protocol/topologies">Topologies</a><a class="navbar__item navbar__link" href="/head-protocol/benchmarks">Benchmarks</a><a class="navbar__item navbar__link" href="/head-protocol/api-reference">API Reference</a></div><div class="navbar__items navbar__items--right"><a href="https://github.com/input-output-hk/hydra" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link"><svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" class="iconLanguage_nlXk"><path fill="currentColor" d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"></path></svg>English</a><ul class="dropdown__menu"><li><a href="/head-protocol/adr/18" target="_self" rel="noopener noreferrer" class="dropdown__link dropdown__link--active" lang="en">English</a></li><li><a href="/head-protocol/fr/adr/18" target="_self" rel="noopener noreferrer" class="dropdown__link" lang="fr">Français</a></li><li><a href="/head-protocol/ja/adr/18" target="_self" rel="noopener noreferrer" class="dropdown__link" lang="ja">日本語</a></li></ul></div><div class="toggle_vylO colorModeToggle_DEke"><button class="clean-btn toggleButton_gllP toggleButtonDisabled_aARS" type="button" disabled="" title="Switch between dark and light mode (currently light mode)" aria-label="Switch between dark and light mode (currently light mode)" aria-live="polite"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_pyhR"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" class="darkToggleIcon_wfgR"><path fill="currentColor" d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg></button></div><div class="searchBox_ZlJk"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="docusaurus_skipToContent_fallback" class="main-wrapper mainWrapper_z2l0"><div class="container margin-vert--lg"><div class="row"><aside class="col col--3"><nav class="sidebar_re4s thin-scrollbar" aria-label="Blog recent posts navigation"><div class="sidebarItemTitle_pO2u margin-bottom--md">Architectural Decision Records</div><ul class="sidebarItemList_Yudw clean-list"><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/1">1. Record Architecture Decisions
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/2">2. Reactive Core
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/3">3. Asynchronous Duplex Client API</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/4">4. Use Handle to model Effects
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/5">5. Use io-classes
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/6">6. Network Broadcasts all messages
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/7">7. Use with-pattern based component interfaces
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/8">8. Custom Prelude
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/9">9. Simplify Logging
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/10">10. Use Direct Connection to `cardano-node`
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/11">11. Use cardano-api
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/12">12. Top-down Test-driven Design
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/13">13. Plutus Contracts Testing Strategy
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/14">14. Token usage in Hydra Scripts
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/15">15. Configuration Through an Admin API
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/16">16. Keep Rejected ADRs
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/17">17. Use UDP protocol for Hydra networking
+
</a></li><li class="sidebarItem__DBe"><a aria-current="page" class="sidebarItemLink_mo7H sidebarItemLinkActive_I1ZP" href="/head-protocol/adr/18">18. Single state in Hydra.Node.
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/19">19. Use of reference scripts
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/20">20. Handling time
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/21">21. Bounded transaction validity on Hydra protocol transactions
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/22">22. Test High-level Properties using Model-Based Testing
+
</a></li><li class="sidebarItem__DBe"><a class="sidebarItemLink_mo7H" href="/head-protocol/adr/23">23. Local chain state in chain layer
+
</a></li></ul></nav></aside><main class="col col--7" itemscope="" itemtype="http://schema.org/Blog"><article itemprop="blogPost" itemscope="" itemtype="http://schema.org/BlogPosting"><header><h1 class="title_f1Hy" itemprop="headline">18. Single state in Hydra.Node.
+
</h1><div class="container_mt6G margin-vert--md"><time datetime="2022-04-13T00:00:00.000Z" itemprop="datePublished">April 13, 2022</time> · <!-- -->4 min read</div></header><div id="post-content" class="markdown" itemprop="articleBody"><h2 class="anchor anchorWithStickyNavbar_LWe7" id="status">Status<a href="#status" class="hash-link" aria-label="Direct link to Status" title="Direct link to Status"></a></h2><p>Superseded by <a href="/head-protocol/adr/23">ADR 23</a></p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="context">Context<a href="#context" class="hash-link" aria-label="Direct link to Context" title="Direct link to Context"></a></h2><ul><li>Currently the <code>hydra-node</code> maintains two pieces of state during the life-cycle of a Hydra Head:<ol><li>A <code>HeadState tx</code> provided by the <code>HydraHead tx m</code> handle interface and part of the <code>Hydra.Node</code> module. It provides the basis for the main <code>hydra-node</code> business logic in <code>Hydra.Node.processNextEvent</code> and <code>Hydra.HeadLogic.update</code><a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Node.hs#L256-L257" target="_blank" rel="noopener noreferrer">Creation</a>, <a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Node.hs#L174" target="_blank" rel="noopener noreferrer">Usage</a></li><li><code>SomeOnChainHeadState</code> is kept in the <code>Hydra.Chain.Direct</code> to keep track of the latest known head state, including notable transaction outputs and information how to spend it (e.g. scripts and datums)
+
<a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Chain/Direct.hs#L156-L162" target="_blank" rel="noopener noreferrer">Code</a>, <a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Chain/Direct.hs#L449" target="_blank" rel="noopener noreferrer">Usage 1</a>, <a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Chain/Direct.hs#L414" target="_blank" rel="noopener noreferrer">Usage 2</a>, <a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Chain/Direct.hs#L349-L352" target="_blank" rel="noopener noreferrer">Usage 3</a>
+
(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.)</li></ol></li><li>The interface between the <code>Hydra.Node</code> and a <code>Hydra.Chain</code> component consists of <ul><li>constructing certain Head protocol transactions given a description of it (<code>PostChainTx tx</code>):<div class="language-hs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-hs codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token hvariable">postTx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">MonadThrow</span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">PostChainTx</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li><li>a callback function when the <code>Hydra.Chain</code> component observed a new Head protocol transaction described by <code>OnChainTx tx</code>:<div class="language-hs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-hs codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">type</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainCallback</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">OnChainTx</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li></ul></li><li>Given by the usage sites above, the <code>Hydra.Chain.Direct</code> module requires additional info to do both, construct protocol transactions with <code>postTx</code> as well as observe potential <code>OnChainTx</code> (<a href="https://github.com/input-output-hk/hydra/blob/a98e2907c4e425de2736782793383aad63132c14/hydra-node/src/Hydra/Chain/Direct.hs#L333-L336" target="_blank" rel="noopener noreferrer">here</a>). Hence we see that, operation of the <code>Hydra.Chain.Direct</code> component (and likely any implementing the interface fully) is <strong>inherently stateful</strong>.</li><li>We are looking at upcoming features to <a href="https://github.com/input-output-hk/hydra/issues/185" target="_blank" rel="noopener noreferrer">handle rollbacks</a> and dealing with <a href="https://github.com/input-output-hk/hydra/issues/187" target="_blank" rel="noopener noreferrer">persisting the head state</a>.<ul><li>Both could benefit from the idea, that the <code>HeadState</code> is just a result of pure <code>Event</code> processing (a.k.a event sourcing).</li><li>Right now the <code>HeadState</code> kept in <code>Hydra.Node</code> alone, is not enough to fully describe the state of the <code>hydra-node</code>. Hence it would not be enough to just persist all the <code>Event</code>s and replaying them to achieve persistence, nor resetting to some previous <code>HeadState</code> in the presence of a rollback.</li></ul></li></ul><h2 class="anchor anchorWithStickyNavbar_LWe7" id="decision">Decision<a href="#decision" class="hash-link" aria-label="Direct link to Decision" title="Direct link to Decision"></a></h2><ul><li><p>We define and keep a "blackbox" <code>ChainStateType tx</code> in the <code>HeadState tx</code></p><ul><li>It shall not be introspectable to the business logic in <code>HeadLogic</code></li><li>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</li><li>For example:</li></ul><div class="language-hs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-hs codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">data</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">HeadState</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">IdleState</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">|</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">InitialState</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"> </span><span class="token hvariable">chainState</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainStateType</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">-- ...</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">|</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">OpenState</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"> </span><span class="token hvariable">chainState</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainStateType</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">-- ...</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">|</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ClosedState</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"> </span><span class="token hvariable">chainState</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainStateType</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">-- ...</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li><li><p>We provide the latest <code>ChainStateType tx</code> to <code>postTx</code>:</p><div class="language-hs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-hs codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token hvariable">postTx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainStateType</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">PostChainTx</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li><li><p>We change the <code>ChainEvent tx</code> data type and callback interface of <code>Chain</code> to:</p><div class="language-hs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-hs codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">data</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainEvent</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">Observation</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"> </span><span class="token hvariable">observedTx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">OnChainTx</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token hvariable">newChainState</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainStateType</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">|</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">Rollback</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainSlot</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token operator" style="color:#393A34">|</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">Tick</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">UTCTime</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token keyword" style="color:#00009f">type</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainCallback</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token constant" style="color:#36acaa">ChainStateType</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">Maybe</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token constant" style="color:#36acaa">ChainEvent</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>with the meaning, that invocation of the callback indicates receival of a transaction which is <code>Maybe</code> observing a relevant <code>ChainEvent tx</code>, where an <code>Observation</code> may include a <code>newChainState</code>.</p></li><li><p>We also decide to extend <code>OnChainEffect</code> with a <code>ChainState tx</code> to explicitly
+
thread the used <code>chainState</code> in the <code>Hydra.HeadLogic</code>.</p></li></ul><h2 class="anchor anchorWithStickyNavbar_LWe7" id="consequences">Consequences<a href="#consequences" class="hash-link" aria-label="Direct link to Consequences" title="Direct link to Consequences"></a></h2><ul><li>We need to change the construction of <code>Chain</code> handles and the call sites of <code>postTx</code></li><li>We need to extract the state handling (similar to the event queue) out of the <code>HydraNode</code> handle and shuffle the main of <code>hydra-node</code> a bit to be able to provide the latest <code>ChainState</code> to the chain callback as a continuation.</li><li>We need to make the <code>ChainState</code> serializable (<code>ToJSON</code>, <code>FromJSON</code>) as it will be part of the <code>HeadState</code>.</li><li>We can drop the <code>TVar</code> of keeping <code>OnChainHeadState</code> in the <code>Hydra.Chain.Direct</code> module.</li><li>We need to update <code>DirectChainSpec</code> and <code>BehaviorSpec</code> test suites to mock/implement the callback & state handling.</li><li>We might be able to simplify the <code>ChainState tx</code> to be just a <code>UTxOType tx</code> later.</li><li>As <code>OnChainEffect</code> and <code>Observation</code> values will contain a <code>ChainStateType tx</code>, traces will automatically include the full <code>ChainState</code>, which might be helpful but also possible big. </li></ul><h2 class="anchor anchorWithStickyNavbar_LWe7" id="alternative">Alternative<a href="#alternative" class="hash-link" aria-label="Direct link to Alternative" title="Direct link to Alternative"></a></h2><ul><li>We could extend <code>PostChainTx</code> (like <code>Observation</code>) with <code>ChainState</code> and keep the signatures:</li></ul><div class="language-hs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-hs codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token hvariable">postTx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">::</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">MonadThrow</span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">PostChainTx</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token keyword" style="color:#00009f">type</span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">ChainCallback</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token constant" style="color:#36acaa">ChainState</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token constant" style="color:#36acaa">Maybe</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token constant" style="color:#36acaa">ChainEvent</span><span class="token plain"> </span><span class="token hvariable">tx</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">-></span><span class="token plain"> </span><span class="token hvariable">m</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><ul><li>Not implemented as it is less clear on the need for a <code>ChainState</code> in the signatures.</li></ul></div><footer class="row docusaurus-mt-lg blogPostFooterDetailsFull_mRVl"><div class="col"><b>Tags:</b><ul class="tags_jXut padding--none margin-left--sm"><li class="tag_QGVx"><a class="tag_zVej tagRegular_sFm0" href="/head-protocol/adr/tags/superseded">Superseded</a></li></ul></div></footer></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Blog post page navigation"><a class="pagination-nav__link pagination-nav__link--prev" href="/head-protocol/adr/17"><div class="pagination-nav__sublabel">Newer Post</div><div class="pagination-nav__label">17. Use UDP protocol for Hydra networking
+
</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/head-protocol/adr/19"><div class="pagination-nav__sublabel">Older Post</div><div class="pagination-nav__label">19. Use of reference scripts
+
</div></a></nav></main><div class="col col--2"><div class="tableOfContents_bqdL thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#status" class="table-of-contents__link toc-highlight">Status</a></li><li><a href="#context" class="table-of-contents__link toc-highlight">Context</a></li><li><a href="#decision" class="table-of-contents__link toc-highlight">Decision</a></li><li><a href="#consequences" class="table-of-contents__link toc-highlight">Consequences</a></li><li><a href="#alternative" class="table-of-contents__link toc-highlight">Alternative</a></li></ul></div></div></div></div></div><footer class="footer footer--dark"><div class="container container-fluid"><div class="row footer__links"><div class="col footer__col"><div class="footer__title">Contributing</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://github.com/input-output-hk/hydra/wiki/Coding-Standards" target="_blank" rel="noopener noreferrer" class="footer__link-item">Coding Standards</a></li><li class="footer__item"><a class="footer__link-item" href="/head-protocol/adr">Architectural Decision Records</a></li><li class="footer__item"><a href="https://github.com/input-output-hk/hydra/wiki/Testing-Strategy" target="_blank" rel="noopener noreferrer" class="footer__link-item">Testing Strategy</a></li></ul></div><div class="col footer__col"><div class="footer__title">Community</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://discord.gg/Qq5vNTg9PT" target="_blank" rel="noopener noreferrer" class="footer__link-item">Discord (#ask-hydra)<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://github.com/input-output-hk/hydra/discussions" target="_blank" rel="noopener noreferrer" class="footer__link-item">Github Discussions<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://cardano.stackexchange.com/questions/tagged/hydra" target="_blank" rel="noopener noreferrer" class="footer__link-item">Stack Exchange<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><div class="col footer__col"><div class="footer__title">More</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/head-protocol/docs/haskell_packages">Haskell Packages</a></li><li class="footer__item"><a class="footer__link-item" href="/head-protocol/monthly">Monthly reports</a></li><li class="footer__item"><a href="https://github.com/input-output-hk/hydra/wiki/Logbook" target="_blank" rel="noopener noreferrer" class="footer__link-item">Logbook</a></li></ul></div><div class="col footer__col"><div class="footer__title">Legal</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://static.iohk.io/terms/iohktermsandconditions.pdf" target="_blank" rel="noopener noreferrer" class="footer__link-item">Terms & Conditions</a></li><li class="footer__item"><a href="https://static.iohk.io/terms/iog-privacy-policy.pdf" target="_blank" rel="noopener noreferrer" class="footer__link-item">Privacy Policy</a></li><li class="footer__item"><a href="https://github.com/input-output-hk/hydra/graphs/contributors" target="_blank" rel="noopener noreferrer" class="footer__link-item">Contributors</a></li></ul></div></div><div class="footer__bottom text--center"><div class="footer__copyright">
+
Built with Docusaurus on 2023-05-11T08:46:48+02:00
+
</div></div></div></footer></div>
+
<script src="/head-protocol/assets/js/runtime~main.0387067f.js"></script>
+
<script src="/head-protocol/assets/js/main.b2f9d7ab.js"></script>
\ No newline at end of file