{-# LANGUAGE FlexibleInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}

module Test.ThreadNet.Util.HasCreator.Mock () where

import           Cardano.Crypto.DSIGN
import           Data.Word (Word64)
import           Ouroboros.Consensus.Mock.Ledger
import           Ouroboros.Consensus.Mock.Protocol.Praos
import           Ouroboros.Consensus.NodeId (CoreNodeId (..))
import           Ouroboros.Consensus.Protocol.BFT
import           Ouroboros.Consensus.Protocol.PBFT
import           Test.ThreadNet.Util.HasCreator


instance HasCreator (SimpleBftBlock c BftMockCrypto) where
    getCreator :: SimpleBftBlock c BftMockCrypto -> CoreNodeId
getCreator = SignedDSIGN MockDSIGN (SignedSimpleBft c BftMockCrypto)
-> CoreNodeId
forall a. SignedDSIGN MockDSIGN a -> CoreNodeId
coreNodeId
               (SignedDSIGN MockDSIGN (SignedSimpleBft c BftMockCrypto)
 -> CoreNodeId)
-> (SimpleBftBlock c BftMockCrypto
    -> SignedDSIGN MockDSIGN (SignedSimpleBft c BftMockCrypto))
-> SimpleBftBlock c BftMockCrypto
-> CoreNodeId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto)
-> SignedDSIGN MockDSIGN (SignedSimpleBft c BftMockCrypto)
BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto)
-> SignedDSIGN
     (BftDSIGN BftMockCrypto) (SignedSimpleBft c BftMockCrypto)
forall c toSign.
BftFields c toSign -> SignedDSIGN (BftDSIGN c) toSign
bftSignature
               (BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto)
 -> SignedDSIGN MockDSIGN (SignedSimpleBft c BftMockCrypto))
-> (SimpleBftBlock c BftMockCrypto
    -> BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto))
-> SimpleBftBlock c BftMockCrypto
-> SignedDSIGN MockDSIGN (SignedSimpleBft c BftMockCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleBftExt c BftMockCrypto
-> BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto)
forall c c'.
SimpleBftExt c c' -> BftFields c' (SignedSimpleBft c c')
simpleBftExt
               (SimpleBftExt c BftMockCrypto
 -> BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto))
-> (SimpleBftBlock c BftMockCrypto -> SimpleBftExt c BftMockCrypto)
-> SimpleBftBlock c BftMockCrypto
-> BftFields BftMockCrypto (SignedSimpleBft c BftMockCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header (SimpleBftBlock c BftMockCrypto)
-> SimpleBftExt c BftMockCrypto
forall c ext ext'. Header (SimpleBlock' c ext ext') -> ext'
simpleHeaderExt
               (Header (SimpleBftBlock c BftMockCrypto)
 -> SimpleBftExt c BftMockCrypto)
-> (SimpleBftBlock c BftMockCrypto
    -> Header (SimpleBftBlock c BftMockCrypto))
-> SimpleBftBlock c BftMockCrypto
-> SimpleBftExt c BftMockCrypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleBftBlock c BftMockCrypto
-> Header (SimpleBftBlock c BftMockCrypto)
forall c ext ext'.
SimpleBlock' c ext ext' -> Header (SimpleBlock' c ext ext')
simpleHeader
      where
        coreNodeId :: SignedDSIGN MockDSIGN a -> CoreNodeId
        coreNodeId :: forall a. SignedDSIGN MockDSIGN a -> CoreNodeId
coreNodeId = Word64 -> CoreNodeId
CoreNodeId (Word64 -> CoreNodeId)
-> (SignedDSIGN MockDSIGN a -> Word64)
-> SignedDSIGN MockDSIGN a
-> CoreNodeId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignedDSIGN MockDSIGN a -> Word64
forall a. SignedDSIGN MockDSIGN a -> Word64
verKeyIdFromSigned

instance HasCreator (SimplePBftBlock c PBftMockCrypto) where
    getCreator :: SimplePBftBlock c PBftMockCrypto -> CoreNodeId
getCreator = SignedDSIGN MockDSIGN (SignedSimplePBft c PBftMockCrypto)
-> CoreNodeId
forall a. SignedDSIGN MockDSIGN a -> CoreNodeId
coreNodeId
               (SignedDSIGN MockDSIGN (SignedSimplePBft c PBftMockCrypto)
 -> CoreNodeId)
-> (SimplePBftBlock c PBftMockCrypto
    -> SignedDSIGN MockDSIGN (SignedSimplePBft c PBftMockCrypto))
-> SimplePBftBlock c PBftMockCrypto
-> CoreNodeId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto)
-> SignedDSIGN MockDSIGN (SignedSimplePBft c PBftMockCrypto)
PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto)
-> SignedDSIGN
     (PBftDSIGN PBftMockCrypto) (SignedSimplePBft c PBftMockCrypto)
forall c toSign.
PBftFields c toSign -> SignedDSIGN (PBftDSIGN c) toSign
pbftSignature
               (PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto)
 -> SignedDSIGN MockDSIGN (SignedSimplePBft c PBftMockCrypto))
-> (SimplePBftBlock c PBftMockCrypto
    -> PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto))
-> SimplePBftBlock c PBftMockCrypto
-> SignedDSIGN MockDSIGN (SignedSimplePBft c PBftMockCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimplePBftExt c PBftMockCrypto
-> PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto)
forall c c'.
SimplePBftExt c c' -> PBftFields c' (SignedSimplePBft c c')
simplePBftExt
               (SimplePBftExt c PBftMockCrypto
 -> PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto))
-> (SimplePBftBlock c PBftMockCrypto
    -> SimplePBftExt c PBftMockCrypto)
