{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Api.Governance.Actions.VotingProcedure where

import           Cardano.Api.Address
import           Cardano.Api.Eon.ConwayEraOnwards
import           Cardano.Api.Eon.ShelleyBasedEra
import           Cardano.Api.Governance.Actions.ProposalProcedure
import           Cardano.Api.HasTypeProxy
import qualified Cardano.Api.ReexposeLedger as Ledger
import           Cardano.Api.SerialiseCBOR
import           Cardano.Api.SerialiseTextEnvelope

import qualified Cardano.Binary as CBOR
import qualified Cardano.Ledger.Api as L
import           Cardano.Ledger.Core (EraCrypto)
import qualified Cardano.Ledger.Core as L

import           Control.Monad (foldM)
import qualified Data.Map as Map
import qualified Data.Set as Set
import           Data.Text (Text)
import qualified Data.Text.Encoding as Text
import           GHC.Generics

newtype GovernanceActionId era = GovernanceActionId
  { forall era.
GovernanceActionId era
-> GovActionId (EraCrypto (ShelleyLedgerEra era))
unGovernanceActionId :: Ledger.GovActionId (EraCrypto (ShelleyLedgerEra era))
  }
  deriving (Int -> GovernanceActionId era -> ShowS
[GovernanceActionId era] -> ShowS
GovernanceActionId era -> String
(Int -> GovernanceActionId era -> ShowS)
-> (GovernanceActionId era -> String)
-> ([GovernanceActionId era] -> ShowS)
-> Show (GovernanceActionId era)
forall era. Int -> GovernanceActionId era -> ShowS
forall era. [GovernanceActionId era] -> ShowS
forall era. GovernanceActionId era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall era. Int -> GovernanceActionId era -> ShowS
showsPrec :: Int -> GovernanceActionId era -> ShowS
$cshow :: forall era. GovernanceActionId era -> String
show :: GovernanceActionId era -> String
$cshowList :: forall era. [GovernanceActionId era] -> ShowS
showList :: [GovernanceActionId era] -> ShowS
Show, GovernanceActionId era -> GovernanceActionId era -> Bool
(GovernanceActionId era -> GovernanceActionId era -> Bool)
-> (GovernanceActionId era -> GovernanceActionId era -> Bool)
-> Eq (GovernanceActionId era)
forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
== :: GovernanceActionId era -> GovernanceActionId era -> Bool
$c/= :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
/= :: GovernanceActionId era -> GovernanceActionId era -> Bool
Eq, Eq (GovernanceActionId era)
Eq (GovernanceActionId era) =>
(GovernanceActionId era -> GovernanceActionId era -> Ordering)
-> (GovernanceActionId era -> GovernanceActionId era -> Bool)
-> (GovernanceActionId era -> GovernanceActionId era -> Bool)
-> (GovernanceActionId era -> GovernanceActionId era -> Bool)
-> (GovernanceActionId era -> GovernanceActionId era -> Bool)
-> (GovernanceActionId era
    -> GovernanceActionId era -> GovernanceActionId era)
-> (GovernanceActionId era
    -> GovernanceActionId era -> GovernanceActionId era)
-> Ord (GovernanceActionId era)
GovernanceActionId era -> GovernanceActionId era -> Bool
GovernanceActionId era -> GovernanceActionId era -> Ordering
GovernanceActionId era
-> GovernanceActionId era -> GovernanceActionId era
forall era. Eq (GovernanceActionId era)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
forall era.
GovernanceActionId era -> GovernanceActionId era -> Ordering
forall era.
GovernanceActionId era
-> GovernanceActionId era -> GovernanceActionId era
$ccompare :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Ordering
compare :: GovernanceActionId era -> GovernanceActionId era -> Ordering
$c< :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
< :: GovernanceActionId era -> GovernanceActionId era -> Bool
$c<= :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
<= :: GovernanceActionId era -> GovernanceActionId era -> Bool
$c> :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
> :: GovernanceActionId era -> GovernanceActionId era -> Bool
$c>= :: forall era.
GovernanceActionId era -> GovernanceActionId era -> Bool
>= :: GovernanceActionId era -> GovernanceActionId era -> Bool
$cmax :: forall era.
GovernanceActionId era
-> GovernanceActionId era -> GovernanceActionId era
max :: GovernanceActionId era
-> GovernanceActionId era -> GovernanceActionId era
$cmin :: forall era.
GovernanceActionId era
-> GovernanceActionId era -> GovernanceActionId era
min :: GovernanceActionId era
-> GovernanceActionId era -> GovernanceActionId era
Ord)

instance IsShelleyBasedEra era => ToCBOR (GovernanceActionId era) where
  toCBOR :: GovernanceActionId era -> Encoding
toCBOR = \case
    GovernanceActionId GovActionId (EraCrypto (ShelleyLedgerEra era))
v ->
      ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era => Encoding) -> Encoding)
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall a b. (a -> b) -> a -> b
$ forall era t. (Era era, EncCBOR t) => t -> Encoding
Ledger.toEraCBOR @(ShelleyLedgerEra era) GovActionId (EraCrypto (ShelleyLedgerEra era))
GovActionId StandardCrypto
v

