-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

-- | Private immutable governance representations and CBOR schemas.
-- No native object is retained. Public modules select the domain operations;
-- parser workers remain private to this module.
module Cometa.Internal.Governance
  ( Url,
    mkUrl,
    urlText,
    DataHash,
    dataHashFromBytes,
    dataHashBytes,
    Anchor (..),
    encodeAnchor,
    decodeAnchor,
    ActionId (..),
    encodeActionId,
    decodeActionId,
    DRep (..),
    encodeDRep,
    decodeDRep,
    Vote (..),
    Voter (..),
    VotingProcedure (..),
    VotingProcedures,
    empty,
    singleton,
    fromList,
    toList,
    insert,
    lookup,
    merge,
    EncodedVotingProcedures,
    encodeVoting,
    decodeVoting,
    fromVotingProcedures,
    toVotingProcedures,
    toCbor,
    encodeVoter,
    decodeVoter,
    encodeProcedure,
    decodeProcedure,
  )
where

-- IMPORTS --------------------------------------------------------------------

import qualified Codec.CBOR.Decoding as Decode
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Read as Read
import qualified Codec.CBOR.Write as Write
import qualified Cometa.Credential as Credential
import Cometa.Error (Error (..))
import Cometa.TransactionId (TransactionId)
import qualified Cometa.TransactionId as TransactionId
import Control.Monad (foldM, replicateM, unless, when)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Word (Word64)
import Prelude hiding (lookup)

-- TYPES ----------------------------------------------------------------------

-- | Metadata location containing 1 through 128 UTF-8 bytes and no NUL.
-- No URI syntax, network reachability, or payload validation is implied.
-- Equality and ordering compare text. 'Show' is diagnostic.
newtype Url = Url Text deriving (Url -> Url -> Bool
(Url -> Url -> Bool) -> (Url -> Url -> Bool) -> Eq Url
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Url -> Url -> Bool
== :: Url -> Url -> Bool
$c/= :: Url -> Url -> Bool
/= :: Url -> Url -> Bool
Eq, Eq Url
Eq Url =>
(Url -> Url -> Ordering)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Url)
-> (Url -> Url -> Url)
-> Ord Url
Url -> Url -> Bool
Url -> Url -> Ordering
Url -> Url -> Url
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
$ccompare :: Url -> Url -> Ordering
compare :: Url -> Url -> Ordering
$c< :: Url -> Url -> Bool
< :: Url -> Url -> Bool
$c<= :: Url -> Url -> Bool
<= :: Url -> Url -> Bool
$c> :: Url -> Url -> Bool
> :: Url -> Url -> Bool
$c>= :: Url -> Url -> Bool
>= :: Url -> Url -> Bool
$cmax :: Url -> Url -> Url
max :: Url -> Url -> Url
$cmin :: Url -> Url -> Url
min :: Url -> Url -> Url
Ord, Int -> Url -> ShowS
[Url] -> ShowS
Url -> String
(Int -> Url -> ShowS)
-> (Url -> String) -> ([Url] -> ShowS) -> Show Url
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Url -> ShowS
showsPrec :: Int -> Url -> ShowS
$cshow :: Url -> String
show :: Url -> String
$cshowList :: [Url] -> ShowS
showList :: [Url] -> ShowS
Show)

-- | An exact 32-byte BLAKE2b-256 digest of off-chain metadata bytes.
-- Importing bytes does not verify metadata. Equality and ordering compare bytes.
newtype DataHash = DataHash ByteString deriving (DataHash -> DataHash -> Bool
(DataHash -> DataHash -> Bool)
-> (DataHash -> DataHash -> Bool) -> Eq DataHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DataHash -> DataHash -> Bool
== :: DataHash -> DataHash -> Bool
$c/= :: DataHash -> DataHash -> Bool
/= :: DataHash -> DataHash -> Bool
Eq, Eq DataHash
Eq DataHash =>
(DataHash -> DataHash -> Ordering)
-> (DataHash -> DataHash -> Bool)
-> (DataHash -> DataHash -> Bool)
-> (DataHash -> DataHash -> Bool)
-> (DataHash -> DataHash -> Bool)
-> (DataHash -> DataHash -> DataHash)
-> (DataHash -> DataHash -> DataHash)
-> Ord DataHash
DataHash -> DataHash -> Bool
DataHash -> DataHash -> Ordering
DataHash -> DataHash -> DataHash
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
$ccompare :: DataHash -> DataHash -> Ordering
compare :: DataHash -> DataHash -> Ordering
$c< :: DataHash -> DataHash -> Bool
< :: DataHash -> DataHash -> Bool
$c<= :: DataHash -> DataHash -> Bool
<= :: DataHash -> DataHash -> Bool
$c> :: DataHash -> DataHash -> Bool
> :: DataHash -> DataHash -> Bool
$c>= :: DataHash -> DataHash -> Bool
>= :: DataHash -> DataHash -> Bool
$cmax :: DataHash -> DataHash -> DataHash
max :: DataHash -> DataHash -> DataHash
$cmin :: DataHash -> DataHash -> DataHash
min :: DataHash -> DataHash -> DataHash
Ord, Int -> DataHash -> ShowS
[DataHash] -> ShowS
DataHash -> String
(Int -> DataHash -> ShowS)
-> (DataHash -> String) -> ([DataHash] -> ShowS) -> Show DataHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DataHash -> ShowS
showsPrec :: Int -> DataHash -> ShowS
$cshow :: DataHash -> String
show :: DataHash -> String
$cshowList :: [DataHash] -> ShowS
showList :: [DataHash] -> ShowS
Show)

-- | A metadata location and the expected hash of its payload. Assembly is total
-- because both components are validated. A location does not authenticate data
-- unless the retrieved bytes are checked against the hash.
data Anchor = Anchor
  { -- | Off-chain metadata location.
    Anchor -> Url
url :: !Url,
    -- | Expected digest of the exact off-chain payload bytes.
    Anchor -> DataHash
dataHash :: !DataHash
  }
  deriving (Anchor -> Anchor -> Bool
(Anchor -> Anchor -> Bool)
-> (Anchor -> Anchor -> Bool) -> Eq Anchor
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Anchor -> Anchor -> Bool
== :: Anchor -> Anchor -> Bool
$c/= :: Anchor -> Anchor -> Bool
/= :: Anchor -> Anchor -> Bool
Eq, Eq Anchor
Eq Anchor =>
(Anchor -> Anchor -> Ordering)
-> (Anchor -> Anchor -> Bool)
-> (Anchor -> Anchor -> Bool)
-> (Anchor -> Anchor -> Bool)
-> (Anchor -> Anchor -> Bool)
-> (Anchor -> Anchor -> Anchor)
-> (Anchor -> Anchor -> Anchor)
-> Ord Anchor
Anchor -> Anchor -> Bool
Anchor -> Anchor -> Ordering
Anchor -> Anchor -> Anchor
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
$ccompare :: Anchor -> Anchor -> Ordering
compare :: Anchor -> Anchor -> Ordering
$c< :: Anchor -> Anchor -> Bool
< :: Anchor -> Anchor -> Bool
$c<= :: Anchor -> Anchor -> Bool
<= :: Anchor -> Anchor -> Bool
$c> :: Anchor -> Anchor -> Bool
> :: Anchor -> Anchor -> Bool
$c>= :: Anchor -> Anchor -> Bool
>= :: Anchor -> Anchor -> Bool
$cmax :: Anchor -> Anchor -> Anchor
max :: Anchor -> Anchor -> Anchor
$cmin :: Anchor -> Anchor -> Anchor
min :: Anchor -> Anchor -> Anchor
Ord, Int -> Anchor -> ShowS
[Anchor] -> ShowS
Anchor -> String
(Int -> Anchor -> ShowS)
-> (Anchor -> String) -> ([Anchor] -> ShowS) -> Show Anchor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Anchor -> ShowS
showsPrec :: Int -> Anchor -> ShowS
$cshow :: Anchor -> String
show :: Anchor -> String
$cshowList :: [Anchor] -> ShowS
showList :: [Anchor] -> ShowS
Show)

