Skip to content

Commit

Permalink
Add features modules to support open wallet state testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed May 26, 2023
1 parent 6832401 commit 8deaeee
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 55 deletions.
3 changes: 3 additions & 0 deletions lib/wallet/cardano-wallet.cabal
Expand Up @@ -248,6 +248,9 @@ library
Cardano.Wallet.Address.Keys.WalletKey
Cardano.Wallet.Address.Keys.WitnessCount
Cardano.Wallet.Address.Pool
Cardano.Wallet.Address.States.Families
Cardano.Wallet.Address.States.Features
Cardano.Wallet.Address.States.Test.State
Cardano.Wallet.Byron.Compatibility
Cardano.Wallet.Checkpoints
Cardano.Wallet.Checkpoints.Policy
Expand Down
46 changes: 46 additions & 0 deletions lib/wallet/src/Cardano/Wallet/Address/States/Families.hs
@@ -0,0 +1,46 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Wallet.Address.States.Families where

import Cardano.Wallet.Address.Derivation
( Depth (..) )
import Cardano.Wallet.Address.Derivation.Byron
( ByronKey )
import Cardano.Wallet.Address.Discovery.Random
( RndAnyState, RndState )
import Cardano.Wallet.Address.Discovery.Sequential
( SeqAnyState, SeqState )
import Cardano.Wallet.Address.Discovery.Shared
( SharedState )
import Cardano.Wallet.Address.States.Test.State
( TestState )
import Cardano.Wallet.Read.NetworkId
( NetworkDiscriminant )
import Data.Kind
( Type )

type family CredFromOf s where
CredFromOf (SharedState n key) = 'CredFromScriptK
CredFromOf (SeqState n key) = 'CredFromKeyK
CredFromOf (RndState n) = 'CredFromKeyK
CredFromOf (TestState s n key kt) = kt
CredFromOf (RndAnyState n p) = 'CredFromKeyK
CredFromOf (SeqAnyState n key p) = 'CredFromKeyK

-- | A type family to get the key type from a state.
type family KeyOf (s :: Type) :: (Depth -> Type -> Type) where
KeyOf (SeqState n k) = k
KeyOf (RndState n) = ByronKey
KeyOf (SharedState n k) = k
KeyOf (SeqAnyState n k p) = k
KeyOf (RndAnyState n p) = ByronKey
KeyOf (TestState s n k kt) = k

type family NetworkOf (s :: Type) :: NetworkDiscriminant where
NetworkOf (SeqState n k) = n
NetworkOf (RndState n) = n
NetworkOf (SharedState n k) = n
NetworkOf (SeqAnyState n k p) = n
NetworkOf (RndAnyState n p) = n
NetworkOf (TestState s n k kt) = n
38 changes: 38 additions & 0 deletions lib/wallet/src/Cardano/Wallet/Address/States/Features.hs
@@ -0,0 +1,38 @@
{-# LANGUAGE DataKinds #-}

module Cardano.Wallet.Address.States.Features
( IsOwned
, TestFeatures (..)
, defaultTestFeatures
)
where

import Prelude

import Cardano.Crypto.Wallet
( XPrv )
import Cardano.Wallet.Address.Derivation
( Depth (..) )
import Cardano.Wallet.Address.Derivation.Shared
()
import Cardano.Wallet.Address.States.Families
( CredFromOf, KeyOf )
import Cardano.Wallet.Primitive.Passphrase.Types
( Passphrase (..) )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )

