Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 51e6c62

Browse files
committed
Re-arrange the restoration task so most of the action happens inside whenRight.
1 parent 773ebbf commit 51e6c62

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

wallet-new/src/Cardano/Wallet/Kernel/Restore.hs

+17-15
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ import Cardano.Wallet.Kernel.NodeStateAdaptor (LockContext (..),
3737
import Cardano.Wallet.Kernel.PrefilterTx (PrefilteredBlock, WalletKey,
3838
toHdAddressId)
3939
import Cardano.Wallet.Kernel.Types (WalletId)
40+
import Cardano.Wallet.Kernel.Util.Core (utxoBalance)
4041

4142
import Pos.Chain.Block (BlockHeader, Blund, HeaderHash, blockHeader,
4243
headerHash, mainBlockSlot)
43-
import Pos.Chain.Txp (Utxo, getTotalCoinsInUtxo)
44-
import Pos.Core (Address, Coin, SlotId, flattenSlotIdExplicit)
44+
import Pos.Chain.Txp (Utxo)
45+
import Pos.Core (Address, Coin, SlotId, flattenSlotIdExplicit, mkCoin)
4546
import Pos.Core.Txp (toaOut, txOutAddress)
4647
import Pos.DB (MonadDBRead)
4748
import Pos.DB.Block (getBlund, getFirstGenesisBlockHash,
@@ -94,7 +95,7 @@ restoreWallet wallet hdRoot (wId, wdc) prefilter = do
9495

9596
-- Return the wallet's current balance.
9697
let myUtxo = foldr (\(u,_,_) -> M.union u) M.empty mine
97-
return (getTotalCoinsInUtxo myUtxo)
98+
return (mkCoin . fromIntegral . utxoBalance $ myUtxo)
9899

99100
where
100101
txoAddr = txOutAddress . toaOut . snd
@@ -140,18 +141,19 @@ restoreWalletHistoryAsync wallet wId target tgtSlot prefilter = do
140141
-- MN TEMPORARY: slow your roll!
141142
threadDelay 5000000 -- 5 sec
142143

143-
-- Gather the information we will need to decide if we are within K blocks of the tip.
144-
slotCount <- getSlotCount (wallet ^. walletNode)
145-
let flat = flattenSlotIdExplicit slotCount
144+
-- Update each account's historical checkpoints
145+
whenRight block $ \mb -> do
146146

147-
k <- getSecurityParameter (wallet ^. walletNode)
147+
-- Gather the information we will need to decide if we are within K blocks of the tip.
148+
slotCount <- getSlotCount (wallet ^. walletNode)
149+
let flat = flattenSlotIdExplicit slotCount
148150

149-
-- Filter the blocks by account
150-
(prefilteredBlocks, txMetas) <- prefilter blund
151-
say (" found " <> pretty (length prefilteredBlocks) <> " relevant accounts.")
151+
k <- getSecurityParameter (wallet ^. walletNode)
152+
153+
-- Filter the blocks by account
154+
(prefilteredBlocks, txMetas) <- prefilter blund
155+
say (" found " <> pretty (length prefilteredBlocks) <> " relevant accounts.")
152156

153-
-- Update each account's historical checkpoints
154-
whenRight block $ \mb -> do
155157
let slotId = mb ^. mainBlockSlot
156158
SecurityParameter k0 = k
157159
say (" about to apply block for " <> pretty (mb ^. mainBlockSlot)
@@ -166,9 +168,9 @@ restoreWalletHistoryAsync wallet wId target tgtSlot prefilter = do
166168
. (wriTargetSlot .~ flat tgtSlot)) wId
167169
say "update done."
168170

169-
-- Store the TxMetas
170-
forM_ txMetas (putTxMeta (wallet ^. walletMeta))
171-
when (length txMetas > 0) $ say (" txMetas: " <> pretty txMetas)
171+
-- Store the TxMetas
172+
forM_ txMetas (putTxMeta (wallet ^. walletMeta))
173+
when (length txMetas > 0) $ say (" txMetas: " <> pretty txMetas)
172174

173175
-- Get the next block from the node and recurse.
174176
nextBlock (block ^. blockHeader) >>= \case

0 commit comments

Comments
 (0)