-- | A proposal's identity: the transaction containing it and its zero-based
-- index within that transaction. The standalone native schema permits the full
-- unsigned 64-bit index range. This does not establish ledger acceptance of an
-- index in a particular era. Ordering compares transaction bytes, then index.
data ActionId = ActionId
  { -- | Transaction-body hash of the transaction containing the proposal.
    ActionId -> TransactionId
transactionId :: !TransactionId,
    -- | Zero-based proposal index, from 0 through 2^64 - 1.
    ActionId -> Word64
index :: !Word64
  }
  deriving (ActionId -> ActionId -> Bool
(ActionId -> ActionId -> Bool)
-> (ActionId -> ActionId -> Bool) -> Eq ActionId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ActionId -> ActionId -> Bool
== :: ActionId -> ActionId -> Bool
$c/= :: ActionId -> ActionId -> Bool
/= :: ActionId -> ActionId -> Bool
Eq, Eq ActionId
Eq ActionId =>
(ActionId -> ActionId -> Ordering)
-> (ActionId -> ActionId -> Bool)
-> (ActionId -> ActionId -> Bool)
-> (ActionId -> ActionId -> Bool)
-> (ActionId -> ActionId -> Bool)
-> (ActionId -> ActionId -> ActionId)
-> (ActionId -> ActionId -> ActionId)
-> Ord ActionId
ActionId -> ActionId -> Bool
ActionId -> ActionId -> Ordering
ActionId -> ActionId -> ActionId
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
$ccompare :: ActionId -> ActionId -> Ordering
compare :: ActionId -> ActionId -> Ordering
$c< :: ActionId -> ActionId -> Bool
< :: ActionId -> ActionId -> Bool
$c<= :: ActionId -> ActionId -> Bool
<= :: ActionId -> ActionId -> Bool
$c> :: ActionId -> ActionId -> Bool
> :: ActionId -> ActionId -> Bool
$c>= :: ActionId -> ActionId -> Bool
>= :: ActionId -> ActionId -> Bool
$cmax :: ActionId -> ActionId -> ActionId
max :: ActionId -> ActionId -> ActionId
$cmin :: ActionId -> ActionId -> ActionId
min :: ActionId -> ActionId -> ActionId
Ord, Int -> ActionId -> ShowS
[ActionId] -> ShowS
ActionId -> String
(Int -> ActionId -> ShowS)
-> (ActionId -> String) -> ([ActionId] -> ShowS) -> Show ActionId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ActionId -> ShowS
showsPrec :: Int -> ActionId -> ShowS
$cshow :: ActionId -> String
show :: ActionId -> String
$cshowList :: [ActionId] -> ShowS
showList :: [ActionId] -> ShowS
Show)

-- | A delegation target. The two special targets are not voting authorities;
-- only a credential can identify a DRep voter.
data DRep
  = -- | A key or script DRep credential.
    DRepCredential !(Credential.Credential Credential.DRep)
  | -- | Delegate to the predefined always-abstain target.
    AlwaysAbstain
  | -- | Delegate to the predefined always-no-confidence target.
    AlwaysNoConfidence
  deriving (DRep -> DRep -> Bool
(DRep -> DRep -> Bool) -> (DRep -> DRep -> Bool) -> Eq DRep
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DRep -> DRep -> Bool
== :: DRep -> DRep -> Bool
$c/= :: DRep -> DRep -> Bool
/= :: DRep -> DRep -> Bool
Eq, Eq DRep
Eq DRep =>
(DRep -> DRep -> Ordering)
-> (DRep -> DRep -> Bool)
-> (DRep -> DRep -> Bool)
-> (DRep -> DRep -> Bool)
-> (DRep -> DRep -> Bool)
-> (DRep -> DRep -> DRep)
-> (DRep -> DRep -> DRep)
-> Ord DRep
DRep -> DRep -> Bool
DRep -> DRep -> Ordering
DRep -> DRep -> DRep
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
$ccompare :: DRep -> DRep -> Ordering
compare :: DRep -> DRep -> Ordering
$c< :: DRep -> DRep -> Bool
< :: DRep -> DRep -> Bool
$c<= :: DRep -> DRep -> Bool
<= :: DRep -> DRep -> Bool
$c> :: DRep -> DRep -> Bool
> :: DRep -> DRep -> Bool
$c>= :: DRep -> DRep -> Bool
>= :: DRep -> DRep -> Bool
$cmax :: DRep -> DRep -> DRep
max :: DRep -> DRep -> DRep
$cmin :: DRep -> DRep -> DRep
min :: DRep -> DRep -> DRep
Ord, Int -> DRep -> ShowS
[DRep] -> ShowS
DRep -> String
(Int -> DRep -> ShowS)
-> (DRep -> String) -> ([DRep] -> ShowS) -> Show DRep
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DRep -> ShowS
showsPrec :: Int -> DRep -> ShowS
$cshow :: DRep -> String
show :: DRep -> String
$cshowList :: [DRep] -> ShowS
showList :: [DRep] -> ShowS
Show)

-- | A governance voting choice. Ordering follows CBOR codes: No=0, Yes=1,
-- Abstain=2. Abstention is an explicit choice, distinct from an absent vote.
data Vote = No | Yes | Abstain deriving (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, Eq Vote
Eq Vote =>
(Vote -> Vote -> Ordering)
-> (Vote -> Vote -> Bool)
-> (Vote -> Vote -> Bool)
-> (Vote -> Vote -> Bool)
-> (Vote -> Vote -> Bool)
-> (Vote -> Vote -> Vote)
-> (Vote -> Vote -> Vote)
-> Ord Vote
Vote -> Vote -> Bool
Vote -> Vote -> Ordering
Vote -> Vote -> Vote
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
$ccompare :: Vote -> Vote -> Ordering
compare :: Vote -> Vote -> Ordering
$c< :: Vote -> Vote -> Bool
< :: Vote -> Vote -> Bool
$c<= :: Vote -> Vote -> Bool
<= :: Vote -> Vote -> Bool
$c> :: Vote -> Vote -> Bool
> :: Vote -> Vote -> Bool
$c>= :: Vote -> Vote -> Bool
>= :: Vote -> Vote -> Bool
$cmax :: Vote -> Vote -> Vote
max :: Vote -> Vote -> Vote
$cmin :: Vote -> Vote -> Vote
min :: Vote -> Vote -> Vote
Ord, 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 -> Vote -> Bounded Vote
forall a. a -> a -> Bounded a
$cminBound :: Vote
minBound :: Vote
$cmaxBound :: Vote
maxBound :: Vote
Bounded, Int -> Vote
Vote -> Int
Vote -> [Vote]
Vote -> Vote
Vote -> Vote -> [Vote]
Vote -> Vote -> Vote -> [Vote]
(Vote -> Vote)
-> (Vote -> Vote)
-> (Int -> Vote)
-> (Vote -> Int)
-> (Vote -> [Vote])
-> (Vote -> Vote -> [Vote])
-> (Vote -> Vote -> [Vote])
-> (Vote -> Vote -> Vote -> [Vote])
-> Enum Vote
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Vote -> Vote
succ :: Vote -> Vote
$cpred :: Vote -> Vote
pred :: Vote -> Vote
$ctoEnum :: Int -> Vote
toEnum :: Int -> Vote
$cfromEnum :: Vote -> Int
fromEnum :: Vote -> Int
$cenumFrom :: Vote -> [Vote]
enumFrom :: Vote -> [Vote]
$cenumFromThen :: Vote -> Vote -> [Vote]
enumFromThen :: Vote -> Vote -> [Vote]
$cenumFromTo :: Vote -> Vote -> [Vote]
enumFromTo :: Vote -> Vote -> [Vote]
$cenumFromThenTo :: Vote -> Vote -> Vote -> [Vote]
enumFromThenTo :: Vote -> Vote -> Vote -> [Vote]
Enum)

