Skip to content

Commit

Permalink
Actually (partially) parse confirmed snapshot from Hydra
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ authored and ch1bo committed Jul 18, 2023
1 parent 8af54f3 commit 418f735
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
34 changes: 4 additions & 30 deletions src/Kupo/App/ChainSync/Hydra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

{-# LANGUAGE PatternSynonyms #-}
module Kupo.App.ChainSync.Hydra
( connect
, runChainSyncClient
Expand All @@ -16,17 +15,13 @@ import Control.Exception.Safe
import Kupo.App.Mailbox
( Mailbox
, putHighFrequencyMessage
, putIntermittentMessage
)
import Kupo.Control.MonadSTM
( MonadSTM (..)
)
import Kupo.Data.Cardano
( Point
, SlotNo
, Tip
, WithOrigin
, pointSlot
)
import Kupo.Data.ChainSync
( IntersectionNotFoundException (..)
Expand All @@ -35,22 +30,10 @@ import Kupo.Data.Ogmios
( PartialBlock
)

import qualified Data.ByteString as BS
import Kupo.Data.Cardano.HeaderHash
( unsafeHeaderHashFromBytes
)
import Kupo.Data.Cardano.Point
( pattern BlockPoint
)
import Kupo.Data.Cardano.Tip
( pattern Tip
)
import Kupo.Data.Hydra
( HydraMessage (..)
, decodeHydraMessage
)
import Kupo.Data.PartialBlock
( PartialBlock (..)
, fromSnapshot
)
import qualified Network.WebSockets as WS
import qualified Network.WebSockets.Json as WS
Expand All @@ -66,21 +49,12 @@ runChainSyncClient
-> [Point]
-> WS.Connection
-> m IntersectionNotFoundException
runChainSyncClient mailbox beforeMainLoop pts ws = do
runChainSyncClient mailbox beforeMainLoop _pts ws = do
beforeMainLoop
forever $ do
WS.receiveJson ws decodeHydraMessage >>= \case
SnapshotConfirmed{} -> do
let headerHash = unsafeHeaderHashFromBytes $ BS.replicate 32 0
let slotNo = 1 -- TODO: decode snapshot number as slot
let blockNo = 1
let tip = Tip slotNo headerHash blockNo
let block =
PartialBlock
{ blockPoint = BlockPoint slotNo headerHash
, blockBody = []
}
atomically (putHighFrequencyMessage mailbox (tip, block))
SnapshotConfirmed{ snapshot } -> do
atomically (putHighFrequencyMessage mailbox (fromSnapshot snapshot))
SomethingElse -> pure ()

connect
Expand Down
65 changes: 58 additions & 7 deletions src/Kupo/Data/Hydra.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,74 @@
{-# LANGUAGE PatternSynonyms #-}

module Kupo.Data.Hydra where

import Kupo.Prelude

import Cardano.Crypto.Hash
( hashToBytes
, hashWith
)
import Data.Aeson
( (.:)
)
import Kupo.Data.Cardano
( BlockNo (..)
, SlotNo (..)
, Tip
, pattern BlockPoint
, pattern Tip
, unsafeHeaderHashFromBytes
)
import Kupo.Data.PartialBlock
( PartialBlock (..)
)

import qualified Data.Aeson.Types as Json
import qualified Data.ByteString.Builder as BS

-- Types

data HydraMessage
= SnapshotConfirmed
| SomethingElse
= SnapshotConfirmed { snapshot :: Snapshot }
| SomethingElse

data Snapshot = Snapshot
{ number :: Word64
}

fromSnapshot :: Snapshot -> (Tip, PartialBlock)
fromSnapshot Snapshot { number } = do
let
headerHash = number
& hashWith @Blake2b_256 (toStrict . BS.toLazyByteString . BS.word64BE)
& hashToBytes
& unsafeHeaderHashFromBytes

slotNo =
SlotNo number

blockNo =
BlockNo number
in
( Tip slotNo headerHash blockNo
, PartialBlock
{ blockPoint = BlockPoint slotNo headerHash
, blockBody = []
}
)

-- Decoders

decodeHydraMessage :: Json.Value -> Json.Parser HydraMessage
decodeHydraMessage =
Json.withObject "HydraMessage" $ \o -> do
tag <- o .: "tag"
case tag of
("SnapshotConfirmed" :: Text) -> pure SnapshotConfirmed
_ -> pure SomethingElse
Json.withObject "HydraMessage" $ \o -> do
tag <- o .: "tag"
case tag of
("SnapshotConfirmed" :: Text) -> SnapshotConfirmed <$> decodeSnapshotConfirmed o
_ -> pure SomethingElse

decodeSnapshotConfirmed :: Json.Object -> Json.Parser Snapshot
decodeSnapshotConfirmed o = do
snapshot <- o .: "snapshot"
number <- snapshot .: "snapshotNumber"
pure $ Snapshot { number }

0 comments on commit 418f735

Please sign in to comment.