Skip to content

Commit

Permalink
[ADP-3350] Fix Cardano.Wallet.Read.Block.SlotNo (#4535)
Browse files Browse the repository at this point in the history
This pull request fixes the `Cardano.Wallet.Read.Block.SlotNo` module
which encountered some difficulties with type class resolution and
orphan instances, to the point where I suspected a compiler bug.

I have also added `NoThunks` and `Generic` instances for the
`Cardano.Wallet.Read.SlotNo` type in preparation for future pull
requests.

### Issue Number

Discovered during ADP-3350
  • Loading branch information
HeinrichApfelmus authored Apr 15, 2024
2 parents e7fc10b + 82d1b6c commit ea95825
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 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 @@ -122,6 +122,7 @@ library
, generics-sop
, lens
, memory
, nothunks
, operational
, ouroboros-consensus
, ouroboros-consensus-cardano
Expand Down
2 changes: 2 additions & 0 deletions lib/read/lib/Cardano/Wallet/Read/Block/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import qualified Ouroboros.Consensus.Shelley.Ledger as O
type ConsensusBlock = O.CardanoBlock O.StandardCrypto

-- Family of era-specific block types
-- TODO: ADP-3351 The results of this type family should be ledger types,
-- not ouroboros-consensus types.
type family BlockT era where
BlockT Byron =
O.ByronBlock
Expand Down
51 changes: 26 additions & 25 deletions lib/read/lib/Cardano/Wallet/Read/Block/SlotNo.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand All @@ -10,26 +11,25 @@ module Cardano.Wallet.Read.Block.SlotNo

import Prelude

import Cardano.Ledger.Binary.Group
( EncCBORGroup
import Cardano.Ledger.Block
( bheader
)
import Cardano.Ledger.Crypto
( Crypto
)
import Cardano.Ledger.Era
( EraSegWits (..)
( StandardCrypto
)
import Cardano.Wallet.Read.Block.Block
( Block (..)
)
import Cardano.Wallet.Read.Block.BlockNo
()
-- ?! GHC 9.6.4: This import looks redundant, but the compilation
-- of getSlotNoShelley will fail if we don't that. No idea why.
import Cardano.Wallet.Read.Eras.KnownEras
( Era (..)
, IsEra (..)
)
import GHC.Generics
( Generic
)
import NoThunks.Class
( NoThunks (..)
)
import Numeric.Natural
( Natural
)
Expand All @@ -39,11 +39,14 @@ import Ouroboros.Consensus.Protocol.Praos
import Ouroboros.Consensus.Protocol.TPraos
( TPraos
)
import Ouroboros.Consensus.Shelley.Protocol.Abstract
( pHeaderSlot
)
import Ouroboros.Consensus.Shelley.Protocol.Praos
()
import Ouroboros.Consensus.Shelley.Protocol.TPraos
()

import qualified Cardano.Ledger.Api as L
import qualified Cardano.Ledger.Shelley.API as Shelley
import qualified Cardano.Protocol.TPraos.BHeader as Shelley
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

Expand All @@ -62,20 +65,18 @@ getEraSlotNo = case theEra @era of
k = SlotNo . fromIntegral . O.unSlotNo

newtype SlotNo = SlotNo {unSlotNo :: Natural}
deriving (Eq, Show)
deriving (Eq, Ord, Show, Generic)

instance NoThunks SlotNo

getSlotNoShelley
:: (L.Era era, EncCBORGroup (TxSeq era), Crypto c)
=> O.ShelleyBlock (TPraos c) era
:: O.ShelleyBlock (TPraos StandardCrypto) era
-> O.SlotNo
getSlotNoShelley
(O.ShelleyBlock (Shelley.Block (Shelley.BHeader header _) _) _) =
Shelley.bheaderSlotNo header
getSlotNoShelley (O.ShelleyBlock block _) =
pHeaderSlot $ bheader block

getSlotNoBabbage
:: (L.Era era, EncCBORGroup (TxSeq era), Crypto crypto)
=> O.ShelleyBlock (Praos crypto) era
:: O.ShelleyBlock (Praos StandardCrypto) era
-> O.SlotNo
getSlotNoBabbage
(O.ShelleyBlock (Shelley.Block (O.Header header _) _) _) =
O.hbSlotNo header
getSlotNoBabbage (O.ShelleyBlock block _) =
pHeaderSlot $ bheader block

0 comments on commit ea95825

Please sign in to comment.