Skip to content

Commit

Permalink
Include TxHistory roll backward in deposit wallet state roll back
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Nov 1, 2024
1 parent e1450b6 commit 77143ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
37 changes: 24 additions & 13 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import Cardano.Wallet.Deposit.Pure
import Cardano.Wallet.Deposit.Pure.API.TxHistory
( ByCustomer
, ByTime
, DownTime
, getEraSlotOfBlock
)
import Cardano.Wallet.Deposit.Read
Expand All @@ -70,7 +71,8 @@ import Cardano.Wallet.Network.Checkpoints.Policy
( defaultPolicy
)
import Cardano.Wallet.Read
( applyEraFun
( Slot
, applyEraFun
)
import Control.Tracer
( Tracer
Expand Down Expand Up @@ -288,29 +290,38 @@ rollForward
-> tip
-> IO ()
rollForward w blocks _nodeTip = do
let blockSlots =
Set.fromList
$ applyEraFun getEraSlotOfBlock
let blockSlots = applyEraFun getEraSlotOfBlock
<$> toList blocks
resolveSlot <-
fmap (flip Map.lookup)
$ flip slotsToUTCTimes blockSlots
$ networkEnv
$ bootEnv
$ env w
resolveSlot <- slotResolver w blockSlots
onWalletState w
$ Delta.update
$ Delta.Replace
. Wallet.rollForwardMany
(fmap Down <$> resolveSlot)
resolveSlot
blocks

rollBackward
:: WalletInstance -> Read.ChainPoint -> IO Read.ChainPoint
rollBackward w point =
rollBackward w point = do
resolveSlot <- slotResolver w [Read.slotFromChainPoint point]
onWalletState w
$ Delta.updateWithResult
$ first Delta.Replace . Wallet.rollBackward point
$ first Delta.Replace . Wallet.rollBackward resolveSlot point

-- | Compute a slot resolver for the given slots.
slotResolver
:: WalletInstance
-> [Slot]
-> IO (Slot -> Maybe DownTime)
slotResolver w slots = do
let slotSet = Set.fromList slots
resolveSlot <-
fmap (flip Map.lookup)
$ flip slotsToUTCTimes slotSet
$ networkEnv
$ bootEnv
$ env w
pure $ fmap Down . resolveSlot

{-----------------------------------------------------------------------------
Operations
Expand Down
15 changes: 11 additions & 4 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ rollForwardOne resolveSlot eblock@(Read.EraValue block) w =
, submissions = Delta.apply (Sbm.rollForward block) (submissions w)
, txHistory =
TxHistory.rollForward
( valueTransferFromResolvedTx . resolveInputs
(getUTxO $ utxoHistory w)
( valueTransferFromResolvedTx
. resolveInputs
(getUTxO $ utxoHistory w)
)
(`addressToCustomer` w)
resolveSlot
Expand All @@ -281,16 +282,22 @@ rollForwardUTxO isOurs block u =
slot = Read.getEraSlotNo $ Read.getEraBHeader block

rollBackward
:: Read.ChainPoint
:: ResolveSlot
-> Read.ChainPoint
-> WalletState
-> (WalletState, Read.ChainPoint)
rollBackward targetPoint w =
rollBackward resolveSlot targetPoint w =
( w
{ walletTip = actualPoint
, utxoHistory =
UTxOHistory.rollBackward actualSlot (utxoHistory w)
, submissions =
Delta.apply (Sbm.rollBackward actualSlot) (submissions w)
, txHistory =
TxHistory.rollBackward
resolveSlot
(Read.slotFromChainPoint targetPoint)
(txHistory w)
}
, actualPoint
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,24 @@ prop_availableBalance_rollForward_twice =
tx1 = payFromFaucet [(addr1, Write.mkAda 1)]
block1 = Read.mockNextBlock Read.GenesisPoint [tx1]
chainPoint1 = Read.getChainPoint block1
w1 = Wallet.rollForwardOne resolveSlot (Read.EraValue block1) w0
w1 = Wallet.rollForwardOne resolveSlot (Read.EraValue block1) w0

tx2 = payFromFaucet [(addr2, Write.mkAda 2)]
block2 = Read.mockNextBlock chainPoint1 [tx2]
w2 = Wallet.rollForwardOne resolveSlot (Read.EraValue block2) w1

prop_availableBalance_rollForward_rollBackward :: Property
prop_availableBalance_rollForward_rollBackward =
Wallet.availableBalance (fst $ Wallet.rollBackward chainPoint0 w3)
Wallet.availableBalance
(fst $ Wallet.rollBackward resolveSlot chainPoint0 w3)
=== Wallet.availableBalance w0
.&&.
Wallet.availableBalance (fst $ Wallet.rollBackward chainPoint1 w3)
Wallet.availableBalance
(fst $ Wallet.rollBackward resolveSlot chainPoint1 w3)
=== Wallet.availableBalance w1
.&&.
Wallet.availableBalance (fst $ Wallet.rollBackward chainPoint2 w3)
Wallet.availableBalance
(fst $ Wallet.rollBackward resolveSlot chainPoint2 w3)
=== Wallet.availableBalance w2
.&&.
Wallet.availableBalance w3
Expand Down

0 comments on commit 77143ef

Please sign in to comment.