Skip to content

Commit

Permalink
Initial NetworkLayer implementation for Byron
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jan 24, 2020
1 parent e1e512f commit b2f287f
Show file tree
Hide file tree
Showing 15 changed files with 1,990 additions and 119 deletions.
104 changes: 104 additions & 0 deletions lib/byron/cardano-wallet-byron.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: cardano-wallet-byron
version: 2020.1.21
synopsis: Wallet backend protocol-specific bits implemented using byron nodes
description: Please see README.md
homepage: https://github.com/input-output-hk/cardano-wallet
author: IOHK Engineering Team
maintainer: [email protected]
copyright: 2020 IOHK
license: Apache-2.0
category: Web
build-type: Simple
cabal-version: >=1.10

flag development
description: Disable `-Werror`
default: False
manual: True

library
default-language:
Haskell2010
default-extensions:
NoImplicitPrelude
OverloadedStrings
ghc-options:
-Wall
-Wcompat
-fwarn-redundant-constraints
if (!flag(development))
ghc-options:
-Werror
build-depends:
base
, async
, bytestring
, cardano-binary
, cardano-crypto-wrapper
, cardano-ledger
, cardano-slotting
, cardano-wallet-cli
, cardano-wallet-core
, cardano-wallet-launcher
, contra-tracer
, cryptonite
, deepseq
, either
, fmt
, io-sim-classes
, iohk-monitoring
, memory
, network
, network-mux
, optparse-applicative
, ouroboros-consensus
, ouroboros-network
, serialise
, stm
, temporary
, text
, text-class
, time
, transformers
, typed-protocols
, typed-protocols-cbor
, warp
hs-source-dirs:
src
exposed-modules:
Cardano.Wallet.Byron
Cardano.Wallet.Byron.Compatibility
Cardano.Wallet.Byron.Network
Cardano.Wallet.Byron.Transaction

executable cardano-wallet-byron
default-language:
Haskell2010
default-extensions:
NoImplicitPrelude
OverloadedStrings
ghc-options:
-threaded -rtsopts
-Wall
-O2
if (!flag(development))
ghc-options:
-Werror
build-depends:
base
, cardano-wallet-byron
, cardano-wallet-cli
, cardano-wallet-core
, cardano-wallet-launcher
, filepath
, fmt
, iohk-monitoring
, network
, optparse-applicative
, text
, text-class
, transformers
hs-source-dirs:
exe
main-is:
cardano-wallet-byron.hs
242 changes: 242 additions & 0 deletions lib/byron/exe/cardano-wallet-byron.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- This module parses command line arguments for the wallet and executes
-- corresponding commands.
--
-- In essence, it's a proxy to the wallet server, which is required for most
-- commands. Commands are turned into corresponding API calls, and submitted
-- to an up-and-running server. Some commands do not require an active server
-- and can be run "offline".

module Main where

import Prelude

import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Trace
( Trace, appendName, logDebug, logInfo )
import Cardano.CLI
( LoggingOptions (..)
, cli
, cmdAddress
, cmdMnemonic
, cmdNetwork
, cmdTransaction
, cmdVersion
, cmdWallet
, databaseOption
, enableWindowsANSI
, helperTracing
, hostPreferenceOption
, listenOption
, loggingOptions
, loggingSeverityOrOffReader
, optionT
, runCli
, setupDirectory
, syncToleranceOption
, withLogging
)
import Cardano.Launcher
( withUtf8Encoding )
import Cardano.Wallet.Api.Server
( HostPreference, Listen (..) )
import Cardano.Wallet.Byron
( TracerSeverities
, Tracers
, Tracers' (..)
, serveWallet
, setupTracers
, tracerDescriptions
, tracerLabels
)
import Cardano.Wallet.Byron.Network
( localSocketAddrInfo )
import Cardano.Wallet.Logging
( transformTextTrace )
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..) )
import Cardano.Wallet.Primitive.Types
( SyncTolerance )
import Cardano.Wallet.Version
( GitRevision, Version, gitRevision, showFullVersion, version )
import Control.Applicative
( Const (..), optional )
import Data.Text
( Text )
import Data.Text.Class
( ToText (..) )
import Network.Socket
( SockAddr )
import Options.Applicative
( CommandFields
, Mod
, Parser
, command
, help
, helper
, info
, internal
, long
, metavar
, option
, progDesc
, value
)
import System.Environment
( getArgs, getExecutablePath )
import System.Exit
( exitWith )

import qualified Data.Text as T

{-------------------------------------------------------------------------------
Main entry point
-------------------------------------------------------------------------------}

