Skip to content

Commit 6ac1c9e

Browse files
authored
ChainDB: minor refactorings/simplifications (#1543)
The commits are intended to be reviewed individually, see their commit messages. No semantic changes.
2 parents a80a6c9 + eec76c1 commit 6ac1c9e

File tree

4 files changed

+44
-58
lines changed

4 files changed

+44
-58
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Breaking
2+
3+
- ChainDB internals: changed type of `FollowerHandle.fhSwitchFork`.

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ import Ouroboros.Network.AnchoredFragment
102102
, AnchoredSeq (..)
103103
)
104104
import qualified Ouroboros.Network.AnchoredFragment as AF
105-
import qualified Ouroboros.Network.AnchoredSeq as AS
106105
import Ouroboros.Network.Protocol.LocalStateQuery.Type (Target (..))
107106

108107
-- | Perform the initial chain selection based on the tip of the ImmutableDB
@@ -947,12 +946,11 @@ chainSelectionForBlock cdb@CDB{..} blockCache hdr punish = electric $ withRegist
947946
AddingBlocks -> pure ()
948947
SwitchingToAFork -> do
949948
-- Update the followers
950-
--
951-
-- 'Follower.switchFork' needs to know the intersection point
952-
-- (@ipoint@) between the old and the current chain.
953-
let ipoint = castPoint $ Diff.getAnchorPoint chainDiff
954949
followerHandles <- Map.elems <$> readTVar cdbFollowers
955-
forM_ followerHandles $ switchFollowerToFork curChain newChain ipoint
950+
-- The suffix of @curChain@ that we are going to orphan by
951+
-- adopting @chainDiff@.
952+
let oldSuffix = AF.anchorNewest (getRollback chainDiff) curChain
953+
forM_ followerHandles $ \hdl -> fhSwitchFork hdl oldSuffix
956954

957955
return (curChain, newChain, events, prevTentativeHeader, newLedger)
958956
let mkTraceEvent = case chainSwitchType of
@@ -967,18 +965,6 @@ chainSelectionForBlock cdb@CDB{..} blockCache hdr punish = electric $ withRegist
967965

968966
forkerClose newForker
969967
where
970-
-- Given the current chain and the new chain as chain fragments, and the
971-
-- intersection point (an optimization, since it has already been
972-
-- computed when calling this function), returns a function that updates
973-
-- the state of a follower via its handle.
974-
switchFollowerToFork curChain newChain ipoint =
975-
let oldPoints =
976-
Set.fromList . fmap headerPoint . AS.toOldestFirst $
977-
Diff.getSuffix $
978-
Diff.diff newChain curChain
979-
in assert (AF.withinFragmentBounds (castPoint ipoint) newChain) $
980-
\followerHandle -> fhSwitchFork followerHandle ipoint oldPoints
981-
982968
ValidatedChainDiff chainDiff newForker = vChainDiff
983969

984970
-- \| We have a new block @b@ that doesn't fit onto the current chain, but
@@ -1174,9 +1160,8 @@ chainSelection chainSelEnv rr chainDiffs =
11741160
writeTVar varTentativeHeader SNothing
11751161
writeTVar varTentativeState tentativeSt
11761162
forTentativeFollowers $ \followerHandle -> do
1177-
let curTipPoint = castPoint $ AF.headPoint curChain
1178-
oldPoints = Set.singleton $ headerPoint tentativeHeader
1179-
fhSwitchFork followerHandle curTipPoint oldPoints
1163+
let oldSuffix = AF.Empty (AF.headAnchor curChain) AF.:> tentativeHeader
1164+
fhSwitchFork followerHandle oldSuffix
11801165
traceWith pipeliningTracer $ TrapTentativeHeader tentativeHeader
11811166
where
11821167
forTentativeFollowers f = getTentativeFollowers >>= mapM_ f

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

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import Data.Functor ((<&>))
2525
import Data.Functor.Identity (Identity (..))
2626
import qualified Data.Map.Strict as Map
2727
import Data.Maybe.Strict (StrictMaybe (..))
28-
import Data.Set (Set)
29-
import qualified Data.Set as Set
3028
import Ouroboros.Consensus.Block
3129
import Ouroboros.Consensus.Config
3230
import Ouroboros.Consensus.Storage.ChainDB.API
@@ -126,9 +124,8 @@ newFollower h registry chainType blockComponent = getEnv h $ \CDB{..} -> do
126124
-- 'closeAllFollowers' will empty that map already.
127125
followerState <- atomically $ readTVar varFollower
128126
closeFollowerState followerState
129-
, fhSwitchFork = \ipoint oldPoints ->
130-
modifyTVar varFollower $
131-
switchFork ipoint oldPoints
127+
, fhSwitchFork = \oldSuffix ->
128+
modifyTVar varFollower $ switchFork oldSuffix
132129
}
133130

