Skip to content

Commit

Permalink
Add primitive block extraction from ledger block
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Oct 31, 2023
1 parent 965c95a commit e2a6049
Show file tree
Hide file tree
Showing 20 changed files with 581 additions and 349 deletions.
29 changes: 15 additions & 14 deletions lib/read/lib/Cardano/Wallet/Read/Block/BlockNo.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE FlexibleContexts #-}

module Cardano.Wallet.Read.Block.BlockNo (getBlockNo) where
module Cardano.Wallet.Read.Block.BlockNo
( getEraBlockNo
) where

import Prelude

Expand All @@ -19,7 +21,6 @@ import Cardano.Wallet.Read
)
import Cardano.Wallet.Read.Eras.EraFun
( EraFun (..)
, EraFunK (..)
)
import Generics.SOP
( K (..)
Expand All @@ -37,18 +38,18 @@ import qualified Ouroboros.Consensus.Protocol.Praos.Header as O
import qualified Ouroboros.Consensus.Shelley.Ledger.Block as O
import qualified Ouroboros.Network.Block as O

getBlockNo :: EraFunK Block O.BlockNo
getBlockNo =
EraFunK
$ EraFun
{ byronFun = \(Block block) -> K $ O.blockNo block
, shelleyFun = \(Block block) -> K $ getBlockNoShelley block
, allegraFun = \(Block block) -> K $ getBlockNoShelley block
, maryFun = \(Block block) -> K $ getBlockNoShelley block
, alonzoFun = \(Block block) -> K $ getBlockNoShelley block
, babbageFun = \(Block block) -> K $ getBlockNoBabbage block
, conwayFun = \(Block block) -> K $ getBlockNoBabbage block
}
-- | Get the block number from a block
getEraBlockNo :: EraFun Block (K O.BlockNo)
getEraBlockNo =
EraFun
{ byronFun = \(Block block) -> K $ O.blockNo block
, shelleyFun = \(Block block) -> K $ getBlockNoShelley block
, allegraFun = \(Block block) -> K $ getBlockNoShelley block
, maryFun = \(Block block) -> K $ getBlockNoShelley block
, alonzoFun = \(Block block) -> K $ getBlockNoShelley block
, babbageFun = \(Block block) -> K $ getBlockNoBabbage block
, conwayFun = \(Block block) -> K $ getBlockNoBabbage block
}

getBlockNoShelley
:: (Era era, EncCBORGroup (TxSeq era), Crypto c)
Expand Down
12 changes: 9 additions & 3 deletions lib/read/lib/Cardano/Wallet/Read/Block/HeaderHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Cardano.Wallet.Read.Block.HeaderHash
, PrevHeaderHash (..)
, HeaderHashT
, PrevHeaderHashT
, getHeaderHash
, getEraHeaderHash
, getPrevHeaderHash
)
where
Expand Down Expand Up @@ -61,6 +61,7 @@ import qualified Ouroboros.Consensus.Shelley.Ledger.Block as O
import qualified Ouroboros.Consensus.Shelley.Protocol.Abstract as Shelley
import qualified Ouroboros.Network.Block as O

-- | Era-specific header hash type from the ledger
type family HeaderHashT era where
HeaderHashT ByronEra = ByronHash
HeaderHashT ShelleyEra = ShelleyHash StandardCrypto
Expand All @@ -70,10 +71,12 @@ type family HeaderHashT era where
HeaderHashT BabbageEra = ShelleyHash StandardCrypto
HeaderHashT ConwayEra = ShelleyHash StandardCrypto

-- | Era-specific header hash type from the ledger
newtype HeaderHash era = HeaderHash (HeaderHashT era)

