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

Jörmungandr: mkNetworkLayer with networkTip #383

Merged
merged 1 commit into from
Jun 11, 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
2 changes: 2 additions & 0 deletions lib/core/src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ instance Exception ErrNetworkUnreachable
data ErrNetworkTip
= ErrNetworkTipNetworkUnreachable ErrNetworkUnreachable
| ErrNetworkTipNotFound
| ErrNetworkTipBlockNotFound (Hash "BlockHeader")
-- ^ The tip-block wasn't found. This would be surprising.
deriving (Generic, Show, Eq)

instance Exception ErrNetworkTip
Expand Down
2 changes: 2 additions & 0 deletions lib/http-bridge/src/Cardano/Wallet/HttpBridge/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ rbNextBlocks bridge start = maybeTip (getNetworkTip bridge) >>= \case

maybeTip = mapExceptT $ fmap $ \case
Left (ErrNetworkTipNetworkUnreachable e) -> Left e
Left (ErrNetworkTipBlockNotFound _) -> Right Nothing
-- HttpBridge never throws ErrNetworkTipBlockNotFound.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semi-awkward

Left ErrNetworkTipNotFound -> Right Nothing
Right tip -> Right (Just tip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ spec = do
where
unwrap (ErrNetworkTipNetworkUnreachable e) = e
unwrap ErrNetworkTipNotFound = ErrNetworkUnreachable "no tip"
unwrap (ErrNetworkTipBlockNotFound _) = ErrNetworkUnreachable "no tip"
newNetworkLayer =
HttpBridge.newNetworkLayer @'Testnet port
closeBridge (handle, _) = do
Expand Down
2 changes: 1 addition & 1 deletion lib/jormungandr/src/Cardano/Wallet/Jormungandr/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type PostSignedTx
-- TODO: Replace SignedTx with something real
data SignedTx

newtype BlockId = BlockId (Hash "block")
newtype BlockId = BlockId (Hash "BlockHeader")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think Hash "BlockHeader" is correct, and BlockId probably isn't needed - as either a newtype or type synonym.

Copy link
Member Author

@Anviking Anviking Jun 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doubles as an ApiT to avoid orphans so it is needed (in the Api type). I kept it in JormungandrLayer for now.

Also I introduced it because jormungandr/JormungandrLayer often refers to "id" in the method-names.

deriving (Eq, Show)

instance ToHttpApiData BlockId where
Expand Down
27 changes: 23 additions & 4 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module Cardano.Wallet.Jormungandr.Network
( JormungandrLayer (..)
, mkJormungandrLayer

, mkNetworkLayer

-- * Re-export
, BaseUrl (..)
, newManager
Expand All @@ -29,9 +31,9 @@ module Cardano.Wallet.Jormungandr.Network
import Prelude

import Cardano.Wallet.Jormungandr.Api
( BlockId, GetBlock, GetBlockDescendantIds, GetTipId, api )
( BlockId (..), GetBlock, GetBlockDescendantIds, GetTipId, api )
import Cardano.Wallet.Network
( ErrNetworkUnreachable (..) )
( ErrNetworkTip (..), ErrNetworkUnreachable (..), NetworkLayer (..) )
import Cardano.Wallet.Primitive.Types
( Block (..) )
import Control.Arrow
Expand All @@ -41,7 +43,7 @@ import Control.Exception
import Control.Monad.Catch
( throwM )
import Control.Monad.Trans.Except
( ExceptT (..) )
( ExceptT (..), withExceptT )
import Data.Proxy
( Proxy (..) )
import Network.HTTP.Client
Expand All @@ -64,7 +66,24 @@ import Servant.Client.Core
import Servant.Links
( Link, safeLink )

-- TODO: Implement a NetworkLayer
mkNetworkLayer :: Monad m => JormungandrLayer m -> NetworkLayer t m
mkNetworkLayer j = NetworkLayer
{ networkTip = do
t@(BlockId hash) <- (getTipId j)
`mappingError` ErrNetworkTipNetworkUnreachable
b <- (getBlock j t)
`mappingError` \case
ErrGetBlockNotFound (BlockId h) ->
ErrNetworkTipBlockNotFound h
ErrGetBlockNetworkUnreachable e ->
ErrNetworkTipNetworkUnreachable e
return (hash, header b)

, nextBlocks = error "nextBlocks to be implemented"
, postTx = error "postTx to be implemented"
}
where
mappingError = flip withExceptT

{-------------------------------------------------------------------------------
Jormungandr Client
Expand Down