Skip to content

Commit 8e9fbae

Browse files
committed
cardano-node: ground ledge db snapshot configuration options
1 parent 0d2b3a9 commit 8e9fbae

7 files changed

Lines changed: 95 additions & 38 deletions

File tree

cardano-node/src/Cardano/Node/Configuration/POM.hs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -545,21 +545,37 @@ instance FromJSON PartialNodeConfiguration where
545545
spArgs = SnapshotPolicyArgs (SnapshotFrequency sf) sn
546546
return $ Just $ LedgerDbConfiguration spArgs DefaultQueryBatchSize V2InMemory deprecatedOpts
547547
Just ledgerDB -> flip (withObject "LedgerDB") ledgerDB $ \o -> do
548-
ldbSnapInterval <- (getLast . (Last mTopLevelSnapInterval <>) . Last <$> snapInterval o) .!= UseDefault
549-
ldbSnapNum <- (getLast . (Last mTopLevelSnapNum <>) . Last <$> snapNum o) .!= UseDefault
550-
ldbSnapOffset <- (fmap Override <$> o .:? "SlotOffset") .!= UseDefault
551-
ldbSnapRateLimit<- (fmap (Override . secondsToDiffTime) <$> o .:? "RateLimit") .!= UseDefault
552-
ldbSnapMinDelay <- o .:? "MinDelay"
553-
ldbSnapMaxDelay <- o .:? "MaxDelay"
554-
ldbSnapDelayRange <-
555-
case (ldbSnapMinDelay, ldbSnapMaxDelay) of
556-
(Just minDelay, Just maxDelay) ->
557-
if minDelay <= maxDelay then
558-
pure (Override (SnapshotDelayRange (secondsToDiffTime minDelay) (secondsToDiffTime maxDelay)))
559-
else fail $ "Invalid ledger snapshot delay range, MinDelay > MaxDelay: "
560-
<> show minDelay <> " > " <> show maxDelay
561-
-- use the default delay range if either min or max is unspecified
562-
_ -> pure UseDefault
548+
-- Parse snapshot options from the "Snapshots" sub-object if present,
549+
-- otherwise fall back to the LedgerDB object for backward compatibility.
550+
let parseSnapshotOpts s = do
551+
sInterval <- (getLast . (Last mTopLevelSnapInterval <>) . Last <$> snapInterval s) .!= UseDefault
552+
sNum <- (getLast . (Last mTopLevelSnapNum <>) . Last <$> snapNum s) .!= UseDefault
553+
sOffset <- (fmap Override <$> s .:? "SlotOffset") .!= UseDefault
554+
sRateLimit <- (fmap (Override . secondsToDiffTime) <$> s .:? "RateLimit") .!= UseDefault
555+
sMinDelay <- s .:? "MinDelay"
556+
sMaxDelay <- s .:? "MaxDelay"
557+
sDelayRange <-
558+
case (sMinDelay, sMaxDelay) of
559+
(Just minDelay, Just maxDelay) ->
560+
if minDelay <= maxDelay then
561+
pure (Override (SnapshotDelayRange (secondsToDiffTime minDelay) (secondsToDiffTime maxDelay)))
562+
else fail $ "Invalid ledger snapshot delay range, MinDelay > MaxDelay: "
563+
<> show minDelay <> " > " <> show maxDelay
564+
-- use the default delay range if either min or max is unspecified
565+
_ -> pure UseDefault
566+
let sf = SnapshotFrequencyArgs {
567+
sfaInterval = unsafeNonZero . unSlotNo <$> sInterval
568+
, sfaOffset = sOffset
569+
, sfaRateLimit = sRateLimit
570+
, sfaDelaySnapshotRange = sDelayRange
571+
}
572+
pure $ SnapshotPolicyArgs (SnapshotFrequency sf) sNum
573+
574+
mSnapshotsVal <- o .:? "Snapshots"
575+
spArgs <- case mSnapshotsVal of
576+
Nothing -> parseSnapshotOpts o
577+
Just sv -> flip (withObject "Snapshots") sv parseSnapshotOpts
578+
563579
qsize <- (fmap RequestedQueryBatchSize <$> o .:? "QueryBatchSize") .!= DefaultQueryBatchSize
564580
backend <- o .:? "Backend" .!= "V2InMemory"
565581
selector <- case backend of
@@ -574,13 +590,6 @@ instance FromJSON PartialNodeConfiguration where
574590
lsmPath :: Maybe FilePath <- o .:? "LSMDatabasePath"
575591
pure $ V2LSM lsmPath
576592
_ -> fail $ "Malformed LedgerDB Backend: " <> backend
577-
let sf = SnapshotFrequencyArgs {
578-
sfaInterval = unsafeNonZero . unSlotNo <$> ldbSnapInterval
579-
, sfaOffset = ldbSnapOffset
580-
, sfaRateLimit = ldbSnapRateLimit
581-
, sfaDelaySnapshotRange = ldbSnapDelayRange
582-
}
583-
spArgs = SnapshotPolicyArgs (SnapshotFrequency sf) ldbSnapNum
584593
pure $ Just $ LedgerDbConfiguration spArgs qsize selector deprecatedOpts
585594

