From 6be3630fbc590c1f5bfcb78a31617b75ebabeb2c Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 16 Oct 2024 08:48:22 -0500 Subject: [PATCH] fix: chain re-org detection --- src/Zenith/DB.hs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Zenith/DB.hs b/src/Zenith/DB.hs index b84f344..1a24fd4 100644 --- a/src/Zenith/DB.hs +++ b/src/Zenith/DB.hs @@ -2030,6 +2030,21 @@ getUnconfPoolBalance pool za = do let oBal = sum oAmts return $ AccountBalance tBal sBal oBal +rewindWalletTransactions :: ConnectionPool -> Int -> IO () +rewindWalletTransactions pool b = do + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + delete $ do + _ <- from $ table @UserTx + return () + delete $ do + txs <- from $ table @WalletTransaction + where_ $ txs ^. WalletTransactionBlock >. val b + return () + update $ \w -> do + set w [ZcashWalletLastSync =. val b] + clearWalletTransactions :: ConnectionPool -> IO () clearWalletTransactions pool = do runNoLoggingT $ @@ -2578,10 +2593,10 @@ completeSync pool st = do -- | Rewind the data store to a given block height rewindWalletData :: ConnectionPool -> Int -> IO () rewindWalletData pool b = do + rewindWalletTransactions pool b runNoLoggingT $ PS.retryOnBusy $ flip PS.runSqlPool pool $ delete $ do blk <- from $ table @ZcashBlock where_ $ blk ^. ZcashBlockHeight >=. val b - clearWalletTransactions pool