Skip to content

Commit

Permalink
Make applyBlocks return a list of incrementally-updated wallet states.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Aug 30, 2019
1 parent e6fe7ba commit 3772c4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ import qualified Cardano.Wallet.DB as DB
import qualified Cardano.Wallet.Primitive.CoinSelection.Random as CoinSelection
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Data.List as L
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.Text as T
Expand Down Expand Up @@ -689,7 +690,7 @@ newWalletLayer tracer bp db nw tl = do
let (h,q) = first (filter nonEmpty) $
splitAt (length blocks - 1) blocks
liftIO $ logDebug t $ pretty (h ++ q)
let (txs, cp') = applyBlocks @s @t (h ++ q) cp
let (txs, cp') = NonEmpty.last $ applyBlocks @s @t (h ++ q) cp
let progress = slotRatio epochLength sup tip
let status' = if progress == maxBound
then Ready
Expand Down
37 changes: 29 additions & 8 deletions lib/core/src/Cardano/Wallet/Primitive/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.Generics.Labels
()
import Data.List
( foldl' )
import Data.List.NonEmpty
( NonEmpty )
import Data.Map.Strict
( Map )
import Data.Maybe
Expand All @@ -97,6 +97,7 @@ import Data.Set
import Numeric.Natural
( Natural )

import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map as Map
import qualified Data.Set as Set

Expand Down Expand Up @@ -207,18 +208,38 @@ applyBlock !b (Wallet !u !pending _ s bh) =
where
pendingExcluding_ = pendingExcluding (Proxy @t)

-- | Helper to apply multiple blocks in sequence to an existing wallet. It's
-- basically just a @foldl' applyBlock@ over the given blocks.
-- | Apply multiple blocks in sequence to an existing wallet.
--
-- Returns a non-empty list of intermediate wallet states where:
--
-- * the /initial element/ is the original wallet state;
--
-- * each /successive element/ is the result of applying the next available
-- unprocessed block from the original list to the previous element;
--
-- * the /final element/ is the state obtained by applying all blocks from
-- the specified list, in order, to the original wallet state.
--
-- For an original wallet state __@w@__ and a list of blocks __@b@__ such that:
--
-- > b = [b1, b2, ..., bn]
--
-- Returns the following list of updates:
--
-- > w :| [w + b1, w + b1 + b2, ..., w + b1 + b2 + ... + bn)]
--
-- Where __@w + bi@__ is equivalent to 'applyBlock' __@bi w@__.
applyBlocks
:: forall s t. (DefineTx t)
=> [Block (Tx t)]
-> Wallet s t
-> (Map (Hash "Tx") (Tx t, TxMeta), Wallet s t)
-> NonEmpty (Map (Hash "Tx") (Tx t, TxMeta), Wallet s t)
applyBlocks blocks cp0 =
foldl' applyBlock' (mempty, cp0) blocks
NonEmpty.scanl applyBlock' (mempty, cp0) blocks
where
applyBlock' (txs, cp) b =
let (txs', cp') = applyBlock b cp in (txs <> txs', cp')
applyBlock' (txs, cp) b = (txs <> txs', cp')
where
(txs', cp') = applyBlock b cp

newPending
:: (Tx t, TxMeta)
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/Primitive/ModelSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ prop_applyBlockTxHistoryIncoming s =
property (outs (filter isIncoming txs) `overlaps` ourAddresses s')
where
cp0 = initWallet @_ @DummyTarget block0 s
(txs, s') = bimap Map.elems getState $ applyBlocks blockchain cp0
(txs, s') = bimap Map.elems getState $ NE.last $ applyBlocks blockchain cp0
isIncoming (_, m) = direction m == Incoming
outs = Set.fromList . concatMap (map address . outputs . fst)
overlaps a b
Expand Down

0 comments on commit 3772c4d

Please sign in to comment.