-- | Authority casting a vote. Committee votes use the hot credential;
-- stake pools have key authorities only. Ordering follows native voter codes
-- (committee key/script, DRep key/script, pool key), then exact hash bytes.
data Voter
  = -- | Constitutional committee hot key or script.
    CommitteeVoter !(Credential.Credential Credential.CommitteeHot)
  | -- | DRep key or script; special delegation targets cannot vote.
    DRepVoter !(Credential.Credential Credential.DRep)
  | -- | Stake pool verification-key hash.
    StakePoolVoter !(Credential.KeyHash Credential.StakePool)
  deriving (Voter -> Voter -> Bool
(Voter -> Voter -> Bool) -> (Voter -> Voter -> Bool) -> Eq Voter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Voter -> Voter -> Bool
== :: Voter -> Voter -> Bool
$c/= :: Voter -> Voter -> Bool
/= :: Voter -> Voter -> Bool
Eq, Eq Voter
Eq Voter =>
(Voter -> Voter -> Ordering)
-> (Voter -> Voter -> Bool)
-> (Voter -> Voter -> Bool)
-> (Voter -> Voter -> Bool)
-> (Voter -> Voter -> Bool)
-> (Voter -> Voter -> Voter)
-> (Voter -> Voter -> Voter)
-> Ord Voter
Voter -> Voter -> Bool
Voter -> Voter -> Ordering
Voter -> Voter -> Voter
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
$ccompare :: Voter -> Voter -> Ordering
compare :: Voter -> Voter -> Ordering
$c< :: Voter -> Voter -> Bool
< :: Voter -> Voter -> Bool
$c<= :: Voter -> Voter -> Bool
<= :: Voter -> Voter -> Bool
$c> :: Voter -> Voter -> Bool
> :: Voter -> Voter -> Bool
$c>= :: Voter -> Voter -> Bool
>= :: Voter -> Voter -> Bool
$cmax :: Voter -> Voter -> Voter
max :: Voter -> Voter -> Voter
$cmin :: Voter -> Voter -> Voter
min :: Voter -> Voter -> Voter
Ord, Int -> Voter -> ShowS
[Voter] -> ShowS
Voter -> String
(Int -> Voter -> ShowS)
-> (Voter -> String) -> ([Voter] -> ShowS) -> Show Voter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Voter -> ShowS
showsPrec :: Int -> Voter -> ShowS
$cshow :: Voter -> String
show :: Voter -> String
$cshowList :: [Voter] -> ShowS
showList :: [Voter] -> ShowS
Show)

-- | A voting choice and optional explanation. 'Nothing' encodes as CBOR null;
-- a present anchor carries a validated metadata location and payload hash.
data VotingProcedure = VotingProcedure
  { -- | Explicit choice on the proposal.
    VotingProcedure -> Vote
vote :: !Vote,
    -- | Optional off-chain explanation.
    VotingProcedure -> Maybe Anchor
anchor :: !(Maybe Anchor)
  }
  deriving (VotingProcedure -> VotingProcedure -> Bool
(VotingProcedure -> VotingProcedure -> Bool)
-> (VotingProcedure -> VotingProcedure -> Bool)
-> Eq VotingProcedure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VotingProcedure -> VotingProcedure -> Bool
== :: VotingProcedure -> VotingProcedure -> Bool
$c/= :: VotingProcedure -> VotingProcedure -> Bool
/= :: VotingProcedure -> VotingProcedure -> Bool
Eq, Eq VotingProcedure
Eq VotingProcedure =>
(VotingProcedure -> VotingProcedure -> Ordering)
-> (VotingProcedure -> VotingProcedure -> Bool)
-> (VotingProcedure -> VotingProcedure -> Bool)
-> (VotingProcedure -> VotingProcedure -> Bool)
-> (VotingProcedure -> VotingProcedure -> Bool)
-> (VotingProcedure -> VotingProcedure -> VotingProcedure)
-> (VotingProcedure -> VotingProcedure -> VotingProcedure)
-> Ord VotingProcedure
VotingProcedure -> VotingProcedure -> Bool
VotingProcedure -> VotingProcedure -> Ordering
VotingProcedure -> VotingProcedure -> VotingProcedure
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
$ccompare :: VotingProcedure -> VotingProcedure -> Ordering
compare :: VotingProcedure -> VotingProcedure -> Ordering
$c< :: VotingProcedure -> VotingProcedure -> Bool
< :: VotingProcedure -> VotingProcedure -> Bool
$c<= :: VotingProcedure -> VotingProcedure -> Bool
<= :: VotingProcedure -> VotingProcedure -> Bool
$c> :: VotingProcedure -> VotingProcedure -> Bool
> :: VotingProcedure -> VotingProcedure -> Bool
$c>= :: VotingProcedure -> VotingProcedure -> Bool
>= :: VotingProcedure -> VotingProcedure -> Bool
$cmax :: VotingProcedure -> VotingProcedure -> VotingProcedure
max :: VotingProcedure -> VotingProcedure -> VotingProcedure
$cmin :: VotingProcedure -> VotingProcedure -> VotingProcedure
min :: VotingProcedure -> VotingProcedure -> VotingProcedure
Ord, Int -> VotingProcedure -> ShowS
[VotingProcedure] -> ShowS
VotingProcedure -> String
(Int -> VotingProcedure -> ShowS)
-> (VotingProcedure -> String)
-> ([VotingProcedure] -> ShowS)
-> Show VotingProcedure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VotingProcedure -> ShowS
showsPrec :: Int -> VotingProcedure -> ShowS
$cshow :: VotingProcedure -> String
show :: VotingProcedure -> String
$cshowList :: [VotingProcedure] -> ShowS
showList :: [VotingProcedure] -> ShowS
Show)

