Skip to content

Commit

Permalink
redesign how DBLayer will be reused for SharedState - resign from sta…
Browse files Browse the repository at this point in the history
…ndalone methods
  • Loading branch information
paweljakubas committed Apr 15, 2021
1 parent 58a6ace commit 53b8e2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 195 deletions.
54 changes: 0 additions & 54 deletions lib/core/src/Cardano/Wallet/DB.hs
Expand Up @@ -30,21 +30,14 @@ module Cardano.Wallet.DB
, ErrRemoveTx (..)
, ErrNoSuchWallet(..)
, ErrWalletAlreadyExists(..)
, ErrAddCosignerKey (..)
) where

import Prelude

import Cardano.Address.Derivation
( XPrv )
import Cardano.Address.Script
( Cosigner )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CosignerInfo, CredentialType )
import Cardano.Wallet.Primitive.AddressDiscovery.SharedState
( SharedWalletInfo )
import Cardano.Wallet.Primitive.Model
( Wallet )
import Cardano.Wallet.Primitive.Types
Expand All @@ -69,8 +62,6 @@ import Control.Monad.Trans.Except
( ExceptT, runExceptT )
import Data.Quantity
( Quantity (..) )
import Data.Time.Clock
( UTCTime )
import Data.Word
( Word32, Word8 )

Expand Down Expand Up @@ -304,45 +295,6 @@ data DBLayer m s k = forall stm. (MonadIO stm, MonadFail stm) => DBLayer
-- The second argument represents the stability window, or said
-- length of the deepest rollback.

, initializeSharedState
:: PrimaryKey WalletId
-> SharedWalletInfo k
-> WalletMetadata
-> GenesisParameters
-> ExceptT ErrWalletAlreadyExists stm ()
-- ^ Create the shared wallet.

, removeSharedWallet
:: PrimaryKey WalletId
-> ExceptT ErrNoSuchWallet stm ()
-- ^ Remove a given share wallet and all its associated data.

, readSharedWalletState
:: PrimaryKey WalletId
-> stm (Maybe (SharedWalletInfo k))
-- ^ Fetch the most recent state of a given shared wallet.
--
-- Return 'Nothing' if there's no such wallet.

, readSharedWalletMetadata
:: PrimaryKey WalletId
-> stm (Maybe WalletMetadata)
-- ^ Fetch the most recent metadata of a given shared wallet.
--
-- Return 'Nothing' if there's no such wallet.

, addCosignerKey
:: PrimaryKey WalletId
-> UTCTime
-> CosignerInfo k
-> ExceptT ErrAddCosignerKey stm ()
-- ^ Adding cosigner key to the shared wallet.

, listCosignerKeys
:: PrimaryKey WalletId
-> stm [(UTCTime, CosignerInfo k)]
-- ^ Get the list of keys all known cosigners in the DB for a given shared wallet, possibly empty.

, atomically
:: forall a. stm a -> m a
-- ^ Execute operations of the database in isolation and atomically.
Expand Down Expand Up @@ -370,12 +322,6 @@ newtype ErrWalletAlreadyExists
= ErrWalletAlreadyExists WalletId -- Wallet already exists in db
deriving (Eq, Show)

-- | Forbidden operation was executed when adding the account key of cosigner
data ErrAddCosignerKey
= ErrAddCosignerKeyNoWallet ErrNoSuchWallet
| ErrAddCosignerKeyAlreadyExists WalletId Cosigner CredentialType
deriving (Eq, Show)

