Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cardano.Wallet.Shelley.Launch.BlockfrostSpec #3176

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/shelley/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ test-suite unit
, bech32
, bech32-th
, bytestring
, blockfrost-api
, blockfrost-client
, cardano-addresses
, cardano-api
, cardano-crypto
Expand Down Expand Up @@ -273,6 +275,7 @@ test-suite unit
Cardano.Wallet.Shelley.CompatibilitySpec
Cardano.Wallet.Shelley.Compatibility.LedgerSpec
Cardano.Wallet.Shelley.LaunchSpec
Cardano.Wallet.Shelley.Launch.BlockfrostSpec
Cardano.Wallet.Shelley.NetworkSpec
Cardano.Wallet.Shelley.TransactionSpec
Spec
Expand Down
16 changes: 4 additions & 12 deletions lib/shelley/exe/cardano-wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ import Cardano.CLI
, tokenMetadataSourceOption
, withLogging
)
import Cardano.Launcher.Node
( CardanoNodeConn )
import Cardano.Startup
( ShutdownHandlerLog
, installSignalHandlers
Expand Down Expand Up @@ -102,9 +100,8 @@ import Cardano.Wallet.Shelley.BlockchainSource
import Cardano.Wallet.Shelley.Launch
( Mode (Light, Normal)
, NetworkConfiguration (..)
, modeFlag
, modeOption
, networkConfigurationOption
, nodeSocketOption
, parseGenesisData
)
import Cardano.Wallet.Version
Expand Down Expand Up @@ -185,7 +182,6 @@ data ServeArgs = ServeArgs
, _mode :: Mode
, _listen :: Listen
, _tlsConfig :: Maybe TlsConfiguration
, _nodeSocket :: CardanoNodeConn
, _networkConfiguration :: NetworkConfiguration
, _database :: Maybe FilePath
, _syncTolerance :: SyncTolerance
Expand All @@ -203,10 +199,9 @@ cmdServe = command "serve" $ info (helper <*> helper' <*> cmd) $

cmd = fmap exec $ ServeArgs
<$> hostPreferenceOption
<*> modeFlag
<*> modeOption
<*> listenOption
<*> optional tlsOption
<*> nodeSocketOption
<*> networkConfigurationOption
<*> optional databaseOption
<*> syncToleranceOption
Expand All @@ -221,7 +216,6 @@ cmdServe = command "serve" $ info (helper <*> helper' <*> cmd) $
mode
listen
tlsConfig
conn
networkConfig
databaseDir
sTolerance
Expand All @@ -242,12 +236,10 @@ cmdServe = command "serve" $ info (helper <*> helper' <*> cmd) $
setupDirectory (logInfo tr . MsgSetupDatabases)

blockchainSource <- case mode of
Normal ->
Normal conn ->
pure $ NodeSource conn vData
Light (Just token) ->
Light token ->
BlockfrostSource <$> Blockfrost.readToken token
Light Nothing ->
exitWith $ ExitFailure 34

exitWith =<< serveWallet
blockchainSource
Expand Down
31 changes: 12 additions & 19 deletions lib/shelley/src/Cardano/Wallet/Shelley/Launch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Cardano.Wallet.Shelley.Launch

-- * Light Mode
, Mode (..)
, modeFlag
, modeOption
) where

import Prelude
Expand Down Expand Up @@ -79,17 +79,7 @@ import Data.Text.Class
import GHC.TypeLits
( KnownNat, Nat, SomeNat (..), someNatVal )
import Options.Applicative
( Parser
, eitherReader
, flag
, flag'
, help
, long
, metavar
, option
, optional
, (<|>)
)
( Parser, eitherReader, flag', help, long, metavar, option, (<|>) )
import Ouroboros.Network.Magic
( NetworkMagic (..) )
import Ouroboros.Network.NodeToClient
Expand Down Expand Up @@ -400,12 +390,15 @@ instance HasSeverityAnnotation TempDirLog where
Mode
-------------------------------------------------------------------------------}

data Mode = Normal | Light (Maybe Blockfrost.TokenFile)
data Mode = Normal CardanoNodeConn | Light Blockfrost.TokenFile
deriving (Show)

modeFlag :: Parser Mode
modeFlag = do
light <- flag False True $
mconcat [ long "light", help "Enable light mode" ]
creds <- optional Blockfrost.tokenFileOption
pure $ if light then Light creds else Normal
modeOption :: Parser Mode
modeOption = normalMode <|> lightMode
where
normalMode =
Normal <$> nodeSocketOption
lightMode =
flag' () (long "light" <> help "Enable light mode") *>
fmap Light Blockfrost.tokenFileOption

6 changes: 3 additions & 3 deletions lib/shelley/src/Cardano/Wallet/Shelley/Launch/Blockfrost.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import Blockfrost.Client.Core
import Blockfrost.Client.Types
( Project (..) )
import Options.Applicative
( Parser, auto, help, long, metavar, option )
( Parser, help, long, metavar, option, str )

newtype TokenFile = TokenFile FilePath
deriving newtype (Eq, Show, Read)
deriving newtype (Eq, Show)

-- | --blockfrost-token-file FILE
tokenFileOption :: Parser TokenFile
tokenFileOption = option auto $ mconcat
tokenFileOption = option (TokenFile <$> str) $ mconcat
[ long "blockfrost-token-file"
, metavar "FILE"
, help $ mconcat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Cardano.Wallet.Shelley.Launch.BlockfrostSpec
( spec
) where

import Prelude

import qualified Blockfrost.Client.Types as Blockfrost
import qualified Data.Text as T

import Blockfrost.Env
( Env (Testnet) )
import Cardano.Wallet.Shelley.Launch
( Mode (Light, Normal), modeOption )
import Cardano.Wallet.Shelley.Launch.Blockfrost
( readToken )
import Options.Applicative
( ParserFailure (execFailure)
, ParserResult (CompletionInvoked, Failure, Success)
, defaultPrefs
, execParserPure
, fullDesc
, info
)
import Test.Hspec
( Spec, describe, expectationFailure, it, shouldBe, shouldReturn )
import UnliftIO
( withSystemTempFile )
import UnliftIO.IO
( hClose )

spec :: Spec
spec = describe "Blockfrost CLI options" $ do
it "modeOption --node-socket" $ do
let parserInfo = info modeOption fullDesc
args = ["--node-socket", "/tmp/file"]
case execParserPure defaultPrefs parserInfo args of
Failure pf -> expectationFailure $ show pf
CompletionInvoked cr -> expectationFailure $ show cr
Success (Light _) -> expectationFailure "Normal mode expected"
Success (Normal _conn) -> pure ()

it "modeOption --light" $ withSystemTempFile "blockfrost.token" $ \f h -> do
let parserInfo = info modeOption fullDesc
args = ["--light", "--blockfrost-token-file", f]
net = "testnet"
projectId = "jlUej4vcMt3nKPRAiNpLUEeKBIEPqgH2"
Unisay marked this conversation as resolved.
Show resolved Hide resolved
case execParserPure defaultPrefs parserInfo args of
Failure pf -> expectationFailure $ show pf
CompletionInvoked cr -> expectationFailure $ show cr
Success (Normal _conn) -> expectationFailure "Light mode expected"
Success (Light tf) -> do
hClose h *> writeFile f (net <> projectId)
readToken tf `shouldReturn`
Blockfrost.Project Testnet (T.pack projectId)

it "modeOption requires --light flag" $ do
let parserInfo = info modeOption fullDesc
args = ["--blockfrost-token-file", "/tmp/file"]
case execParserPure defaultPrefs parserInfo args of
Failure pf | (help, _code, _int) <- execFailure pf "" ->
show help `shouldBe`
"Missing: --light\n\n\
\Usage: (--node-socket FILE | \
\--light --blockfrost-token-file FILE)"
result -> expectationFailure $ show result
3 changes: 3 additions & 0 deletions nix/materialized/stack-nix/cardano-wallet.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.