Skip to content
Permalink
Browse files

remove PendingTx table in favor and filter by 'TxStatus' instead

  • Loading branch information...
KtorZ committed Jun 12, 2019
1 parent 43818b1 commit a4e00b4535e52598bc38fe7b775c97b2c7fee451
@@ -561,8 +561,7 @@ newWalletLayer block0 db nw tl = do
-> NonEmpty TxOut
-> ExceptT ErrCreateUnsignedTx IO CoinSelection
_createUnsignedTx wid opts recipients = do
(w, _) <- withExceptT ErrCreateUnsignedTxNoSuchWallet
(_readWallet wid)
(w, _) <- withExceptT ErrCreateUnsignedTxNoSuchWallet (_readWallet wid)
let utxo = availableUTxO w
(sel, utxo') <- withExceptT ErrCreateUnsignedTxCoinSelection $
CoinSelection.random opts recipients utxo
@@ -38,7 +38,6 @@ import Cardano.Wallet.DB.Sqlite.TH
, Checkpoint (..)
, EntityField (..)
, Key (..)
, PendingTx (..)
, PrivateKey (..)
, SeqState (..)
, SeqStateExternalPool (..)
@@ -316,8 +315,7 @@ newDBLayer fp = do

addIndexes :: SqlPersistM ()
addIndexes = mapM_ (`rawExecute` [])
[ createIndex "pending_tx_wallet_id" "pending_tx (wallet_id)"
, createIndex "tx_meta_wallet_id" "tx_meta (wallet_id)"
[ createIndex "tx_meta_wallet_id" "tx_meta (wallet_id)"
, createIndex "tx_in_tx_id" "tx_in (tx_id)"
, createIndex "tx_out_tx_id" "tx_out (tx_id)"
]
@@ -387,9 +385,9 @@ mkCheckpointEntity
:: forall s t. W.TxId t
=> W.WalletId
-> W.Wallet s t
-> (Checkpoint, [UTxO], [PendingTx], [TxIn], [TxOut])
-> (Checkpoint, [UTxO], [TxIn], [TxOut])
mkCheckpointEntity wid wal =
(cp, utxo, map (pendingTx . TxId . fst) pending, ins, outs)
(cp, utxo, ins, outs)
where
pending = [(W.txId @t tx, tx) | tx <- Set.toList (W.getPending wal)]
(ins, outs) = mkTxInputsOutputs pending
@@ -401,11 +399,6 @@ mkCheckpointEntity wid wal =
, checkpointTableSlot = sl
, checkpointTableParent = BlockId parent
}
pendingTx tid = PendingTx
{ pendingTxTableWalletId = wid
, pendingTxTableCheckpointSlot = sl
, pendingTxTableId2 = tid
}
utxo = [ UTxO wid sl (TxId input) ix addr coin
| (W.TxIn input ix, W.TxOut addr coin) <- utxoMap ]
utxoMap = Map.assocs (W.getUTxO (W.totalUTxO wal))
@@ -524,11 +517,10 @@ insertCheckpoint
-> W.Wallet s t
-> SqlPersistM ()
insertCheckpoint wid cp = do
let (cp', utxo, pendings, ins, outs) = mkCheckpointEntity wid cp
let (cp', utxo, ins, outs) = mkCheckpointEntity wid cp
insert_ cp'
dbChunked insertMany_ ins
dbChunked insertMany_ outs
dbChunked insertMany_ pendings
dbChunked insertMany_ utxo
insertState (wid, (W.currentTip cp) ^. #slotId) (W.getState cp)

@@ -539,7 +531,6 @@ deleteCheckpoints
-> SqlPersistM ()
deleteCheckpoints wid = do
deleteWhere [UtxoTableWalletId ==. wid]
deleteWhere [PendingTxTableWalletId ==. wid]
deleteWhere [CheckpointTableWalletId ==. wid]
deleteState @s wid -- clear state

@@ -619,47 +610,48 @@ deleteMany filters entity types
deleteCascadeWhere ((entity <-. take chunkSize types):filters)
deleteMany filters entity (drop chunkSize types)

-- | Delete transactions that aren't referred to by either Pending or TxMeta of
-- any wallet.
-- | Delete transactions that aren't referred to by TxMeta of any wallet.
deleteLooseTransactions :: SqlPersistM ()
deleteLooseTransactions = do
deleteLoose "tx_in"
deleteLoose "tx_out"
where
-- Deletes all TxIn/TxOuts returned by the sub-select.
-- The sub-select outer joins PendingTx and TxMeta with TxIn/TxOut.
-- All rows of the join table with both PendingTx and TxMeta as NULL are
-- loose (unreferenced) transactions.
-- The sub-select outer joins TxMeta with TxIn/TxOut.
-- All rows of the join table TxMeta as NULL are loose (unreferenced)
-- transactions.
deleteLoose t = flip rawExecute [] $
"DELETE FROM "<> t <>" WHERE tx_id IN (" <>
"SELECT "<> t <>".tx_id FROM "<> t <>" " <>
"LEFT OUTER JOIN tx_meta ON tx_meta.tx_id = "<> t <>".tx_id " <>
"LEFT OUTER JOIN pending_tx ON pending_tx.tx_id = "<> t <>".tx_id " <>
"WHERE (tx_meta.tx_id IS NULL) AND (pending_tx.tx_id IS NULL))"
"WHERE (tx_meta.tx_id IS NULL))"


selectLatestCheckpoint
:: W.WalletId
-> SqlPersistM (Maybe Checkpoint)
selectLatestCheckpoint wid = fmap entityVal <$>
selectFirst [CheckpointTableWalletId ==. wid]
[LimitTo 1, Desc CheckpointTableSlot]
selectFirst
[ CheckpointTableWalletId ==. wid
] [ LimitTo 1, Desc CheckpointTableSlot ]

selectUTxO
:: Checkpoint
-> SqlPersistM [UTxO]
selectUTxO (Checkpoint wid sl _parent) = fmap entityVal <$>
selectList [UtxoTableWalletId ==. wid, UtxoTableCheckpointSlot ==. sl] []
selectList
[ UtxoTableWalletId ==. wid
, UtxoTableCheckpointSlot ==. sl
] []

selectPending
:: Checkpoint
-> SqlPersistM [TxId]
selectPending (Checkpoint wid sl _parent) =
fmap (pendingTxTableId2 . entityVal)
<$> selectList
[ PendingTxTableWalletId ==. wid
, PendingTxTableCheckpointSlot ==. sl
] []
selectPending (Checkpoint wid _ _) = fmap (txMetaTableTxId . entityVal) <$>
selectList
[ TxMetaTableWalletId ==. wid
, TxMetaTableStatus ==. W.Pending
] []

selectTxs
:: [TxId]
@@ -94,7 +94,7 @@ TxMeta
-- A transaction input associated with TxMeta.
--
-- There is no wallet ID because these values depend only on the transaction,
-- not the wallet. txInputTableTxId is referred to by TxMeta and PendingTx
-- not the wallet. txInputTableTxId is referred to by TxMeta
TxIn
txInputTableTxId TxId sql=tx_id
txInputTableOrder Int sql=order
@@ -107,7 +107,7 @@ TxIn
-- A transaction output associated with TxMeta.
--
-- There is no wallet ID because these values depend only on the transaction,
-- not the wallet. txOutputTableTxId is referred to by TxMeta and PendingTx
-- not the wallet. txOutputTableTxId is referred to by TxMeta
TxOut
txOutputTableTxId TxId sql=tx_id
txOutputTableIndex Word32 sql=index
@@ -157,20 +157,6 @@ UTxO sql=utxo
Foreign Checkpoint fk_checkpoint_utxo utxoTableWalletId utxoTableCheckpointSlot
deriving Show Generic

-- The pending transactions for a wallet checkpoint.
PendingTx

-- The wallet checkpoint (wallet_id, slot)
pendingTxTableWalletId W.WalletId sql=wallet_id
pendingTxTableCheckpointSlot W.SlotId sql=slot

-- Transaction TxIn and TxOut
pendingTxTableId2 TxId sql=tx_id

Primary pendingTxTableWalletId pendingTxTableCheckpointSlot pendingTxTableId2
Foreign Checkpoint fk_pending_tx pendingTxTableWalletId pendingTxTableCheckpointSlot
deriving Show Generic

-- State for sequential scheme address discovery
SeqState

0 comments on commit a4e00b4

Please sign in to comment.
You can’t perform that action at this time.