-- | A primary key which can take many forms depending on the value. This may
-- become a type family as we move forward, but for now, it illustrate that
-- some queries are ran against some sort of store;
Expand Down
83 changes: 16 additions & 67 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -81,7 +81,6 @@ import Cardano.DB.Sqlite.Delete
import Cardano.Wallet.DB
( DBFactory (..)
, DBLayer (..)
, ErrAddCosignerKey (..)
, ErrNoSuchWallet (..)
, ErrRemoveTx (..)
, ErrWalletAlreadyExists (..)
Expand All @@ -91,7 +90,6 @@ import Cardano.Wallet.DB
)
import Cardano.Wallet.DB.Sqlite.TH
( Checkpoint (..)
, CosignerKey (..)
, DelegationCertificate (..)
, DelegationReward (..)
, EntityField (..)
Expand All @@ -103,7 +101,6 @@ import Cardano.Wallet.DB.Sqlite.TH
, SeqState (..)
, SeqStateAddress (..)
, SeqStatePendingIx (..)
, SharedWallet (..)
, StakeKeyCertificate (..)
, TxIn (..)
, TxMeta (..)
Expand Down Expand Up @@ -136,8 +133,6 @@ import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CosignerInfo (..), CredentialType )
import Cardano.Wallet.Primitive.AddressDiscovery.SharedState
( SharedWalletInfo (..) )
import Cardano.Wallet.Primitive.Slotting
( TimeInterpreter
, epochOf
Expand Down Expand Up @@ -189,8 +184,6 @@ import Data.Text
( Text )
import Data.Text.Class
( ToText (..), fromText )
import Data.Time.Clock
( UTCTime )
import Data.Typeable
( Typeable )
import Data.Word
Expand Down Expand Up @@ -243,6 +236,7 @@ import UnliftIO.MVar
import qualified Cardano.Wallet.Primitive.AddressDerivation as W
import qualified Cardano.Wallet.Primitive.AddressDiscovery.Random as Rnd
import qualified Cardano.Wallet.Primitive.AddressDiscovery.Sequential as Seq
import qualified Cardano.Wallet.Primitive.AddressDiscovery.SharedState as Shared
import qualified Cardano.Wallet.Primitive.Model as W
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.Address as W
Expand Down Expand Up @@ -1163,7 +1157,6 @@ withDBLayer tr defaultFieldValues dbFile ti action = do
data WalletDBLog
= MsgDB DBLog
| MsgCheckpointCache W.WalletId CheckpointCacheLog
| MsgSharedWallet W.WalletId SharedWalletLog
deriving (Generic, Show, Eq)

data CheckpointCacheLog
Expand All @@ -1173,24 +1166,16 @@ data CheckpointCacheLog
| MsgDrop
deriving (Generic, Show, Eq)

data SharedWalletLog
= MsgCreatingSharedWallet
| MsgAddingCosigner Cosigner CredentialType
| MsgRemovingSharedWallet
deriving (Generic, Show, Eq)

instance HasPrivacyAnnotation WalletDBLog
instance HasSeverityAnnotation WalletDBLog where
getSeverityAnnotation = \case
MsgDB msg -> getSeverityAnnotation msg
MsgCheckpointCache _ _ -> Debug
MsgSharedWallet _ _ -> Notice

instance ToText WalletDBLog where
toText = \case
MsgDB msg -> toText msg
MsgCheckpointCache wid msg -> "Checkpoint cache " <> toText wid <> ": " <> toText msg
MsgSharedWallet wid msg -> "Shared wallet "<> toText wid <> ": "<> toText msg

instance ToText CheckpointCacheLog where
toText = \case
Expand All @@ -1199,18 +1184,6 @@ instance ToText CheckpointCacheLog where
MsgRefresh -> "Refresh"
MsgDrop -> "Drop"

instance ToText SharedWalletLog where
toText = \case
MsgRemovingSharedWallet -> "Remove"
MsgCreatingSharedWallet -> "Create"
MsgAddingCosigner (Cosigner c) cred -> mconcat
[ "Adding the account public key for the cosigner#"
, T.pack (show c)
, " for "
, toText cred
, " credential."
]

-- | Runs an IO action with a new 'DBLayer' backed by a sqlite in-memory
-- database.
withDBLayerInMemory
Expand Down Expand Up @@ -1607,45 +1580,6 @@ newDBLayerWith cacheBehavior tr ti SqliteContext{runQuery} = do
W.Coin . maybe 0 (rewardAccountBalance . entityVal) <$>
selectFirst [RewardWalletId ==. wid] []

{-----------------------------------------------------------------------
Shared Wallet
-----------------------------------------------------------------------}

, initializeSharedState = \(PrimaryKey wid) state meta gp -> ExceptT $ do
res <- handleConstraint (ErrWalletAlreadyExists wid) $
insert_ (mkSharedWalletEntity wid state meta gp)
liftIO $ traceWith tr $ MsgSharedWallet wid MsgCreatingSharedWallet
pure res

, removeSharedWallet = \(PrimaryKey wid) -> ExceptT $ do
selectSharedWallet wid >>= \case
Nothing -> pure $ Left $ ErrNoSuchWallet wid
Just _ -> Right <$> do
liftIO $ traceWith tr $ MsgSharedWallet wid MsgRemovingSharedWallet
deleteWhere [SharedWalletWalletId ==. wid]
deleteWhere [CosignerKeyWalletId ==. wid]

, readSharedWalletState = \(PrimaryKey wid) ->
selectSharedWallet wid >>= \case
Nothing -> pure Nothing
Just _ -> selectSharedWalletState wid

, readSharedWalletMetadata = \(PrimaryKey wid) ->
selectSharedWallet wid >>= \case
Nothing -> pure Nothing
Just _ -> selectSharedWalletMetadata wid

, addCosignerKey = \(PrimaryKey wid) utctime info -> ExceptT $ do
selectSharedWallet wid >>= \case
Nothing -> pure $ Left $ ErrAddCosignerKeyNoWallet $ ErrNoSuchWallet wid
Just _ -> do
res <- handleConstraint (ErrAddCosignerKeyAlreadyExists wid (cosigner info) (credential info)) $
insert_ (mkCosignerKeyEntity wid utctime info)
liftIO $ traceWith tr $ MsgSharedWallet wid (MsgAddingCosigner (cosigner info) (credential info))
pure res

, listCosignerKeys = \(PrimaryKey _wid) -> undefined

{-----------------------------------------------------------------------
ACID Execution
-----------------------------------------------------------------------}
Expand Down Expand Up @@ -2494,6 +2428,19 @@ selectSeqStatePendingIxs wid =
where
fromRes = fmap (W.Index . seqStatePendingIxIndex . entityVal)

instance
( Eq (k 'AccountK XPub)
, PersistPublicKey (k 'AccountK)
, PersistPublicKey (k 'AddressK)
, MkKeyFingerprint k (Proxy n, k 'AddressK XPub)
, PaymentAddress n k
, SoftDerivation k
, Typeable n
) => PersistState (Shared.SharedState n k) where
insertState (_wid, _sl) _st = undefined

selectState (_wid, _sl) = undefined

{-------------------------------------------------------------------------------
HD Random address discovery
-------------------------------------------------------------------------------}
Expand Down Expand Up @@ -2578,6 +2525,7 @@ selectRndStatePending wid = do
assocFromEntity (RndStatePendingAddress _ accIx addrIx addr) =
((W.Index accIx, W.Index addrIx), addr)

{--
mkSharedWalletEntity
:: PersistPublicKey (k 'AccountK)
=> W.WalletId
Expand Down Expand Up @@ -2658,3 +2606,4 @@ mkCosignerKeyEntity wid utctime info = CosignerKey
}
where
(Cosigner c) = cosigner info
--}
35 changes: 14 additions & 21 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/TH.hs
Expand Up @@ -35,8 +35,6 @@ import Cardano.Wallet.DB.Sqlite.Types
( BlockId, HDPassphrase, TxId, sqlSettings' )
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CredentialType )
import Cardano.Wallet.Primitive.AddressDiscovery.SharedState
( SharedWalletState )
import Data.Quantity
( Percentage (..) )
import Data.Text
Expand Down Expand Up @@ -381,35 +379,30 @@ RndStatePendingAddress
deriving Show Generic

-- Shared Wallet
SharedWallet
sharedWalletWalletId W.WalletId sql=wallet_id
sharedWalletCreationTime UTCTime sql=creation_time
sharedWalletName Text sql=name
sharedWalletAccountXPub B8.ByteString sql=account_xpub
sharedWalletAccountIndex Word32 sql=account_ix
sharedWalletScriptGap W.AddressPoolGap sql=pool_gap
sharedWalletPaymentScript (Script Cosigner) sql=payment_script
sharedWalletDelegationScript (Script Cosigner) Maybe sql=delegation_script
sharedWalletState SharedWalletState sql=wallet_state
sharedWalletPassphraseLastUpdatedAt UTCTime Maybe sql=passphrase_last_updated_at
sharedWalletPassphraseScheme W.PassphraseScheme Maybe sql=passphrase_scheme
sharedWalletGenesisHash BlockId sql=genesis_hash
sharedWalletGenesisStart UTCTime sql=genesis_start

Primary sharedWalletWalletId
SharedState
sharedStateWalletId W.WalletId sql=wallet_id
sharedStateAccountXPub B8.ByteString sql=account_xpub
sharedStateAccountIndex Word32 sql=account_ix
sharedStateScriptGap W.AddressPoolGap sql=pool_gap
sharedStatePaymentScript (Script Cosigner) sql=payment_script
sharedStateDelegationScript (Script Cosigner) Maybe sql=delegation_script
sharedStateDerivationPrefix W.DerivationPrefix sql=derivation_prefix

Primary sharedStateWalletId
deriving Show Generic

CosignerKey
cosignerKeyWalletId W.WalletId sql=wallet_id
cosignerKeyCreationTime UTCTime sql=creation_time
cosignerKeySlot SlotNo sql=slot
cosignerKeyCredential CredentialType sql=credential
cosignerKeyAccountXPub B8.ByteString sql=account_xpub
cosignerKeyIndex Word8 sql=cosigner_ix
cosignerKeyIndex Word8 sql=cosigner_index

Primary
cosignerKeyWalletId
cosignerKeySlot
cosignerKeyCredential
cosignerKeyIndex
Foreign SharedWallet fk_shared_wallet_cosigner_key cosignerKeyWalletId ! ON DELETE CASCADE
Foreign Wallet fk_shared_wallet_cosigner_key cosignerKeyWalletId ! ON DELETE CASCADE
deriving Show Generic
|]
12 changes: 0 additions & 12 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Expand Up @@ -32,8 +32,6 @@ import Cardano.Api.Typed
)
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CredentialType )
import Cardano.Wallet.Primitive.AddressDiscovery.SharedState
( SharedWalletState )
import Cardano.Slotting.Slot
( SlotNo (..) )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -532,16 +530,6 @@ instance PersistField CredentialType where
instance PersistFieldSql CredentialType where
sqlType _ = sqlType (Proxy @Text)

----------------------------------------------------------------------------
-- SharedWalletState

instance PersistField SharedWalletState where
toPersistValue = toPersistValue . toText
fromPersistValue = fromPersistValueFromText

instance PersistFieldSql SharedWalletState where
sqlType _ = sqlType (Proxy @Text)

----------------------------------------------------------------------------

----------------------------------------------------------------------------
Expand Down

0 comments on commit 53b8e2f

Please sign in to comment.