Skip to content

Commit 56fd3e6

Browse files
committed
test: Add tx_out.consumed_by_tx tests
1 parent 0f1d93f commit 56fd3e6

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ test-suite cardano-chain-gen
157157
Test.Cardano.Db.Mock.Unit.Conway.Config.JsonbInSchema
158158
Test.Cardano.Db.Mock.Unit.Conway.Config.Parse
159159
Test.Cardano.Db.Mock.Unit.Conway.Config.MigrateConsumedPruneTxOut
160+
Test.Cardano.Db.Mock.Unit.Conway.Config.TxOutConsumed
160161
Test.Cardano.Db.Mock.Unit.Conway.Governance
161162
Test.Cardano.Db.Mock.Unit.Conway.InlineAndReference
162163
Test.Cardano.Db.Mock.Unit.Conway.Other

cardano-chain-gen/src/Cardano/Mock/Query.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE DataKinds #-}
13
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE KindSignatures #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
26
{-# LANGUAGE TypeApplications #-}
37

48
module Cardano.Mock.Query (
@@ -17,6 +21,7 @@ module Cardano.Mock.Query (
1721
queryEpochStateCount,
1822
queryCommitteeByTxHash,
1923
queryCommitteeMemberCountByTxHash,
24+
queryConsumedTxOutCount,
2025
) where
2126

2227
import qualified Cardano.Db as Db
@@ -270,3 +275,15 @@ queryCommitteeMemberCountByTxHash txHash = do
270275
pure countRows
271276

272277
pure (maybe 0 unValue res)
278+
279+
queryConsumedTxOutCount ::
280+
forall (a :: Db.TxOutTableType) io.
281+
(MonadIO io, Db.TxOutFields a) =>
282+
ReaderT SqlBackend io Word64
283+
queryConsumedTxOutCount = do
284+
res <- selectOne $ do
285+
txOut <- from $ table @(Db.TxOutTable a)
286+
where_ (not_ $ isNothing_ $ txOut ^. Db.txOutConsumedByTxIdField @a)
287+
pure countRows
288+
289+
pure $ maybe 0 unValue res

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import qualified Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.EpochDisabled a
77
import qualified Test.Cardano.Db.Mock.Unit.Conway.Config.JsonbInSchema as Config
88
import qualified Test.Cardano.Db.Mock.Unit.Conway.Config.MigrateConsumedPruneTxOut as MigrateConsumedPruneTxOut
99
import qualified Test.Cardano.Db.Mock.Unit.Conway.Config.Parse as Config
10+
import qualified Test.Cardano.Db.Mock.Unit.Conway.Config.TxOutConsumed as TxOutConsumed
1011
import qualified Test.Cardano.Db.Mock.Unit.Conway.Governance as Governance
1112
import qualified Test.Cardano.Db.Mock.Unit.Conway.InlineAndReference as InlineRef
1213
import qualified Test.Cardano.Db.Mock.Unit.Conway.Other as Other
@@ -85,6 +86,11 @@ unitTests iom knownMigrations =
8586
"populate db then reset with use_address_table config config active"
8687
$ MigrateConsumedPruneTxOut.populateDbRestartWithAddressConfig iom knownMigrations
8788
]
89+
, testGroup
90+
"tx-out consumed by tx id"
91+
[ test "without txout-consumed" TxOutConsumed.consumeTx
92+
, test "with txout-consumed" TxOutConsumed.consumeTxConsumed
93+
]
8894
]
8995
, testGroup
9096
"simple"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NumericUnderscores #-}
3+
{-# LANGUAGE TypeApplications #-}
4+
5+
module Test.Cardano.Db.Mock.Unit.Conway.Config.TxOutConsumed (
6+
consumeTx,
7+
consumeTxConsumed,
8+
) where
9+
10+
import qualified Cardano.Db as Db
11+
import Cardano.Mock.ChainSync.Server (IOManager ())
12+
import Cardano.Mock.Forging.Interpreter (withConwayLedgerState)
13+
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
14+
import Cardano.Mock.Forging.Types (ForgingError (..), UTxOIndex (..))
15+
import qualified Cardano.Mock.Query as Query
16+
import Cardano.Prelude
17+
import Test.Cardano.Db.Mock.Config
18+
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
19+
import Test.Cardano.Db.Mock.Validate
20+
import Test.Tasty.HUnit (Assertion ())
21+
import Prelude ()
22+
23+
consumeTx :: IOManager -> [(Text, Text)] -> Assertion
24+
consumeTx =
25+
withFullConfigAndDropDB conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
26+
startDBSync dbSync
27+
28+
-- Forge a payment transaction
29+
tx0 <-
30+
withConwayLedgerState interpreter $
31+
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 20_000 20_000 0
32+
33+
-- Forge a block with a transaction
34+
void $ Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ -> Right tx0
35+
void $ Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \state' -> do
36+
utxo' <- maybeToEither CantFindUTxO (head $ Conway.mkUTxOConway tx0)
37+
Conway.mkPaymentTx (UTxOPair utxo') (UTxOIndex 2) 10_000 500 0 state'
38+
39+
-- Verify the new transaction count
40+
assertBlockNoBackoff dbSync 2
41+
42+
-- Should not have consumed_by_tx
43+
assertEqBackoff
44+
dbSync
45+
(Query.queryConsumedTxOutCount @'Db.TxOutCore)
46+
0
47+
[]
48+
"Unexpected consumed_by_tx count"
49+
where
50+
testLabel = "conwayConsumeTx"
51+
52+
consumeTxConsumed :: IOManager -> [(Text, Text)] -> Assertion
53+
consumeTxConsumed =
54+
withCustomConfig cmdLineArgs cfg conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
55+
startDBSync dbSync
56+
57+
-- Forge a payment transaction
58+
tx0 <-
59+
withConwayLedgerState interpreter $
60+
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 20_000 20_000 0
61+
62+
-- Forge a block with a transaction
63+
void $ Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ -> Right tx0
64+
void $ Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \state' -> do
65+
utxo' <- maybeToEither CantFindUTxO (head $ Conway.mkUTxOConway tx0)
66+
Conway.mkPaymentTx (UTxOPair utxo') (UTxOIndex 2) 10_000 500 0 state'
67+
68+
-- Wait for it to sync
69+
assertBlockNoBackoff dbSync 2
70+
assertTxCount dbSync 13
71+
72+
-- Should have consumed_by_tx
73+
assertEqBackoff
74+
dbSync
75+
(Query.queryConsumedTxOutCount @'Db.TxOutCore)
76+
2
77+
[]
78+
"Unexpected consumed_by_tx count"
79+
where
80+
cmdLineArgs = initCommandLineArgs
81+
cfg = Just (configConsume False)
82+
testLabel = "conwayConsumeTxConsumed"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12,16]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[12,16]

0 commit comments

Comments
 (0)