2
2
3
3
module Cardano.DbSync.Ledger.Async where
4
4
5
+ import Cardano.DbSync.Types
6
+ import Data.Set (Set )
7
+ import Data.Map (Map )
5
8
import Cardano.DbSync.Ledger.Types
6
9
import Cardano.Ledger.BaseTypes (EpochNo )
7
10
import Cardano.Ledger.Crypto (StandardCrypto )
8
11
import qualified Cardano.Ledger.EpochBoundary as Ledger
9
12
import Control.Concurrent.Class.MonadSTM.Strict
10
13
import qualified Control.Concurrent.STM.TBQueue as TBQ
14
+ import qualified Cardano.Ledger.Rewards as Ledger
15
+
16
+ --------------------------------------------------------------------------------
17
+ -- EpochStake
18
+ --------------------------------------------------------------------------------
11
19
12
20
newEpochStakeChannels :: IO EpochStakeChannels
13
21
newEpochStakeChannels =
@@ -18,9 +26,9 @@ newEpochStakeChannels =
18
26
<*> newTVarIO Nothing
19
27
20
28
-- To be used by the main thread
21
- ensureEpochDone :: EpochStakeChannels -> EpochNo -> Ledger. SnapShot StandardCrypto -> IO ()
22
- ensureEpochDone sQueue epoch snapshot = atomically $ do
23
- mLastEpochDone <- waitFinished sQueue
29
+ ensureStakeDone :: EpochStakeChannels -> EpochNo -> Ledger. SnapShot StandardCrypto -> IO ()
30
+ ensureStakeDone sQueue epoch snapshot = atomically $ do
31
+ mLastEpochDone <- waitStakeFinished sQueue
24
32
case mLastEpochDone of
25
33
Just lastEpochDone | lastEpochDone == epoch -> pure ()
26
34
_ -> do
@@ -29,8 +37,8 @@ ensureEpochDone sQueue epoch snapshot = atomically $ do
29
37
retry
30
38
31
39
-- To be used by the main thread
32
- waitFinished :: EpochStakeChannels -> STM IO (Maybe EpochNo )
33
- waitFinished sQueue = do
40
+ waitStakeFinished :: EpochStakeChannels -> STM IO (Maybe EpochNo )
41
+ waitStakeFinished sQueue = do
34
42
stakeThreadState <- readTVar (epochResult sQueue)
35
43
case stakeThreadState of
36
44
Just (lastEpoch, Done ) -> pure $ Just lastEpoch -- Normal case
@@ -42,3 +50,42 @@ writeEpochStakeAction :: EpochStakeChannels -> EpochNo -> Ledger.SnapShot Standa
42
50
writeEpochStakeAction sQueue epoch snapShot checkFirst = do
43
51
TBQ. writeTBQueue (estakeQueue sQueue) $ EpochStakeDBAction epoch snapShot checkFirst
44
52
writeTVar (epochResult sQueue) $ Just (epoch, Running )
53
+
54
+
55
+ --------------------------------------------------------------------------------
56
+ -- Rewards
57
+ --------------------------------------------------------------------------------
58
+
59
+ newRewardsChannels :: IO RewardsChannels
60
+ newRewardsChannels =
61
+ -- This may never be more than 1. But let's keep it a queue for extensibility shake.
62
+ -- This may allow us to parallelize the events workload even further
63
+ RewardsChannels
64
+ <$> TBQ. newTBQueueIO 1
65
+ <*> newTVarIO Nothing
66
+
67
+ -- To be used by the main thread
68
+ ensureRewardsDone :: RewardsChannels -> EpochNo -> EpochNo -> Map StakeCred (Set (Ledger. Reward StandardCrypto )) -> IO ()
69
+ ensureRewardsDone sQueue epoch epoch' mp = atomically $ do
70
+ mLastEpochDone <- waitRewardsFinished sQueue
71
+ case mLastEpochDone of
72
+ Just lastEpochDone | lastEpochDone == epoch -> pure ()
73
+ _ -> do
74
+ -- If last is not already there, put it to list and wait again
75
+ writeRewardsAction sQueue epoch epoch' mp True
76
+ retry
77
+
78
+ -- To be used by the main thread
79
+ waitRewardsFinished :: RewardsChannels -> STM IO (Maybe EpochNo )
80
+ waitRewardsFinished sQueue = do
81
+ rewardsThreadState <- readTVar (rewardsResult sQueue)
82
+ case rewardsThreadState of
83
+ Just (lastEpoch, Done ) -> pure $ Just lastEpoch -- Normal case
84
+ Just (_, Running ) -> retry -- Wait to finish current work.
85
+ Nothing -> pure Nothing -- This will happen after a restart
86
+
87
+ -- To be used by the main thread
88
+ writeRewardsAction :: RewardsChannels -> EpochNo -> EpochNo -> Map StakeCred (Set (Ledger. Reward StandardCrypto )) -> Bool -> STM IO ()
89
+ writeRewardsAction sQueue epoch epoch' mp checkFirst = do
90
+ TBQ. writeTBQueue (rQueue sQueue) $ RewardsDBAction epoch epoch' mp checkFirst
91
+ writeTVar (rewardsResult sQueue) $ Just (epoch, Running )
0 commit comments