586595
parseByronProtocol v = do

cardano-node/test/Test/Cardano/Node/POM.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE ScopedTypeVariables #-}
12
{-# LANGUAGE NamedFieldPuns #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
{-# LANGUAGE PatternSynonyms #-}
@@ -30,10 +31,12 @@ import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing (..))
3031
import Ouroboros.Network.TxSubmission.Inbound.V2.Types
3132

3233
import Data.Bifunctor (first)
34+
import qualified Data.ByteString.Lazy as LBS
3335
import Data.Monoid (Last (..))
3436
import Data.String
3537
import Data.Text (Text)
3638

39+
import Data.Aeson (eitherDecode)
3740
import Hedgehog (Property, discover, withTests, (===))
3841
import qualified Hedgehog
3942
import Hedgehog.Internal.Property (evalEither, failWith)
@@ -302,6 +305,42 @@ eExpectedConfig = do
302305
, ncTxSubmissionInitDelay = defaultTxSubmissionInitDelay
303306
}
304307

308+
-- | Test that the legacy flat LedgerDB snapshot config format (options directly
309+
-- under LedgerDB) parses identically to the new nested Snapshots format.
310+
--
311+
-- TODO: this test could be removed once the old format is deprecated.
312+
prop_legacySnapshotFormat_POM :: Property
313+
prop_legacySnapshotFormat_POM =
314+
withTests 1 . Hedgehog.property $ do
315+
let legacyJson = "{ " <> dummyRequiredValues <> ", "
316+
<> "\"LedgerDB\": {"
317+
<> " \"Backend\": \"V2InMemory\","
318+
<> " \"SnapshotInterval\": 4320,"
319+
<> " \"NumOfDiskSnapshots\": 2"
320+
<> "} }"
321+
newJson = "{ " <> dummyRequiredValues <> ", "
322+
<> "\"LedgerDB\": {"
323+
<> " \"Backend\": \"V2InMemory\","
324+
<> " \"Snapshots\": {"
325+
<> " \"SnapshotInterval\": 4320,"
326+
<> " \"NumOfDiskSnapshots\": 2"
327+
<> " }"
328+
<> "} }"
329+
legacyConfig :: PartialNodeConfiguration <- evalEither $ eitherDecode legacyJson
330+
newConfig :: PartialNodeConfiguration <- evalEither $ eitherDecode newJson
331+
pncLedgerDbConfig legacyConfig === pncLedgerDbConfig newConfig
332+
where
333+
dummyRequiredValues :: LBS.ByteString
334+
dummyRequiredValues = mconcat
335+
[ "\"ByronGenesisFile\": \"x\""
336+
, ", \"ShelleyGenesisFile\": \"x\""
337+
, ", \"AlonzoGenesisFile\": \"x\""
338+
, ", \"ConwayGenesisFile\": \"x\""
339+
, ", \"LastKnownBlockVersion-Major\": 0"
340+
, ", \"LastKnownBlockVersion-Minor\": 0"
341+
, ", \"LastKnownBlockVersion-Alt\": 0"
342+
]
343+
305344
-- -----------------------------------------------------------------------------
306345

