Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1333 - add new AddressDetail table for TxOut #1820

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ module Test.Cardano.Db.Mock.Config (
withCustomConfigAndLogs,
withFullConfig',
replaceConfigFile,
txOutTableTypeFromConfig,
) where

import Cardano.Api (NetworkMagic (..))
import qualified Cardano.Db as Db
import qualified Cardano.Db as DB
import Cardano.DbSync
import Cardano.DbSync.Config
import Cardano.DbSync.Config.Cardano
Expand Down Expand Up @@ -224,16 +225,16 @@ pollDBSync env = do
withDBSyncEnv :: IO DBSyncEnv -> (DBSyncEnv -> IO a) -> IO a
withDBSyncEnv mkEnv = bracket mkEnv stopDBSyncIfRunning

getDBSyncPGPass :: DBSyncEnv -> Db.PGPassSource
getDBSyncPGPass :: DBSyncEnv -> DB.PGPassSource
getDBSyncPGPass = enpPGPassSource . dbSyncParams

queryDBSync :: DBSyncEnv -> ReaderT SqlBackend (NoLoggingT IO) a -> IO a
queryDBSync env = Db.runWithConnectionNoLogging (getDBSyncPGPass env)
queryDBSync env = DB.runWithConnectionNoLogging (getDBSyncPGPass env)

getPoolLayer :: DBSyncEnv -> IO PoolDataLayer
getPoolLayer env = do
pgconfig <- runOrThrowIO $ Db.readPGPass (enpPGPassSource $ dbSyncParams env)
pool <- runNoLoggingT $ createPostgresqlPool (Db.toConnectionString pgconfig) 1 -- Pool size of 1 for tests
pgconfig <- runOrThrowIO $ DB.readPGPass (enpPGPassSource $ dbSyncParams env)
pool <- runNoLoggingT $ createPostgresqlPool (DB.toConnectionString pgconfig) 1 -- Pool size of 1 for tests
pure $
postgresqlPoolDataLayer
nullTracer
Expand Down Expand Up @@ -274,15 +275,15 @@ mkShelleyCredentials bulkFile = do
-- | staticDir can be shared by tests running in parallel. mutableDir not.
mkSyncNodeParams :: FilePath -> FilePath -> CommandLineArgs -> IO SyncNodeParams
mkSyncNodeParams staticDir mutableDir CommandLineArgs {..} = do
pgconfig <- runOrThrowIO Db.readPGPassDefault
pgconfig <- runOrThrowIO DB.readPGPassDefault

pure $
SyncNodeParams
{ enpConfigFile = mkConfigFile staticDir claConfigFilename
, enpSocketPath = SocketPath $ mutableDir </> ".socket"
, enpMaybeLedgerStateDir = Just $ LedgerStateDir $ mutableDir </> "ledger-states"
, enpMigrationDir = MigrationDir "../schema"
, enpPGPassSource = Db.PGPassCached pgconfig
, enpPGPassSource = DB.PGPassCached pgconfig
, enpEpochDisabled = claEpochDisabled
, enpHasCache = claHasCache
, enpSkipFix = claSkipFix
Expand All @@ -301,21 +302,21 @@ mkConfigFile :: FilePath -> FilePath -> ConfigFile
mkConfigFile staticDir cliConfigFilename =
ConfigFile $ staticDir </> cliConfigFilename

configPruneForceTxIn :: SyncNodeConfig -> SyncNodeConfig
configPruneForceTxIn cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutPrune (ForceTxIn True)}}
configPruneForceTxIn :: Bool -> SyncNodeConfig -> SyncNodeConfig
configPruneForceTxIn useTxOutAddress cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutConsumedPrune (ForceTxIn True) (UseTxOutAddress useTxOutAddress)}}

configPrune :: SyncNodeConfig -> SyncNodeConfig
configPrune cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutPrune (ForceTxIn False)}}
configPrune :: Bool -> SyncNodeConfig -> SyncNodeConfig
configPrune useTxOutAddress cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutConsumedPrune (ForceTxIn False) (UseTxOutAddress useTxOutAddress)}}

