Skip to content

Commit

Permalink
Add ErrBadFormat exception
Browse files Browse the repository at this point in the history
To be thrown when the database file is in a bad format (missing tables, malformatted entries, other forms of corruption, …)
  • Loading branch information
HeinrichApfelmus committed Dec 22, 2021
1 parent a89063a commit cb103b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
11 changes: 11 additions & 0 deletions lib/core/src/Cardano/Wallet/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Cardano.Wallet.DB
, gapSize

-- * Errors
, ErrBadFormat(..)
, ErrNoSuchWallet(..)
, ErrWalletAlreadyExists(..)
, ErrNoSuchTransaction (..)
Expand Down Expand Up @@ -72,6 +73,8 @@ import Data.Quantity
( Quantity (..) )
import Data.Word
( Word32, Word8 )
import UnliftIO.Exception
( Exception )

import qualified Data.List as L

Expand Down Expand Up @@ -326,6 +329,14 @@ data DBLayer m s k = forall stm. (MonadIO stm, MonadFail stm) => DBLayer
-- ^ Execute operations of the database in isolation and atomically.
}

-- | Can't read the database file because it's in a bad format
-- (corrupted, too old, …)
data ErrBadFormat
= ErrBadFormatAddressState
deriving (Eq,Show)

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
Expand Down
29 changes: 18 additions & 11 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/CheckpointsOld.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import Cardano.Address.Script
( Cosigner (..), ScriptTemplate (..) )
import Cardano.DB.Sqlite
( dbChunked )
import Cardano.Wallet.DB
( ErrBadFormat (..) )
import Cardano.Wallet.DB.Checkpoints
( Checkpoints (..)
, DeltaCheckpoints (..)
Expand Down Expand Up @@ -103,7 +105,7 @@ import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
( MaybeT (..) )
import Data.Bifunctor
( second )
( bimap, second )
import Data.DBVar
( Store (..) )
import Data.Functor
Expand Down Expand Up @@ -140,6 +142,8 @@ import Database.Persist.Sql
)
import Database.Persist.Sqlite
( SqlPersistT )
import UnliftIO.Exception
( toException )

import qualified Cardano.Wallet.Primitive.AddressDerivation as W
import qualified Cardano.Wallet.Primitive.AddressDiscovery.Random as Rnd
Expand Down Expand Up @@ -199,9 +203,7 @@ mkStoreCheckpoints
mkStoreCheckpoints wid =
Store{ loadS = load, writeS = write, updateS = \_ -> update }
where
load = do
cps <- selectAllCheckpoints wid
pure $ Right $ loadCheckpoints cps
load = bimap toException loadCheckpoints <$> selectAllCheckpoints wid

write cps = forM_ (Map.toList $ checkpoints cps) $ \(pt,cp) ->
update (PutCheckpoint pt cp)
Expand Down Expand Up @@ -236,18 +238,23 @@ mkStoreCheckpoints wid =
selectAllCheckpoints
:: forall s. PersistAddressBook s
=> W.WalletId
-> SqlPersistT IO [(W.Slot, W.Wallet s)]
-> SqlPersistT IO (Either ErrBadFormat [(W.Slot, W.Wallet s)])
selectAllCheckpoints wid = do
cps <- fmap entityVal <$> selectList
[ CheckpointWalletId ==. wid ]
[ Desc CheckpointSlot ]
-- FIXME LATER during ADP-1043: Presence of these tables?
prologue <- fromJust <$> loadPrologue wid
forM cps $ \cp -> do
utxo <- selectUTxO cp
discoveries <- loadDiscoveries wid (checkpointSlot cp)
let st = withIso addressIso $ \_ from -> from (prologue, discoveries)
pure $ let c = checkpointFromEntity @s cp utxo st in (getPoint c, c)
mprologue <- loadPrologue wid
case mprologue of
Nothing -> pure $ Left ErrBadFormatAddressState
Just prologue -> fmap Right $
forM cps $ \cp -> do
utxo <- selectUTxO cp
discoveries <- loadDiscoveries wid (checkpointSlot cp)
let st = withIso addressIso $ \_ from ->
from (prologue, discoveries)
c = checkpointFromEntity @s cp utxo st
pure (getPoint c, c)

selectUTxO
:: Checkpoint
Expand Down

0 comments on commit cb103b4

Please sign in to comment.