From fd8cd040d3616e530d765ab0baa6880399d41219 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Wed, 21 Feb 2024 13:18:26 +0100 Subject: [PATCH 1/2] Move `lightSync` into separate `LightLayer` --- .../src/Cardano/Wallet/Network.hs | 39 +++++++++++++------ .../Cardano/Wallet/Network/Implementation.hs | 1 - .../Wallet/DummyTarget/Primitive/Types.hs | 1 - lib/wallet/src/Cardano/Wallet.hs | 12 +----- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/network-layer/src/Cardano/Wallet/Network.hs b/lib/network-layer/src/Cardano/Wallet/Network.hs index 3bdbcb9fde7..f059ead627e 100644 --- a/lib/network-layer/src/Cardano/Wallet/Network.hs +++ b/lib/network-layer/src/Cardano/Wallet/Network.hs @@ -17,6 +17,10 @@ module Cardano.Wallet.Network , mapChainSyncLog , withFollowStatsMonitoring + -- * Light-mode + , LightLayer (..) + , LightBlocks + -- * Logging (for testing) , FollowStats (..) , Rearview (..) @@ -110,6 +114,9 @@ import qualified Internal.Cardano.Write.Tx as Write ( PParams ) +{----------------------------------------------------------------------------- + NetworkLayer +------------------------------------------------------------------------------} -- | Interface for network capabilities. data NetworkLayer m block = NetworkLayer { chainSync @@ -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 @@ -186,10 +186,6 @@ 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 @@ -197,6 +193,9 @@ instance Functor m => Functor (NetworkLayer m) where 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 @@ -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 -------------------------------------------------------------------------------} diff --git a/lib/network-layer/src/Cardano/Wallet/Network/Implementation.hs b/lib/network-layer/src/Cardano/Wallet/Network/Implementation.hs index 66f2e21ea6f..3278115e5a4 100644 --- a/lib/network-layer/src/Cardano/Wallet/Network/Implementation.hs +++ b/lib/network-layer/src/Cardano/Wallet/Network/Implementation.hs @@ -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 = diff --git a/lib/unit/test-common/Cardano/Wallet/DummyTarget/Primitive/Types.hs b/lib/unit/test-common/Cardano/Wallet/DummyTarget/Primitive/Types.hs index 31d2e16fe38..3bb5f2d2967 100644 --- a/lib/unit/test-common/Cardano/Wallet/DummyTarget/Primitive/Types.hs +++ b/lib/unit/test-common/Cardano/Wallet/DummyTarget/Primitive/Types.hs @@ -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" diff --git a/lib/wallet/src/Cardano/Wallet.hs b/lib/wallet/src/Cardano/Wallet.hs index 2d1af0bf8e3..cf64f0b82ad 100644 --- a/lib/wallet/src/Cardano/Wallet.hs +++ b/lib/wallet/src/Cardano/Wallet.hs @@ -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 @@ -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 From 6dfcbb8e029cb6a4e778064dfb0702cf27bd2ed8 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Wed, 21 Feb 2024 14:02:22 +0100 Subject: [PATCH 2/2] Move `Cardano.Wallet.Network.Light` to `network-layer` --- .../cardano-wallet-network-layer.cabal | 53 ++++++++++++++++--- .../Wallet/Network/Implementation/UnliftIO.hs | 2 + .../src/Cardano/Wallet/Network/Light.hs | 2 +- .../test}/Cardano/Wallet/Network/LightSpec.hs | 2 +- lib/network-layer/test/Main.hs | 5 +- lib/unit/cardano-wallet-unit.cabal | 1 - lib/wallet/cardano-wallet.cabal | 1 - 7 files changed, 51 insertions(+), 15 deletions(-) rename lib/{wallet => network-layer}/src/Cardano/Wallet/Network/Light.hs (99%) rename lib/{unit/test/unit => network-layer/test}/Cardano/Wallet/Network/LightSpec.hs (99%) diff --git a/lib/network-layer/cardano-wallet-network-layer.cabal b/lib/network-layer/cardano-wallet-network-layer.cabal index d327d195fb6..d647afa603f 100644 --- a/lib/network-layer/cardano-wallet-network-layer.cabal +++ b/lib/network-layer/cardano-wallet-network-layer.cabal @@ -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. @@ -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 @@ -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 diff --git a/lib/network-layer/src/Cardano/Wallet/Network/Implementation/UnliftIO.hs b/lib/network-layer/src/Cardano/Wallet/Network/Implementation/UnliftIO.hs index b105cc9c6f7..8448b106ff6 100644 --- a/lib/network-layer/src/Cardano/Wallet/Network/Implementation/UnliftIO.hs +++ b/lib/network-layer/src/Cardano/Wallet/Network/Implementation/UnliftIO.hs @@ -4,6 +4,8 @@ module Cardano.Wallet.Network.Implementation.UnliftIO ) where +import Prelude + import qualified Control.Monad.Catch as Exceptions import qualified UnliftIO diff --git a/lib/wallet/src/Cardano/Wallet/Network/Light.hs b/lib/network-layer/src/Cardano/Wallet/Network/Light.hs similarity index 99% rename from lib/wallet/src/Cardano/Wallet/Network/Light.hs rename to lib/network-layer/src/Cardano/Wallet/Network/Light.hs index fc86eff1496..8b2d6e85f3d 100644 --- a/lib/wallet/src/Cardano/Wallet/Network/Light.hs +++ b/lib/network-layer/src/Cardano/Wallet/Network/Light.hs @@ -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 diff --git a/lib/unit/test/unit/Cardano/Wallet/Network/LightSpec.hs b/lib/network-layer/test/Cardano/Wallet/Network/LightSpec.hs similarity index 99% rename from lib/unit/test/unit/Cardano/Wallet/Network/LightSpec.hs rename to lib/network-layer/test/Cardano/Wallet/Network/LightSpec.hs index b7a0f9ac5d0..853da6d51d8 100644 --- a/lib/unit/test/unit/Cardano/Wallet/Network/LightSpec.hs +++ b/lib/network-layer/test/Cardano/Wallet/Network/LightSpec.hs @@ -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 diff --git a/lib/network-layer/test/Main.hs b/lib/network-layer/test/Main.hs index 3e2059e31f5..a824f8c30c8 100644 --- a/lib/network-layer/test/Main.hs +++ b/lib/network-layer/test/Main.hs @@ -1,4 +1 @@ -module Main (main) where - -main :: IO () -main = putStrLn "Test suite not yet implemented." +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} diff --git a/lib/unit/cardano-wallet-unit.cabal b/lib/unit/cardano-wallet-unit.cabal index 3a54586c6db..38bf15f320e 100644 --- a/lib/unit/cardano-wallet-unit.cabal +++ b/lib/unit/cardano-wallet-unit.cabal @@ -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 diff --git a/lib/wallet/cardano-wallet.cabal b/lib/wallet/cardano-wallet.cabal index a12ac77e24d..a40d7aa01b4 100644 --- a/lib/wallet/cardano-wallet.cabal +++ b/lib/wallet/cardano-wallet.cabal @@ -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