From e910b831dd401dd0086d8adac760256ace659925 Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Wed, 30 Nov 2022 10:38:05 +0100 Subject: [PATCH 1/2] Add ToJSON/FromJSON instances for ChainPoint There is already a ToJSON instance for ChainTip so we figured we could upstream this from hydra-cardano-api. --- cardano-api/src/Cardano/Api/Block.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cardano-api/src/Cardano/Api/Block.hs b/cardano-api/src/Cardano/Api/Block.hs index f91229d615c..0c557deb214 100644 --- a/cardano-api/src/Cardano/Api/Block.hs +++ b/cardano-api/src/Cardano/Api/Block.hs @@ -358,6 +358,24 @@ instance Ord ChainPoint where compare _ ChainPointAtGenesis = GT compare (ChainPoint sn _) (ChainPoint sn' _) = compare sn sn' +instance ToJSON ChainPoint where + toJSON = \case + ChainPointAtGenesis -> object ["tag" .= String "ChainPointAtGenesis"] + ChainPoint slot blockHash -> + object + [ "tag" .= String "ChainPoint" + , "slot" .= toJSON slot + , "blockHash" .= toJSON blockHash + ] + +instance FromJSON ChainPoint where + parseJSON = withObject "ChainPoint" $ \o -> do + tag <- o .: "tag" + case tag :: Text of + "ChainPointAtGenesis" -> pure ChainPointAtGenesis + "ChainPoint" -> ChainPoint <$> o .: "slot" <*> o .: "blockHash" + _ -> fail "Expected tag to be ChainPointAtGenesis | ChainPoint" + toConsensusPointInMode :: ConsensusMode mode -> ChainPoint -> Consensus.Point (ConsensusBlockForMode mode) From 3b01002f548a7fdbe847c7997cdcba20edb75c90 Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Wed, 30 Nov 2022 12:23:38 +0100 Subject: [PATCH 2/2] Add LambdaCase pragma --- cardano-api/src/Cardano/Api/Block.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/cardano-api/src/Cardano/Api/Block.hs b/cardano-api/src/Cardano/Api/Block.hs index 0c557deb214..51917d04b49 100644 --- a/cardano-api/src/Cardano/Api/Block.hs +++ b/cardano-api/src/Cardano/Api/Block.hs @@ -2,6 +2,7 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-}