configConsume :: SyncNodeConfig -> SyncNodeConfig
configConsume cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutConsumed (ForceTxIn False)}}
configConsume :: Bool -> SyncNodeConfig -> SyncNodeConfig
configConsume useTxOutAddress cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutConsumed (ForceTxIn False) (UseTxOutAddress useTxOutAddress)}}

configBootstrap :: SyncNodeConfig -> SyncNodeConfig
configBootstrap cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutBootstrap (ForceTxIn False)}}
configBootstrap :: Bool -> SyncNodeConfig -> SyncNodeConfig
configBootstrap useTxOutAddress cfg = do
cfg {dncInsertOptions = (dncInsertOptions cfg) {sioTxOut = TxOutConsumedBootstrap (ForceTxIn False) (UseTxOutAddress useTxOutAddress)}}

configPlutusDisable :: SyncNodeConfig -> SyncNodeConfig
configPlutusDisable cfg = do
Expand Down Expand Up @@ -574,12 +575,12 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
-- we dont fork dbsync here. Just prepare it as an action
withDBSyncEnv (mkDBSyncEnv dbsyncParams syncNodeConfig partialDbSyncRun) $ \dbSyncEnv -> do
let pgPass = getDBSyncPGPass dbSyncEnv
tableNames <- Db.getAllTablleNames pgPass
tableNames <- DB.getAllTablleNames pgPass
-- We only want to create the table schema once for the tests so here we check
-- if there are any table names.
if null tableNames || shouldDropDB
then void . hSilence [stderr] $ Db.recreateDB pgPass
else void . hSilence [stderr] $ Db.truncateTables pgPass tableNames
then void . hSilence [stderr] $ DB.recreateDB pgPass
else void . hSilence [stderr] $ DB.truncateTables pgPass tableNames
action interpreter mockServer dbSyncEnv
where
mutableDir = mkMutableDir testLabelFilePath
Expand All @@ -605,3 +606,15 @@ replaceConfigFile newFilename dbSync@DBSyncEnv {..} = do
configDir = mkConfigDir . takeDirectory . unConfigFile . enpConfigFile $ dbSyncParams
newParams =
dbSyncParams {enpConfigFile = ConfigFile $ configDir </> newFilename}

txOutTableTypeFromConfig :: DBSyncEnv -> DB.TxOutTableType
txOutTableTypeFromConfig dbSyncEnv =
case sioTxOut $ dncInsertOptions $ dbSyncConfig dbSyncEnv of
TxOutDisable -> DB.TxOutCore
TxOutEnable useTxOutAddress -> getTxOutTT useTxOutAddress
TxOutConsumed _ useTxOutAddress -> getTxOutTT useTxOutAddress
TxOutConsumedPrune _ useTxOutAddress -> getTxOutTT useTxOutAddress
TxOutConsumedBootstrap _ useTxOutAddress -> getTxOutTT useTxOutAddress
where
getTxOutTT :: UseTxOutAddress -> DB.TxOutTableType
getTxOutTT value = if unUseTxOutAddress value then DB.TxOutVariantAddress else DB.TxOutCore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ module Test.Cardano.Db.Mock.Unit.Alonzo.Plutus (
) where

import qualified Cardano.Crypto.Hash as Crypto
import Cardano.Db (TxOutTableType (..))
import qualified Cardano.Db as DB
import qualified Cardano.Db.Schema.Core.TxOut as C
import qualified Cardano.Db.Schema.Variant.TxOut as V
import Cardano.DbSync.Era.Shelley.Generic.Util (renderAddress)
import Cardano.Ledger.Coin
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..), PolicyID (..))
Expand Down Expand Up @@ -90,10 +93,24 @@ simpleScript =
Alonzo.mkLockByScriptTx (UTxOIndex 0) [True] 20000 20000