-- | At most one procedure for each voter/action pair. Empty is valid; stored
-- voters always have at least one procedure. Equality is semantic; ordering
-- compares sorted voter/action entries and their procedures. 'Show' is diagnostic.
newtype VotingProcedures = VotingProcedures (Map Voter (Map ActionId VotingProcedure))
  deriving (VotingProcedures -> VotingProcedures -> Bool
(VotingProcedures -> VotingProcedures -> Bool)
-> (VotingProcedures -> VotingProcedures -> Bool)
-> Eq VotingProcedures
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VotingProcedures -> VotingProcedures -> Bool
== :: VotingProcedures -> VotingProcedures -> Bool
$c/= :: VotingProcedures -> VotingProcedures -> Bool
/= :: VotingProcedures -> VotingProcedures -> Bool
Eq, Eq VotingProcedures
Eq VotingProcedures =>
(VotingProcedures -> VotingProcedures -> Ordering)
-> (VotingProcedures -> VotingProcedures -> Bool)
-> (VotingProcedures -> VotingProcedures -> Bool)
-> (VotingProcedures -> VotingProcedures -> Bool)
-> (VotingProcedures -> VotingProcedures -> Bool)
-> (VotingProcedures -> VotingProcedures -> VotingProcedures)
-> (VotingProcedures -> VotingProcedures -> VotingProcedures)
-> Ord VotingProcedures
VotingProcedures -> VotingProcedures -> Bool
VotingProcedures -> VotingProcedures -> Ordering
VotingProcedures -> VotingProcedures -> VotingProcedures
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
$ccompare :: VotingProcedures -> VotingProcedures -> Ordering
compare :: VotingProcedures -> VotingProcedures -> Ordering
$c< :: VotingProcedures -> VotingProcedures -> Bool
< :: VotingProcedures -> VotingProcedures -> Bool
$c<= :: VotingProcedures -> VotingProcedures -> Bool
<= :: VotingProcedures -> VotingProcedures -> Bool
$c> :: VotingProcedures -> VotingProcedures -> Bool
> :: VotingProcedures -> VotingProcedures -> Bool
$c>= :: VotingProcedures -> VotingProcedures -> Bool
>= :: VotingProcedures -> VotingProcedures -> Bool
$cmax :: VotingProcedures -> VotingProcedures -> VotingProcedures
max :: VotingProcedures -> VotingProcedures -> VotingProcedures
$cmin :: VotingProcedures -> VotingProcedures -> VotingProcedures
min :: VotingProcedures -> VotingProcedures -> VotingProcedures
Ord, Int -> VotingProcedures -> ShowS
[VotingProcedures] -> ShowS
VotingProcedures -> String
(Int -> VotingProcedures -> ShowS)
-> (VotingProcedures -> String)
-> ([VotingProcedures] -> ShowS)
-> Show VotingProcedures
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VotingProcedures -> ShowS
showsPrec :: Int -> VotingProcedures -> ShowS
$cshow :: VotingProcedures -> String
show :: VotingProcedures -> String
$cshowList :: [VotingProcedures] -> ShowS
showList :: [VotingProcedures] -> ShowS
Show)

-- | Voting data with its exact original CBOR encoding. Equality and ordering
-- compare encoded bytes; 'toVotingProcedures' permits semantic comparison.
-- Imported map order and nonminimal encodings remain observable through 'toCbor'.
data EncodedVotingProcedures = EncodedVotingProcedures !ByteString !VotingProcedures
  deriving (Int -> EncodedVotingProcedures -> ShowS
[EncodedVotingProcedures] -> ShowS
EncodedVotingProcedures -> String
(Int -> EncodedVotingProcedures -> ShowS)
-> (EncodedVotingProcedures -> String)
-> ([EncodedVotingProcedures] -> ShowS)
-> Show EncodedVotingProcedures
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedVotingProcedures -> ShowS
showsPrec :: Int -> EncodedVotingProcedures -> ShowS
$cshow :: EncodedVotingProcedures -> String
show :: EncodedVotingProcedures -> String
$cshowList :: [EncodedVotingProcedures] -> ShowS
showList :: [EncodedVotingProcedures] -> ShowS
Show)

instance Eq EncodedVotingProcedures where
  EncodedVotingProcedures
left == :: EncodedVotingProcedures -> EncodedVotingProcedures -> Bool
== EncodedVotingProcedures
right = EncodedVotingProcedures -> ByteString
toCbor EncodedVotingProcedures
left ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== EncodedVotingProcedures -> ByteString
toCbor EncodedVotingProcedures
right

instance Ord EncodedVotingProcedures where
  compare :: EncodedVotingProcedures -> EncodedVotingProcedures -> Ordering
compare EncodedVotingProcedures
left EncodedVotingProcedures
right = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (EncodedVotingProcedures -> ByteString
toCbor EncodedVotingProcedures
left) (EncodedVotingProcedures -> ByteString
toCbor EncodedVotingProcedures
right)

-- DEFINITIONS ----------------------------------------------------------------

-- | Validate 1 through 128 UTF-8 bytes and absence of NUL. Bad length returns
-- 'InvalidAnchorUrlLength'; NUL returns 'InvalidAnchorUrl'. No URI parsing or
-- network request occurs.
mkUrl :: Text -> Either Error Url
mkUrl :: Text -> Either Error Url
mkUrl Text
value
  | Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
128 = Error -> Either Error Url
forall a b. a -> Either a b
Left (Int -> Error
InvalidAnchorUrlLength Int
size)
  | (Char -> Bool) -> Text -> Bool
Text.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\0') Text
value = Error -> Either Error Url
forall a b. a -> Either a b
Left Error
InvalidAnchorUrl
  | Bool
otherwise = Url -> Either Error Url
forall a b. b -> Either a b
Right (Text -> Url
Url Text
value)
  where
    size :: Int
size = ByteString -> Int
ByteString.length (Text -> ByteString
Text.encodeUtf8 Text
value)

-- | Exact validated location text.
urlText :: Url -> Text
urlText :: Url -> Text
urlText (Url Text
value) = Text
value

-- | Import exactly 32 bytes without hashing. Another length returns
-- 'InvalidAnchorDataHashLength' with the actual byte count.
dataHashFromBytes :: ByteString -> Either Error DataHash
dataHashFromBytes :: ByteString -> Either Error DataHash
dataHashFromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = DataHash -> Either Error DataHash
forall a b. b -> Either a b
Right (ByteString -> DataHash
DataHash ByteString
bytes)
  | Bool
otherwise = Error -> Either Error DataHash
forall a b. a -> Either a b
Left (Int -> Error
InvalidAnchorDataHashLength (ByteString -> Int
ByteString.length ByteString
bytes))

-- | Exact expected payload hash bytes.
dataHashBytes :: DataHash -> ByteString
dataHashBytes :: DataHash -> ByteString
dataHashBytes (DataHash ByteString
bytes) = ByteString
bytes

-- | No votes.
empty :: VotingProcedures
empty :: VotingProcedures
empty = Map Voter (Map ActionId VotingProcedure) -> VotingProcedures
VotingProcedures Map Voter (Map ActionId VotingProcedure)
forall k a. Map k a
Map.empty

-- | One voter casting one procedure on one action. All arguments are validated
-- domain values, so assembly cannot fail.
singleton :: Voter -> ActionId -> VotingProcedure -> VotingProcedures
singleton :: Voter -> ActionId -> VotingProcedure -> VotingProcedures
singleton Voter
voter ActionId
action VotingProcedure
procedure = Map Voter (Map ActionId VotingProcedure) -> VotingProcedures
VotingProcedures (Voter
-> Map ActionId VotingProcedure
-> Map Voter (Map ActionId VotingProcedure)
forall k a. k -> a -> Map k a
Map.singleton Voter
voter (ActionId -> VotingProcedure -> Map ActionId VotingProcedure
forall k a. k -> a -> Map k a
Map.singleton ActionId
action VotingProcedure
procedure))

