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 0026107 commit 9942c25
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
32 changes: 21 additions & 11 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import Cardano.Wallet.Deposit.Pure
import Cardano.Wallet.Deposit.Pure.API.TxHistory
( ByCustomer
, ByTime
, LookupTimeFromSlot
)
import Cardano.Wallet.Deposit.Read
( Address
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 @@ -285,14 +287,9 @@ rollForward
-> IO ()
rollForward w blocks _nodeTip = do
let blockSlots =
Set.fromList
$ applyEraFun getEraSlotOfBlock
<$> toList blocks
timeFromSlot <-
flip slotsToUTCTimes blockSlots
$ networkEnv
$ bootEnv
$ env w
applyEraFun getEraSlotOfBlock
<$> toList blocks
timeFromSlot <- slotResolver w blockSlots
onWalletState w
$ Delta.update
$ Delta.Replace
Expand All @@ -302,10 +299,23 @@ rollForward w blocks _nodeTip = do

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

-- | Compute a slot resolver for the given slots.
slotResolver
:: WalletInstance
-> [Slot]
-> IO LookupTimeFromSlot
slotResolver w slots = do
let slotSet = Set.fromList slots
flip slotsToUTCTimes slotSet
$ networkEnv
$ bootEnv
$ env w

{-----------------------------------------------------------------------------
Operations
Expand Down
10 changes: 8 additions & 2 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,22 @@ rollForwardUTxO isOurs block u =
slot = Read.getEraSlotNo $ Read.getEraBHeader block

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

tx2 = payFromFaucet [(addr2, Write.mkAda 2)]
block2 = Read.mockNextBlock chainPoint1 [tx2]
w2 = Wallet.rollForwardOne timeFromSlot (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 timeFromSlot chainPoint0 w3)
=== Wallet.availableBalance w0
.&&.
Wallet.availableBalance (fst $ Wallet.rollBackward chainPoint1 w3)
Wallet.availableBalance
(fst $ Wallet.rollBackward timeFromSlot chainPoint1 w3)
=== Wallet.availableBalance w1
.&&.
Wallet.availableBalance (fst $ Wallet.rollBackward chainPoint2 w3)
Wallet.availableBalance
(fst $ Wallet.rollBackward timeFromSlot chainPoint2 w3)
=== Wallet.availableBalance w2
.&&.
Wallet.availableBalance w3
Expand Down

0 comments on commit 9942c25

Please sign in to comment.