Skip to content

Commit

Permalink
extend SeqState and add knownScripts
Browse files Browse the repository at this point in the history
fix tests and add automatic migration part

fix migrate tests - try 1

fix adding column - should be the last one in table

add knownScripts to SeqState
  • Loading branch information
paweljakubas committed Nov 19, 2020
1 parent 5d2fc1b commit a556c93
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -646,6 +646,7 @@ createIcarusWallet ctx wid wname credentials = db & \DBLayer{..} -> do
(Seq.pendingChangeIxs s)
(Seq.rewardAccountKey s)
(Seq.derivationPrefix s)
Map.empty
now <- lift getCurrentTime
let meta = WalletMetadata
{ name = wname
Expand Down
6 changes: 4 additions & 2 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -684,6 +684,7 @@ data DefaultFieldValues = DefaultFieldValues
, defaultDesiredNumberOfPool :: Word16
, defaultMinimumUTxOValue :: W.Coin
, defaultHardforkEpoch :: Maybe W.EpochNo
, defaultMultisigPoolGap :: Maybe Seq.AddressPoolGap
}

-- | Sets up a connection to the SQLite database.
Expand Down Expand Up @@ -1702,7 +1703,8 @@ instance
, SoftDerivation k
) => PersistState (Seq.SeqState n k) where
insertState (wid, sl) st = do
let (intPool, extPool) = (Seq.internalPool st, Seq.externalPool st)
let (intPool, extPool) =
(Seq.internalPool st, Seq.externalPool st)
let (accountXPub, _) = W.invariant
"Internal & External pool use different account public keys!"
(Seq.accountPubKey intPool, Seq.accountPubKey extPool)
Expand Down Expand Up @@ -1732,7 +1734,7 @@ instance
intPool <- lift $ selectAddressPool @n wid sl iGap accountXPub
extPool <- lift $ selectAddressPool @n wid sl eGap accountXPub
pendingChangeIxs <- lift $ selectSeqStatePendingIxs wid
pure $ Seq.SeqState intPool extPool pendingChangeIxs rewardXPub prefix
pure $ Seq.SeqState intPool extPool pendingChangeIxs rewardXPub prefix Map.empty

insertAddressPool
:: forall n k c. (PaymentAddress n k, Typeable c)
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Primitive/AddressDerivation.hs
Expand Up @@ -163,7 +163,7 @@ import qualified Data.Text.Encoding as T
--
-- @m | purpose' | cointype' | account' | role | address@
data Depth
= RootK | PurposeK | CoinTypeK | AccountK | RoleK | AddressK
= RootK | PurposeK | CoinTypeK | AccountK | RoleK | AddressK | ScriptK

-- | Marker for addresses type engaged. We want to handle four cases here.
-- The first two are pertinent to UTxO accounting,
Expand Down
Expand Up @@ -69,6 +69,8 @@ module Cardano.Wallet.Primitive.AddressDiscovery.Sequential

import Prelude