instance IsShelleyBasedEra era => FromCBOR (GovernanceActionId era) where
  fromCBOR :: forall s. Decoder s (GovernanceActionId era)
fromCBOR = do
    !GovActionId (EraCrypto (ShelleyLedgerEra era))
v <- ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    Decoder s (GovActionId (EraCrypto (ShelleyLedgerEra era))))
-> Decoder s (GovActionId (EraCrypto (ShelleyLedgerEra era)))
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era =>
  Decoder s (GovActionId (EraCrypto (ShelleyLedgerEra era))))
 -> Decoder s (GovActionId (EraCrypto (ShelleyLedgerEra era))))
-> (ShelleyBasedEraConstraints era =>
    Decoder s (GovActionId (EraCrypto (ShelleyLedgerEra era))))
-> Decoder s (GovActionId (EraCrypto (ShelleyLedgerEra era)))
forall a b. (a -> b) -> a -> b
$ forall era t s. (Era era, DecCBOR t) => Decoder s t
Ledger.fromEraCBOR @(ShelleyLedgerEra era)
    GovernanceActionId era -> Decoder s (GovernanceActionId era)
forall a. a -> Decoder s a
forall (m :: * -> *) a. Monad m => a -> m a
return (GovernanceActionId era -> Decoder s (GovernanceActionId era))
-> GovernanceActionId era -> Decoder s (GovernanceActionId era)
forall a b. (a -> b) -> a -> b
$ GovActionId (EraCrypto (ShelleyLedgerEra era))
-> GovernanceActionId era
forall era.
GovActionId (EraCrypto (ShelleyLedgerEra era))
-> GovernanceActionId era
GovernanceActionId GovActionId (EraCrypto (ShelleyLedgerEra era))
v

newtype Voter era = Voter (Ledger.Voter (L.EraCrypto (ShelleyLedgerEra era)))
  deriving (Int -> Voter era -> ShowS
[Voter era] -> ShowS
Voter era -> String
(Int -> Voter era -> ShowS)
-> (Voter era -> String)
-> ([Voter era] -> ShowS)
-> Show (Voter era)
forall era. Int -> Voter era -> ShowS
forall era. [Voter era] -> ShowS
forall era. Voter era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall era. Int -> Voter era -> ShowS
showsPrec :: Int -> Voter era -> ShowS
$cshow :: forall era. Voter era -> String
show :: Voter era -> String
$cshowList :: forall era. [Voter era] -> ShowS
showList :: [Voter era] -> ShowS
Show, Voter era -> Voter era -> Bool
(Voter era -> Voter era -> Bool)
-> (Voter era -> Voter era -> Bool) -> Eq (Voter era)
forall era. Voter era -> Voter era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall era. Voter era -> Voter era -> Bool
== :: Voter era -> Voter era -> Bool
$c/= :: forall era. Voter era -> Voter era -> Bool
/= :: Voter era -> Voter era -> Bool
Eq, Eq (Voter era)
Eq (Voter era) =>
(Voter era -> Voter era -> Ordering)
-> (Voter era -> Voter era -> Bool)
-> (Voter era -> Voter era -> Bool)
-> (Voter era -> Voter era -> Bool)
-> (Voter era -> Voter era -> Bool)
-> (Voter era -> Voter era -> Voter era)
-> (Voter era -> Voter era -> Voter era)
-> Ord (Voter era)
Voter era -> Voter era -> Bool
Voter era -> Voter era -> Ordering
Voter era -> Voter era -> Voter era
forall era. Eq (Voter era)
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall era. Voter era -> Voter era -> Bool
forall era. Voter era -> Voter era -> Ordering
forall era. Voter era -> Voter era -> Voter era
$ccompare :: forall era. Voter era -> Voter era -> Ordering
compare :: Voter era -> Voter era -> Ordering
$c< :: forall era. Voter era -> Voter era -> Bool
< :: Voter era -> Voter era -> Bool
$c<= :: forall era. Voter era -> Voter era -> Bool
<= :: Voter era -> Voter era -> Bool
$c> :: forall era. Voter era -> Voter era -> Bool
> :: Voter era -> Voter era -> Bool
$c>= :: forall era. Voter era -> Voter era -> Bool
>= :: Voter era -> Voter era -> Bool
$cmax :: forall era. Voter era -> Voter era -> Voter era
max :: Voter era -> Voter era -> Voter era
$cmin :: forall era. Voter era -> Voter era -> Voter era
min :: Voter era -> Voter era -> Voter era
Ord)

