Skip to content

Commit 3eea066

Browse files
committed
Include TxHistory roll forward in deposit wallet state roll forward
1 parent e9ad52d commit 3eea066

File tree

2 files changed

+64
-11
lines changed
  • lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit

2 files changed

+64
-11
lines changed

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ import Cardano.Crypto.Wallet
4646
import Cardano.Wallet.Address.BIP32
4747
( BIP32Path
4848
)
49+
import Cardano.Wallet.Deposit.IO.Network.Type
50+
( NetworkEnv (slotsToUTCTimes)
51+
)
4952
import Cardano.Wallet.Deposit.Pure
5053
( Customer
5154
, ValueTransfer
@@ -56,6 +59,7 @@ import Cardano.Wallet.Deposit.Pure
5659
import Cardano.Wallet.Deposit.Pure.API.TxHistory
5760
( ByCustomer
5861
, ByTime
62+
, getEraSlotOfBlock
5963
)
6064
import Cardano.Wallet.Deposit.Read
6165
( Address
@@ -65,19 +69,28 @@ import Cardano.Wallet.Deposit.Read
6569
import Cardano.Wallet.Network.Checkpoints.Policy
6670
( defaultPolicy
6771
)
72+
import Cardano.Wallet.Read
73+
( applyEraFun
74+
)
6875
import Control.Tracer
6976
( Tracer
7077
, contramap
7178
)
7279
import Data.Bifunctor
7380
( first
7481
)
82+
import Data.Foldable
83+
( Foldable (..)
84+
)
7585
import Data.List.NonEmpty
7686
( NonEmpty
7787
)
7888
import Data.Map.Strict
7989
( Map
8090
)
91+
import Data.Ord
92+
( Down (..)
93+
)
8194
import Data.Time
8295
( UTCTime
8396
)
@@ -92,6 +105,8 @@ import qualified Data.Delta as Delta
92105
( Replace (..)
93106
)
94107
import qualified Data.Delta.Update as Delta
108+
import qualified Data.Map.Strict as Map
109+
import qualified Data.Set as Set
95110
import qualified Data.Store as Store
96111

97112
{-----------------------------------------------------------------------------
@@ -113,8 +128,7 @@ data WalletBootEnv m = WalletBootEnv
113128
type WalletStore = Store.UpdateStore IO Wallet.DeltaWalletState
114129

115130
-- | The full environment needed to run a wallet.
116-
data WalletEnv m
117-
= WalletEnv
131+
data WalletEnv m = WalletEnv
118132
{ bootEnv :: WalletBootEnv m
119133
-- ^ The boot environment.
120134
, store :: WalletStore
@@ -269,11 +283,27 @@ getAllDeposits w i =
269283
Wallet.getAllDeposits i <$> readWalletState w
270284

271285
rollForward
272-
:: WalletInstance -> NonEmpty (Read.EraValue Read.Block) -> tip -> IO ()
273-
rollForward w blocks _nodeTip =
286+
:: WalletInstance
287+
-> NonEmpty (Read.EraValue Read.Block)
288+
-> tip
289+
-> IO ()
290+
rollForward w blocks _nodeTip = do
291+
let blockSlots =
292+
Set.fromList
293+
$ applyEraFun getEraSlotOfBlock
294+
<$> toList blocks
295+
resolveSlot <-
296+
fmap (flip Map.lookup)
297+
$ flip slotsToUTCTimes blockSlots
298+
$ networkEnv
299+
$ bootEnv
300+
$ env w
274301
onWalletState w
275302
$ Delta.update
276-
$ Delta.Replace . Wallet.rollForwardMany blocks
303+
$ Delta.Replace
304+
. Wallet.rollForwardMany
305+
(fmap Down <$> resolveSlot)
306+
blocks
277307

278308
rollBackward
279309
:: WalletInstance -> Read.ChainPoint -> IO Read.ChainPoint

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ import Cardano.Wallet.Deposit.Pure.API.TxHistory
8080
( ByCustomer
8181
, ByTime
8282
, DownTime
83+
, ResolveSlot
8384
, TxHistory (..)
8485
)
86+
import Cardano.Wallet.Deposit.Pure.UTxO.Tx
87+
( resolveInputs
88+
, valueTransferFromResolvedTx
89+
)
8590
import Cardano.Wallet.Deposit.Pure.UTxO.UTxOHistory
8691
( UTxOHistory
92+
, getUTxO
8793
)
8894
import Cardano.Wallet.Deposit.Pure.UTxO.ValueTransfer
8995
( ValueTransfer (..)
@@ -120,6 +126,7 @@ import Data.Word.Odd
120126
)
121127

122128
import qualified Cardano.Wallet.Deposit.Pure.Address as Address
129+
import qualified Cardano.Wallet.Deposit.Pure.API.TxHistory as TxHistory
123130
import qualified Cardano.Wallet.Deposit.Pure.Balance as Balance
124131
import qualified Cardano.Wallet.Deposit.Pure.RollbackWindow as Rollback
125132
import qualified Cardano.Wallet.Deposit.Pure.Submissions as Sbm
@@ -230,16 +237,32 @@ getWalletTip :: WalletState -> Read.ChainPoint
230237
getWalletTip = walletTip
231238

232239
rollForwardMany
233-
:: NonEmpty (Read.EraValue Read.Block) -> WalletState -> WalletState
234-
rollForwardMany blocks w = foldl' (flip rollForwardOne) w blocks
240+
:: ResolveSlot
241+
-> NonEmpty (Read.EraValue Read.Block)
242+
-> WalletState
243+
-> WalletState
244+
rollForwardMany resolveSlot blocks w =
245+
foldl' (flip $ rollForwardOne resolveSlot) w blocks
235246

236247
rollForwardOne
237-
:: Read.EraValue Read.Block -> WalletState -> WalletState
238-
rollForwardOne (Read.EraValue block) w =
248+
:: ResolveSlot
249+
-> Read.EraValue Read.Block
250+
-> WalletState
251+
-> WalletState
252+
rollForwardOne resolveSlot eblock@(Read.EraValue block) w =
239253
w
240254
{ walletTip = Read.getChainPoint block
241255
, utxoHistory = rollForwardUTxO isOurs block (utxoHistory w)
242256
, submissions = Delta.apply (Sbm.rollForward block) (submissions w)
257+
, txHistory =
258+
TxHistory.rollForward
259+
( valueTransferFromResolvedTx . resolveInputs
260+
(getUTxO $ utxoHistory w)
261+
)
262+
(`addressToCustomer` w)
263+
resolveSlot
264+
eblock
265+
(txHistory w)
243266
}
244267
where
245268
isOurs :: Address -> Bool
@@ -282,8 +305,8 @@ rollBackward targetPoint w =
282305
-- any other point than the target point (or genesis).
283306
actualPoint =
284307
if (targetSlot `Rollback.member` UTxOHistory.getRollbackWindow h)
285-
-- FIXME: Add test for rollback window of `submissions`
286-
then targetPoint
308+
then -- FIXME: Add test for rollback window of `submissions`
309+
targetPoint
287310
else Read.GenesisPoint
288311

289312
availableBalance :: WalletState -> Read.Value

0 commit comments

Comments
 (0)