From 159c9f47d91ff88264fbfaba1754da22b3058eb1 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Mon, 16 Dec 2019 18:42:14 +0100 Subject: [PATCH 1/2] re-use 'deleteWallet' function to avoid logic duplication And seemingly, share fixes when they are done --- lib/core/src/Cardano/Wallet/Api/Server.hs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/Api/Server.hs b/lib/core/src/Cardano/Wallet/Api/Server.hs index e9c7101bcaf..fcc2f9eb0f7 100644 --- a/lib/core/src/Cardano/Wallet/Api/Server.hs +++ b/lib/core/src/Cardano/Wallet/Api/Server.hs @@ -470,9 +470,8 @@ wallets ctx = :<|> getUTxOsStatistics ctx deleteWallet - :: forall ctx s t k n. - ( s ~ SeqState n k - , ctx ~ ApiLayer s t k + :: forall ctx s t k. + ( ctx ~ ApiLayer s t k ) => ctx -> ApiT WalletId @@ -1009,15 +1008,8 @@ deleteByronWallet => ApiLayer s t k -> ApiT WalletId -> Handler NoContent -deleteByronWallet ctx (ApiT wid) = do - liftHandler $ withWorkerCtx ctx wid throwE $ - \worker -> W.deleteWallet worker wid - liftIO $ Registry.remove re wid - liftIO $ (df ^. #removeDatabase) wid - return NoContent - where - re = ctx ^. workerRegistry @s @k - df = ctx ^. dbFactory @s @k +deleteByronWallet = + deleteWallet getByronWallet :: forall t k. () From 46391803d2a35aa7608d46988411bed13d98b92c Mon Sep 17 00:00:00 2001 From: KtorZ Date: Mon, 16 Dec 2019 19:36:02 +0100 Subject: [PATCH 2/2] actually create one MVar per DB context ... --- lib/core/src/Cardano/Wallet/DB/Sqlite.hs | 19 ++++++++++++------- .../test/unit/Cardano/Wallet/DB/SqliteSpec.hs | 5 +++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/DB/Sqlite.hs b/lib/core/src/Cardano/Wallet/DB/Sqlite.hs index 6fc72584a8c..63e8c05b458 100644 --- a/lib/core/src/Cardano/Wallet/DB/Sqlite.hs +++ b/lib/core/src/Cardano/Wallet/DB/Sqlite.hs @@ -92,7 +92,7 @@ import Cardano.Wallet.Primitive.AddressDiscovery import Control.Arrow ( (***) ) import Control.Concurrent.MVar - ( newEmptyMVar, putMVar, readMVar ) + ( modifyMVar_, newMVar ) import Control.DeepSeq ( NFData ) import Control.Exception @@ -111,6 +111,8 @@ import Data.Coerce ( coerce ) import Data.Either ( isRight ) +import Data.Functor + ( ($>) ) import Data.Generics.Internal.VL.Lens ( (^.) ) import Data.List.Split @@ -220,7 +222,7 @@ newDBFactory -- ^ Path to database directory, or Nothing for in-memory database -> IO (DBFactory IO s k) newDBFactory cfg tr mDatabaseDir = do - mvar <- newEmptyMVar + mvar <- newMVar mempty case mDatabaseDir of Nothing -> pure DBFactory { withDatabase = \_ action -> @@ -229,17 +231,20 @@ newDBFactory cfg tr mDatabaseDir = do pure () } Just databaseDir -> pure DBFactory - { withDatabase = \wid action -> - withDBLayer cfg tracerDB (Just $ databaseFile wid) $ \(ctx,db) -> do - putMVar mvar ctx - action db + { withDatabase = \wid action -> do + withDBLayer cfg tracerDB (Just $ databaseFile wid) + $ \(ctx, db) -> do + modifyMVar_ mvar (pure . Map.insert wid ctx) + action db , removeDatabase = \wid -> do let files = [ databaseFile wid , databaseFile wid <> "-wal" , databaseFile wid <> "-shm" ] - readMVar mvar >>= destroyDBLayer + modifyMVar_ mvar $ \m -> case Map.lookup wid m of + Nothing -> pure m + Just ctx -> destroyDBLayer ctx $> Map.delete wid m mapM_ removePathForcibly files } where diff --git a/lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs b/lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs index f58961a9fe2..9705f7e85c8 100644 --- a/lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs @@ -113,6 +113,8 @@ import Cardano.Wallet.Unsafe ( unsafeRunExceptT ) import Control.Concurrent ( forkIO ) +import Control.Concurrent.MVar + ( newEmptyMVar, takeMVar, tryPutMVar ) import Control.DeepSeq ( NFData ) import Control.Exception @@ -356,6 +358,7 @@ fileModeSpec = do withSystemTempDirectory "DBFactory" $ \dir -> do cfg <- defaultConfigTesting DBFactory{..} <- newDBFactory cfg nullTracer (Just dir) + mvar <- newEmptyMVar -- NOTE -- Start a concurrent worker which makes action on the DB in @@ -363,8 +366,10 @@ fileModeSpec = do _ <- forkIO $ withDatabase testWid $ \(DBLayer{..} :: TestDBSeq) -> do forever $ do cp <- atomically $ readCheckpoint $ PrimaryKey testWid + _ <- tryPutMVar mvar () B8.putStrLn (B8.pack $ show cp) + takeMVar mvar removeDatabase testWid listDirectory dir `shouldReturn` mempty