Skip to content

Commit

Permalink
Move adjustNoSuchWallet and ErrNoSuchWallet
Browse files Browse the repository at this point in the history
… to the `Cardano.Wallet.DB.WalletState` module.
  • Loading branch information
HeinrichApfelmus committed Jun 20, 2022
1 parent 69ce334 commit 3c3f667
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 28 deletions.
21 changes: 7 additions & 14 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ import Cardano.Wallet.CoinSelection.Internal.Balance
import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet (..)
, ErrPutLocalTxSubmission (..)
, ErrRemoveTx (..)
, ErrWalletAlreadyExists (..)
Expand All @@ -264,7 +263,13 @@ import Cardano.Wallet.DB
, sparseCheckpoints
)
import Cardano.Wallet.DB.WalletState
( DeltaMap (..), DeltaWalletState1 (..), fromWallet, getLatest, getSlot )
( DeltaWalletState1 (..)
, ErrNoSuchWallet (..)
, adjustNoSuchWallet
, fromWallet
, getLatest
, getSlot
)
import Cardano.Wallet.Logging
( BracketLog
, BracketLog' (..)
Expand Down Expand Up @@ -1492,18 +1497,6 @@ importRandomAddresses ctx wid addrs = db & \DBLayer{..} ->
s0 = getState $ getLatest wal
es1 = foldl' (\s addr -> s >>= Rnd.importAddress addr) (Right s0) addrs

-- | Adjust a specific wallet if it exists or return 'ErrNoSuchWallet'.
adjustNoSuchWallet
:: WalletId
-> (ErrNoSuchWallet -> e)
-> (w -> Either e (dw, b))
-> (Map WalletId w -> (Maybe (DeltaMap WalletId dw), Either e b))
adjustNoSuchWallet wid err update wallets = case Map.lookup wid wallets of
Nothing -> (Nothing, Left $ err $ ErrNoSuchWallet wid)
Just wal -> case update wal of
Left e -> (Nothing, Left e)
Right (dw, b) -> (Just $ Adjust wid dw, Right b)

-- NOTE
-- Addresses coming from the transaction history might be payment or
-- delegation addresses. So we normalize them all to be delegation addresses
Expand Down
8 changes: 1 addition & 7 deletions lib/core/src/Cardano/Wallet/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module Cardano.Wallet.DB

-- * Errors
, ErrBadFormat(..)
, ErrNoSuchWallet(..)
, ErrWalletAlreadyExists(..)
, ErrNoSuchTransaction (..)
, ErrRemoveTx (..)
Expand All @@ -39,7 +38,7 @@ import Prelude
import Cardano.Address.Derivation
( XPrv )
import Cardano.Wallet.DB.WalletState
( DeltaMap, DeltaWalletState )
( DeltaMap, DeltaWalletState, ErrNoSuchWallet (..) )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..) )
import Cardano.Wallet.Primitive.Model
Expand Down Expand Up @@ -353,11 +352,6 @@ data ErrBadFormat

instance Exception ErrBadFormat

-- | Can't perform given operation because there's no wallet
newtype ErrNoSuchWallet
= ErrNoSuchWallet WalletId -- Wallet is gone or doesn't exist yet
deriving (Eq, Show)

-- | Can't add a transaction to the local tx submission pool.
data ErrPutLocalTxSubmission
= ErrPutLocalTxSubmissionNoSuchWallet ErrNoSuchWallet
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/DB/Layer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import Cardano.Wallet.DB
( DBFactory (..)
, DBLayer (..)
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet (..)
, ErrPutLocalTxSubmission (..)
, ErrRemoveTx (..)
, ErrWalletAlreadyExists (..)
Expand Down Expand Up @@ -119,6 +118,7 @@ import Cardano.Wallet.DB.Store.Checkpoints
import Cardano.Wallet.DB.WalletState
( DeltaMap (..)
, DeltaWalletState1 (..)
, ErrNoSuchWallet (..)
, findNearestPoint
, fromGenesis
, fromWallet
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/Cardano/Wallet/DB/Pure/Layer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Cardano.Address.Derivation
import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet (..)
, ErrPutLocalTxSubmission (..)
, ErrRemoveTx (..)
, ErrWalletAlreadyExists (..)
Expand Down Expand Up @@ -58,6 +57,8 @@ import Cardano.Wallet.DB.Pure.Implementation
, mRollbackTo
, mUpdatePendingTxForExpiry
)
import Cardano.Wallet.DB.WalletState
( ErrNoSuchWallet (..) )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..) )
import Cardano.Wallet.Primitive.Passphrase
Expand Down
32 changes: 31 additions & 1 deletion lib/core/src/Cardano/Wallet/DB/WalletState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ module Cardano.Wallet.DB.WalletState
-- * Delta types
, DeltaWalletState1 (..)
, DeltaWalletState

-- * Multiple wallets
, DeltaMap (..)
, ErrNoSuchWallet (..)
, adjustNoSuchWallet
) where