assertBlockNoBackoff dbSync (fromIntegral $ length a + 2)
assertEqQuery dbSync (fmap getOutFields <$> DB.queryScriptOutputs) [expectedFields] "Unexpected script outputs"
assertEqQuery dbSync (fmap getOutFields <$> DB.queryScriptOutputs TxOutCore) [expectedFields] "Unexpected script outputs"
where
testLabel = "simpleScript-alonzo"
getOutFields txOut = (DB.txOutAddress txOut, DB.txOutAddressHasScript txOut, DB.txOutValue txOut, DB.txOutDataHash txOut)
getOutFields txOutW = case txOutW of
DB.CTxOutW txOut ->
( C.txOutAddress txOut
, C.txOutAddressHasScript txOut
, C.txOutValue txOut
, C.txOutDataHash txOut
)
DB.VTxOutW txout mAddress -> case mAddress of
Just address ->
( V.addressAddress address
, V.addressHasScript address
, V.txOutValue txout
, V.txOutDataHash txout
)
Nothing -> error "AlonzoSimpleScript: expected an address"
expectedFields =
( renderAddress alwaysSucceedsScriptAddr
, True
Expand Down
20 changes: 17 additions & 3 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ unitTests iom knownMigrations =
[ testCase "default insert config" Config.defaultInsertConfig
, testCase "insert config" Config.insertConfig
, testGroup
"consumed-tx-out and prune-tx-out"
[ test "flag check" MigrateConsumedPruneTxOut.txConsumedColumnCheck
, test "basic prune" MigrateConsumedPruneTxOut.basicPrune
"tx-out"
[ test "basic prune" MigrateConsumedPruneTxOut.basicPrune
, test "basic prune with address table" MigrateConsumedPruneTxOut.basicPruneWithAddress
, test "prune with simple rollback" MigrateConsumedPruneTxOut.pruneWithSimpleRollback
, test "prune with full tx rollback" MigrateConsumedPruneTxOut.pruneWithFullTxRollback
, test "pruning should keep some tx" MigrateConsumedPruneTxOut.pruningShouldKeepSomeTx
Expand All @@ -48,6 +48,20 @@ unitTests iom knownMigrations =
, expectFailSilent "set prune flag, restart missing prune flag" $ MigrateConsumedPruneTxOut.pruneRestartMissingFlag iom knownMigrations
, expectFailSilent "set bootstrap flag, restart missing bootstrap flag" $ MigrateConsumedPruneTxOut.bootstrapRestartMissingFlag iom knownMigrations
]
, testGroup
"tx-out using Address table"
[ test "basic prune with address table" MigrateConsumedPruneTxOut.basicPruneWithAddress
, test "prune with simple rollback with address table" MigrateConsumedPruneTxOut.pruneWithSimpleRollbackWithAddress
, test "prune with full tx rollback with address table" MigrateConsumedPruneTxOut.pruneWithFullTxRollbackWithAddress
, test "pruning should keep some tx with address table" MigrateConsumedPruneTxOut.pruningShouldKeepSomeTxWithAddress
, test "prune and rollback one block with address table" MigrateConsumedPruneTxOut.pruneAndRollBackOneBlockWithAddress
, test "no pruning and rollback with address table" MigrateConsumedPruneTxOut.noPruneAndRollBackWithAddress
, test "prune same block with address table" MigrateConsumedPruneTxOut.pruneSameBlockWithAddress
, test "no pruning same block with address table" MigrateConsumedPruneTxOut.noPruneSameBlockWithAddress
, expectFailSilent "restart with new consumed set to false, with address table" $ MigrateConsumedPruneTxOut.migrateAndPruneRestartWithAddress iom knownMigrations
, expectFailSilent "set prune flag, restart missing prune flag, with address table" $ MigrateConsumedPruneTxOut.pruneRestartMissingFlagWithAddress iom knownMigrations
, expectFailSilent "set bootstrap flag, restart missing bootstrap flag, with address table" $ MigrateConsumedPruneTxOut.bootstrapRestartMissingFlagWithAddress iom knownMigrations
]
]
, testGroup
"simple"
Expand Down
Loading
Loading