Skip to content

Commit

Permalink
Merge pull request #513 from input-output-hk/ch1bo/docs-api-behavior
Browse files Browse the repository at this point in the history
Add a page for API Behavior
  • Loading branch information
ch1bo committed Sep 28, 2022
2 parents 7b58957 + 3e11846 commit 23af129
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 35 deletions.
18 changes: 18 additions & 0 deletions docs/core-concepts/behavior.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
sidebar_position: 3
---

# API Behavior

This page documents the behavior of a `hydra-node` at the API layer. That is, how the system behaves given [ClientInputs](https://hydra.family/head-protocol/haddock/hydra-node/Hydra-API-ClientInput.html#t:ClientInput) and what [ServerOutputs](https://hydra.family/head-protocol/haddock/hydra-node/Hydra-API-ServerOutput.html#t:ServerOutput) are produced in response to it. See also the [API reference](https://hydra.family/head-protocol/api-reference/) for more details about individual API messages.

The formalism uses [UML statechart](https://en.wikipedia.org/wiki/UML_state_machine) language where transitions are labeled: `input [condition] / output`. When two outputs (e.g. `A` and `B`) are expected we write `A,B`, while `{A,B}` denotes mutual exclusiveness of outputs.

![](https://www.plantuml.com/plantuml/png/ZP6zJWCn383tF8Ldr8gz0oggG7G112UMgaEANMefSKw92zSYtftaKbrBOM3pp_UTVSuqgOswzSvi60d8jxe3fFGQkLKEOipYOWdZyHNCXPMjuptB6qpw52xXYIpkcZ0BthCQymFwBLKiQpLGmZCZAxSircNsUXLYGU_8uZoLx4_yeIM1oS2Lr9Y-U6pUqqVJPUIpwLwoYwEccylx8RhfMew4NwDdiMtQ19q5MNFqXFKpPSZCXKG8aneCmshPc4Fx51oG84f92GUe_AALiDN7a1Al76LweUm9MfiI9R1hfGYOTST2o-EHgsRCcJvsjDQJmzNsi45VvdtPinAL_z-3Jabmwzqt)

[Edit this diagram](https://www.plantuml.com/plantuml/uml/ZP6zJWCn383tF8Ldr8gz0oggG7G112UMgaEANMefSKw92zSYtftaKbrBOM3pp_UTVSuqgOswzSvi60d8jxe3fFGQkLKEOipYOWdZyHNCXPMjuptB6qpw52xXYIpkcZ0BthCQymFwBLKiQpLGmZCZAxSircNsUXLYGU_8uZoLx4_yeIM1oS2Lr9Y-U6pUqqVJPUIpwLwoYwEccylx8RhfMew4NwDdiMtQ19q5MNFqXFKpPSZCXKG8aneCmshPc4Fx51oG84f92GUe_AALiDN7a1Al76LweUm9MfiI9R1hfGYOTST2o-EHgsRCcJvsjDQJmzNsi45VvdtPinAL_z-3Jabmwzqt)

Not pictured is the `CommandFailed` output, which is implicit emitted whenever an input is used when no transition below applies. Also non-state-changing or life-cycle relevant inputs like `GetUTxO` are not mentioned, as well as outputs like `Greetings`, `InvalidInput`, `PeerConnected`, `PeerDisconnected` and `GetUTxOResponse`.

A special case is the `RolledBack` output. This means that the chain rolled back, but it includes no particular information in which state the Hydra Head is now. Frankly, this is quite hard to use - we will improve on this!, but should not invalidate any of the behavioral rules.

8 changes: 4 additions & 4 deletions hydra-node/hydra-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ library
Hydra.Chain.Direct.Tx
Hydra.Chain.Direct.Util
Hydra.Chain.Direct.Wallet
Hydra.ClientInput
Hydra.API.ClientInput
Hydra.ContestationPeriod
Hydra.Crypto
Hydra.HeadLogic
Expand All @@ -111,7 +111,7 @@ library
Hydra.Node.Version
Hydra.Options
Hydra.Party
Hydra.ServerOutput
Hydra.API.ServerOutput
Hydra.Snapshot

other-modules: Paths_hydra_node
Expand Down Expand Up @@ -271,7 +271,7 @@ test-suite tests
Hydra.Chain.Direct.TimeHandleSpec
Hydra.Chain.Direct.TxSpec
Hydra.Chain.Direct.WalletSpec
Hydra.ClientInputSpec
Hydra.API.ClientInputSpec
Hydra.CryptoSpec
Hydra.FireForgetSpec
Hydra.HeadLogicSpec
Expand All @@ -287,7 +287,7 @@ test-suite tests
Hydra.NodeSpec
Hydra.OptionsSpec
Hydra.PartySpec
Hydra.ServerOutputSpec
Hydra.API.ServerOutputSpec
Hydra.SnapshotStrategySpec
Paths_hydra_node
Spec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE UndecidableInstances #-}

module Hydra.ClientInput where
module Hydra.API.ClientInput where

import Hydra.Prelude

Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/API/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import Control.Concurrent.STM.TChan (newBroadcastTChanIO, writeTChan)
import Control.Concurrent.STM.TVar (TVar, modifyTVar', newTVarIO, readTVar)
import Control.Exception (IOException)
import qualified Data.Aeson as Aeson
import Hydra.ClientInput (ClientInput)
import Hydra.API.ClientInput (ClientInput)
import Hydra.Ledger (IsTx (..))
import Hydra.Logging (Tracer, traceWith)
import Hydra.Network (IP, PortNumber)
import Hydra.Party (Party)
import Hydra.ServerOutput (ServerOutput (Greetings, InvalidInput))
import Hydra.API.ServerOutput (ServerOutput (Greetings, InvalidInput))
import Network.WebSockets (
acceptRequest,
receiveData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{-# LANGUAGE UndecidableInstances #-}

module Hydra.ServerOutput where
module Hydra.API.ServerOutput where

import Hydra.API.ClientInput (ClientInput (..))
import Hydra.Chain (PostChainTx, PostTxError)
import Hydra.ClientInput (ClientInput (..))
import Hydra.Crypto (MultiSignature)
import Hydra.Ledger (IsTx, UTxOType, ValidationError)
import Hydra.Network (Host)
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/src/Hydra/HeadLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Hydra.Chain (
PostChainTx (..),
PostTxError,
)
import Hydra.ClientInput (ClientInput (..))
import Hydra.API.ClientInput (ClientInput (..))
import Hydra.ContestationPeriod
import Hydra.Crypto (HydraKey, Signature, SigningKey, aggregateInOrder, sign, verify)
import Hydra.Ledger (
Expand All @@ -41,7 +41,7 @@ import Hydra.Ledger (
)
import Hydra.Network.Message (Message (..))
import Hydra.Party (Party (vkey))
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Hydra.Snapshot (ConfirmedSnapshot (..), Snapshot (..), SnapshotNumber, getSnapshot)

-- * Types
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Logging/Monitoring.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Hydra.Logging.Messages (HydraLog (..))
import Hydra.Network (PortNumber)
import Hydra.Network.Message (Message (ReqTx))
import Hydra.Node (HydraNodeLog (BeginEvent, EndEffect, EndEvent))
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Hydra.Snapshot (Snapshot (confirmed))
import System.Metrics.Prometheus.Http.Scrape (serveMetrics)
import System.Metrics.Prometheus.Metric (Metric (CounterMetric, HistogramMetric))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{-# LANGUAGE TypeApplications #-}

module Hydra.ClientInputSpec where
module Hydra.API.ClientInputSpec where

import Hydra.Prelude
import Test.Hydra.Prelude

import Cardano.Binary (serialize')
import Data.Aeson (Result (..), Value (String), fromJSON)
import qualified Data.ByteString.Base16 as Base16
import Hydra.API.ClientInput (ClientInput)
import Hydra.Cardano.Api (serialiseToTextEnvelope, toLedgerTx)
import Hydra.ClientInput (ClientInput)
import Hydra.Ledger.Cardano (Tx)
import Hydra.Ledger.Simple (SimpleTx)
import Test.Aeson.GenericSpecs (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{-# LANGUAGE TypeApplications #-}

module Hydra.ServerOutputSpec where
module Hydra.API.ServerOutputSpec where

import Hydra.Prelude
import Test.Hydra.Prelude

import Hydra.API.ServerOutput (ServerOutput)
import Hydra.Ledger.Cardano (Tx)
import Hydra.Ledger.Simple (SimpleTx)
import Hydra.ServerOutput (ServerOutput)
import Test.Aeson.GenericSpecs (
Settings (..),
defaultSettings,
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/API/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import qualified Data.Aeson as Aeson
import Hydra.API.Server (Server (Server, sendOutput), withAPIServer)
import Hydra.Ledger.Simple (SimpleTx)
import Hydra.Logging (nullTracer, showLogsOnFailure)
import Hydra.ServerOutput (ServerOutput (Greetings, InvalidInput, ReadyToCommit), input)
import Hydra.API.ServerOutput (ServerOutput (Greetings, InvalidInput, ReadyToCommit), input)
import Network.WebSockets (Connection, receiveData, runClient, sendBinaryData)
import Test.Hydra.Fixture (alice)
import Test.Network.Ports (withFreePort)
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/BehaviorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import GHC.Records (getField)
import Hydra.API.Server (Server (..))
import Hydra.Cardano.Api (SigningKey)
import Hydra.Chain (Chain (..), ChainEvent (..), HeadParameters (..), OnChainTx (..), PostChainTx (..))
import Hydra.ClientInput
import Hydra.API.ClientInput
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod), toNominalDiffTime)
import Hydra.Crypto (HydraKey, aggregate, sign)
import Hydra.HeadLogic (
Expand All @@ -46,7 +46,7 @@ import Hydra.Node (
runHydraNode,
)
import Hydra.Party (Party, deriveParty)
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Hydra.Snapshot (Snapshot (..), SnapshotNumber, getSnapshot)
import Test.Aeson.GenericSpecs (roundtripAndGoldenSpecs)
import Test.Hydra.Fixture (alice, aliceSk, bob, bobSk)
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/HeadLogicSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import Hydra.Ledger.Simple (SimpleTx (..), aValidTx, simpleLedger, utxoRef)
import Hydra.Network (Host (..))
import Hydra.Network.Message (Message (AckSn, Connected, ReqSn, ReqTx))
import Hydra.Party (Party (..))
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Hydra.Snapshot (ConfirmedSnapshot (..), Snapshot (..), getSnapshot)
import Test.Aeson.GenericSpecs (roundtripAndGoldenSpecs)
import Test.Hydra.Fixture (alice, aliceSk, bob, bobSk, carol, carolSk, cperiod)
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/Logging/MonitoringSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Hydra.Logging.Messages (HydraLog (Node))
import Hydra.Logging.Monitoring
import Hydra.Network.Message (Message (ReqTx))
import Hydra.Node (HydraNodeLog (BeginEvent, EndEffect))
import Hydra.ServerOutput (ServerOutput (SnapshotConfirmed))
import Hydra.API.ServerOutput (ServerOutput (SnapshotConfirmed))
import Hydra.Snapshot (Snapshot (Snapshot))
import Network.HTTP.Req (GET (..), NoReqBody (..), bsResponse, defaultHttpConfig, http, port, req, responseBody, runReq, (/:))
import Test.Hydra.Fixture (alice)
Expand Down
8 changes: 4 additions & 4 deletions hydra-node/test/Hydra/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ import Hydra.BehaviorSpec (
import Hydra.Cardano.Api.Prelude (fromShelleyPaymentCredential)
import Hydra.Chain (HeadParameters (..))
import Hydra.Chain.Direct.Fixture (defaultGlobals, defaultLedgerEnv, testNetworkId)
import Hydra.ClientInput (ClientInput (NewTx))
import qualified Hydra.ClientInput as Input
import Hydra.API.ClientInput (ClientInput (NewTx))
import qualified Hydra.API.ClientInput as Input
import Hydra.Crypto (HydraKey)
import Hydra.HeadLogic (Committed, PendingCommits)
import Hydra.Ledger (IsTx (..))
import Hydra.Ledger.Cardano (cardanoLedger, genAdaValue, genKeyPair, genSigningKey, mkSimpleTx)
import Hydra.Logging (Tracer)
import Hydra.Node (HydraNodeLog, runHydraNode)
import Hydra.Party (Party, deriveParty)
import Hydra.ServerOutput (ServerOutput (GetUTxOResponse, ReadyToCommit, SnapshotConfirmed))
import qualified Hydra.ServerOutput as Output
import Hydra.API.ServerOutput (ServerOutput (GetUTxOResponse, ReadyToCommit, SnapshotConfirmed))
import qualified Hydra.API.ServerOutput as Output
import qualified Hydra.Snapshot as Snapshot
import Test.QuickCheck (elements, frequency, resize, sized, suchThat, tabulate, vectorOf)
import Test.QuickCheck.DynamicLogic (DynLogicModel)
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/ModelSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ import qualified Data.Map as Map
import qualified Data.Set as Set
import Hydra.BehaviorSpec (TestHydraNode (..))
import Hydra.Chain.Direct.Fixture (testNetworkId)
import Hydra.ClientInput (ClientInput (..))
import Hydra.API.ClientInput (ClientInput (..))
import Hydra.Model (
GlobalState (..),
Nodes (Nodes, nodes),
OffChainState (..),
WorldState (..),
)
import Hydra.Party (Party (..), deriveParty)
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Test.QuickCheck (Property, counterexample, forAll, property, withMaxSuccess, within)
import Test.QuickCheck.Gen.Unsafe (Capture (Capture), capture)
import Test.QuickCheck.Monadic (PropertyM, assert, monadic', monitor, run)
Expand Down
4 changes: 2 additions & 2 deletions hydra-node/test/Hydra/NodeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Hydra.Chain (
PostChainTx (InitTx),
PostTxError (NoSeedInput),
)
import Hydra.ClientInput (ClientInput (..))
import Hydra.API.ClientInput (ClientInput (..))
import Hydra.Crypto (HydraKey, sign)
import Hydra.HeadLogic (
Environment (..),
Expand All @@ -38,7 +38,7 @@ import Hydra.Node (
stepHydraNode,
)
import Hydra.Party (Party, deriveParty)
import Hydra.ServerOutput (ServerOutput (PostTxOnChainFailed))
import Hydra.API.ServerOutput (ServerOutput (PostTxOnChainFailed))
import Hydra.Snapshot (Snapshot (..))
import Test.Hydra.Fixture (alice, aliceSk, bob, bobSk, carol, carolSk, cperiod)

Expand Down
4 changes: 2 additions & 2 deletions hydra-tui/src/Hydra/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import Hydra.Cardano.Api (
SigningKey,
)
import Hydra.Chain.Direct.Util (readFileTextEnvelopeThrow)
import Hydra.ClientInput (ClientInput)
import Hydra.API.ClientInput (ClientInput)
import Hydra.Ledger (IsTx)
import Hydra.Network (Host (Host, hostname, port))
import Hydra.ServerOutput (ServerOutput)
import Hydra.API.ServerOutput (ServerOutput)
import Hydra.TUI.Options (Options (..))
import Network.WebSockets (ConnectionException, receiveData, runClient, sendBinaryData)

Expand Down
4 changes: 2 additions & 2 deletions hydra-tui/src/Hydra/TUI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ import Graphics.Vty (
)
import qualified Graphics.Vty as Vty
import Graphics.Vty.Attributes (defAttr)
import Hydra.API.ClientInput (ClientInput (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Hydra.Chain.CardanoClient (CardanoClient (..), mkCardanoClient)
import Hydra.Chain.Direct.Util (isMarkedOutput)
import Hydra.Client (Client (..), HydraEvent (..), withClient)
import Hydra.ClientInput (ClientInput (..))
import Hydra.ContestationPeriod (ContestationPeriod (UnsafeContestationPeriod))
import Hydra.Ledger (IsTx (..))
import Hydra.Ledger.Cardano (mkSimpleTx)
import Hydra.Network (Host (..))
import Hydra.Party (Party (..))
import Hydra.ServerOutput (ServerOutput (..))
import Hydra.Snapshot (Snapshot (..))
import Hydra.TUI.Options (Options (..))
import Lens.Micro (Lens', lens, (%~), (.~), (?~), (^.), (^?))
Expand Down
4 changes: 2 additions & 2 deletions hydraw/src/Hydra/Painter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import qualified Data.Aeson as Aeson
import qualified Data.Map as Map
import Hydra.Cardano.Api
import Hydra.Chain.Direct.Util (readFileTextEnvelopeThrow)
import Hydra.ClientInput (ClientInput (GetUTxO, NewTx))
import Hydra.API.ClientInput (ClientInput (GetUTxO, NewTx))
import Hydra.Ledger.Cardano (emptyTxBody)
import Hydra.Network (Host (..))
import Hydra.Prelude
import Hydra.ServerOutput (ServerOutput (GetUTxOResponse))
import Hydra.API.ServerOutput (ServerOutput (GetUTxOResponse))
import Network.WebSockets (
Connection,
runClient,
Expand Down

0 comments on commit 23af129

Please sign in to comment.