-- | Assemble triples in any order. Repeating a voter/action pair returns
-- 'DuplicateGovernanceVote', even when the procedures agree. No vote is discarded.
fromList :: [(Voter, ActionId, VotingProcedure)] -> Either Error VotingProcedures
fromList :: [(Voter, ActionId, VotingProcedure)]
-> Either Error VotingProcedures
fromList = (VotingProcedures
 -> (Voter, ActionId, VotingProcedure)
 -> Either Error VotingProcedures)
-> VotingProcedures
-> [(Voter, ActionId, VotingProcedure)]
-> Either Error VotingProcedures
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (\VotingProcedures
votes (Voter
voter, ActionId
action, VotingProcedure
procedure) -> Voter
-> ActionId
-> VotingProcedure
-> VotingProcedures
-> Either Error VotingProcedures
insert Voter
voter ActionId
action VotingProcedure
procedure VotingProcedures
votes) VotingProcedures
empty

-- | Entries sorted by voter kind/hash, then transaction hash/index. Input
-- insertion order is not retained by this semantic collection.
toList :: VotingProcedures -> [(Voter, ActionId, VotingProcedure)]
toList :: VotingProcedures -> [(Voter, ActionId, VotingProcedure)]
toList (VotingProcedures Map Voter (Map ActionId VotingProcedure)
voters) =
  [(Voter
voter, ActionId
action, VotingProcedure
procedure) | (Voter
voter, Map ActionId VotingProcedure
actions) <- Map Voter (Map ActionId VotingProcedure)
-> [(Voter, Map ActionId VotingProcedure)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map Voter (Map ActionId VotingProcedure)
voters, (ActionId
action, VotingProcedure
procedure) <- Map ActionId VotingProcedure -> [(ActionId, VotingProcedure)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map ActionId VotingProcedure
actions]

-- | Add a vote, rejecting an existing voter/action pair with
-- 'DuplicateGovernanceVote'. Other votes remain unchanged.
insert :: Voter -> ActionId -> VotingProcedure -> VotingProcedures -> Either Error VotingProcedures
insert :: Voter
-> ActionId
-> VotingProcedure
-> VotingProcedures
-> Either Error VotingProcedures
insert Voter
voter ActionId
action VotingProcedure
procedure (VotingProcedures Map Voter (Map ActionId VotingProcedure)
voters)
  | ActionId -> Map ActionId VotingProcedure -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member ActionId
action Map ActionId VotingProcedure
actions = Error -> Either Error VotingProcedures
forall a b. a -> Either a b
Left Error
DuplicateGovernanceVote
  | Bool
otherwise = VotingProcedures -> Either Error VotingProcedures
forall a b. b -> Either a b
Right (Map Voter (Map ActionId VotingProcedure) -> VotingProcedures
VotingProcedures (Voter
-> Map ActionId VotingProcedure
-> Map Voter (Map ActionId VotingProcedure)
-> Map Voter (Map ActionId VotingProcedure)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Voter
voter (ActionId
-> VotingProcedure
-> Map ActionId VotingProcedure
-> Map ActionId VotingProcedure
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ActionId
action VotingProcedure
procedure Map ActionId VotingProcedure
actions) Map Voter (Map ActionId VotingProcedure)
voters))
  where
    actions :: Map ActionId VotingProcedure
actions = Map ActionId VotingProcedure
-> Voter
-> Map Voter (Map ActionId VotingProcedure)
-> Map ActionId VotingProcedure
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Map ActionId VotingProcedure
forall k a. Map k a
Map.empty Voter
voter Map Voter (Map ActionId VotingProcedure)
voters

-- | Find the procedure for a voter/action pair; absence is legitimate.
lookup :: Voter -> ActionId -> VotingProcedures -> Maybe VotingProcedure
lookup :: Voter -> ActionId -> VotingProcedures -> Maybe VotingProcedure
lookup Voter
voter ActionId
action (VotingProcedures Map Voter (Map ActionId VotingProcedure)
voters) = Voter
-> Map Voter (Map ActionId VotingProcedure)
-> Maybe (Map ActionId VotingProcedure)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Voter
voter Map Voter (Map ActionId VotingProcedure)
voters Maybe (Map ActionId VotingProcedure)
-> (Map ActionId VotingProcedure -> Maybe VotingProcedure)
-> Maybe VotingProcedure
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ActionId -> Map ActionId VotingProcedure -> Maybe VotingProcedure
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ActionId
action

-- | Combine disjoint voter/action pairs. Any overlap returns
-- 'DuplicateGovernanceVote', even when both votes agree. Successful merge is
-- associative and commutative, with 'empty' as its identity.
merge :: VotingProcedures -> VotingProcedures -> Either Error VotingProcedures
merge :: VotingProcedures
-> VotingProcedures -> Either Error VotingProcedures
merge VotingProcedures
left VotingProcedures
right = (VotingProcedures
 -> (Voter, ActionId, VotingProcedure)
 -> Either Error VotingProcedures)
-> VotingProcedures
-> [(Voter, ActionId, VotingProcedure)]
-> Either Error VotingProcedures
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (\VotingProcedures
votes (Voter
voter, ActionId
action, VotingProcedure
procedure) -> Voter
-> ActionId
-> VotingProcedure
-> VotingProcedures
-> Either Error VotingProcedures
insert Voter
voter ActionId
action VotingProcedure
procedure VotingProcedures
votes) VotingProcedures
left (VotingProcedures -> [(Voter, ActionId, VotingProcedure)]
toList VotingProcedures
right)

encodeAnchor :: Anchor -> ByteString
encodeAnchor :: Anchor -> ByteString
encodeAnchor = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (Anchor -> Encoding) -> Anchor -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Anchor -> Encoding
anchorEncoding

decodeAnchor :: ByteString -> Either Error Anchor
decodeAnchor :: ByteString -> Either Error Anchor
decodeAnchor = (forall s. Decoder s Anchor) -> ByteString -> Either Error Anchor
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s Anchor
forall s. Decoder s Anchor
anchorDecoder

encodeActionId :: ActionId -> ByteString
encodeActionId :: ActionId -> ByteString
encodeActionId = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (ActionId -> Encoding) -> ActionId -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ActionId -> Encoding
actionEncoding

decodeActionId :: ByteString -> Either Error ActionId
decodeActionId :: ByteString -> Either Error ActionId
decodeActionId = (forall s. Decoder s ActionId)
-> ByteString -> Either Error ActionId
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s ActionId
forall s. Decoder s ActionId
actionDecoder

encodeDRep :: DRep -> ByteString
encodeDRep :: DRep -> ByteString
encodeDRep = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (DRep -> Encoding) -> DRep -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DRep -> Encoding
drepEncoding

decodeDRep :: ByteString -> Either Error DRep
decodeDRep :: ByteString -> Either Error DRep
decodeDRep = (forall s. Decoder s DRep) -> ByteString -> Either Error DRep
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s DRep
forall s. Decoder s DRep
drepDecoder

encodeVoter :: Voter -> ByteString
encodeVoter :: Voter -> ByteString
encodeVoter = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (Voter -> Encoding) -> Voter -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Voter -> Encoding
voterEncoding

decodeVoter :: ByteString -> Either Error Voter
decodeVoter :: ByteString -> Either Error Voter
decodeVoter = (forall s. Decoder s Voter) -> ByteString -> Either Error Voter
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s Voter
forall s. Decoder s Voter
voterDecoder

encodeProcedure :: VotingProcedure -> ByteString
encodeProcedure :: VotingProcedure -> ByteString
encodeProcedure = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (VotingProcedure -> Encoding) -> VotingProcedure -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VotingProcedure -> Encoding
procedureEncoding

decodeProcedure :: ByteString -> Either Error VotingProcedure
decodeProcedure :: ByteString -> Either Error VotingProcedure
decodeProcedure = (forall s. Decoder s VotingProcedure)
-> ByteString -> Either Error VotingProcedure
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s VotingProcedure
forall s. Decoder s VotingProcedure
procedureDecoder

-- | Serialize fresh data in native voter/hash/index order with definite maps,
-- definite arrays and shortest integer encodings. Re-encoding semantic data
-- inspected from external CBOR can change transaction identity; use 'toCbor'
-- on the encoded wrapper to retain imported bytes.
encodeVoting :: VotingProcedures -> ByteString
encodeVoting :: VotingProcedures -> ByteString
encodeVoting = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (VotingProcedures -> Encoding) -> VotingProcedures -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VotingProcedures -> Encoding
votingEncoding

-- | Decode exactly one complete nested voting map. Definite and indefinite
-- containers and nonminimal unsigned integers are accepted. Duplicate voter
-- keys, duplicate voter/action pairs and empty inner maps are rejected rather
-- than merged or discarded. Invalid schema, bounds, UTF-8, or trailing bytes
-- returns 'InvalidGovernanceCbor'. Original bytes are preserved unchanged.
decodeVoting :: ByteString -> Either Error EncodedVotingProcedures
decodeVoting :: ByteString -> Either Error EncodedVotingProcedures
decodeVoting ByteString
bytes = ByteString -> VotingProcedures -> EncodedVotingProcedures
EncodedVotingProcedures ByteString
bytes (VotingProcedures -> EncodedVotingProcedures)
-> Either Error VotingProcedures
-> Either Error EncodedVotingProcedures
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s VotingProcedures)
-> ByteString -> Either Error VotingProcedures
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete Decoder s VotingProcedures
forall s. Decoder s VotingProcedures
votingDecoder ByteString
bytes

-- | Assign fresh minimal, sorted CBOR to a semantic collection.
fromVotingProcedures :: VotingProcedures -> EncodedVotingProcedures
fromVotingProcedures :: VotingProcedures -> EncodedVotingProcedures
fromVotingProcedures VotingProcedures
value = ByteString -> VotingProcedures -> EncodedVotingProcedures
EncodedVotingProcedures (VotingProcedures -> ByteString
encodeVoting VotingProcedures
value) VotingProcedures
value

-- | Inspect votes without changing their preserved encoding.
toVotingProcedures :: EncodedVotingProcedures -> VotingProcedures
toVotingProcedures :: EncodedVotingProcedures -> VotingProcedures
toVotingProcedures (EncodedVotingProcedures ByteString
_ VotingProcedures
value) = VotingProcedures
value

-- | The exact imported encoding, or the fresh encoding assigned by
-- 'fromVotingProcedures'.
toCbor :: EncodedVotingProcedures -> ByteString
toCbor :: EncodedVotingProcedures -> ByteString
toCbor (EncodedVotingProcedures ByteString
bytes VotingProcedures
_) = ByteString
bytes

anchorEncoding :: Anchor -> Encode.Encoding
anchorEncoding :: Anchor -> Encoding
anchorEncoding (Anchor Url
location DataHash
digest) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
Encode.encodeString (Url -> Text
urlText Url
location) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (DataHash -> ByteString
dataHashBytes DataHash
digest)

actionEncoding :: ActionId -> Encode.Encoding
actionEncoding :: ActionId -> Encoding
actionEncoding (ActionId TransactionId
hash Word64
actionIndex) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (TransactionId -> ByteString
TransactionId.toBytes TransactionId
hash) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
actionIndex

credentialEncoding :: Credential.Credential purpose -> Encode.Encoding
credentialEncoding :: forall purpose. Credential purpose -> Encoding
credentialEncoding Credential purpose
credential = case Credential purpose
credential of
  Credential.KeyCredential KeyHash purpose
hash -> Word -> Encoding
Encode.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (KeyHash purpose -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash purpose
hash)
  Credential.ScriptCredential ScriptHash
hash -> Word -> Encoding
Encode.encodeWord Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
hash)

