Skip to content

Commit

Permalink
Remove wallet id index from meta store
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 1, 2023
1 parent 0456438 commit 171c997
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 401 deletions.
1 change: 0 additions & 1 deletion lib/wallet/cardano-wallet.cabal
Expand Up @@ -866,7 +866,6 @@ test-suite unit
Cardano.Wallet.DB.Store.Submissions.StoreSpec
Cardano.Wallet.DB.Store.Transactions.StoreSpec
Cardano.Wallet.DB.Store.Wallets.LayerSpec
Cardano.Wallet.DB.Store.Wallets.ModelSpec
Cardano.Wallet.DB.Store.Wallets.StoreSpec
Cardano.Wallet.DelegationSpec
Cardano.Wallet.DummyTarget.Primitive.Types
Expand Down
10 changes: 4 additions & 6 deletions lib/wallet/src/Cardano/Wallet/DB.hs
Expand Up @@ -489,7 +489,7 @@ mkDBLayerFromParts ti DBLayerCollection{..} = DBLayer
readCurrentTip wid >>= \case
Just tip -> do
inLedgers <- if status `elem` [Nothing, Just WTxMeta.InLedger]
then readTxHistory_ dbTxHistory wid range tip limit order
then readTxHistory_ dbTxHistory range tip limit order
else pure []
let isInSubmission = has (txStatus . _InSubmission)
isExpired = has (txStatus . _Expired)
Expand Down Expand Up @@ -518,7 +518,7 @@ mkDBLayerFromParts ti DBLayerCollection{..} = DBLayer
, getTx = \wid txid -> wrapNoSuchWallet wid $ do
readCurrentTip wid >>= \case
Just tip -> do
historical <- getTx_ dbTxHistory wid txid tip
historical <- getTx_ dbTxHistory txid tip
case historical of
Just tx -> pure $ Just tx
Nothing -> withSubmissions wid Nothing $ \submissions -> do
Expand Down Expand Up @@ -726,8 +726,7 @@ data DBTxHistory stm = DBTxHistory
-- an error, but need not.

, readTxHistory_
:: WalletId
-> Range SlotNo
:: Range SlotNo
-> BlockHeader
-> Maybe Natural
-> SortOrder
Expand All @@ -738,8 +737,7 @@ data DBTxHistory stm = DBTxHistory
-- Returns an empty list if the wallet isn't found.

, getTx_
:: WalletId
-> Hash "Tx"
:: Hash "Tx"
-> BlockHeader
-> stm (Maybe TransactionInfo)
-- ^ Fetch the latest transaction by id, returns Nothing when the
Expand Down
10 changes: 5 additions & 5 deletions lib/wallet/src/Cardano/Wallet/DB/Layer.hs
Expand Up @@ -628,8 +628,8 @@ newDBLayerWith _cacheBehavior _tr ti SqliteContext{runQuery} = mdo
updateS (store transactionsQS) Nothing
. ExpandTxWalletsHistory wid

, readTxHistory_ = \wid range tip mlimit order -> do
allTransactions <- queryS transactionsQS $ All wid
, readTxHistory_ = \range tip mlimit order -> do
allTransactions <- queryS transactionsQS All
let whichMeta DB.TxMeta{..} = and $ catMaybes
[ (txMetaSlot >=) <$> W.inclusiveLowerBound range
, (txMetaSlot <=) <$> W.inclusiveUpperBound range
Expand All @@ -647,8 +647,8 @@ newDBLayerWith _cacheBehavior _tr ti SqliteContext{runQuery} = mdo
forM transactions $
selectTransactionInfo ti tip lookupTx lookupTxOut

, getTx_ = \wid txid tip -> do
transactions <- queryS transactionsQS $ One wid (TxId txid)
, getTx_ = \txid tip -> do
transactions <- queryS transactionsQS $ One $ TxId txid
let lookupTx = queryS transactionsQS . GetByTxId
lookupTxOut = queryS transactionsQS . GetTxOut
forM transactions $
Expand Down Expand Up @@ -697,7 +697,7 @@ newDBLayerWith _cacheBehavior _tr ti SqliteContext{runQuery} = mdo
[ StakeKeyCertSlot >. nearestPoint
]
updateS (store transactionsQS) Nothing $
RollbackTxWalletsHistory wid nearestPoint
RollbackTxWalletsHistory nearestPoint

pure
$ W.chainPointFromBlockHeader
Expand Down
3 changes: 0 additions & 3 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Meta/Model.hs
Expand Up @@ -19,7 +19,6 @@ module Cardano.Wallet.DB.Store.Meta.Model
, TxMetaHistory(..)
, mkTxMetaHistory
, rollbackTxMetaHistory
, WalletsMeta
, mkTxMetaFromEntity
) where

Expand Down Expand Up @@ -129,8 +128,6 @@ mkTxMetaHistory wid txs = TxMetaHistory $
| (tx, meta) <- txs
]

type WalletsMeta = Map W.WalletId TxMetaHistory

