Skip to content

Commit

Permalink
Merge #3543
Browse files Browse the repository at this point in the history
3543: [ADP-2322] Rename `TxHistory` model to `TxSet` r=paolino a=HeinrichApfelmus

This pull request renames the data type `TxHistory` from `Cardano.Wallet.DB.Store.Transactions.Model` to `TxPile`, as this better reflects the purpose of the data type — it's a pile of transactions.

### Issue Number

ADP-2322

Co-authored-by: Heinrich Apfelmus <heinrich.apfelmus@iohk.io>
  • Loading branch information
iohk-bors[bot] and HeinrichApfelmus committed Oct 27, 2022
2 parents d3c7e4d + 91e83e4 commit 562b727
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 131 deletions.
14 changes: 7 additions & 7 deletions lib/wallet/src/Cardano/Wallet/DB/Layer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import Cardano.Wallet.DB.Sqlite.Schema
import Cardano.Wallet.DB.Sqlite.Types
( BlockId (..), TxId (..) )
import Cardano.Wallet.DB.Store.CBOR.Model
( TxCBORHistory (..) )
( TxCBORSet (..) )
import Cardano.Wallet.DB.Store.Checkpoints
( PersistAddressBook (..), blockHeaderFromEntity, mkStoreWallets )
import Cardano.Wallet.DB.Store.Meta.Model
Expand All @@ -114,9 +114,9 @@ import Cardano.Wallet.DB.Store.Meta.Model
import Cardano.Wallet.DB.Store.Submissions.Model
( TxLocalSubmissionHistory (..) )
import Cardano.Wallet.DB.Store.Transactions.Model
( TxHistory (..), decorateTxIns, withdrawals )
( TxSet (..), decorateTxIns, withdrawals )
import Cardano.Wallet.DB.Store.TransactionsWithCBOR.Model
( TxHistoryWithCBOR (TxHistoryWithCBOR) )
( TxSetWithCBOR (..) )
import Cardano.Wallet.DB.Store.Wallets.Model
( DeltaWalletsMetaWithSubmissions (..)
, TxWalletsHistory
Expand Down Expand Up @@ -1054,7 +1054,7 @@ selectTxHistory
-> TxWalletsHistory
-> m [W.TransactionInfo]
selectTxHistory tip ti wid minWithdrawal order whichMeta
(TxHistoryWithCBOR txHistory (TxCBORHistory txCBORHistory), wmetas) = do
(TxSetWithCBOR txSet (TxCBORSet txCBORSet), wmetas) = do
tinfos <- sequence $ do
(TxMetaHistory metas, _) <- maybeToList $ Map.lookup wid wmetas
meta <- toList metas
Expand All @@ -1065,12 +1065,12 @@ selectTxHistory tip ti wid minWithdrawal order whichMeta
(\coin -> any (>= coin)
$ txWithdrawalAmount <$> withdrawals transaction)
minWithdrawal
let decoration = decorateTxIns txHistory transaction
let decoration = decorateTxIns txSet transaction
pure $ mkTransactionInfo
ti tip
transaction
decoration
(Map.lookup (txMetaTxId meta) txCBORHistory)
(Map.lookup (txMetaTxId meta) txCBORSet)
meta
pure $ sortTx tinfos
where
Expand All @@ -1079,7 +1079,7 @@ selectTxHistory tip ti wid minWithdrawal order whichMeta
$ (,) <$> slotNo . txInfoMeta <*> Down . txInfoId
W.Descending -> sortOn
$ (,) <$> (Down . slotNo . txInfoMeta) <*> txInfoId
TxHistory txs = txHistory
TxSet txs = txSet


-- | Returns the initial submission slot and submission record for all pending
Expand Down
12 changes: 6 additions & 6 deletions lib/wallet/src/Cardano/Wallet/DB/Store/CBOR/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# LANGUAGE TypeFamilies #-}

module Cardano.Wallet.DB.Store.CBOR.Model
( TxCBORHistory (..)
( TxCBORSet (..)
, DeltaTxCBOR (..)
)
where
Expand All @@ -24,12 +24,12 @@ import Fmt
import GHC.Generics
( Generic )

newtype TxCBORHistory =
TxCBORHistory {relations :: Map TxId TxCBOR}
newtype TxCBORSet =
TxCBORSet {relations :: Map TxId TxCBOR}
deriving ( Eq, Show, Generic, Monoid, Semigroup )

data DeltaTxCBOR
= Append TxCBORHistory
= Append TxCBORSet
-- ^ Add or overwrite (by id) transactions cbor.
| DeleteTx TxId
-- ^ Remove cbor by transaction id.
Expand All @@ -39,7 +39,7 @@ instance Buildable DeltaTxCBOR where
build = build . show

instance Delta DeltaTxCBOR where
type Base DeltaTxCBOR = TxCBORHistory
type Base DeltaTxCBOR = TxCBORSet
apply (Append addendum) x = addendum <> x
apply (DeleteTx tid) (TxCBORHistory m) = TxCBORHistory
apply (DeleteTx tid) (TxCBORSet m) = TxCBORSet
$ Map.delete tid m
10 changes: 5 additions & 5 deletions lib/wallet/src/Cardano/Wallet/DB/Store/CBOR/Store.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Copyright: 2022 IOHK
License: Apache-2.0
Implementation of a 'Store' for 'TxCBORHistory'.
Implementation of a 'Store' for 'TxCBORSet'.
-}

Expand All @@ -24,7 +24,7 @@ import Cardano.Wallet.DB.Sqlite.Schema
import Cardano.Wallet.DB.Sqlite.Types
( TxId (..) )
import Cardano.Wallet.DB.Store.CBOR.Model
( DeltaTxCBOR (..), TxCBORHistory (..) )
( DeltaTxCBOR (..), TxCBORSet (..) )
import Cardano.Wallet.Read.Eras
import Cardano.Wallet.Read.Tx.CBOR
( TxCBOR )
Expand Down Expand Up @@ -76,8 +76,8 @@ fromTxCBOR :: CBOR -> Either (CBOR, TxCBORRaw ) (TxId, TxCBOR)
fromTxCBOR s@(CBOR {..}) = bimap (s ,) (cborTxId ,) $
match eraValueSerialize $ (cborTxCBOR, cborTxEra) ^. fromIso i

repsertCBORs :: TxCBORHistory -> SqlPersistT IO ()
repsertCBORs (TxCBORHistory txs) =
repsertCBORs :: TxCBORSet -> SqlPersistT IO ()
repsertCBORs (TxCBORSet txs) =
repsertMany
[(fromJust keyFromRecordM x, x)
| x <- fst . toTxCBOR <$> Map.assocs txs
Expand All @@ -94,7 +94,7 @@ mkStoreCBOR = Store
cbors <- selectList [] []
pure $ first (SomeException . CBOROutOfEra . snd) $ do
ps <- mapM (fromTxCBOR . entityVal) cbors
pure . TxCBORHistory . Map.fromList $ ps
pure . TxCBORSet . Map.fromList $ ps
, writeS = \txs -> do
repsertCBORs txs
, updateS = \_ -> \case
Expand Down
58 changes: 29 additions & 29 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
Copyright: © 2022 IOHK
License: Apache-2.0
Data type 'TxHistory' for storing a set of transactions.
Data type 'TxSet' for storing a set of transactions.
Transactions are encoded "as" expressed in DB tables.
-}
module Cardano.Wallet.DB.Store.Transactions.Model
( DeltaTxHistory (..)
, TxHistory (..)
( DeltaTxSet (..)
, TxSet (..)
, TxRelation (..)
, tokenCollateralOrd
, tokenOutOrd
, mkTxHistory
, mkTxSet

-- * Decoration
, DecoratedTxIns
Expand Down Expand Up @@ -105,40 +105,40 @@ data TxRelation =
}
deriving ( Generic, Eq, Show )

-- | Transactions history is 'TxRelation's indexed by 'TxId'
newtype TxHistory =
TxHistory { relations :: Map TxId TxRelation }
-- | A 'TxSet' is a map of 'TxRelation's indexed by their 'TxId'.
newtype TxSet =
TxSet { relations :: Map TxId TxRelation }
deriving ( Generic, Eq, Show )

instance Monoid TxHistory where
mempty = TxHistory mempty
instance Monoid TxSet where
mempty = TxSet mempty

instance Semigroup TxHistory where
TxHistory h1 <> TxHistory h2 =
TxHistory $ h1 <> h2
instance Semigroup TxSet where
TxSet h1 <> TxSet h2 =
TxSet $ h1 <> h2

instance Buildable TxHistory where
build txs = "TxHistory " <> build (show $ relations txs)
instance Buildable TxSet where
build txs = "TxSet " <> build (show $ relations txs)

-- | Verbs to change a 'TxHistory'.
data DeltaTxHistory
= Append TxHistory
-- | Verbs to change a 'TxSet'.
data DeltaTxSet
= Append TxSet
-- ^ Add new set of transactions.
-- Overwrites transactions whose id is already present in the 'TxHistory'.
-- /Overwrites/ transactions whose id is already present in the 'TxSet'.
| DeleteTx TxId
-- ^ Try to remove the transaction at the given transaction id.
deriving ( Show, Eq, Generic )

instance Buildable DeltaTxHistory where
instance Buildable DeltaTxSet where
build action = build $ show action

instance Delta DeltaTxHistory where
type Base DeltaTxHistory = TxHistory
instance Delta DeltaTxSet where
type Base DeltaTxSet = TxSet
-- transactions are immutable so here there should happen no rewriting
-- but we mimic the repsert in the store
apply (Append txs) h = txs <> h
apply (DeleteTx tid) (TxHistory txs) =
TxHistory $ Map.delete tid txs
apply (DeleteTx tid) (TxSet txs) =
TxSet $ Map.delete tid txs

{-------------------------------------------------------------------------------
Type conversions
Expand Down Expand Up @@ -253,9 +253,9 @@ mkTxRelation tx =
indexed :: (Enum a, Num a) => [b] -> [(a, b)]
indexed = zip [0 .. ]

-- | Convert high level transactions definition in low level DB history
mkTxHistory :: [W.Tx] -> TxHistory
mkTxHistory txs = TxHistory $ fold $ do
-- | Convert high level transactions definition in low level 'TxSet'.
mkTxSet :: [W.Tx] -> TxSet
mkTxSet txs = TxSet $ fold $ do
tx <- txs
let relation = mkTxRelation tx
pure $ Map.singleton (TxId $ tx ^. #txId) relation
Expand Down Expand Up @@ -330,10 +330,10 @@ lookupTxOutForTxCollateral tx =
Map.lookup (toKeyTxCollateral tx) . unDecoratedTxIns

-- | Decorate the Tx inputs of a given 'TxRelation'
-- by searching the 'TxHistory' for corresponding output values.
-- by searching the 'TxSet' for corresponding output values.
decorateTxIns
:: TxHistory -> TxRelation -> DecoratedTxIns
decorateTxIns (TxHistory relations) TxRelation{ins,collateralIns} =
:: TxSet -> TxRelation -> DecoratedTxIns
decorateTxIns (TxSet relations) TxRelation{ins,collateralIns} =
DecoratedTxIns . Map.fromList . catMaybes $
(lookupOutput . toKeyTxIn <$> ins)
++ (lookupOutput . toKeyTxCollateral <$> collateralIns)
Expand Down
35 changes: 17 additions & 18 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Store.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
Copyright: 2022 IOHK
License: Apache-2.0
Implementation of a 'Store' for 'TxHistory'.
Implementation of a 'Store' for 'TxSet'.
-}
module Cardano.Wallet.DB.Store.Transactions.Store
( selectTxHistory
, putTxHistory
( selectTxSet
, putTxSet
, mkStoreTransactions ) where

import Prelude
Expand All @@ -33,9 +33,9 @@ import Cardano.Wallet.DB.Sqlite.Schema
import Cardano.Wallet.DB.Sqlite.Types
( TxId )
import Cardano.Wallet.DB.Store.Transactions.Model
( DeltaTxHistory (..)
, TxHistory (..)
( DeltaTxSet (..)
, TxRelation (..)
, TxSet (..)
, tokenCollateralOrd
, tokenOutOrd
)
Expand Down Expand Up @@ -69,17 +69,17 @@ import Database.Persist.Sql
import qualified Data.Map.Strict as Map

mkStoreTransactions
:: Store (SqlPersistT IO) DeltaTxHistory
:: Store (SqlPersistT IO) DeltaTxSet
mkStoreTransactions =
Store
{ loadS = Right <$> selectTxHistory
{ loadS = Right <$> selectTxSet
, writeS = write
, updateS = update
}

update :: TxHistory -> DeltaTxHistory -> SqlPersistT IO ()
update :: TxSet -> DeltaTxSet -> SqlPersistT IO ()
update _ change = case change of
Append txs -> putTxHistory txs
Append txs -> putTxSet txs
DeleteTx tid -> do
deleteWhere [TxInputTxId ==. tid ]
deleteWhere [TxCollateralTxId ==. tid ]
Expand All @@ -89,7 +89,7 @@ update _ change = case change of
deleteWhere [TxCollateralOutTxId ==. tid ]
deleteWhere [TxWithdrawalTxId ==. tid ]

write :: TxHistory -> SqlPersistT IO ()
write :: TxSet -> SqlPersistT IO ()
write txs = do
deleteWhere @_ @_ @TxIn mempty
deleteWhere @_ @_ @TxCollateral mempty
Expand All @@ -98,12 +98,11 @@ write txs = do
deleteWhere @_ @_ @TxCollateralOutToken mempty
deleteWhere @_ @_ @TxCollateralOut mempty
deleteWhere @_ @_ @TxWithdrawal mempty
putTxHistory txs
putTxSet txs


-- | Insert multiple transactions
putTxHistory :: TxHistory -> SqlPersistT IO ()
putTxHistory (TxHistory tx_map) = forM_ tx_map $ \TxRelation {..} -> do
-- | Insert multiple transactions.
putTxSet:: TxSet -> SqlPersistT IO ()
putTxSet (TxSet tx_map) = forM_ tx_map $ \TxRelation {..} -> do
repsertMany' ins
repsertMany' collateralIns
repsertMany' $ fst <$> outs
Expand All @@ -119,9 +118,9 @@ putTxHistory (TxHistory tx_map) = forM_ tx_map $ \TxRelation {..} -> do
-- needed to submit large numberot transactions
chunked f xs = mapM_ f (chunksOf 1000 xs)

-- | Select transactions history from the database
selectTxHistory :: SqlPersistT IO TxHistory
selectTxHistory = TxHistory <$> select
-- | Select transactions from the database.
selectTxSet :: SqlPersistT IO TxSet
selectTxSet = TxSet <$> select
where
selectListAll = selectList [] []
select :: SqlPersistT IO (Map TxId TxRelation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{-# LANGUAGE TypeFamilies #-}

module Cardano.Wallet.DB.Store.TransactionsWithCBOR.Model
( TxHistoryWithCBOR (..)
( TxSetWithCBOR (..)
, DeltaTx (..)
)
where
Expand All @@ -12,9 +12,9 @@ import Prelude
import Cardano.Wallet.DB.Sqlite.Types
( TxId )
import Cardano.Wallet.DB.Store.CBOR.Model
( TxCBORHistory )
( TxCBORSet )
import Cardano.Wallet.DB.Store.Transactions.Model
( TxHistory )
( TxSet )
import Data.Delta
( Delta (..) )
import Fmt
Expand All @@ -25,22 +25,22 @@ import GHC.Generics
import qualified Cardano.Wallet.DB.Store.CBOR.Model as CBOR
import qualified Cardano.Wallet.DB.Store.Transactions.Model as Txs

data TxHistoryWithCBOR =
TxHistoryWithCBOR
{ txHistory :: !TxHistory
, txCBORs :: !TxCBORHistory
data TxSetWithCBOR =
TxSetWithCBOR
{ txHistory :: !TxSet
, txCBORs :: !TxCBORSet
}
deriving ( Eq, Show, Generic )

instance Monoid TxHistoryWithCBOR where
mempty = TxHistoryWithCBOR mempty mempty
instance Monoid TxSetWithCBOR where
mempty = TxSetWithCBOR mempty mempty

instance Semigroup TxHistoryWithCBOR where
TxHistoryWithCBOR tx cb <> TxHistoryWithCBOR tx' cb' =
TxHistoryWithCBOR (tx <> tx') (cb <> cb')
instance Semigroup TxSetWithCBOR where
TxSetWithCBOR tx cb <> TxSetWithCBOR tx' cb' =
TxSetWithCBOR (tx <> tx') (cb <> cb')

data DeltaTx
= Append TxHistoryWithCBOR
= Append TxSetWithCBOR
-- ^ Add or overwrite (by id) transactions history.
| DeleteTx TxId
-- ^ Remove transaction by id.
Expand All @@ -50,12 +50,12 @@ instance Buildable DeltaTx where
build = build . show

instance Delta DeltaTx where
type Base DeltaTx = TxHistoryWithCBOR
apply (Append (TxHistoryWithCBOR oldtxs oldcbors))
(TxHistoryWithCBOR newtxs newcbors)
= TxHistoryWithCBOR
type Base DeltaTx = TxSetWithCBOR
apply (Append (TxSetWithCBOR oldtxs oldcbors))
(TxSetWithCBOR newtxs newcbors)
= TxSetWithCBOR
(apply (Txs.Append newtxs) oldtxs)
(apply (CBOR.Append newcbors) oldcbors)
apply (DeleteTx tid) (TxHistoryWithCBOR txs bors) = TxHistoryWithCBOR
apply (DeleteTx tid) (TxSetWithCBOR txs bors) = TxSetWithCBOR
(apply (Txs.DeleteTx tid) txs)
(apply (CBOR.DeleteTx tid) bors)

0 comments on commit 562b727

Please sign in to comment.