import Prelude
Expand All @@ -41,7 +45,7 @@ import Cardano.Wallet.Address.Book
import Cardano.Wallet.Checkpoints
( Checkpoints )
import Cardano.Wallet.Primitive.Types
( BlockHeader )
( BlockHeader, WalletId )
import Cardano.Wallet.Primitive.Types.UTxO
( UTxO )
import Data.Delta
Expand All @@ -52,6 +56,8 @@ import Data.Generics.Internal.VL
( withIso )
import Data.Generics.Internal.VL.Lens
( over, view, (^.) )
import Data.Map.Strict
( Map )
import Data.Word
( Word32 )
import Fmt
Expand All @@ -62,6 +68,7 @@ import GHC.Generics
import qualified Cardano.Wallet.Checkpoints as CPS
import qualified Cardano.Wallet.Primitive.Model as W
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Data.Map.Strict as Map

{-------------------------------------------------------------------------------
Wallet Checkpoint
Expand Down Expand Up @@ -155,3 +162,26 @@ instance Buildable (DeltaWalletState1 s) where

instance Show (DeltaWalletState1 s) where
show = pretty

{-------------------------------------------------------------------------------
Multiple wallets.
-------------------------------------------------------------------------------}
-- | Adjust a specific wallet if it exists or return 'ErrNoSuchWallet'.
adjustNoSuchWallet
:: WalletId
-> (ErrNoSuchWallet -> e)
-> (w -> Either e (dw, b))
-> (Map WalletId w -> (Maybe (DeltaMap WalletId dw), Either e b))
adjustNoSuchWallet wid err update wallets = case Map.lookup wid wallets of
Nothing -> (Nothing, Left $ err $ ErrNoSuchWallet wid)
Just wal -> case update wal of
Left e -> (Nothing, Left e)
Right (dw, b) -> (Just $ Adjust wid dw, Right b)

{-------------------------------------------------------------------------------
Errors
-------------------------------------------------------------------------------}
-- | Can't perform given operation because there's no wallet
newtype ErrNoSuchWallet
= ErrNoSuchWallet WalletId -- Wallet is gone or doesn't exist yet
deriving (Eq, Show)
4 changes: 3 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/LayerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import Cardano.DB.Sqlite
import Cardano.Mnemonic
( SomeMnemonic (..) )
import Cardano.Wallet.DB
( DBFactory (..), DBLayer (..), ErrNoSuchWallet (..), cleanDB )
( DBFactory (..), DBLayer (..), cleanDB )
import Cardano.Wallet.DB.Arbitrary
( GenState, KeyValPairs (..) )
import Cardano.Wallet.DB.Layer
Expand All @@ -73,6 +73,8 @@ import Cardano.Wallet.DB.Sqlite.Migration
)
import Cardano.Wallet.DB.StateMachine
( TestConstraints, prop_parallel, prop_sequential, validateGenerators )
import Cardano.Wallet.DB.WalletState
( ErrNoSuchWallet (..) )
import Cardano.Wallet.DummyTarget.Primitive.Types
( block0, dummyGenesisParameters, dummyTimeInterpreter )
import Cardano.Wallet.Gen
Expand Down
3 changes: 2 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Prelude

import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchWallet (..)
, ErrWalletAlreadyExists (..)
, SparseCheckpointsConfig (..)
, cleanDB
Expand All @@ -41,6 +40,8 @@ import Cardano.Wallet.DB.Arbitrary
)
import Cardano.Wallet.DB.Pure.Implementation
( filterTxHistory )
import Cardano.Wallet.DB.WalletState
( ErrNoSuchWallet (..) )
import Cardano.Wallet.DummyTarget.Primitive.Types
( dummyGenesisParameters )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
Expand Down
3 changes: 2 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/StateMachine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import Cardano.Address.Script
import Cardano.Wallet.DB
( DBLayer (..)
, ErrNoSuchTransaction (..)
, ErrNoSuchWallet (..)
, ErrPutLocalTxSubmission (..)
, ErrRemoveTx (..)
, ErrWalletAlreadyExists (..)
Expand Down Expand Up @@ -99,6 +98,8 @@ import Cardano.Wallet.DB.Pure.Implementation
, mRollbackTo
, mUpdatePendingTxForExpiry
)
import Cardano.Wallet.DB.WalletState
( ErrNoSuchWallet (..) )
import Cardano.Wallet.DummyTarget.Primitive.Types
( dummyGenesisParameters, dummyTimeInterpreter )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down
4 changes: 3 additions & 1 deletion lib/core/test/unit/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ import Cardano.Wallet.CoinSelection
, SelectionError (..)
)
import Cardano.Wallet.DB
( DBLayer (..), ErrNoSuchWallet (..), putTxHistory )
( DBLayer (..), putTxHistory )
import Cardano.Wallet.DB.WalletState
( ErrNoSuchWallet (..) )
import Cardano.Wallet.DummyTarget.Primitive.Types
( block0
, dummyNetworkLayer
Expand Down

0 comments on commit 3c3f667

Please sign in to comment.