mkTxMetaFromEntity :: TxMeta -> W.TxMeta
mkTxMetaFromEntity TxMeta{..} = W.TxMeta
{ W.status = txMetaStatus
Expand Down
31 changes: 13 additions & 18 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Meta/Store.hs
@@ -1,4 +1,5 @@

{-# LANGUAGE TypeApplications #-}

{- |
Copyright: © 2018-2022 IOHK
Expand All @@ -17,8 +18,6 @@ import Cardano.Wallet.DB.Sqlite.Schema
( EntityField (..), TxMeta (..) )
import Cardano.Wallet.DB.Store.Meta.Model
( DeltaTxMetaHistory (..), TxMetaHistory (..) )
import Cardano.Wallet.Primitive.Types
( WalletId )
import Control.Arrow
( (&&&) )
import Control.Exception
Expand All @@ -38,44 +37,40 @@ import Database.Persist.Sql
, deleteWhere
, repsertMany
, selectList
, (==.)
, (>.)
)

import qualified Data.Map.Strict as Map

-- | Create an SQL store to hold meta transactions for a wallet.
mkStoreMetaTransactions :: WalletId
-> Store (SqlPersistT IO) DeltaTxMetaHistory
mkStoreMetaTransactions :: Store (SqlPersistT IO) DeltaTxMetaHistory
mkStoreMetaTransactions
wid = Store { loadS = load wid, writeS = write wid, updateS = update wid }
= Store { loadS = load, writeS = write, updateS = update}

update :: WalletId
-> Maybe TxMetaHistory
update
:: Maybe TxMetaHistory
-> DeltaTxMetaHistory
-> SqlPersistT IO ()
update wid _ change = case change of
update _ change = case change of
Expand txs -> putMetas txs
Rollback point -> do
let isAfter = TxMetaSlot >. point
deleteWhere
[ TxMetaWalletId ==. wid
, isAfter
[ isAfter
]

write :: WalletId -> TxMetaHistory -> SqlPersistT IO ()
write wid txs = do
deleteWhere [TxMetaWalletId ==. wid]
write :: TxMetaHistory -> SqlPersistT IO ()
write txs = do
deleteWhere @_ @_ @TxMeta []
putMetas txs

load :: WalletId
-> SqlPersistT IO (Either SomeException TxMetaHistory)
load wid =
load ::SqlPersistT IO (Either SomeException TxMetaHistory)
load =
Right
. TxMetaHistory
. Map.fromList
. fmap ((txMetaTxId &&& id) . entityVal)
<$> selectList [TxMetaWalletId ==. wid] []
<$> selectList [] []

-- | Insert multiple meta-transactions, overwriting the previous version in
-- case of the same transaction index.
Expand Down
38 changes: 17 additions & 21 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Wallets/Layer.hs
Expand Up @@ -24,14 +24,16 @@ import Cardano.Wallet.DB.Sqlite.Types
( TxId (..) )
import Cardano.Wallet.DB.Store.Meta.Model
( TxMetaHistory (relations) )
import Cardano.Wallet.DB.Store.Meta.Store
( mkStoreMetaTransactions )
import Cardano.Wallet.DB.Store.QueryStore
( QueryStore (..) )
import Cardano.Wallet.DB.Store.Transactions.Model
( TxRelation )
import Cardano.Wallet.DB.Store.Wallets.Model
( DeltaTxWalletsHistory (..) )
import Cardano.Wallet.DB.Store.Wallets.Store
( mkStoreTxWalletsHistory, mkStoreWalletsMeta )
( mkStoreTxWalletsHistory )
import Data.DBVar
( Store (..), newCachedStore )
import Data.Foldable
Expand All @@ -41,8 +43,8 @@ import Data.Word
import Database.Persist.Sql
( SqlPersistT )


import qualified Cardano.Wallet.DB.Store.Transactions.Layer as TxSet
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut as W
import qualified Data.Map.Strict as Map

Expand All @@ -52,8 +54,8 @@ import qualified Data.Map.Strict as Map
data QueryTxWalletsHistory b where
GetByTxId :: TxId -> QueryTxWalletsHistory (Maybe (Either TxRelation CBOR))
GetTxOut :: (TxId, Word32) -> QueryTxWalletsHistory (Maybe W.TxOut)
One :: W.WalletId -> TxId -> QueryTxWalletsHistory (Maybe TxMeta)
All :: W.WalletId -> QueryTxWalletsHistory [TxMeta]
One :: TxId -> QueryTxWalletsHistory (Maybe TxMeta)
All :: QueryTxWalletsHistory [TxMeta]

{-----------------------------------------------------------------------------
Query Store type
Expand All @@ -66,34 +68,28 @@ newQueryStoreTxWalletsHistory
=> m QueryStoreTxWalletsHistory
newQueryStoreTxWalletsHistory = do
let txsQueryStore = TxSet.mkQueryStoreTxSet

let storeTransactions = store txsQueryStore
storeWalletsMeta <- newCachedStore mkStoreWalletsMeta

storeMetas <- newCachedStore mkStoreMetaTransactions
let storeTxWalletsHistory = mkStoreTxWalletsHistory
storeTransactions -- on disk
storeWalletsMeta -- in memory
storeMetas -- in memory

let readAllMetas :: W.WalletId -> m [TxMeta]
readAllMetas wid = do
Right wmetas <- loadS storeWalletsMeta
pure
. maybe [] (toList . relations)
$ Map.lookup wid wmetas
let readAllMetas :: m [TxMeta]
readAllMetas = do
Right wmetas <- loadS storeMetas
pure $ (toList . relations) wmetas

query :: forall a. QueryTxWalletsHistory a -> SqlPersistT IO a
query = \case
GetByTxId txid -> do
queryS txsQueryStore $ TxSet.GetByTxId txid
GetTxOut key -> do
queryS txsQueryStore $ TxSet.GetTxOut key

One wid txid -> do
Right wmetas <- loadS storeWalletsMeta
pure $ do
metas <- Map.lookup wid wmetas
Map.lookup txid . relations $ metas
All wid ->
readAllMetas wid
One txid -> do
Right wmetas <- loadS storeMetas
pure $ Map.lookup txid . relations $ wmetas
All -> readAllMetas

pure QueryStore
{ queryS = query
Expand Down

0 comments on commit 171c997

Please sign in to comment.