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 a data type `ChainPoint` to the `Cardano.Wallet.Read` hierarchy. In order to add this type, we also need to * Add functions related to hashing. The `Cardano.Crypto.Hash.Class` provides a very thoughtful API, we re-export it. * Add a type `RawHeaderHash` that is an era-independent string of bytes representing a `HeaderHash`. ### Comments * This `ChainPoint` type is meant to be consistent with the other types in the `Cardano.Wallet.Read` hierarchy, which are in turn meant to be consistent with the ledger specification. * The ledger specification stipulates that `SlotNo ~ Natural`. We stick to this type. * The goal is to eventually remove the old `primitive` types. ### Issue Number ADP-3350
- Loading branch information
Showing
6 changed files
with
161 additions
and
5 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
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
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
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
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,64 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{- | | ||
Copyright: © 2024 Cardano Foundation | ||
License: Apache-2.0 | ||
Data types relating to the consensus about the blockchain. | ||
-} | ||
module Cardano.Wallet.Read.Chain | ||
( ChainPoint (GenesisPoint, BlockPoint) | ||
, getChainPoint | ||
, prettyChainPoint | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Read.Block | ||
( Block | ||
, RawHeaderHash | ||
, SlotNo (..) | ||
, getEraHeaderHash | ||
, getEraSlotNo | ||
, getRawHeaderHash | ||
) | ||
import Cardano.Wallet.Read.Eras | ||
( IsEra | ||
) | ||
import GHC.Generics | ||
( Generic | ||
) | ||
import NoThunks.Class | ||
( NoThunks (..) | ||
) | ||
|
||
import qualified Cardano.Wallet.Read.Hash as Hash | ||
import qualified Data.Text as T | ||
|
||
{----------------------------------------------------------------------------- | ||
ChainPoint | ||
------------------------------------------------------------------------------} | ||
|
||
-- | A point (block) on the Cardano blockchain. | ||
data ChainPoint | ||
= GenesisPoint | ||
| BlockPoint !SlotNo !RawHeaderHash | ||
deriving (Eq, Ord, Show, Generic) | ||
|
||
instance NoThunks ChainPoint | ||
|
||
{-# INLINABLE getChainPoint #-} | ||
getChainPoint :: IsEra era => Block era -> ChainPoint | ||
getChainPoint block = | ||
BlockPoint | ||
(getEraSlotNo block) | ||
(getRawHeaderHash $ getEraHeaderHash block) | ||
|
||
-- | Short printed representation of a 'ChainPoint'. | ||
prettyChainPoint :: ChainPoint -> T.Text | ||
prettyChainPoint GenesisPoint = | ||
"[point genesis]" | ||
prettyChainPoint (BlockPoint slot hash) = | ||
"[point " <> hashF hash <> " at slot " <> slotF slot <> "]" | ||
where | ||
hashF = T.take 8 . Hash.hashToTextAsHex | ||
slotF (SlotNo n) = T.pack (show n) |
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,38 @@ | ||
{- | | ||
Copyright: © 2024 Cardano Foundation | ||
License: Apache-2.0 | ||
Abstract and specific Hash functionality. | ||
-} | ||
module Cardano.Wallet.Read.Hash | ||
( -- * Core operations | ||
H.Hash | ||
, H.HashAlgorithm (H.digest, H.hashAlgorithmName) | ||
, H.sizeHash | ||
, H.hashWith | ||
|
||
-- * Conversions | ||
, H.castHash | ||
, H.hashToBytes | ||
, H.hashFromBytes | ||
, H.hashToBytesShort | ||
, H.hashFromBytesShort | ||
|
||
-- * Rendering and parsing | ||
, H.hashToBytesAsHex | ||
, H.hashFromBytesAsHex | ||
, H.hashToTextAsHex | ||
, H.hashFromTextAsHex | ||
, H.hashToStringAsHex | ||
, H.hashFromStringAsHex | ||
|
||
-- * Specific Hash algorithms | ||
, Blake2b_224 | ||
, Blake2b_256 | ||
) where | ||
|
||
import Cardano.Crypto.Hash.Blake2b | ||
( Blake2b_224 | ||
, Blake2b_256 | ||
) | ||
import qualified Cardano.Crypto.Hash.Class as H |