diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Plutus.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Plutus.hs index 290bbd0a5..c066fc90c 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Plutus.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Plutus.hs @@ -110,7 +110,7 @@ simpleScript = , V.txOutValue txout , V.txOutDataHash txout ) - Nothing -> error "AlonzosimpleScript: expected an address" + Nothing -> error "AlonzoSimpleScript: expected an address" expectedFields = ( renderAddress alwaysSucceedsScriptAddr , True diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/MigrateConsumedPruneTxOut.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/MigrateConsumedPruneTxOut.hs index 1bcf65e97..1bae7c3aa 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/MigrateConsumedPruneTxOut.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/MigrateConsumedPruneTxOut.hs @@ -112,7 +112,7 @@ basicPrune = do pruneWithSimpleRollback :: IOManager -> [(Text, Text)] -> Assertion pruneWithSimpleRollback = - withCustomConfig cmdLineArgs Nothing conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do + withCustomConfigAndLogs cmdLineArgs Nothing conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do let txOutTableType = txOutTableTypeFromConfig dbSync -- Forge some blocks blk0 <- forgeNext interpreter mockBlock0 diff --git a/cardano-db-sync/src/Cardano/DbSync/Api.hs b/cardano-db-sync/src/Cardano/DbSync/Api.hs index ea3fbb677..6a13a3a5e 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Api.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Api.hs @@ -168,6 +168,7 @@ initPruneConsumeMigration :: Bool -> Bool -> Bool -> Bool -> DB.PruneConsumeMigr initPruneConsumeMigration consumed pruneTxOut bootstrap forceTxIn' = DB.PruneConsumeMigration { DB.pcmPruneTxOut = pruneTxOut || bootstrap + , DB.pcmConsumedTxOut = consumed , DB.pcmConsumeOrPruneTxOut = consumed || pruneTxOut || bootstrap , DB.pcmSkipTxIn = not forceTxIn' && (consumed || pruneTxOut || bootstrap) } diff --git a/cardano-db-sync/src/Cardano/DbSync/Config/Types.hs b/cardano-db-sync/src/Cardano/DbSync/Config/Types.hs index 1c2c867bc..d55bfcaac 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Config/Types.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Config/Types.hs @@ -470,6 +470,7 @@ optionsToList SyncInsertOptions {..} = , toJsonIfSet "offchain_pool_data" sioOffchainPoolData , toJsonIfSet "pool_stats" sioPoolStats , toJsonIfSet "json_type" sioJsonType + , toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema ] toJsonIfSet :: ToJSON a => Text -> a -> Maybe Pair @@ -562,8 +563,8 @@ instance ToJSON TxOutConfig where instance FromJSON TxOutConfig where parseJSON = Aeson.withObject "tx_out" $ \obj -> do val <- obj .: "value" - useAddress' <- obj .: "use_address_table" .!= UseTxOutAddress False forceTxIn' <- obj .:? "force_tx_in" .!= ForceTxIn False + useAddress' <- obj .:? "use_address_table" .!= UseTxOutAddress False case val :: Text of "enable" -> pure (TxOutEnable useAddress') diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs index 1317b9604..51ad9952b 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs @@ -12,7 +12,7 @@ module Cardano.DbSync.Era.Shelley.Query ( ) where import Cardano.Db -import qualified Cardano.DbSync.Api as Db +import Cardano.DbSync.Api (getTxOutTableType) import Cardano.DbSync.Api.Types (SyncEnv) import qualified Cardano.DbSync.Era.Shelley.Generic as Generic import Cardano.DbSync.Util @@ -28,16 +28,16 @@ resolveStakeAddress addr = queryStakeAddress addr renderByteArray resolveInputTxOutId :: MonadIO m => SyncEnv -> Generic.TxIn -> ReaderT SqlBackend m (Either LookupFail (TxId, TxOutIdW)) resolveInputTxOutId syncEnv txIn = - queryTxOutId (Db.getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) + queryTxOutId (getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) resolveInputValue :: MonadIO m => SyncEnv -> Generic.TxIn -> ReaderT SqlBackend m (Either LookupFail (TxId, DbLovelace)) resolveInputValue syncEnv txIn = - queryTxOutValue (Db.getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) + queryTxOutValue (getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) resolveInputTxOutIdValue :: MonadIO m => SyncEnv -> Generic.TxIn -> ReaderT SqlBackend m (Either LookupFail (TxId, TxOutIdW, DbLovelace)) resolveInputTxOutIdValue syncEnv txIn = - queryTxOutIdValue (Db.getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) + queryTxOutIdValue (getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) queryResolveInputCredentials :: MonadIO m => SyncEnv -> Generic.TxIn -> ReaderT SqlBackend m (Either LookupFail (Maybe ByteString, Bool)) queryResolveInputCredentials syncEnv txIn = do - queryTxOutCredentials (Db.getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) + queryTxOutCredentials (getTxOutTableType syncEnv) (Generic.toTxHash txIn, fromIntegral (Generic.txInIndex txIn)) diff --git a/cardano-db-sync/src/Cardano/DbSync/Fix/ConsumedBy.hs b/cardano-db-sync/src/Cardano/DbSync/Fix/ConsumedBy.hs index dc8f2f15d..e340706e5 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Fix/ConsumedBy.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Fix/ConsumedBy.hs @@ -7,6 +7,8 @@ import qualified Cardano.Chain.Block as Byron hiding (blockHash) import qualified Cardano.Chain.UTxO as Byron import qualified Cardano.Crypto as Crypto (serializeCborHash) import qualified Cardano.Db as DB +import Cardano.DbSync.Api (getTrace, getTxOutTableType) +import Cardano.DbSync.Api.Types (SyncEnv) import Cardano.DbSync.Era.Byron.Insert import Cardano.DbSync.Era.Byron.Util (blockPayload, unTxHash) import Cardano.DbSync.Era.Util @@ -16,8 +18,6 @@ import Cardano.Prelude hiding (length, (.)) import Database.Persist.SqlBackend.Internal import Ouroboros.Consensus.Byron.Ledger (ByronBlock (..)) import Ouroboros.Consensus.Cardano.Block (HardForkBlock (..)) -import Cardano.DbSync.Api.Types (SyncEnv) -import Cardano.DbSync.Api (getTxOutTableType, getTrace) type FixEntry = (DB.TxOutIdW, DB.TxId) diff --git a/cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs b/cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs index 75b25e740..6bc15bea1 100644 --- a/cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs +++ b/cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs @@ -32,7 +32,6 @@ tests = prop_syncInsertConfigFromJSON :: Property prop_syncInsertConfigFromJSON = property $ do json <- forAll genDefaultJson - Aeson.fromJSON json === Aeson.Success (def :: SyncInsertConfig) prop_syncInsertConfigRoundtrip :: Property @@ -118,7 +117,9 @@ genDefaultJson = [ [aesonQQ| { "tx_out": { - "value": "enable" + "value": "enable", + "use_address_table": false, + "force_tx_in": false }, "ledger": "enable", "shelley": { @@ -148,7 +149,8 @@ genDefaultJson = , [aesonQQ| { "tx_out": { - "value": "enable" + "value": "enable", + "use_address_table": false }, "ledger": "enable", "shelley": { diff --git a/cardano-db-tool/app/cardano-db-tool.hs b/cardano-db-tool/app/cardano-db-tool.hs index 285e15cfa..16e2a9bf3 100644 --- a/cardano-db-tool/app/cardano-db-tool.hs +++ b/cardano-db-tool/app/cardano-db-tool.hs @@ -64,7 +64,7 @@ runCommand cmd = void $ runMigrations pgConfig False mdir mldir Fix CmdTxOutMigration txOutTableType -> do - runWithConnectionNoLogging PGPassDefaultEnv $ migrateTxOut Nothing txOutTableType + runWithConnectionNoLogging PGPassDefaultEnv $ migrateTxOutTests txOutTableType CmdUtxoSetAtBlock blkid txOutAddressType -> utxoSetAtSlot txOutAddressType blkid CmdPrepareSnapshot pargs -> runPrepareSnapshot pargs CmdValidateDb txOutAddressType -> runDbValidation txOutAddressType diff --git a/cardano-db-tool/src/Cardano/DbTool/Validate/BlockTxs.hs b/cardano-db-tool/src/Cardano/DbTool/Validate/BlockTxs.hs index 104909274..e4e0a9849 100644 --- a/cardano-db-tool/src/Cardano/DbTool/Validate/BlockTxs.hs +++ b/cardano-db-tool/src/Cardano/DbTool/Validate/BlockTxs.hs @@ -79,7 +79,7 @@ validateBlockCount (blockNo, txCountExpected) = do then Right () else Left $ ValidateError blockNo txCountActual txCountExpected --- This queries by BlockNo, the one in Cardano.Db.Operations.Core.Query queries by BlockId. +-- This queries by BlockNo, the one in Cardano.Db.Operations.Query queries by BlockId. queryBlockTxCount :: MonadIO m => Word64 -> ReaderT SqlBackend m Word64 queryBlockTxCount blockNo = do res <- select $ do diff --git a/cardano-db/app/gen-schema-docs.hs b/cardano-db/app/gen-schema-docs.hs index b787e5661..a931ddf3d 100644 --- a/cardano-db/app/gen-schema-docs.hs +++ b/cardano-db/app/gen-schema-docs.hs @@ -60,7 +60,7 @@ docHeader branchName = , "** which may not accurately reflect the version number)" ] , "\n" - , "**Note:** This file is auto-generated from the documentation in cardano-db/src/Cardano/Db/Schema.hs\ + , "**Note:** This file is auto-generated from the documentation in cardano-db/src/Cardano/Db/Schema/BaseSchema.hs\ \ by the command `cabal run -- gen-schema-docs doc/schema.md`. This document should only be updated\ \ during the release process and updated on the release branch." , "\n" diff --git a/cardano-db/cardano-db.cabal b/cardano-db/cardano-db.cabal index 9b709d85b..2cad675a9 100644 --- a/cardano-db/cardano-db.cabal +++ b/cardano-db/cardano-db.cabal @@ -40,18 +40,18 @@ library Cardano.Db.Migration Cardano.Db.Migration.Haskell Cardano.Db.Migration.Version - Cardano.Db.Operations.Core.AlterTable - Cardano.Db.Operations.Core.Delete - Cardano.Db.Operations.Core.Insert - Cardano.Db.Operations.Core.MinId - Cardano.Db.Operations.Core.Query - Cardano.Db.Operations.Core.QueryHelper + Cardano.Db.Operations.AlterTable + Cardano.Db.Operations.Delete + Cardano.Db.Operations.Insert + Cardano.Db.Operations.Other.MinId + Cardano.Db.Operations.Query + Cardano.Db.Operations.QueryHelper Cardano.Db.Operations.Types - Cardano.Db.Operations.Variant.ConsumedTxOut - Cardano.Db.Operations.Variant.JsonbQuery - Cardano.Db.Operations.Variant.TxOutDelete - Cardano.Db.Operations.Variant.TxOutInsert - Cardano.Db.Operations.Variant.TxOutQuery + Cardano.Db.Operations.Other.ConsumedTxOut + Cardano.Db.Operations.Other.JsonbQuery + Cardano.Db.Operations.TxOut.TxOutDelete + Cardano.Db.Operations.TxOut.TxOutInsert + Cardano.Db.Operations.TxOut.TxOutQuery Cardano.Db.PGConfig Cardano.Db.Run Cardano.Db.Schema.BaseSchema diff --git a/cardano-db/src/Cardano/Db.hs b/cardano-db/src/Cardano/Db.hs index f3c241357..630df6f2a 100644 --- a/cardano-db/src/Cardano/Db.hs +++ b/cardano-db/src/Cardano/Db.hs @@ -6,34 +6,24 @@ module Cardano.Db ( Tx (..), TxIn (..), gitRev, - -- CTX.migrateTxOut, - -- CTX.runExtraMigrations, - -- CTX.queryTxConsumedColumnExists, - -- CTX.queryTxOutConsumedNullCount, - -- CTX.queryTxOutConsumedCount, - -- CTX.querySetNullTxOut, ) where import Cardano.Db.Error as X import Cardano.Db.Git.Version (gitRev) import Cardano.Db.Migration as X import Cardano.Db.Migration.Version as X -import Cardano.Db.Operations.Core.AlterTable as X -import Cardano.Db.Operations.Core.Delete as X -import Cardano.Db.Operations.Core.Insert as X -import Cardano.Db.Operations.Core.MinId as X -import Cardano.Db.Operations.Core.Query as X -import Cardano.Db.Operations.Core.QueryHelper as X +import Cardano.Db.Operations.AlterTable as X +import Cardano.Db.Operations.Delete as X +import Cardano.Db.Operations.Insert as X +import Cardano.Db.Operations.Other.ConsumedTxOut as X +import Cardano.Db.Operations.Other.JsonbQuery as X +import Cardano.Db.Operations.Other.MinId as X +import Cardano.Db.Operations.Query as X +import Cardano.Db.Operations.QueryHelper as X +import Cardano.Db.Operations.TxOut.TxOutDelete as X +import Cardano.Db.Operations.TxOut.TxOutInsert as X +import Cardano.Db.Operations.TxOut.TxOutQuery as X import Cardano.Db.Operations.Types as X - --- import qualified Cardano.Db.Operations.Variant.ConsumedTxOut as CTX -import Cardano.Db.Operations.Variant.ConsumedTxOut as X - --- (migrateTxOut, queryTxConsumedColumnExists, queryTxOutConsumedCount, queryTxOutConsumedNullCount, runExtraMigrations, querySetNullTxOut) -import Cardano.Db.Operations.Variant.JsonbQuery as X -import Cardano.Db.Operations.Variant.TxOutDelete as X -import Cardano.Db.Operations.Variant.TxOutInsert as X -import Cardano.Db.Operations.Variant.TxOutQuery as X import Cardano.Db.PGConfig as X import Cardano.Db.Run as X import Cardano.Db.Schema.BaseSchema as X diff --git a/cardano-db/src/Cardano/Db/Migration.hs b/cardano-db/src/Cardano/Db/Migration.hs index c91fcb53b..ff2c57842 100644 --- a/cardano-db/src/Cardano/Db/Migration.hs +++ b/cardano-db/src/Cardano/Db/Migration.hs @@ -28,10 +28,11 @@ import Cardano.BM.Trace (Trace) import Cardano.Crypto.Hash (Blake2b_256, ByteString, Hash, hashToStringAsHex, hashWith) import Cardano.Db.Migration.Haskell import Cardano.Db.Migration.Version -import Cardano.Db.Operations.Core.Query +import Cardano.Db.Operations.Query import Cardano.Db.PGConfig import Cardano.Db.Run import Cardano.Db.Schema.BaseSchema +import Cardano.Db.Schema.Core.TxOut (migrateCoreTxOutCardanoDb) import Cardano.Prelude (Typeable, textShow) import Control.Exception (Exception, SomeException, handle) import Control.Monad.Extra @@ -238,7 +239,10 @@ createMigration source (MigrationDir migdir) = do create :: ReaderT SqlBackend (NoLoggingT IO) (Maybe (MigrationVersion, Text)) create = do ver <- getSchemaVersion - statements <- getMigration migrateBaseCardanoDb + -- here is the place to combine any "core" schemas to the base schema + statementsBase <- getMigration migrateBaseCardanoDb + statementsTxOut <- getMigration migrateCoreTxOutCardanoDb + let statements = statementsBase <> statementsTxOut if null statements then pure Nothing else do diff --git a/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Queries.hs b/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Queries.hs deleted file mode 100644 index 3cf6dbac7..000000000 --- a/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Queries.hs +++ /dev/null @@ -1,381 +0,0 @@ -{-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE NumericUnderscores #-} -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE RankNTypes #-} -{-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TypeApplications #-} - -module Cardano.Db.Migration.Extra.CosnumedTxOut.Queries where - -import Cardano.BM.Trace (Trace, logError, logInfo, logWarning) -import Cardano.Db.Error (LookupFail (..)) -import Cardano.Db.Insert (insertMany', insertUnchecked) -import Cardano.Db.Migration.Extra.CosnumedTxOut.Schema -import Cardano.Db.Query (isJust, listToMaybe, queryBlockHeight, queryMaxRefId) -import Cardano.Prelude (textShow) -import Control.Exception.Lifted (handle, throwIO) -import Control.Monad.Extra (unless, when, whenJust) -import Control.Monad.IO.Class (MonadIO, liftIO) -import Control.Monad.Trans.Control (MonadBaseControl) -import Control.Monad.Trans.Reader (ReaderT) -import Data.Text (Text) -import Data.Word (Word64) -import Database.Esqueleto.Experimental hiding (update, (<=.), (=.), (==.)) -import qualified Database.Esqueleto.Experimental as E -import Database.Persist ((<=.), (=.), (==.)) -import Database.Persist.Class (update) -import Database.Persist.Sql (deleteWhereCount) -import Database.PostgreSQL.Simple (SqlError) - -pageSize :: Word64 -pageSize = 100_000 - -data ConsumedTriplet = ConsumedTriplet - { ctTxOutTxId :: TxId -- The txId of the txOut - , ctTxOutIndex :: Word64 -- Tx index of the txOut - , ctTxInTxId :: TxId -- The txId of the txId - } - --------------------------------------------------------------------------------------------------- --- Queries --------------------------------------------------------------------------------------------------- -queryUpdateListTxOutConsumedByTxId :: MonadIO m => [(TxOutId, TxId)] -> ReaderT SqlBackend m () -queryUpdateListTxOutConsumedByTxId ls = do - mapM_ (uncurry updateTxOutConsumedByTxId) ls - -queryTxConsumedColumnExists :: MonadIO m => ReaderT SqlBackend m Bool -queryTxConsumedColumnExists = do - columnExists :: [Text] <- - fmap unSingle - <$> rawSql - ( mconcat - [ "SELECT column_name FROM information_schema.columns " - , "WHERE table_name='tx_out' and column_name='consumed_by_tx_id'" - ] - ) - [] - pure (not $ null columnExists) - --- | This is a count of the null consumed_by_tx_id -queryTxOutConsumedNullCount :: MonadIO m => ReaderT SqlBackend m Word64 -queryTxOutConsumedNullCount = do - res <- select $ do - txOut <- from $ table @TxOut - where_ (isNothing $ txOut ^. TxOutConsumedByTxId) - pure countRows - pure $ maybe 0 unValue (listToMaybe res) - -queryTxOutConsumedCount :: MonadIO m => ReaderT SqlBackend m Word64 -queryTxOutConsumedCount = do - res <- select $ do - txOut <- from $ table @TxOut - where_ (not_ $ isNothing $ txOut ^. TxOutConsumedByTxId) - pure countRows - pure $ maybe 0 unValue (listToMaybe res) - -querySetNullTxOut :: MonadIO m => Trace IO Text -> Maybe TxId -> ReaderT SqlBackend m () -querySetNullTxOut trce mMinTxId = do - whenJust mMinTxId $ \txId -> do - txOutIds <- getTxOutConsumedAfter txId - mapM_ setNullTxOutConsumedAfter txOutIds - let updatedEntries = length txOutIds - liftIO $ logInfo trce $ "Set to null " <> textShow updatedEntries <> " tx_out.consumed_by_tx_id" - -createConsumedTxOut :: - forall m. - ( MonadBaseControl IO m - , MonadIO m - ) => - ReaderT SqlBackend m () -createConsumedTxOut = do - handle exceptHandler $ - rawExecute - "ALTER TABLE tx_out ADD COLUMN consumed_by_tx_id INT8 NULL" - [] - handle exceptHandler $ - rawExecute - "CREATE INDEX IF NOT EXISTS idx_tx_out_consumed_by_tx_id ON tx_out (consumed_by_tx_id)" - [] - handle exceptHandler $ - rawExecute - "ALTER TABLE ma_tx_out ADD CONSTRAINT ma_tx_out_tx_out_id_fkey FOREIGN KEY(tx_out_id) REFERENCES tx_out(id) ON DELETE CASCADE ON UPDATE RESTRICT" - [] - where - exceptHandler :: SqlError -> ReaderT SqlBackend m a - exceptHandler e = - liftIO $ throwIO (DBPruneConsumed $ show e) - -_validateMigration :: MonadIO m => Trace IO Text -> ReaderT SqlBackend m Bool -_validateMigration trce = do - _migrated <- queryTxConsumedColumnExists - -- unless migrated $ runMigration - txInCount <- countTxIn - consumedTxOut <- countConsumed - if txInCount > consumedTxOut - then do - liftIO $ - logWarning trce $ - mconcat - [ "Found incomplete TxOut migration. There are" - , textShow txInCount - , " TxIn, but only" - , textShow consumedTxOut - , " consumed TxOut" - ] - pure False - else - if txInCount == consumedTxOut - then do - liftIO $ logInfo trce "Found complete TxOut migration" - pure True - else do - liftIO $ - logError trce $ - mconcat - [ "The impossible happened! There are" - , textShow txInCount - , " TxIn, but " - , textShow consumedTxOut - , " consumed TxOut" - ] - pure False - -queryWrongConsumedBy :: MonadIO m => ReaderT SqlBackend m Word64 -queryWrongConsumedBy = do - res <- select $ do - txOut <- from $ table @TxOut - where_ (just (txOut ^. TxOutTxId) ==. txOut ^. TxOutConsumedByTxId) - pure countRows - pure $ maybe 0 unValue (listToMaybe res) - --------------------------------------------------------------------------------------------------- --- Inserts --------------------------------------------------------------------------------------------------- -insertTxOutExtra :: (MonadBaseControl IO m, MonadIO m) => TxOut -> ReaderT SqlBackend m TxOutId -insertTxOutExtra = insertUnchecked "TxOutExtra" - -insertManyTxOutExtra :: (MonadBaseControl IO m, MonadIO m) => [TxOut] -> ReaderT SqlBackend m [TxOutId] -insertManyTxOutExtra = insertMany' "TxOut" - --------------------------------------------------------------------------------------------------- --- Updates --------------------------------------------------------------------------------------------------- -updateTxOutConsumedByTxId :: MonadIO m => TxOutId -> TxId -> ReaderT SqlBackend m () -updateTxOutConsumedByTxId txOutId txId = - update txOutId [TxOutConsumedByTxId =. Just txId] - --- | This requires an index at TxOutConsumedByTxId. -getTxOutConsumedAfter :: MonadIO m => TxId -> ReaderT SqlBackend m [TxOutId] -getTxOutConsumedAfter txId = do - res <- select $ do - txOut <- from $ table @TxOut - where_ (txOut ^. TxOutConsumedByTxId >=. just (val txId)) - pure $ txOut ^. persistIdField - pure $ unValue <$> res - --- | This requires an index at TxOutConsumedByTxId. -setNullTxOutConsumedAfter :: MonadIO m => TxOutId -> ReaderT SqlBackend m () -setNullTxOutConsumedAfter txOutId = do - update txOutId [TxOutConsumedByTxId =. Nothing] - -migrateTxOut :: - ( MonadBaseControl IO m - , MonadIO m - ) => - Maybe (Trace IO Text) -> - ReaderT SqlBackend m () -migrateTxOut mTrace = do - _ <- createConsumedTxOut - migrateNextPage 0 - where - migrateNextPage :: MonadIO m => Word64 -> ReaderT SqlBackend m () - migrateNextPage offst = do - whenJust mTrace $ \trce -> - liftIO $ logInfo trce $ "Handling input offset " <> textShow offst - page <- getInputPage offst pageSize - updatePageEntries page - when (fromIntegral (length page) == pageSize) $ - migrateNextPage $! - offst - + pageSize - --------------------------------------------------------------------------------------------------- --- Delete + Update --------------------------------------------------------------------------------------------------- - -deleteAndUpdateConsumedTxOut :: - forall m. - (MonadIO m, MonadBaseControl IO m) => - Trace IO Text -> - Word64 -> - ReaderT SqlBackend m () -deleteAndUpdateConsumedTxOut trce blockNoDiff = do - maxTxId <- findMaxTxInId blockNoDiff - case maxTxId of - Left errMsg -> do - liftIO $ logInfo trce $ "No tx_out were deleted as no blocks found: " <> errMsg - liftIO $ logInfo trce "Now Running extra migration prune tx_out" - migrateTxOut (Just trce) - Right mTxId -> do - migrateNextPage mTxId False 0 - where - migrateNextPage :: TxId -> Bool -> Word64 -> ReaderT SqlBackend m () - migrateNextPage maxTxId ranCreateConsumedTxOut offst = do - pageEntries <- getInputPage offst pageSize - resPageEntries <- splitAndProcessPageEntries trce ranCreateConsumedTxOut maxTxId pageEntries - when (fromIntegral (length pageEntries) == pageSize) $ - migrateNextPage maxTxId resPageEntries $! - offst - + pageSize - --- Split the page entries by maxTxInId and process -splitAndProcessPageEntries :: - forall m. - (MonadIO m, MonadBaseControl IO m) => - Trace IO Text -> - Bool -> - TxId -> - [ConsumedTriplet] -> - ReaderT SqlBackend m Bool -splitAndProcessPageEntries trce ranCreateConsumedTxOut maxTxId pageEntries = do - let entriesSplit = span (\tr -> ctTxInTxId tr <= maxTxId) pageEntries - case entriesSplit of - ([], []) -> do - shouldCreateConsumedTxOut trce ranCreateConsumedTxOut - pure True - -- the whole list is less that maxTxInId - (xs, []) -> do - deletePageEntries xs - pure False - -- the whole list is greater that maxTxInId - ([], ys) -> do - shouldCreateConsumedTxOut trce ranCreateConsumedTxOut - updatePageEntries ys - pure True - -- the list has both bellow and above maxTxInId - (xs, ys) -> do - deletePageEntries xs - shouldCreateConsumedTxOut trce ranCreateConsumedTxOut - updatePageEntries ys - pure True - --- | Update -updatePageEntries :: - MonadIO m => - [ConsumedTriplet] -> - ReaderT SqlBackend m () -updatePageEntries = - mapM_ updateTxOutConsumedByTxIdUnique - -updateTxOutConsumedByTxIdUnique :: MonadIO m => ConsumedTriplet -> ReaderT SqlBackend m () -updateTxOutConsumedByTxIdUnique ConsumedTriplet {ctTxOutTxId, ctTxOutIndex, ctTxInTxId} = - updateWhere [TxOutTxId ==. ctTxOutTxId, TxOutIndex ==. ctTxOutIndex] [TxOutConsumedByTxId =. Just ctTxInTxId] - --- | Delete --- this builds up a single delete query using the pageEntries list -deletePageEntries :: - MonadIO m => - [ConsumedTriplet] -> - ReaderT SqlBackend m () -deletePageEntries = mapM_ (\ConsumedTriplet {ctTxOutTxId, ctTxOutIndex} -> deleteTxOutConsumed ctTxOutTxId ctTxOutIndex) - -deleteTxOutConsumed :: MonadIO m => TxId -> Word64 -> ReaderT SqlBackend m () -deleteTxOutConsumed txOutId index = - deleteWhere [TxOutTxId ==. txOutId, TxOutIndex ==. index] - -shouldCreateConsumedTxOut :: - (MonadIO m, MonadBaseControl IO m) => - Trace IO Text -> - Bool -> - ReaderT SqlBackend m () -shouldCreateConsumedTxOut trce rcc = - unless rcc $ do - liftIO $ logInfo trce "Created ConsumedTxOut when handling page entries." - createConsumedTxOut - --------------------------------------------------------------------------------------------------- --- Delete --------------------------------------------------------------------------------------------------- -deleteConsumedTxOut :: - forall m. - MonadIO m => - Trace IO Text -> - Word64 -> - ReaderT SqlBackend m () -deleteConsumedTxOut trce blockNoDiff = do - maxTxInId <- findMaxTxInId blockNoDiff - case maxTxInId of - Left errMsg -> liftIO $ logInfo trce $ "No tx_out was deleted: " <> errMsg - Right mxtid -> deleteConsumedBeforeTx trce mxtid - -deleteConsumedBeforeTx :: MonadIO m => Trace IO Text -> TxId -> ReaderT SqlBackend m () -deleteConsumedBeforeTx trce txId = do - countDeleted <- deleteWhereCount [TxOutConsumedByTxId <=. Just txId] - liftIO $ logInfo trce $ "Deleted " <> textShow countDeleted <> " tx_out" - --------------------------------------------------------------------------------------------------- --- Helpers --------------------------------------------------------------------------------------------------- -findMaxTxInId :: forall m. MonadIO m => Word64 -> ReaderT SqlBackend m (Either Text TxId) -findMaxTxInId blockNoDiff = do - mBlockHeight <- queryBlockHeight - maybe (pure $ Left "No blocks found") findConsumed mBlockHeight - where - findConsumed :: Word64 -> ReaderT SqlBackend m (Either Text TxId) - findConsumed tipBlockNo = do - if tipBlockNo <= blockNoDiff - then pure $ Left $ "Tip blockNo is " <> textShow tipBlockNo - else do - mBlockId <- queryBlockNo $ tipBlockNo - blockNoDiff - maybe - (pure $ Left $ "BlockNo hole found at " <> textShow (tipBlockNo - blockNoDiff)) - findConsumedBeforeBlock - mBlockId - - findConsumedBeforeBlock :: BlockId -> ReaderT SqlBackend m (Either Text TxId) - findConsumedBeforeBlock blockId = do - mTxId <- queryMaxRefId TxBlockId blockId False - case mTxId of - Nothing -> pure $ Left $ "No txs found before " <> textShow blockId - Just txId -> pure $ Right txId - -queryBlockNo :: MonadIO m => Word64 -> ReaderT SqlBackend m (Maybe BlockId) -queryBlockNo blkNo = do - res <- select $ do - blk <- from $ table @Block - where_ (blk ^. BlockBlockNo E.==. just (val blkNo)) - pure (blk ^. BlockId) - pure $ fmap unValue (listToMaybe res) - -getInputPage :: MonadIO m => Word64 -> Word64 -> ReaderT SqlBackend m [ConsumedTriplet] -getInputPage offs pgSize = do - res <- select $ do - txIn <- from $ table @TxIn - limit (fromIntegral pgSize) - offset (fromIntegral offs) - orderBy [asc (txIn ^. TxInId)] - pure txIn - pure $ convert <$> res - where - convert txIn = - ConsumedTriplet - { ctTxOutTxId = txInTxOutId (entityVal txIn) - , ctTxOutIndex = txInTxOutIndex (entityVal txIn) - , ctTxInTxId = txInTxInId (entityVal txIn) - } - -countTxIn :: MonadIO m => ReaderT SqlBackend m Word64 -countTxIn = do - res <- select $ do - _ <- from $ table @TxIn - pure countRows - pure $ maybe 0 unValue (listToMaybe res) - -countConsumed :: MonadIO m => ReaderT SqlBackend m Word64 -countConsumed = do - res <- select $ do - txOut <- from $ table @TxOut - where_ (isJust $ txOut ^. TxOutConsumedByTxId) - pure countRows - pure $ maybe 0 unValue (listToMaybe res) diff --git a/cardano-db/src/Cardano/Db/Operations/Core/AlterTable.hs b/cardano-db/src/Cardano/Db/Operations/AlterTable.hs similarity index 98% rename from cardano-db/src/Cardano/Db/Operations/Core/AlterTable.hs rename to cardano-db/src/Cardano/Db/Operations/AlterTable.hs index 3523c6138..adefd1de4 100644 --- a/cardano-db/src/Cardano/Db/Operations/Core/AlterTable.hs +++ b/cardano-db/src/Cardano/Db/Operations/AlterTable.hs @@ -5,7 +5,7 @@ {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -Wno-unused-local-binds #-} -module Cardano.Db.Operations.Core.AlterTable ( +module Cardano.Db.Operations.AlterTable ( AlterTable (..), DbAlterTableException (..), ManualDbConstraints (..), diff --git a/cardano-db/src/Cardano/Db/Operations/Core/Delete.hs b/cardano-db/src/Cardano/Db/Operations/Delete.hs similarity index 98% rename from cardano-db/src/Cardano/Db/Operations/Core/Delete.hs rename to cardano-db/src/Cardano/Db/Operations/Delete.hs index d59446d57..87179087c 100644 --- a/cardano-db/src/Cardano/Db/Operations/Core/Delete.hs +++ b/cardano-db/src/Cardano/Db/Operations/Delete.hs @@ -8,7 +8,7 @@ {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeOperators #-} -module Cardano.Db.Operations.Core.Delete ( +module Cardano.Db.Operations.Delete ( deleteBlocksSlotNo, deleteBlocksSlotNoNoTrace, deleteDelistedPool, @@ -25,8 +25,8 @@ module Cardano.Db.Operations.Core.Delete ( ) where import Cardano.BM.Trace (Trace, logWarning, nullTracer) -import Cardano.Db.Operations.Core.MinId (MinIds (..), MinIdsWrapper (..), completeMinId, textToMinIds) -import Cardano.Db.Operations.Core.Query +import Cardano.Db.Operations.Other.MinId (MinIds (..), MinIdsWrapper (..), completeMinId, textToMinIds) +import Cardano.Db.Operations.Query import Cardano.Db.Operations.Types (TxOutTableType (..)) import Cardano.Db.Schema.BaseSchema import qualified Cardano.Db.Schema.Core.TxOut as C diff --git a/cardano-db/src/Cardano/Db/Operations/Core/Insert.hs b/cardano-db/src/Cardano/Db/Operations/Insert.hs similarity index 99% rename from cardano-db/src/Cardano/Db/Operations/Core/Insert.hs rename to cardano-db/src/Cardano/Db/Operations/Insert.hs index f937bf696..c9933190f 100644 --- a/cardano-db/src/Cardano/Db/Operations/Core/Insert.hs +++ b/cardano-db/src/Cardano/Db/Operations/Insert.hs @@ -6,7 +6,7 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} -module Cardano.Db.Operations.Core.Insert ( +module Cardano.Db.Operations.Insert ( insertAdaPots, insertBlock, insertCollateralTxIn, @@ -97,7 +97,7 @@ module Cardano.Db.Operations.Core.Insert ( insertBlockChecked, ) where -import Cardano.Db.Operations.Core.Query +import Cardano.Db.Operations.Query import Cardano.Db.Schema.BaseSchema import Cardano.Db.Types import Cardano.Prelude (textShow) @@ -494,8 +494,9 @@ insertAlwaysNoConfidence = do , drepHashHasScript = False } --- ----------------------------------------------------------------------------- - +-------------------------------------------------------------------------------- +-- Custom insert functions +-------------------------------------------------------------------------------- data DbInsertException = DbInsertException String SqlError deriving (Show) diff --git a/cardano-db/src/Cardano/Db/Operations/Variant/ConsumedTxOut.hs b/cardano-db/src/Cardano/Db/Operations/Other/ConsumedTxOut.hs similarity index 73% rename from cardano-db/src/Cardano/Db/Operations/Variant/ConsumedTxOut.hs rename to cardano-db/src/Cardano/Db/Operations/Other/ConsumedTxOut.hs index 31d03d4bf..ed90bd77b 100644 --- a/cardano-db/src/Cardano/Db/Operations/Variant/ConsumedTxOut.hs +++ b/cardano-db/src/Cardano/Db/Operations/Other/ConsumedTxOut.hs @@ -11,18 +11,18 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} -module Cardano.Db.Operations.Variant.ConsumedTxOut where +module Cardano.Db.Operations.Other.ConsumedTxOut where import Cardano.BM.Trace (Trace, logError, logInfo, logWarning) import Cardano.Db.Error (LookupFail (..), logAndThrowIO) -import Cardano.Db.Operations.Core.Insert (insertExtraMigration) -import Cardano.Db.Operations.Core.Query (listToMaybe, queryAllExtraMigrations, queryBlockHeight, queryBlockNo, queryMaxRefId) -import Cardano.Db.Operations.Core.QueryHelper (isJust) -import Cardano.Db.Operations.Types (TxOutFields (..), TxOutIdW (..), TxOutTable, TxOutTableType (..)) +import Cardano.Db.Operations.Insert (insertExtraMigration) +import Cardano.Db.Operations.Query (listToMaybe, queryAllExtraMigrations, queryBlockHeight, queryBlockNo, queryMaxRefId) +import Cardano.Db.Operations.QueryHelper (isJust) +import Cardano.Db.Operations.Types (TxOutFields (..), TxOutIdW (..), TxOutTable, TxOutTableType (..), isTxOutVariantAddress) import Cardano.Db.Schema.BaseSchema import qualified Cardano.Db.Schema.Core.TxOut as C import qualified Cardano.Db.Schema.Variant.TxOut as V -import Cardano.Db.Types (ExtraMigration (..), PruneConsumeMigration (..), wasPruneTxOutPreviouslySet) +import Cardano.Db.Types (ExtraMigration (..), MigrationValues (..), PruneConsumeMigration (..), processMigrationValues) import Cardano.Prelude (textShow) import Control.Exception (throw) import Control.Exception.Lifted (handle, throwIO) @@ -31,6 +31,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Trans.Control (MonadBaseControl) import Control.Monad.Trans.Reader (ReaderT) import Data.Text (Text) +import qualified Data.Text as Text import Data.Word (Word64) import Database.Esqueleto.Experimental hiding (update, (<=.), (=.), (==.)) import qualified Database.Esqueleto.Experimental as E @@ -109,106 +110,51 @@ querySetNullTxOut trce txOutTableType mMinTxId = do let updatedEntries = length txOutIds liftIO $ logInfo trce $ "Set to null " <> textShow updatedEntries <> " tx_out.consumed_by_tx_id" --- TODO: cmdv need to fix the raw execute -createConsumedTxOut :: - forall m. - ( MonadBaseControl IO m - , MonadIO m - ) => - ReaderT SqlBackend m () -createConsumedTxOut = do - handle exceptHandler $ - rawExecute - "ALTER TABLE tx_out ADD COLUMN consumed_by_tx_id INT8 NULL" - [] - handle exceptHandler $ - rawExecute - "CREATE INDEX IF NOT EXISTS idx_tx_out_consumed_by_tx_id ON tx_out (consumed_by_tx_id)" - [] - handle exceptHandler $ - rawExecute - "ALTER TABLE ma_tx_out ADD CONSTRAINT ma_tx_out_tx_out_id_fkey FOREIGN KEY(tx_out_id) REFERENCES tx_out(id) ON DELETE CASCADE ON UPDATE RESTRICT" - [] - where - exceptHandler :: SqlError -> ReaderT SqlBackend m a - exceptHandler e = - liftIO $ throwIO (DBPruneConsumed $ show e) - -_validateMigration :: MonadIO m => Trace IO Text -> TxOutTableType -> ReaderT SqlBackend m Bool -_validateMigration trce txOutTableType = do - _migrated <- queryTxConsumedColumnExists - -- unless migrated $ runMigration - txInCount <- countTxIn - consumedTxOut <- countConsumed txOutTableType - if txInCount > consumedTxOut - then do - liftIO $ - logWarning trce $ - mconcat - [ "Found incomplete TxOut migration. There are" - , textShow txInCount - , " TxIn, but only" - , textShow consumedTxOut - , " consumed TxOut" - ] - pure False - else - if txInCount == consumedTxOut - then do - liftIO $ logInfo trce "Found complete TxOut migration" - pure True - else do - liftIO $ - logError trce $ - mconcat - [ "The impossible happened! There are" - , textShow txInCount - , " TxIn, but " - , textShow consumedTxOut - , " consumed TxOut" - ] - pure False - updateListTxOutConsumedByTxId :: MonadIO m => [(TxOutIdW, TxId)] -> ReaderT SqlBackend m () updateListTxOutConsumedByTxId ls = do queryUpdateListTxOutConsumedByTxId ls runExtraMigrations :: (MonadBaseControl IO m, MonadIO m) => Trace IO Text -> TxOutTableType -> Word64 -> PruneConsumeMigration -> ReaderT SqlBackend m () -runExtraMigrations trce txOutTableType blockNoDiff PruneConsumeMigration {..} = do - hasConsumedField <- queryTxConsumedColumnExists +runExtraMigrations trce txOutTableType blockNoDiff pcm = do ems <- queryAllExtraMigrations - let wPruneTxOutPreviouslySet = wasPruneTxOutPreviouslySet ems + let migrationValues = processMigrationValues ems pcm + -- Has the user given txout address config && the migration wasn't previously set + when (isTxOutVariantAddress txOutTableType && not (isTxOutAddressPreviouslySet migrationValues)) $ do + updateTxOutAndCreateAddress + insertExtraMigration TxOutAddressPreviouslySet + -- first check if pruneTxOut flag is missing and it has previously been used - case (pcmPruneTxOut, wPruneTxOutPreviouslySet) of - (False, True) -> - throw $ - DBExtraMigration - ( "If --prune-tx-out flag is enabled and then db-sync is stopped all future executions of db-sync " - <> "should still have this flag activated. Otherwise, it is considered bad usage and can cause crashes." - ) - _ -> do - case (hasConsumedField, pcmConsumeOrPruneTxOut, pcmPruneTxOut) of + when (isPruneTxOutPreviouslySet migrationValues && not (pcmPruneTxOut pcm)) $ + throw $ + DBExtraMigration + "If --prune-tx-out flag is enabled and then db-sync is stopped all future executions of db-sync should still have this flag activated. Otherwise, it is considered bad usage and can cause crashes." + handleMigration migrationValues + where + handleMigration :: (MonadBaseControl IO m, MonadIO m) => MigrationValues -> ReaderT SqlBackend m () + handleMigration migrationValues@MigrationValues {..} = do + let PruneConsumeMigration {..} = pruneConsumeMigration + case (isConsumeTxOutPreviouslySet, pcmConsumeOrPruneTxOut, pcmPruneTxOut) of + -- No Migration Needed (False, False, False) -> do - liftIO $ logInfo trce "No extra migration specified" + liftIO $ logInfo trce "runExtraMigrations: No extra migration specified" + -- Already migrated (True, True, False) -> do - liftIO $ logInfo trce "Extra migration consumed_tx_out already executed" - (True, False, False) -> liftIO $ logAndThrowIO trce migratedButNotSet + liftIO $ logInfo trce "runExtraMigrations: Extra migration consumed_tx_out already executed" + -- Invalid State + (True, False, False) -> liftIO $ logAndThrowIO trce "runExtraMigrations: consumed-tx-out or prune-tx-out is not set, but consumed migration is found." + -- Consume TxOut (False, True, False) -> do - liftIO $ logInfo trce "Running extra migration consumed_tx_out" - migrateTxOut (Just trce) txOutTableType - (False, _, True) -> do - shouldInsertToMigrationTable - deleteAndUpdateConsumedTxOut trce txOutTableType blockNoDiff - (True, _, True) -> do - shouldInsertToMigrationTable - liftIO $ logInfo trce "Running extra migration prune tx_out" - deleteConsumedTxOut trce txOutTableType blockNoDiff - where - migratedButNotSet = "consumed-tx-out or prune-tx-out is not set, but consumed migration is found." - -- if PruneTxOutFlagPreviouslySet isn't already set then set it. - shouldInsertToMigrationTable :: (MonadBaseControl IO m, MonadIO m) => ReaderT SqlBackend m () - shouldInsertToMigrationTable = do - unless wPruneTxOutPreviouslySet $ insertExtraMigration PruneTxOutFlagPreviouslySet + liftIO $ logInfo trce "runExtraMigrations: Running extra migration consumed_tx_out" + insertExtraMigration ConsumeTxOutPreviouslySet + migrateTxOut trce txOutTableType $ Just migrationValues + -- Prune TxOut + (_, _, True) -> do + unless isPruneTxOutPreviouslySet $ insertExtraMigration PruneTxOutFlagPreviouslySet + if isConsumeTxOutPreviouslySet + then do + liftIO $ logInfo trce "runExtraMigrations: Running extra migration prune tx_out" + deleteConsumedTxOut trce txOutTableType blockNoDiff + else deleteAndUpdateConsumedTxOut trce txOutTableType migrationValues blockNoDiff queryWrongConsumedBy :: TxOutTableType -> MonadIO m => ReaderT SqlBackend m Word64 queryWrongConsumedBy = \case @@ -271,46 +217,52 @@ setNullTxOutConsumedAfter txOutTableType txOutId = CTxOutIdW txOutId' -> update txOutId' [C.TxOutConsumedByTxId =. Nothing] VTxOutIdW txOutId' -> update txOutId' [V.TxOutConsumedByTxId =. Nothing] +migrateTxOutTests :: (MonadIO m, MonadBaseControl IO m) => TxOutTableType -> ReaderT SqlBackend m () +migrateTxOutTests txOutTableType = do + _ <- createConsumedTxOut + migrateNextPageTxOut Nothing txOutTableType 0 + migrateTxOut :: ( MonadBaseControl IO m , MonadIO m ) => - Maybe (Trace IO Text) -> + Trace IO Text -> TxOutTableType -> + Maybe MigrationValues -> ReaderT SqlBackend m () -migrateTxOut mTrace txOutTableType = do +migrateTxOut trce txOutTableType _mMvs = do + liftIO $ logInfo trce "migrateTxOut:" _ <- createConsumedTxOut - migrateNextPage 0 - where - migrateNextPage :: MonadIO m => Word64 -> ReaderT SqlBackend m () - migrateNextPage offst = do - whenJust mTrace $ \trce -> - liftIO $ logInfo trce $ "Handling input offset " <> textShow offst - page <- getInputPage offst pageSize - updatePageEntries txOutTableType page - when (fromIntegral (length page) == pageSize) $ - migrateNextPage $! - offst - + pageSize + migrateNextPageTxOut (Just trce) txOutTableType 0 + +migrateNextPageTxOut :: MonadIO m => Maybe (Trace IO Text) -> TxOutTableType -> Word64 -> ReaderT SqlBackend m () +migrateNextPageTxOut mTrce txOutTableType offst = do + whenJust mTrce $ \trce -> + liftIO $ logInfo trce $ "Handling input offset " <> textShow offst + page <- getInputPage offst pageSize + updatePageEntries txOutTableType page + when (fromIntegral (length page) == pageSize) $ + migrateNextPageTxOut mTrce txOutTableType $! + (offst + pageSize) -------------------------------------------------------------------------------------------------- -- Delete + Update -------------------------------------------------------------------------------------------------- - deleteAndUpdateConsumedTxOut :: forall m. (MonadIO m, MonadBaseControl IO m) => Trace IO Text -> TxOutTableType -> + MigrationValues -> Word64 -> ReaderT SqlBackend m () -deleteAndUpdateConsumedTxOut trce txOutTableType blockNoDiff = do +deleteAndUpdateConsumedTxOut trce txOutTableType migrationValues blockNoDiff = do maxTxId <- findMaxTxInId blockNoDiff case maxTxId of Left errMsg -> do liftIO $ logInfo trce $ "No tx_out were deleted as no blocks found: " <> errMsg liftIO $ logInfo trce "Now Running extra migration prune tx_out" - migrateTxOut (Just trce) txOutTableType + migrateTxOut trce txOutTableType $ Just migrationValues Right mTxId -> do migrateNextPage mTxId False 0 where @@ -355,6 +307,16 @@ splitAndProcessPageEntries trce txOutTableType ranCreateConsumedTxOut maxTxId pa updatePageEntries txOutTableType ys pure True +shouldCreateConsumedTxOut :: + (MonadIO m, MonadBaseControl IO m) => + Trace IO Text -> + Bool -> + ReaderT SqlBackend m () +shouldCreateConsumedTxOut trce rcc = + unless rcc $ do + liftIO $ logInfo trce "Created ConsumedTxOut when handling page entries." + createConsumedTxOut + -- | Update updatePageEntries :: MonadIO m => @@ -369,7 +331,6 @@ updateTxOutConsumedByTxIdUnique txOutTableType ConsumedTriplet {ctTxOutTxId, ctT TxOutCore -> updateWhere [C.TxOutTxId ==. ctTxOutTxId, C.TxOutIndex ==. ctTxOutIndex] [C.TxOutConsumedByTxId =. Just ctTxInTxId] TxOutVariantAddress -> updateWhere [V.TxOutTxId ==. ctTxOutTxId, V.TxOutIndex ==. ctTxOutIndex] [V.TxOutConsumedByTxId =. Just ctTxInTxId] --- | Delete -- this builds up a single delete query using the pageEntries list deletePageEntries :: MonadIO m => @@ -383,15 +344,75 @@ deleteTxOutConsumed txOutTableType txOutId index = case txOutTableType of TxOutCore -> deleteWhere [C.TxOutTxId ==. txOutId, C.TxOutIndex ==. index] TxOutVariantAddress -> deleteWhere [V.TxOutTxId ==. txOutId, V.TxOutIndex ==. index] -shouldCreateConsumedTxOut :: - (MonadIO m, MonadBaseControl IO m) => - Trace IO Text -> - Bool -> +-------------------------------------------------------------------------------------------------- +-- Raw Queries +-------------------------------------------------------------------------------------------------- +createConsumedTxOut :: + forall m. + ( MonadBaseControl IO m + , MonadIO m + ) => ReaderT SqlBackend m () -shouldCreateConsumedTxOut trce rcc = - unless rcc $ do - liftIO $ logInfo trce "Created ConsumedTxOut when handling page entries." - createConsumedTxOut +createConsumedTxOut = do + handle exceptHandler $ rawExecute createIndex [] + handle exceptHandler $ rawExecute addConstraint [] + where + createIndex = + "CREATE INDEX IF NOT EXISTS idx_tx_out_consumed_by_tx_id ON tx_out (consumed_by_tx_id)" + + addConstraint = + ( Text.unlines + [ "do $$" + , "begin" + , " if not exists (" + , " select 1" + , " from information_schema.table_constraints" + , " where constraint_name = 'ma_tx_out_tx_out_id_fkey'" + , " and table_name = 'ma_tx_out'" + , " ) then" + , " execute 'alter table ma_tx_out add constraint ma_tx_out_tx_out_id_fkey foreign key(tx_out_id) references tx_out(id) on delete cascade on update restrict';" + , " end if;" + , "end $$;" + ] + ) + + exceptHandler :: SqlError -> ReaderT SqlBackend m a + exceptHandler e = + liftIO $ throwIO (DBPruneConsumed $ show e) + +updateTxOutAndCreateAddress :: + forall m. + ( MonadBaseControl IO m + , MonadIO m + ) => + ReaderT SqlBackend m () +updateTxOutAndCreateAddress = do + handle exceptHandler $ rawExecute alterTxOutQuery [] + handle exceptHandler $ rawExecute createAddressTableQuery [] + where + alterTxOutQuery = + Text.unlines + [ "ALTER TABLE \"tx_out\"" + , " ADD COLUMN \"address_id\" INT8 NOT NULL," + , " DROP COLUMN \"address\"," + , " DROP COLUMN \"address_has_script\"," + , " DROP COLUMN \"payment_cred\"," + , " DROP COLUMN \"stake_address_id\"" + ] + createAddressTableQuery = + Text.unlines + [ "CREATE TABLE \"address\" (" + , " \"id\" SERIAL8 PRIMARY KEY UNIQUE," + , " \"address\" VARCHAR NOT NULL," + , " \"raw\" BYTEA NOT NULL," + , " \"has_script\" BOOLEAN NOT NULL," + , " \"payment_cred\" hash28type NULL," + , " \"stake_address_id\" INT8 NULL" + , ")" + ] + exceptHandler :: SqlError -> ReaderT SqlBackend m a + exceptHandler e = + liftIO $ throwIO (DBPruneConsumed $ show e) -------------------------------------------------------------------------------------------------- -- Delete @@ -484,3 +505,37 @@ countConsumed = \case where_ (isJust $ txOut ^. txOutConsumedByTxIdField @a) pure countRows pure $ maybe 0 unValue (listToMaybe res) + +_validateMigration :: MonadIO m => Trace IO Text -> TxOutTableType -> ReaderT SqlBackend m Bool +_validateMigration trce txOutTableType = do + _migrated <- queryTxConsumedColumnExists + txInCount <- countTxIn + consumedTxOut <- countConsumed txOutTableType + if txInCount > consumedTxOut + then do + liftIO $ + logWarning trce $ + mconcat + [ "Found incomplete TxOut migration. There are" + , textShow txInCount + , " TxIn, but only" + , textShow consumedTxOut + , " consumed TxOut" + ] + pure False + else + if txInCount == consumedTxOut + then do + liftIO $ logInfo trce "Found complete TxOut migration" + pure True + else do + liftIO $ + logError trce $ + mconcat + [ "The impossible happened! There are" + , textShow txInCount + , " TxIn, but " + , textShow consumedTxOut + , " consumed TxOut" + ] + pure False diff --git a/cardano-db/src/Cardano/Db/Operations/Variant/JsonbQuery.hs b/cardano-db/src/Cardano/Db/Operations/Other/JsonbQuery.hs similarity index 98% rename from cardano-db/src/Cardano/Db/Operations/Variant/JsonbQuery.hs rename to cardano-db/src/Cardano/Db/Operations/Other/JsonbQuery.hs index e8b3862d9..7ae86600b 100644 --- a/cardano-db/src/Cardano/Db/Operations/Variant/JsonbQuery.hs +++ b/cardano-db/src/Cardano/Db/Operations/Other/JsonbQuery.hs @@ -3,7 +3,7 @@ {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} -module Cardano.Db.Operations.Variant.JsonbQuery where +module Cardano.Db.Operations.Other.JsonbQuery where import Cardano.Db.Error (LookupFail (..)) import Control.Exception.Lifted (handle, throwIO) diff --git a/cardano-db/src/Cardano/Db/Operations/Core/MinId.hs b/cardano-db/src/Cardano/Db/Operations/Other/MinId.hs similarity index 98% rename from cardano-db/src/Cardano/Db/Operations/Core/MinId.hs rename to cardano-db/src/Cardano/Db/Operations/Other/MinId.hs index 127f7e0a1..3c03942a7 100644 --- a/cardano-db/src/Cardano/Db/Operations/Core/MinId.hs +++ b/cardano-db/src/Cardano/Db/Operations/Other/MinId.hs @@ -8,9 +8,9 @@ {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE NoImplicitPrelude #-} -module Cardano.Db.Operations.Core.MinId where +module Cardano.Db.Operations.Other.MinId where -import Cardano.Db.Operations.Core.Query (queryMinRefId) +import Cardano.Db.Operations.Query (queryMinRefId) import Cardano.Db.Operations.Types (MaTxOutFields (..), TxOutFields (..), TxOutTableType (..)) import Cardano.Db.Schema.BaseSchema import qualified Cardano.Db.Schema.Core.TxOut as C diff --git a/cardano-db/src/Cardano/Db/Operations/Core/Query.hs b/cardano-db/src/Cardano/Db/Operations/Query.hs similarity index 99% rename from cardano-db/src/Cardano/Db/Operations/Core/Query.hs rename to cardano-db/src/Cardano/Db/Operations/Query.hs index 0ec15a8df..7b8934455 100644 --- a/cardano-db/src/Cardano/Db/Operations/Core/Query.hs +++ b/cardano-db/src/Cardano/Db/Operations/Query.hs @@ -4,7 +4,7 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} -module Cardano.Db.Operations.Core.Query ( +module Cardano.Db.Operations.Query ( LookupFail (..), -- queries used by db-sync queryBlockCount, @@ -103,7 +103,7 @@ module Cardano.Db.Operations.Core.Query ( ) where import Cardano.Db.Error -import Cardano.Db.Operations.Core.QueryHelper (defaultUTCTime, isJust, maybeToEither, unValue2, unValue3, unValue5, unValueSumAda) +import Cardano.Db.Operations.QueryHelper (defaultUTCTime, isJust, maybeToEither, unValue2, unValue3, unValue5, unValueSumAda) import Cardano.Db.Schema.BaseSchema import Cardano.Db.Types import Cardano.Ledger.BaseTypes (CertIx (..), TxIx (..)) diff --git a/cardano-db/src/Cardano/Db/Operations/Core/QueryHelper.hs b/cardano-db/src/Cardano/Db/Operations/QueryHelper.hs similarity index 98% rename from cardano-db/src/Cardano/Db/Operations/Core/QueryHelper.hs rename to cardano-db/src/Cardano/Db/Operations/QueryHelper.hs index b16dfa9df..64da0a70f 100644 --- a/cardano-db/src/Cardano/Db/Operations/Core/QueryHelper.hs +++ b/cardano-db/src/Cardano/Db/Operations/QueryHelper.hs @@ -3,7 +3,7 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} -module Cardano.Db.Operations.Core.QueryHelper where +module Cardano.Db.Operations.QueryHelper where import Cardano.Db.Schema.BaseSchema import Cardano.Db.Types diff --git a/cardano-db/src/Cardano/Db/Operations/Variant/TxOutDelete.hs b/cardano-db/src/Cardano/Db/Operations/TxOut/TxOutDelete.hs similarity index 81% rename from cardano-db/src/Cardano/Db/Operations/Variant/TxOutDelete.hs rename to cardano-db/src/Cardano/Db/Operations/TxOut/TxOutDelete.hs index 39e714d14..f17328aa4 100644 --- a/cardano-db/src/Cardano/Db/Operations/Variant/TxOutDelete.hs +++ b/cardano-db/src/Cardano/Db/Operations/TxOut/TxOutDelete.hs @@ -3,7 +3,7 @@ {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} -module Cardano.Db.Operations.Variant.TxOutDelete where +module Cardano.Db.Operations.TxOut.TxOutDelete where import Cardano.Db.Operations.Types (TxOutTableType (..)) import qualified Cardano.Db.Schema.Core.TxOut as C @@ -28,12 +28,10 @@ deleteCoreTxOutTablesAfterTxId mtxOutId mmaTxOutId = do whenJust mmaTxOutId $ \maTxOutId -> deleteWhere [C.MaTxOutId >=. maTxOutId] whenJust mtxOutId $ \txOutId -> deleteWhere [C.TxOutId >=. txOutId] --- TODO: cmdv: probably won't need to remove the addressId here but have it just incase -deleteVariantTxOutTablesAfterTxId :: MonadIO m => Maybe V.TxOutId -> Maybe V.MaTxOutId -> Maybe V.AddressId -> ReaderT SqlBackend m () -deleteVariantTxOutTablesAfterTxId mtxOutId mmaTxOutId mAddrId = do +deleteVariantTxOutTablesAfterTxId :: MonadIO m => Maybe V.TxOutId -> Maybe V.MaTxOutId -> ReaderT SqlBackend m () +deleteVariantTxOutTablesAfterTxId mtxOutId mmaTxOutId = do whenJust mmaTxOutId $ \maTxOutId -> deleteWhere [V.MaTxOutId >=. maTxOutId] whenJust mtxOutId $ \txOutId -> deleteWhere [V.TxOutId >=. txOutId] - whenJust mAddrId $ \addrId -> deleteWhere [V.AddressId >=. addrId] deleteTxOut :: MonadIO m => TxOutTableType -> ReaderT SqlBackend m Int64 deleteTxOut = \case diff --git a/cardano-db/src/Cardano/Db/Operations/Variant/TxOutInsert.hs b/cardano-db/src/Cardano/Db/Operations/TxOut/TxOutInsert.hs similarity index 96% rename from cardano-db/src/Cardano/Db/Operations/Variant/TxOutInsert.hs rename to cardano-db/src/Cardano/Db/Operations/TxOut/TxOutInsert.hs index f33a6b243..ad9cb1239 100644 --- a/cardano-db/src/Cardano/Db/Operations/Variant/TxOutInsert.hs +++ b/cardano-db/src/Cardano/Db/Operations/TxOut/TxOutInsert.hs @@ -5,9 +5,9 @@ {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -Wno-deferred-out-of-scope-variables #-} -module Cardano.Db.Operations.Variant.TxOutInsert where +module Cardano.Db.Operations.TxOut.TxOutInsert where -import Cardano.Db.Operations.Core.Insert (insertMany', insertUnchecked) +import Cardano.Db.Operations.Insert (insertMany', insertUnchecked) import Cardano.Db.Operations.Types (MaTxOutIdW (..), MaTxOutW (..), TxOutIdW (..), TxOutW (..)) import qualified Cardano.Db.Schema.Core.TxOut as C import qualified Cardano.Db.Schema.Variant.TxOut as V diff --git a/cardano-db/src/Cardano/Db/Operations/Variant/TxOutQuery.hs b/cardano-db/src/Cardano/Db/Operations/TxOut/TxOutQuery.hs similarity index 99% rename from cardano-db/src/Cardano/Db/Operations/Variant/TxOutQuery.hs rename to cardano-db/src/Cardano/Db/Operations/TxOut/TxOutQuery.hs index f78cf93dd..d06f6c0df 100644 --- a/cardano-db/src/Cardano/Db/Operations/Variant/TxOutQuery.hs +++ b/cardano-db/src/Cardano/Db/Operations/TxOut/TxOutQuery.hs @@ -11,10 +11,10 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} -module Cardano.Db.Operations.Variant.TxOutQuery where +module Cardano.Db.Operations.TxOut.TxOutQuery where import Cardano.Db.Error (LookupFail (..)) -import Cardano.Db.Operations.Core.QueryHelper (isJust, maybeToEither, txLessEqual, unValue2, unValue3, unValueSumAda) +import Cardano.Db.Operations.QueryHelper (isJust, maybeToEither, txLessEqual, unValue2, unValue3, unValueSumAda) import Cardano.Db.Operations.Types (TxOutFields (..), TxOutIdW (..), TxOutTableType (..), TxOutW (..), UtxoQueryResult (..)) import Cardano.Db.Schema.BaseSchema import qualified Cardano.Db.Schema.Core.TxOut as C diff --git a/cardano-db/src/Cardano/Db/Operations/Types.hs b/cardano-db/src/Cardano/Db/Operations/Types.hs index 98d60cbf7..7753742fd 100644 --- a/cardano-db/src/Cardano/Db/Operations/Types.hs +++ b/cardano-db/src/Cardano/Db/Operations/Types.hs @@ -213,3 +213,11 @@ convertMaTxOutIdVariant = mapMaybe unwrapVariant where unwrapVariant (VMaTxOutIdW maTxOutId) = Just maTxOutId unwrapVariant _ = Nothing + +isTxOutCore :: TxOutTableType -> Bool +isTxOutCore TxOutCore = True +isTxOutCore TxOutVariantAddress = False + +isTxOutVariantAddress :: TxOutTableType -> Bool +isTxOutVariantAddress TxOutVariantAddress = True +isTxOutVariantAddress TxOutCore = False diff --git a/cardano-db/src/Cardano/Db/Schema/CoreSchema.hs b/cardano-db/src/Cardano/Db/Schema/CoreSchema.hs deleted file mode 100644 index 8ab13404a..000000000 --- a/cardano-db/src/Cardano/Db/Schema/CoreSchema.hs +++ /dev/null @@ -1 +0,0 @@ -module Cardano.Db.Schema.CoreSchema where diff --git a/cardano-db/src/Cardano/Db/Types.hs b/cardano-db/src/Cardano/Db/Types.hs index 2567372d3..8735983fd 100644 --- a/cardano-db/src/Cardano/Db/Types.hs +++ b/cardano-db/src/Cardano/Db/Types.hs @@ -4,6 +4,7 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} @@ -23,14 +24,15 @@ module Cardano.Db.Types ( CertNo (..), PoolCert (..), ExtraMigration (..), + MigrationValues (..), VoteUrl (..), VoteMetaHash (..), Vote (..), VoterRole (..), GovActionType (..), BootstrapState (..), + processMigrationValues, isStakeDistrComplete, - wasPruneTxOutPreviouslySet, bootstrapState, extraDescription, deltaCoinToDbInt65, @@ -196,14 +198,32 @@ data ExtraMigration | PruneTxOutFlagPreviouslySet | BootstrapStarted | BootstrapFinished + | ConsumeTxOutPreviouslySet + | TxOutAddressPreviouslySet deriving (Eq, Show, Read) +data MigrationValues = MigrationValues + { isStakeDistrEnded :: !Bool + , isPruneTxOutPreviouslySet :: !Bool + , isConsumeTxOutPreviouslySet :: !Bool + , isTxOutAddressPreviouslySet :: !Bool + , pruneConsumeMigration :: !PruneConsumeMigration + } + deriving (Eq, Show) + +processMigrationValues :: [ExtraMigration] -> PruneConsumeMigration -> MigrationValues +processMigrationValues migrations pcm = + MigrationValues + { isStakeDistrEnded = StakeDistrEnded `elem` migrations + , isPruneTxOutPreviouslySet = PruneTxOutFlagPreviouslySet `elem` migrations + , isConsumeTxOutPreviouslySet = ConsumeTxOutPreviouslySet `elem` migrations + , isTxOutAddressPreviouslySet = TxOutAddressPreviouslySet `elem` migrations + , pruneConsumeMigration = pcm + } + isStakeDistrComplete :: [ExtraMigration] -> Bool isStakeDistrComplete = elem StakeDistrEnded -wasPruneTxOutPreviouslySet :: [ExtraMigration] -> Bool -wasPruneTxOutPreviouslySet = elem PruneTxOutFlagPreviouslySet - data BootstrapState = BootstrapNotStarted | BootstrapInProgress @@ -221,6 +241,7 @@ data PruneConsumeMigration = PruneConsumeMigration , -- we make the assumption that if the user is using prune flag -- they will also want consume automatically set for them. pcmConsumeOrPruneTxOut :: Bool + , pcmConsumedTxOut :: Bool , pcmSkipTxIn :: Bool } deriving (Eq, Show) @@ -236,6 +257,10 @@ extraDescription = \case "The bootstrap syncing is in progress" BootstrapFinished -> "The bootstrap is finalised" + ConsumeTxOutPreviouslySet -> + "The --consume-tx-out flag has previously been enabled" + TxOutAddressPreviouslySet -> + "The addition of a Address table for TxOuts was previously set" instance Ord PoolCert where compare a b = compare (pcCertNo a) (pcCertNo b) diff --git a/cardano-db/test/schema-rollback.hs b/cardano-db/test/schema-rollback.hs index ccecf4127..5e5ed7bef 100644 --- a/cardano-db/test/schema-rollback.hs +++ b/cardano-db/test/schema-rollback.hs @@ -41,7 +41,7 @@ main = do findTablesWithBlockNo :: IO [ByteString] findTablesWithBlockNo = do - xs <- mapMaybe removeCommentsAndEmpty . getSchema <$> BS.readFile "cardano-db/src/Cardano/Db/Schema.hs" + xs <- mapMaybe removeCommentsAndEmpty . getSchema <$> BS.readFile "cardano-db/src/Cardano/Db/Schema/BaseSchema.hs" when (length xs < 10) $ error $ "Expected at least 10 lines of schema definition, but got only " ++ show (length xs) @@ -91,7 +91,7 @@ findTablesWithDelete = . mapMaybe getTableName . mapMaybe removeCommentsAndEmpty . getDeleteAfterBlockNo - <$> BS.readFile "cardano-db/src/Cardano/Db/Operations/Core/Delete.hs" + <$> BS.readFile "cardano-db/src/Cardano/Db/Operations/Delete.hs" where getDeleteAfterBlockNo :: ByteString -> [ByteString] getDeleteAfterBlockNo = diff --git a/doc/interesting-queries.md b/doc/interesting-queries.md index b15c67946..2b3e7ec75 100644 --- a/doc/interesting-queries.md +++ b/doc/interesting-queries.md @@ -629,4 +629,4 @@ them. --- -[Query.hs]: https://github.com/IntersectMBO/cardano-db-sync/blob/master/cardano-db/src/Cardano/Db/Operations/Core/Query.hs +[Query.hs]: https://github.com/IntersectMBO/cardano-db-sync/blob/master/cardano-db/src/Cardano/Db/Operations/Query.hs diff --git a/flake.nix b/flake.nix index 87b676cf8..9fd272eb9 100644 --- a/flake.nix +++ b/flake.nix @@ -217,7 +217,7 @@ packages.cardano-db.package.extraSrcFiles = ["../config/pgpass-testnet"]; packages.cardano-db.components.tests.schema-rollback.extraSrcFiles = - [ "src/Cardano/Db/Schema.hs" "src/Cardano/Db/Operations/Core/Delete.hs" ]; + [ "src/Cardano/Db/Schema.hs" "src/Cardano/Db/Operations/Delete.hs" ]; packages.cardano-db.components.tests.test-db.extraSrcFiles = [ "../config/pgpass-mainnet" ]; packages.cardano-chain-gen.package.extraSrcFiles = diff --git a/schema/migration-2-0044-20240912.sql b/schema/migration-2-0044-20240912.sql new file mode 100644 index 000000000..6ca15ae24 --- /dev/null +++ b/schema/migration-2-0044-20240912.sql @@ -0,0 +1,21 @@ +-- Persistent generated migration. + +CREATE FUNCTION migrate() RETURNS void AS $$ +DECLARE + next_version int ; +BEGIN + SELECT stage_two + 1 INTO next_version FROM schema_version ; + IF next_version = 44 THEN + -- EXECUTE 'ALTER TABLE "reward" ADD COLUMN "id" INT8 NOT NULL' ; + -- EXECUTE 'ALTER TABLE "reward_rest" ADD COLUMN "id" INT8 NOT NULL' ; + EXECUTE 'ALTER TABLE "tx_out" ADD COLUMN "consumed_by_tx_id" INT8 NULL' ; + -- Hand written SQL statements can be added here. + UPDATE schema_version SET stage_two = next_version ; + RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; + END IF ; +END ; +$$ LANGUAGE plpgsql ; + +SELECT migrate() ; + +DROP FUNCTION migrate() ; diff --git a/scripts/run-everything-tmux.sh b/scripts/run-everything-tmux.sh new file mode 100755 index 000000000..a4de0dab6 --- /dev/null +++ b/scripts/run-everything-tmux.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +HOMEIOG=$HOME/Code/IOG + +dbsync="$(find $HOMEIOG/cardano-db-sync/ -name cardano-db-sync -type f)" + +session="IOHK" + +# Check if the session exists, discarding output +# We can check $? for the exit status (zero for success, non-zero for failure) +tmux has-session -t $session 2>/dev/null + +# if there is a session named IOHK then kill it +if [ $? = 1 ]; then + tmux kill-session -t $session + killall cardano-node +fi + +tmux new-session -d -s $session + +tmux rename-window $session +tmux split-window -h +# tmux split-window -v +# tmux split-window -v +# tmux select-layout tiled + +# Cardano Node +tmux send-keys -t 0 "cd $HOMEIOG/cardano-node/" 'C-m' +tmux send-keys -t 0 "cardano-node run --config $HOMEIOG/testnet/config.json --database-path $HOMEIOG/testnet/db/ --socket-path $HOMEIOG/testnet/db/node.socket --host-addr 0.0.0.0 --port 1337 --topology $HOMEIOG/testnet/topology.json" 'C-m' + +# Cardano DB-Sync +tmux send-keys -t 1 "cd $HOMEIOG/cardano-db-sync/" 'C-m'; sleep 3 +tmux send-keys -t 1 "export PGPASSFILE=$HOMEIOG/cardano-db-sync/config/pgpass-mainnet" 'C-m'; sleep 2 +# tmux send-keys -t 1 "$dbsync --config $HOMEIOG/testnet/db-sync-config.json --socket-path $HOMEIOG/testnet/db/node.socket --state-dir $HOMEIOG/testnet/ledger-state --schema-dir $HOMEIOG/cardano-db-sync/schema/ +RTS -p -hc -L200 -RTS" 'C-m' +tmux send-keys -t 1 "PGPASSFILE=$HOMEIOG/cardano-db-sync/config/pgpass-mainnet $dbsync --config $HOMEIOG/testnet/db-sync-config.json --socket-path $HOMEIOG/testnet/db/node.socket --state-dir $HOMEIOG/testnet/ledger-state --schema-dir $HOMEIOG/cardano-db-sync/schema/" 'C-m' + +tmux send-keys -t 0 "cd $HOMEIOG/" 'C-m' + +tmux attach-session -t $session