Skip to content

Commit

Permalink
Merge #2661 #2664
Browse files Browse the repository at this point in the history
2661: CLI: Force command-line arguments to be UTF-8 decoded r=rvl a=rvl

### Issue Number

Bug found while doing ADP-915.

### Overview

Applies the same fix as in input-output-hk/offchain-metadata-tools#27 so that command-line arguments are also decoded as UTF-8 text, regardless of the user's locale setting.



2664: Fix slow ApiSharedWallet unit tests r=rvl a=rvl

### Issue Number

Found during ADP-902

### Overview

The `Cardano.Wallet.Api.Types` tests are running very slowly. Also the unit tests were sometimes timing out in CI.

Profiling showed that the `Arbitrary` instance of `ApiSharedWallet` was the slowest part.
This change improves the speed of this generator.
Now it is the OpenAPI validation which is the slowest part.

### Comments

We may also be able to reduce the size of arbitrary `ApiSharedWallet` values, so that validation gets faster. Because it's still not especially quick. Something like the `instance Arbitrary WalletName` change in this PR could probably be applied.


Co-authored-by: Rodney Lorrimar <rodney.lorrimar@iohk.io>
  • Loading branch information
iohk-bors[bot] and rvl committed May 25, 2021
3 parents 1bf7164 + 140e850 + 7f9b920 commit 667037b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
7 changes: 5 additions & 2 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,15 @@ import Test.QuickCheck
, arbitraryPrintableChar
, arbitrarySizedBoundedIntegral
, choose
, chooseInt
, counterexample
, elements
, frequency
, oneof
, property
, scale
, shrinkIntegral
, sized
, vector
, vectorOf
, (.&&.)
Expand Down Expand Up @@ -1568,8 +1570,9 @@ instance Arbitrary WalletId where

instance Arbitrary WalletName where
arbitrary = do
nameLength <- choose (walletNameMinLength, walletNameMaxLength)
WalletName . T.pack <$> replicateM nameLength arbitraryPrintableChar
len <- Test.QuickCheck.scale (min walletNameMaxLength) $ sized $ \n ->
chooseInt (walletNameMinLength, walletNameMinLength `max` n)
WalletName . T.pack <$> vectorOf len arbitraryPrintableChar
shrink (WalletName t)
| T.length t <= walletNameMinLength = []
| otherwise = [WalletName $ T.take walletNameMinLength t]
Expand Down
48 changes: 13 additions & 35 deletions lib/core/test/unit/Cardano/Wallet/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ module Cardano.Wallet.Gen
, genScriptTemplate
, genScriptTemplateComplete
, genScriptTemplateEntry
, genXPub
, genMockXPub
, genNatural
) where

import Prelude

import Cardano.Address.Derivation
( XPrv, XPub, xpubFromBytes )
( XPub, xpubFromBytes )
import Cardano.Address.Script
( Cosigner (..), Script (..), ScriptTemplate (..) )
import Cardano.Api
Expand All @@ -43,18 +43,9 @@ import Cardano.Api
, metadataFromJson
)
import Cardano.Mnemonic
( ConsistentEntropy
, EntropySize
, Mnemonic
, SomeMnemonic (..)
, entropyToMnemonic
)
( ConsistentEntropy, EntropySize, Mnemonic, entropyToMnemonic )
import Cardano.Wallet.Api.Types
( ApiScriptTemplateEntry (..), XPubOrSelf (..) )
import Cardano.Wallet.Primitive.AddressDerivation
( Passphrase (..), WalletKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Shared
( retrieveAllCosigners )
import Cardano.Wallet.Primitive.Types
Expand All @@ -77,6 +68,8 @@ import Data.List
( sortOn )
import Data.List.Extra
( nubOrdOn )
import Data.Maybe
( fromMaybe )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
Expand All @@ -94,7 +87,6 @@ import Numeric.Natural
import Test.QuickCheck
( Arbitrary (..)
, Gen
, InfiniteList (..)
, Positive (..)
, PrintableString (..)
, arbitrarySizedNatural
Expand All @@ -114,10 +106,8 @@ import Test.QuickCheck
)

import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Codec.CBOR.Write as CBOR
import qualified Data.Aeson as Aeson
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.HashMap.Strict as HM
Expand Down Expand Up @@ -323,7 +313,7 @@ genScriptTemplate = do
script <- genScriptCosigners `suchThat` (not . null . retrieveAllCosigners)
let scriptCosigners = retrieveAllCosigners script
cosignersSubset <- sublistOf scriptCosigners `suchThat` (not . null)
xpubs <- vectorOf (length cosignersSubset) genXPub
xpubs <- vectorOf (length cosignersSubset) genMockXPub
pure $ ScriptTemplate (Map.fromList $ zip cosignersSubset xpubs) script

genScriptTemplateEntry :: Gen ApiScriptTemplateEntry
Expand All @@ -338,27 +328,15 @@ genScriptTemplateComplete :: Gen ScriptTemplate
genScriptTemplateComplete = do
script <- genScriptCosigners `suchThat` (not . null . retrieveAllCosigners)
let scriptCosigners = retrieveAllCosigners script
xpubs <- vectorOf (length scriptCosigners) genXPub
xpubs <- vectorOf (length scriptCosigners) genMockXPub
pure $ ScriptTemplate (Map.fromList $ zip scriptCosigners xpubs) script

genRootKeySeqWithPass
:: Passphrase "encryption"
-> Gen (ShelleyKey depth XPrv)
genRootKeySeqWithPass encryptionPass = do
s <- SomeMnemonic <$> genMnemonic @15
g <- Just . SomeMnemonic <$> genMnemonic @12
return $ Shelley.unsafeGenerateKeyFromSeed (s, g) encryptionPass

genXPub :: Gen XPub
genXPub = do
getRawKey . publicKey <$> (genRootKeySeqWithPass =<< genPassphrase (0, 16))
genMockXPub :: Gen XPub
genMockXPub = fromMaybe impossible . xpubFromBytes . BS.pack <$> genBytes
where
genBytes = vectorOf 64 arbitrary
impossible = error "incorrect length in genMockXPub"

genXPubOrSelf :: Gen XPubOrSelf
genXPubOrSelf =
oneof [SomeAccountKey <$> genXPub, pure Self]

genPassphrase :: (Int, Int) -> Gen (Passphrase purpose)
genPassphrase range = do
n <- choose range
InfiniteList bytes _ <- arbitrary
return $ Passphrase $ BA.convert $ BS.pack $ take n bytes
oneof [SomeAccountKey <$> genMockXPub, pure Self]
3 changes: 3 additions & 0 deletions lib/launcher/src/Cardano/Startup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import Data.Either.Extra
( eitherToMaybe )
import Data.Text.Class
( ToText (..) )
import GHC.IO.Encoding
( setFileSystemEncoding )
import System.IO
( Handle, hIsOpen, hSetEncoding, mkTextEncoding, stderr, stdin, stdout )
import System.IO.CodePage
Expand Down Expand Up @@ -79,6 +81,7 @@ setUtf8EncodingHandles :: IO ()
setUtf8EncodingHandles = do
utf8' <- mkTextEncoding "UTF-8//TRANSLIT"
mapM_ (`hSetEncoding` utf8') [stdin, stdout, stderr]
setFileSystemEncoding utf8'

{-------------------------------------------------------------------------------
Shutdown handlers
Expand Down

0 comments on commit 667037b

Please sign in to comment.