4
4
{-# LANGUAGE OverloadedRecordDot #-}
5
5
{-# LANGUAGE RecordWildCards #-}
6
6
{-# LANGUAGE ScopedTypeVariables #-}
7
+ {-# LANGUAGE TupleSections #-}
7
8
{-# LANGUAGE TypeApplications #-}
8
9
{-# LANGUAGE NoFieldSelectors #-}
9
10
@@ -16,10 +17,12 @@ import Control.Concurrent.Class.MonadSTM (
16
17
)
17
18
import Control.Exception (assert )
18
19
import Control.Monad (forM )
20
+ import Data.Bifunctor
19
21
import Data.Kind
22
+ import Data.Maybe (fromMaybe )
20
23
import LeiosProtocol.Common
21
24
import LeiosProtocol.Short hiding (Stage (.. ))
22
- import PraosProtocol.Common (fixupBlock , mkPartialBlock )
25
+ import PraosProtocol.Common (CPUTask ( CPUTask ), fixupBlock , mkPartialBlock )
23
26
import System.Random
24
27
25
28
data BuffersView m = BuffersView
@@ -83,7 +86,7 @@ data BlockGeneratorConfig m = BlockGeneratorConfig
83
86
, nodeId :: NodeId
84
87
, buffers :: BuffersView m
85
88
, schedule :: SlotNo -> m [(SomeRole , Word64 )]
86
- , submit :: [SomeAction ] -> m ()
89
+ , submit :: [([ CPUTask ], SomeAction ) ] -> m ()
87
90
}
88
91
89
92
blockGenerator ::
@@ -99,30 +102,36 @@ blockGenerator BlockGeneratorConfig{..} = go (0, 0)
99
102
(actions, blkId') <- runStateT (mapM (execute slot) roles) blkId
100
103
submit actions
101
104
go (blkId', slot + 1 )
102
- execute slot (SomeRole r, wins) = assert (wins >= 1 ) $ SomeAction r <$> execute' slot r wins
103
- execute' :: SlotNo -> Role a -> Word64 -> StateT Int m a
105
+ execute slot (SomeRole r, wins) = assert (wins >= 1 ) $ second ( SomeAction r) <$> execute' slot r wins
106
+ execute' :: SlotNo -> Role a -> Word64 -> StateT Int m ([ CPUTask ], a )
104
107
execute' slot Base _wins = do
105
108
rbData <- lift $ atomically $ buffers. newRBData
106
- let body = mkRankingBlockBody leios nodeId rbData. freshestCertifiedEB rbData. txsPayload
109
+ let meb = rbData. freshestCertifiedEB
110
+ let ! task = CPUTask $ fromMaybe 0 $ leios. delays. certificateCreation . snd <$> meb
111
+ let body = mkRankingBlockBody leios nodeId meb rbData. txsPayload
112
+ let ! rb = fixupBlock @ _ @ RankingBlock rbData. headAnchor (mkPartialBlock slot body)
107
113
-- TODO: maybe submit should do the fixupBlock.
108
- return $! fixupBlock @ _ @ RankingBlock rbData. headAnchor (mkPartialBlock slot body)
109
- execute' slot Propose wins = do
110
- ibData <- lift $ atomically $ buffers. newIBData
111
- forM [toEnum $ fromIntegral sub | sub <- [0 .. wins - 1 ]] $ \ sub -> do
112
- i <- nextBlkId InputBlockId
113
- let header = mkInputBlockHeader leios i slot sub nodeId ibData. referenceRankingBlock
114
- return $! mkInputBlock leios header ibData. txsPayload
115
- execute' slot Endorse _wins = do
116
- i <- nextBlkId EndorseBlockId
117
- ibs <- lift $ atomically $ buffers. ibs
118
- return $! mkEndorseBlock leios i slot nodeId $ inputBlocksToEndorse leios slot ibs
119
- execute' slot Vote votes = do
120
- votingFor <- lift $ atomically $ do
121
- ibs <- buffers. ibs
122
- ebs <- buffers. ebs
123
- pure $ endorseBlocksToVoteFor leios slot ibs ebs
124
- i <- nextBlkId VoteId
125
- return $! mkVoteMsg leios i slot nodeId votes votingFor
114
+ return ([task], rb)
115
+ execute' slot Propose wins =
116
+ ([] ,) <$> do
117
+ ibData <- lift $ atomically $ buffers. newIBData
118
+ forM [toEnum $ fromIntegral sub | sub <- [0 .. wins - 1 ]] $ \ sub -> do
119
+ i <- nextBlkId InputBlockId
120
+ let header = mkInputBlockHeader leios i slot sub nodeId ibData. referenceRankingBlock
121
+ return $! mkInputBlock leios header ibData. txsPayload
122
+ execute' slot Endorse _wins =
123
+ ([] ,) <$> do
124
+ i <- nextBlkId EndorseBlockId
125
+ ibs <- lift $ atomically $ buffers. ibs
126
+ return $! mkEndorseBlock leios i slot nodeId $ inputBlocksToEndorse leios slot ibs
127
+ execute' slot Vote votes =
128
+ ([] ,) <$> do
129
+ votingFor <- lift $ atomically $ do
130
+ ibs <- buffers. ibs
131
+ ebs <- buffers. ebs
132
+ pure $ endorseBlocksToVoteFor leios slot ibs ebs
133
+ i <- nextBlkId VoteId
134
+ return $! mkVoteMsg leios i slot nodeId votes votingFor
126
135
nextBlkId :: (NodeId -> Int -> a ) -> StateT Int m a
127
136
nextBlkId f = do
128
137
i <- get
0 commit comments