instance IsShelleyBasedEra era => ToCBOR (Voter era) where
  toCBOR :: Voter era -> Encoding
toCBOR (Voter Voter (EraCrypto (ShelleyLedgerEra era))
v) = ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era => Encoding) -> Encoding)
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall a b. (a -> b) -> a -> b
$ forall era t. (Era era, EncCBOR t) => t -> Encoding
Ledger.toEraCBOR @(ShelleyLedgerEra era) Voter (EraCrypto (ShelleyLedgerEra era))
Voter StandardCrypto
v

instance IsShelleyBasedEra era => FromCBOR (Voter era) where
  fromCBOR :: forall s. Decoder s (Voter era)
fromCBOR = do
    !Voter (EraCrypto (ShelleyLedgerEra era))
v <- ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    Decoder s (Voter (EraCrypto (ShelleyLedgerEra era))))
-> Decoder s (Voter (EraCrypto (ShelleyLedgerEra era)))
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era =>
  Decoder s (Voter (EraCrypto (ShelleyLedgerEra era))))
 -> Decoder s (Voter (EraCrypto (ShelleyLedgerEra era))))
-> (ShelleyBasedEraConstraints era =>
    Decoder s (Voter (EraCrypto (ShelleyLedgerEra era))))
-> Decoder s (Voter (EraCrypto (ShelleyLedgerEra era)))
forall a b. (a -> b) -> a -> b
$ forall era t s. (Era era, DecCBOR t) => Decoder s t
Ledger.fromEraCBOR @(ShelleyLedgerEra era)
    Voter era -> Decoder s (Voter era)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Voter era -> Decoder s (Voter era))
-> Voter era -> Decoder s (Voter era)
forall a b. (a -> b) -> a -> b
$ Voter (EraCrypto (ShelleyLedgerEra era)) -> Voter era
forall era. Voter (EraCrypto (ShelleyLedgerEra era)) -> Voter era
Voter Voter (EraCrypto (ShelleyLedgerEra era))
v

data Vote
  = No
  | Yes
  | Abstain
  deriving (Int -> Vote -> ShowS
[Vote] -> ShowS
Vote -> String
(Int -> Vote -> ShowS)
-> (Vote -> String) -> ([Vote] -> ShowS) -> Show Vote
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Vote -> ShowS
showsPrec :: Int -> Vote -> ShowS
$cshow :: Vote -> String
show :: Vote -> String
$cshowList :: [Vote] -> ShowS
showList :: [Vote] -> ShowS
Show, Vote -> Vote -> Bool
(Vote -> Vote -> Bool) -> (Vote -> Vote -> Bool) -> Eq Vote
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Vote -> Vote -> Bool
== :: Vote -> Vote -> Bool
$c/= :: Vote -> Vote -> Bool
/= :: Vote -> Vote -> Bool
Eq)

toVote :: Vote -> Ledger.Vote
toVote :: Vote -> Vote
toVote = \case
  Vote
No -> Vote
Ledger.VoteNo
  Vote
Yes -> Vote
Ledger.VoteYes
  Vote
Abstain -> Vote
Ledger.Abstain

createVotingProcedure
  :: ()
  => ConwayEraOnwards era
  -> Vote
  -> Maybe (Ledger.Url, Text)
  -- ^ Anchor
  -> VotingProcedure era
createVotingProcedure :: forall era.
ConwayEraOnwards era
-> Vote -> Maybe (Url, Text) -> VotingProcedure era
createVotingProcedure ConwayEraOnwards era
eon Vote
vChoice Maybe (Url, Text)
mProposalAnchor =
  let proposalAnchor :: Maybe (Url, ByteString)
