<commit_before>{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types            #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE ViewPatterns          #-}

-- | Internal state of the MPC algorithm (“multi-party computation”) – the
-- algorithm which computes a shared seed with other nodes and decides which
-- nodes will be leaders of the next epoch.

module Pos.State.Storage.Mpc
       (
         MpcStorage
       , HasMpcStorage(mpcStorage)

       , calculateLeaders
       , getLocalMpcData
       , getGlobalMpcDataByDepth
       , getOurCommitment
       , getOurOpening
       , getOurShares
       , setSecret
       , mpcApplyBlocks
       , mpcProcessCommitment
       , mpcProcessOpening
       , mpcProcessShares
       , mpcProcessVssCertificate
       , mpcRollback
       , mpcVerifyBlock
       , mpcVerifyBlocks
       ) where

import           Control.Lens            (Lens', ix, makeClassy, preview, to, use, view,
                                          (%=), (.=), (^.))
import           Crypto.Random           (drgNewSeed, seedFromInteger, withDRG)
import           Data.Default            (Default, def)
import           Data.Hashable           (Hashable)
import qualified Data.HashMap.Strict     as HM
import           Data.Ix                 (inRange)
import           Data.List.NonEmpty      (NonEmpty ((:|)))
import qualified Data.List.NonEmpty      as NE
import           Data.SafeCopy           (base, deriveSafeCopySimple)
import qualified Data.Vector             as V
import           Serokell.Util.Verify    (VerificationRes (..), isVerSuccess,
                                          verifyGeneric)
import           Universum

import           Pos.Constants           (k)
import           Pos.Crypto              (PublicKey, Share,
                                          Signed (signedSig, signedValue), Threshold,
                                          VssKeyPair, decryptShare, toVssPublicKey,
                                          verify, verifyShare)
import           Pos.FollowTheSatoshi    (FtsError, calculateSeed, followTheSatoshi)
import           Pos.State.Storage.Types (AltChain)
import           Pos.Types               (Address (getAddress), Block, Commitment (..),
                                          CommitmentSignature, CommitmentsMap,
                                          MpcData (..), Opening (..), OpeningsMap,
                                          SharesMap, SlotId (..), SlotLeaders, Utxo,
                                          VssCertificate, VssCertificatesMap, blockMpc,
                                          blockSlot, mdCommitments, mdOpenings, mdShares,
                                          mdVssCertificates, verifyOpening)
import           Pos.Util                (readerToState, zoom', _neHead)

data MpcStorageVersion = MpcStorageVersion
    { -- | Secret that we are using for the current epoch.
      _mpcCurrentSecret      :: !(Maybe (Commitment, Opening))
    , -- | Local set of 'Commitment's. These are valid commitments which are
      -- known to the node and not stored in blockchain. It is useful only
      -- for the first 'k' slots, after that it should be discarded.
      _mpcLocalCommitments   :: !CommitmentsMap
    , -- | Set of 'Commitment's stored in blocks for current epoch. This can
      -- be calculated by 'mconcat'ing stored commitments, but it would be
      -- inefficient to do it every time we need to know if commitments is
      -- stored in blocks.
      _mpcGlobalCommitments  :: !CommitmentsMap
    , -- | Local set of decrypted shares (encrypted shares are stored in
      -- commitments).
      _mpcLocalShares        :: !SharesMap
    , -- | Decrypted shares stored in blocks. These shares are guaranteed to
      -- match encrypted shares stored in 'mpcGlobalCommitments'.
      _mpcGlobalShares       :: !SharesMap
    , -- | Local set of openings
      _mpcLocalOpenings      :: !OpeningsMap
    , -- | Openings stored in blocks
      _mpcGlobalOpenings     :: !OpeningsMap
    , -- | Local set of VSS certificates
      _mpcLocalCertificates  :: !VssCertificatesMap
    , -- | VSS certificates stored in blocks (for all time, not just for
      -- current epoch)
      _mpcGlobalCertificates :: !VssCertificatesMap }

makeClassy ''MpcStorageVersion
deriveSafeCopySimple 0 'base ''MpcStorageVersion

instance Default MpcStorageVersion where
    def =
        MpcStorageVersion
        { _mpcCurrentSecret = Nothing
        , _mpcLocalCommitments = mempty
        , _mpcGlobalCommitments = mempty
        , _mpcLocalShares = mempty
        , _mpcGlobalShares = mempty
        , _mpcLocalOpenings = mempty
        , _mpcGlobalOpenings = mempty
        , _mpcLocalCertificates = mempty
        , _mpcGlobalCertificates = mempty<commit_msg>[POS-36] Fix mpcGlobalCertificates initialization<commit_after>{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types            #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE ViewPatterns          #-}

-- | Internal state of the MPC algorithm (“multi-party computation”) – the
-- algorithm which computes a shared seed with other nodes and decides which
-- nodes will be leaders of the next epoch.

module Pos.State.Storage.Mpc
       (
         MpcStorage
       , HasMpcStorage(mpcStorage)

       , calculateLeaders
       , getLocalMpcData
       , getGlobalMpcDataByDepth
       , getOurCommitment
       , getOurOpening
       , getOurShares
       , setSecret
       , mpcApplyBlocks
       , mpcProcessCommitment
       , mpcProcessOpening
       , mpcProcessShares
       , mpcProcessVssCertificate
       , mpcRollback
       , mpcVerifyBlock
       , mpcVerifyBlocks
       ) where

import           Control.Lens            (Lens', ix, makeClassy, preview, to, use, view,
                                          (%=), (.=), (^.))
import           Crypto.Random           (drgNewSeed, seedFromInteger, withDRG)
import           Data.Default            (Default, def)
import           Data.Hashable           (Hashable)
import qualified Data.HashMap.Strict     as HM
import           Data.Ix                 (inRange)
import           Data.List.NonEmpty      (NonEmpty ((:|)))
import qualified Data.List.NonEmpty      as NE
import           Data.SafeCopy           (base, deriveSafeCopySimple)
import qualified Data.Vector             as V
import           Serokell.Util.Verify    (VerificationRes (..), isVerSuccess,
                                          verifyGeneric)
import           Universum

import           Pos.Constants           (k)
import           Pos.Crypto              (PublicKey, Share,
                                          Signed (signedSig, signedValue), Threshold,
                                          VssKeyPair, decryptShare, toVssPublicKey,
                                          verify, verifyShare)
import           Pos.FollowTheSatoshi    (FtsError, calculateSeed, followTheSatoshi)
import           Pos.Genesis             (genesisCertificates)
import           Pos.State.Storage.Types (AltChain)
import           Pos.Types               (Address (getAddress), Block, Commitment (..),
                                          CommitmentSignature, CommitmentsMap,
                                          MpcData (..), Opening (..), OpeningsMap,
                                          SharesMap, SlotId (..), SlotLeaders, Utxo,
                                          VssCertificate, VssCertificatesMap, blockMpc,
                                          blockSlot, mdCommitments, mdOpenings, mdShares,
                                          mdVssCertificates, verifyOpening)
import           Pos.Util                (readerToState, zoom', _neHead)

data MpcStorageVersion = MpcStorageVersion
    { -- | Secret that we are using for the current epoch.
      _mpcCurrentSecret      :: !(Maybe (Commitment, Opening))
    , -- | Local set of 'Commitment's. These are valid commitments which are
      -- known to the node and not stored in blockchain. It is useful only
      -- for the first 'k' slots, after that it should be discarded.
      _mpcLocalCommitments   :: !CommitmentsMap
    , -- | Set of 'Commitment's stored in blocks for current epoch. This can
      -- be calculated by 'mconcat'ing stored commitments, but it would be
      -- inefficient to do it every time we need to know if commitments is
      -- stored in blocks.
      _mpcGlobalCommitments  :: !CommitmentsMap
    , -- | Local set of decrypted shares (encrypted shares are stored in
      -- commitments).
      _mpcLocalShares        :: !SharesMap
    , -- | Decrypted shares stored in blocks. These shares are guaranteed to
      -- match encrypted shares stored in 'mpcGlobalCommitments'.
      _mpcGlobalShares       :: !SharesMap
    , -- | Local set of openings
      _mpcLocalOpenings      :: !OpeningsMap
    , -- | Openings stored in blocks
      _mpcGlobalOpenings     :: !OpeningsMap
    , -- | Local set of VSS certificates
      _mpcLocalCertificates  :: !VssCertificatesMap
    , -- | VSS certificates stored in blocks (for all time, not just for
      -- current epoch)
      _mpcGlobalCertificates :: !VssCertificatesMap }

makeClassy ''MpcStorageVersion
deriveSafeCopySimple 0 'base ''MpcStorageVersion

instance Default MpcStorageVersion where
    def =
        MpcStorageVersion
        { _mpcCurrentSecret = Nothing
        , _mpcLocalCommitments = mempty
        , _mpcGlobalCommitments = mempty
        , _mpcLocalShares = mempty
        , _mpcGlobalShares = mempty
        , _mpcLocalOpenings = mempty
        , _mpcGlobalOpenings = mempty
        , _mpcLocalCertificates = mempty
        , _mpcGlobalCertificates = genesisCertificates
{'fraction_non_alphanumeric': 0.07248430856448673, 'fraction_numerical': 0.0006074104069649726, 'mean_word_length': 1.7804672108077682, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2825019', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2540, 'n_words': 715}