type IsOwned s =
s
-> (KeyOf s 'RootK XPrv, Passphrase "encryption")
-> Address
-> Maybe (KeyOf s (CredFromOf s) XPrv, Passphrase "encryption")

newtype TestFeatures s = TestFeatures
{ isOwned :: IsOwned s
}

defaultTestFeatures :: TestFeatures s
defaultTestFeatures = TestFeatures
{ isOwned = error "isOwned: not implemented"
}
25 changes: 25 additions & 0 deletions lib/wallet/src/Cardano/Wallet/Address/States/Test/State.hs
@@ -0,0 +1,25 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE KindSignatures #-}

module Cardano.Wallet.Address.States.Test.State where

import Prelude

import Cardano.Wallet.Address.Derivation
( Depth )
import Cardano.Wallet.Read.NetworkId
( NetworkDiscriminant )
import Data.Kind
( Type )
import GHC.Generics
( Generic )

newtype
TestState
s
(n :: NetworkDiscriminant)
(k :: Depth -> Type -> Type)
(ktype :: Depth)
= TestState s
deriving (Generic, Show, Eq)
59 changes: 13 additions & 46 deletions lib/wallet/src/Cardano/Wallet/Flavor.hs
@@ -1,7 +1,6 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
Expand All @@ -18,7 +17,7 @@ module Cardano.Wallet.Flavor
( WalletFlavorS (..)
, WalletFlavor (..)
, KeyOf
, TestState (..)
, TestState
, KeyFlavorS (..)
, keyFlavorFromState
, keyOfWallet
Expand All @@ -37,8 +36,6 @@ where

import Prelude

import Cardano.Wallet.Address.Derivation
( Depth (CredFromKeyK, CredFromScriptK) )
import Cardano.Wallet.Address.Derivation.Byron
( ByronKey )
import Cardano.Wallet.Address.Derivation.Icarus
Expand All @@ -53,14 +50,13 @@ import Cardano.Wallet.Address.Discovery.Sequential
( SeqAnyState, SeqState )
import Cardano.Wallet.Address.Discovery.Shared
( SharedState (..) )
import Cardano.Wallet.Read.NetworkId
( NetworkDiscriminant )
import Cardano.Wallet.Address.States.Families
import Cardano.Wallet.Address.States.Features
( TestFeatures, defaultTestFeatures )
import Cardano.Wallet.Address.States.Test.State
( TestState )
import Cardano.Wallet.TypeLevel
( Excluding, Including )
import Data.Kind
( Type )
import GHC.Generics
( Generic )

-- | A singleton type to capture the flavor of a state.
data WalletFlavorS s where
Expand All @@ -70,7 +66,8 @@ data WalletFlavorS s where
SharedWallet :: WalletFlavorS (SharedState n SharedKey)
BenchByronWallet :: WalletFlavorS (RndAnyState n p)
BenchShelleyWallet :: WalletFlavorS (SeqAnyState n ShelleyKey p)
TestStateS :: WalletFlavorS (TestState s ShelleyKey)
TestStateS :: KeyFlavorS k -> TestFeatures s
-> WalletFlavorS (TestState s n k kt)

data WalletFlavors
= ShelleyF
Expand All @@ -88,8 +85,7 @@ type family FlavorOf s where
FlavorOf (SharedState n SharedKey) = 'SharedF
FlavorOf (RndAnyState n p) = 'BenchByronF
FlavorOf (SeqAnyState n ShelleyKey p) = 'BenchShelleyF
FlavorOf (TestState s ShelleyKey) = 'TestStateF

FlavorOf (TestState s n k ktype) = 'TestStateF

type AllFlavors =
'[ 'ShelleyF
Expand Down Expand Up @@ -125,21 +121,9 @@ instance WalletFlavor (RndAnyState n p) where
instance WalletFlavor (SharedState n SharedKey) where
walletFlavor = SharedWallet

instance WalletFlavor (TestState n ShelleyKey) where
walletFlavor = TestStateS

-- | A type for states that will be used in tests.
newtype TestState s (k :: (Depth -> Type -> Type)) = TestState s
deriving (Generic, Show, Eq)

-- | A type family to get the key type from a state.
type family KeyOf (s :: Type) :: (Depth -> Type -> Type) where
KeyOf (SeqState n k) = k
KeyOf (RndState n) = ByronKey
KeyOf (SharedState n k) = k
KeyOf (SeqAnyState n k p) = k
KeyOf (RndAnyState n p) = ByronKey
KeyOf (TestState s k) = k
instance (KeyFlavor k)
=> WalletFlavor (TestState s n k kt ) where
walletFlavor = TestStateS keyFlavor defaultTestFeatures

-- | A singleton type to capture the flavor of a key.
data KeyFlavorS a where
Expand All @@ -148,7 +132,6 @@ data KeyFlavorS a where
ShelleyKeyS :: KeyFlavorS ShelleyKey
SharedKeyS :: KeyFlavorS SharedKey


class KeyFlavor a where
keyFlavor :: KeyFlavorS a

Expand All @@ -164,7 +147,6 @@ instance KeyFlavor ShelleyKey where
instance KeyFlavor SharedKey where
keyFlavor = SharedKeyS


-- | Map a wallet flavor to a key flavor.
keyOfWallet :: WalletFlavorS s -> KeyFlavorS (KeyOf s)
keyOfWallet ShelleyWallet = ShelleyKeyS
Expand All @@ -173,7 +155,7 @@ keyOfWallet ByronWallet = ByronKeyS
keyOfWallet SharedWallet = SharedKeyS
keyOfWallet BenchByronWallet = ByronKeyS
keyOfWallet BenchShelleyWallet = ShelleyKeyS
keyOfWallet TestStateS = ShelleyKeyS
keyOfWallet (TestStateS kf _) = kf

-- | A function to reify the flavor of a key from a state type.
--
Expand All @@ -182,13 +164,6 @@ keyOfWallet TestStateS = ShelleyKeyS
keyFlavorFromState :: forall s. WalletFlavor s => KeyFlavorS (KeyOf s)
keyFlavorFromState = keyOfWallet (walletFlavor @s)

type family NetworkOf (s :: Type) :: NetworkDiscriminant where
NetworkOf (SeqState n k) = n
NetworkOf (RndState n) = n
NetworkOf (SharedState n k) = n
NetworkOf (SeqAnyState n k p) = n
NetworkOf (RndAnyState n p) = n

-- | Constraints for a state with a specific key.
type StateWithKey s k = (WalletFlavor s, KeyOf s ~ k)

Expand All @@ -213,11 +188,3 @@ shelleyOrShared x r h = case x of
SharedWallet -> h SharedWallet
IcarusWallet -> h IcarusWallet
_ -> r

type family CredFromOf s where
CredFromOf (SharedState n key) = 'CredFromScriptK
CredFromOf (SeqState n key) = 'CredFromKeyK
CredFromOf (RndState n) = 'CredFromKeyK
CredFromOf (TestState n key) = 'CredFromKeyK
CredFromOf (RndAnyState n p) = 'CredFromKeyK
CredFromOf (SeqAnyState n key p) = 'CredFromKeyK
21 changes: 12 additions & 9 deletions lib/wallet/test/unit/Cardano/WalletSpec.hs
Expand Up @@ -62,6 +62,8 @@ import Cardano.Wallet.Address.Discovery
)
import Cardano.Wallet.Address.Keys.WalletKey
( publicKey )
import Cardano.Wallet.Address.States.Test.State
( TestState (..) )
import Cardano.Wallet.DB
( DBFresh, DBLayer (..), hoistDBFresh, hoistDBLayer, putTxHistory )
import Cardano.Wallet.DB.Fixtures
Expand All @@ -79,12 +81,7 @@ import Cardano.Wallet.DummyTarget.Primitive.Types
, mkTxId
)
import Cardano.Wallet.Flavor
( CredFromOf
, KeyFlavorS (ShelleyKeyS)
, KeyOf
, TestState (..)
, WalletFlavor (..)
)
( CredFromOf, KeyFlavorS (ShelleyKeyS), KeyOf, WalletFlavor (..) )
import Cardano.Wallet.Gen
( genMnemonic, genSlotNo )
import Cardano.Wallet.Network
Expand Down Expand Up @@ -286,6 +283,8 @@ import qualified Cardano.Wallet.Primitive.Migration as Migration
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Cardano.Wallet.Read as Read
import Cardano.Wallet.Read.NetworkId
( NetworkDiscriminant (Mainnet) )
import qualified Cardano.Wallet.Submissions.Submissions as Smbs
import qualified Cardano.Wallet.Submissions.TxStatus as Sbms
import qualified Data.ByteArray as BA
Expand Down Expand Up @@ -560,7 +559,7 @@ walletListTransactionsWithLimit wallet@(_, _, _) =
test (Just l) (Just r) Ascending Identity
$ \slot -> slot >= l && slot <= r

type DummyStateWithAddresses = TestState [Address] ShelleyKey
type DummyStateWithAddresses = TestState [Address] 'Mainnet ShelleyKey 'CredFromKeyK

instance IsOurs DummyStateWithAddresses Address where
isOurs a s@(TestState addr) =
Expand Down Expand Up @@ -1318,8 +1317,12 @@ mockNetworkLayer = dummyNetworkLayer
dummyTip = BlockHeader (SlotNo 0) (Quantity 0) dummyHash (Just dummyHash)
dummyHash = Hash "dummy hash"

type DummyState
= TestState (Map Address (Index 'Soft 'CredFromKeyK)) ShelleyKey
type DummyState =
TestState
(Map Address (Index 'Soft 'CredFromKeyK))
'Mainnet
ShelleyKey
'CredFromKeyK

instance Sqlite.AddressBookIso DummyState where
data Prologue DummyState = DummyPrologue
Expand Down

0 comments on commit 8deaeee

Please sign in to comment.