Skip to content

Commit

Permalink
move back loadState
Browse files Browse the repository at this point in the history
  • Loading branch information
rrruko committed Dec 18, 2023
1 parent 0306ce8 commit 6a54f77
Showing 4 changed files with 23 additions and 19 deletions.
17 changes: 0 additions & 17 deletions hydra-node/src/Hydra/Chain/Offline.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Hydra.Chain.Offline (
withOfflineChain,
loadState,
) where

import Hydra.Prelude
@@ -135,19 +134,3 @@ withOfflineChain tracer OfflineConfig{ledgerGenesisFile, initialUTxOFile} global
case res of
Left () -> error "'connectTo' cannot terminate but did?"
Right a -> pure a

-- | Load a 'HeadState' from persistence.
loadState ::
(MonadThrow m, IsChainState tx) =>
Tracer m (HydraNodeLog tx) ->
PersistenceIncremental (StateChanged tx) m ->
ChainStateType tx ->
m (HeadState tx, ChainStateHistory tx)
loadState tracer persistence defaultChainState = do
events <- loadAll persistence
traceWith tracer LoadedState{numberOfEvents = fromIntegral $ length events}
let headState = recoverState initialState events
chainStateHistory = recoverChainStateHistory defaultChainState events
pure (headState, chainStateHistory)
where
initialState = Idle IdleState{chainState = defaultChainState}
20 changes: 20 additions & 0 deletions hydra-node/src/Hydra/Node.hs
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import Hydra.API.Server (Server, sendOutput)
import Hydra.Cardano.Api (AsType (AsPaymentKey, AsSigningKey, AsVerificationKey), getVerificationKey)
import Hydra.Chain (
Chain (..),
ChainStateHistory,
ChainStateType,
HeadParameters (..),
IsChainState,
@@ -34,10 +35,13 @@ import Hydra.HeadLogic (
Environment (..),
Event (..),
HeadState (..),
IdleState (..),
Outcome (..),
aggregateState,
collectEffects,
defaultTTL,
recoverChainStateHistory,
recoverState,
)
import Hydra.HeadLogic qualified as Logic
import Hydra.HeadLogic.Outcome (StateChanged (..))
@@ -286,3 +290,19 @@ createNodeState initialState = do
{ modifyHeadState = stateTVar tv
, queryHeadState = readTVar tv
}

-- | Load a 'HeadState' from persistence.
loadState ::
(MonadThrow m, IsChainState tx) =>
Tracer m (HydraNodeLog tx) ->
PersistenceIncremental (StateChanged tx) m ->
ChainStateType tx ->
m (HeadState tx, ChainStateHistory tx)
loadState tracer persistence defaultChainState = do
events <- loadAll persistence
traceWith tracer LoadedState{numberOfEvents = fromIntegral $ length events}
let headState = recoverState initialState events
chainStateHistory = recoverChainStateHistory defaultChainState events
pure (headState, chainStateHistory)
where
initialState = Idle IdleState{chainState = defaultChainState}
3 changes: 2 additions & 1 deletion hydra-node/src/Hydra/Node/Run.hs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ import Hydra.Chain.CardanoClient (QueryPoint (..), queryGenesisParameters)
import Hydra.Chain.Direct (loadChainContext, mkTinyWallet, withDirectChain)
import Hydra.Chain.Direct.Fixture (defaultGlobals)
import Hydra.Chain.Direct.State (initialChainState)
import Hydra.Chain.Offline (loadState, withOfflineChain)
import Hydra.Chain.Offline (withOfflineChain)
import Hydra.HeadId (HeadId (..))
import Hydra.HeadLogic (
Environment (..),
@@ -50,6 +50,7 @@ import Hydra.Node (
createNodeState,
initEnvironment,
initEnvironmentOffline,
loadState,
runHydraNode,
)
import Hydra.Node.EventQueue (EventQueue (..), createEventQueue)
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/NodeSpec.hs
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import Hydra.API.Server (Server (..))
import Hydra.API.ServerOutput (ServerOutput (..))
import Hydra.Cardano.Api (SigningKey)
import Hydra.Chain (Chain (..), ChainEvent (..), HeadParameters (..), IsChainState, OnChainTx (..), PostTxError (NoSeedInput))
import Hydra.Chain.Offline (loadState)
import Hydra.ContestationPeriod (ContestationPeriod (..))
import Hydra.Crypto (HydraKey, sign)
import Hydra.HeadLogic (
@@ -33,6 +32,7 @@ import Hydra.Node (
HydraNodeLog (..),
checkHeadState,
createNodeState,
loadState,
stepHydraNode,
)
import Hydra.Node.EventQueue (EventQueue (..), createEventQueue)

0 comments on commit 6a54f77

Please sign in to comment.