Skip to content

Commit

Permalink
Order result
Browse files Browse the repository at this point in the history
  • Loading branch information
berewt committed Jun 2, 2023
1 parent 18cc5c8 commit fd047b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 5 additions & 5 deletions marconi-chain-index/src/Marconi/ChainIndex/Indexers/MintBurn.hs
Expand Up @@ -245,7 +245,7 @@ sqliteInsert c es = SQL.executeMany c template $ toRows =<< toList es
template =
"INSERT INTO minting_policy_events \
\( slotNo, blockHeaderHash, blockNo, \
\txId, txIndexInBlock, policyId, assetName, \
\txIndexInBlock, txId, policyId, assetName, \
\ quantity, redeemerIx, redeemerData ) \
\VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

Expand Down Expand Up @@ -298,11 +298,11 @@ queryStoredTxMintEvents sqlCon (conditions, params) =
allConditions = Text.intercalate " AND " $ fmap SQL.fromQuery conditions
whereClause = if allConditions == "" then "" else "WHERE " <> allConditions
query =
" SELECT slotNo, blockHeaderHash, blockNo, txId, txIndexInBlock, \
" SELECT slotNo, blockHeaderHash, blockNo, txIndexInBlock, txId, \
\ policyId, assetName, quantity, redeemerIx, redeemerData \
\ FROM minting_policy_events \
\ " <> whereClause <> " \
\ ORDER BY slotNo, txId"
\ ORDER BY blockNo ASC, txIndexInBlock ASC"

groupBySlotAndHash :: [TxMintEvent] -> [TxMintEvent]
groupBySlotAndHash events = events
Expand Down Expand Up @@ -374,7 +374,7 @@ instance RI.Queryable MintBurnHandle where
case querySlotNo of
Nothing -> memoryEventsList
Just sn -> filter (\e -> txMintEventSlotNo e <= sn) memoryEventsList
TxMintEvent slotNo blockHeaderHash blockNo txAssets <- filteredMemoryEvents <> storedEvents
TxMintEvent slotNo blockHeaderHash blockNo txAssets <- storedEvents <> filteredMemoryEvents
TxMintInfo txId txIx mintAssets <- NE.toList txAssets
MintAsset policyId assetName quantity redeemerIx redeemerData <- NE.toList mintAssets
pure $
Expand Down Expand Up @@ -408,7 +408,7 @@ instance RI.Buffered MintBurnHandle where
fmap MintBurnEvent . fromRows <$> SQL.query sqlCon query (SQL.Only k)
where
query =
" SELECT slotNo, blockHeaderHash, txId, txIndexInBlock, \
" SELECT slotNo, blockHeaderHash, blockNo, txIndexInBlock, txId, \
\ policyId, assetName, quantity, redeemerIx, redeemerData \
\ FROM minting_policy_events \
\ WHERE slotNo >= (SELECT MAX(slotNo) - ? FROM minting_policy_events) \
Expand Down
Expand Up @@ -16,15 +16,18 @@ import Control.Concurrent qualified as IO
import Control.Concurrent.Async qualified as IO
import Control.Concurrent.STM qualified as IO
import Control.Exception (catch)
import Control.Lens ((^.))
import Control.Lens (view, (^.))
import Control.Monad (forM, forM_, void)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson qualified as Aeson
import Data.Coerce (coerce)
import Data.Foldable (foldlM)
import Data.Function (on)
import Data.List qualified as List
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.List.NonEmpty qualified as NonEmpty
import Data.Maybe (mapMaybe)
import Data.Ord (comparing)
import Data.Set qualified as Set
import Data.String (fromString)
import Data.Word (Word64)
Expand Down Expand Up @@ -80,6 +83,10 @@ tests = testGroup "MintBurn"
"Querying everything at target slot should return all rows from genesis until that slot"
"propQueryingAllMintBurnAtPointShouldReturnMintsUntilThatPoint"
propQueryingAllMintBurnAtPointShouldReturnMintsUntilThatPoint
, testPropertyNamed
"Querying everything and check the order of the result"
"propQueryingReturnResultOrderedByAscendingBlockNumberAndTxIndex"
propQueryingReturnResultOrderedByAscendingBlockNumberAndTxIndex
, testPropertyNamed
"Querying by AssetId all possible AssetIds at target slot should yield same results as querying everything until that slot"
"propQueryingAssetIdsIndividuallyAtPointShouldBeSameAsQueryingAllAtPoint"
Expand Down Expand Up @@ -134,6 +141,26 @@ propQueryingEverythingShouldReturnAllIndexedEvents = H.property $ do
-- gotten out of the indexer:
equalSet (MintBurn.groupBySlotAndHash insertedEvents) (MintBurn.fromRows queryResult)

-- | Create transactions, index them, query indexer and find mint events.
propQueryingReturnResultOrderedByAscendingBlockNumberAndTxIndex :: Property
propQueryingReturnResultOrderedByAscendingBlockNumberAndTxIndex = H.property $ do
(indexer, _, _) <- Gen.genIndexWithEvents ":memory:"
-- Query results:
MintBurnResult queryResult <- liftIO $ raiseException $ RI.query indexer $ QueryAllMintBurn Nothing
-- Compare the sets of events inserted to the indexer and the set
-- gotten out of the indexer:
H.footnote $ show queryResult
case queryResult of
[] -> pure ()
x:xs -> void $ foldlM checkOrder x xs
where
checkOrder x y = case comparing (view MintBurn.txMintRowBlockNo) x y of
LT -> pure y
EQ -> if ((<=) `on` view MintBurn.txMintRowTxIx) x y
then pure y
else fail $ "error on TxId " <> show x <> show y
GT -> fail $ "error on BlockNo " <> show x <> show y

propQueryingAssetIdsIndividuallyShouldBeSameAsQueryingAll :: Property
propQueryingAssetIdsIndividuallyShouldBeSameAsQueryingAll = H.property $ do
(indexer, insertedEvents, _) <- Gen.genIndexWithEvents ":memory:"
Expand Down

0 comments on commit fd047b8

Please sign in to comment.