Skip to content

Commit

Permalink
Add conversions of ChainPoint to/from primitive types
Browse files Browse the repository at this point in the history
… to be removed later
  • Loading branch information
HeinrichApfelmus committed Apr 16, 2024
1 parent 9f71904 commit aec5d98
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Types/Block.hs
Expand Up @@ -14,6 +14,9 @@ module Cardano.Wallet.Primitive.Types.Block
, isGenesisBlockHeader
, compareSlot
, chainPointFromBlockHeader
, chainPointFromBlockHeader'
, toWalletChainPoint
, fromWalletChainPoint
, toSlot
)

Expand All @@ -22,21 +25,32 @@ 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
)
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)
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aec5d98

Please sign in to comment.