Skip to content

Commit dd9770d

Browse files
committed
Move PerasWeightSnapshot to separate module
It makes sense to use this without using an entire PerasCertDB, so decouple these. It might be nice to rename PerasCertDB-bench, but doesn't seem like a priority.
1 parent e1cb8f9 commit dd9770d

File tree

9 files changed

+57
-45
lines changed

9 files changed

+57
-45
lines changed

ouroboros-consensus/bench/PerasCertDB-bench/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{-# LANGUAGE ImportQualifiedPost #-}
22
{-# LANGUAGE LambdaCase #-}
33

4-
-- | This module contains benchmarks for Peras chain weight calculation as implemented by
5-
-- the by the 'Ouroboros.Consensus.Storage.PerasCertDB.API.boostedWeightForFragment'
6-
-- function.
4+
-- | This module contains benchmarks for Peras chain weight calculation as
5+
-- implemented by the by the
6+
-- 'Ouroboros.Consensus.Peras.Weight.boostedWeightForFragment' function.
77
--
88
-- We benchmark the calculation on a static sequence of chain fragments of increasing
99
-- length, ranging from 0 to around 8640, with a sampling rate of 100. The chain fragments
@@ -15,7 +15,7 @@ import Data.List (iterate')
1515
import Data.Map.Strict qualified as Map
1616
import Numeric.Natural (Natural)
1717
import Ouroboros.Consensus.Block (PerasWeight (PerasWeight), SlotNo (..))
18-
import Ouroboros.Consensus.Storage.PerasCertDB.API
18+
import Ouroboros.Consensus.Peras.Weight
1919
( PerasWeightSnapshot (..)
2020
, boostedWeightForFragment
2121
)

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ library
190190
Ouroboros.Consensus.Node.Run
191191
Ouroboros.Consensus.Node.Serialisation
192192
Ouroboros.Consensus.NodeId
193+
Ouroboros.Consensus.Peras.Weight
193194
Ouroboros.Consensus.Protocol.Abstract
194195
Ouroboros.Consensus.Protocol.BFT
195196
Ouroboros.Consensus.Protocol.LeaderSchedule
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE DerivingVia #-}
4+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
6+
7+
module Ouroboros.Consensus.Peras.Weight
8+
( PerasWeightSnapshot (..)
9+
, boostedWeightForPoint
10+
, boostedWeightForFragment
11+
) where
12+
13+
import Data.Map.Strict (Map)
14+
import qualified Data.Map.Strict as Map
15+
import GHC.Generics (Generic)
16+
import NoThunks.Class
17+
import Ouroboros.Consensus.Block
18+
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
19+
import qualified Ouroboros.Network.AnchoredFragment as AF
20+
21+
newtype PerasWeightSnapshot blk = PerasWeightSnapshot
22+
{ getPerasWeightSnapshot :: Map (Point blk) PerasWeight
23+
}
24+
deriving stock (Show, Eq)
25+
deriving Generic
26+
deriving newtype NoThunks
27+
28+
boostedWeightForPoint ::
29+
forall blk.
30+
StandardHash blk =>
31+
PerasWeightSnapshot blk -> Point blk -> PerasWeight
32+
boostedWeightForPoint (PerasWeightSnapshot weightByPoint) pt =
33+
Map.findWithDefault mempty pt weightByPoint
34+
35+
boostedWeightForFragment ::
36+
forall blk.
37+
HasHeader blk =>
38+
PerasWeightSnapshot blk ->
39+
AnchoredFragment blk ->
40+
PerasWeight
41+
boostedWeightForFragment weightSnap frag =
42+
-- TODO think about whether this could be done in sublinear complexity
43+
foldMap
44+
(boostedWeightForPoint weightSnap)
45+
(blockPoint <$> AF.toOldestFirst frag)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/API.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ import Ouroboros.Consensus.HeaderStateHistory
8383
import Ouroboros.Consensus.HeaderValidation (HeaderWithTime (..))
8484
import Ouroboros.Consensus.Ledger.Abstract
8585
import Ouroboros.Consensus.Ledger.Extended
86+
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
8687
import Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunishment
8788
import Ouroboros.Consensus.Storage.Common
8889
import Ouroboros.Consensus.Storage.LedgerDB
8990
( GetForkerError
9091
, ReadOnlyForker'
9192
, Statistics
9293
)
93-
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasWeightSnapshot)
9494
import Ouroboros.Consensus.Storage.Serialisation
9595
import Ouroboros.Consensus.Util.CallStack
9696
import Ouroboros.Consensus.Util.IOLike

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Query.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Ouroboros.Consensus.HeaderStateHistory
4444
import Ouroboros.Consensus.HeaderValidation (HeaderWithTime)
4545
import Ouroboros.Consensus.Ledger.Abstract (EmptyMK)
4646
import Ouroboros.Consensus.Ledger.Extended
47+
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
4748
import Ouroboros.Consensus.Protocol.Abstract
4849
import Ouroboros.Consensus.Storage.ChainDB.API
4950
( BlockComponent (..)
@@ -54,7 +55,6 @@ import Ouroboros.Consensus.Storage.ImmutableDB (ImmutableDB)
5455
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
5556
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
5657
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
57-
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasWeightSnapshot)
5858
import Ouroboros.Consensus.Storage.VolatileDB (VolatileDB)
5959
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
6060
import Ouroboros.Consensus.Util (eitherToMaybe)
Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE DeriveGeneric #-}
32
{-# LANGUAGE DerivingVia #-}
4-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
53
{-# LANGUAGE ScopedTypeVariables #-}
64

75
module Ouroboros.Consensus.Storage.PerasCertDB.API
86
( PerasCertDB (..)
9-
, PerasWeightSnapshot (..)
10-
, boostedWeightForPoint
11-
, boostedWeightForFragment
127
) where
138

14-
import Data.Map.Strict (Map)
15-
import qualified Data.Map.Strict as Map
16-
import GHC.Generics (Generic)
179
import NoThunks.Class
1810
import Ouroboros.Consensus.Block
11+
import Ouroboros.Consensus.Peras.Weight
1912
import Ouroboros.Consensus.Util.IOLike
20-
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
21-
import qualified Ouroboros.Network.AnchoredFragment as AF
2213

2314
data PerasCertDB m blk = PerasCertDB
2415
{ addCert :: PerasCert blk -> m ()
@@ -28,30 +19,3 @@ data PerasCertDB m blk = PerasCertDB
2819
, closeDB :: m ()
2920
}
3021
deriving NoThunks via OnlyCheckWhnfNamed "PerasCertDB" (PerasCertDB m blk)
31-
32-
newtype PerasWeightSnapshot blk = PerasWeightSnapshot
33-
{ getPerasWeightSnapshot :: Map (Point blk) PerasWeight
34-
}
35-
deriving stock (Show, Eq)
36-
deriving Generic
37-
deriving newtype NoThunks
38-
39-
boostedWeightForPoint ::
40-
forall blk.
41-
StandardHash blk =>
42-
PerasWeightSnapshot blk -> Point blk -> PerasWeight
43-
boostedWeightForPoint (PerasWeightSnapshot weightByPoint) pt =
44-
Map.findWithDefault mempty pt weightByPoint
45-
46-
boostedWeightForFragment ::
47-
forall blk.
48-
HasHeader blk =>
49-
PerasWeightSnapshot blk ->
50-
AnchoredFragment blk ->
51-
PerasWeight
52-
boostedWeightForFragment weightSnap frag =
53-
-- TODO think about whether this could be done in sublinear complexity
54-
-- probably should write microbenchmarks at some point to see if this is a bottleneck
55-
foldMap
56-
(boostedWeightForPoint weightSnap)
57-
(blockPoint <$> AF.toOldestFirst frag)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/PerasCertDB/Impl.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import qualified Data.Map.Strict as Map
2929
import GHC.Generics (Generic)
3030
import NoThunks.Class
3131
import Ouroboros.Consensus.Block
32+
import Ouroboros.Consensus.Peras.Weight
3233
import Ouroboros.Consensus.Storage.PerasCertDB.API
3334
import Ouroboros.Consensus.Util.Args
3435
import Ouroboros.Consensus.Util.CallStack

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/PerasCertDB/Model.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Data.Set (Set)
1919
import qualified Data.Set as Set
2020
import GHC.Generics (Generic)
2121
import Ouroboros.Consensus.Block
22-
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasWeightSnapshot (..))
22+
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot (..))
2323

2424
data Model blk = Model
2525
{ certs :: Set (PerasCert blk)

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/PerasCertDB/StateMachine.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import Control.Monad.State
1818
import Control.Tracer (nullTracer)
1919
import qualified Data.List.NonEmpty as NE
2020
import Ouroboros.Consensus.Block
21+
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
2122
import qualified Ouroboros.Consensus.Storage.PerasCertDB as PerasCertDB
22-
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasCertDB, PerasWeightSnapshot)
23+
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasCertDB)
2324
import Ouroboros.Consensus.Util.IOLike
2425
import qualified Test.Ouroboros.Storage.PerasCertDB.Model as Model
2526
import Test.QuickCheck

0 commit comments

Comments
 (0)