Skip to content

Commit cda0271

Browse files
committed
Refactor: SelectView = BlockNo × TiebreakerView (#1591)
This PR does not change any behavior; most of the diff is purely mechanical. Previously, the `SelectView`s of all `ConsensusProtocol`s contained the `BlockNo` as the most important criterion for comparing chains, with only tiebreaking behavior (among chains of equal length) being different across different protocols. This PR makes this explicit: `SelectView` is now longer an associated type family of `ConsensusProtocol`, but rather an ordinary data type ```haskell data SelectView p = SelectView { svBlockNo :: !BlockNo , svTiebreakerView :: !(TiebreakerView p) } ``` where `TiebreakerView` is now an associated type family of `ConsensusProtocol`. This PR also removes the `WithBlockNo` type that the HFC was already using to always attach a `BlockNo` to its `HardForkSelectView`. The code can be simpler now that `SelectView`s always automatically contain an explicit `BlockNo`. Also see #1542 (comment) ## Primary motivation The main motivation for this is to enable weighted chain selection in Peras (tweag/cardano-peras#62), which can now be easily modeled by adding the weight of a chain to the block number component, at least morally; we probably want to eventually have more type safety here to make it hard to "forget" to account for the weight, such as defining ```haskell data WeightedSelectView p = WeightedSelectView { wsvWeight :: !PerasWeight , wsvTiebreakerView :: !(TiebreakerView p) } ``` If the only goal were to minimize the diff, then it would also be possible to add a function `(BlockNo -> BlockNo) -> SelectView p -> SelectView p`, but it seems more honest to "properly" extract out the block number.
1 parent c8ca5b1 commit cda0271

File tree

30 files changed

+323
-290
lines changed

30 files changed

+323
-290
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Patch
2+
3+
- Adapted to changes related to `SelectView`.
4+
5+
Concretely, this changes the structure of `SelectView (BlockProtocol (CardanoBlock c))`, but it still contains the same data as before.

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/PBFT.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ instance BlockSupportsProtocol ByronBlock where
7474
where
7575
epochSlots = byronEpochSlots cfg
7676

77-
selectView _ = mkPBftSelectView
77+
tiebreakerView _ = mkPBftTiebreakerView
7878

7979
toPBftLedgerView :: Delegation.Map -> PBftLedgerView PBftByronCrypto
8080
toPBftLedgerView = PBftLedgerView . Delegation.unMap

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/CanHardFork.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import Ouroboros.Consensus.Protocol.PBFT.State (PBftState)
8282
import qualified Ouroboros.Consensus.Protocol.PBFT.State as PBftState
8383
import Ouroboros.Consensus.Protocol.Praos (Praos)
8484
import qualified Ouroboros.Consensus.Protocol.Praos as Praos
85+
import Ouroboros.Consensus.Protocol.Praos.Common (PraosTiebreakerView)
8586
import Ouroboros.Consensus.Protocol.TPraos
8687
import qualified Ouroboros.Consensus.Protocol.TPraos as TPraos
8788
import Ouroboros.Consensus.Shelley.HFEras ()
@@ -164,10 +165,10 @@ instance CardanoHardForkConstraints c => CanHardFork (CardanoEras c) where
164165
}
165166
hardForkChainSel =
166167
-- Byron <-> Shelley, ...
167-
TCons (SOP.hpure CompareBlockNo)
168+
TCons (SOP.hpure NoTiebreakerAcrossEras)
168169
-- Inter-Shelley-based
169170
$
170-
Tails.hcpure (Proxy @(HasPraosSelectView c)) CompareSameSelectView
171+
Tails.hcpure (Proxy @(HasPraosTiebreakerView c)) SameTiebreakerAcrossEras
171172
hardForkInjectTxs =
172173
PCons (ignoringBoth $ Pair2 cannotInjectTx cannotInjectValidatedTx)
173174
$ PCons
@@ -233,8 +234,8 @@ instance CardanoHardForkConstraints c => CanHardFork (CardanoEras c) where
233234
fromAlonzo x = fromConway $ ConwayMeasure x mempty
234235
fromConway x = x
235236

236-
class SelectView (BlockProtocol blk) ~ PraosChainSelectView c => HasPraosSelectView c blk
237-
instance SelectView (BlockProtocol blk) ~ PraosChainSelectView c => HasPraosSelectView c blk
237+
class TiebreakerView (BlockProtocol blk) ~ PraosTiebreakerView c => HasPraosTiebreakerView c blk
238+
instance TiebreakerView (BlockProtocol blk) ~ PraosTiebreakerView c => HasPraosTiebreakerView c blk
238239