proposalAnchor = (Text -> ByteString) -> (Url, Text) -> (Url, ByteString)
forall a b. (a -> b) -> (Url, a) -> (Url, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ByteString
Text.encodeUtf8 ((Url, Text) -> (Url, ByteString))
-> Maybe (Url, Text) -> Maybe (Url, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Url, Text)
mProposalAnchor
   in ConwayEraOnwards era
-> (ConwayEraOnwardsConstraints era => VotingProcedure era)
-> VotingProcedure era
forall era a.
ConwayEraOnwards era -> (ConwayEraOnwardsConstraints era => a) -> a
conwayEraOnwardsConstraints ConwayEraOnwards era
eon ((ConwayEraOnwardsConstraints era => VotingProcedure era)
 -> VotingProcedure era)
-> (ConwayEraOnwardsConstraints era => VotingProcedure era)
-> VotingProcedure era
forall a b. (a -> b) -> a -> b
$
        VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era
forall era.
VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era
VotingProcedure (VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era)
-> VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era
forall a b. (a -> b) -> a -> b
$
          Ledger.VotingProcedure
            { vProcVote :: Vote
Ledger.vProcVote = Vote -> Vote
toVote Vote
vChoice
            , vProcAnchor :: StrictMaybe (Anchor (EraCrypto (ShelleyLedgerEra era)))
Ledger.vProcAnchor = Maybe (Anchor (EraCrypto (ShelleyLedgerEra era)))
-> StrictMaybe (Anchor (EraCrypto (ShelleyLedgerEra era)))
forall a. Maybe a -> StrictMaybe a
Ledger.maybeToStrictMaybe (Maybe (Anchor (EraCrypto (ShelleyLedgerEra era)))
 -> StrictMaybe (Anchor (EraCrypto (ShelleyLedgerEra era))))
-> Maybe (Anchor (EraCrypto (ShelleyLedgerEra era)))
-> StrictMaybe (Anchor (EraCrypto (ShelleyLedgerEra era)))
forall a b. (a -> b) -> a -> b
$ (Url -> ByteString -> Anchor StandardCrypto)
-> (Url, ByteString) -> Anchor StandardCrypto
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Url -> ByteString -> Anchor StandardCrypto
createAnchor ((Url, ByteString) -> Anchor StandardCrypto)
-> Maybe (Url, ByteString) -> Maybe (Anchor StandardCrypto)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Url, ByteString)
proposalAnchor
            }

newtype VotingProcedure era = VotingProcedure
  { forall era.
VotingProcedure era -> VotingProcedure (ShelleyLedgerEra era)
unVotingProcedure :: Ledger.VotingProcedure (ShelleyLedgerEra era)
  }
  deriving (Int -> VotingProcedure era -> ShowS
[VotingProcedure era] -> ShowS
VotingProcedure era -> String
(Int -> VotingProcedure era -> ShowS)
-> (VotingProcedure era -> String)
-> ([VotingProcedure era] -> ShowS)
-> Show (VotingProcedure era)
forall era. Int -> VotingProcedure era -> ShowS
forall era. [VotingProcedure era] -> ShowS
forall era. VotingProcedure era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall era. Int -> VotingProcedure era -> ShowS
showsPrec :: Int -> VotingProcedure era -> ShowS
$cshow :: forall era. VotingProcedure era -> String
show :: VotingProcedure era -> String
$cshowList :: forall era. [VotingProcedure era] -> ShowS
showList :: [VotingProcedure era] -> ShowS
Show, VotingProcedure era -> VotingProcedure era -> Bool
(VotingProcedure era -> VotingProcedure era -> Bool)
-> (VotingProcedure era -> VotingProcedure era -> Bool)
-> Eq (VotingProcedure era)
forall era. VotingProcedure era -> VotingProcedure era -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall era. VotingProcedure era -> VotingProcedure era -> Bool
== :: VotingProcedure era -> VotingProcedure era -> Bool
$c/= :: forall era. VotingProcedure era -> VotingProcedure era -> Bool
/= :: VotingProcedure era -> VotingProcedure era -> Bool
Eq)

instance IsShelleyBasedEra era => ToCBOR (VotingProcedure era) where
  toCBOR :: VotingProcedure era -> Encoding
toCBOR (VotingProcedure VotingProcedure (ShelleyLedgerEra era)
vp) = ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints ShelleyBasedEra era
sbe ((ShelleyBasedEraConstraints era => Encoding) -> Encoding)
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall a b. (a -> b) -> a -> b
$ forall era t. (Era era, EncCBOR t) => t -> Encoding
L.toEraCBOR @(ShelleyLedgerEra era) VotingProcedure (ShelleyLedgerEra era)
vp
   where
    sbe :: ShelleyBasedEra era
sbe = forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era

instance IsShelleyBasedEra era => FromCBOR (VotingProcedure era) where
  fromCBOR :: forall s. Decoder s (VotingProcedure era)
fromCBOR =
    ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    Decoder s (VotingProcedure era))
-> Decoder s (VotingProcedure era)
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era =>
  Decoder s (VotingProcedure era))
 -> Decoder s (VotingProcedure era))
-> (ShelleyBasedEraConstraints era =>
    Decoder s (VotingProcedure era))
-> Decoder s (VotingProcedure era)
forall a b. (a -> b) -> a -> b
$
      VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era