drepEncoding :: DRep -> Encode.Encoding
drepEncoding :: DRep -> Encoding
drepEncoding DRep
drep = case DRep
drep of
  DRepCredential Credential DRep
credential -> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Credential DRep -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential DRep
credential
  DRep
AlwaysAbstain -> Word -> Encoding
Encode.encodeListLen Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
2
  DRep
AlwaysNoConfidence -> Word -> Encoding
Encode.encodeListLen Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
3

voterEncoding :: Voter -> Encode.Encoding
voterEncoding :: Voter -> Encoding
voterEncoding Voter
voter =
  Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> case Voter
voter of
    CommitteeVoter Credential CommitteeHot
credential -> Credential CommitteeHot -> Encoding
forall purpose. Credential purpose -> Encoding
credentialEncoding Credential CommitteeHot
credential
    DRepVoter Credential DRep
credential -> case Credential DRep
credential of
      Credential.KeyCredential KeyHash DRep
hash -> Word -> Encoding
Encode.encodeWord Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (KeyHash DRep -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash DRep
hash)
      Credential.ScriptCredential ScriptHash
hash -> Word -> Encoding
Encode.encodeWord Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
hash)
    StakePoolVoter KeyHash StakePool
hash -> Word -> Encoding
Encode.encodeWord Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (KeyHash StakePool -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash StakePool
hash)

procedureEncoding :: VotingProcedure -> Encode.Encoding
procedureEncoding :: VotingProcedure -> Encoding
procedureEncoding (VotingProcedure Vote
choice Maybe Anchor
explanation) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord (Vote -> Word
voteCode Vote
choice) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding -> (Anchor -> Encoding) -> Maybe Anchor -> Encoding
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Encoding
Encode.encodeNull Anchor -> Encoding
anchorEncoding Maybe Anchor
explanation

votingEncoding :: VotingProcedures -> Encode.Encoding
votingEncoding :: VotingProcedures -> Encoding
votingEncoding (VotingProcedures Map Voter (Map ActionId VotingProcedure)
voters) = Word -> Encoding
Encode.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Map Voter (Map ActionId VotingProcedure) -> Int
forall k a. Map k a -> Int
Map.size Map Voter (Map ActionId VotingProcedure)
voters)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((Voter, Map ActionId VotingProcedure) -> Encoding)
-> [(Voter, Map ActionId VotingProcedure)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Voter, Map ActionId VotingProcedure) -> Encoding
encodeVoterVotes (Map Voter (Map ActionId VotingProcedure)
-> [(Voter, Map ActionId VotingProcedure)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map Voter (Map ActionId VotingProcedure)
voters)
  where
    encodeVoterVotes :: (Voter, Map ActionId VotingProcedure) -> Encoding
encodeVoterVotes (Voter
voter, Map ActionId VotingProcedure
actions) = Voter -> Encoding
voterEncoding Voter
voter Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Map ActionId VotingProcedure -> Int
forall k a. Map k a -> Int
Map.size Map ActionId VotingProcedure
actions)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((ActionId, VotingProcedure) -> Encoding)
-> [(ActionId, VotingProcedure)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(ActionId
action, VotingProcedure
procedure) -> ActionId -> Encoding
actionEncoding ActionId
action Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> VotingProcedure -> Encoding
procedureEncoding VotingProcedure
procedure) (Map ActionId VotingProcedure -> [(ActionId, VotingProcedure)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map ActionId VotingProcedure
actions)

voteCode :: Vote -> Word
voteCode :: Vote -> Word
voteCode Vote
No = Word
0
voteCode Vote
Yes = Word
1
voteCode Vote
Abstain = Word
2

anchorDecoder :: Decode.Decoder s Anchor
anchorDecoder :: forall s. Decoder s Anchor
anchorDecoder = Int -> Decoder s Anchor -> Decoder s Anchor
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s Anchor -> Decoder s Anchor)
-> Decoder s Anchor -> Decoder s Anchor
forall a b. (a -> b) -> a -> b
$ do
  Url
