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

Scaffold lib/jormungandr folder #244

Merged
merged 3 commits into from
May 9, 2019
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
10 changes: 10 additions & 0 deletions .weeder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@
- name: Module not compiled
- module: Cardano.Launcher.Windows

- package:
- name: cardano-wallet-jormungandr
- section:
- name: test:unit
- message:
- name: Weeds exported
- module:
- name: Spec
- identifier: main

2 changes: 1 addition & 1 deletion exe/launcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Prelude

import Cardano.CLI
( Port, parseArgWith )
import Cardano.Environment
import Cardano.Environment.HttpBridge
( Network, network )
import Cardano.Launcher
( Command (Command)
Expand Down
2 changes: 1 addition & 1 deletion exe/wallet/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Cardano.CLI
, parseArgWith
, putErrLn
)
import Cardano.Environment
import Cardano.Environment.HttpBridge
( network )
import Cardano.Wallet
( mkWalletLayer )
Expand Down
2 changes: 2 additions & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ library
Cardano.Wallet.Api.Types
Cardano.Wallet.DB
Cardano.Wallet.DB.MVar
Cardano.Environment
Cardano.Wallet.Network
Cardano.Wallet.Primitive.AddressDerivation
Cardano.Wallet.Primitive.AddressDiscovery
Expand Down Expand Up @@ -130,6 +131,7 @@ test-suite unit
Cardano.Wallet.ApiSpec
Cardano.Wallet.DB.MVarSpec
Cardano.Wallet.DBSpec
Cardano.EnvironmentSpec
Cardano.Wallet.NetworkSpec
Cardano.Wallet.Primitive.AddressDerivationSpec
Cardano.Wallet.Primitive.AddressDiscoverySpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,28 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}

-- |
-- | Helpers for reading ENV vars using 'unsafePerformIO' with readable error
-- messages.
--
-- Copyright: © 2018-2019 IOHK
-- License: MIT
--
-- This module contains static configuration parameters. Rather than providing
-- and carrying around a configuration file through the application, we resolve
-- configuration data at runtime using the available environment.
--
-- This gives us a flexible and portable approach to software configuration, and
-- remove some pain from the development perspective. Prior to starting, the
-- wallet is expected to have a few configuration parameter available. One may
-- rely on a `.env` file to bundle configuration settings together for a given
-- target environment.

module Cardano.Environment
(
-- * Networking
Network(..)
, network
, ProtocolMagic(..)
, protocolMagic

-- * Internals
, ErrMissingOrInvalidEnvVar(..)
ErrMissingOrInvalidEnvVar(..)
, unsafeLookupEnv
) where

import Prelude

import Control.Exception
( Exception (..), throwIO )
import Data.Int
( Int32 )
import Data.Text
( Text )
import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..) )
( FromText (..), TextDecodingError (..) )
import Fmt
( Buildable (..), nameF, padLeftF, pretty )
import GHC.Generics
( Generic )
import System.Environment
( getProgName, lookupEnv )
import System.IO.Unsafe
Expand Down Expand Up @@ -124,43 +104,3 @@ unsafeLookupEnv k = unsafePerformIO $ do
, command = cmd
, additionalContext = Just (v, err)
}

{-------------------------------------------------------------------------------
Environment
-------------------------------------------------------------------------------}

-- | Available network options.
data Network = Mainnet | Testnet | Staging
deriving (Generic, Show, Eq, Enum)

instance FromText Network where
fromText = \case
"mainnet" -> Right Mainnet
"testnet" -> Right Testnet
"staging" -> Right Staging
s -> Left $ TextDecodingError $ T.unpack s
<> " is neither \"mainnet\", \"testnet\" nor \"staging\"."

instance ToText Network where
toText = \case
Mainnet -> "mainnet"
Testnet -> "testnet"
Staging -> "staging"

