|
| 1 | +{-# LANGUAGE GADTs #-} |
| 2 | +{-# LANGUAGE TypeApplications #-} |
| 3 | +{-# LANGUAGE ViewPatterns #-} |
| 4 | + |
| 5 | +module Cardano.Snapshots.Run ( |
| 6 | + canonicalizeSnapshots, |
| 7 | + NodeDatabasePaths, |
| 8 | +) where |
| 9 | + |
| 10 | +import qualified Cardano.Api.Consensus as Api |
| 11 | +import Cardano.Node.Configuration.LedgerDB |
| 12 | +import Cardano.Node.Configuration.POM |
| 13 | +import Cardano.Node.Protocol |
| 14 | +import Cardano.Node.Types (ConfigYamlFilePath (..)) |
| 15 | +import Control.Exception |
| 16 | +import Control.Monad (forM_) |
| 17 | +import Control.Monad.Except |
| 18 | +import Data.Monoid (Last (..)) |
| 19 | +import Ouroboros.Consensus.Cardano.SnapshotConversion |
| 20 | +import Ouroboros.Consensus.Node (NodeDatabasePaths (..), immutableDbPath) |
| 21 | +import System.Directory (doesFileExist, listDirectory) |
| 22 | +import System.FilePath ((</>)) |
| 23 | + |
| 24 | +canonicalizeSnapshots :: FilePath -> Maybe NodeDatabasePaths -> IO () |
| 25 | +canonicalizeSnapshots cfg (Last -> db) = do |
| 26 | + configYamlPc <- parseNodeConfigurationFP $ Just $ ConfigYamlFilePath cfg |
| 27 | + |
| 28 | + let cfgFromFile = defaultPartialNodeConfiguration <> configYamlPc |
| 29 | + |
| 30 | + mOut = getLast (pncCanonicalSnapshotOutputPath cfgFromFile) |
| 31 | + |
| 32 | + mOtherConfigs = do |
| 33 | + a <- getLast (pncDatabaseFile cfgFromFile <> db) |
| 34 | + b <- getLast (pncLedgerDbConfig cfgFromFile) |
| 35 | + c <- getLast (pncProtocolConfig cfgFromFile) |
| 36 | + d <- getLast (pncProtocolFiles cfgFromFile) |
| 37 | + pure (a, b, c, d) |
| 38 | + |
| 39 | + case (mOut, mOtherConfigs) of |
| 40 | + (Nothing, _) -> pure () |
| 41 | + (_, Nothing) -> error "Impossible, some arguments were missing yet there should be at least a default value for those" |
| 42 | + (Just out, Just (immutableDbPath -> dbPath, LedgerDbConfiguration _ _ _ selector _, pInfo, cfgFiles)) -> do |
| 43 | + snaps <- listDirectory (dbPath </> "ledger") |
| 44 | + someConsensusProto <- |
| 45 | + runThrowExceptT $ |
| 46 | + mkConsensusProtocol |
| 47 | + pInfo |
| 48 | + (Just cfgFiles) |
| 49 | + case someConsensusProto of |
| 50 | + SomeConsensusProtocol Api.CardanoBlockType pInfoArgs -> do |
| 51 | + let inFmt = case selector of |
| 52 | + V1LMDB{} -> LMDB |
| 53 | + V2InMemory{} -> Mem |
| 54 | + V2LSM Nothing -> flip LSM (dbPath </> "lsm") |
| 55 | + V2LSM (Just lsmDb) -> flip LSM (dbPath </> lsmDb) |
| 56 | + forM_ snaps $ \snap -> do |
| 57 | + exists <- doesFileExist (out </> snap </> "meta") |
| 58 | + if exists |
| 59 | + then putStrLn $ "Snapshot at " <> dbPath </> "ledger" </> snap <> " already converted" |
| 60 | + else do |
| 61 | + putStrLn $ "Converting snapshot at " <> dbPath </> "ledger" </> snap |
| 62 | + runThrowExceptT $ convertSnapshot False (fst $ Api.protocolInfo @IO pInfoArgs) (inFmt (dbPath </> "ledger" </> snap)) (Mem $ out </> snap) |
| 63 | + putStrLn "Done" |
| 64 | + _ -> pure () |
| 65 | + |
| 66 | +runThrowExceptT :: (Exception e) => ExceptT e IO a -> IO a |
| 67 | +runThrowExceptT act = runExceptT act >>= either throwIO pure |
0 commit comments