Skip to content

Commit

Permalink
Change watchNodeTip to use Read.ChainPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Apr 18, 2024
1 parent e5e1f69 commit 85e1eb1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/network-layer/src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ data NetworkLayer m block = NetworkLayer
-- ^ Get the last known slotting parameters. In principle, these can
-- only change once per era.
, watchNodeTip
:: (BlockHeader -> m ())
:: (Read.ChainTip -> m ())
-> m ()
-- ^ Register a callback for when the node tip changes.
-- This function should never finish, unless the callback throws an
Expand Down
13 changes: 7 additions & 6 deletions lib/network-layer/src/Cardano/Wallet/Network/Implementation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ import Cardano.Wallet.Primitive.Ledger.Read.Block.Header
)
import Cardano.Wallet.Primitive.Ledger.Shelley
( fromPoint
, fromTip
, fromTip'
, nodeToClientVersions
, toCardanoEra
Expand Down Expand Up @@ -385,6 +384,7 @@ import qualified Cardano.Wallet.Primitive.SyncProgress as SP
import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.RewardAccount as W
import qualified Cardano.Wallet.Primitive.Types.Tx as W
import qualified Cardano.Wallet.Read as Read
import qualified Codec.CBOR.Term as CBOR
import qualified Data.Map as Map
import qualified Data.Set as Set
Expand Down Expand Up @@ -654,10 +654,11 @@ withNodeNetworkLayerBase
return res
Nothing -> pure $ StakePoolsSummary 0 mempty mempty

_watchNodeTip readTip cb = do
_watchNodeTip readTip callback = do
observeForever readTip $ \tip -> do
let header = fromTip getGenesisBlockHash tip
bracketTracer (contramap (MsgWatcherUpdate header) tr) $ cb header
let tip' = fromOuroborosTip tip
bracketTracer (contramap (MsgWatcherUpdate tip') tr)
$ callback tip'

-- TODO(#2042): Make wallets call manually, with matching stopObserving.
_getCachedRewardAccountBalance rewardsObserver k = do
Expand Down Expand Up @@ -1356,7 +1357,7 @@ data Log where
-> Log
-- ^ Number of pools in stake distribution, and rewards map,
-- respectively.
MsgWatcherUpdate :: BlockHeader -> BracketLog -> Log
MsgWatcherUpdate :: Read.ChainTip -> BracketLog -> Log
MsgInterpreter :: CardanoInterpreter StandardCrypto -> Log
-- TODO: Combine ^^ and vv
MsgInterpreterLog :: TimeInterpreterLog -> Log
Expand Down Expand Up @@ -1428,7 +1429,7 @@ instance ToText Log where
]
MsgWatcherUpdate tip b ->
"Update watcher with tip: "
<> pretty tip
<> Read.prettyChainTip tip
<> ". Callback "
<> toText b
<> "."
Expand Down
18 changes: 12 additions & 6 deletions lib/unit/test/unit/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ import Cardano.Wallet.Primitive.Passphrase.Current
)
import Cardano.Wallet.Primitive.Types
( ActiveSlotCoefficient (..)
, BlockHeader (BlockHeader)
, NetworkParameters (..)
, SlotNo (..)
, SlottingParameters (..)
Expand Down Expand Up @@ -1002,13 +1001,20 @@ prop_localTxSubmission tc = monadicIO $ do
, watchNodeTip = mockNodeTip (numSlots tc) 0
}

mockNodeTip end sl cb
| sl < end = do
let h = Hash ""
void $ cb $ BlockHeader (SlotNo sl) (Quantity (fromIntegral sl)) h (Just h)
mockNodeTip end (sl + 1) cb
mockNodeTip end slot callback
| slot < end = do
let tip = Read.BlockTip
{ slotNo = Read.SlotNo $ fromIntegral slot
, headerHash = mockHash
, blockNo = Read.BlockNo $ fromIntegral slot
}
void $ callback tip
mockNodeTip end (slot + 1) callback
| otherwise = pure ()

mockHash :: Read.RawHeaderHash
mockHash = Read.mockRawHeaderHash 0

stash :: MVar [a] -> a -> TxRetryTestM ()
stash var x = modifyMVar_ var (\xs -> pure (x:xs))

Expand Down
22 changes: 13 additions & 9 deletions lib/wallet/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,8 +1651,8 @@ manageRewardBalance
-> DBLayer IO (SeqState n ShelleyKey)
-> IO ()
manageRewardBalance tr' netLayer db = do
watchNodeTip netLayer $ \bh -> do
traceWith tr $ MsgRewardBalanceQuery bh
watchNodeTip netLayer $ \nodeTip -> do
traceWith tr $ MsgRewardBalanceQuery nodeTip
query <- do
(acct, _, _) <- readRewardAccount db
liftIO $ getCachedRewardAccountBalance netLayer acct
Expand Down Expand Up @@ -1695,8 +1695,8 @@ manageSharedRewardBalance
-> DBLayer IO (SharedState n SharedKey)
-> IO ()
manageSharedRewardBalance tr' netLayer db = do
watchNodeTip netLayer $ \bh -> do
traceWith tr $ MsgRewardBalanceQuery bh
watchNodeTip netLayer $ \nodeTip -> do
traceWith tr $ MsgRewardBalanceQuery nodeTip
query <- runExceptT $ do
(acct, _, _) <-
lift $ readRewardAccount @(SharedState n SharedKey) db
Expand Down Expand Up @@ -2804,10 +2804,13 @@ runLocalTxSubmissionPool
-> WalletLayer m s
-> m ()
runLocalTxSubmissionPool cfg ctx = db & \DBLayer{..} -> do
submitPending <- rateLimited $ \bh -> bracketTracer trBracket $ do
submitPending <- rateLimited $ \nodeTip -> bracketTracer trBracket $ do
sp <- currentSlottingParameters nw
pending <- readLocalTxSubmissionPending ctx
let sl = bh ^. #slotNo
let sl = case nodeTip of
Read.GenesisTip -> SlotNo 0
Read.BlockTip{slotNo} ->
SlotNo $ fromIntegral $ Read.unSlotNo slotNo
pendingOldStyle = pending >>= mkLocalTxSubmission
-- Re-submit transactions due, ignore errors
forM_ (filter (isScheduled sp sl) pendingOldStyle) $ \st -> do
Expand Down Expand Up @@ -3856,7 +3859,7 @@ data WalletFollowLog
data WalletLog
= MsgMigrationUTxOBefore UTxOStatistics
| MsgMigrationUTxOAfter UTxOStatistics
| MsgRewardBalanceQuery BlockHeader
| MsgRewardBalanceQuery Read.ChainTip
| MsgRewardBalanceResult (Either ErrFetchRewards Coin)
| MsgRewardBalanceExited
| MsgTxSubmit TxSubmitLog
Expand Down Expand Up @@ -3906,8 +3909,9 @@ instance ToText WalletLog where
"About to migrate the following distribution: \n" <> pretty summary
MsgMigrationUTxOAfter summary ->
"Expected distribution after complete migration: \n" <> pretty summary
MsgRewardBalanceQuery bh ->
"Updating the reward balance for block " <> pretty bh
MsgRewardBalanceQuery nodeTip ->
"Updating the reward balance for block "
<> Read.prettyChainTip nodeTip
MsgRewardBalanceResult (Right amt) ->
"The reward balance is " <> pretty amt
MsgRewardBalanceResult (Left err) ->
Expand Down

0 comments on commit 85e1eb1

Please sign in to comment.