@@ -41,7 +41,6 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Background
41
41
, addBlockRunner
42
42
) where
43
43
44
- import Cardano.Ledger.BaseTypes (unNonZero )
45
44
import Control.Exception (assert )
46
45
import Control.Monad (forM_ , forever , void )
47
46
import Control.Monad.Trans.Class (lift )
@@ -57,7 +56,6 @@ import Data.Word
57
56
import GHC.Generics (Generic )
58
57
import GHC.Stack (HasCallStack )
59
58
import Ouroboros.Consensus.Block
60
- import Ouroboros.Consensus.Config
61
59
import Ouroboros.Consensus.HardFork.Abstract
62
60
import Ouroboros.Consensus.Ledger.Inspect
63
61
import Ouroboros.Consensus.Ledger.SupportsProtocol
@@ -69,6 +67,7 @@ import Ouroboros.Consensus.Storage.ChainDB.API
69
67
import Ouroboros.Consensus.Storage.ChainDB.Impl.ChainSel
70
68
( chainSelSync
71
69
)
70
+ import qualified Ouroboros.Consensus.Storage.ChainDB.Impl.Query as Query
72
71
import Ouroboros.Consensus.Storage.ChainDB.Impl.Types
73
72
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
74
73
import qualified Ouroboros.Consensus.Storage.LedgerDB as LedgerDB
@@ -132,10 +131,11 @@ launchBgTasks cdb@CDB{..} replayed = do
132
131
Copying blocks from the VolatileDB to the ImmutableDB
133
132
-------------------------------------------------------------------------------}
134
133
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.
136
136
--
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') .
139
139
--
140
140
-- The copied blocks are removed from the fragment stored in 'cdbChain'.
141
141
--
@@ -153,10 +153,11 @@ copyToImmutableDB ::
153
153
) =>
154
154
ChainDbEnv m blk ->
155
155
Electric m (WithOrigin SlotNo )
156
- copyToImmutableDB CDB {.. } = electric $ do
156
+ copyToImmutableDB cdb @ CDB {.. } = electric $ do
157
157
toCopy <- atomically $ do
158
158
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
160
161
toCopy :: [Point blk ]
161
162
toCopy =
162
163
map headerPoint $
@@ -165,10 +166,10 @@ copyToImmutableDB CDB{..} = electric $ do
165
166
return toCopy
166
167
167
168
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.
172
173
then trace NoBlocksToCopyToImmutableDB
173
174
else forM_ toCopy $ \ pt -> do
174
175
let hash = case pointHash pt of
@@ -193,7 +194,6 @@ copyToImmutableDB CDB{..} = electric $ do
193
194
-- Get the /possibly/ updated tip of the ImmutableDB
194
195
atomically $ ImmutableDB. getTipSlot cdbImmutableDB
195
196
where
196
- SecurityParam k = configSecurityParam cdbTopLevelConfig
197
197
trace = traceWith (contramap TraceCopyToImmutableDBEvent cdbTracer)
198
198
199
199
-- \| Remove the header corresponding to the given point from the beginning
@@ -218,9 +218,10 @@ copyToImmutableDB CDB{..} = electric $ do
218
218
-- | Copy blocks from the VolatileDB to ImmutableDB and trigger further tasks in
219
219
-- other threads.
220
220
--
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,
224
225
--
225
226
-- * Trigger LedgerDB maintenance tasks, namely flushing, taking snapshots and
226
227
-- garbage collection.
@@ -254,15 +255,15 @@ copyToImmutableDBRunner cdb@CDB{..} ledgerDbTasksTrigger gcSchedule fuse = do
254
255
LedgerDB. tryFlush cdbLedgerDB
255
256
forever copyAndTrigger
256
257
where
257
- SecurityParam k = configSecurityParam cdbTopLevelConfig
258
-
259
258
copyAndTrigger :: m ()
260
259
copyAndTrigger = do
261
- -- Wait for the chain to grow larger than @k@
260
+ -- Wait 'cdbChain' to become longer than 'getCurrentChain'.
262
261
numToWrite <- atomically $ do
263
262
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
266
267
267
268
-- Copy blocks to ImmutableDB
268
269
--
0 commit comments