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

[ADP-3292] Separate lightSync in cardano-wallet-network-layer #4462

Merged
merged 2 commits into from
Feb 21, 2024
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
53 changes: 46 additions & 7 deletions lib/network-layer/cardano-wallet-network-layer.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 3.4
cabal-version: 3.6
name: cardano-wallet-network-layer
version: 0.1.0.0
synopsis: Node communication layer functionality.
Expand All @@ -14,19 +14,39 @@ maintainer: hal@cardanofoundation.org
category: Network
build-type: Simple
extra-doc-files: CHANGELOG.md

-- extra-source-files:

common warnings
ghc-options: -Wall
common language
default-language:
Haskell2010
default-extensions:
NoImplicitPrelude
OverloadedStrings

common opts-lib
ghc-options: -Wall -Wcompat -Wredundant-constraints

if flag(release)
ghc-options: -O2 -Werror

common opts-exe
import: opts-lib
ghc-options: -threaded -rtsopts

flag release
description: Enable optimization and `-Werror`
default: False
manual: True

library
import: warnings
import: language, opts-lib
hs-source-dirs: src
exposed-modules:
Cardano.Wallet.Network
Cardano.Wallet.Network.Implementation
Cardano.Wallet.Network.Implementation.Ouroboros
Cardano.Wallet.Network.Implementation.UnliftIO
Cardano.Wallet.Network.Light
Cardano.Wallet.Network.Logging
Cardano.Wallet.Network.Logging.Aggregation

Expand Down Expand Up @@ -73,5 +93,24 @@ library
, unliftio
, unliftio-core

hs-source-dirs: src
default-language: Haskell2010
test-suite unit
import: language, opts-exe
type: exitcode-stdio-1.0
hs-source-dirs: test
build-depends:
base
, bytestring
, cardano-wallet-network-layer
, cardano-wallet-primitive
, contra-tracer
, io-classes
, text
, transformers
, hspec
, QuickCheck
build-tool-depends:
hspec-discover:hspec-discover
main-is:
Main.hs
other-modules:
Cardano.Wallet.Network.LightSpec
39 changes: 28 additions & 11 deletions lib/network-layer/src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module Cardano.Wallet.Network
, mapChainSyncLog
, withFollowStatsMonitoring

-- * Light-mode
, LightLayer (..)
, LightBlocks

-- * Logging (for testing)
, FollowStats (..)
, Rearview (..)
Expand Down Expand Up @@ -110,6 +114,9 @@ import qualified Internal.Cardano.Write.Tx as Write
( PParams
)

{-----------------------------------------------------------------------------
NetworkLayer
------------------------------------------------------------------------------}
-- | Interface for network capabilities.
data NetworkLayer m block = NetworkLayer
{ chainSync
Expand All @@ -120,13 +127,6 @@ data NetworkLayer m block = NetworkLayer
-- The callbacks provided in the 'ChainFollower' argument
-- are used to handle intersection finding,
-- the arrival of new blocks, and rollbacks.
, lightSync
:: Maybe
( ChainFollower m ChainPoint BlockHeader (LightBlocks m Block)
-> m ()
)
-- ^ Connect to a data source that offers an efficient
-- query @Address -> Transactions@.
, currentNodeTip
:: m BlockHeader
-- ^ Get the current tip from the chain producer
Expand Down Expand Up @@ -186,17 +186,16 @@ data NetworkLayer m block = NetworkLayer
-- history has not yet been fetched from the node on startup.
}

-- | In light-mode, we receive either a list of blocks as usual,
-- or a 'LightSummary' of blocks.
type LightBlocks m block = Either (NonEmpty block) (LightSummary m)

instance Functor m => Functor (NetworkLayer m) where
fmap f nl =
nl
{ chainSync = \tr follower ->
chainSync nl tr $ mapChainFollower id id id (fmap f) follower
}

{-----------------------------------------------------------------------------
ChainFollower
------------------------------------------------------------------------------}
-- | A collection of callbacks to use with the 'chainSync' function.
data ChainFollower m point tip blocks = ChainFollower
{ checkpointPolicy :: Integer -> CheckpointPolicy
Expand Down Expand Up @@ -274,6 +273,24 @@ mapChainFollower fpoint12 fpoint21 ftip fblocks cf =
, rollBackward = fmap fpoint12 . rollBackward cf . fpoint21
}