main :: IO ()
main = withUtf8Encoding $ do
enableWindowsANSI
runCli $ cli $ mempty
<> cmdServe
<> cmdMnemonic
<> cmdWallet @'Mainnet
<> cmdTransaction @'Mainnet
<> cmdAddress @'Mainnet
<> cmdNetwork @'Mainnet
<> cmdVersion

beforeMainLoop
:: Trace IO MainLog
-> SockAddr
-> IO ()
beforeMainLoop tr =
logInfo tr . MsgListenAddress

{-------------------------------------------------------------------------------
Command - 'serve'
-------------------------------------------------------------------------------}

-- | Arguments for the 'serve' command
data ServeArgs = ServeArgs
{ _hostPreference :: HostPreference
, _listen :: Listen
, _nodeSocket :: FilePath
, _database :: Maybe FilePath
, _syncTolerance :: SyncTolerance
, _logging :: LoggingOptions TracerSeverities
} deriving (Show, Eq)

cmdServe
:: Mod CommandFields (IO ())
cmdServe = command "serve" $ info (helper <*> helper' <*> cmd) $ mempty
<> progDesc "Serve API that listens for commands/actions."
where
helper' = helperTracing tracerDescriptions

cmd = fmap exec $ ServeArgs
<$> hostPreferenceOption
<*> listenOption
<*> nodeSocketOption
<*> optional databaseOption
<*> syncToleranceOption
<*> loggingOptions tracerSeveritiesOption
exec
:: ServeArgs
-> IO ()
exec args@(ServeArgs hostPreference listen nodeSocket databaseDir sTolerance logOpt) = do
let addrInfo = localSocketAddrInfo nodeSocket
withTracers logOpt $ \tr tracers -> do
logDebug tr $ MsgServeArgs args
whenJust databaseDir $ setupDirectory (logInfo tr . MsgSetupDatabases)
exitWith =<< serveWallet @'Mainnet
tracers
sTolerance
databaseDir
hostPreference
listen
addrInfo
(beforeMainLoop tr)

whenJust m fn = case m of
Nothing -> pure ()
Just a -> fn a

{-------------------------------------------------------------------------------
Options
-------------------------------------------------------------------------------}

-- | --node-socket=FILE
nodeSocketOption :: Parser FilePath
nodeSocketOption = optionT $ mempty
<> long "node-socket"
<> metavar "FILE"
<> help "Path to the node's domain socket."

tracerSeveritiesOption :: Parser TracerSeverities
tracerSeveritiesOption = Tracers
<$> traceOpt applicationTracer (Just Info)
<*> traceOpt apiServerTracer (Just Info)
<*> traceOpt walletEngineTracer (Just Info)
<*> traceOpt walletDbTracer (Just Info)
where
traceOpt field def = fmap Const . option loggingSeverityOrOffReader $ mempty
<> long ("trace-" <> T.unpack (getConst (field tracerLabels)))
<> value def
<> metavar "SEVERITY"
<> internal

{-------------------------------------------------------------------------------
Logging
-------------------------------------------------------------------------------}

-- FIXME: reduce duplication. See 'cardano-wallet-jormungandr.hs'

data MainLog
= MsgCmdLine String [String]
| MsgVersion Version GitRevision
| MsgSetupStateDir Text
| MsgSetupDatabases Text
| MsgServeArgs ServeArgs
| MsgListenAddress SockAddr
deriving (Show, Eq)

instance ToText MainLog where
toText msg = case msg of
MsgCmdLine exe args ->
T.pack $ unwords ("Command line:":exe:args)
MsgVersion ver rev ->
"Running as v" <> T.pack (showFullVersion ver rev)
MsgSetupStateDir txt ->
"Wallet state: " <> txt
MsgSetupDatabases txt ->
"Wallet databases: " <> txt
MsgServeArgs args ->
T.pack $ show args
MsgListenAddress addr ->
"Wallet backend server listening on " <> T.pack (show addr)

withTracers
:: LoggingOptions TracerSeverities
-> (Trace IO MainLog -> Tracers IO -> IO a)
-> IO a
withTracers logOpt action =
withLogging Nothing (loggingMinSeverity logOpt) $ \(_, tr) -> do
let trMain = appendName "main" (transformTextTrace tr)
let tracers = setupTracers (loggingTracers logOpt) tr
logInfo trMain $ MsgVersion version gitRevision
logInfo trMain =<< MsgCmdLine <$> getExecutablePath <*> getArgs
action trMain tracers
Loading

0 comments on commit b2f287f

Please sign in to comment.