Skip to content

Commit

Permalink
Add checkpointPolicy field to ChainFollower
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Jul 1, 2022
1 parent d84a9f2 commit 5570557
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
10 changes: 7 additions & 3 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ import qualified Cardano.Address.Script as CA
import qualified Cardano.Api as Cardano
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Crypto.Wallet as CC
import qualified Cardano.Wallet.Checkpoints.Policy as CP
import qualified Cardano.Wallet.CoinSelection as CS
import qualified Cardano.Wallet.Primitive.AddressDiscovery.Random as Rnd
import qualified Cardano.Wallet.Primitive.AddressDiscovery.Sequential as Seq
Expand Down Expand Up @@ -969,7 +970,8 @@ restoreWallet
-> WalletId
-> ExceptT ErrNoSuchWallet IO ()
restoreWallet ctx wid = db & \DBLayer{..} ->
let readChainPoints = liftIO $ atomically $ listCheckpoints wid
let checkpointPolicy = CP.defaultPolicy
readChainPoints = liftIO $ atomically $ listCheckpoints wid
rollBackward =
throwInIO . rollbackBlocks @_ @s @k ctx wid . toSlot
rollForward' = \blockdata tip -> throwInIO $
Expand All @@ -979,13 +981,15 @@ restoreWallet ctx wid = db & \DBLayer{..} ->
catchFromIO $ case (maybeDiscover, lightSync nw) of
(Just discover, Just sync) ->
sync $ ChainFollower
{ readChainPoints
{ checkpointPolicy
, readChainPoints
, rollForward = rollForward' . either List (Summary discover)
, rollBackward
}
(_,_) -> -- light-mode not available
chainSync nw (contramap MsgChainFollow tr) $ ChainFollower
{ readChainPoints
{ checkpointPolicy
, readChainPoints
, rollForward = rollForward' . List
, rollBackward
}
Expand Down
18 changes: 16 additions & 2 deletions lib/core/src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import Cardano.BM.Data.Severity
( Severity (..) )
import Cardano.BM.Data.Tracer
( HasPrivacyAnnotation (..), HasSeverityAnnotation (..) )
import Cardano.Wallet.Checkpoints.Policy
( CheckpointPolicy )
import Cardano.Wallet.Primitive.BlockSummary
( LightSummary )
import Cardano.Wallet.Primitive.Slotting
Expand Down Expand Up @@ -196,12 +198,23 @@ instance Functor m => Functor (NetworkLayer m) where

-- | A collection of callbacks to use with the 'chainSync' function.
data ChainFollower m point tip blocks = ChainFollower
{ readChainPoints :: m [point]
{ checkpointPolicy :: Integer -> CheckpointPolicy
-- ^ The policy for creating and pruning checkpoints that
-- is used by the 'ChainFollower'.
-- The argument of this field is the @epochStability@.
--
-- Exposing this policy here enables any chain synchronizer
-- which does not retrieve full blocks, such as 'lightSync',
-- to specifically target those block heights at which
-- the 'ChainFollower' intends to create checkpoints.

, readChainPoints :: m [point]
-- ^ Callback for reading the local tip. Used to negotiate the
-- intersection with the node.
--
-- A response of [] is interpreted as `Origin` -- i.e. the chain will be
-- served from genesis.

, rollForward :: blocks -> tip -> m ()
-- ^ Callback for rolling forward.
--
Expand Down Expand Up @@ -254,7 +267,8 @@ mapChainFollower
-> ChainFollower m point2 tip2 blocks2
mapChainFollower fpoint12 fpoint21 ftip fblocks cf =
ChainFollower
{ readChainPoints = map fpoint12 <$> readChainPoints cf
{ checkpointPolicy = checkpointPolicy cf
, readChainPoints = map fpoint12 <$> readChainPoints cf
, rollForward = \bs tip -> rollForward cf (fblocks bs) (ftip tip)
, rollBackward = fmap fpoint12 . rollBackward cf . fpoint21
}
Expand Down
6 changes: 5 additions & 1 deletion lib/core/test/unit/Cardano/Wallet/Network/LightSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import Test.QuickCheck
, (===)
)

import qualified Cardano.Wallet.Checkpoints.Policy as CP
import qualified Data.ByteString.Char8 as B8
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -302,7 +303,10 @@ mkFollower
-> ChainFollower m ChainPoint BlockHeader
(LightBlocks m Block addr txs)
mkFollower lift = ChainFollower
{ readChainPoints = lift $ map chainPointFromBlockHeader . NE.toList <$> get
{ checkpointPolicy = \epochStability ->
CP.atTip <> CP.atGenesis
<> CP.trailingArithmetic 2 (min 1 $ epochStability `div` 3)
, readChainPoints = lift $ map chainPointFromBlockHeader . NE.toList <$> get
, rollForward = \blocks _tip -> lift $ modify $ \s -> case blocks of
Left bs ->
if latest s `isParentOf` NE.head bs
Expand Down
4 changes: 3 additions & 1 deletion lib/shelley/bench/restore-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ import UnliftIO.Temporary
( withSystemTempFile )

import qualified Cardano.Wallet as W
import qualified Cardano.Wallet.Checkpoints.Policy as CP
import qualified Cardano.Wallet.DB.Layer as Sqlite
import qualified Cardano.Wallet.Primitive.AddressDerivation.Byron as Byron
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
Expand Down Expand Up @@ -708,7 +709,8 @@ bench_baseline_restoration
synchronizer <- async
$ chainSync nw nullTracer
$ ChainFollower
{ readChainPoints = readTVarIO chainPointT
{ checkpointPolicy = CP.atTip
, readChainPoints = readTVarIO chainPointT
, rollForward = \blocks ntip -> do
atomically $ writeTVar chainPointT
[chainPointFromBlockHeader ntip]
Expand Down
4 changes: 3 additions & 1 deletion lib/shelley/src/Cardano/Wallet/Shelley/Pools.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ import qualified Blockfrost.Client as BF
import qualified Cardano.Pool.DB as PoolDb
import qualified Cardano.Pool.DB.Sqlite as Pool
import qualified Cardano.Wallet.Api.Types as Api
import qualified Cardano.Wallet.Checkpoints.Policy as CP
import qualified Cardano.Wallet.Shelley.Network.Blockfrost.Monad as BFM
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -728,7 +729,8 @@ monitorStakePools tr (NetworkParameters gp sp _pp) nl DBLayer{..} =
toChainPoint (BlockHeader sl _ h _) = ChainPoint sl h

chainSync nl (contramap MsgChainMonitoring tr) $ ChainFollower
{ readChainPoints = map toChainPoint <$> initCursor
{ checkpointPolicy = CP.defaultPolicy
, readChainPoints = map toChainPoint <$> initCursor
, rollForward = rollForward
, rollBackward = rollback
}
Expand Down

0 comments on commit 5570557

Please sign in to comment.