Skip to content

Commit

Permalink
Add Block to PrevHeaderHash era dependent extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Nov 1, 2023
1 parent ea523db commit d2a8001
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/read/cardano-wallet-read.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ library
, lens
, memory
, ouroboros-network-api
, ouroboros-consensus
, ouroboros-consensus-cardano
, ouroboros-consensus-protocol
, text
Expand Down
60 changes: 58 additions & 2 deletions lib/read/lib/Cardano/Wallet/Read/Block/HeaderHash.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Wallet.Read.Block.HeaderHash (getHeaderHash) where
module Cardano.Wallet.Read.Block.HeaderHash
( HeaderHash (..)
, PrevHeaderHash (..)
, HeaderHashT
, PrevHeaderHashT
, getHeaderHash
, getPrevHeaderHash
)
where

import Prelude

Expand All @@ -25,14 +33,21 @@ import Cardano.Ledger.Era
( Era
, EraSegWits (..)
)
import Cardano.Protocol.TPraos.BHeader
( PrevHash
)
import Cardano.Wallet.Read
( Block (..)
)
import Cardano.Wallet.Read.Eras.EraFun
( EraFun (..)
)
import Ouroboros.Consensus.Block.Abstract
( headerPrevHash
)
import Ouroboros.Consensus.Byron.Ledger
( ByronHash
( ByronBlock
, ByronHash
)
import Ouroboros.Consensus.Shelley.Ledger
( ShelleyHash
Expand Down Expand Up @@ -80,3 +95,44 @@ getHeaderHashShelley
-> ShelleyHash StandardCrypto
getHeaderHashShelley
(O.ShelleyBlock (Shelley.Block header _) _) = Shelley.pHeaderHash header

type family PrevHeaderHashT era where
PrevHeaderHashT ByronEra = O.ChainHash ByronBlock
PrevHeaderHashT ShelleyEra = PrevHash StandardCrypto
PrevHeaderHashT AllegraEra = PrevHash StandardCrypto
PrevHeaderHashT MaryEra = PrevHash StandardCrypto
PrevHeaderHashT AlonzoEra = PrevHash StandardCrypto
PrevHeaderHashT BabbageEra = PrevHash StandardCrypto
PrevHeaderHashT ConwayEra = PrevHash StandardCrypto

newtype PrevHeaderHash era = PrevHeaderHash (PrevHeaderHashT era)

getPrevHeaderHashShelley
:: ( Era era
, EncCBORGroup (TxSeq era)
, EncCBOR (Shelley.ShelleyProtocolHeader proto)
, Shelley.ProtocolHeaderSupportsEnvelope proto
)
=> O.ShelleyBlock proto era
-> PrevHash (ProtoCrypto proto)
getPrevHeaderHashShelley (O.ShelleyBlock (Shelley.Block header _) _) =
Shelley.pHeaderPrevHash header

getPrevHeaderHash :: EraFun Block PrevHeaderHash
getPrevHeaderHash =
EraFun
{ byronFun = \(Block block) ->
PrevHeaderHash $ headerPrevHash $ O.getHeader block
, shelleyFun = \(Block block) ->
PrevHeaderHash $ getPrevHeaderHashShelley block
, allegraFun = \(Block block) ->
PrevHeaderHash $ getPrevHeaderHashShelley block
, maryFun = \(Block block) ->
PrevHeaderHash $ getPrevHeaderHashShelley block
, alonzoFun = \(Block block) ->
PrevHeaderHash $ getPrevHeaderHashShelley block
, babbageFun = \(Block block) ->
PrevHeaderHash $ getPrevHeaderHashShelley block
, conwayFun = \(Block block) ->
PrevHeaderHash $ getPrevHeaderHashShelley block
}

0 comments on commit d2a8001

Please sign in to comment.