Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversions of
ChainPoint
to/from ouroboros types
- Loading branch information
1 parent
aec5d98
commit 7609210
Showing
2 changed files
with
66 additions
and
0 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 |