Skip to content

Commit

Permalink
Sqlite: Fix updating of TxMeta in putTxHistory
Browse files Browse the repository at this point in the history
- putMany was keeping the old TxMeta entries, rather than replacing
  them.

- Clean up imports.
  • Loading branch information
paweljakubas authored and rvl committed May 24, 2019
1 parent 6a54e8f commit 390f85b
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ import Cardano.Wallet.DB
, ErrWalletAlreadyExists (..)
, PrimaryKey (..)
)
import Cardano.Wallet.DB.Sqlite.TH
( AddressPool (..)
, AddressPoolId
, AddressPoolIndex (..)
, Checkpoint (..)
, EntityField (..)
, PendingTx (..)
, PrivateKey (..)
, SeqState (..)
, SeqStateExternalPool (..)
, SeqStateId
, SeqStateInternalPool (..)
, SeqStatePendingIx (..)
, TxIn (..)
, TxMeta (..)
, TxOut (..)
, UTxO (..)
, Wallet (..)
, migrateAll
, unWalletKey
)
import Cardano.Wallet.DB.Sqlite.Types
( AddressPoolXPub (..), TxId (..) )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -96,8 +117,6 @@ import System.IO
import System.Log.FastLogger
( fromLogStr )

import Cardano.Wallet.DB.Sqlite.TH

import qualified Cardano.Wallet.Primitive.AddressDerivation as W
import qualified Cardano.Wallet.Primitive.AddressDiscovery as W
import qualified Cardano.Wallet.Primitive.Model as W
Expand Down Expand Up @@ -183,7 +202,7 @@ newDBLayer fp = do
selectWallet wid >>= \case
Just _ -> Right <$> do
deleteCheckpoints @s wid
deleteTxHistory wid []
deleteTxMetas wid
deleteLooseTransactions
deleteWhere [PrivateKeyTableWalletId ==. wid]
deleteCascadeWhere [WalTableId ==. wid]
Expand Down Expand Up @@ -243,8 +262,7 @@ newDBLayer fp = do
selectWallet wid >>= \case
Just _ -> do
let (metas, txins, txouts) = mkTxHistory wid txs
deleteTxHistory wid metas
putMany metas
putTxMetas wid metas
putMany txins
putMany txouts
deleteLooseTransactions
Expand Down Expand Up @@ -506,14 +524,21 @@ deleteCheckpoints wid = do
deleteState @s wid -- clear state

-- | Delete unused TxMeta values for a wallet.
deleteTxHistory
deleteTxMetas
:: W.WalletId
-> SqlPersistM ()
deleteTxMetas wid = deleteWhere [ TxMetaTableWalletId ==. wid ]

-- | Add new TxMeta rows, overwriting existing ones.
putTxMetas
:: W.WalletId
-> [TxMeta]
-- ^ The TxMeta entries to keep
-> SqlPersistM ()
deleteTxHistory wid inUse = deleteWhere
[ TxMetaTableWalletId ==. wid
, TxMetaTableTxId /<-. map txMetaTableTxId inUse ]
putTxMetas wid metas = do
deleteWhere
[ TxMetaTableWalletId ==. wid
, TxMetaTableTxId <-. map txMetaTableTxId metas ]
insertMany_ metas

-- | Delete transactions that aren't referred to by either Pending or TxMeta of
-- any wallet.
Expand Down

0 comments on commit 390f85b

Please sign in to comment.