location <- (Text -> Either Error Url) -> Text -> Decoder s Url
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated Text -> Either Error Url
mkUrl (Text -> Decoder s Url) -> Decoder s Text -> Decoder s Url
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s Text
forall s. Decoder s Text
Decode.decodeString
  DataHash
digest <- (ByteString -> Either Error DataHash)
-> ByteString -> Decoder s DataHash
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error DataHash
dataHashFromBytes (ByteString -> Decoder s DataHash)
-> Decoder s ByteString -> Decoder s DataHash
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  Anchor -> Decoder s Anchor
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Url -> DataHash -> Anchor
Anchor Url
location DataHash
digest)

actionDecoder :: Decode.Decoder s ActionId
actionDecoder :: forall s. Decoder s ActionId
actionDecoder = Int -> Decoder s ActionId -> Decoder s ActionId
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s ActionId -> Decoder s ActionId)
-> Decoder s ActionId -> Decoder s ActionId
forall a b. (a -> b) -> a -> b
$ do
  TransactionId
hash <- (ByteString -> Either Error TransactionId)
-> ByteString -> Decoder s TransactionId
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error TransactionId
TransactionId.fromBytes (ByteString -> Decoder s TransactionId)
-> Decoder s ByteString -> Decoder s TransactionId
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  TransactionId -> Word64 -> ActionId
ActionId TransactionId
hash (Word64 -> ActionId) -> Decoder s Word64 -> Decoder s ActionId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64

credentialDecoder :: Word -> Decode.Decoder s (Credential.Credential purpose)
credentialDecoder :: forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
kind = do
  ByteString
bytes <- Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  case Word
kind of
    Word
0 -> KeyHash purpose -> Credential purpose
forall purpose. KeyHash purpose -> Credential purpose
Credential.KeyCredential (KeyHash purpose -> Credential purpose)
-> Decoder s (KeyHash purpose) -> Decoder s (Credential purpose)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either Error (KeyHash purpose))
-> ByteString -> Decoder s (KeyHash purpose)
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error (KeyHash purpose)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes ByteString
bytes
    Word
1 -> ScriptHash -> Credential purpose
forall purpose. ScriptHash -> Credential purpose
Credential.ScriptCredential (ScriptHash -> Credential purpose)
-> Decoder s ScriptHash -> Decoder s (Credential purpose)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either Error ScriptHash)
-> ByteString -> Decoder s ScriptHash
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes ByteString
bytes
    Word
_ -> String -> Decoder s (Credential purpose)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid credential kind"

drepDecoder :: Decode.Decoder s DRep
drepDecoder :: forall s. Decoder s DRep
drepDecoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  Word
kind <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  let expected :: Int
expected = if Word
kind Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Word
2 then Int
2 else Int
1
  Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size
  DRep
value <- case Word
kind of
    Word
0 -> Credential DRep -> DRep
DRepCredential (Credential DRep -> DRep)
-> Decoder s (Credential DRep) -> Decoder s DRep
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word -> Decoder s (Credential DRep)
forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
0
    Word
1 -> Credential DRep -> DRep
DRepCredential (Credential DRep -> DRep)
-> Decoder s (Credential DRep) -> Decoder s DRep
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word -> Decoder s (Credential DRep)
forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
1
    Word
2 -> DRep -> Decoder s DRep
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DRep
AlwaysAbstain
    Word
3 -> DRep -> Decoder s DRep
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DRep
AlwaysNoConfidence
    Word
_ -> String -> Decoder s DRep
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid DRep kind"
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  DRep -> Decoder s DRep
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DRep
value

voterDecoder :: Decode.Decoder s Voter
voterDecoder :: forall s. Decoder s Voter
voterDecoder = Int -> Decoder s Voter -> Decoder s Voter
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s Voter -> Decoder s Voter)
-> Decoder s Voter -> Decoder s Voter
forall a b. (a -> b) -> a -> b
$ do
  Word
kind <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  case Word
kind of
    Word
0 -> Credential CommitteeHot -> Voter
CommitteeVoter (Credential CommitteeHot -> Voter)
-> Decoder s (Credential CommitteeHot) -> Decoder s Voter
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word -> Decoder s (Credential CommitteeHot)
forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
0
    Word
1 -> Credential CommitteeHot -> Voter
CommitteeVoter (Credential CommitteeHot -> Voter)
-> Decoder s (Credential CommitteeHot) -> Decoder s Voter
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word -> Decoder s (Credential CommitteeHot)
forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
1
    Word
2 -> Credential DRep -> Voter
DRepVoter (Credential DRep -> Voter)
-> Decoder s (Credential DRep) -> Decoder s Voter
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word -> Decoder s (Credential DRep)
forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
0
    Word
3 -> Credential DRep -> Voter
DRepVoter (Credential DRep -> Voter)
-> Decoder s (Credential DRep) -> Decoder s Voter
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Word -> Decoder s (Credential DRep)
forall s purpose. Word -> Decoder s (Credential purpose)
credentialDecoder Word
1
    Word
4 -> KeyHash StakePool -> Voter
StakePoolVoter (KeyHash StakePool -> Voter)
-> Decoder s (KeyHash StakePool) -> Decoder s Voter
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((ByteString -> Either Error (KeyHash StakePool))
-> ByteString -> Decoder s (KeyHash StakePool)
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error (KeyHash StakePool)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes (ByteString -> Decoder s (KeyHash StakePool))
-> Decoder s ByteString -> Decoder s (KeyHash StakePool)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes)
    Word
_ -> String -> Decoder s Voter
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid voter kind"

procedureDecoder :: Decode.Decoder s VotingProcedure
procedureDecoder :: forall s. Decoder s VotingProcedure
procedureDecoder = Int -> Decoder s VotingProcedure -> Decoder s VotingProcedure
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s VotingProcedure -> Decoder s VotingProcedure)
-> Decoder s VotingProcedure -> Decoder s VotingProcedure
forall a b. (a -> b) -> a -> b
$ do
  Word
code <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  Vote
choice <- case Word
code of
    Word
0 -> Vote -> Decoder s Vote
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vote
No
    Word
1 -> Vote -> Decoder s Vote
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vote
Yes
    Word
2 -> Vote -> Decoder s Vote
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vote
Abstain
    Word
