From dd2201eb7b74f95ac83edace636f70bc110a3f96 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Wed, 17 Apr 2024 16:30:34 +0200 Subject: [PATCH] Use records for `ChainPoint` and `ChainTip` --- .../Wallet/Network/Implementation/Types.hs | 9 +++-- lib/read/lib/Cardano/Wallet/Read/Chain.hs | 34 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/network-layer/src/Cardano/Wallet/Network/Implementation/Types.hs b/lib/network-layer/src/Cardano/Wallet/Network/Implementation/Types.hs index 18c61b6280b..1e5c020d4ea 100644 --- a/lib/network-layer/src/Cardano/Wallet/Network/Implementation/Types.hs +++ b/lib/network-layer/src/Cardano/Wallet/Network/Implementation/Types.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE NamedFieldPuns #-} {-| Copyright: © 2024 Cardano Foundation License: Apache-2.0 @@ -63,8 +65,11 @@ fromOuroborosPoint (O.BlockPoint slot h) = toOuroborosTip :: ChainTip -> O.Tip (CardanoBlock sc) toOuroborosTip GenesisTip = O.TipGenesis -toOuroborosTip (BlockTip slot h blockNo) = - O.Tip (toCardanoSlotNo slot) (toCardanoHash h) (toCardanoBlockNo blockNo) +toOuroborosTip BlockTip{slotNo,headerHash,blockNo} = + O.Tip + (toCardanoSlotNo slotNo) + (toCardanoHash headerHash) + (toCardanoBlockNo blockNo) fromOuroborosTip :: O.Tip (CardanoBlock sc) -> ChainTip fromOuroborosTip O.TipGenesis = diff --git a/lib/read/lib/Cardano/Wallet/Read/Chain.hs b/lib/read/lib/Cardano/Wallet/Read/Chain.hs index 371bfaa1b0a..2d950d493b0 100644 --- a/lib/read/lib/Cardano/Wallet/Read/Chain.hs +++ b/lib/read/lib/Cardano/Wallet/Read/Chain.hs @@ -1,4 +1,7 @@ {-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE NoFieldSelectors #-} +{-# LANGUAGE NamedFieldPuns #-} {- | Copyright: © 2024 Cardano Foundation License: Apache-2.0 @@ -7,13 +10,13 @@ Data types relating to the consensus about the blockchain. -} module Cardano.Wallet.Read.Chain ( -- * ChainPoint - ChainPoint (GenesisPoint, BlockPoint) + ChainPoint (..) , getChainPoint , prettyChainPoint , chainPointFromChainTip -- * ChainTip - , ChainTip (GenesisTip, BlockTip) + , ChainTip (..) , getChainTip , prettyChainTip ) where @@ -50,7 +53,10 @@ import qualified Data.Text as T -- | A point (block) on the Cardano blockchain. data ChainPoint = GenesisPoint - | BlockPoint !SlotNo !RawHeaderHash + | BlockPoint + { slotNo :: !SlotNo + , headerHash :: !RawHeaderHash + } deriving (Eq, Ord, Show, Generic) instance NoThunks ChainPoint @@ -59,8 +65,9 @@ instance NoThunks ChainPoint getChainPoint :: IsEra era => Block era -> ChainPoint getChainPoint block = BlockPoint - (getEraSlotNo block) - (getRawHeaderHash $ getEraHeaderHash block) + { slotNo = getEraSlotNo block + , headerHash = getRawHeaderHash $ getEraHeaderHash block + } -- | Short printed representation of a 'ChainPoint'. prettyChainPoint :: ChainPoint -> T.Text @@ -84,7 +91,11 @@ chainPointFromChainTip (BlockTip slot hash _) = BlockPoint slot hash -- Records the 'ChainPoint' and the 'BlockNo' of the block. data ChainTip = GenesisTip - | BlockTip !SlotNo !RawHeaderHash !BlockNo + | BlockTip + { slotNo :: !SlotNo + , headerHash :: !RawHeaderHash + , blockNo :: !BlockNo + } deriving (Eq, Ord, Show, Generic) instance NoThunks ChainTip @@ -93,16 +104,17 @@ instance NoThunks ChainTip getChainTip :: IsEra era => Block era -> ChainTip getChainTip block = BlockTip - (getEraSlotNo block) - (getRawHeaderHash $ getEraHeaderHash block) - (getEraBlockNo block) + { slotNo = getEraSlotNo block + , headerHash = getRawHeaderHash $ getEraHeaderHash block + , blockNo = getEraBlockNo block + } -- | Short printed representation of a 'ChainPoint'. prettyChainTip :: ChainTip -> T.Text prettyChainTip GenesisTip = "[tip genesis]" -prettyChainTip (BlockTip slotNo hash blockNo) = - "[tip " <> hashF hash +prettyChainTip BlockTip{slotNo,headerHash,blockNo} = + "[tip " <> hashF headerHash <> " at slot " <> slotNoF slotNo <> " at blockNo " <> blockNoF blockNo <> "]"