Skip to content

Commit bc2ec2c

Browse files
committed
Integrated weighted BlockFetch decision logic
1 parent 5362482 commit bc2ec2c

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

cabal.project

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ if impl (ghc >= 9.12)
6060
source-repository-package
6161
type: git
6262
location: https://github.com/IntersectMBO/ouroboros-network
63-
tag: 3e8d3b4b8c87ead794876c62d7fe25f32efb5142
64-
--sha256: 08fpkx3iagj83nn413h9a865zjcj3lrf7017a756qd2wg2jg3amq
63+
tag: 073938c7fa91fa45cf58c2f572c578ef278e8ff3
64+
--sha256: sha256-jQ79Iym796tk1JLexyzGCC7vQyCAt/FJo5Y666ggAG0=
6565
subdir:
6666
ouroboros-network-api
67+
ouroboros-network
6768

6869
source-repository-package
6970
type: git

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/BlockFetch/ClientInterface.hs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Ouroboros.Consensus.Ledger.SupportsProtocol
3333
)
3434
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client as CSClient
3535
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as CSJumping
36-
import Ouroboros.Consensus.Peras.Weight (emptyPerasWeightSnapshot)
36+
import Ouroboros.Consensus.Peras.Weight (PerasWeightSnapshot)
3737
import Ouroboros.Consensus.Storage.ChainDB.API
3838
( AddBlockPromise
3939
, ChainDB
@@ -46,11 +46,13 @@ import qualified Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunis
4646
import Ouroboros.Consensus.Util.AnchoredFragment
4747
import Ouroboros.Consensus.Util.IOLike
4848
import Ouroboros.Consensus.Util.Orphans ()
49+
import Ouroboros.Consensus.Util.STM
4950
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
5051
import qualified Ouroboros.Network.AnchoredFragment as AF
5152
import Ouroboros.Network.Block (MaxSlotNo)
5253
import Ouroboros.Network.BlockFetch.ConsensusInterface
5354
( BlockFetchConsensusInterface (..)
55+
, ChainComparison (..)
5456
, ChainSelStarvation
5557
, FetchMode (..)
5658
, FromConsensus (..)
@@ -67,6 +69,7 @@ data ChainDbView m blk = ChainDbView
6769
, getMaxSlotNo :: STM m MaxSlotNo
6870
, addBlockAsync :: InvalidBlockPunishment m -> blk -> m (AddBlockPromise m blk)
6971
, getChainSelStarvation :: STM m ChainSelStarvation
72+
, getPerasWeightSnapshot :: STM m (WithFingerprint (PerasWeightSnapshot blk))
7073
}
7174

7275
defaultChainDbView :: ChainDB m blk -> ChainDbView m blk
@@ -78,6 +81,7 @@ defaultChainDbView chainDB =
7881
, getMaxSlotNo = ChainDB.getMaxSlotNo chainDB
7982
, addBlockAsync = ChainDB.addBlockAsync chainDB
8083
, getChainSelStarvation = ChainDB.getChainSelStarvation chainDB
84+
, getPerasWeightSnapshot = ChainDB.getPerasWeightSnapshot chainDB
8185
}
8286

8387
readFetchModeDefault ::
@@ -227,6 +231,16 @@ mkBlockFetchConsensusInterface
227231
readFetchedMaxSlotNo :: STM m MaxSlotNo
228232
readFetchedMaxSlotNo = getMaxSlotNo chainDB
229233