getHeaderHash :: EraFun Block HeaderHash
getHeaderHash =
-- | Get the header hash from a block
getEraHeaderHash :: EraFun Block HeaderHash
getEraHeaderHash =
EraFun
{ byronFun = \(Block block) -> HeaderHash $ O.blockHash block
, shelleyFun = \(Block block) -> HeaderHash $ getHeaderHashShelley block
Expand All @@ -96,6 +99,7 @@ getHeaderHashShelley
getHeaderHashShelley
(O.ShelleyBlock (Shelley.Block header _) _) = Shelley.pHeaderHash header

-- | Era-specific previous header hash type from the ledger
type family PrevHeaderHashT era where
PrevHeaderHashT ByronEra = O.ChainHash ByronBlock
PrevHeaderHashT ShelleyEra = PrevHash StandardCrypto
Expand All @@ -105,6 +109,7 @@ type family PrevHeaderHashT era where
PrevHeaderHashT BabbageEra = PrevHash StandardCrypto
PrevHeaderHashT ConwayEra = PrevHash StandardCrypto

-- | Era-specific previous header hash type from the ledger
newtype PrevHeaderHash era = PrevHeaderHash (PrevHeaderHashT era)

getPrevHeaderHashShelley
Expand All @@ -118,6 +123,7 @@ getPrevHeaderHashShelley
getPrevHeaderHashShelley (O.ShelleyBlock (Shelley.Block header _) _) =
Shelley.pHeaderPrevHash header

-- | Get the previous header hash from a block
getPrevHeaderHash :: EraFun Block PrevHeaderHash
getPrevHeaderHash =
EraFun
Expand Down
31 changes: 16 additions & 15 deletions lib/read/lib/Cardano/Wallet/Read/Block/SlotNo.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE FlexibleContexts #-}

module Cardano.Wallet.Read.Block.SlotNo (getSlotNo) where
module Cardano.Wallet.Read.Block.SlotNo
( getEraSlotNo
) where

import Prelude

Expand All @@ -19,7 +21,6 @@ import Cardano.Wallet.Read
)
import Cardano.Wallet.Read.Eras.EraFun
( EraFun (..)
, EraFunK (..)
)
import Generics.SOP
( K (..)
Expand All @@ -37,25 +38,25 @@ import qualified Ouroboros.Consensus.Protocol.Praos.Header as O
import qualified Ouroboros.Consensus.Shelley.Ledger.Block as O
import qualified Ouroboros.Network.Block as O

getSlotNo :: EraFunK Block O.SlotNo
getSlotNo =
EraFunK
$ EraFun
{ byronFun = \(Block block) -> K $ O.blockSlot block
, shelleyFun = \(Block block) -> K $ getSlotNoShelley block
, allegraFun = \(Block block) -> K $ getSlotNoShelley block
, maryFun = \(Block block) -> K $ getSlotNoShelley block
, alonzoFun = \(Block block) -> K $ getSlotNoShelley block
, babbageFun = \(Block block) -> K $ getSlotNoBabbage block
, conwayFun = \(Block block) -> K $ getSlotNoBabbage block
}
-- | Get the slot number of the block.
getEraSlotNo :: EraFun Block (K O.SlotNo)
getEraSlotNo =
EraFun
{ byronFun = \(Block block) -> K $ O.blockSlot block
, shelleyFun = \(Block block) -> K $ getSlotNoShelley block
, allegraFun = \(Block block) -> K $ getSlotNoShelley block
, maryFun = \(Block block) -> K $ getSlotNoShelley block
, alonzoFun = \(Block block) -> K $ getSlotNoShelley block
, babbageFun = \(Block block) -> K $ getSlotNoBabbage block
, conwayFun = \(Block block) -> K $ getSlotNoBabbage block
}

getSlotNoShelley
:: (Era era, EncCBORGroup (TxSeq era), Crypto c)
=> O.ShelleyBlock (TPraos c) era
-> O.SlotNo
getSlotNoShelley
( O.ShelleyBlock (Shelley.Block (Shelley.BHeader header _) _) _) =
(O.ShelleyBlock (Shelley.Block (Shelley.BHeader header _) _) _) =
Shelley.bheaderSlotNo header
getSlotNoBabbage
:: (Era era, EncCBORGroup (TxSeq era), Crypto crypto)
Expand Down
32 changes: 4 additions & 28 deletions lib/read/lib/Cardano/Wallet/Read/Block/Txs.hs
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

module Cardano.Wallet.Read.Block.Txs
( getTxs
, getTxsForEra
, txsFromBlock
( getEraTransactions
) where

import Prelude

import Cardano.Api
( ByronEra
)
import Cardano.Ledger.Api
( StandardCrypto
)
import Cardano.Ledger.Binary
( EncCBOR
)
import Cardano.Wallet.Read.Block
( Block (..)
, fromConsensusBlock
)
import Cardano.Wallet.Read.Eras
( (:.:) (..)
, EraFun (..)
, EraValue
, MkEraValue
, applyEraFun
, project
)
import Cardano.Wallet.Read.Tx
( Tx (..)
Expand All @@ -39,9 +28,6 @@ import Cardano.Wallet.Read.Tx
import Data.Foldable
( toList
)
import Generics.SOP
( unComp
)
import Ouroboros.Consensus.Shelley.Protocol.Abstract
( ShelleyProtocolHeader
)
Expand All @@ -53,12 +39,11 @@ import qualified Cardano.Ledger.Era as Shelley
import qualified Cardano.Ledger.Shelley.API as Shelley
import qualified Ouroboros.Consensus.Byron.Ledger as Byron
import qualified Ouroboros.Consensus.Byron.Ledger as O
import qualified Ouroboros.Consensus.Cardano.Block as O
import qualified Ouroboros.Consensus.Shelley.Ledger as O

-- | Get sequence of transactions in the block.
txsFromBlock :: EraFun Block ([] :.: Tx)
txsFromBlock =
-- | Get the list of transactions in the block.
getEraTransactions :: EraFun Block ([] :.: Tx)
getEraTransactions =
EraFun
{ byronFun = getTxs' getTxsFromBlockByron
, shelleyFun = getTxs' getTxsFromBlockShelleyAndOn
Expand All @@ -84,12 +69,3 @@ getTxsFromBlockShelleyAndOn
-> [Ledger.Tx era]
getTxsFromBlockShelleyAndOn (O.ShelleyBlock (Shelley.Block _ txs) _) =
toList (Shelley.fromTxSeq txs)

getTxs :: O.CardanoBlock StandardCrypto -> EraValue ([] :.: Tx)
getTxs = applyEraFun txsFromBlock . fromConsensusBlock

getTxsForEra
:: MkEraValue ([] :.: Tx) p
-> O.CardanoBlock StandardCrypto
-> Maybe [Tx p]
getTxsForEra ew = fmap unComp . project ew . getTxs
Loading

0 comments on commit e2a6049

Please sign in to comment.