<commit_before>module Pos.Ssc.GodTossing.Richmen
       ( gtLrcConsumer
       ) where

import           Universum

import qualified Pos.DB                   as DB
import           Pos.Lrc.Consumer         (LrcConsumer (..))
import           Pos.Lrc.Types            (RichmenStake)
import           Pos.Ssc.Extra.Richmen    (MonadSscRichmen (..), isEmptySscRichmen)
import           Pos.Ssc.GodTossing.Types (SscGodTossing)
import           Pos.Types                (Coin, EpochIndex, mkCoin)
import           Pos.WorkMode             (WorkMode)

-- | Consumer will be called on every Richmen computation.
gtLrcConsumer :: WorkMode SscGodTossing m => LrcConsumer m
gtLrcConsumer = LrcConsumer
    {
      -- [CSL-93] Use eligibility threshold here
      lcThreshold = const (mkCoin 0)
    , lcIfNeedCompute = ifNeed
    , lcComputedCallback = onComputed
    , lcConsiderDelegated = False
    }

-- Returns True if cached value doesnt't correspond to current epoch
ifNeed
    :: WorkMode SscGodTossing m
    => EpochIndex -> m Bool
ifNeed toCompute = do
    (epochIndex, richmen) <- DB.getGtRichmen
    isEmpty <- isEmptySscRichmen
    let needWrite = toCompute == epochIndex && isEmpty
    when needWrite $ writeSscRichmen (epochIndex, richmen)
    pure (epochIndex < toCompute)

-- Store computed value into DB and cache.
onComputed :: WorkMode SscGodTossing m => EpochIndex -> Coin -> RichmenStake -> m ()
onComputed epoch _ richmen = do
    DB.putGtRichmen (epoch, richmen)
    writeSscRichmen (epoch, richmen)<commit_msg>[CSL-519] Use proper LrcConsumer for GodTossing<commit_after>module Pos.Ssc.GodTossing.Richmen
       ( gtLrcConsumer
       ) where

-- import           Universum

import qualified Pos.DB                   as DB
import           Pos.DB.Lrc               (RCSsc)
import           Pos.Lrc.Consumer         (LrcConsumer (..),
                                           lrcConsumerFromComponentSimple)
import           Pos.Ssc.GodTossing.Types (SscGodTossing)

-- | Consumer will be called on every Richmen computation.
gtLrcConsumer :: DB.MonadDB SscGodTossing m => LrcConsumer m
gtLrcConsumer = lrcConsumerFromComponentSimple @RCSsc
{'fraction_non_alphanumeric': 0.07653061224489796, 'fraction_numerical': 0.0027829313543599257, 'mean_word_length': 2.3703125, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6324360', 'n_tokens_mistral': 665, 'n_tokens_neox': 651, 'n_words': 168}