Skip to content

Commit c0aacc2

Browse files
committed
WIP ThreadNet: resurrect protocolInfoPraosShelleyBased
1 parent ea464a5 commit c0aacc2

File tree

1 file changed

+170
-8
lines changed
  • ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node

1 file changed

+170
-8
lines changed

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Node/Praos.hs

Lines changed: 170 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,61 @@ module Ouroboros.Consensus.Shelley.Node.Praos
1414
( -- * BlockForging
1515
praosBlockForging
1616
, praosSharedBlockForging
17+
, protocolInfoPraosShelleyBased
1718
) where
1819

1920
import qualified Cardano.Ledger.Api.Era as L
21+
import qualified Cardano.Ledger.Api.Transition as L
22+
import Cardano.Ledger.BaseTypes (NonZero (unNonZero))
23+
import qualified Cardano.Ledger.BaseTypes as SL
24+
import qualified Cardano.Ledger.Shelley.API as SL
25+
import qualified Cardano.Protocol.TPraos.API as SL
2026
import qualified Cardano.Protocol.TPraos.OCert as Absolute
2127
import qualified Cardano.Protocol.TPraos.OCert as SL
28+
import Cardano.Slotting.EpochInfo
29+
import Cardano.Slotting.Time (mkSlotLength)
30+
import Control.Monad.Except (Except)
31+
import qualified Control.Tracer as Tracer
2232
import qualified Data.Text as T
33+
import Lens.Micro ((^.))
2334
import Ouroboros.Consensus.Block
24-
import Ouroboros.Consensus.Config (configConsensus)
35+
import Ouroboros.Consensus.Config
36+
import qualified Ouroboros.Consensus.HardFork.History as History
37+
import Ouroboros.Consensus.HeaderValidation
38+
import Ouroboros.Consensus.Ledger.Abstract
39+
import Ouroboros.Consensus.Ledger.Extended
40+
import Ouroboros.Consensus.Ledger.SupportsMempool (TxLimits)
2541
import qualified Ouroboros.Consensus.Ledger.SupportsMempool as Mempool
42+
import Ouroboros.Consensus.Ledger.Tables.Utils
43+
import Ouroboros.Consensus.Node.ProtocolInfo
44+
import Ouroboros.Consensus.Protocol.Abstract
45+
import Ouroboros.Consensus.Protocol.Ledger.HotKey (HotKey)
2646
import qualified Ouroboros.Consensus.Protocol.Ledger.HotKey as HotKey
2747
import Ouroboros.Consensus.Protocol.Praos
28-
( Praos
48+
( ConsensusConfig (..)
49+
, Praos
2950
, PraosParams (..)
51+
, PraosState (..)
3052
, praosCheckCanForge
3153
)
54+
import Ouroboros.Consensus.Protocol.Praos.AgentClient
55+
import Ouroboros.Consensus.Protocol.Praos.Common
56+
import Ouroboros.Consensus.Protocol.TPraos
3257
import Ouroboros.Consensus.Shelley.Ledger
33-
( ShelleyBlock
34-
, ShelleyCompatible
35-
, forgeShelleyBlock
36-
)
58+
import Ouroboros.Consensus.Shelley.Ledger.Inspect ()
59+
import Ouroboros.Consensus.Shelley.Ledger.NetworkProtocolVersion ()
3760
import Ouroboros.Consensus.Shelley.Node.Common
38-
( ShelleyEraWithCrypto
61+
( ProtocolParamsShelleyBased (..)
62+
, ShelleyEraWithCrypto
3963
, ShelleyLeaderCredentials (..)
64+
, shelleyBlockIssuerVKey
65+
, validateGenesis
4066
)
67+
import Ouroboros.Consensus.Shelley.Node.Serialisation ()
4168
import Ouroboros.Consensus.Shelley.Protocol.Praos ()
42-
import Ouroboros.Consensus.Util.IOLike (IOLike)
69+
import Ouroboros.Consensus.Shelley.Protocol.TPraos ()
70+
import Ouroboros.Consensus.Util.Assert
71+
import Ouroboros.Consensus.Util.IOLike
4372

4473
{-------------------------------------------------------------------------------
4574
BlockForging
@@ -102,3 +131,136 @@ praosSharedBlockForging
102131
cfg
103132
, finalize = HotKey.finalize hotKey
104133
}
134+
135+
protocolInfoPraosShelleyBased ::
136+
forall m era c.
137+
( ShelleyCompatible (Praos c) era
138+
, TxLimits (ShelleyBlock (Praos c) era)
139+
, KESAgentContext c m
140+
) =>
141+
ProtocolParamsShelleyBased c ->
142+
L.TransitionConfig era ->
143+
-- | see 'shelleyProtVer', mutatis mutandi
144+
SL.ProtVer ->
145+
( ProtocolInfo (ShelleyBlock (Praos c) era)
146+
, Tracer.Tracer m KESAgentClientTrace -> m [MkBlockForging m (ShelleyBlock (Praos c) era)]
147+
)
148+
protocolInfoPraosShelleyBased
149+
ProtocolParamsShelleyBased
150+
{ shelleyBasedInitialNonce = initialNonce
151+
, shelleyBasedLeaderCredentials = credentialss
152+
}
153+
transitionCfg
154+
protVer =
155+
assertWithMsg (validateGenesis genesis) $
156+
( ProtocolInfo
157+
{ pInfoConfig = topLevelConfig
158+
, pInfoInitLedger = initExtLedgerState
159+
}
160+
, \tr -> pure $ mkBlockForging tr <$> credentialss
161+
)
162+
where
163+
mkBlockForging ::
164+
Tracer.Tracer m KESAgentClientTrace ->
165+
ShelleyLeaderCredentials c ->
166+
MkBlockForging m (ShelleyBlock (Praos c) era)
167+
mkBlockForging tr credentials = MkBlockForging $ do
168+
let canBeLeader = shelleyLeaderCredentialsCanBeLeader credentials
169+
170+
hotKey :: HotKey c m <-
171+
instantiatePraosCredentials
172+
(praosMaxKESEvo praosParams)
173+
tr
174+
(praosCanBeLeaderCredentialsSource canBeLeader)
175+
176+
return $ praosBlockForging praosParams hotKey credentials
177+
178+
genesis :: SL.ShelleyGenesis
179+
genesis = transitionCfg ^. L.tcShelleyGenesisL
180+
181+
maxMajorProtVer :: MaxMajorProtVer
182+
maxMajorProtVer = MaxMajorProtVer $ SL.pvMajor protVer
183+
184+
topLevelConfig :: TopLevelConfig (ShelleyBlock (Praos c) era)
185+
topLevelConfig =
186+
TopLevelConfig
187+
{ topLevelConfigProtocol = consensusConfig
188+
, topLevelConfigLedger = ledgerConfig
189+
, topLevelConfigBlock = blockConfig
190+
, topLevelConfigCodec = ShelleyCodecConfig
191+
, topLevelConfigStorage = storageConfig
192+
, topLevelConfigCheckpoints = emptyCheckpointsMap
193+
}
194+
195+
consensusConfig :: ConsensusConfig (BlockProtocol (ShelleyBlock (Praos c) era))
196+
consensusConfig =
197+
PraosConfig
198+
{ praosParams
199+
, praosEpochInfo = epochInfo
200+
}
201+
202+
ledgerConfig :: LedgerConfig (ShelleyBlock (Praos c) era)
203+
ledgerConfig =
204+
mkShelleyLedgerConfig
205+
genesis
206+
(transitionCfg ^. L.tcTranslationContextL)
207+
epochInfo
208+
209+
epochInfo :: EpochInfo (Except History.PastHorizonException)
210+
epochInfo =
211+
fixedEpochInfo
212+
(SL.sgEpochLength genesis)
213+
(mkSlotLength $ SL.fromNominalDiffTimeMicro $ SL.sgSlotLength genesis)
214+
215+
praosParams :: PraosParams
216+
praosParams =
217+
PraosParams
218+
{ praosSlotsPerKESPeriod = SL.sgSlotsPerKESPeriod genesis
219+
, praosLeaderF = SL.mkActiveSlotCoeff $ SL.sgActiveSlotsCoeff genesis
220+
, praosSecurityParam = SecurityParam $ SL.sgSecurityParam genesis
221+
, praosMaxKESEvo = SL.sgMaxKESEvolutions genesis
222+
, praosMaxMajorPV = maxMajorProtVer
223+
, praosRandomnessStabilisationWindow =
224+
SL.computeRandomnessStabilisationWindow
225+
(unNonZero $ SL.sgSecurityParam genesis)
226+
(SL.mkActiveSlotCoeff $ SL.sgActiveSlotsCoeff genesis)
227+
}
228+
229+
blockConfig :: BlockConfig (ShelleyBlock (Praos c) era)
230+
blockConfig =
231+
mkShelleyBlockConfig
232+
protVer
233+
genesis
234+
(shelleyBlockIssuerVKey <$> credentialss)
235+
236+
storageConfig :: StorageConfig (ShelleyBlock (Praos c) era)
237+
storageConfig =
238+
ShelleyStorageConfig
239+
{ shelleyStorageConfigSlotsPerKESPeriod = praosSlotsPerKESPeriod praosParams
240+
, shelleyStorageConfigSecurityParam = praosSecurityParam praosParams
241+
}
242+
243+
initLedgerState :: LedgerState (ShelleyBlock (Praos c) era) ValuesMK
244+
initLedgerState =
245+
unstowLedgerTables
246+
ShelleyLedgerState
247+
{ shelleyLedgerTip = Origin
248+
, shelleyLedgerState =
249+
L.injectIntoTestState transitionCfg $
250+
L.createInitialState transitionCfg
251+
, shelleyLedgerTransition = ShelleyTransitionInfo{shelleyAfterVoting = 0}
252+
, shelleyLedgerTables = emptyLedgerTables
253+
}
254+
255+
initChainDepState :: PraosState
256+
initChainDepState =
257+
translateChainDepState (Proxy @(TPraos c, Praos c)) $
258+
TPraosState Origin $
259+
SL.initialChainDepState initialNonce (SL.sgGenDelegs genesis)
260+
261+
initExtLedgerState :: ExtLedgerState (ShelleyBlock (Praos c) era) ValuesMK
262+
initExtLedgerState =
263+
ExtLedgerState
264+
{ ledgerState = initLedgerState
265+
, headerState = genesisHeaderState initChainDepState
266+
}

0 commit comments

Comments
 (0)