-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3374: [ADP-1983] implement local tx submission store r=paolino a=paolino - [x] implement store expansion - [x] implement store pruning - [x] implement store querying - [x] couple this store with meta transactions store - [x] change the db layer implementation ### Comments DB properties/specs seems only covered in the state machine tests ### Issue Number ADP-1983 Co-authored-by: paolo veronelli <paolo.veronelli@gmail.com>
- Loading branch information
Showing
10 changed files
with
469 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
{- | | ||
Copyright: © 2022 IOHK | ||
License: Apache-2.0 | ||
Data type 'TxLocalSubmissionHistory' for storing a set of submitted transactions. | ||
Transactions are encoded "as" expressed in DB tables. | ||
-} | ||
module Cardano.Wallet.DB.Store.Submissions.Model | ||
( TxLocalSubmissionHistory (..) | ||
, DeltaTxLocalSubmission (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.DB.Sqlite.Schema | ||
( LocalTxSubmission (..) ) | ||
import Cardano.Wallet.DB.Sqlite.Types | ||
( TxId ) | ||
import Data.Delta | ||
( Delta (..) ) | ||
import Data.Map.Strict | ||
( Map ) | ||
import Fmt | ||
( Buildable (..) ) | ||
import GHC.Generics | ||
( Generic ) | ||
|
||
import qualified Data.Map.Strict as Map | ||
import qualified Data.Set as Set | ||
|
||
-- | All transactions that the wallet has submitted to a node, | ||
-- indexed by transaction id. | ||
-- | ||
-- Typically, transactions are submitted through the | ||
-- LocalTxSubmission mini-protocol, hence the name of this type. | ||
newtype TxLocalSubmissionHistory = | ||
TxLocalSubmissionHistory {relations :: Map TxId LocalTxSubmission} | ||
deriving ( Eq, Show, Generic, Monoid, Semigroup ) | ||
|
||
data DeltaTxLocalSubmission | ||
= Expand TxLocalSubmissionHistory | ||
-- ^ Add or overwrite (by id) local-submission-transactions. | ||
| Prune [TxId] | ||
-- ^ Remove submissions by id. | ||
deriving ( Eq, Show, Generic ) | ||
|
||
instance Buildable DeltaTxLocalSubmission where | ||
build = build . show | ||
|
||
instance Delta DeltaTxLocalSubmission where | ||
type Base DeltaTxLocalSubmission = TxLocalSubmissionHistory | ||
apply (Expand addendum) x = addendum <> x | ||
apply (Prune tids) (TxLocalSubmissionHistory m) = TxLocalSubmissionHistory | ||
$ Map.withoutKeys m (Set.fromList tids) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
{- | | ||
Copyright: 2022 IOHK | ||
License: Apache-2.0 | ||
Implementation of a 'Store' for 'TxLocalSubmissionHistory'. | ||
-} | ||
|
||
module Cardano.Wallet.DB.Store.Submissions.Store ( mkStoreSubmissions ) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.DB.Sqlite.Schema | ||
( EntityField (..), LocalTxSubmission (..) ) | ||
import Cardano.Wallet.DB.Store.Submissions.Model | ||
( DeltaTxLocalSubmission (..), TxLocalSubmissionHistory (..) ) | ||
import Cardano.Wallet.Primitive.Types | ||
( WalletId ) | ||
import Control.Arrow | ||
( (&&&) ) | ||
import Control.Monad | ||
( forM_ ) | ||
import Data.DBVar | ||
( Store (..) ) | ||
import Data.Foldable | ||
( toList ) | ||
import Data.Maybe | ||
( fromJust ) | ||
import Database.Persist | ||
( PersistEntity (keyFromRecordM) | ||
, PersistQueryWrite (deleteWhere) | ||
, PersistStoreWrite (repsertMany) | ||
, entityVal | ||
, selectList | ||
, (==.) | ||
) | ||
import Database.Persist.Sql | ||
( SqlPersistT ) | ||
|
||
import qualified Data.Map.Strict as Map | ||
|
||
repsertLocalSubmissions :: TxLocalSubmissionHistory -> SqlPersistT IO () | ||
repsertLocalSubmissions | ||
(TxLocalSubmissionHistory txs) = | ||
repsertMany [(fromJust keyFromRecordM x, x) | x <- toList txs ] | ||
|
||
mkStoreSubmissions :: WalletId | ||
-> Store (SqlPersistT IO) DeltaTxLocalSubmission | ||
mkStoreSubmissions wid = | ||
Store | ||
{ loadS = Right | ||
. TxLocalSubmissionHistory | ||
. Map.fromList | ||
. fmap ((localTxSubmissionTxId &&& id) . entityVal) | ||
<$> selectList [LocalTxSubmissionWalletId ==. wid ] [] | ||
, writeS = \txs -> do | ||
deleteWhere [LocalTxSubmissionWalletId ==. wid ] | ||
repsertLocalSubmissions txs | ||
, updateS = \_ -> \case | ||
Expand addendum -> repsertLocalSubmissions addendum | ||
Prune tids -> forM_ tids $ \tid -> deleteWhere | ||
[ LocalTxSubmissionWalletId ==. wid | ||
, LocalTxSubmissionTxId ==. tid | ||
] | ||
} | ||
|
Oops, something went wrong.