From aec5d98e3da9d96f508fd6b21d4ef3c37348a3fa Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Fri, 12 Apr 2024 13:41:18 +0200 Subject: [PATCH] Add conversions of `ChainPoint` to/from primitive types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … to be removed later --- .../Cardano/Wallet/Primitive/Types/Block.hs | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Block.hs b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Block.hs index fe32933afde..ebeefdbe329 100644 --- a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Block.hs +++ b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Block.hs @@ -14,6 +14,9 @@ module Cardano.Wallet.Primitive.Types.Block , isGenesisBlockHeader , compareSlot , chainPointFromBlockHeader + , chainPointFromBlockHeader' + , toWalletChainPoint + , fromWalletChainPoint , toSlot ) @@ -22,14 +25,14 @@ where import Prelude import Cardano.Slotting.Slot - ( SlotNo + ( SlotNo (..) , WithOrigin (..) ) import Cardano.Wallet.Primitive.Types.Certificates ( DelegationCertificate ) import Cardano.Wallet.Primitive.Types.Hash - ( Hash (getHash) + ( Hash (..) ) import Cardano.Wallet.Primitive.Types.Tx.Tx ( Tx @@ -37,6 +40,17 @@ import Cardano.Wallet.Primitive.Types.Tx.Tx import Control.DeepSeq ( NFData ) +import Control.Lens + ( view + ) +import Data.ByteArray.Encoding + ( Base (Base16) + , convertToBase + ) +import Data.Maybe + ( fromJust + , isNothing + ) import Data.Quantity ( Quantity (getQuantity) ) @@ -57,16 +71,8 @@ import NoThunks.Class ( NoThunks ) -import Control.Lens - ( view - ) -import Data.ByteArray.Encoding - ( Base (Base16) - , convertToBase - ) -import Data.Maybe - ( isNothing - ) +import qualified Cardano.Wallet.Read as Read +import qualified Cardano.Wallet.Read.Hash as Hash import qualified Data.Text.Encoding as T data Block = Block @@ -145,6 +151,27 @@ chainPointFromBlockHeader header@(BlockHeader sl _ hash _) | isGenesisBlockHeader header = ChainPointAtGenesis | otherwise = ChainPoint sl hash +chainPointFromBlockHeader' :: BlockHeader -> Read.ChainPoint +chainPointFromBlockHeader' = + fromWalletChainPoint . chainPointFromBlockHeader + +toWalletChainPoint :: Read.ChainPoint -> ChainPoint +toWalletChainPoint Read.GenesisPoint = ChainPointAtGenesis +toWalletChainPoint (Read.BlockPoint (Read.SlotNo slot) hash) = + ChainPoint + (SlotNo $ fromIntegral slot) + (Hash $ Hash.hashToBytes hash) + +fromWalletChainPoint :: ChainPoint -> Read.ChainPoint +fromWalletChainPoint ChainPointAtGenesis = Read.GenesisPoint +fromWalletChainPoint (ChainPoint slot hash) = + Read.BlockPoint + (toReadSlotNo slot) + (fromJust $ Hash.hashFromBytes $ getHash hash) + +toReadSlotNo :: SlotNo -> Read.SlotNo +toReadSlotNo (SlotNo n) = Read.SlotNo (fromIntegral n) + instance NFData ChainPoint instance NoThunks ChainPoint