-- | Get the current target 'Network' from the Environment.
--
-- Throws a runtime exception is the ENV var isn't set or, is invalid.
network :: Network
network =
unsafeLookupEnv "NETWORK"
{-# NOINLINE network #-}

newtype ProtocolMagic = ProtocolMagic Int32
deriving (Generic, Show)

-- | Get the 'ProtocolMagic' corresponding to a given 'Network'.
protocolMagic :: Network -> ProtocolMagic
protocolMagic = \case
Mainnet -> ProtocolMagic 764824073
Staging -> ProtocolMagic 633343913
Testnet -> ProtocolMagic 1097911063
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.EnvironmentSpec where
module Cardano.EnvironmentSpec
( spec
) where

import Prelude

import Cardano.Environment
( ErrMissingOrInvalidEnvVar (..), Network, unsafeLookupEnv )
( ErrMissingOrInvalidEnvVar (..), unsafeLookupEnv )
import Data.Maybe
( isNothing )
import Data.Proxy
( Proxy (..) )
import Data.Text.Class
( TextDecodingError (..) )
( FromText (..), TextDecodingError (..), ToText (..) )
import GHC.Generics
( Generic )
import System.Environment
( setEnv, unsetEnv )
import Test.Hspec
Expand All @@ -23,14 +27,11 @@ import Test.QuickCheck
( Arbitrary (..) )
import Test.QuickCheck.Arbitrary.Generic
( genericArbitrary, genericShrink )
import Test.Text.Roundtrip
( textRoundtrip )

import qualified Data.Text as T

spec :: Spec
spec = do
describe "Can perform roundtrip textual encoding & decoding" $ do
textRoundtrip $ Proxy @Network

describe "ErrMissingOrInvalidEnvVar (Show / displayException)" $ do
let errNoAdditionalContext = ErrMissingOrInvalidEnvVar
{ name = "PATATE"
Expand Down Expand Up @@ -72,9 +73,26 @@ spec = do
io `shouldThrow` selector

{-------------------------------------------------------------------------------
Arbitrary Instances
Types
-------------------------------------------------------------------------------}

data Network = Mainnet | Testnet | Staging
deriving Generic

instance Arbitrary Network where
arbitrary = genericArbitrary
shrink = genericShrink

instance FromText Network where
fromText = \case
"mainnet" -> Right Mainnet
"testnet" -> Right Testnet
"staging" -> Right Staging
s -> Left $ TextDecodingError $ T.unpack s
<> " is neither \"mainnet\", \"testnet\" nor \"staging\"."

instance ToText Network where
toText = \case
Mainnet -> "mainnet"
Testnet -> "testnet"
Staging -> "staging"
5 changes: 2 additions & 3 deletions lib/http-bridge/cardano-wallet-http-bridge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ library
, cryptonite
, digest
, exceptions
, fmt
, http-api-data
, http-client
, http-media
Expand All @@ -57,7 +56,7 @@ library
hs-source-dirs:
src
exposed-modules:
Cardano.Environment
Cardano.Environment.HttpBridge
Cardano.Wallet.Binary.HttpBridge
Cardano.Wallet.Compatibility.HttpBridge
Cardano.Wallet.Network.HttpBridge
Expand Down Expand Up @@ -105,7 +104,7 @@ test-suite unit
main-is:
Main.hs
other-modules:
Cardano.EnvironmentSpec
Cardano.Environment.HttpBridgeSpec
Cardano.Wallet.Binary.HttpBridgeSpec
Cardano.Wallet.Network.HttpBridge.ApiSpec
Cardano.Wallet.Network.HttpBridgeSpec
Expand Down
74 changes: 74 additions & 0 deletions lib/http-bridge/src/Cardano/Environment/HttpBridge.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}

-- |
-- Copyright: © 2018-2019 IOHK
-- License: MIT
--
-- This module contains static configuration parameters. Rather than providing
-- and carrying around a configuration file through the application, we resolve
-- configuration data at runtime using the available environment.
--
-- This gives us a flexible and portable approach to software configuration, and
-- remove some pain from the development perspective. Prior to starting, the
-- wallet is expected to have a few configuration parameter available. One may
-- rely on a `.env` file to bundle configuration settings together for a given
-- target environment.

module Cardano.Environment.HttpBridge
(
-- * Networking
Network(..)
, network
, ProtocolMagic(..)
, protocolMagic
) where

import Prelude

import Cardano.Environment
( unsafeLookupEnv )
import Data.Int
( Int32 )
import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..) )
import GHC.Generics
( Generic )

import qualified Data.Text as T

-- | Available network options.
data Network = Mainnet | Testnet | Staging
deriving (Generic, Show, Eq, Enum)

instance FromText Network where
fromText = \case
"mainnet" -> Right Mainnet
"testnet" -> Right Testnet
"staging" -> Right Staging
s -> Left $ TextDecodingError $ T.unpack s
<> " is neither \"mainnet\", \"testnet\" nor \"staging\"."

instance ToText Network where
toText = \case
Mainnet -> "mainnet"
Testnet -> "testnet"
Staging -> "staging"

-- | Get the current target 'Network' from the Environment.
--
-- Throws a runtime exception is the ENV var isn't set or, is invalid.
network :: Network
network =
unsafeLookupEnv "NETWORK"
{-# NOINLINE network #-}

newtype ProtocolMagic = ProtocolMagic Int32
deriving (Generic, Show)

-- | Get the 'ProtocolMagic' corresponding to a given 'Network'.
protocolMagic :: Network -> ProtocolMagic
protocolMagic = \case
Mainnet -> ProtocolMagic 764824073
Staging -> ProtocolMagic 633343913
Testnet -> ProtocolMagic 1097911063
2 changes: 1 addition & 1 deletion lib/http-bridge/src/Cardano/Wallet/Binary/HttpBridge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Prelude

import Cardano.Crypto.Wallet
( ChainCode (..), XPub (..) )
import Cardano.Environment
import Cardano.Environment.HttpBridge
( ProtocolMagic (..) )
import Cardano.Wallet.Primitive.Types
( Address (..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Cardano.Wallet.Compatibility.HttpBridge

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( Network (Mainnet, Staging, Testnet), network, protocolMagic )
import Cardano.Wallet.Binary.HttpBridge
( encodeAddress, encodeProtocolMagic, encodeTx )
Expand Down
2 changes: 1 addition & 1 deletion lib/http-bridge/src/Cardano/Wallet/Network/HttpBridge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Cardano.Wallet.Network.HttpBridge

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( network )
import Cardano.Wallet.Network
( ErrNetworkTip (..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Cardano.Wallet.Transaction.HttpBridge

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( Network (..), ProtocolMagic (..), network, protocolMagic )
import Cardano.Wallet.Binary.HttpBridge
( toByteString )
Expand Down
2 changes: 1 addition & 1 deletion lib/http-bridge/test/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Main where

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( network )
import Cardano.Launcher
( Command (Command), StdStream (..), installSignalHandlers, launch )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Cardano.Wallet.Network.HttpBridgeSpec

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( Network (..), network )
import Cardano.Launcher
( Command (..), StdStream (..), launch )
Expand Down
2 changes: 1 addition & 1 deletion lib/http-bridge/test/integration/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Cardano.WalletSpec

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( network )
import Cardano.Launcher
( Command (..), StdStream (..), launch )
Expand Down
2 changes: 1 addition & 1 deletion lib/http-bridge/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Main where

import Prelude

import Cardano.Environment
import Cardano.Environment.HttpBridge
( Network (..), network )
import Cardano.Launcher
( Command (..), StdStream (..), launch )
Expand Down
Loading