134131
makeNewFollower ::
@@ -505,29 +502,27 @@ forward registry varFollower blockComponent CDB{..} =
505502
writeTVar varFollower $
506503
FollowerInImmutableDB (RollBackTo pt) immIt
507504
return True
508-
_otherwise -> case pointToWithOriginRealPoint pt of
505+
_otherwise -> case pt of
509506
-- Genesis is always "in" the ImmutableDB
510-
Origin -> do
507+
GenesisPoint -> do
511508
join $ atomically $ updateState FollowerInit
512509
return True
513-
NotOrigin pt' -> do
514-
inImmutableDB <- ImmutableDB.hasBlock cdbImmutableDB pt'
515-
if inImmutableDB
516-
then do
517-
immIt <-
518-
ImmutableDB.streamAfterKnownPoint
519-
cdbImmutableDB
520-
registry
521-
((,) <$> getPoint <*> blockComponent)
522-
pt
523-
join $
524-
atomically $
525-
updateState $
526-
FollowerInImmutableDB (RollBackTo pt) immIt
527-
return True
528-
else
529-
-- The point is not in the current chain
530-
return False
510+
BlockPoint{} ->
511+
ImmutableDB.streamAfterPoint
512+
cdbImmutableDB
513+
registry
514+
((,) <$> getPoint <*> blockComponent)
515+
pt
516+
>>= \case
517+
Right immIt -> do
518+
join $
519+
atomically $
520+
updateState $
521+
FollowerInImmutableDB (RollBackTo pt) immIt
522+
return True
523+
Left _ ->
524+
-- The point is not in the current chain
525+
return False
531526

532527
-- \| Update the state of the follower to the given state. If the current
533528
-- state is 'FollowerInImmutableDB', close the ImmutableDB iterator to avoid
@@ -547,25 +542,26 @@ forward registry varFollower blockComponent CDB{..} =
547542
-- intersection point if it is.
548543
switchFork ::
549544
forall m blk b.
550-
HasHeader blk =>
551-
-- | Intersection point between the new and the old chain.
552-
Point blk ->
553-
-- | Set of points that are in the old chain and not in the
554-
-- new chain.
555-
Set (Point blk) ->
545+
GetHeader blk =>
546+
-- | Suffix of the old chain anchored at the intersection with the new chain.
547+
AnchoredFragment (Header blk) ->
556548
FollowerState m blk b ->
557549
FollowerState m blk b
558-
switchFork ipoint oldPoints =
550+
switchFork oldSuffix =
559551
\case
560552
-- Roll back to the intersection point if and only if the position of the
561553
-- follower is not in the new chain, but was part of the volatile DB. By the
562554
-- invariant that the follower state is always in the current chain, it then
563-
-- should be in `oldPoints`.
555+
-- should be on `oldSuffix` (ignoring the anchor).
564556
FollowerInMem (RollBackTo pt)
565-
| pt `Set.member` oldPoints -> FollowerInMem (RollBackTo ipoint)
557+
| isOnOldSuffix pt -> FollowerInMem (RollBackTo ipoint)
566558
FollowerInMem (RollForwardFrom pt)
567-
| pt `Set.member` oldPoints -> FollowerInMem (RollBackTo ipoint)
559+
| isOnOldSuffix pt -> FollowerInMem (RollBackTo ipoint)
568560
followerState -> followerState
561+
where
562+
ipoint = castPoint $ AF.anchorPoint oldSuffix
563+
564+
isOnOldSuffix pt = AF.pointOnFragment (castPoint pt) oldSuffix
569565

570566
-- | Close all open block and header 'Follower's.
571567
closeAllFollowers ::

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ import Data.Maybe (mapMaybe)
8787
import Data.Maybe.Strict (StrictMaybe (..))
8888
import Data.MultiSet (MultiSet)
8989
import qualified Data.MultiSet as MultiSet
90-
import Data.Set (Set)
9190
import Data.Typeable
9291
import Data.Void (Void)
9392
import Data.Word (Word64)
@@ -425,8 +424,11 @@ newtype FollowerKey = FollowerKey Word
425424
data FollowerHandle m blk = FollowerHandle
426425
{ fhChainType :: ChainType
427426
-- ^ Whether we follow the tentative chain.
428-
, fhSwitchFork :: Point blk -> Set (Point blk) -> STM m ()
427+
, fhSwitchFork :: AnchoredFragment (Header blk) -> STM m ()
429428
-- ^ When we have switched to a fork, all open 'Follower's must be notified.
429+
--
430+
-- Receives the suffix of the old chain anchored at the intersection with the
431+
-- new chain.
430432
, fhClose :: m ()
431433
-- ^ When closing the ChainDB, we must also close all open 'Follower's, as
432434
-- they might be holding on to resources.

0 commit comments

Comments
 (0)