forall era.
VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era
VotingProcedure (VotingProcedure (ShelleyLedgerEra era) -> VotingProcedure era)
-> Decoder s (VotingProcedure (ShelleyLedgerEra era))
-> Decoder s (VotingProcedure era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall era t s. (Era era, DecCBOR t) => Decoder s t
L.fromEraCBOR @(ShelleyLedgerEra era)

instance IsShelleyBasedEra era => SerialiseAsCBOR (VotingProcedure era) where
  serialiseToCBOR :: VotingProcedure era -> ByteString
serialiseToCBOR = ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    VotingProcedure era -> ByteString)
-> VotingProcedure era
-> ByteString
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ShelleyBasedEraConstraints era => VotingProcedure era -> ByteString
VotingProcedure era -> ByteString
forall a. ToCBOR a => a -> ByteString
CBOR.serialize'
  deserialiseFromCBOR :: AsType (VotingProcedure era)
-> ByteString -> Either DecoderError (VotingProcedure era)
deserialiseFromCBOR AsType (VotingProcedure era)
_proxy = ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    ByteString -> Either DecoderError (VotingProcedure era))
-> ByteString
-> Either DecoderError (VotingProcedure era)
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ShelleyBasedEraConstraints era =>
ByteString -> Either DecoderError (VotingProcedure era)
ByteString -> Either DecoderError (VotingProcedure era)
forall a. FromCBOR a => ByteString -> Either DecoderError a
CBOR.decodeFull'

instance IsShelleyBasedEra era => HasTextEnvelope (VotingProcedure era) where
  textEnvelopeType :: AsType (VotingProcedure era) -> TextEnvelopeType
textEnvelopeType AsType (VotingProcedure era)
_ = TextEnvelopeType
"Governance vote"

instance HasTypeProxy era => HasTypeProxy (VotingProcedure era) where
  data AsType (VotingProcedure era) = AsVote
  proxyToAsType :: Proxy (VotingProcedure era) -> AsType (VotingProcedure era)
proxyToAsType Proxy (VotingProcedure era)
_ = AsType (VotingProcedure era)
forall era. AsType (VotingProcedure era)
AsVote

newtype VotingProcedures era = VotingProcedures
  { forall era.
VotingProcedures era -> VotingProcedures (ShelleyLedgerEra era)
unVotingProcedures :: L.VotingProcedures (ShelleyLedgerEra era)
  }

deriving instance Eq (VotingProcedures era)

deriving instance Generic (VotingProcedures era)

deriving instance Show (VotingProcedures era)

instance IsShelleyBasedEra era => ToCBOR (VotingProcedures era) where
  toCBOR :: VotingProcedures era -> Encoding
toCBOR = \case
    VotingProcedures VotingProcedures (ShelleyLedgerEra era)
vp ->
      ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era => Encoding) -> Encoding)
-> (ShelleyBasedEraConstraints era => Encoding) -> Encoding
forall a b. (a -> b) -> a -> b
$
        forall era t. (Era era, EncCBOR t) => t -> Encoding
L.toEraCBOR @(ShelleyLedgerEra era) VotingProcedures (ShelleyLedgerEra era)
vp

instance IsShelleyBasedEra era => FromCBOR (VotingProcedures era) where
  fromCBOR :: forall s. Decoder s (VotingProcedures era)
fromCBOR =
    ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    Decoder s (VotingProcedures era))
-> Decoder s (VotingProcedures era)
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ((ShelleyBasedEraConstraints era =>
  Decoder s (VotingProcedures era))
 -> Decoder s (VotingProcedures era))
-> (ShelleyBasedEraConstraints era =>
    Decoder s (VotingProcedures era))
-> Decoder s (VotingProcedures era)
forall a b. (a -> b) -> a -> b
$
      VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
forall era.
VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
VotingProcedures (VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era)
-> Decoder s (VotingProcedures (ShelleyLedgerEra era))
-> Decoder s (VotingProcedures era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall era t s. (Era era, DecCBOR t) => Decoder s t
L.fromEraCBOR @(ShelleyLedgerEra era)

instance IsShelleyBasedEra era => SerialiseAsCBOR (VotingProcedures era) where
  serialiseToCBOR :: VotingProcedures era -> ByteString
serialiseToCBOR = ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    VotingProcedures era -> ByteString)
-> VotingProcedures era
-> ByteString
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ShelleyBasedEraConstraints era =>
VotingProcedures era -> ByteString
VotingProcedures era -> ByteString
forall a. ToCBOR a => a -> ByteString
CBOR.serialize'
  deserialiseFromCBOR :: AsType (VotingProcedures era)
-> ByteString -> Either DecoderError (VotingProcedures era)
deserialiseFromCBOR AsType (VotingProcedures era)
_proxy = ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era =>
    ByteString -> Either DecoderError (VotingProcedures era))
-> ByteString
-> Either DecoderError (VotingProcedures era)
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra @era) ShelleyBasedEraConstraints era =>
ByteString -> Either DecoderError (VotingProcedures era)
ByteString -> Either DecoderError (VotingProcedures era)
forall a. FromCBOR a => ByteString -> Either DecoderError a
CBOR.decodeFull'

