-
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.
- Loading branch information
Showing
5 changed files
with
253 additions
and
3 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
161 changes: 161 additions & 0 deletions
161
lib/jormungandr/src/Cardano/Wallet/Network/Jormungandr.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,161 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-} | ||
|
||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
-- License: MIT | ||
-- | ||
-- | ||
-- A @NetworkLayer@ supporting jormungandr as a chain producer. | ||
|
||
module Cardano.Wallet.Network.Jormungandr | ||
( Jormungandr (..) | ||
, mkNetworkLayer | ||
, newNetworkLayer | ||
, mkJormungandr | ||
|
||
-- * Re-export | ||
, BaseUrl (..) | ||
, newManager | ||
, defaultManagerSettings | ||
, Scheme (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Network | ||
( ErrNetworkTip (..) | ||
, ErrNetworkUnreachable (..) | ||
, ErrPostTx (..) | ||
, NetworkLayer (..) | ||
) | ||
import Cardano.Wallet.Network.Jormungandr.Api | ||
( BlockId, GetBlock, GetTipId, SignedTx, api ) | ||
import Cardano.Wallet.Primitive.Types | ||
( Block ) | ||
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 Network.HTTP.Types.Status | ||
( status404 ) | ||
import Servant.API | ||
( (:<|>) (..) ) | ||
import Servant.Client | ||
( BaseUrl (..), ClientM, Scheme (..), client, mkClientEnv, runClientM ) | ||
import Servant.Client.Core | ||
( ServantError (..), responseStatusCode ) | ||
import Servant.Links | ||
( Link, safeLink ) | ||
|
||
-- | 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. | ||
data Jormungandr m = HttpBridge | ||
{ getBlock | ||
:: BlockId -> ExceptT ErrNetworkUnreachable m Block | ||
, getTipId | ||
:: ExceptT ErrNetworkTip m BlockId | ||
, getDecendantIds | ||
:: BlockId -> Int -> ExceptT ErrGetDecendants m [BlockId] | ||
, postSignedTx | ||
:: SignedTx -> ExceptT ErrPostTx m () | ||
} | ||
|
||
-- | Error while trying to get decendants | ||
data ErrGetDecendants | ||
= ErrGetDecendantsNetworkUnreachable ErrNetworkUnreachable | ||
| ErrGetDecendantsParentNotFound | ||
deriving (Show, Eq) | ||
|
||
instance Exception ErrGetDecendants | ||
|
||
-- | 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 = HttpBridge | ||
{ getBlock = \hash -> ExceptT $ do | ||
let ctx = safeLink api (Proxy @GetBlock) hash | ||
run (cGetBlock hash) >>= defaultHandler ctx | ||
|
||
, getTipId = ExceptT $ do | ||
run cGetTipId >>= \case | ||
Left (FailureResponse e) | responseStatusCode e == status404 -> | ||
return $ Left ErrNetworkTipNotFound | ||
x -> do | ||
let ctx = safeLink api (Proxy @GetTipId) | ||
left ErrNetworkTipNetworkUnreachable <$> defaultHandler ctx x | ||
|
||
, getDecendantIds = undefined | ||
, postSignedTx = undefined | ||
} | ||
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) | ||
|
||
cGetBlock | ||
:<|> cGetTipId | ||
:<|> _cGetBlockDecendantIds | ||
:<|> _cPostSignedTx | ||
= client api | ||
|
||
data ErrUnexpectedNetworkFailure | ||
= ErrUnexpectedNetworkFailure Link ServantError | ||
deriving (Show) | ||
|
||
instance Exception ErrUnexpectedNetworkFailure |
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
68 changes: 68 additions & 0 deletions
68
lib/jormungandr/test/integration/Cardano/Wallet/Network/JormungandrSpec.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,68 @@ | ||
module Cardano.Wallet.Network.JormungandrSpec | ||
( spec | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Launcher | ||
( Command (..), StdStream (..), launch ) | ||
import Cardano.Wallet.Network.Jormungandr | ||
( BaseUrl (..) | ||
, Scheme (Http) | ||
, defaultManagerSettings | ||
, getBlock | ||
, getTipId | ||
, mkJormungandr | ||
, newManager | ||
) | ||
import Cardano.Wallet.Network.Jormungandr.Api | ||
( BlockId (..) ) | ||
import Cardano.Wallet.Primitive.Types | ||
( Block (..), 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 ) | ||
|
||
import qualified Data.ByteString as BS | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "Happy paths" | ||
$ beforeAll startNode $ afterAll closeNode $ do | ||
it "get tip (should be genesis)" $ \(_, client) -> do | ||
(BlockId (Hash tipHash)) <- run $ getTipId client | ||
tipHash `shouldBe` genesisHash | ||
(BS.length tipHash) `shouldBe` 32 | ||
|
||
it "get block (genesis)" $ \(_, client) -> do | ||
b <- run $ getBlock client (BlockId $ Hash genesisHash) | ||
length (transactions b) `shouldBe` 1 | ||
|
||
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" | ||
|
||
run x = (either undefined id) <$> runExceptT x | ||
|
||
closeNode (handle, _) = do | ||
cancel handle | ||
threadDelay 1000000 | ||
|
||
startNode = do | ||
handle <- async $ launch $ return $ Command | ||
"jormungandr" | ||
[ "--genesis-block", dir ++ "/block-0.bin" | ||
, "--config", dir ++ "/node.config" | ||
, "--secret", dir ++ "/secret.yaml" | ||
] (return ()) | ||
Inherit | ||
manager <- newManager defaultManagerSettings | ||
let client = mkJormungandr manager (BaseUrl Http "localhost" 8080 "") | ||
threadDelay 1000000 | ||
return (handle, client) | ||
|
||
dir = "test/data/jormungandr" |
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