Skip to content

Commit b433ed7

Browse files
committed
Add versioning to ledger tables
1 parent 0469405 commit b433ed7

File tree

7 files changed

+47
-1
lines changed

7 files changed

+47
-1
lines changed

ouroboros-consensus-cardano/app/snapshot-converter.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ main = withStdTerminalHandles $ do
287287
let crcOut = maybe inCRC (crcOfConcat inCRC) mCRCOut
288288

289289
lift $ putStr "Generating new metadata file..." >> hFlush stdout
290-
putMetadata outFilePath (SnapshotMetadata outBackend crcOut)
290+
putMetadata outFilePath (SnapshotMetadata outBackend crcOut TablesCodecVersion1)
291291

292292
lift $ putColored Green True "Done"
293293

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Patch
10+
11+
- A bullet item for the Patch category.
12+
13+
-->
14+
<!--
15+
### Non-Breaking
16+
17+
- A bullet item for the Non-Breaking category.
18+
19+
-->
20+
21+
### Breaking
22+
23+
- Version the ledger tables in snapshots. For now only the 'TablesCodecVersion1' exists. Snapshots without a version will be regarded as invalid, thus triggering a replay of the chain.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/Snapshots.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.Snapshots
2828
, SnapshotFailure (..)
2929
, SnapshotMetadata (..)
3030
, SnapshotPolicyArgs (..)
31+
, TablesCodecVersion (..)
3132
, defaultSnapshotPolicyArgs
3233

3334
-- * Codec
@@ -82,6 +83,7 @@ import Control.Monad.Except
8283
import Control.Tracer
8384
import Data.Aeson (FromJSON (..), ToJSON (..), (.:), (.=))
8485
import qualified Data.Aeson as Aeson
86+
import Data.Aeson.Types (Parser)
8587
import Data.Functor.Identity
8688
import qualified Data.List as List
8789
import Data.Maybe (isJust, mapMaybe)
@@ -162,9 +164,24 @@ data ReadSnapshotErr
162164
ReadMetadataError FsPath MetadataErr
163165
deriving (Eq, Show)
164166

167+
data TablesCodecVersion = TablesCodecVersion1
168+
deriving (Eq, Show)
169+
170+
instance ToJSON TablesCodecVersion where
171+
toJSON TablesCodecVersion1 = Aeson.Number 1
172+
173+
instance FromJSON TablesCodecVersion where
174+
parseJSON v = enforceVersion =<< parseJSON v
175+
176+
enforceVersion :: Word8 -> Parser TablesCodecVersion
177+
enforceVersion v = case v of
178+
1 -> pure TablesCodecVersion1
179+
_ -> fail "Unknown or outdated tables codec version"
180+
165181
data SnapshotMetadata = SnapshotMetadata
166182
{ snapshotBackend :: SnapshotBackend
167183
, snapshotChecksum :: CRC
184+
, snapshotTablesCodecVersion :: TablesCodecVersion
168185
}
169186
deriving (Eq, Show)
170187

@@ -173,13 +190,15 @@ instance ToJSON SnapshotMetadata where
173190
Aeson.object
174191
[ "backend" .= snapshotBackend sm
175192
, "checksum" .= getCRC (snapshotChecksum sm)
193+
, "tablesCodecVersion" .= toJSON (snapshotTablesCodecVersion sm)
176194
]
177195

178196
instance FromJSON SnapshotMetadata where
179197
parseJSON = Aeson.withObject "SnapshotMetadata" $ \o ->
180198
SnapshotMetadata
181199
<$> o .: "backend"
182200
<*> fmap CRC (o .: "checksum")
201+
<*> (parseJSON =<< (o .: "tablesCodecVersion"))
183202

184203
data SnapshotBackend
185204
= UTxOHDMemSnapshot

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V1/Snapshots.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ writeSnapshot fs@(SomeHasFS hasFS) backingStore encLedger snapshot cs = do
260260
SnapshotMetadata
261261
{ snapshotBackend = bsSnapshotBackend backingStore
262262
, snapshotChecksum = crc
263+
, snapshotTablesCodecVersion = TablesCodecVersion1
263264
}
264265
bsCopy
265266
backingStore

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/InMemory.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ writeSnapshot fs@(SomeHasFS hasFs) encLedger ds st = do
204204
SnapshotMetadata
205205
{ snapshotBackend = UTxOHDMemSnapshot
206206
, snapshotChecksum = maybe crc1 (crcOfConcat crc1) crc2
207+
, snapshotTablesCodecVersion = TablesCodecVersion1
207208
}
208209

209210
implTakeSnapshot ::

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/LSM.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ writeSnapshot fs@(SomeHasFS hasFs) encLedger ds st = do
418418
SnapshotMetadata
419419
{ snapshotBackend = UTxOHDLSMSnapshot
420420
, snapshotChecksum = maybe crc1 (crcOfConcat crc1) crc2
421+
, snapshotTablesCodecVersion = TablesCodecVersion1
421422
}
422423

423424
-- | Delete snapshot from disk and also from the LSM tree database.

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/Snapshots.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ instance Arbitrary SnapshotMetadata where
3535
SnapshotMetadata
3636
<$> arbitrary
3737
<*> fmap CRC arbitrary
38+
<*> pure TablesCodecVersion1

0 commit comments

Comments
 (0)