Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
105 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/network-layer/src/Cardano/Wallet/Network/Implementation/Types.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters