Skip to content

Commit

Permalink
Refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
berewt committed Jun 2, 2023
1 parent 49a7fd8 commit 8acce35
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 52 deletions.
54 changes: 25 additions & 29 deletions marconi-chain-index/src/Marconi/ChainIndex/Indexers/MintBurn.hs
Expand Up @@ -68,17 +68,18 @@ import Ouroboros.Consensus.Shelley.Eras qualified as OEra
-- * Event


-- | The key of the tx that mint the asset
data TxMintKey = TxMintKey
-- | The info about the tx that does the minting
data TxMintInfo = TxMintInfo
{ txMintTxId :: C.TxId
, txMintIndexInBlock :: TxIndexInBlock
, txMintAsset :: NE.NonEmpty MintAsset
} deriving (Show, Eq, Ord)

data TxMintEvent = TxMintEvent
{ txMintEventSlotNo :: !C.SlotNo
, txMintEventBlockHeaderHash :: !(C.Hash C.BlockHeader)
, txMintEventBlockNo :: !C.BlockNo
, txMintEventTxAssets :: !(NE.NonEmpty (TxMintKey, NE.NonEmpty MintAsset))
, txMintEventTxAssets :: !(NE.NonEmpty TxMintInfo)
} deriving (Show, Eq, Ord)

data MintAsset = MintAsset
Expand All @@ -96,9 +97,9 @@ toUpdate (C.BlockInMode (C.Block (C.BlockHeader slotNo blockHeaderHash blockNo)
x : xs -> Just $ TxMintEvent slotNo blockHeaderHash blockNo (x NE.:| xs)
[] -> Nothing

txMints :: TxIndexInBlock -> C.Tx era -> Maybe (TxMintKey, NE.NonEmpty MintAsset)
txMints :: TxIndexInBlock -> C.Tx era -> Maybe TxMintInfo
txMints ix (C.Tx txb _) = case txbMints txb of
x : xs -> Just (TxMintKey (C.getTxId txb) ix, x NE.:| xs )
x : xs -> Just $ TxMintInfo (C.getTxId txb) ix (x NE.:| xs )
[] -> Nothing

txbMints :: C.TxBody era -> [MintAsset]
Expand Down Expand Up @@ -156,22 +157,12 @@ fromAlonzoData = C.fromPlutusData . LA.getPlutusData -- from cardano-api:src/Car

-- * Sqlite

instance SQL.FromRow TxMintKey where

fromRow = TxMintKey <$> SQL.field <*> SQL.field

instance SQL.ToRow TxMintKey where

toRow key =
[ SQL.toField $ txMintTxId key
, SQL.toField $ txMintIndexInBlock key
]

data TxMintRow = TxMintRow
{ _txMintRowSlotNo :: !C.SlotNo
, _txMintRowBlockHeaderHash :: !(C.Hash C.BlockHeader)
, _txMintRowBlockNo :: !C.BlockNo
, _txMintRowTxKey :: !TxMintKey
, _txMintRowTxIx :: !TxIndexInBlock
, _txMintRowTxId :: !C.TxId
, _txMintRowPolicyId :: !C.PolicyId
, _txMintRowAssetName :: !C.AssetName
, _txMintRowQuantity :: !C.Quantity
Expand All @@ -186,18 +177,18 @@ instance SQL.FromRow TxMintRow where

fromRow = TxMintRow
<$> SQL.field <*> SQL.field <*> SQL.field
<*> SQL.fromRow
<*> SQL.field <*> SQL.field <*> SQL.field
<*> SQL.field <*> SQL.field
<*> SQL.field <*> SQL.field <*> SQL.field
<*> SQL.field

instance SQL.ToRow TxMintRow where

toRow = traverse view
[ txMintRowSlotNo . to SQL.toField
, txMintRowBlockHeaderHash . to SQL.toField
, txMintRowBlockNo . to SQL.toField
, txMintRowTxKey . to (SQL.toField . txMintTxId)
, txMintRowTxKey . to (SQL.toField . txMintIndexInBlock)
, txMintRowTxIx . to SQL.toField
, txMintRowTxId . to SQL.toField
, txMintRowPolicyId . to SQL.toField
, txMintRowAssetName . to SQL.toField
, txMintRowQuantity . to SQL.toField
Expand All @@ -211,7 +202,8 @@ instance FromJSON TxMintRow where
<$> v .: "slotNo"
<*> v .: "blockHeaderHash"
<*> v .: "blockNo"
<*> (TxMintKey <$> v .: "txId" <*> v .: "txIndexInBlock")
<*> v .: "txIndexInBlock"
<*> v .: "txId"
<*> v .: "policyId"
<*> v .: "assetName"
<*> v .: "quantity"
Expand All @@ -224,8 +216,8 @@ instance ToJSON TxMintRow where
[ "slotNo" .= _txMintRowSlotNo row
, "blockHeaderHash" .= _txMintRowBlockHeaderHash row
, "blockNo" .= _txMintRowBlockNo row
, "txId" .= txMintTxId (_txMintRowTxKey row)
, "txIndexInBlock" .= txMintIndexInBlock (_txMintRowTxKey row)
, "txIndexInBlock" .= _txMintRowTxIx row
, "txId" .= _txMintRowTxId row
, "policyId" .= _txMintRowPolicyId row
, "assetName" .= _txMintRowAssetName row
, "quantity" .= _txMintRowQuantity row
Expand Down Expand Up @@ -265,12 +257,13 @@ sqliteInsert c es = SQL.executeMany c template $ toRows =<< toList es

toRows :: TxMintEvent -> [TxMintRow]
toRows e = do
(txId, txMintAssets) <- NE.toList $ txMintEventTxAssets e
TxMintInfo txId txIx txMintAssets <- NE.toList $ txMintEventTxAssets e
mintAsset <- NE.toList txMintAssets
pure $ TxMintRow
(txMintEventSlotNo e)
(txMintEventBlockHeaderHash e)
(txMintEventBlockNo e)
txIx
txId
(mintAssetPolicyId mintAsset)
(mintAssetAssetName mintAsset)
Expand All @@ -283,13 +276,15 @@ fromRows :: [TxMintRow] -> [TxMintEvent]
fromRows rows = do
rs@(r :| _) <- NE.groupBy ((==) `on` slotNo) rows -- group by SlotNo
pure $ TxMintEvent (slotNo r) (hash r) (blockNo r) $ do
rs' <- NE.groupBy1 ((==) `on` txKey) rs -- group by TxMintKey
pure (txKey r, rowToMintAsset <$> rs')
rs' <- NE.groupBy1 ((==) `on` assetKey) rs -- group by TxMintKey
pure $ TxMintInfo (txId r) (txIx r) (rowToMintAsset <$> rs')
where
assetKey row = (txId row, txIx row)
txId = view txMintRowTxId
txIx = view txMintRowTxIx
slotNo = view txMintRowSlotNo
blockNo = view txMintRowBlockNo
hash = view txMintRowBlockHeaderHash
txKey = view txMintRowTxKey
rowToMintAsset :: TxMintRow -> MintAsset
rowToMintAsset row =
MintAsset
Expand Down Expand Up @@ -386,13 +381,14 @@ instance RI.Queryable MintBurnHandle where
Nothing -> memoryEventsList
Just sn -> filter (\e -> txMintEventSlotNo e <= sn) memoryEventsList
TxMintEvent slotNo blockHeaderHash blockNo txAssets <- filteredMemoryEvents <> storedEvents
(txId, mintAssets) <- NE.toList txAssets
TxMintInfo txId txIx mintAssets <- NE.toList txAssets
MintAsset policyId assetName quantity redeemerIx redeemerData <- NE.toList mintAssets
pure $
TxMintRow
slotNo
blockHeaderHash
blockNo
txIx
txId
policyId
assetName
Expand Down
2 changes: 1 addition & 1 deletion marconi-chain-index/src/Marconi/ChainIndex/Types.hs
Expand Up @@ -25,7 +25,7 @@ module Marconi.ChainIndex.Types
mintBurnDbName,
SecurityParam(SecurityParam),
IndexingDepth(MinIndexingDepth, MaxIndexingDepth),
TxIndexInBlock
TxIndexInBlock(TxIndexInBlock)
) where

import Cardano.Api qualified as C
Expand Down
Expand Up @@ -143,9 +143,9 @@ propQueryingAssetIdsIndividuallyShouldBeSameAsQueryingAll = H.property $ do
let assetIds = concatMap
(\e -> concat
$ NonEmpty.toList
$ fmap (\(_, assets) ->
$ fmap (
fmap (\(MintAsset policyId assetName _ _ _) -> (policyId, assetName))
$ NonEmpty.toList assets)
. NonEmpty.toList . MintBurn.txMintAsset)
$ MintBurn.txMintEventTxAssets e)
insertedEvents
combinedTxMintRows <- fmap concat <$> forM assetIds $ \(policyId, assetName) -> do
Expand Down Expand Up @@ -177,9 +177,9 @@ propQueryingAssetIdsIndividuallyAtPointShouldBeSameAsQueryingAllAtPoint = H.prop
let assetIds = concatMap
(\e -> concat
$ NonEmpty.toList
$ fmap (\(_, assets) ->
$ fmap (
fmap (\(MintAsset policyId assetName _ _ _) -> (policyId, assetName))
$ NonEmpty.toList assets)
. NonEmpty.toList . MintBurn.txMintAsset)
$ MintBurn.txMintEventTxAssets e)
insertedEvents
combinedTxMintRows <- fmap concat <$> forM assetIds $ \(policyId, assetName) -> do
Expand Down Expand Up @@ -210,9 +210,9 @@ propQueryingAssetIdsAtLatestPointShouldBeSameAsAssetIdsQuery = H.property $ do
let assetIds = concatMap
(\e -> concat
$ NonEmpty.toList
$ fmap (\(_, assets) ->
$ fmap (
fmap (\(MintAsset policyId assetName _ _ _) -> (policyId, assetName))
$ NonEmpty.toList assets)
. NonEmpty.toList . MintBurn.txMintAsset)
$ MintBurn.txMintEventTxAssets e)
insertedEvents

Expand Down Expand Up @@ -323,7 +323,7 @@ endToEnd = H.withShrinks 0 $ integration $ (liftIO TN.setDarwinTmpdir >>) $ HE.r
liftIO $ raiseException $ RI.query indexer $ QueryAllMintBurn Nothing
case MintBurn.fromRows txMintRows of
event : _ -> case MintBurn.txMintEventTxAssets event of
(_txId, gottenMintEvents :: NonEmpty MintAsset) :| [] -> let
(MintBurn.TxMintInfo _txId _txIx gottenMintEvents) :| [] -> let
in equalSet (mintsToPolicyAssets $ NonEmpty.toList gottenMintEvents) (getPolicyAssets txMintValue)
_ -> fail "More than one mint/burn event, but we created only one!"
_ -> fail "No events in indexer, but we inserted one!"
Expand Down
Expand Up @@ -8,15 +8,14 @@ module Marconi.Sidechain.Api.Query.Indexers.MintBurn
import Cardano.Api qualified as C

import Control.Concurrent.STM (STM, TMVar, atomically, newEmptyTMVarIO, tryReadTMVar)
import Control.Lens (to, (^.))
import Control.Lens ((^.))

import Data.Word (Word64)

import Control.Monad.Except (runExceptT)
import Marconi.ChainIndex.Indexers.MintBurn (MintBurnHandle, StorableQuery (QueryByAssetId),
StorableResult (MintBurnResult), TxMintRow, txMintIndexInBlock,
txMintRowBlockHeaderHash, txMintRowBlockNo, txMintRowQuantity,
txMintRowRedeemerData, txMintRowSlotNo, txMintRowTxKey, txMintTxId)
StorableResult (MintBurnResult), TxMintRow)
import Marconi.ChainIndex.Indexers.MintBurn qualified as MintBurn
import Marconi.Core.Storable (State)
import Marconi.Core.Storable qualified as Storable
import Marconi.Sidechain.Api.Routes (AssetIdTxResult (AssetIdTxResult))
Expand Down Expand Up @@ -70,12 +69,12 @@ queryByPolicyAndAssetId env policyId assetId slotNo = do

toAssetIdTxResult :: TxMintRow -> AssetIdTxResult
toAssetIdTxResult x = AssetIdTxResult
(x ^. txMintRowBlockHeaderHash)
(x ^. txMintRowBlockNo)
(x ^. txMintRowTxKey . to txMintIndexInBlock)
(x ^. txMintRowSlotNo)
(x ^. txMintRowTxKey . to txMintTxId)
(x ^. MintBurn.txMintRowBlockHeaderHash)
(x ^. MintBurn.txMintRowBlockNo)
(x ^. MintBurn.txMintRowTxIx)
(x ^. MintBurn.txMintRowSlotNo)
(x ^. MintBurn.txMintRowTxId)
Nothing
(Just $ x ^. txMintRowRedeemerData)
(x ^. txMintRowQuantity)
(Just $ x ^. MintBurn.txMintRowRedeemerData)
(x ^. MintBurn.txMintRowQuantity)

Expand Up @@ -66,7 +66,7 @@ queryMintingPolicyTest = property $ do
(mintAssetAssetName params)
Nothing)
. Set.toList . Set.fromList -- required to remove the potential duplicate assets
. concatMap (NonEmpty.toList . snd)
. concatMap (NonEmpty.toList . MintBurn.txMintAsset )
. concatMap NonEmpty.toList
. fmap MintBurn.txMintEventTxAssets
$ events
Expand All @@ -91,7 +91,7 @@ propMintBurnEventInsertionAndJsonRpcQueryRoundTrip action = property $ do
let (qParams :: [(PolicyId, AssetName)]) =
Set.toList . Set.fromList
. fmap (\mps -> (mintAssetPolicyId mps, mintAssetAssetName mps))
. concatMap (NonEmpty.toList . snd)
. concatMap (NonEmpty.toList . MintBurn.txMintAsset)
. concatMap NonEmpty.toList
. fmap MintBurn.txMintEventTxAssets
$ events
Expand Down
@@ -1,18 +1,22 @@
[
{
"blockHeaderHash": "6161616161616161616161616161616161616161616161616161616161616161",
"blockNo": 1047,
"quantity": -10,
"redeemer": "34",
"redeemerHash": "eb8649214997574e20c464388a172420d25403682bbbb80c496831c8cc1f8f0d",
"slotNo": 1,
"txId": "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000"
"txId": "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000",
"txIx": 0
},
{
"blockHeaderHash": "6161616161616161616161616161616161616161616161616161616161616161",
"blockNo": 1047,
"quantity": 10,
"redeemer": "34",
"redeemerHash": "eb8649214997574e20c464388a172420d25403682bbbb80c496831c8cc1f8f0d",
"slotNo": 1,
"txId": "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000"
"txId": "ec7d3bd7c6a3a31368093b077af0db46ceac77956999eb842373e08c6420f000",
"txIx": 1
}
]
6 changes: 6 additions & 0 deletions marconi-sidechain/test/Spec/Marconi/Sidechain/Routes.hs
Expand Up @@ -145,6 +145,8 @@ propJSONRountripGetTxsBurningAssetIdResult = property $ do
hsd <- Gen.maybe CGen.genHashableScriptData
AssetIdTxResult
<$> Gen.genHashBlockHeader
<*> Gen.genBlockNo
<*> (Gen.integral $ Range.linear 0 10)
<*> Gen.genSlotNo
<*> CGen.genTxId
<*> pure (fmap C.hashScriptDataBytes hsd)
Expand Down Expand Up @@ -263,13 +265,17 @@ goldenMintingPolicyHashTxResult = do
let mints =
[ AssetIdTxResult
blockHeaderHash
(C.BlockNo 1047)
0
(C.SlotNo 1)
txId
(Just $ C.hashScriptDataBytes $ C.unsafeHashableScriptData redeemerData)
(Just redeemerData)
(C.Quantity $ -10)
, AssetIdTxResult
blockHeaderHash
(C.BlockNo 1047)
1
(C.SlotNo 1)
txId
(Just $ C.hashScriptDataBytes $ C.unsafeHashableScriptData redeemerData)
Expand Down

0 comments on commit 8acce35

Please sign in to comment.