-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jormungandr client supporting
getTipId
- Loading branch information
Showing
5 changed files
with
244 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
-- License: MIT | ||
-- | ||
-- | ||
-- | ||
-- This module allows the wallet to retrieve blocks from a known @Jormungandr@ | ||
-- node. This is done by providing a @NetworkLayer@ with some logic building on | ||
-- top of an underlying @Jormungandr@ HTTP client. | ||
module Cardano.Wallet.Jormungandr.Network | ||
( Jormungandr (..) | ||
, mkJormungandr | ||
|
||
-- * Re-export | ||
, BaseUrl (..) | ||
, newManager | ||
, defaultManagerSettings | ||
, Scheme (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Jormungandr.Api | ||
( BlockId, GetTipId, api ) | ||
import Cardano.Wallet.Network | ||
( ErrNetworkTip (..), ErrNetworkUnreachable (..) ) | ||
import Control.Arrow | ||
( left ) | ||
import Control.Exception | ||
( Exception ) | ||
import Control.Monad.Catch | ||
( throwM ) | ||
import Control.Monad.Trans.Except | ||
( ExceptT (..) ) | ||
import Data.Proxy | ||
( Proxy (..) ) | ||
import Network.HTTP.Client | ||
( Manager, defaultManagerSettings, newManager ) | ||
import Servant.Client | ||
( BaseUrl (..), ClientM, Scheme (..), client, mkClientEnv, runClientM ) | ||
import Servant.Client.Core | ||
( ServantError (..) ) | ||
import Servant.Links | ||
( Link, safeLink ) | ||
|
||
-- TODO: Implement a NetworkLayer | ||
-- -- | Constructs a network layer with the given @Jormungandr@ client. | ||
-- mkNetworkLayer :: Monad m => Jormungandr m -> NetworkLayer m | ||
-- mkNetworkLayer _httpBridge = NetworkLayer | ||
-- { nextBlocks = undefined | ||
-- , networkTip = undefined | ||
-- , postTx = undefined | ||
-- } | ||
-- | ||
-- -- | Creates a jormungandr 'NetworkLayer' using the given connection | ||
-- -- settings. | ||
-- newNetworkLayer | ||
-- :: Int -> IO (NetworkLayer IO) | ||
-- newNetworkLayer port = mkNetworkLayer <$> newHttpBridge port | ||
-- where | ||
-- newHttpBridge = undefined | ||
|
||
{------------------------------------------------------------------------------- | ||
Jormungandr Client | ||
-------------------------------------------------------------------------------} | ||
|
||
-- | Endpoints of the jormungandr REST API. | ||
newtype Jormungandr m = Jormungandr | ||
{ getTipId | ||
:: ExceptT ErrNetworkTip m BlockId | ||
} | ||
|
||
-- | Error while trying to get descendants | ||
data ErrGetDescendants | ||
= ErrGetDescendantsNetworkUnreachable ErrNetworkUnreachable | ||
| ErrGetDescendantsParentNotFound | ||
deriving (Show, Eq) | ||
|
||
instance Exception ErrGetDescendants | ||
|
||
-- | Construct a Jormungandr-client | ||
-- | ||
-- >>> mgr <- newManager defaultManagerSettings | ||
-- >>> j = mkJormungandr mgr (BaseUrl Http "localhost" 8080 "") | ||
-- >>> runExceptT $ getTipId j | ||
-- Right (BlockId (Hash {getHash = "26c640a3de09b74398c14ca0a137ec78"})) | ||
mkJormungandr | ||
:: Manager -> BaseUrl -> Jormungandr IO | ||
mkJormungandr mgr baseUrl = Jormungandr | ||
{ getTipId = ExceptT $ do | ||
let ctx = safeLink api (Proxy @GetTipId) | ||
run cGetTipId >>= \x -> | ||
left ErrNetworkTipNetworkUnreachable <$> defaultHandler ctx x | ||
} | ||
where | ||
run :: ClientM a -> IO (Either ServantError a) | ||
run query = runClientM query (mkClientEnv mgr baseUrl) | ||
|
||
defaultHandler | ||
:: Link | ||
-> Either ServantError a | ||
-> IO (Either ErrNetworkUnreachable a) | ||
defaultHandler ctx = \case | ||
Right c -> return $ Right c | ||
|
||
-- The node has not started yet or has exited. | ||
-- This could be recovered from by either waiting for the node | ||
-- initialise, or restarting the node. | ||
Left (ConnectionError e) -> | ||
return $ Left $ ErrNetworkUnreachable e | ||
|
||
-- Other errors (status code, decode failure, invalid content type | ||
-- headers). These are considered to be programming errors, so crash. | ||
Left e -> do | ||
throwM (ErrUnexpectedNetworkFailure ctx e) | ||
|
||
cGetTipId = client api | ||
|
||
data ErrUnexpectedNetworkFailure | ||
= ErrUnexpectedNetworkFailure Link ServantError | ||
deriving (Show) | ||
|
||
instance Exception ErrUnexpectedNetworkFailure |
89 changes: 89 additions & 0 deletions
89
lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
module Cardano.Wallet.Jormungandr.NetworkSpec | ||
( spec | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Launcher | ||
( Command (..), StdStream (..), launch ) | ||
import Cardano.Wallet | ||
( unsafeRunExceptT ) | ||
import Cardano.Wallet.Jormungandr.Api | ||
( BlockId (..) ) | ||
import Cardano.Wallet.Jormungandr.Network | ||
( BaseUrl (..) | ||
, Scheme (Http) | ||
, defaultManagerSettings | ||
, getTipId | ||
, mkJormungandr | ||
, newManager | ||
) | ||
import Cardano.Wallet.Network | ||
( ErrNetworkTip (..) ) | ||
import Cardano.Wallet.Primitive.Types | ||
( Hash (..) ) | ||
import Control.Concurrent | ||
( threadDelay ) | ||
import Control.Concurrent.Async | ||
( async, cancel ) | ||
import Control.Monad.Trans.Except | ||
( runExceptT ) | ||
import Test.Hspec | ||
( Spec | ||
, afterAll | ||
, beforeAll | ||
, describe | ||
, it | ||
, shouldBe | ||
, shouldReturn | ||
, shouldSatisfy | ||
) | ||
|
||
import qualified Data.ByteString as BS | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "happy paths" $ beforeAll startNode $ afterAll closeNode $ do | ||
describe "getTipId" $ do | ||
it "gets the id of the genesis block" $ \(_, client) -> do | ||
(BlockId (Hash tipHash)) <- unsafeRunExceptT $ getTipId client | ||
tipHash `shouldBe` genesisHash | ||
(BS.length tipHash) `shouldBe` 32 | ||
|
||
describe "error paths" $ beforeAll newClient $ do | ||
describe "getTipId" $ do | ||
it "gets a 'ErrNetworkUnreachable' if jormungandr isn't up (1)" | ||
$ \bridge -> do | ||
let msg x = "Expected a ErrNetworkUnreachable' failure but got " | ||
<> show x | ||
let action = do | ||
res <- runExceptT $ getTipId bridge | ||
res `shouldSatisfy` \case | ||
Left (ErrNetworkTipNetworkUnreachable _) -> True | ||
_ -> error (msg res) | ||
action `shouldReturn` () | ||
|
||
where | ||
genesisHash = "&\198@\163\222\t\183C\152\193L\160\161\&7\236x\245\229\EOT\175\177\167\131\190\b\b/\174\212\177:\179" | ||
|
||
newClient = do | ||
threadDelay 1000000 | ||
manager <- newManager defaultManagerSettings | ||
return $ mkJormungandr manager (BaseUrl Http "localhost" 8081 "") | ||
|
||
startNode = do | ||
let dir = "test/data/jormungandr" | ||
handle <- async $ launch $ return $ Command | ||
"jormungandr" | ||
[ "--genesis-block", dir ++ "/block-0.bin" | ||
, "--config", dir ++ "/node.config" | ||
, "--secret", dir ++ "/secret.yaml" | ||
] (return ()) | ||
Inherit | ||
client <- newClient | ||
return (handle, client) | ||
|
||
closeNode (handle, _) = do | ||
cancel handle | ||
threadDelay 1000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters