From c68c504b53d5b966efaa755a4dbafa0415f94bff Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Sat, 10 Aug 2024 07:52:45 -0500 Subject: [PATCH] Refactor for new schema for `ZcashTransaction` --- src/Zenith/CLI.hs | 6 +++--- src/Zenith/Core.hs | 11 ++++++----- src/Zenith/DB.hs | 25 ++++++++++++++++++------- src/Zenith/GUI.hs | 2 +- src/Zenith/Scanner.hs | 2 +- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/Zenith/CLI.hs b/src/Zenith/CLI.hs index 51389bd..33626f5 100644 --- a/src/Zenith/CLI.hs +++ b/src/Zenith/CLI.hs @@ -717,10 +717,10 @@ abMBarAttr = A.attrName "menubar" scanZebra :: T.Text -> T.Text -> Int -> Int -> BC.BChan Tick -> ZcashNet -> IO () -scanZebra dbP zHost zPort b eChan net = do +scanZebra dbP zHost zPort b eChan znet = do bStatus <- liftIO $ checkBlockChain zHost zPort pool <- runNoLoggingT $ initPool dbP - dbBlock <- runNoLoggingT $ getMaxBlock pool + dbBlock <- getMaxBlock pool $ ZcashNetDB znet confUp <- try $ updateConfs zHost zPort pool :: IO (Either IOError ()) case confUp of Left _e0 -> @@ -765,7 +765,7 @@ scanZebra dbP zHost zPort b eChan net = do Left e2 -> liftIO $ BC.writeBChan eChan $ TickMsg e2 Right hb -> do let blockTime = getBlockTime hb - mapM_ (processTx zHost zPort blockTime pool (ZcashNetDB net)) $ + mapM_ (processTx zHost zPort blockTime pool (ZcashNetDB znet)) $ bl_txs $ addTime blk blockTime liftIO $ BC.writeBChan eChan $ TickVal step addTime :: BlockResponse -> Int -> BlockResponse diff --git a/src/Zenith/Core.hs b/src/Zenith/Core.hs index 61a74d4..38d396d 100644 --- a/src/Zenith/Core.hs +++ b/src/Zenith/Core.hs @@ -237,7 +237,7 @@ findSaplingOutputs config b znet za = do let zebraPort = c_zebraPort config let zn = getNet znet pool <- runNoLoggingT $ initPool dbPath - tList <- getShieldedOutputs pool b + tList <- getShieldedOutputs pool b znet trees <- getCommitmentTrees zebraHost zebraPort (b - 1) let sT = SaplingCommitmentTree $ ztiSapling trees decryptNotes sT zn pool tList @@ -328,7 +328,7 @@ findOrchardActions config b znet za = do let zebraPort = c_zebraPort config let zn = getNet znet pool <- runNoLoggingT $ initPool dbPath - tList <- getOrchardActions pool b + tList <- getOrchardActions pool b znet trees <- getCommitmentTrees zebraHost zebraPort (b - 1) let sT = OrchardCommitmentTree $ ztiOrchard trees decryptNotes sT zn pool tList @@ -700,19 +700,20 @@ syncWallet :: syncWallet config w = do startTime <- liftIO getCurrentTime let walletDb = c_dbPath config + let znet = zcashWalletNetwork $ entityVal w pool <- runNoLoggingT $ initPool walletDb accs <- runNoLoggingT $ getAccounts pool $ entityKey w addrs <- concat <$> mapM (runNoLoggingT . getAddresses pool . entityKey) accs intAddrs <- concat <$> mapM (runNoLoggingT . getInternalAddresses pool . entityKey) accs - chainTip <- runNoLoggingT $ getMaxBlock pool + chainTip <- getMaxBlock pool znet let lastBlock = zcashWalletLastSync $ entityVal w let startBlock = if lastBlock > 0 then lastBlock else zcashWalletBirthdayHeight $ entityVal w - mapM_ (liftIO . findTransparentNotes pool startBlock) addrs - mapM_ (liftIO . findTransparentNotes pool startBlock) intAddrs + mapM_ (liftIO . findTransparentNotes pool startBlock znet) addrs + mapM_ (liftIO . findTransparentNotes pool startBlock znet) intAddrs mapM_ (liftIO . findTransparentSpends pool . entityKey) accs sapNotes <- liftIO $ diff --git a/src/Zenith/DB.hs b/src/Zenith/DB.hs index 3446b9f..3a8f70c 100644 --- a/src/Zenith/DB.hs +++ b/src/Zenith/DB.hs @@ -450,14 +450,17 @@ saveAccount pool a = -- | Returns the largest block in storage getMaxBlock :: Pool SqlBackend -- ^ The database pool - -> NoLoggingT IO Int -getMaxBlock pool = do + -> ZcashNetDB + -> IO Int +getMaxBlock pool net = do b <- + runNoLoggingT $ PS.retryOnBusy $ flip PS.runSqlPool pool $ do selectOne $ do txs <- from $ table @ZcashTransaction where_ (txs ^. ZcashTransactionBlock >. val 0) + where_ (txs ^. ZcashTransactionNetwork ==. val net) orderBy [desc $ txs ^. ZcashTransactionBlock] pure txs case b of @@ -645,14 +648,16 @@ saveTransaction pool t n wt = getZcashTransactions :: ConnectionPool -- ^ The database path -> Int -- ^ Block + -> ZcashNet -- ^ Network -> IO [Entity ZcashTransaction] -getZcashTransactions pool b = +getZcashTransactions pool b net = runNoLoggingT $ PS.retryOnBusy $ flip PS.runSqlPool pool $ do select $ do txs <- from $ table @ZcashTransaction - where_ $ txs ^. ZcashTransactionBlock >. val b + where_ (txs ^. ZcashTransactionBlock >. val b) + where_ (txs ^. ZcashTransactionNetwork ==. val (ZcashNetDB net)) orderBy [asc $ txs ^. ZcashTransactionBlock] return txs @@ -832,9 +837,10 @@ saveWalletOrchNote pool wId pos wit ch za zt dn = do findTransparentNotes :: ConnectionPool -- ^ The database path -> Int -- ^ Starting block + -> ZcashNetDB -- ^ Network to use -> Entity WalletAddress -> IO () -findTransparentNotes pool b t = do +findTransparentNotes pool b net t = do let tReceiver = t_rec =<< readUnifiedAddressDB (entityVal t) case tReceiver of Just tR -> do @@ -854,6 +860,7 @@ findTransparentNotes pool b t = do (\(txs :& tNotes) -> txs ^. ZcashTransactionId ==. tNotes ^. TransparentNoteTx) where_ (txs ^. ZcashTransactionBlock >. val b) + where_ (txs ^. ZcashTransactionNetwork ==. val net) where_ (tNotes ^. TransparentNoteScript ==. val s) pure (txs, tNotes) mapM_ @@ -907,8 +914,9 @@ saveSapNote pool wsn = getShieldedOutputs :: ConnectionPool -- ^ database path -> Int -- ^ block + -> ZcashNetDB -- ^ network to use -> IO [(Entity ZcashTransaction, Entity ShieldOutput)] -getShieldedOutputs pool b = +getShieldedOutputs pool b net = runNoLoggingT $ PS.retryOnBusy $ flip PS.runSqlPool pool $ do @@ -918,6 +926,7 @@ getShieldedOutputs pool b = (\(txs :& sOutputs) -> txs ^. ZcashTransactionId ==. sOutputs ^. ShieldOutputTx) where_ (txs ^. ZcashTransactionBlock >=. val b) + where_ (txs ^. ZcashTransactionNetwork ==. val net) orderBy [ asc $ txs ^. ZcashTransactionId , asc $ sOutputs ^. ShieldOutputPosition @@ -928,8 +937,9 @@ getShieldedOutputs pool b = getOrchardActions :: ConnectionPool -- ^ database path -> Int -- ^ block + -> ZcashNetDB -- ^ network to use -> IO [(Entity ZcashTransaction, Entity OrchAction)] -getOrchardActions pool b = +getOrchardActions pool b net = runNoLoggingT $ PS.retryOnBusy $ flip PS.runSqlPool pool $ do @@ -939,6 +949,7 @@ getOrchardActions pool b = (\(txs :& oActions) -> txs ^. ZcashTransactionId ==. oActions ^. OrchActionTx) where_ (txs ^. ZcashTransactionBlock >=. val b) + where_ (txs ^. ZcashTransactionNetwork ==. val net) orderBy [asc $ txs ^. ZcashTransactionId, asc $ oActions ^. OrchActionPosition] pure (txs, oActions) diff --git a/src/Zenith/GUI.hs b/src/Zenith/GUI.hs index aac7955..0c386e8 100644 --- a/src/Zenith/GUI.hs +++ b/src/Zenith/GUI.hs @@ -1151,7 +1151,7 @@ scanZebra dbPath zHost zPort net sendMsg = do bStatus <- liftIO $ checkBlockChain zHost zPort pool <- runNoLoggingT $ initPool dbPath b <- liftIO $ getMinBirthdayHeight pool - dbBlock <- runNoLoggingT $ getMaxBlock pool + dbBlock <- getMaxBlock pool $ ZcashNetDB net let sb = max dbBlock b confUp <- try $ updateConfs zHost zPort pool :: IO (Either IOError ()) case confUp of diff --git a/src/Zenith/Scanner.hs b/src/Zenith/Scanner.hs index 0149890..61b890e 100644 --- a/src/Zenith/Scanner.hs +++ b/src/Zenith/Scanner.hs @@ -61,7 +61,7 @@ rescanZebra host port dbFilePath = do pool3 <- runNoLoggingT $ initPool dbFilePath clearWalletTransactions pool1 clearWalletData pool1 - dbBlock <- runNoLoggingT $ getMaxBlock pool1 + dbBlock <- getMaxBlock pool1 znet b <- liftIO $ getMinBirthdayHeight pool1 let sb = max dbBlock b if sb > zgb_blocks bStatus || sb < 1