instance IsShelleyBasedEra era => HasTextEnvelope (VotingProcedures era) where
  textEnvelopeType :: AsType (VotingProcedures era) -> TextEnvelopeType
textEnvelopeType AsType (VotingProcedures era)
_ = TextEnvelopeType
"Governance voting procedures"

instance HasTypeProxy era => HasTypeProxy (VotingProcedures era) where
  data AsType (VotingProcedures era) = AsVotingProcedures
  proxyToAsType :: Proxy (VotingProcedures era) -> AsType (VotingProcedures era)
proxyToAsType Proxy (VotingProcedures era)
_ = AsType (VotingProcedures era)
forall era. AsType (VotingProcedures era)
AsVotingProcedures

emptyVotingProcedures :: VotingProcedures era
emptyVotingProcedures :: forall era. VotingProcedures era
emptyVotingProcedures = VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
forall era.
VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
VotingProcedures (VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era)
-> VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
forall a b. (a -> b) -> a -> b
$ Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> VotingProcedures (ShelleyLedgerEra era)
forall era.
Map
  (Voter (EraCrypto era))
  (Map (GovActionId (EraCrypto era)) (VotingProcedure era))
-> VotingProcedures era
L.VotingProcedures Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
forall k a. Map k a
Map.empty

singletonVotingProcedures
  :: ()
  => ConwayEraOnwards era
  -> L.Voter (L.EraCrypto (ShelleyLedgerEra era))
  -> L.GovActionId (L.EraCrypto (ShelleyLedgerEra era))
  -> L.VotingProcedure (ShelleyLedgerEra era)
  -> VotingProcedures era
singletonVotingProcedures :: forall era.
ConwayEraOnwards era
-> Voter (EraCrypto (ShelleyLedgerEra era))
-> GovActionId (EraCrypto (ShelleyLedgerEra era))
-> VotingProcedure (ShelleyLedgerEra era)
-> VotingProcedures era
singletonVotingProcedures ConwayEraOnwards era
_ Voter (EraCrypto (ShelleyLedgerEra era))
voter GovActionId (EraCrypto (ShelleyLedgerEra era))
govActionId VotingProcedure (ShelleyLedgerEra era)
votingProcedure =
  VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
forall era.
VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
VotingProcedures (VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era)
-> VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
forall a b. (a -> b) -> a -> b
$
    Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> VotingProcedures (ShelleyLedgerEra era)
forall era.
Map
  (Voter (EraCrypto era))
  (Map (GovActionId (EraCrypto era)) (VotingProcedure era))
