-
Notifications
You must be signed in to change notification settings - Fork 174
*EraBlockHeader typeclasses deprecating BHeaderView #5560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d3cac18
9460744
5b493ec
db48be5
fa619be
23cc203
5b35f0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| {-# LANGUAGE FlexibleContexts #-} | ||
| {-# LANGUAGE FlexibleInstances #-} | ||
| {-# LANGUAGE MultiParamTypeClasses #-} | ||
| {-# LANGUAGE NamedFieldPuns #-} | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
| {-# LANGUAGE StandaloneDeriving #-} | ||
|
|
@@ -19,6 +20,7 @@ module Cardano.Ledger.Alonzo.Rules.Bbody ( | |
| AlonzoBbodyPredFailure (..), | ||
| AlonzoBbodyEvent (..), | ||
| alonzoBbodyTransition, | ||
| validateExUnits, | ||
| ) where | ||
|
|
||
| import Cardano.Ledger.Allegra.Rules (AllegraUtxoPredFailure) | ||
|
|
@@ -30,16 +32,15 @@ import Cardano.Ledger.Alonzo.Rules.Utxos (AlonzoUtxosPredFailure) | |
| import Cardano.Ledger.Alonzo.Rules.Utxow (AlonzoUtxowPredFailure) | ||
| import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), pointWiseExUnits) | ||
| import Cardano.Ledger.Alonzo.Tx (totExUnits) | ||
| import Cardano.Ledger.BHeaderView (BHeaderView (..), isOverlaySlot) | ||
| import Cardano.Ledger.BaseTypes (Mismatch (..), Relation (..), ShelleyBase, epochInfoPure) | ||
| import Cardano.Ledger.BaseTypes (Mismatch (..), Relation (..), ShelleyBase) | ||
| import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..)) | ||
| import Cardano.Ledger.Binary.Coders | ||
| import Cardano.Ledger.Block (Block (..)) | ||
| import Cardano.Ledger.Keys (coerceKeyRole) | ||
| import Cardano.Ledger.Block (Block (..), EraBlockHeader (..)) | ||
| import Cardano.Ledger.Shelley.BlockBody (incrBlocks) | ||
| import Cardano.Ledger.Shelley.LedgerState (LedgerState) | ||
| import Cardano.Ledger.Shelley.Rules ( | ||
| BbodyEnv (..), | ||
| BbodySignal (..), | ||
| ShelleyBbodyEvent (..), | ||
| ShelleyBbodyPredFailure (..), | ||
| ShelleyBbodyState (..), | ||
|
|
@@ -53,12 +54,15 @@ import Cardano.Ledger.Shelley.Rules ( | |
| ShelleyPpupPredFailure, | ||
| ShelleyUtxoPredFailure, | ||
| ShelleyUtxowPredFailure, | ||
| validateBodyHash, | ||
| validateBodySize, | ||
| ) | ||
| import Cardano.Ledger.Slot (epochInfoEpoch, epochInfoFirst) | ||
| import Cardano.Ledger.Slot (slotToEpochBoundary) | ||
| import Control.DeepSeq (NFData) | ||
| import Control.Monad.Trans.Reader (asks) | ||
| import Control.State.Transition ( | ||
| Embed (..), | ||
| Rule, | ||
| RuleType (..), | ||
| STS (..), | ||
| TRC (..), | ||
| TransitionRule, | ||
|
|
@@ -73,9 +77,6 @@ import GHC.Generics (Generic) | |
| import Lens.Micro ((^.)) | ||
| import NoThunks.Class (NoThunks (..)) | ||
|
|
||
| -- ======================================= | ||
| -- A new PredicateFailure type | ||
|
|
||
| data AlonzoBbodyPredFailure era | ||
| = ShelleyInAlonzoBbodyPredFailure (ShelleyBbodyPredFailure era) | ||
| | TooManyExUnits (Mismatch RelLTEQ ExUnits) | ||
|
|
@@ -166,14 +167,33 @@ instance | |
| dec 1 = SumD TooManyExUnits <! From | ||
| dec n = Invalid n | ||
|
|
||
| -- ======================================== | ||
| -- The STS instance | ||
| -- | Validate that total execution units (all transactions) do not exceed block limit. | ||
| validateExUnits :: | ||
| forall era. | ||
| ( AlonzoEraTx era | ||
| , InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era | ||
| ) => | ||
| StrictSeq.StrictSeq (Tx TopTx era) -> | ||
| -- | Max block exunits protocol parameter. | ||
| ExUnits -> | ||
| Rule (EraRule "BBODY" era) 'Transition () | ||
| validateExUnits txs ppMax = | ||
| let txTotal = foldMap totExUnits txs | ||
| in pointWiseExUnits (<=) txTotal ppMax | ||
| ?! injectFailure | ||
| ( TooManyExUnits $ | ||
| Mismatch | ||
| { mismatchSupplied = txTotal | ||
| , mismatchExpected = ppMax | ||
| } | ||
| ) | ||
|
|
||
| alonzoBbodyTransition :: | ||
| forall era. | ||
| ( STS (EraRule "BBODY" era) | ||
| , Signal (EraRule "BBODY" era) ~ Block BHeaderView era | ||
| , Signal (EraRule "BBODY" era) ~ BbodySignal era | ||
| , InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era | ||
| , InjectRuleFailure "BBODY" ShelleyBbodyPredFailure era | ||
| , BaseM (EraRule "BBODY" era) ~ ShelleyBase | ||
| , State (EraRule "BBODY" era) ~ ShelleyBbodyState era | ||
| , Environment (EraRule "BBODY" era) ~ BbodyEnv era | ||
|
|
@@ -182,97 +202,53 @@ alonzoBbodyTransition :: | |
| , State (EraRule "LEDGERS" era) ~ LedgerState era | ||
| , Signal (EraRule "LEDGERS" era) ~ Seq (Tx TopTx era) | ||
| , EraBlockBody era | ||
| , AlonzoEraTxWits era | ||
| , AlonzoEraPParams era | ||
| , AlonzoEraTx era | ||
| ) => | ||
| TransitionRule (EraRule "BBODY" era) | ||
| alonzoBbodyTransition = | ||
| judgmentContext | ||
| >>= \( TRC | ||
| ( BbodyEnv pp account | ||
| , BbodyState ls b | ||
| , Block bh txsSeq | ||
| ) | ||
| ) -> do | ||
| let txs = txsSeq ^. txSeqBlockBodyL | ||
| actualBodySize = bBodySize (pp ^. ppProtocolVersionL) txsSeq | ||
| actualBodyHash = hashBlockBody @era txsSeq | ||
|
|
||
| actualBodySize | ||
| == fromIntegral (bhviewBSize bh) | ||
| ?! injectFailure | ||
| ( ShelleyInAlonzoBbodyPredFailure | ||
| ( WrongBlockBodySizeBBODY $ | ||
| Mismatch | ||
| { mismatchSupplied = actualBodySize | ||
| , mismatchExpected = fromIntegral $ bhviewBSize bh | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| actualBodyHash | ||
| == bhviewBHash bh | ||
| ?! injectFailure | ||
| ( ShelleyInAlonzoBbodyPredFailure | ||
| ( InvalidBodyHashBBODY @era $ | ||
| Mismatch | ||
| { mismatchSupplied = actualBodyHash | ||
| , mismatchExpected = bhviewBHash bh | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| -- Note that this may not actually be a stake pool - it could be a | ||
| -- genesis key delegate. However, this would only entail an overhead of | ||
| -- 7 counts, and it's easier than differentiating here. | ||
| -- | ||
| -- TODO move this computation inside 'incrBlocks' where it belongs. Here | ||
| -- we make an assumption that 'incrBlocks' must enforce, better for it | ||
| -- to be done in 'incrBlocks' where we can see that the assumption is | ||
| -- enforced. | ||
| let hkAsStakePool = coerceKeyRole $ bhviewID bh | ||
| slot = bhviewSlot bh | ||
| (firstSlotNo, curEpochNo) <- liftSTS $ do | ||
| ei <- asks epochInfoPure | ||
| let curEpochNo = epochInfoEpoch ei slot | ||
| pure (epochInfoFirst ei curEpochNo, curEpochNo) | ||
|
|
||
| ls' <- | ||
| trans @(EraRule "LEDGERS" era) $ | ||
| TRC (LedgersEnv (bhviewSlot bh) curEpochNo pp account, ls, StrictSeq.fromStrict txs) | ||
|
|
||
| {- ∑(tx ∈ txs)(totExunits tx) ≤ maxBlockExUnits pp -} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to keep this comment around, since I assume it's from the spec.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I also assume it is from the spec, but we hardly have those as a convention everywhere any more. So I thought it is more consistent to remove it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think? Should we keep it still? |
||
| let txTotal, ppMax :: ExUnits | ||
| txTotal = foldMap totExUnits txs | ||
| ppMax = pp ^. ppMaxBlockExUnitsL | ||
| pointWiseExUnits (<=) txTotal ppMax | ||
| ?! injectFailure (TooManyExUnits Mismatch {mismatchSupplied = txTotal, mismatchExpected = ppMax}) | ||
|
|
||
| pure $ | ||
| BbodyState @era | ||
| ls' | ||
| ( incrBlocks | ||
| (isOverlaySlot firstSlotNo (pp ^. ppDG) slot) | ||
| hkAsStakePool | ||
| b | ||
| ) | ||
| alonzoBbodyTransition = do | ||
| TRC (BbodyEnv pp account, BbodyState ls blocksMade, BbodySignal blk@Block {blockBody}) <- | ||
| judgmentContext | ||
|
|
||
| validateBodySize blk (pp ^. ppProtocolVersionL) | ||
|
|
||
| validateBodyHash blk | ||
|
|
||
| let bhSlot = blk ^. blockHeaderSlotL | ||
|
|
||
| (firstSlot, curEpoch) <- liftSTS $ slotToEpochBoundary bhSlot | ||
|
|
||
| let txs = blockBody ^. txSeqBlockBodyL | ||
|
|
||
| ls' <- | ||
| trans @(EraRule "LEDGERS" era) $ | ||
| TRC | ||
| ( LedgersEnv bhSlot curEpoch pp account | ||
| , ls | ||
| , StrictSeq.fromStrict txs | ||
| ) | ||
|
|
||
| validateExUnits @era txs $ pp ^. ppMaxBlockExUnitsL | ||
|
|
||
| pure $ BbodyState ls' $ incrBlocks blk firstSlot (pp ^. ppDG) blocksMade | ||
|
|
||
| instance | ||
| ( EraRule "BBODY" era ~ AlonzoBBODY era | ||
| , InjectRuleFailure "BBODY" AlonzoBbodyPredFailure era | ||
| , InjectRuleFailure "BBODY" ShelleyBbodyPredFailure era | ||
| , Embed (EraRule "LEDGERS" era) (AlonzoBBODY era) | ||
| , Environment (EraRule "LEDGERS" era) ~ ShelleyLedgersEnv era | ||
| , State (EraRule "LEDGERS" era) ~ LedgerState era | ||
| , Signal (EraRule "LEDGERS" era) ~ Seq (Tx TopTx era) | ||
| , AlonzoEraTxWits era | ||
| , EraBlockBody era | ||
| , AlonzoEraPParams era | ||
| , AlonzoEraTx era | ||
| ) => | ||
| STS (AlonzoBBODY era) | ||
| where | ||
| type State (AlonzoBBODY era) = ShelleyBbodyState era | ||
|
|
||
| type Signal (AlonzoBBODY era) = Block BHeaderView era | ||
| type Signal (AlonzoBBODY era) = BbodySignal era | ||
|
|
||
| type Environment (AlonzoBBODY era) = BbodyEnv era | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.