Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
use defaultAddressGap to make sure core unit tests pass

hlint
  • Loading branch information
paweljakubas committed Dec 2, 2020
1 parent d8e5c32 commit a099993
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
17 changes: 9 additions & 8 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -1819,11 +1819,11 @@ insertScriptPool wid sl pool = do
<- Map.toList (Seq.verPoolIndexedKeys pool)
]
void $ dbChunked insertMany_ $
concat $ map toDB $ Map.toList (Seq.verPoolKnownScripts pool)
concatMap toDB $ Map.toList (Seq.verPoolKnownScripts pool)
where
toAddress = W.Address . xpubToBytes . getRawKey
toDB (scriptHash, verKeys) =
zipWith (\v -> SeqStateScriptHash wid sl scriptHash (toAddress v)) verKeys [0..]
zipWith (SeqStateScriptHash wid sl scriptHash . toAddress) verKeys [0..]

instance Ord ScriptHash where
compare (ScriptHash sh1) (ScriptHash sh2) = compare sh1 sh2
Expand Down Expand Up @@ -1861,12 +1861,13 @@ selectScriptPool wid sl gap xpub = do
Nothing -> id
knownScripts =
Map.map (map snd . sortOn fst) .
foldr (\(sh, vK, i) -> insertIfNotNothing sh vK i) Map.empty .
map (\x -> ( seqStateScriptHashScriptHash x
, xpubFromBytes $ W.unAddress $
seqStateScriptHashVerificationKey x
, seqStateScriptHashIndex x
))
foldr
((\ (sh, vK, i) -> insertIfNotNothing sh vK i)
. (\ x -> ( seqStateScriptHashScriptHash x
, xpubFromBytes $ W.unAddress $
seqStateScriptHashVerificationKey x
, seqStateScriptHashIndex x)))
Map.empty
scriptPoolFromEntities
:: [SeqStateAddress]
-> [SeqStateScriptHash]
Expand Down
42 changes: 40 additions & 2 deletions lib/core/test/unit/Cardano/Wallet/DB/Arbitrary.hs
Expand Up @@ -31,6 +31,8 @@ import Prelude

import Cardano.Address.Derivation
( XPrv, XPub )
import Cardano.Address.Script
( ScriptHash (..) )
import Cardano.Crypto.Wallet
( unXPrv )
import Cardano.Mnemonic
Expand All @@ -49,6 +51,8 @@ import Cardano.Wallet.Primitive.AddressDerivation
, Index (..)
, NetworkDiscriminant (..)
, Passphrase (..)
, Role (..)
, SoftDerivation (..)
, WalletKey (..)
, publicKey
)
Expand All @@ -64,12 +68,14 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPool
, DerivationPrefix (..)
, SeqState (..)
, VerificationKeyPool
, accountPubKey
, coinTypeAda
, gap
, defaultAddressPoolGap
, mkAddressPool
, mkVerificationKeyPool
, purposeCIP1852
, toVerKeyHash
)
import Cardano.Wallet.Primitive.Model
( Wallet
Expand All @@ -79,6 +85,10 @@ import Cardano.Wallet.Primitive.Model
, unsafeInitWallet
, utxo
)
import Cardano.Wallet.Primitive.Scripts
()
import Cardano.Wallet.Primitive.Slotting
( unsafeEpochNo )
import Cardano.Wallet.Primitive.Types
( Block (..)
, BlockHeader (..)
Expand Down Expand Up @@ -468,7 +478,7 @@ instance Arbitrary (SeqState 'Mainnet ShelleyKey) where
<*> arbitrary
<*> pure arbitraryRewardAccount
<*> pure defaultSeqStatePrefix
<*> pure (mkVerificationKeyPool (accountPubKey extPool) (gap extPool) Map.empty Map.empty)
<*> genVerificationKeyPool (accountPubKey extPool)

defaultSeqStatePrefix :: DerivationPrefix
defaultSeqStatePrefix = DerivationPrefix
Expand All @@ -477,6 +487,34 @@ defaultSeqStatePrefix = DerivationPrefix
, minBound
)

instance Arbitrary ScriptHash where
arbitrary =
pure $ ScriptHash (BS.replicate 28 0)

genVerificationKeyPool
:: ShelleyKey 'AccountK XPub
-> Gen (VerificationKeyPool ShelleyKey)
genVerificationKeyPool accXPub = do
nVerKeys <- choose (5,10)
let minIndex = getIndex @'Soft minBound
let toVerKey ix =
deriveAddressPublicKey accXPub MultisigScript
(toEnum (fromInteger $ toInteger $ minIndex + ix))
verKeysIxs <- L.nub <$> vectorOf nVerKeys (choose (0, 15))
let nVerKeys' = L.length verKeysIxs
let setUsed ix =
if ix `elem` verKeysIxs then
Used
else
Unused
let indexedKeysMap = map (\ix -> (toVerKeyHash $ toVerKey ix, (Index ix, setUsed ix)))
[0 .. maximum verKeysIxs]
knownScripts <- vectorOf nVerKeys' arbitrary
let knownScriptsMap =
zipWith (\s k -> (s,[k])) knownScripts (coerce . toVerKey <$> verKeysIxs)
pure $ mkVerificationKeyPool accXPub defaultAddressPoolGap
(Map.fromList indexedKeysMap) (Map.fromList knownScriptsMap)

instance Arbitrary (ShelleyKey 'RootK XPrv) where
shrink _ = []
arbitrary = elements rootKeysSeq
Expand Down
13 changes: 13 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Primitive/ScriptsSpec.hs
Expand Up @@ -118,6 +118,8 @@ spec = do
(property prop_scriptsOutsideNotDiscovered)
it "before and after discovery pool number of last consequitive Unused keys stays the same"
(property prop_unusedVerKeysConstant)
it "discovered verification keys in scripts are consistent between knownScripts and indexedKeys"
(property prop_verKeysConsistent)

prop_scriptFromOurVerKeys
:: AccountXPubWithScripts
Expand Down Expand Up @@ -222,6 +224,17 @@ prop_unusedVerKeysConstant (AccountXPubWithScripts accXPub' scripts') = do
getVerKeyMap
L.length (lastUnusedKeys seqState) === L.length (lastUnusedKeys seqState')

prop_verKeysConsistent
:: AccountXPubWithScripts
-> Property
prop_verKeysConsistent (AccountXPubWithScripts accXPub' scripts') = do
let seqState0 = initializeState accXPub'
let seqState = foldr (\script s -> snd $ isShared script s) seqState0 scripts'
let verKeys = L.nub $ concat $ Map.elems $ getKnownScripts seqState
let verKeyHashes = Map.keys $ Map.filter (\(_, isUsed) -> isUsed == Used) $
getVerKeyMap seqState
Set.fromList verKeyHashes === Set.fromList (map toVerKeyHash verKeys)

data AccountXPubWithScripts = AccountXPubWithScripts
{ accXPub :: ShelleyKey 'AccountK XPub
, scripts :: [Script]
Expand Down

0 comments on commit a099993

Please sign in to comment.