239240
{-------------------------------------------------------------------------------
240241
Translation from Byron to Shelley

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Block.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,8 @@ import Ouroboros.Consensus.HardFork.Combinator
7171
)
7272
import Ouroboros.Consensus.HeaderValidation
7373
import Ouroboros.Consensus.Protocol.Abstract
74-
( ChainDepState
75-
, SelectView
76-
)
7774
import Ouroboros.Consensus.Protocol.Praos.Common
78-
( PraosChainSelectView
75+
( PraosTiebreakerView
7976
)
8077
import Ouroboros.Consensus.Protocol.Signed (SignedHeader)
8178
import Ouroboros.Consensus.Shelley.Eras
@@ -118,7 +115,7 @@ class
118115
, Show (SL.TranslationContext era)
119116
, -- Currently the chain select view is identical
120117
-- Era and proto crypto must coincide
121-
SelectView proto ~ PraosChainSelectView (ProtoCrypto proto)
118+
TiebreakerView proto ~ PraosTiebreakerView (ProtoCrypto proto)
122119
, -- Need to be able to sign the protocol header
123120
SignedHeader (ShelleyProtocolHeader proto)
124121
, -- ChainDepState needs to be serialisable

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Protocol.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module Ouroboros.Consensus.Shelley.Ledger.Protocol () where
1111

1212
import qualified Cardano.Ledger.Shelley.API as SL
1313
import Ouroboros.Consensus.Block
14+
import Ouroboros.Consensus.Protocol.Praos.Common
1415
import Ouroboros.Consensus.Protocol.Signed
15-
import Ouroboros.Consensus.Protocol.TPraos
1616
import Ouroboros.Consensus.Shelley.Ledger.Block
1717
import Ouroboros.Consensus.Shelley.Ledger.Config (BlockConfig (..))
1818
import Ouroboros.Consensus.Shelley.Protocol.Abstract
@@ -30,13 +30,12 @@ import Ouroboros.Consensus.Shelley.Protocol.Abstract
3030
instance ShelleyCompatible proto era => BlockSupportsProtocol (ShelleyBlock proto era) where
3131
validateView _cfg = protocolHeaderView @proto . shelleyHeaderRaw
3232

33-
selectView _ hdr@(ShelleyHeader shdr _) =
34-
PraosChainSelectView
35-
{ csvChainLength = blockNo hdr
36-
, csvSlotNo = blockSlot hdr
37-
, csvIssuer = hdrIssuer
38-
, csvIssueNo = pHeaderIssueNo shdr
39-
, csvTieBreakVRF = pTieBreakVRFValue shdr
33+
tiebreakerView _ hdr@(ShelleyHeader shdr _) =
34+
PraosTiebreakerView
35+
{ ptvSlotNo = blockSlot hdr
36+
, ptvIssuer = hdrIssuer
37+
, ptvIssueNo = pHeaderIssueNo shdr
38+
, ptvTieBreakVRF = pTieBreakVRFValue shdr
4039
}
4140
where
4241
hdrIssuer :: SL.VKey 'SL.BlockIssuer

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/Infra/ShelleyBasedHardFork.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ instance
271271
, translateTxOutWith = SL.upgradeTxOut
272272
}
273273

274-
hardForkChainSel = Tails.mk2 CompareSameSelectView
274+
hardForkChainSel = Tails.mk2 SameTiebreakerAcrossEras
275275

276276
hardForkInjectTxs =
277277
InPairs.mk2 $

ouroboros-consensus-cardano/test/cardano-test/Test/Consensus/Cardano/DiffusionPipelining.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Ouroboros.Consensus.Byron.Ledger (ByronBlock)
2222
import Ouroboros.Consensus.Cardano (CardanoBlock)
2323
import Ouroboros.Consensus.HardFork.Combinator
2424
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras
25+
import Ouroboros.Consensus.Protocol.Abstract
2526
import Ouroboros.Consensus.Protocol.PBFT
2627
import Ouroboros.Consensus.Shelley.Eras
2728
import Ouroboros.Consensus.Shelley.Ledger
@@ -79,7 +80,7 @@ instance GenTentativeHeaderViews ByronBlock where
7980
nubOrd . sort <$> listOf do
8081
bno <- arbitrary
8182
isEBB <- toIsEBB <$> arbitrary
82-
pure $ PBftSelectView bno isEBB
83+
pure $ SelectView bno (PBftTiebreakerView isEBB)
8384

8485
instance ShelleyCompatible proto era => GenTentativeHeaderViews (ShelleyBlock proto era) where
8586
genTentativeHeaderViews _ = do

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ instance CanHardFork '[BlockA, BlockB] where
441441
, translateChainDepState = PCons chainDepState_AtoB PNil
442442
, crossEraForecast = PCons forecast_AtoB PNil
443443
}
444-
hardForkChainSel = Tails.mk2 CompareBlockNo
444+
hardForkChainSel = Tails.mk2 NoTiebreakerAcrossEras
445445
hardForkInjectTxs = InPairs.mk2 injectTx_AtoB
446446

447447
hardForkInjTxMeasure = \case
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Breaking
2+
3+
- Removed `PraosChainSelectView`, use `SelectView (TPraos c)`/`SelectView (Praos
4+
c)` instead.

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ instance PraosCrypto c => ConsensusProtocol (Praos c) where
389389
type ChainDepState (Praos c) = PraosState
390390
type IsLeader (Praos c) = PraosIsLeader c
391391
type CanBeLeader (Praos c) = PraosCanBeLeader c
392-
type SelectView (Praos c) = PraosChainSelectView c
392+
type TiebreakerView (Praos c) = PraosTiebreakerView c
393393
type LedgerView (Praos c) = Views.LedgerView
394394
type ValidationErr (Praos c) = PraosValidationErr c
395395
type ValidateView (Praos c) = PraosValidateView c

0 commit comments

Comments
 (0)