|
| 1 | +-- | Utilities for comparing the @Spent.'SpentInfoEvent'@ indexer to cardano-db-sync results. |
| 2 | +module Test.Marconi.Cardano.DbSyncComparison.SpentInfoResult ( |
| 3 | + -- * Test builder |
| 4 | + mkSpentInfoEventAtQueryTest, |
| 5 | + |
| 6 | + -- ** Generating results from cardano-db-sync data |
| 7 | + -- $sql-query |
| 8 | +) where |
| 9 | + |
| 10 | +import Cardano.Api qualified as C |
| 11 | +import Cardano.Api.Extended.Streaming (BlockEvent (BlockEvent)) |
| 12 | +import Cardano.Api.Shelley qualified as C |
| 13 | +import Control.Exception (throwIO) |
| 14 | +import Control.Monad.Except (runExceptT) |
| 15 | +import Data.Aeson qualified as Aeson |
| 16 | +import Data.ByteString.Short qualified as BSS |
| 17 | +import Data.List.NonEmpty qualified as NEList |
| 18 | +import Marconi.Cardano.Core.Runner qualified as Runner |
| 19 | +import Marconi.Cardano.Indexers.Spent qualified as Spent |
| 20 | +import Marconi.Core qualified as Core |
| 21 | +import Marconi.Core.Indexer.SQLiteIndexer qualified as SQLiteIndexer |
| 22 | +import System.FilePath ((</>)) |
| 23 | +import Test.Marconi.Cardano.DbSyncComparison.Common ( |
| 24 | + DbSyncComparisonConfig (DbSyncComparisonConfig), |
| 25 | + eraToString, |
| 26 | + pathToGoldenFile, |
| 27 | + pathToOutFile, |
| 28 | + queryIndexerOnSnapshot, |
| 29 | + ) |
| 30 | +import Test.Tasty (TestTree) |
| 31 | +import Test.Tasty.Golden (goldenVsFileDiff) |
| 32 | + |
| 33 | +{- | Builds a golden test for the @Core.'EventAtQuery'@ for @Spent.'SpentInfoEvent'@ |
| 34 | +and compares it to manually generated results from `cardano-db-sync`. See documentation |
| 35 | +section "Generating results from cardano-db-sync data" for the SQL query run to generate |
| 36 | +golden files. |
| 37 | +-} |
| 38 | +mkSpentInfoEventAtQueryTest :: String -> DbSyncComparisonConfig -> TestTree |
| 39 | +mkSpentInfoEventAtQueryTest testName cfg@(DbSyncComparisonConfig nodeType era slotNo dbPath _ _) = |
| 40 | + goldenVsFileDiff |
| 41 | + testName |
| 42 | + (\expected actual -> ["diff", "--color=always", expected, actual]) |
| 43 | + goldenFile |
| 44 | + outFile |
| 45 | + runTest |
| 46 | + where |
| 47 | + runTest = do |
| 48 | + indexer <- |
| 49 | + either throwIO pure |
| 50 | + =<< runExceptT (Spent.mkSpentIndexer SQLiteIndexer.inMemoryDB) |
| 51 | + queryResult :: Maybe Spent.SpentInfoEvent <- |
| 52 | + queryIndexerOnSnapshot |
| 53 | + nodeType |
| 54 | + subChainPath |
| 55 | + dbPath |
| 56 | + spentInfoIndexerConfig |
| 57 | + (Just $ chainPointAtSlotNo slotNo) |
| 58 | + Core.EventAtQuery |
| 59 | + indexer |
| 60 | + -- NOTE: See documentation $sql-query for mention of sorting results. |
| 61 | + Aeson.encodeFile outFile (NEList.sort <$> queryResult) |
| 62 | + |
| 63 | + outFile = pathToOutFile cfg |
| 64 | + goldenFile = pathToGoldenFile cfg |
| 65 | + subChainPath = "../../mainnet-snapshots" </> eraToString era |
| 66 | + |
| 67 | +-- | Config used for testing SpentInfo indexer on snapshot with no rollback. |
| 68 | +spentInfoIndexerConfig :: Runner.RunIndexerOnSnapshotConfig BlockEvent Spent.SpentInfoEvent |
| 69 | +spentInfoIndexerConfig = Runner.RunIndexerOnSnapshotConfig preprocessor 1 |
| 70 | + where |
| 71 | + preprocessor = Runner.RunIndexerEventPreprocessing extractSpent (const Nothing) (const Nothing) |
| 72 | + mkIndexedTimedSpent |
| 73 | + :: C.ChainPoint -> C.TxBody era -> Core.ProcessedInput C.ChainPoint Spent.SpentInfoEvent |
| 74 | + mkIndexedTimedSpent point = Core.Index . Core.Timed point . NEList.nonEmpty . Spent.getInputs |
| 75 | + extractSpent :: BlockEvent -> [Core.ProcessedInput C.ChainPoint Spent.SpentInfoEvent] |
| 76 | + extractSpent (BlockEvent (C.BlockInMode (C.Block (C.BlockHeader slotNo hash _) txs) _) _ _) = |
| 77 | + map (mkIndexedTimedSpent (C.ChainPoint slotNo hash) . C.getTxBody) txs |
| 78 | + |
| 79 | +{- | Create a 'ChainPoint' from a 'SlotNo' with arbitrary hash. |
| 80 | +Used solely for testing here, where the hash information of chain point is |
| 81 | +known not to be used. |
| 82 | +
|
| 83 | +NOTE: This is needed since the only way to query SpentInfo at a given slot number, as of |
| 84 | +writing, is to use the 'ChainPoint' provided to 'query' with 'EventAtQuery'. |
| 85 | +-} |
| 86 | +chainPointAtSlotNo :: C.SlotNo -> C.ChainPoint |
| 87 | +chainPointAtSlotNo = flip C.ChainPoint (C.HeaderHash BSS.empty) |
| 88 | + |
| 89 | +{- $sql-query |
| 90 | + Results were generated using the following SQL query (PostgreSQL 11.19) on the |
| 91 | + database created from 'cardano-db-sync-13.1.1.3', varying the slot number according to the test. |
| 92 | + This creates the resulting JSON golden file. |
| 93 | +
|
| 94 | + Note the result is sorted according to fields 'spentTxOutRef' then 'spentAtTxId', to avoid |
| 95 | + false negatives because of different orderings in the golden test. |
| 96 | +
|
| 97 | + @ |
| 98 | + SET client_encoding = 'UTF8'; |
| 99 | +
|
| 100 | + \set slot 18728839 |
| 101 | +
|
| 102 | + CREATE TEMP TABLE out_result AS |
| 103 | + WITH tx_at_slot AS ( |
| 104 | + SELECT |
| 105 | + tx.id, |
| 106 | + tx.hash, |
| 107 | + tx.valid_contract, |
| 108 | + tx.block_index, |
| 109 | + block.slot_no |
| 110 | + FROM block |
| 111 | + JOIN tx ON tx.block_id = block.id |
| 112 | + WHERE block.slot_no = :slot |
| 113 | + -- tx_in_* tables attaches info on spent UTxOs from either the |
| 114 | + -- collateral_tx_in or tx_in tables, based on whether the TxIn |
| 115 | + -- or collateral TxIns would have been spent based on whether |
| 116 | + -- the script was valid or invalid, respectively. |
| 117 | + ), tx_in_collat AS ( |
| 118 | + -- Script is invalid, so collateral is spent |
| 119 | + SELECT |
| 120 | + tx_at_slot.id, |
| 121 | + tx_at_slot.hash, |
| 122 | + collateral_tx_in.tx_out_id, |
| 123 | + collateral_tx_in.tx_out_index |
| 124 | + FROM tx_at_slot |
| 125 | + JOIN collateral_tx_in ON tx_at_slot.id = collateral_tx_in.tx_in_id |
| 126 | + WHERE NOT tx_at_slot.valid_contract |
| 127 | + ), tx_in_nocollat AS ( |
| 128 | + -- Script is valid (or there is no script), so usual TxIn is spent |
| 129 | + SELECT |
| 130 | + tx_at_slot.id, |
| 131 | + tx_at_slot.hash, |
| 132 | + tx_in.tx_out_id, |
| 133 | + tx_in.tx_out_index |
| 134 | + FROM tx_at_slot |
| 135 | + JOIN tx_in ON tx_at_slot.id = tx_in.tx_in_id |
| 136 | + WHERE tx_at_slot.valid_contract |
| 137 | + ), tx_in_data AS ( |
| 138 | + SELECT * FROM tx_in_collat |
| 139 | + UNION ALL |
| 140 | + SELECT * FROM tx_in_nocollat |
| 141 | + ), result AS ( |
| 142 | + -- Attach the TxIx hash identifying each TxIn from the tx table |
| 143 | + -- based on the tx_out_id. |
| 144 | + SELECT |
| 145 | + -- Transaction where this TxIn was spent |
| 146 | + encode(tx_in_data.hash, 'hex') as spentAtTxId, |
| 147 | + -- Identifier for where this TxIn was created (appeared as TxOut) |
| 148 | + -- See FromJSON instance for TxIn |
| 149 | + -- https://cardano-api.cardano.intersectmbo.org/cardano-api/src/Cardano.Api.TxIn.html#TxIn |
| 150 | + concat(encode(tx.hash, 'hex'), '#', tx_in_data.tx_out_index) as spentTxOutRef |
| 151 | + FROM |
| 152 | + tx_in_data |
| 153 | + JOIN tx on tx_in_data.tx_out_id = tx.id |
| 154 | + -- Ordering used so that Haskell value can be encoded with the same ordering. |
| 155 | + ORDER BY spentTxOutRef, spentAtTxId |
| 156 | + ) |
| 157 | + -- JSON formatting to match To/FromJSON instance in cardano-api |
| 158 | + SELECT |
| 159 | + json_agg( |
| 160 | + json_build_object( |
| 161 | + 'spentAtTxId', |
| 162 | + spentAtTxId, |
| 163 | + 'spentTxOutRef', |
| 164 | + spentTxOutRef |
| 165 | + ) |
| 166 | + ) |
| 167 | + FROM result; |
| 168 | +
|
| 169 | + \copy out_result TO result.out.golden |
| 170 | + @ |
| 171 | + - |
| 172 | +-} |
0 commit comments