Skip to content

Commit

Permalink
ouroboros-consensus-shelley: treat TranslationContext properly
Browse files Browse the repository at this point in the history
Alonzo is the first era with a `TranslationContext` that is not `()`.
Therefore, this commit must pipe through the `TranslationContext` through to
the calls to `translateEra`. In particular, some of this are in classes used by
the HFC in which the only era-specific input is the `LedgerConfig`. For that
reason, we're adding `TranslationContext` to `ShelleyLedgerConfig`. It's
possible we should instead alter the HFC to carry something in addition
to/instead of the `LedgerConfig`, but we're taking the direct path for now.

By adding a field to the `ShelleyLedgerConfig` we take on the burden of
providing that `TranslationConfig` in every interface and/or test function that
creates a Shelley protocol info etc. This is noisy but simple, since all eras
before `AlonzoEra` just use `()` for their context.
  • Loading branch information
nfrisby committed May 13, 2021
1 parent d2019e9 commit 625d570
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 79 deletions.
Expand Up @@ -55,7 +55,8 @@ import Ouroboros.Consensus.Cardano.CanHardFork
(ShelleyPartialLedgerConfig (..), forecastAcrossShelley,
translateChainDepStateAcrossShelley)
import Ouroboros.Consensus.Cardano.Node
(ProtocolParamsTransition (..), TriggerHardFork (..))
(ProtocolTransitionParamsShelleyBased (..),
TriggerHardFork (..))

import Test.ThreadNet.TxGen
import Test.ThreadNet.TxGen.Shelley ()
Expand Down Expand Up @@ -128,7 +129,7 @@ type ShelleyBasedHardForkConstraints era1 era2 =
, SL.TranslationError era2 SL.NewEpochState ~ Void
, SL.TranslationError era2 SL.ShelleyGenesis ~ Void

, SL.TranslationContext era2 ~ ()
, SL.TranslationContext era1 ~ ()
)

instance ShelleyBasedHardForkConstraints era1 era2
Expand All @@ -149,8 +150,14 @@ instance ShelleyBasedHardForkConstraints era1 era2
(HFC.Translate LedgerState)
(ShelleyBlock era1)
(ShelleyBlock era2)
translateLedgerState = InPairs.ignoringBoth $ HFC.Translate $ \_epochNo ->
unComp . SL.translateEra' () . Comp
translateLedgerState =
InPairs.RequireBoth
$ \_cfg1 cfg2 -> HFC.Translate
$ \_epochNo ->
unComp
. SL.translateEra'
(shelleyLedgerTranslationContext (unwrapLedgerConfig cfg2))
. Comp

translateLedgerView ::
InPairs.RequiringBoth
Expand All @@ -166,21 +173,29 @@ instance ShelleyBasedHardForkConstraints era1 era2

hardForkInjectTxs =
InPairs.mk2
$ InPairs.ignoringBoth
$ Pair2 (InjectTx translateTx) (InjectValidatedTx translateValidatedTx)
$ InPairs.RequireBoth $ \_cfg1 cfg2 ->
let ctxt = shelleyLedgerTranslationContext (unwrapLedgerConfig cfg2)
in
Pair2
(InjectTx (translateTx ctxt))
(InjectValidatedTx (translateValidatedTx ctxt))
where
translateTx ::
GenTx (ShelleyBlock era1)
SL.TranslationContext era2
-> GenTx (ShelleyBlock era1)
-> Maybe (GenTx (ShelleyBlock era2))
translateTx =
fmap unComp . eitherToMaybe . runExcept . SL.translateEra () . Comp
translateTx transCtxt =
fmap unComp
. eitherToMaybe . runExcept . SL.translateEra transCtxt
. Comp

translateValidatedTx ::
WrapValidatedGenTx (ShelleyBlock era1)
SL.TranslationContext era2
-> WrapValidatedGenTx (ShelleyBlock era1)
-> Maybe (WrapValidatedGenTx (ShelleyBlock era2))
translateValidatedTx =
translateValidatedTx transCtxt =
fmap unComp
. eitherToMaybe . runExcept . SL.translateEra ()
. eitherToMaybe . runExcept . SL.translateEra transCtxt
. Comp

instance ShelleyBasedHardForkConstraints era1 era2
Expand All @@ -204,12 +219,12 @@ protocolInfoShelleyBasedHardFork ::
=> ProtocolParamsShelleyBased era1
-> SL.ProtVer
-> SL.ProtVer
-> ProtocolParamsTransition (ShelleyBlock era1) (ShelleyBlock era2)
-> ProtocolTransitionParamsShelleyBased era2
-> ProtocolInfo m (ShelleyBasedHardForkBlock era1 era2)
protocolInfoShelleyBasedHardFork protocolParamsShelleyBased
protVer1
protVer2
protocolParamsTransition =
protocolTransitionParams =
protocolInfoBinary
-- Era 1
protocolInfo1
Expand Down Expand Up @@ -237,12 +252,16 @@ protocolInfoShelleyBasedHardFork protocolParamsShelleyBased
protocolInfo1 =
protocolInfoShelleyBased
protocolParamsShelleyBased
()
protVer1

eraParams1 :: History.EraParams
eraParams1 = shelleyEraParams genesis1

ProtocolParamsTransition { transitionTrigger } = protocolParamsTransition
ProtocolTransitionParamsShelleyBased {
transitionTranslationContext = transCtxt2
, transitionTrigger
} = protocolTransitionParams

toPartialLedgerConfig1 ::
LedgerConfig (ShelleyBlock era1)
Expand All @@ -255,7 +274,7 @@ protocolInfoShelleyBasedHardFork protocolParamsShelleyBased
-- Era 2

