Skip to content

Commit 5362482

Browse files
committed
Introduce weighted chain comparisons
1 parent 2ae06b4 commit 5362482

File tree

10 files changed

+171
-171
lines changed

10 files changed

+171
-171
lines changed

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,18 @@ initNodeKernel
270270
gsmTracerArgs
271271
GSM.GsmView
272272
{ GSM.antiThunderingHerd = Just gsmAntiThunderingHerd
273-
, GSM.getCandidateOverSelection = pure $ \(headers, _lst) state ->
274-
case AF.intersectionPoint headers (csCandidate state) of
275-
Nothing -> GSM.CandidateDoesNotIntersect
276-
Just{} ->
277-
GSM.WhetherCandidateIsBetter $ -- precondition requires intersection
278-
preferAnchoredCandidate
279-
(configBlock cfg)
280-
headers
281-
(csCandidate state)
273+
, GSM.getCandidateOverSelection = do
274+
weights <- ChainDB.getPerasWeightSnapshot chainDB
275+
pure $ \(headers, _lst) state ->
276+
case AF.intersectionPoint headers (csCandidate state) of
277+
Nothing -> GSM.CandidateDoesNotIntersect
278+
Just{} ->
279+
GSM.WhetherCandidateIsBetter $ -- precondition requires intersection
280+
preferAnchoredCandidate
281+
(configBlock cfg)
282+
(forgetFingerprint weights)
283+
headers
284+
(csCandidate state)
282285
, GSM.peerIsIdle = csIdling
283286
, GSM.durationUntilTooOld =
284287
gsmDurationUntilTooOld

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/Genesis/Tests/LoE/CaughtUp.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import qualified Ouroboros.Consensus.Node.GSM as GSM
5858
import Ouroboros.Consensus.Node.Genesis (setGetLoEFragment)
5959
import Ouroboros.Consensus.Node.GsmState
6060
import Ouroboros.Consensus.NodeId
61+
import Ouroboros.Consensus.Peras.Weight (emptyPerasWeightSnapshot)
6162
import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
6263
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
6364
import qualified Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunishment as Punishment
@@ -301,10 +302,13 @@ mkGsmEntryPoints varChainSyncHandles chainDB writeGsmState =
301302
Just{} ->
302303
-- precondition requires intersection
303304
GSM.WhetherCandidateIsBetter $
304-
preferAnchoredCandidate (configBlock cfg) selection candFrag
305+
preferAnchoredCandidate (configBlock cfg) weights selection candFrag
305306
where
306307
candFrag = csCandidate candidateState
307308

309+
-- TODO https://github.com/tweag/cardano-peras/issues/67
310+
weights = emptyPerasWeightSnapshot
311+
308312
forkGDD ::
309313
forall m.
310314
IOLike m =>

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

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +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)
3637
import Ouroboros.Consensus.Storage.ChainDB.API
3738
( AddBlockPromise
3839
, ChainDB
@@ -244,7 +245,7 @@ mkBlockFetchConsensusInterface
244245
AnchoredFragment (HeaderWithTime blk) ->
245246
AnchoredFragment (HeaderWithTime blk) ->
246247
Bool
247-
plausibleCandidateChain ours cand
248+
plausibleCandidateChain ours cand =
248249
-- 1. The ChainDB maintains the invariant that the anchor of our fragment
249250
-- corresponds to the immutable tip.
250251
--
@@ -258,45 +259,24 @@ mkBlockFetchConsensusInterface
258259
-- point. This means that we are no longer guaranteed that the
259260
-- precondition holds.
260261
--
261-
-- 4. Our chain's anchor can only move forward. We can detect this by
262-
-- looking at the block/slot numbers of the anchors: When the anchor
263-
-- advances, either the block number increases (usual case), or the
264-
-- block number stays the same, but the slot number increases (EBB
265-
-- case).
266-
--
267-
| anchorBlockNoAndSlot cand < anchorBlockNoAndSlot ours -- (4)
268-
=
269-
case (AF.null ours, AF.null cand) of
270-
-- Both are non-empty, the precondition trivially holds.
271-
(False, False) -> preferAnchoredCandidate bcfg ours cand
272-
-- The candidate is shorter than our chain and, worse, we'd have to
273-
-- roll back past our immutable tip (the anchor of @cand@).
274-
(_, True) -> False
275-
-- As argued above we can only reach this case when our chain's anchor
276-
-- has changed (4).
277-
--
278-
-- It is impossible for our chain to change /and/ still be empty: the
279-
-- anchor of our chain only changes when a new block becomes
280-
-- immutable. For a new block to become immutable, we must have
281-
-- extended our chain with at least @k + 1@ blocks. Which means our
282-
-- fragment can't be empty.
283-
(True, _) -> error "impossible"
284-
| otherwise =
285-
preferAnchoredCandidate bcfg ours cand
286-
where
287-
anchorBlockNoAndSlot ::
288-
AnchoredFragment (HeaderWithTime blk) ->
289-
(WithOrigin BlockNo, WithOrigin SlotNo)
290-
anchorBlockNoAndSlot frag =
291-
(AF.anchorToBlockNo a, AF.anchorToSlotNo a)
292-
where
293-
a = AF.anchor frag
262+
-- 4. Therefore, we check whether the candidate fragments still intersects
263+
-- with our fragment; if not, then it is only a matter of time until the
264+
-- ChainSync client disconnects from that peer.
265+
case AF.intersectionPoint ours cand of
266+
-- REVIEW: Hmm, maybe we want to change 'preferAnchoredCandidates' to
267+
-- also just return 'False' in this case (and we remove the
268+
-- precondition).
269+
Nothing -> False
270+
Just _ -> preferAnchoredCandidate bcfg weights ours cand
294271

295272
compareCandidateChains ::
296273
AnchoredFragment (HeaderWithTime blk) ->
297274
AnchoredFragment (HeaderWithTime blk) ->
298275
Ordering
299-
compareCandidateChains = compareAnchoredFragments bcfg
276+
compareCandidateChains = compareAnchoredFragments bcfg weights
277+
278+
-- TODO requires https://github.com/IntersectMBO/ouroboros-network/pull/5161
279+
weights = emptyPerasWeightSnapshot
300280

301281
headerForgeUTCTime :: FromConsensus (HeaderWithTime blk) -> STM m UTCTime
302282
headerForgeUTCTime =

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ChainSync/Client.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as Ju
125125
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.State
126126
import Ouroboros.Consensus.Node.GsmState (GsmState (..))
127127
import Ouroboros.Consensus.Node.NetworkProtocolVersion
128+
import Ouroboros.Consensus.Peras.Weight (emptyPerasWeightSnapshot)
128129
import Ouroboros.Consensus.Protocol.Abstract
129130
import Ouroboros.Consensus.Storage.ChainDB (ChainDB)
130131
import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
@@ -1856,7 +1857,12 @@ checkTime cfgEnv dynEnv intEnv =
18561857
checkPreferTheirsOverOurs kis
18571858
| -- Precondition is fulfilled as ourFrag and theirFrag intersect by
18581859
-- construction.
1859-
preferAnchoredCandidate (configBlock cfg) ourFrag theirFrag =
1860+
preferAnchoredCandidate
1861+
(configBlock cfg)
1862+
-- TODO: remove this entire check, see https://github.com/tweag/cardano-peras/issues/64
1863+
emptyPerasWeightSnapshot
1864+
ourFrag
1865+
theirFrag =
18601866
pure ()
18611867
| otherwise =
18621868
throwSTM $

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
177177

178178
traceWith initChainSelTracer StartedInitChainSelection
179179
initialLoE <- Args.cdbsLoE cdbSpecificArgs
180+
initialWeights <- atomically $ PerasCertDB.getWeightSnapshot perasCertDB
180181
chain <- withRegistry $ \rr -> do
181182
chainAndLedger <-
182183
ChainSel.initialChainSelection
@@ -188,6 +189,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
188189
(Args.cdbsTopLevelConfig cdbSpecificArgs)
189190
varInvalid
190191
(void initialLoE)
192+
(forgetFingerprint initialWeights)
191193
traceWith initChainSelTracer InitialChainSelected
192194

193195
let chain = VF.validatedFragment chainAndLedger

0 commit comments

Comments
 (0)