Skip to content

Commit

Permalink
[ADP-3350] Conversion to/from Read.ChainPoint (#4540)
Browse files Browse the repository at this point in the history
This pull request adds conversion function to/from the data type
`ChainPoint` that belongs to the `Cardano.Wallet.Read` hierarchy.

This includes
* Conversions to/from legacy types in `primitive`
* Conversions to/from types in `ouroboros-consensus` for the networking
layer

### Comments

* The goal is to eventually remove the old `primitive` types. However,
we add conversions `Read.ChainPoint ⟷ ChainPoint` here in order to have
more control over the source code impact of future pull requests.
Specifically, we can then change the types in `NetworkLayer` from
`ChainPoint` to `Read.ChainPoint` while touching only a small portion of
the code in `Cardano.Wallet` thanks to a cleverly inserted conversion.

### Issue Number

ADP-3350
  • Loading branch information
HeinrichApfelmus committed Apr 16, 2024
2 parents 9f71904 + 7609210 commit 352ed60
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/network-layer/cardano-wallet-network-layer.cabal
Expand Up @@ -47,6 +47,7 @@ library
Cardano.Wallet.Network.Implementation
Cardano.Wallet.Network.Implementation.Ouroboros
Cardano.Wallet.Network.Implementation.UnliftIO
Cardano.Wallet.Network.Implementation.Types
Cardano.Wallet.Network.Light
Cardano.Wallet.Network.LocalStateQuery
Cardano.Wallet.Network.LocalStateQuery.Extra
Expand Down
@@ -0,0 +1,65 @@
{-|
Copyright: © 2024 Cardano Foundation
License: Apache-2.0
Conversions between types from @ouroboros-consensus@
and "Cardano.Wallet.Read".
-}
module Cardano.Wallet.Network.Implementation.Types
( fromOuroborosPoint
, toOuroborosPoint
) where

import Prelude

import Cardano.Wallet.Read
( BHeader
, ChainPoint (..)
, SlotNo (..)
)
import Cardano.Wallet.Read.Hash
( Blake2b_256
, Hash
, hashFromBytesShort
, hashToBytesShort
)
import Data.Maybe
( fromJust
)
import Ouroboros.Consensus.Cardano.Block
( CardanoBlock
, CardanoEras
)
import Ouroboros.Consensus.HardFork.Combinator
( OneEraHash (..)
)

import qualified Ouroboros.Network.Block as O

{-----------------------------------------------------------------------------
ChainPoint conversions
------------------------------------------------------------------------------}

toOuroborosPoint :: ChainPoint -> O.Point (CardanoBlock sc)
toOuroborosPoint GenesisPoint =
O.GenesisPoint
toOuroborosPoint (BlockPoint slot h) =
O.BlockPoint (toCardanoSlotNo slot) (toCardanoHash h)

toCardanoSlotNo :: SlotNo -> O.SlotNo
toCardanoSlotNo (SlotNo slot) = O.SlotNo (toEnum $ fromEnum slot)

toCardanoHash :: Hash Blake2b_256 BHeader -> OneEraHash (CardanoEras sc)
toCardanoHash = OneEraHash . hashToBytesShort

fromOuroborosPoint :: O.Point (CardanoBlock sc) -> ChainPoint
fromOuroborosPoint O.GenesisPoint =
GenesisPoint
fromOuroborosPoint (O.BlockPoint slot h) =
BlockPoint (fromCardanoSlotNo slot) (fromCardanoHash h)

fromCardanoSlotNo :: O.SlotNo -> SlotNo
fromCardanoSlotNo (O.SlotNo slot) = SlotNo (fromIntegral slot)

fromCardanoHash :: OneEraHash (CardanoEras sc) -> Hash Blake2b_256 BHeader
fromCardanoHash = fromJust . hashFromBytesShort . getOneEraHash
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 352ed60

Please sign in to comment.