<commit_before>{-# LANGUAGE DeriveGeneric   #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies    #-}

-- | A “dynamic state” implementation of SSC. Nodes exchange commitments,
-- openings, and shares, and in the end arrive at a shared seed.
--
-- See https://eprint.iacr.org/2015/889.pdf (“A Provably Secure
-- Proof-of-Stake Blockchain Protocol”), section 4 for more details.

module Pos.Ssc.DynamicState.Types
       (
         -- * Instance types
         DSPayload(..)
       , DSProof(..)
       , DSMessage(..)
       , filterDSPayload
       , mkDSProof
       , verifyDSPayload

       -- * Lenses
       -- ** DSPayload
       , mdCommitments
       , mdOpenings
       , mdShares
       , mdVssCertificates

       -- * Utilities
       , hasCommitment
       , hasOpening
       , hasShares
       ) where

import           Control.Lens              (makeLenses, (^.))
import           Data.Binary               (Binary)
import qualified Data.HashMap.Strict       as HM
import           Data.Ix                   (inRange)
import           Data.MessagePack          (MessagePack)
import           Data.SafeCopy             (base, deriveSafeCopySimple)
import           Data.Text.Buildable       (Buildable (..))
import           Formatting                (bprint, (%))
import           Serokell.Util             (VerificationRes, isVerSuccess, listJson,
                                            verifyGeneric)
import           Universum

import           Pos.Constants             (k)
import           Pos.Crypto                (Hash, PublicKey, Share, hash)
import           Pos.Ssc.Class.Types       (SscTypes (SscPayload))
import           Pos.Ssc.DynamicState.Base (CommitmentsMap, Opening, OpeningsMap,
                                            SharesMap, SignedCommitment, VssCertificate,
                                            VssCertificatesMap, checkCert, isCommitmentId,
                                            isOpeningId, isSharesId,
                                            verifySignedCommitment)
import           Pos.Types                 (MainBlockHeader, SlotId (..), headerSlot)

----------------------------------------------------------------------------
-- SscMessage
----------------------------------------------------------------------------

data DSMessage
    = DSCommitment !PublicKey
                   !SignedCommitment
    | DSOpening !PublicKey
                !Opening
    | DSShares !PublicKey  -- ^ Public key of node who decrypted
                           -- shares and sent them to us.
               (HashMap PublicKey Share) -- ^ (pk, share) means that
                                         -- 'share' was generated by
                                         -- owner of 'pk'
    | DSVssCertificate !PublicKey
                       !VssCertificate
    deriving (Show)

deriveSafeCopySimple 0 'base ''DSMessage

----------------------------------------------------------------------------
-- SscPayload
----------------------------------------------------------------------------

-- | MPC-related content of main body.
data DSPayload = DSPayload
    { -- | Commitments are added during the first phase of epoch.
      _mdCommitments     :: !CommitmentsMap
      -- | Openings are added during the second phase of epoch.
    , _mdOpenings        :: !OpeningsMap
      -- | Decrypted shares to be used in the third phase.<commit_msg>Replace haddock comment with regular comment<commit_after>{-# LANGUAGE DeriveGeneric   #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies    #-}

-- | A “dynamic state” implementation of SSC. Nodes exchange commitments,
-- openings, and shares, and in the end arrive at a shared seed.
--
-- See https://eprint.iacr.org/2015/889.pdf (“A Provably Secure
-- Proof-of-Stake Blockchain Protocol”), section 4 for more details.

module Pos.Ssc.DynamicState.Types
       (
         -- * Instance types
         DSPayload(..)
       , DSProof(..)
       , DSMessage(..)
       , filterDSPayload
       , mkDSProof
       , verifyDSPayload

       -- * Lenses
       -- ** DSPayload
       , mdCommitments
       , mdOpenings
       , mdShares
       , mdVssCertificates

       -- * Utilities
       , hasCommitment
       , hasOpening
       , hasShares
       ) where

import           Control.Lens              (makeLenses, (^.))
import           Data.Binary               (Binary)
import qualified Data.HashMap.Strict       as HM
import           Data.Ix                   (inRange)
import           Data.MessagePack          (MessagePack)
import           Data.SafeCopy             (base, deriveSafeCopySimple)
import           Data.Text.Buildable       (Buildable (..))
import           Formatting                (bprint, (%))
import           Serokell.Util             (VerificationRes, isVerSuccess, listJson,
                                            verifyGeneric)
import           Universum

import           Pos.Constants             (k)
import           Pos.Crypto                (Hash, PublicKey, Share, hash)
import           Pos.Ssc.Class.Types       (SscTypes (SscPayload))
import           Pos.Ssc.DynamicState.Base (CommitmentsMap, Opening, OpeningsMap,
                                            SharesMap, SignedCommitment, VssCertificate,
                                            VssCertificatesMap, checkCert, isCommitmentId,
                                            isOpeningId, isSharesId,
                                            verifySignedCommitment)
import           Pos.Types                 (MainBlockHeader, SlotId (..), headerSlot)

----------------------------------------------------------------------------
-- SscMessage
----------------------------------------------------------------------------

data DSMessage
    = DSCommitment !PublicKey
                   !SignedCommitment
    | DSOpening !PublicKey
                !Opening
    | DSShares !PublicKey  -- Public key of node who decrypted shares
                           -- and sent them to us.
               (HashMap PublicKey Share) -- (pk, share) means that
                                         -- 'share' was generated by
                                         -- owner of 'pk'
    | DSVssCertificate !PublicKey
                       !VssCertificate
    deriving (Show)

deriveSafeCopySimple 0 'base ''DSMessage

----------------------------------------------------------------------------
-- SscPayload
----------------------------------------------------------------------------

-- | MPC-related content of main body.
data DSPayload = DSPayload
    { -- | Commitments are added during the first phase of epoch.
      _mdCommitments     :: !CommitmentsMap
      -- | Openings are added during the second phase of epoch.
    , _mdOpenings        :: !OpeningsMap
      -- | Decrypted shares to be used in the third phase.
{'fraction_non_alphanumeric': 0.1609094884127678, 'fraction_numerical': 0.0026235242675994755, 'mean_word_length': 1.156505342551854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 29, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4208000', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1609, 'n_words': 434}