_ -> String -> Decoder s Vote
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid vote code"
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  Maybe Anchor
explanation <- if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeNull then Decoder s ()
forall s. Decoder s ()
Decode.decodeNull Decoder s ()
-> Decoder s (Maybe Anchor) -> Decoder s (Maybe Anchor)
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Anchor -> Decoder s (Maybe Anchor)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Anchor
forall a. Maybe a
Nothing else Anchor -> Maybe Anchor
forall a. a -> Maybe a
Just (Anchor -> Maybe Anchor)
-> Decoder s Anchor -> Decoder s (Maybe Anchor)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Anchor
forall s. Decoder s Anchor
anchorDecoder
  VotingProcedure -> Decoder s VotingProcedure
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vote -> Maybe Anchor -> VotingProcedure
VotingProcedure Vote
choice Maybe Anchor
explanation)

votingDecoder :: Decode.Decoder s VotingProcedures
votingDecoder :: forall s. Decoder s VotingProcedures
votingDecoder = do
  [(Voter, [(ActionId, VotingProcedure)])]
pairs <- Decoder s (Voter, [(ActionId, VotingProcedure)])
-> Decoder s [(Voter, [(ActionId, VotingProcedure)])]
forall s a. Decoder s a -> Decoder s [a]
mapEntries (Decoder s (Voter, [(ActionId, VotingProcedure)])
 -> Decoder s [(Voter, [(ActionId, VotingProcedure)])])
-> Decoder s (Voter, [(ActionId, VotingProcedure)])
-> Decoder s [(Voter, [(ActionId, VotingProcedure)])]
forall a b. (a -> b) -> a -> b
$ do
    Voter
voter <- Decoder s Voter
forall s. Decoder s Voter
voterDecoder
    [(ActionId, VotingProcedure)]
actions <- Decoder s (ActionId, VotingProcedure)
-> Decoder s [(ActionId, VotingProcedure)]
forall s a. Decoder s a -> Decoder s [a]
mapEntries ((,) (ActionId -> VotingProcedure -> (ActionId, VotingProcedure))
-> Decoder s ActionId
-> Decoder s (VotingProcedure -> (ActionId, VotingProcedure))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ActionId
forall s. Decoder s ActionId
actionDecoder Decoder s (VotingProcedure -> (ActionId, VotingProcedure))
-> Decoder s VotingProcedure
-> Decoder s (ActionId, VotingProcedure)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s VotingProcedure
forall s. Decoder s VotingProcedure
procedureDecoder)
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([(ActionId, VotingProcedure)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(ActionId, VotingProcedure)]
actions) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Empty inner voting map")
    (Voter, [(ActionId, VotingProcedure)])
-> Decoder s (Voter, [(ActionId, VotingProcedure)])
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Voter
voter, [(ActionId, VotingProcedure)]
actions)
  Map Voter (Map ActionId VotingProcedure)
voters <- (Map Voter (Map ActionId VotingProcedure)
 -> (Voter, [(ActionId, VotingProcedure)])
 -> Decoder s (Map Voter (Map ActionId VotingProcedure)))
-> Map Voter (Map ActionId VotingProcedure)
-> [(Voter, [(ActionId, VotingProcedure)])]
-> Decoder s (Map Voter (Map ActionId VotingProcedure))
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Map Voter (Map ActionId VotingProcedure)
-> (Voter, [(ActionId, VotingProcedure)])
-> Decoder s (Map Voter (Map ActionId VotingProcedure))
forall {m :: * -> *} {t :: * -> *} {k} {k} {a}.
(MonadFail m, Foldable t, Ord k, Ord k) =>
Map k (Map k a) -> (k, t (k, a)) -> m (Map k (Map k a))
addVoter Map Voter (Map ActionId VotingProcedure)
forall k a. Map k a
Map.empty [(Voter, [(ActionId, VotingProcedure)])]
pairs
  VotingProcedures -> Decoder s VotingProcedures
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map Voter (Map ActionId VotingProcedure) -> VotingProcedures
VotingProcedures Map Voter (Map ActionId VotingProcedure)
voters)
  where
    addVoter :: Map k (Map k a) -> (k, t (k, a)) -> m (Map k (Map k a))
addVoter Map k (Map k a)
voters (k
voter, t (k, a)
actions)
      | k -> Map k (Map k a) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member k
voter Map k (Map k a)
voters = String -> m (Map k (Map k a))
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Duplicate voter key"
      | Bool
otherwise = do
          Map k a
unique <- (Map k a -> (k, a) -> m (Map k a))
-> Map k a -> t (k, a) -> m (Map k a)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Map k a -> (k, a) -> m (Map k a)
forall {k} {m :: * -> *} {a}.
(Ord k, MonadFail m) =>
Map k a -> (k, a) -> m (Map k a)
addAction Map k a
forall k a. Map k a
Map.empty t (k, a)
actions
          Map k (Map k a) -> m (Map k (Map k a))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k -> Map k a -> Map k (Map k a) -> Map k (Map k a)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
voter Map k a
unique Map k (Map k a)
voters)
    addAction :: Map k a -> (k, a) -> m (Map k a)
addAction Map k a
actions (k
action, a
procedure)
      | k -> Map k a -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member k
action Map k a
actions = String -> m (Map k a)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Duplicate governance action key"
      | Bool
otherwise = Map k a -> m (Map k a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
action a
procedure Map k a
actions)

mapEntries :: Decode.Decoder s a -> Decode.Decoder s [a]
mapEntries :: forall s a. Decoder s a -> Decoder s [a]
mapEntries Decoder s a
entry = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeMapLenOrIndef
  case Maybe Int
size of
    Just Int
count -> Int -> Decoder s a -> Decoder s [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Decoder s a
entry
    Maybe Int
Nothing -> Decoder s [a]
go
  where
    go :: Decoder s [a]
go = do
      Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
      if Bool
ended then [a] -> Decoder s [a]
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [] else (:) (a -> [a] -> [a]) -> Decoder s a -> Decoder s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
entry Decoder s ([a] -> [a]) -> Decoder s [a] -> Decoder s [a]
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s [a]
go

arrayOf :: Int -> Decode.Decoder s a -> Decode.Decoder s a
arrayOf :: forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
expected Decoder s a
entry = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size
  a
value <- Decoder s a
entry
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  a -> Decoder s a
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value

checkSize :: Int -> Maybe Int -> Decode.Decoder s ()
checkSize :: forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size = case Maybe Int
size of
  Just Int
actual -> Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
expected) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Incorrect governance array length")
  Maybe Int
Nothing -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

endArray :: Maybe Int -> Decode.Decoder s ()
endArray :: forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size = case Maybe Int
size of
  Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Maybe Int
Nothing -> do
    Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ended (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Extra governance array elements")

validated :: (a -> Either Error b) -> a -> Decode.Decoder s b
validated :: forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated a -> Either Error b
validate a
value = (Error -> Decoder s b)
-> (b -> Decoder s b) -> Either Error b -> Decoder s b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s b
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s b)
-> (Error -> String) -> Error -> Decoder s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) b -> Decoder s b
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either Error b
validate a
value)

decodeComplete :: (forall s. Decode.Decoder s a) -> ByteString -> Either Error a
decodeComplete :: forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete forall s. Decoder s a
decoder ByteString
bytes = case (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes Decoder s a
forall s. Decoder s a
decoder (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
  Left DeserialiseFailure
failure -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidGovernanceCbor (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
failure)))
  Right (ByteString
remaining, a
value)
    | ByteString -> Bool
Lazy.null ByteString
remaining -> a -> Either Error a
forall a b. b -> Either a b
Right a
value
    | Bool
otherwise -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidGovernanceCbor Text
"Trailing bytes after governance value")