307346
tests :: IO Bool

configuration/cardano/mainnet-config-legacy.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
"LastKnownBlockVersion-Minor": 0,
1414
"LedgerDB": {
1515
"Backend": "V2InMemory",
16-
"NumOfDiskSnapshots": 2,
1716
"QueryBatchSize": 100000,
18-
"SnapshotInterval": 4320
17+
"Snapshots": {
18+
"NumOfDiskSnapshots": 2,
19+
"SnapshotInterval": 4320
20+
}
1921
},
2022
"MaxKnownMajorProtocolVersion": 2,
2123
"MinNodeVersion": "10.7.0",

configuration/cardano/mainnet-config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
"LastKnownBlockVersion-Minor": 0,
1414
"LedgerDB": {
1515
"Backend": "V2InMemory",
16-
"NumOfDiskSnapshots": 2,
1716
"QueryBatchSize": 100000,
18-
"SnapshotInterval": 4320
17+
"Snapshots": {
18+
"NumOfDiskSnapshots": 2,
19+
"SnapshotInterval": 4320
20+
}
1921
},
2022
"MaxKnownMajorProtocolVersion": 2,
2123
"MinNodeVersion": "10.7.0",

configuration/cardano/mainnet-config.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ ConsensusMode: PraosMode
8080
# Additional configuration options can be found at:
8181
# https://ouroboros-consensus.cardano.intersectmbo.org/docs/for-developers/utxo-hd/migrating
8282
LedgerDB:
83-
# The time interval between snapshots, in seconds.
84-
SnapshotInterval: 4320
85-
86-
# The number of disk snapshots to keep.
87-
NumOfDiskSnapshots: 2
83+
# The backend can either be in memory with `V2InMemory` or on disk with
84+
# `V1LMDB`.
85+
Backend: V2InMemory
8886

8987
# When querying the store for a big range of UTxOs (such as with
9088
# QueryUTxOByAddress), the store will be read in batches of this size.
9189
QueryBatchSize: 100000
9290

93-
# The backend can either be in memory with `V2InMemory` or on disk with
94-
# `V1LMDB`.
95-
Backend: V2InMemory
91+
Snapshots:
92+
# The time interval between snapshots, in seconds.
93+
SnapshotInterval: 4320
94+
95+
# The number of disk snapshots to keep.
96+
NumOfDiskSnapshots: 2
9697

9798
##### Version Information #####
9899

configuration/cardano/testnet-template-config-legacy.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"LastKnownBlockVersion-Minor": 1,
1313
"LedgerDB": {
1414
"Backend": "V2InMemory",
15-
"NumOfDiskSnapshots": 2,
1615
"QueryBatchSize": 100000,
17-
"SnapshotInterval": 216
16+
"Snapshots": {
17+
"NumOfDiskSnapshots": 2,
18+
"SnapshotInterval": 216
19+
}
1820
},
1921
"MaxConcurrencyDeadline": 4,
2022
"MaxKnownMajorProtocolVersion": 2,

configuration/cardano/testnet-template-config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
"LastKnownBlockVersion-Minor": 1,
1414
"LedgerDB": {
1515
"Backend": "V2InMemory",
16-
"NumOfDiskSnapshots": 2,
1716
"QueryBatchSize": 100000,
18-
"SnapshotInterval": 216
17+
"Snapshots": {
18+
"NumOfDiskSnapshots": 2,
19+
"SnapshotInterval": 216
20+
}
1921
},
2022
"MaxConcurrencyDeadline": 4,
2123
"MaxKnownMajorProtocolVersion": 2,

0 commit comments

Comments
 (0)