import Cardano.Address.Script
( ScriptHash )
import Cardano.Crypto.Wallet
( XPrv, XPub )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -274,6 +276,8 @@ role =
UtxoInternal
| t == typeRep (Proxy :: Proxy 'UtxoExternal) ->
UtxoExternal
| t == typeRep (Proxy :: Proxy 'MultisigScript) ->
MultisigScript
_ ->
MutableAccount

Expand Down Expand Up @@ -566,24 +570,28 @@ data SeqState (n :: NetworkDiscriminant) k = SeqState
-- ^ Reward account public key associated with this wallet
, derivationPrefix :: DerivationPrefix
-- ^ Derivation path prefix from a root key up to the internal account
, knownScripts :: !(Map ScriptHash [k 'ScriptK XPub])
-- ^ Known script hashes that contain our verification key hashes
}
deriving stock (Generic)

deriving instance
( Show (k 'AccountK XPub)
, Show (k 'AddressK XPub)
, Show (k 'ScriptK XPub)
, Show (KeyFingerprint "payment" k)
) => Show (SeqState n k)

instance
( NFData (k 'AccountK XPub)
, NFData (k 'AddressK XPub)
, NFData (k 'ScriptK XPub)
, NFData (KeyFingerprint "payment" k)
)
=> NFData (SeqState n k)

instance PersistPublicKey (k 'AccountK) => Buildable (SeqState n k) where
build (SeqState intP extP chgs _ path) = "SeqState:\n"
build (SeqState intP extP chgs _ path _) = "SeqState:\n"
<> indentF 4 ("Derivation prefix: " <> build (toText path))
<> indentF 4 (build intP)
<> indentF 4 (build extP)
Expand Down Expand Up @@ -651,10 +659,11 @@ mkSeqStateFromRootXPrv (rootXPrv, pwd) purpose g =
mkAddressPool @n (publicKey accXPrv) g []
intPool =
mkAddressPool @n (publicKey accXPrv) g []
scripts = Map.empty
prefix =
DerivationPrefix ( purpose, coinTypeAda, minBound )
in
SeqState intPool extPool emptyPendingIxs rewardXPub prefix
SeqState intPool extPool emptyPendingIxs rewardXPub prefix scripts

-- | Construct a Sequential state for a wallet from public account key.
mkSeqStateFromAccountXPub
Expand All @@ -677,10 +686,11 @@ mkSeqStateFromAccountXPub accXPub purpose g =
mkAddressPool @n accXPub g []
intPool =
mkAddressPool @n accXPub g []
scripts = Map.empty
prefix =
DerivationPrefix ( purpose, coinTypeAda, minBound )
in
SeqState intPool extPool emptyPendingIxs rewardXPub prefix
SeqState intPool extPool emptyPendingIxs rewardXPub prefix scripts

-- NOTE
-- We have to scan both the internal and external chain. Note that, the
Expand All @@ -693,7 +703,7 @@ instance
, MkKeyFingerprint k Address
) => IsOurs (SeqState n k) Address
where
isOurs addr (SeqState !s1 !s2 !ixs !rpk !prefix) =
isOurs addr (SeqState !s1 !s2 !ixs !rpk !prefix !scripts) =
let
DerivationPrefix (purpose, coinType, accountIx) = prefix
(internal, !s1') = lookupAddress @n (const Used) addr s1
Expand Down Expand Up @@ -722,7 +732,7 @@ instance

_ -> Nothing
in
(ixs' `deepseq` ours `deepseq` ours, SeqState s1' s2' ixs' rpk prefix)
(ixs' `deepseq` ours `deepseq` ours, SeqState s1' s2' ixs' rpk prefix scripts)

instance
( SoftDerivation k
Expand All @@ -736,14 +746,14 @@ instance
type ArgGenChange (SeqState n k) =
(k 'AddressK XPub -> k 'AddressK XPub -> Address)

genChange mkAddress (SeqState intPool extPool pending rpk path) =
genChange mkAddress (SeqState intPool extPool pending rpk path scripts) =
let
(ix, pending') = nextChangeIndex intPool pending
accountXPub = accountPubKey intPool
addressXPub = deriveAddressPublicKey accountXPub UtxoInternal ix
addr = mkAddress addressXPub rpk
in
(addr, SeqState intPool extPool pending' rpk path)
(addr, SeqState intPool extPool pending' rpk path scripts)

instance
( IsOurs (SeqState n k) Address
Expand All @@ -753,7 +763,7 @@ instance
, AddressIndexDerivationType k ~ 'Soft
)
=> IsOwned (SeqState n k) k where
isOwned (SeqState !s1 !s2 _ _ _) (rootPrv, pwd) addr =
isOwned (SeqState !s1 !s2 _ _ _ _) (rootPrv, pwd) addr =
let
xPrv1 = lookupAndDeriveXPrv s1
xPrv2 = lookupAndDeriveXPrv s2
Expand All @@ -779,7 +789,7 @@ instance
, MkKeyFingerprint k Address
, SoftDerivation k
) => CompareDiscovery (SeqState n k) where
compareDiscovery (SeqState !s1 !s2 _ _ _) a1 a2 =
compareDiscovery (SeqState !s1 !s2 _ _ _ _) a1 a2 =
case (ix a1 s1 <|> ix a1 s2, ix a2 s1 <|> ix a2 s2) of
(Nothing, Nothing) -> EQ
(Nothing, Just _) -> GT
Expand Down Expand Up @@ -829,12 +839,14 @@ newtype SeqAnyState (network :: NetworkDiscriminant) key (p :: Nat) = SeqAnyStat
deriving instance
( Show (k 'AccountK XPub)
, Show (k 'AddressK XPub)
, Show (k 'ScriptK XPub)
, Show (KeyFingerprint "payment" k)
) => Show (SeqAnyState n k p)

instance
( NFData (k 'AccountK XPub)
, NFData (k 'AddressK XPub)
, NFData (k 'ScriptK XPub)
, NFData (KeyFingerprint "payment" k)
)
=> NFData (SeqAnyState n k p)
Expand Down
5 changes: 3 additions & 2 deletions lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs
Expand Up @@ -477,14 +477,15 @@ instance Arbitrary (Index 'WholeDomain depth) where
-------------------------------------------------------------------------------}

instance Arbitrary (SeqState 'Mainnet JormungandrKey) where
shrink (SeqState intPool extPool ixs rwd prefix) =
(\(i, e, x) -> SeqState i e x rwd prefix) <$> shrink (intPool, extPool, ixs)
shrink (SeqState intPool extPool ixs rwd prefix scripts) =
(\(i, e, x) -> SeqState i e x rwd prefix scripts) <$> shrink (intPool, extPool, ixs)
arbitrary = SeqState
<$> arbitrary
<*> arbitrary
<*> arbitrary
<*> pure arbitraryRewardAccount
<*> pure defaultSeqStatePrefix
<*> pure Map.empty

defaultSeqStatePrefix :: DerivationPrefix
defaultSeqStatePrefix = DerivationPrefix
Expand Down
1 change: 1 addition & 0 deletions lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
Expand Up @@ -833,6 +833,7 @@ defaultFieldValues = DefaultFieldValues
, defaultDesiredNumberOfPool = 0
, defaultMinimumUTxOValue = Coin 0
, defaultHardforkEpoch = Nothing
, defaultMultisigPoolGap = Nothing
}

newDBLayer'
Expand Down
Expand Up @@ -132,6 +132,7 @@ import Test.Text.Roundtrip

import qualified Cardano.Wallet.Primitive.AddressDerivation.Icarus as Icarus
import qualified Cardano.Wallet.Primitive.AddressDerivation.Jormungandr as Jormungandr
import qualified Data.Map.Strict as Map

spec :: Spec
spec = do
Expand Down Expand Up @@ -478,7 +479,7 @@ prop_changeIsOnlyKnownAfterGeneration
prop_changeIsOnlyKnownAfterGeneration (intPool, extPool) =
let
s0 :: SeqState 'Mainnet JormungandrKey
s0 = SeqState intPool extPool emptyPendingIxs rewardAccount defaultPrefix
s0 = SeqState intPool extPool emptyPendingIxs rewardAccount defaultPrefix Map.empty
addrs0 = fst <$> knownAddresses s0
(change, s1) = genChange (\k _ -> paymentAddress @'Mainnet k) s0
addrs1 = fst <$> knownAddresses s1
Expand Down Expand Up @@ -702,12 +703,12 @@ instance
return $ mkAddressPool @'Mainnet ourAccount g (zip addrs statuses)

instance Arbitrary (SeqState 'Mainnet JormungandrKey) where
shrink (SeqState intPool extPool ixs rwd prefix) =
(\(i, e) -> SeqState i e ixs rwd prefix) <$> shrink (intPool, extPool)
shrink (SeqState intPool extPool ixs rwd prefix scripts) =
(\(i, e) -> SeqState i e ixs rwd prefix scripts) <$> shrink (intPool, extPool)
arbitrary = do
intPool <- arbitrary
extPool <- arbitrary
return $ SeqState intPool extPool emptyPendingIxs rewardAccount defaultPrefix
return $ SeqState intPool extPool emptyPendingIxs rewardAccount defaultPrefix Map.empty

-- | Wrapper to encapsulate accounting style proxies that are so-to-speak,
-- different types in order to easily map over them and avoid duplicating
Expand Down
1 change: 1 addition & 0 deletions lib/shelley/src/Cardano/Wallet/Shelley.hs
Expand Up @@ -371,6 +371,7 @@ serveWallet
minimumUTxOvalue pp
, defaultHardforkEpoch =
hardforkEpochNo pp
, defaultMultisigPoolGap = Nothing
}
)
(timeInterpreter nl)
Expand Down

0 comments on commit a556c93

Please sign in to comment.