234+
readChainComparison :: STM m (WithFingerprint (ChainComparison (HeaderWithTime blk)))
235+
readChainComparison =
236+
fmap mkChainComparison <$> getPerasWeightSnapshot chainDB
237+
where
238+
mkChainComparison weights =
239+
ChainComparison
240+
{ plausibleCandidateChain = plausibleCandidateChain weights
241+
, compareCandidateChains = compareCandidateChains weights
242+
}
243+
230244
-- Note that @ours@ comes from the ChainDB and @cand@ from the ChainSync
231245
-- client.
232246
--
@@ -242,10 +256,11 @@ mkBlockFetchConsensusInterface
242256
-- fragment, our current chain might have changed.
243257
plausibleCandidateChain ::
244258
HasCallStack =>
259+
PerasWeightSnapshot blk ->
245260
AnchoredFragment (HeaderWithTime blk) ->
246261
AnchoredFragment (HeaderWithTime blk) ->
247262
Bool
248-
plausibleCandidateChain ours cand =
263+
plausibleCandidateChain weights ours cand =
249264
-- 1. The ChainDB maintains the invariant that the anchor of our fragment
250265
-- corresponds to the immutable tip.
251266
--
@@ -270,13 +285,11 @@ mkBlockFetchConsensusInterface
270285
Just _ -> preferAnchoredCandidate bcfg weights ours cand
271286

272287
compareCandidateChains ::
288+
PerasWeightSnapshot blk ->
273289
AnchoredFragment (HeaderWithTime blk) ->
274290
AnchoredFragment (HeaderWithTime blk) ->
275291
Ordering
276-
compareCandidateChains = compareAnchoredFragments bcfg weights
277-
278-
-- TODO requires https://github.com/IntersectMBO/ouroboros-network/pull/5161
279-
weights = emptyPerasWeightSnapshot
292+
compareCandidateChains = compareAnchoredFragments bcfg
280293

281294
headerForgeUTCTime :: FromConsensus (HeaderWithTime blk) -> STM m UTCTime
282295
headerForgeUTCTime =

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/STM.hs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
{-# LANGUAGE DeriveAnyClass #-}
2-
{-# LANGUAGE DeriveFunctor #-}
3-
{-# LANGUAGE DeriveGeneric #-}
41
{-# LANGUAGE DerivingStrategies #-}
52
{-# LANGUAGE FlexibleContexts #-}
6-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
73
{-# LANGUAGE LambdaCase #-}
84
{-# LANGUAGE RankNTypes #-}
95
{-# LANGUAGE ScopedTypeVariables #-}
@@ -36,10 +32,12 @@ import Control.Monad (void)
3632
import Control.Monad.State (StateT (..))
3733
import Control.ResourceRegistry
3834
import Data.Void
39-
import Data.Word (Word64)
40-
import GHC.Generics (Generic)
4135
import GHC.Stack
4236
import Ouroboros.Consensus.Util.IOLike
37+
import Ouroboros.Network.BlockFetch.ConsensusInterface
38+
( Fingerprint (..)
39+
, WithFingerprint (..)
40+
)
4341

4442
{-------------------------------------------------------------------------------
4543
Misc
@@ -83,20 +81,6 @@ blockUntilJust getMaybeA = do
8381
blockUntilAllJust :: MonadSTM m => [STM m (Maybe a)] -> STM m [a]
8482
blockUntilAllJust = mapM blockUntilJust
8583

86-
-- | Simple type that can be used to indicate something in a @TVar@ is
87-
-- changed.
88-
newtype Fingerprint = Fingerprint Word64
89-
deriving stock (Show, Eq, Generic)
90-
deriving newtype Enum
91-
deriving anyclass NoThunks
92-
93-
-- | Store a value together with its fingerprint.
94-
data WithFingerprint a = WithFingerprint
95-
{ forgetFingerprint :: !a
96-
, getFingerprint :: !Fingerprint
97-
}
98-
deriving (Show, Eq, Functor, Generic, NoThunks)
99-
10084
{-------------------------------------------------------------------------------
10185
Simulate monad stacks
10286
-------------------------------------------------------------------------------}

ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/BlockFetch/Client.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ runBlockFetchTest BlockFetchClientTestSetup{..} = withRegistry \registry -> do
306306
getMaxSlotNo = ChainDB.getMaxSlotNo chainDB
307307
addBlockAsync = ChainDB.addBlockAsync chainDB
308308
getChainSelStarvation = ChainDB.getChainSelStarvation chainDB
309+
getPerasWeightSnapshot = ChainDB.getPerasWeightSnapshot chainDB
309310
pure BlockFetchClientInterface.ChainDbView{..}
310311
where
311312
cdbTracer = Tracer \case

0 commit comments

Comments
 (0)