Skip to content

Commit

Permalink
Fix GetHeadIds read model
Browse files Browse the repository at this point in the history
We must access the TVar in the actual GetHeadIds IO action instead only
when setting it up.
  • Loading branch information
ch1bo committed Jan 12, 2024
1 parent e2ec89a commit 0c1ec19
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions hydra-explorer/src/Hydra/Explorer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import System.Environment (withArgs)
type ExplorerState = [HeadObservation]

observerHandler :: TVar IO ExplorerState -> ExplorerState -> IO ()
observerHandler explorerState observations =
observerHandler explorerState observations = do
atomically $
modifyTVar' explorerState (<> observations)

main :: IO ()
main = do
withTracer (Verbose "hydra-explorer") $ \tracer -> do
explorerState <- newTVarIO (mempty :: ExplorerState)
getHeadIds <- getHeadIdsReadModel explorerState
let getHeadIds = readModelGetHeadIds explorerState
args <- getArgs
race
-- FIXME: this is going to be problematic on mainnet.
Expand All @@ -64,23 +64,22 @@ main = do
putStrLn $ "Listening on: tcp/" <> show port
)

getHeadIdsReadModel :: TVar IO ExplorerState -> IO GetHeadIds
getHeadIdsReadModel tv = atomically $ do
readModelGetHeadIds :: TVar IO ExplorerState -> GetHeadIds
readModelGetHeadIds tv = atomically $ do
currentState <- readTVar tv
let headIds =
mapMaybe
( \case
NoHeadTx -> Nothing
Init InitObservation{headId} -> Just headId
Abort AbortObservation{headId} -> Just headId
Commit CommitObservation{headId} -> Just headId
CollectCom CollectComObservation{headId} -> Just headId
Close CloseObservation{headId} -> Just headId
Contest ContestObservation{headId} -> Just headId
Fanout FanoutObservation{headId} -> Just headId
)
currentState
pure $ pure headIds
pure $
mapMaybe
( \case
NoHeadTx -> Nothing
Init InitObservation{headId} -> Just headId
Abort AbortObservation{headId} -> Just headId
Commit CommitObservation{headId} -> Just headId
CollectCom CollectComObservation{headId} -> Just headId
Close CloseObservation{headId} -> Just headId
Contest ContestObservation{headId} -> Just headId
Fanout FanoutObservation{headId} -> Just headId
)
currentState

type GetHeadIds = IO [HeadId]

Expand Down

0 comments on commit 0c1ec19

Please sign in to comment.