Skip to content

Commit 6c7098f

Browse files
committed
ChainDB.Background: avoid hardcoding immutability criterion
1 parent 8521045 commit 6c7098f

File tree

1 file changed

+21
-20
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl

1 file changed

+21
-20
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Background
4141
, addBlockRunner
4242
) where
4343

44-
import Cardano.Ledger.BaseTypes (unNonZero)
4544
import Control.Exception (assert)
4645
import Control.Monad (forM_, forever, void)
4746
import Control.Monad.Trans.Class (lift)
@@ -57,7 +56,6 @@ import Data.Word
5756
import GHC.Generics (Generic)
5857
import GHC.Stack (HasCallStack)
5958
import Ouroboros.Consensus.Block
60-
import Ouroboros.Consensus.Config
6159
import Ouroboros.Consensus.HardFork.Abstract
6260
import Ouroboros.Consensus.Ledger.Inspect
6361
import Ouroboros.Consensus.Ledger.SupportsProtocol
@@ -69,6 +67,7 @@ import Ouroboros.Consensus.Storage.ChainDB.API
6967
import Ouroboros.Consensus.Storage.ChainDB.Impl.ChainSel
7068
( chainSelSync
7169
)
70+
import qualified Ouroboros.Consensus.Storage.ChainDB.Impl.Query as Query
7271
import Ouroboros.Consensus.Storage.ChainDB.Impl.Types
7372
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
7473
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
@@ -132,10 +131,11 @@ launchBgTasks cdb@CDB{..} replayed = do
132131
Copying blocks from the VolatileDB to the ImmutableDB
133132
-------------------------------------------------------------------------------}
134133

135-
-- | Copy the blocks older than @k@ from the VolatileDB to the ImmutableDB.
134+
-- | Copy the blocks older than the immutable tip from the VolatileDB to the
135+
-- ImmutableDB.
136136
--
137-
-- These headers of these blocks can be retrieved by dropping the @k@ most
138-
-- recent blocks from the fragment stored in 'cdbChain'.
137+
-- These headers of these blocks can be retrieved by considering headers in
138+
-- 'cdbChain' that are not also in 'getCurrentChain' (a suffix of 'cdbChain').
139139
--
140140
-- The copied blocks are removed from the fragment stored in 'cdbChain'.
141141
--
@@ -153,10 +153,11 @@ copyToImmutableDB ::
153153
) =>
154154
ChainDbEnv m blk ->
155155
Electric m (WithOrigin SlotNo)
156-
copyToImmutableDB CDB{..} = electric $ do
156+
copyToImmutableDB cdb@CDB{..} = electric $ do
157157
toCopy <- atomically $ do
158158
curChain <- icWithoutTime <$> readTVar cdbChain
159-
let nbToCopy = max 0 (AF.length curChain - fromIntegral (unNonZero k))
159+
curChainVolSuffix <- Query.getCurrentChain cdb
160+
let nbToCopy = max 0 $ AF.length curChain - AF.length curChainVolSuffix
160161
toCopy :: [Point blk]
161162
toCopy =
162163
map headerPoint $
@@ -165,10 +166,10 @@ copyToImmutableDB CDB{..} = electric $ do
165166
return toCopy
166167

167168
if null toCopy
168-
-- This can't happen in practice, as we're only called when the fragment
169-
-- is longer than @k@. However, in the tests, we will be calling this
170-
-- function manually, which means it might be called when there are no
171-
-- blocks to copy.
169+
-- This can't happen in practice, as we're only called when there are new
170+
-- immutable blocks. However, in the tests, we will be calling this function
171+
-- manually, which means it might be called when there are no blocks to
172+
-- copy.
172173
then trace NoBlocksToCopyToImmutableDB
173174
else forM_ toCopy $ \pt -> do
174175
let hash = case pointHash pt of
@@ -193,7 +194,6 @@ copyToImmutableDB CDB{..} = electric $ do
193194
-- Get the /possibly/ updated tip of the ImmutableDB
194195
atomically $ ImmutableDB.getTipSlot cdbImmutableDB
195196
where
196-
SecurityParam k = configSecurityParam cdbTopLevelConfig
197197
trace = traceWith (contramap TraceCopyToImmutableDBEvent cdbTracer)
198198

199199
-- \| Remove the header corresponding to the given point from the beginning
@@ -218,9 +218,10 @@ copyToImmutableDB CDB{..} = electric $ do
218218
-- | Copy blocks from the VolatileDB to ImmutableDB and trigger further tasks in
219219
-- other threads.
220220
--
221-
-- We watch the chain for changes. Whenever the chain is longer than @k@, then
222-
-- the headers older than @k@ are copied from the VolatileDB to the ImmutableDB
223-
-- (using 'copyToImmutableDB'). Once that is complete,
221+
-- We watch the 'cdbChain' for changes. Whenever the chain is longer than
222+
-- 'getCurrentChain', then the headers of of the former that are not on the
223+
-- latter are copied from the VolatileDB to the ImmutableDB (using
224+
-- 'copyToImmutableDB'). Once that is complete,
224225
--
225226
-- * Trigger LedgerDB maintenance tasks, namely flushing, taking snapshots and
226227
-- garbage collection.
@@ -254,15 +255,15 @@ copyToImmutableDBRunner cdb@CDB{..} ledgerDbTasksTrigger gcSchedule fuse = do
254255
LedgerDB.tryFlush cdbLedgerDB
255256
forever copyAndTrigger
256257
where
257-
SecurityParam k = configSecurityParam cdbTopLevelConfig
258-
259258
copyAndTrigger :: m ()
260259
copyAndTrigger = do
261-
-- Wait for the chain to grow larger than @k@
260+
-- Wait 'cdbChain' to become longer than 'getCurrentChain'.
262261
numToWrite <- atomically $ do
263262
curChain <- icWithoutTime <$> readTVar cdbChain
264-
check $ fromIntegral (AF.length curChain) > unNonZero k
265-
return $ fromIntegral (AF.length curChain) - unNonZero k
263+
curChainVolSuffix <- Query.getCurrentChain cdb
264+
let numToWrite = AF.length curChain - AF.length curChainVolSuffix
265+
check $ numToWrite > 0
266+
return $ fromIntegral numToWrite
266267

267268
-- Copy blocks to ImmutableDB
268269
--

0 commit comments

Comments
 (0)