Skip to content

Commit

Permalink
Add variant for TxOut
Browse files Browse the repository at this point in the history
Modified-by: Cmdv <vincent@cmdv.me>
  • Loading branch information
Cmdv committed Sep 9, 2024
1 parent 97930bd commit 421611f
Show file tree
Hide file tree
Showing 78 changed files with 3,020 additions and 2,361 deletions.
34 changes: 24 additions & 10 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ 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
import Cardano.DbSync.Config.Types (SyncInsertOptions (..), TxOutConfig (..), UseTxOutAddress (..))
import Cardano.DbSync.Error (runOrThrowIO)
import Cardano.DbSync.Types (CardanoBlock, MetricSetters (..))
import Cardano.Mock.ChainSync.Server
Expand Down Expand Up @@ -209,16 +211,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 @@ -259,15 +261,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 Down Expand Up @@ -503,12 +505,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 @@ -534,3 +536,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 @@ -34,7 +34,6 @@ insertConfig = do
, sioPoolStats = PoolStatsConfig False
, sioJsonType = JsonTypeDisable
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
, sioAddressDetail = AddressDetailConfig False
}

dncInsertOptions cfg @?= expected
Expand Down
23 changes: 20 additions & 3 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Plutus.hs
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,12 +93,26 @@ 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 =
( Just $ renderAddress alwaysSucceedsScriptAddr
( renderAddress alwaysSucceedsScriptAddr
, True
, DB.DbLovelace 20000
, Just $ Crypto.hashToBytes (extractHash $ hashData @StandardAlonzo plutusDataList)
Expand Down
Loading

0 comments on commit 421611f

Please sign in to comment.