@@ -24,21 +24,23 @@ import Data.Set (Set, fromList, toList, union)
2424import Data.Text (Text , intercalate , unpack )
2525import Database.SQLite.Simple (NamedParam ((:=) ), open )
2626import Database.SQLite.Simple qualified as SQL
27- import Ledger.Tx.CardanoAPI (ToCardanoError (DeserialisationError , Tag , TxBodyError ))
2827import Marconi.Api.Types (DBConfig (DBConfig , utxoConn ),
2928 DBQueryEnv (DBQueryEnv , _DbConf , _Network , _QueryAddresses , _QueryComm ),
3029 HasDBQueryEnv (dbConf , queryAddresses , queryComm ), HasUtxoQueryComm (indexer , queryReq ),
31- TargetAddresses , UtxoQueryComm ( UtxoQueryComm , _Indexer , _QueryReq ) ,
32- UtxoTxOutReport (UtxoTxOutReport ))
30+ QueryExceptions ( AddressNotInListError , QueryError ), TargetAddresses ,
31+ UtxoQueryComm ( UtxoQueryComm , _Indexer , _QueryReq ), UtxoTxOutReport (UtxoTxOutReport ))
3332import Marconi.Index.Utxo (UtxoIndex , UtxoRow (UtxoRow , _reference ), toRows )
3433import Marconi.Types (CardanoAddress , TxOutRef )
3534import RewindableIndex.Index.VSqlite qualified as Ix
3635
36+ -- | Bootstraps the utxo query environment.
37+ -- The module is responsible for accessing SQLite for quries.
38+ -- The main issue we try to avoid here is mixing inserts and quries in SQLite to avoid locking the database
3739bootstrap
38- :: FilePath
39- -> TargetAddresses
40- -> CApi. NetworkId
41- -> IO DBQueryEnv
40+ :: FilePath -- ^ file path the SQLite utxos database
41+ -> TargetAddresses -- ^ user provided target addresses
42+ -> CApi. NetworkId -- ^ cardano networkId
43+ -> IO DBQueryEnv -- ^ returns Query runtime environment
4244bootstrap dbPath targetAddresses nId = do
4345 dbconf <- DBConfig <$> open dbPath
4446 _QueryReq <- atomically (newEmptyTMVar :: STM (TMVar () ) )
@@ -49,68 +51,81 @@ bootstrap dbPath targetAddresses nId = do
4951 , _QueryAddresses = targetAddresses
5052 , _Network = nId
5153 }
52-
54+ -- | finds reports for all user-provided addresses.
55+ -- TODO we need to use streaming
56+ --
5357findAll
54- :: DBQueryEnv
55- -> IO (Set UtxoTxOutReport ) -- ^ set of corresponding TxOutRefs
58+ :: DBQueryEnv -- ^ Query run time environment
59+ -> IO (Set UtxoTxOutReport ) -- ^ set of corresponding TxOutRefs
5660findAll env = fromList <$> forConcurrently addresses f
5761 where
5862 addresses = NonEmpty. toList (env ^. queryAddresses)
5963 f :: CardanoAddress -> IO (UtxoTxOutReport )
6064 f addr = UtxoTxOutReport (CApi. serialiseAddress addr) <$> findByCardanoAddress env (CApi. toAddressAny addr)
6165
66+ -- | Query utxos address address
67+ --
6268utxoQuery :: DBConfig -> CApi. AddressAny -> IO (Set TxOutRef )
63- utxoQuery dbConfig address = SQL. queryNamed (utxoConn dbConfig)
64- " SELECT * FROM utxos WHERE utxos.address=:address"
69+ utxoQuery dbConfig address = putStrLn " utxoquery " >> SQL. queryNamed (utxoConn dbConfig)
70+ " SELECT txid, inputIx FROM utxos WHERE utxos.address=:address"
6571 [" :address" := address]
66- >>= pure . fromList . fmap _reference
72+ >>= pure . fromList -- . fmap _reference
6773
74+ -- | Query utxos by Cardano Address
75+ -- To Cardano error may occure
6876findByCardanoAddress
69- :: DBQueryEnv
70- -> CApi. AddressAny
71- -> IO (Set TxOutRef ) -- ^ To Plutus address conversion error may occure
77+ :: DBQueryEnv -- ^ Query run time environment
78+ -> CApi. AddressAny -- ^ Cardano address to query
79+ -> IO (Set TxOutRef )
7280findByCardanoAddress env address = withQueryAction env address utxoQuery
7381
7482-- | Retrieve a Set of TxOutRefs associated with the given Cardano Era address
7583-- We return an empty Set if no address is found
7684findByAddress
77- :: DBQueryEnv
85+ :: DBQueryEnv -- ^ Query run time environment
7886 -> Text -- ^ Bech32 Address
79- -> IO (Either ToCardanoError UtxoTxOutReport ) -- ^ To Plutus address conversion error may occure
87+ -> IO (Either QueryExceptions UtxoTxOutReport ) -- ^ To Plutus address conversion error may occure
8088findByAddress env addressText =
8189 let
82- f :: Either CApi. Bech32DecodeError CardanoAddress -> IO (Either ToCardanoError UtxoTxOutReport )
90+ f :: Either CApi. Bech32DecodeError CardanoAddress -> IO (Either QueryExceptions UtxoTxOutReport )
8391 f (Right address)
84- | address `elem` (env ^. queryAddresses) =
92+ | address `elem` (env ^. queryAddresses) = -- allow for targetAddress search only
8593 (pure . CApi. toAddressAny $ address)
8694 >>= findByCardanoAddress env
8795 >>= pure . Right . UtxoTxOutReport addressText
88- | otherwise = pure . Left . TxBodyError $
96+ | otherwise = pure . Left . AddressNotInListError . QueryError $
8997 unpack addressText <> " not in the provided target addresses"
90- f (Left e) = pure . Left $ Tag (unpack addressText
98+ f (Left e) = pure . Left $ QueryError (unpack addressText
9199 <> " generated error: "
92100 <> show e)
93- DeserialisationError
101+
94102 in
95103 f $ CApi. deserialiseFromBech32 CApi. AsShelleyAddress addressText
96104
97-
98- queryInMemory :: CApi. AddressAny -> UtxoIndex -> IO ( Set TxOutRef )
105+ -- | query in-momory utxos for the given address
106+ --
107+ queryInMemory
108+ :: CApi. AddressAny -- ^ address to query
109+ -> UtxoIndex -- ^ inmemory, hot-store, storage for utxos
110+ -> IO ( Set TxOutRef )
99111queryInMemory address ix =
100- Ix. getBuffer (ix ^. Ix. storage)
101- >>= pure
112+ let
113+ isTargetAddress :: UtxoRow -> Bool
114+ isTargetAddress (UtxoRow a _ ) = address == a
115+ in
116+ Ix. getBuffer (ix ^. Ix. storage)
117+ >>= pure
102118 . fromList
103119 . fmap _reference
104- . filter ( isTargetAddress address)
120+ . filter isTargetAddress
105121 . concatMap toRows
106122
107- isTargetAddress :: CApi. AddressAny -> UtxoRow -> Bool
108- isTargetAddress address (UtxoRow a _ ) = address == a
109-
123+ -- | Execute the query function
124+ -- We must stop the utxo insers before doing the query
110125withQueryAction
111- :: DBQueryEnv
112- -> CApi. AddressAny
113- -> (DBConfig -> CApi. AddressAny -> IO (Set TxOutRef ) )
126+ :: DBQueryEnv -- ^ Query run time environment
127+ -> CApi. AddressAny -- ^ Cardano address to query
128+ -> (DBConfig -> CApi. AddressAny -> IO (Set TxOutRef ) ) -- ^ Query function to run
114129 -> IO (Set TxOutRef )
115130withQueryAction env address qAction =
116131 let
@@ -123,10 +138,12 @@ withQueryAction env address qAction =
123138 pure . union fromColdStore $ fromHotStore
124139 in
125140 bracket_
126- (atomically $ putTMVar qreq () )
127- (atomically $ putTMVar qreq () )
141+ (atomically $ putTMVar qreq () ) -- block inserts
142+ (atomically $ putTMVar qreq () ) -- unblock inserts
128143 action
129144
145+ -- | report target addresses
146+ -- Used by JSON-RPC
130147reportQueryAddresses
131148 :: DBQueryEnv
132149 -> IO (Set CardanoAddress )
0 commit comments