This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathDelegation.hs
84 lines (69 loc) · 3.05 KB
/
Delegation.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{-# LANGUAGE TypeFamilies #-}
-- | Richmen computation for delegation.
module Pos.DB.Lrc.Consumer.Delegation
(
-- * The 'RichmenComponent'
dlgRichmenComponent
-- * The consumer
, dlgLrcConsumer
-- * Functions for getting richmen
, getDlgRichmen
, tryGetDlgRichmen
, getDlgRichmenObft
) where
import Universum
import Data.HashSet (fromList)
import Pos.Chain.Genesis (configGenesisWStakeholders)
import Pos.Chain.Genesis as Genesis (Config (..))
import Pos.Chain.Lrc (RichmenComponent (..), RichmenSet)
import Pos.Chain.Update (BlockVersionData (..))
import Pos.Core (EpochIndex)
import Pos.DB (MonadDB, MonadDBRead, MonadGState)
import Pos.DB.Lrc.Consumer (LrcConsumer,
lrcConsumerFromComponentSimple)
import Pos.DB.Lrc.Context (HasLrcContext, lrcActionOnEpochReason)
import Pos.DB.Lrc.RichmenBase
import Pos.Util.Util (getKeys)
----------------------------------------------------------------------------
-- RichmenComponent
----------------------------------------------------------------------------
dlgRichmenComponent :: BlockVersionData -> RichmenComponent RichmenSet
dlgRichmenComponent genesisBvd = RichmenComponent
{ rcToData = getKeys . snd
, rcTag = "dlg"
, rcInitialThreshold = bvdHeavyDelThd genesisBvd
, rcConsiderDelegated = False
}
----------------------------------------------------------------------------
-- The consumer
----------------------------------------------------------------------------
-- | Consumer will be called on every Richmen computation.
dlgLrcConsumer :: (MonadGState m, MonadDB m) => BlockVersionData -> LrcConsumer m
dlgLrcConsumer genesisBvd = lrcConsumerFromComponentSimple
(dlgRichmenComponent genesisBvd)
bvdHeavyDelThd
----------------------------------------------------------------------------
-- Getting richmen
----------------------------------------------------------------------------
-- | Wait for LRC results to become available and then get delegation ricmen
-- data for the given epoch.
getDlgRichmen
:: (MonadIO m, MonadDBRead m, MonadReader ctx m, HasLrcContext ctx)
=> BlockVersionData
-> Text -- ^ Function name (to include into error message)
-> EpochIndex -- ^ Epoch for which you want to know the richmen
-> m RichmenSet
getDlgRichmen genesisBvd fname epoch = lrcActionOnEpochReason
epoch
(fname <> ": couldn't get delegation richmen")
(tryGetDlgRichmen genesisBvd)
-- | Like 'getDlgRichmen', but doesn't wait and doesn't fail.
tryGetDlgRichmen
:: MonadDBRead m => BlockVersionData -> EpochIndex -> m (Maybe RichmenSet)
tryGetDlgRichmen = getRichmen . dlgRichmenComponent
-- | For OBFT, we retrieve the genesis stakeholders and classify them as the
-- "richmen". We don't perform any LRC operations here.
getDlgRichmenObft
:: Genesis.Config
-> RichmenSet
getDlgRichmenObft = fromList . configGenesisWStakeholders