88module Plutus.ChainIndex.HandlersSpec (tests ) where
99
1010import Control.Concurrent.STM (newTVarIO )
11- import Control.Lens
12- import Control.Monad (forM , forM_ )
11+ import Control.Lens ( view )
12+ import Control.Monad (forM )
1313import Control.Monad.Freer (Eff )
1414import Control.Monad.Freer.Extras.Beam (BeamEffect )
1515import Control.Monad.IO.Class (liftIO )
1616import Control.Tracer (nullTracer )
1717import Data.Default (def )
18- import Data.Set ( member )
18+ import Data.Set qualified as S
1919import Database.Beam.Migrate.Simple (autoMigrate )
2020import Database.Beam.Sqlite qualified as Sqlite
2121import Database.Beam.Sqlite.Migrate qualified as Sqlite
2222import Database.SQLite.Simple qualified as Sqlite
2323import Generators qualified as Gen
2424import Hedgehog (Property , assert , forAll , property , (===) )
25- import Ledger (Address ( Address , addressCredential ), TxOut ( TxOut , txOutAddress ), outValue )
25+ import Ledger (outValue )
2626import Plutus.ChainIndex (Page (pageItems ), PageQuery (PageQuery ), RunRequirements (.. ), appendBlock , citxOutputs ,
27- runChainIndexEffects , txFromTxId , utxoSetAtAddress , utxoSetWithCurrency )
27+ runChainIndexEffects , txFromTxId , utxoSetMembership , utxoSetWithCurrency )
2828import Plutus.ChainIndex.ChainIndexError (ChainIndexError (.. ))
2929import Plutus.ChainIndex.DbSchema (checkedSqliteDb )
3030import Plutus.ChainIndex.Effects (ChainIndexControlEffect , ChainIndexQueryEffect )
3131import Plutus.ChainIndex.Tx (_ValidTx , citxTxId )
32+ import Plutus.ChainIndex.Types (BlockProcessOption (.. ))
3233import Plutus.V1.Ledger.Ada qualified as Ada
3334import Plutus.V1.Ledger.Value (AssetClass (AssetClass ), flattenValue )
3435import Test.Tasty
3536import Test.Tasty.Hedgehog (testProperty )
37+ import Util (utxoSetFromBlockAddrs )
3638
3739tests :: TestTree
3840tests = do
@@ -47,6 +49,9 @@ tests = do
4749 [ testProperty " each txOutRef should be unspent" eachTxOutRefWithCurrencyShouldBeUnspentSpec
4850 , testProperty " should restrict to non-ADA currencies" cantRequestForTxOutRefsWithAdaSpec
4951 ]
52+ , testGroup " BlockProcessOption"
53+ [ testProperty " do not store txs" doNotStoreTxs
54+ ]
5055 ]
5156
5257-- | Tests we can correctly query a tx in the database using a tx id. We also
@@ -74,26 +79,16 @@ eachTxOutRefAtAddressShouldBeUnspentSpec :: Property
7479eachTxOutRefAtAddressShouldBeUnspentSpec = property $ do
7580 ((tip, block), state) <- forAll $ Gen. runTxGenState Gen. genNonEmptyBlock
7681
77- let addresses =
78- fmap (\ TxOut { txOutAddress = Address { addressCredential }} -> addressCredential)
79- $ view (traverse . citxOutputs . _ValidTx) block
80-
8182 result <- liftIO $ Sqlite. withConnection " :memory:" $ \ conn -> do
8283 Sqlite. runBeamSqlite conn $ autoMigrate Sqlite. migrationBackend checkedSqliteDb
8384 liftIO $ runChainIndex conn $ do
8485 -- Append the generated block in the chain index
8586 appendBlock tip block def
86-
87- forM addresses $ \ addr -> do
88- let pq = PageQuery 200 Nothing
89- (_, utxoRefs) <- utxoSetAtAddress pq addr
90- pure $ pageItems utxoRefs
87+ utxoSetFromBlockAddrs block
9188
9289 case result of
93- Left _ -> Hedgehog. assert False
94- Right utxoRefsGroups -> do
95- forM_ (concat utxoRefsGroups) $ \ utxoRef -> do
96- Hedgehog. assert $ utxoRef `member` view Gen. txgsUtxoSet state
90+ Left _ -> Hedgehog. assert False
91+ Right utxoGroups -> S. fromList (concat utxoGroups) === view Gen. txgsUtxoSet state
9792
9893-- | After generating and appending a block in the chain index, verify that
9994-- querying the chain index with each of the addresses in the block returns
@@ -120,10 +115,8 @@ eachTxOutRefWithCurrencyShouldBeUnspentSpec = property $ do
120115 pure $ pageItems utxoRefs
121116
122117 case result of
123- Left _ -> Hedgehog. assert False
124- Right utxoRefsGroups -> do
125- forM_ (concat utxoRefsGroups) $ \ utxoRef ->
126- Hedgehog. assert $ utxoRef `member` view Gen. txgsUtxoSet state
118+ Left _ -> Hedgehog. assert False
119+ Right utxoGroups -> S. fromList (concat utxoGroups) === view Gen. txgsUtxoSet state
127120
128121-- | Requesting UTXOs containing Ada should not return anything because every
129122-- transaction output must have a minimum of 1 ADA. So asking for UTXOs with ADA
@@ -146,6 +139,25 @@ cantRequestForTxOutRefsWithAdaSpec = property $ do
146139 Left _ -> Hedgehog. assert False
147140 Right utxoRefs -> Hedgehog. assert $ null utxoRefs
148141
142+ -- | Do not store txs through BlockProcessOption.
143+ -- The UTxO set must still be stored.
144+ -- But cannot be fetched through addresses as addresses are not stored.
145+ doNotStoreTxs :: Property
146+ doNotStoreTxs = property $ do
147+ ((tip, block), state) <- forAll $ Gen. runTxGenState Gen. genNonEmptyBlock
148+ result <- liftIO $ Sqlite. withConnection " :memory:" $ \ conn -> do
149+ Sqlite. runBeamSqlite conn $ autoMigrate Sqlite. migrationBackend checkedSqliteDb
150+ liftIO $ runChainIndex conn $ do
151+ appendBlock tip block BlockProcessOption {bpoStoreTxs= False }
152+ tx <- txFromTxId (view citxTxId (head block))
153+ utxosFromAddr <- utxoSetFromBlockAddrs block
154+ utxosStored <- traverse utxoSetMembership (S. toList (view Gen. txgsUtxoSet state))
155+ pure (tx, concat utxosFromAddr, utxosStored)
156+ case result of
157+ Right (Nothing , [] , utxosStored) -> Hedgehog. assert $ and (snd <$> utxosStored)
158+ _ -> Hedgehog. assert False
159+
160+ -- | Run a chain index action against a SQLite connection.
149161runChainIndex
150162 :: Sqlite. Connection
151163 -> Eff '[ ChainIndexQueryEffect
0 commit comments