-> VotingProcedures era
L.VotingProcedures (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> VotingProcedures (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> VotingProcedures (ShelleyLedgerEra era)
forall a b. (a -> b) -> a -> b
$
      Voter (EraCrypto (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. k -> a -> Map k a
Map.singleton Voter (EraCrypto (ShelleyLedgerEra era))
voter (Map
   (GovActionId (EraCrypto (ShelleyLedgerEra era)))
   (VotingProcedure (ShelleyLedgerEra era))
 -> Map
      (Voter (EraCrypto (ShelleyLedgerEra era)))
      (Map
         (GovActionId (EraCrypto (ShelleyLedgerEra era)))
         (VotingProcedure (ShelleyLedgerEra era))))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall a b. (a -> b) -> a -> b
$
        GovActionId (EraCrypto (ShelleyLedgerEra era))
-> VotingProcedure (ShelleyLedgerEra era)
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
forall k a. k -> a -> Map k a
Map.singleton GovActionId (EraCrypto (ShelleyLedgerEra era))
govActionId VotingProcedure (ShelleyLedgerEra era)
votingProcedure

-- | A voter, and the conflicting votes of this voter (i.e. votes with the same governance action identifier)
newtype VotesMergingConflict era
  = VotesMergingConflict
      ( L.Voter (L.EraCrypto (ShelleyLedgerEra era))
      , [L.GovActionId (L.EraCrypto (ShelleyLedgerEra era))]
      )
  deriving Int -> VotesMergingConflict era -> ShowS
[VotesMergingConflict era] -> ShowS
VotesMergingConflict era -> String
(Int -> VotesMergingConflict era -> ShowS)
-> (VotesMergingConflict era -> String)
-> ([VotesMergingConflict era] -> ShowS)
-> Show (VotesMergingConflict era)
forall era. Int -> VotesMergingConflict era -> ShowS
forall era. [VotesMergingConflict era] -> ShowS
forall era. VotesMergingConflict era -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall era. Int -> VotesMergingConflict era -> ShowS
showsPrec :: Int -> VotesMergingConflict era -> ShowS
$cshow :: forall era. VotesMergingConflict era -> String
show :: VotesMergingConflict era -> String
$cshowList :: forall era. [VotesMergingConflict era] -> ShowS
showList :: [VotesMergingConflict era] -> ShowS
Show

-- | @mergeVotingProcedures vote1 vote2@ merges @vote1@ and @vote2@ into a single vote,
-- or fails if the votes are incompatible.
mergeVotingProcedures
  :: ()
  => VotingProcedures era
  -- ^ Votes to merge
  -> VotingProcedures era
  -- ^ Votes to merge
  -> Either (VotesMergingConflict era) (VotingProcedures era)
  -- ^ Either the conflict found, or the merged votes
mergeVotingProcedures :: forall era.
VotingProcedures era
-> VotingProcedures era
-> Either (VotesMergingConflict era) (VotingProcedures era)
mergeVotingProcedures VotingProcedures era
vpsa VotingProcedures era
vpsb =
  VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
forall era.
VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era
VotingProcedures (VotingProcedures (ShelleyLedgerEra era) -> VotingProcedures era)
-> (Map
      (Voter (EraCrypto (ShelleyLedgerEra era)))
      (Map
         (GovActionId (EraCrypto (ShelleyLedgerEra era)))
         (VotingProcedure (ShelleyLedgerEra era)))
    -> VotingProcedures (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> VotingProcedures era
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> VotingProcedures (ShelleyLedgerEra era)
forall era.
Map
  (Voter (EraCrypto era))
  (Map (GovActionId (EraCrypto era)) (VotingProcedure era))
-> VotingProcedures era
L.VotingProcedures (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> VotingProcedures era)
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
-> Either (VotesMergingConflict era) (VotingProcedures era)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> Voter (EraCrypto (ShelleyLedgerEra era))
 -> Either
      (VotesMergingConflict era)
      (Map
         (Voter (EraCrypto (ShelleyLedgerEra era)))
         (Map
            (GovActionId (EraCrypto (ShelleyLedgerEra era)))
            (VotingProcedure (ShelleyLedgerEra era)))))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Set (Voter (EraCrypto (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Voter (EraCrypto (ShelleyLedgerEra era))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
mergeVotesOfOneVoter Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
forall k a. Map k a
Map.empty Set (Voter (EraCrypto (ShelleyLedgerEra era)))
allVoters
 where
  mapa :: Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
mapa = VotingProcedures (ShelleyLedgerEra era)
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall era.
VotingProcedures era
-> Map
     (Voter (EraCrypto era))
     (Map (GovActionId (EraCrypto era)) (VotingProcedure era))
L.unVotingProcedures (VotingProcedures era -> VotingProcedures (ShelleyLedgerEra era)
forall era.
VotingProcedures era -> VotingProcedures (ShelleyLedgerEra era)
unVotingProcedures VotingProcedures era
vpsa)
  mapb :: Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
mapb = VotingProcedures (ShelleyLedgerEra era)
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall era.
VotingProcedures era
-> Map
     (Voter (EraCrypto era))
     (Map (GovActionId (EraCrypto era)) (VotingProcedure era))
L.unVotingProcedures (VotingProcedures era -> VotingProcedures (ShelleyLedgerEra era)
forall era.
VotingProcedures era -> VotingProcedures (ShelleyLedgerEra era)
unVotingProcedures VotingProcedures era
vpsb)
  allVoters :: Set (Voter (EraCrypto (ShelleyLedgerEra era)))
allVoters = Set (Voter (EraCrypto (ShelleyLedgerEra era)))
-> Set (Voter (EraCrypto (ShelleyLedgerEra era)))
-> Set (Voter (EraCrypto (ShelleyLedgerEra era)))
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Set (Voter (EraCrypto (ShelleyLedgerEra era)))
forall k a. Map k a -> Set k
Map.keysSet Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
mapa) (Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Set (Voter (EraCrypto (ShelleyLedgerEra era)))
forall k a. Map k a -> Set k
Map.keysSet Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
mapb)
  mergeVotesOfOneVoter :: Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Voter (EraCrypto (ShelleyLedgerEra era))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
mergeVotesOfOneVoter Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
acc Voter (EraCrypto (ShelleyLedgerEra era))
voter =
    Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
acc (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> Map
      (Voter (EraCrypto (ShelleyLedgerEra era)))
      (Map
         (GovActionId (EraCrypto (ShelleyLedgerEra era)))
         (VotingProcedure (ShelleyLedgerEra era))))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case (Voter (EraCrypto (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Maybe
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Voter (EraCrypto (ShelleyLedgerEra era))
voter Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
mapa, Voter (EraCrypto (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Maybe
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Voter (EraCrypto (ShelleyLedgerEra era))
voter Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
mapb) of
      (Just Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
v, Maybe
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
Nothing) -> Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. b -> Either a b
Right (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> Either
      (VotesMergingConflict era)
      (Map
         (Voter (EraCrypto (ShelleyLedgerEra era)))
         (Map
            (GovActionId (EraCrypto (ShelleyLedgerEra era)))
            (VotingProcedure (ShelleyLedgerEra era)))))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. (a -> b) -> a -> b
$ Voter (EraCrypto (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. k -> a -> Map k a
Map.singleton Voter (EraCrypto (ShelleyLedgerEra era))
voter Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
v -- Take only available value
      (Maybe
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
Nothing, Just Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
v) -> Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. b -> Either a b
Right (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> Either
      (VotesMergingConflict era)
      (Map
         (Voter (EraCrypto (ShelleyLedgerEra era)))
         (Map
            (GovActionId (EraCrypto (ShelleyLedgerEra era)))
            (VotingProcedure (ShelleyLedgerEra era)))))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. (a -> b) -> a -> b
$ Voter (EraCrypto (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. k -> a -> Map k a
Map.singleton Voter (EraCrypto (ShelleyLedgerEra era))
voter Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
v -- Take only available value
      (Maybe
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
Nothing, Maybe
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
Nothing) -> Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. b -> Either a b
Right Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
forall k a. Map k a
Map.empty -- No value
      (Just Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
va, Just Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
vb) ->
        -- Here's the case where we're unioning different votes for the same voter
        if [GovActionId (EraCrypto (ShelleyLedgerEra era))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GovActionId (EraCrypto (ShelleyLedgerEra era))]
intersection -- No conflict: sets of keys from left and right is disjoint
          then Map
  (Voter (EraCrypto (ShelleyLedgerEra era)))
  (Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. b -> Either a b
Right (Map
   (Voter (EraCrypto (ShelleyLedgerEra era)))
   (Map
      (GovActionId (EraCrypto (ShelleyLedgerEra era)))
      (VotingProcedure (ShelleyLedgerEra era)))
 -> Either
      (VotesMergingConflict era)
      (Map
         (Voter (EraCrypto (ShelleyLedgerEra era)))
         (Map
            (GovActionId (EraCrypto (ShelleyLedgerEra era)))
            (VotingProcedure (ShelleyLedgerEra era)))))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. (a -> b) -> a -> b
$ Voter (EraCrypto (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (Voter (EraCrypto (ShelleyLedgerEra era)))
     (Map
        (GovActionId (EraCrypto (ShelleyLedgerEra era)))
        (VotingProcedure (ShelleyLedgerEra era)))
forall k a. k -> a -> Map k a
Map.singleton Voter (EraCrypto (ShelleyLedgerEra era))
voter (Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
va Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
vb)
          else VotesMergingConflict era
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. a -> Either a b
Left (VotesMergingConflict era
 -> Either
      (VotesMergingConflict era)
      (Map
         (Voter (EraCrypto (ShelleyLedgerEra era)))
         (Map
            (GovActionId (EraCrypto (ShelleyLedgerEra era)))
            (VotingProcedure (ShelleyLedgerEra era)))))
-> VotesMergingConflict era
-> Either
     (VotesMergingConflict era)
     (Map
        (Voter (EraCrypto (ShelleyLedgerEra era)))
        (Map
           (GovActionId (EraCrypto (ShelleyLedgerEra era)))
           (VotingProcedure (ShelleyLedgerEra era))))
forall a b. (a -> b) -> a -> b
$ (Voter (EraCrypto (ShelleyLedgerEra era)),
 [GovActionId (EraCrypto (ShelleyLedgerEra era))])
-> VotesMergingConflict era
forall era.
(Voter (EraCrypto (ShelleyLedgerEra era)),
 [GovActionId (EraCrypto (ShelleyLedgerEra era))])
-> VotesMergingConflict era
VotesMergingConflict (Voter (EraCrypto (ShelleyLedgerEra era))
voter, [GovActionId (EraCrypto (ShelleyLedgerEra era))]
intersection) -- Ooops, a conflict! Let's report it!
       where
        intersection :: [GovActionId (EraCrypto (ShelleyLedgerEra era))]
intersection = Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
-> [GovActionId (EraCrypto (ShelleyLedgerEra era))]
forall k a. Map k a -> [k]
Map.keys (Map
   (GovActionId (EraCrypto (ShelleyLedgerEra era)))
   (VotingProcedure (ShelleyLedgerEra era))
 -> [GovActionId (EraCrypto (ShelleyLedgerEra era))])
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> [GovActionId (EraCrypto (ShelleyLedgerEra era))]
forall a b. (a -> b) -> a -> b
$ Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
-> Map
     (GovActionId (EraCrypto (ShelleyLedgerEra era)))
     (VotingProcedure (ShelleyLedgerEra era))
forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.intersection Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
va Map
  (GovActionId (EraCrypto (ShelleyLedgerEra era)))
  (VotingProcedure (ShelleyLedgerEra era))
vb