-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actually (partially) parse confirmed snapshot from Hydra
- Loading branch information
Showing
2 changed files
with
62 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |