<commit_before>-- | Pos.Util.Relay serialization instances

module Pos.Binary.Relay () where

import           Universum

import           Pos.Binary.Class                 (Bi (..))
import           Pos.Binary.Crypto                ()
import           Pos.Crypto                       (hash)
import           Pos.Ssc.GodTossing.Types.Message (GtMsgContents (..))
import           Pos.Txp.Types.Communication      (TxMsgContents (..))
import           Pos.Types                        (TxId)
import           Pos.Types.Address                (StakeholderId)
import           Pos.Update.Types                 (UpId, UpdateProposal)
import           Pos.Util.Relay                   (DataMsg (..), InvMsg (..), ReqMsg (..))

instance (Bi tag, Bi key) => Bi (InvMsg key tag) where
    put InvMsg {..} = put imTag >> put imKeys
    get = liftM2 InvMsg get get

instance (Bi tag, Bi key) => Bi (ReqMsg key tag) where
    put ReqMsg {..} = put rmTag >> put rmKeys
    get = liftM2 ReqMsg get get

-- Sometimes we want another instances to exist
--instance (Bi tag, Bi contents) => Bi (DataMsg key contents) where
--    put DataMsg {..} = put dmContents >> put dmKey
--    get = liftM2 DataMsg get get

instance Bi (DataMsg StakeholderId GtMsgContents) where
    put DataMsg {..} = put dmContents >> put dmKey
    get = liftM2 DataMsg get get

instance Bi (DataMsg TxId TxMsgContents) where
    put (DataMsg (TxMsgContents dmTx dmWitness dmDistr) _) =
        put dmTx >> put dmWitness >> put dmDistr
    get = do
      tx <- get
      conts <- TxMsgContents tx <$> get <*> get
      pure $ DataMsg conts (hash tx)

instance Bi (DataMsg UpId UpdateProposal) where
    put DataMsg {..} = put dmContents >> put dmKey
    get = liftM2 DataMsg get get<commit_msg>Add check to deserialization of GT DataMsg<commit_after>-- | Pos.Util.Relay serialization instances

module Pos.Binary.Relay () where

import           Universum

import           Pos.Binary.Class                 (Bi (..))
import           Pos.Binary.Crypto                ()
import           Pos.Crypto                       (hash)
import           Pos.Ssc.GodTossing.Types.Base    (VssCertificate (..))
import           Pos.Ssc.GodTossing.Types.Message (GtMsgContents (..))
import           Pos.Txp.Types.Communication      (TxMsgContents (..))
import           Pos.Types                        (TxId)
import           Pos.Types.Address                (StakeholderId, addressHash)
import           Pos.Update.Types                 (UpId, UpdateProposal)
import           Pos.Util.Relay                   (DataMsg (..), InvMsg (..), ReqMsg (..))

instance (Bi tag, Bi key) => Bi (InvMsg key tag) where
    put InvMsg {..} = put imTag >> put imKeys
    get = liftM2 InvMsg get get

instance (Bi tag, Bi key) => Bi (ReqMsg key tag) where
    put ReqMsg {..} = put rmTag >> put rmKeys
    get = liftM2 ReqMsg get get

-- Sometimes we want another instances to exist
--instance (Bi tag, Bi contents) => Bi (DataMsg key contents) where
--    put DataMsg {..} = put dmContents >> put dmKey
--    get = liftM2 DataMsg get get

instance Bi (DataMsg StakeholderId GtMsgContents) where
    put DataMsg {..} = put dmContents >> put dmKey
    get = do
        dmContents <- get
        dmKey <- get
        case dmContents of
            MCCommitment (pk, _, _) ->
                when (addressHash pk /= dmKey) $
                fail
                    "get@DataMsg@GodTossing: stakeholder ID doesn't correspond to public key from commitment"
            MCVssCertificate VssCertificate {..} ->
                when (addressHash vcSigningKey /= dmKey) $
                fail
                    "get@DataMsg@GodTossing: stakeholder ID doesn't correspond to public key from VSS certificate"
            _ -> pass
        return $ DataMsg {..}

instance Bi (DataMsg TxId TxMsgContents) where
    put (DataMsg (TxMsgContents dmTx dmWitness dmDistr) _) =
        put dmTx >> put dmWitness >> put dmDistr
    get = do
      tx <- get
      conts <- TxMsgContents tx <$> get <*> get
      pure $ DataMsg conts (hash tx)

instance Bi (DataMsg UpId UpdateProposal) where
    put DataMsg {..} = put dmContents >> put dmKey
    get = liftM2 DataMsg get get
{'fraction_non_alphanumeric': 0.0964660936007641, 'fraction_numerical': 0.002148997134670487, 'mean_word_length': 1.902979902979903, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3272329', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1209, 'n_words': 399}