-> SimplePBftBlock c PBftMockCrypto
-> PBftFields PBftMockCrypto (SignedSimplePBft c PBftMockCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header (SimplePBftBlock c PBftMockCrypto)
-> SimplePBftExt c PBftMockCrypto
forall c ext ext'. Header (SimpleBlock' c ext ext') -> ext'
simpleHeaderExt
               (Header (SimplePBftBlock c PBftMockCrypto)
 -> SimplePBftExt c PBftMockCrypto)
-> (SimplePBftBlock c PBftMockCrypto
    -> Header (SimplePBftBlock c PBftMockCrypto))
-> SimplePBftBlock c PBftMockCrypto
-> SimplePBftExt c PBftMockCrypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimplePBftBlock c PBftMockCrypto
-> Header (SimplePBftBlock c PBftMockCrypto)
forall c ext ext'.
SimpleBlock' c ext ext' -> Header (SimpleBlock' c ext ext')
simpleHeader
      where
        coreNodeId :: SignedDSIGN MockDSIGN a -> CoreNodeId
        coreNodeId :: forall a. SignedDSIGN MockDSIGN a -> CoreNodeId
coreNodeId = Word64 -> CoreNodeId
CoreNodeId (Word64 -> CoreNodeId)
-> (SignedDSIGN MockDSIGN a -> Word64)
-> SignedDSIGN MockDSIGN a
-> CoreNodeId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SignedDSIGN MockDSIGN a -> Word64
forall a. SignedDSIGN MockDSIGN a -> Word64
verKeyIdFromSigned

instance HasCreator (SimplePraosBlock c PraosMockCrypto) where
    getCreator :: SimplePraosBlock c PraosMockCrypto -> CoreNodeId
getCreator = PraosExtraFields PraosMockCrypto -> CoreNodeId
forall c. PraosExtraFields c -> CoreNodeId
praosCreator
               (PraosExtraFields PraosMockCrypto -> CoreNodeId)
-> (SimplePraosBlock c PraosMockCrypto
    -> PraosExtraFields PraosMockCrypto)
-> SimplePraosBlock c PraosMockCrypto
-> CoreNodeId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PraosFields PraosMockCrypto (SignedSimplePraos c PraosMockCrypto)
-> PraosExtraFields PraosMockCrypto
forall crypto typeBeingSigned.
PraosFields crypto typeBeingSigned -> PraosExtraFields crypto
praosExtraFields
               (PraosFields PraosMockCrypto (SignedSimplePraos c PraosMockCrypto)
 -> PraosExtraFields PraosMockCrypto)
-> (SimplePraosBlock c PraosMockCrypto
    -> PraosFields
         PraosMockCrypto (SignedSimplePraos c PraosMockCrypto))
-> SimplePraosBlock c PraosMockCrypto
-> PraosExtraFields PraosMockCrypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimplePraosExt c PraosMockCrypto
-> PraosFields
     PraosMockCrypto (SignedSimplePraos c PraosMockCrypto)
forall c c'.
SimplePraosExt c c' -> PraosFields c' (SignedSimplePraos c c')
simplePraosExt
               (SimplePraosExt c PraosMockCrypto
 -> PraosFields
      PraosMockCrypto (SignedSimplePraos c PraosMockCrypto))
-> (SimplePraosBlock c PraosMockCrypto
    -> SimplePraosExt c PraosMockCrypto)
-> SimplePraosBlock c PraosMockCrypto
-> PraosFields
     PraosMockCrypto (SignedSimplePraos c PraosMockCrypto)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header (SimplePraosBlock c PraosMockCrypto)
-> SimplePraosExt c PraosMockCrypto
forall c ext ext'. Header (SimpleBlock' c ext ext') -> ext'
simpleHeaderExt
               (Header (SimplePraosBlock c PraosMockCrypto)
 -> SimplePraosExt c PraosMockCrypto)
-> (SimplePraosBlock c PraosMockCrypto
    -> Header (SimplePraosBlock c PraosMockCrypto))
-> SimplePraosBlock c PraosMockCrypto
-> SimplePraosExt c PraosMockCrypto
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimplePraosBlock c PraosMockCrypto
-> Header (SimplePraosBlock c PraosMockCrypto)
forall c ext ext'.
SimpleBlock' c ext ext' -> Header (SimpleBlock' c ext ext')
simpleHeader

instance HasCreator (SimplePraosRuleBlock c) where
    getCreator :: SimplePraosRuleBlock c -> CoreNodeId
getCreator = SimplePraosRuleExt -> CoreNodeId
simplePraosRuleExt
               (SimplePraosRuleExt -> CoreNodeId)
-> (SimplePraosRuleBlock c -> SimplePraosRuleExt)
-> SimplePraosRuleBlock c
-> CoreNodeId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header (SimplePraosRuleBlock c) -> SimplePraosRuleExt
forall c ext ext'. Header (SimpleBlock' c ext ext') -> ext'
simpleHeaderExt
               (Header (SimplePraosRuleBlock c) -> SimplePraosRuleExt)
-> (SimplePraosRuleBlock c -> Header (SimplePraosRuleBlock c))
-> SimplePraosRuleBlock c
-> SimplePraosRuleExt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimplePraosRuleBlock c -> Header (SimplePraosRuleBlock c)
forall c ext ext'.
SimpleBlock' c ext ext' -> Header (SimpleBlock' c ext ext')
simpleHeader

-- | Get the id of the signer from a signature. Used for testing.
verKeyIdFromSigned :: SignedDSIGN MockDSIGN a -> Word64
verKeyIdFromSigned :: forall a. SignedDSIGN MockDSIGN a -> Word64
verKeyIdFromSigned (SignedDSIGN (SigMockDSIGN Hash ShortHash ()
_ Word64
i)) = Word64
i