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

Commit 09de855

Browse files
authored
Merge pull request #3737 from input-output-hk/optimise/cbr-379-prefiltering
[CBR-379] When prefiltering a block for multiple wallets, traverse the block on…
2 parents 8bf4c8c + 3c911b2 commit 09de855

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/Cardano/Wallet/Kernel/BListener.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ prefilterBlock' pw b = do
7070
aux :: [(WalletId, EncryptedSecretKey)]
7171
-> ((BlockContext, Map HdAccountId PrefilteredBlock), [TxMeta])
7272
aux ws =
73-
let (conMap, conMeta) = mconcat $ map (uncurry (prefilterBlock b)) ws
73+
let (conMap, conMeta) = prefilterBlock b ws
7474
in ((b ^. rbContext, conMap), conMeta)
7575

7676
data BackfillFailed

src/Cardano/Wallet/Kernel/PrefilterTx.hs

+24-10
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,23 @@ prefilterTx wKey tx = ((prefInps',prefOuts'),metas)
159159
(nothingToZero acc prefOutCoins)
160160
(onlyOurInps && onlyOurOuts) acc) allAccounts
161161

162+
-- | Prefilter the transaction with each wallet key respectively and
163+
-- combine the results.
164+
--
165+
-- NOTE: we can rely on a Monoidal fold here to combine the maps
166+
-- 'Map HdAccountId a' since the accounts will be unique accross wallet keys.
167+
prefilterTxForWallets :: [WalletKey]
168+
-> ResolvedTx
169+
-> ((Map HdAccountId (Set TxIn)
170+
, Map HdAccountId UtxoSummaryRaw)
171+
, [TxMeta])
172+
prefilterTxForWallets wKeys tx =
173+
mconcat $ map ((flip prefilterTx) tx) wKeys
174+
162175
-- | Prefilter inputs of a transaction
163176
prefilterInputs :: WalletKey
164-
-> [(TxIn, ResolvedInput)]
165-
-> (Bool, Map HdAccountId (Set (TxIn,Coin)))
177+
-> [(TxIn, ResolvedInput)]
178+
-> (Bool, Map HdAccountId (Set (TxIn,Coin)))
166179
prefilterInputs wKey inps
167180
= prefilterResolvedTxPairs wKey mergeF inps
168181
where
@@ -265,26 +278,24 @@ extendWithSummary (onlyOurInps,onlyOurOuts) utxoWithAddrId
265278
and Transaction metadata.
266279
-------------------------------------------------------------------------------}
267280

268-
-- | Prefilter the transactions of a resolved block for the given wallet.
281+
-- | Prefilter the transactions of a resolved block for the given wallets.
269282
--
270283
-- Returns prefiltered blocks indexed by HdAccountId.
271284
prefilterBlock :: ResolvedBlock
272-
-> WalletId
273-
-> EncryptedSecretKey
285+
-> [(WalletId, EncryptedSecretKey)]
274286
-> (Map HdAccountId PrefilteredBlock, [TxMeta])
275-
prefilterBlock block wid esk =
287+
prefilterBlock block rawKeys =
276288
(Map.fromList
277289
$ map (mkPrefBlock (block ^. rbContext) inpAll outAll)
278290
$ Set.toList accountIds
279291
, metas)
280292
where
281-
wdc :: WalletDecrCredentials
282-
wdc = keyToWalletDecrCredentials $ KeyForRegular esk
283-
wKey = (wid, wdc)
293+
wKeys :: [WalletKey]
294+
wKeys = map toWalletKey rawKeys
284295

285296
inps :: [Map HdAccountId (Set TxIn)]
286297
outs :: [Map HdAccountId UtxoSummaryRaw]
287-
(ios, conMetas) = unzip $ map (prefilterTx wKey) (block ^. rbTxs)
298+
(ios, conMetas) = unzip $ map (prefilterTxForWallets wKeys) (block ^. rbTxs)
288299
(inps, outs) = unzip ios
289300
metas = concat conMetas
290301

@@ -295,6 +306,9 @@ prefilterBlock block wid esk =
295306

296307
accountIds = Map.keysSet inpAll `Set.union` Map.keysSet outAll
297308

309+
toWalletKey :: (WalletId, EncryptedSecretKey) -> WalletKey
310+
toWalletKey (wid, esk) = (wid, keyToWalletDecrCredentials $ KeyForRegular esk)
311+
298312
mkPrefBlock :: BlockContext
299313
-> Map HdAccountId (Set TxIn)
300314
-> Map HdAccountId (Map TxIn (TxOutAux, AddressSummary))

src/Cardano/Wallet/Kernel/Restore.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ mkPrefilter :: Kernel.PassiveWallet
163163
-> IO (Map HD.HdAccountId PrefilteredBlock, [TxMeta])
164164
mkPrefilter pw wId esk blund = blundToResolvedBlock (pw ^. walletNode) blund <&> \case
165165
Nothing -> (M.empty, [])
166-
Just rb -> prefilterBlock rb wId esk
166+
Just rb -> prefilterBlock rb [(wId,esk)]
167167

168168
-- | Begin a restoration for a wallet that is already known. This is used
169169
-- to put an existing wallet back into a restoration state when something has

src/Cardano/Wallet/WalletLayer/Kernel/Wallets.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ prefilter :: EncryptedSecretKey
161161
prefilter esk wallet wId blund =
162162
blundToResolvedBlock (wallet ^. Kernel.walletNode) blund <&> \case
163163
Nothing -> (Map.empty, [])
164-
Just rb -> prefilterBlock rb wId esk
164+
Just rb -> prefilterBlock rb [(wId,esk)]
165165

166166
-- | Updates the 'SpendingPassword' for this wallet.
167167
updateWallet :: MonadIO m

0 commit comments

Comments
 (0)