genesis2 :: SL.ShelleyGenesis era2
genesis2 = SL.translateEra' () genesis1
genesis2 = SL.translateEra' transCtxt2 genesis1

protocolInfo2 :: ProtocolInfo m (ShelleyBlock era2)
protocolInfo2 =
Expand All @@ -265,6 +284,7 @@ protocolInfoShelleyBasedHardFork protocolParamsShelleyBased
, shelleyBasedInitialNonce
, shelleyBasedLeaderCredentials
}
transCtxt2
protVer2

eraParams2 :: History.EraParams
Expand Down
Expand Up @@ -52,7 +52,8 @@ import Ouroboros.Consensus.Shelley.Node

import Ouroboros.Consensus.Cardano.Condense ()
import Ouroboros.Consensus.Cardano.Node
(ProtocolParamsTransition (..), TriggerHardFork (..))
(ProtocolTransitionParamsShelleyBased (..),
TriggerHardFork (..))

import Test.ThreadNet.General
import Test.ThreadNet.Network (NodeOutput (..),
Expand Down Expand Up @@ -263,8 +264,10 @@ prop_simple_allegraMary_convergence TestSetup
}
(SL.ProtVer majorVersion1 0)
(SL.ProtVer majorVersion2 0)
ProtocolParamsTransition {
transitionTrigger = TriggerHardForkAtVersion majorVersion2
ProtocolTransitionParamsShelleyBased {
transitionTranslationContext = ()
, transitionTrigger =
TriggerHardForkAtVersion majorVersion2
}
}
, mkRekeyM = Nothing
Expand Down
23 changes: 13 additions & 10 deletions ouroboros-consensus-cardano-test/test/Test/ThreadNet/Cardano.hs
Expand Up @@ -46,7 +46,6 @@ import Ouroboros.Consensus.Byron.Node
import qualified Shelley.Spec.Ledger.API as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL (ActiveSlotCoeff)

import Ouroboros.Consensus.Shelley.Ledger.Block (ShelleyBlock)
import Ouroboros.Consensus.Shelley.Node

import Ouroboros.Consensus.Cardano.Block
Expand Down Expand Up @@ -257,8 +256,10 @@ prop_simple_cardano_convergence TestSetup
genesisShelley
setupInitialNonce
(coreNodes !! fromIntegral nid)
ProtocolParamsTransition {
transitionTrigger = TriggerHardForkAtVersion shelleyMajorVersion
ProtocolTransitionParamsShelleyBased {
transitionTranslationContext = ()
, transitionTrigger =
TriggerHardForkAtVersion shelleyMajorVersion
}
, mkRekeyM = Nothing
}
Expand Down Expand Up @@ -454,9 +455,7 @@ mkProtocolCardanoAndHardForkTxs
-> SL.Nonce
-> Shelley.CoreNode c
-- HardForks
-> ProtocolParamsTransition
ByronBlock
(ShelleyBlock (ShelleyEra c))
-> ProtocolTransitionParamsShelleyBased (ShelleyEra c)
-> TestNodeInitialization m (CardanoBlock c)
mkProtocolCardanoAndHardForkTxs
pbftParams coreNodeId genesisByron generatedSecretsByron propPV
Expand Down Expand Up @@ -508,11 +507,15 @@ mkProtocolCardanoAndHardForkTxs
maryProtVer = SL.ProtVer maryMajorVersion 0
}
protocolParamsByronShelley
ProtocolParamsTransition {
transitionTrigger = TriggerHardForkAtVersion allegraMajorVersion
ProtocolTransitionParamsShelleyBased {
transitionTranslationContext = ()
, transitionTrigger =
TriggerHardForkAtVersion allegraMajorVersion
}
ProtocolParamsTransition {
transitionTrigger = TriggerHardForkAtVersion maryMajorVersion
ProtocolTransitionParamsShelleyBased {
transitionTranslationContext = ()
, transitionTrigger =
TriggerHardForkAtVersion maryMajorVersion
}

-- Byron
Expand Down
Expand Up @@ -52,7 +52,8 @@ import Ouroboros.Consensus.Shelley.Node

import Ouroboros.Consensus.Cardano.Condense ()
import Ouroboros.Consensus.Cardano.Node
(ProtocolParamsTransition (..), TriggerHardFork (..))
(ProtocolTransitionParamsShelleyBased (..),
TriggerHardFork (..))

import Test.ThreadNet.General
import Test.ThreadNet.Network (NodeOutput (..),
Expand Down Expand Up @@ -271,8 +272,10 @@ prop_simple_shelleyAllegra_convergence TestSetup
}
(SL.ProtVer majorVersion1 0)
(SL.ProtVer majorVersion2 0)
ProtocolParamsTransition {
transitionTrigger = TriggerHardForkAtVersion majorVersion2
ProtocolTransitionParamsShelleyBased {
transitionTranslationContext = ()
, transitionTrigger =
TriggerHardForkAtVersion majorVersion2
}
}
, mkRekeyM = Nothing
Expand Down
Expand Up @@ -12,7 +12,7 @@ module Ouroboros.Consensus.Cardano (
, ProtocolParamsByron (..)
, ProtocolParamsMary (..)
, ProtocolParamsShelley (..)
, ProtocolParamsTransition (..)
, ProtocolTransitionParamsShelleyBased (..)
, module X
) where

Expand Down
Expand Up @@ -506,6 +506,10 @@ translateLedgerStateAllegraToMaryWrapper =
Translate $ \_epochNo ->
unComp . SL.translateEra' () . Comp

{-------------------------------------------------------------------------------
Translation from Allegra to Mary
-------------------------------------------------------------------------------}

translateTxAllegraToMaryWrapper ::
PraosCrypto c
=> InjectTx
Expand Down

0 comments on commit 625d570

Please sign in to comment.