{-----------------------------------------------------------------------------
LightSync
------------------------------------------------------------------------------}
-- | Interface for light-mode synchronization.
newtype LightLayer m block = LightLayer
{ lightSync
:: Maybe
( ChainFollower m ChainPoint BlockHeader (LightBlocks m Block)
-> m ()
)
-- ^ Connect to a data source that offers an efficient
-- query @Address -> Transactions@.
}

-- | In light-mode, we receive either a list of blocks as usual,
-- or a 'LightSummary' of blocks.
type LightBlocks m block = Either (NonEmpty block) (LightSummary m)

{-------------------------------------------------------------------------------
Errors
-------------------------------------------------------------------------------}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ withNodeNetworkLayerBase
let trChainSync = MsgConnectionStatus ClientChainSync >$< tr
retryHandlers = handlers ClientChainSync
connectClient trChainSync retryHandlers client versionData conn
, lightSync = Nothing
, currentNodeTip =
fromTip getGenesisBlockHash <$> atomically readNodeTip
, currentNodeEra =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Cardano.Wallet.Network.Implementation.UnliftIO
)
where

import Prelude

import qualified Control.Monad.Catch as Exceptions
import qualified UnliftIO

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Cardano.BM.Data.Tracer
import Cardano.Wallet.Network
( ChainFollower (..)
)
import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Block
( BlockHeader (..)
, ChainPoint (..)
, chainPointFromBlockHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Cardano.Wallet.Network.Light
, hoistLightSyncSource
, lightSync
)
import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Block
( BlockHeader (..)
, ChainPoint (..)
, chainPointFromBlockHeader
Expand Down
5 changes: 1 addition & 4 deletions lib/network-layer/test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
module Main (main) where

main :: IO ()
main = putStrLn "Test suite not yet implemented."
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
1 change: 0 additions & 1 deletion lib/unit/cardano-wallet-unit.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ test-suite unit
Cardano.Wallet.DB.Store.WalletState.StoreSpec
Cardano.Wallet.Delegation.ModelSpec
Cardano.Wallet.DelegationSpec
Cardano.Wallet.Network.LightSpec
Cardano.Wallet.Network.PortsSpec
Cardano.Wallet.NetworkSpec
Cardano.Wallet.Primitive.Delegation.StateSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ dummyNodeProtocolParameters = C.ProtocolParameters
dummyNetworkLayer :: HasCallStack => NetworkLayer m a
dummyNetworkLayer = NetworkLayer
{ chainSync = err "chainSync"
, lightSync = Nothing
, currentNodeEra = err "currentNodeEra"
, currentNodeTip = err "currentNodeTip"
, watchNodeTip = err "watchNodeTip"
Expand Down
1 change: 0 additions & 1 deletion lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ library
Cardano.Wallet.Flavor
Cardano.Wallet.Gen
Cardano.Wallet.Network.Config
Cardano.Wallet.Network.Light
Cardano.Wallet.Pools
Cardano.Wallet.Primitive.Delegation.State
Cardano.Wallet.Primitive.Delegation.UTxO
Expand Down
12 changes: 2 additions & 10 deletions lib/wallet/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ import Cardano.Wallet.Address.Keys.WalletKey
, liftRawKey
)
import Cardano.Wallet.Address.MaybeLight
( MaybeLight (maybeDiscover)
( MaybeLight
)
import Cardano.Wallet.Address.States.IsOwned
( isOwned
Expand Down Expand Up @@ -1241,15 +1241,7 @@ restoreWallet ctx = db & \DBLayer{..} ->
rollBackward = rollbackBlocks ctx . toSlot
rollForward' = restoreBlocks ctx (contramap MsgWalletFollow tr)
in
catchFromIO $ case (maybeDiscover, lightSync nw) of
(Just discover, Just sync) ->
sync $ ChainFollower
{ checkpointPolicy
, readChainPoints
, rollForward = rollForward' . either List (Summary discover)
, rollBackward
}
(_,_) -> -- light-mode not available
catchFromIO $
chainSync nw (contramap MsgChainFollow tr) $ ChainFollower
{ checkpointPolicy
, readChainPoints
Expand Down
Loading