-- 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.

-- | Seven governance action alternatives assembled from validated components.
-- For example, @NoConfidence Nothing@ starts a new committee confidence chain.
-- These values describe proposals; they do not establish ledger eligibility.
module Cometa.Governance.Action
  ( Action (..),
    Removals,
    removalsFromList,
    removalsToList,
    EncodedRemovals,
    encodeRemovals,
    encodeRemovalsWith,
    decodeRemovals,
    fromRemovals,
    toRemovals,
    removalsCbor,
    removalsSetEncoding,
    withRemovalsSetEncoding,
    Withdrawals,
    withdrawalsFromList,
    withdrawalsToList,
    EncodedAction,
    encodeCbor,
    decodeCbor,
    fromAction,
    toAction,
    toCbor,
  )
where

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

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Address.Reward (RewardAddress)
import qualified Cometa.Address.Reward as Reward
import Cometa.Credential (CommitteeCold, Credential, ScriptHash)
import qualified Cometa.Credential as Credential
import Cometa.Encoding.Set (SetEncoding)
import Cometa.Error (Error (..))
import Cometa.Governance.ActionId (ActionId)
import qualified Cometa.Governance.ActionId as ActionId
import Cometa.Governance.Committee (Members)
import qualified Cometa.Governance.Committee as Committee
import Cometa.Governance.Constitution (Constitution)
import qualified Cometa.Governance.Constitution as Constitution
import qualified Cometa.Internal.ProposalCbor as Cbor
import qualified Cometa.Internal.SetEncoding as SetEncoding
import Cometa.Protocol.Parameters.Update (Update)
import qualified Cometa.Protocol.Parameters.Update as Update
import Cometa.Protocol.Version (ProtocolVersion)
import qualified Cometa.Protocol.Version as Version
import Cometa.UnitInterval (UnitInterval)
import qualified Cometa.UnitInterval as Interval
import Control.Monad (unless)
import Data.ByteString (ByteString)
import Data.List (sort, sortOn)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Word (Word64)

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

-- | Cold credentials to remove, sorted by kind and hash. Repeated credentials
-- are retained. Ledger acceptance is a separate check.
newtype Removals = Removals [Credential CommitteeCold] deriving (Removals -> Removals -> Bool
(Removals -> Removals -> Bool)
-> (Removals -> Removals -> Bool) -> Eq Removals
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Removals -> Removals -> Bool
== :: Removals -> Removals -> Bool
$c/= :: Removals -> Removals -> Bool
/= :: Removals -> Removals -> Bool
Eq, Eq Removals
Eq Removals =>
(Removals -> Removals -> Ordering)
-> (Removals -> Removals -> Bool)
-> (Removals -> Removals -> Bool)
-> (Removals -> Removals -> Bool)
-> (Removals -> Removals -> Bool)
-> (Removals -> Removals -> Removals)
-> (Removals -> Removals -> Removals)
-> Ord Removals
Removals -> Removals -> Bool
Removals -> Removals -> Ordering
Removals -> Removals -> Removals
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 :: Removals -> Removals -> Ordering
compare :: Removals -> Removals -> Ordering
$c< :: Removals -> Removals -> Bool
< :: Removals -> Removals -> Bool
$c<= :: Removals -> Removals -> Bool
<= :: Removals -> Removals -> Bool
$c> :: Removals -> Removals -> Bool
> :: Removals -> Removals -> Bool
$c>= :: Removals -> Removals -> Bool
>= :: Removals -> Removals -> Bool
$cmax :: Removals -> Removals -> Removals
max :: Removals -> Removals -> Removals
$cmin :: Removals -> Removals -> Removals
min :: Removals -> Removals -> Removals
Ord, Int -> Removals -> ShowS
[Removals] -> ShowS
Removals -> String
(Int -> Removals -> ShowS)
-> (Removals -> String) -> ([Removals] -> ShowS) -> Show Removals
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Removals -> ShowS
showsPrec :: Int -> Removals -> ShowS
$cshow :: Removals -> String
show :: Removals -> String
$cshowList :: [Removals] -> ShowS
showList :: [Removals] -> ShowS
Show)

-- | A standalone removal collection retaining its exact CBOR representation.
-- Equality and ordering compare bytes; inspect credentials with 'toRemovals'.
data EncodedRemovals = EncodedRemovals !ByteString !Removals deriving (Int -> EncodedRemovals -> ShowS
[EncodedRemovals] -> ShowS
EncodedRemovals -> String
(Int -> EncodedRemovals -> ShowS)
-> (EncodedRemovals -> String)
-> ([EncodedRemovals] -> ShowS)
-> Show EncodedRemovals
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedRemovals -> ShowS
showsPrec :: Int -> EncodedRemovals -> ShowS
$cshow :: EncodedRemovals -> String
show :: EncodedRemovals -> String
$cshowList :: [EncodedRemovals] -> ShowS
showList :: [EncodedRemovals] -> ShowS
Show)

instance Eq EncodedRemovals where
  EncodedRemovals
left == :: EncodedRemovals -> EncodedRemovals -> Bool
== EncodedRemovals
right = EncodedRemovals -> ByteString
removalsCbor EncodedRemovals
left ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== EncodedRemovals -> ByteString
removalsCbor EncodedRemovals
right

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

-- | Unique reward accounts and lovelace amounts, including zero amounts.
newtype Withdrawals = Withdrawals (Map RewardAddress Word64) deriving (Withdrawals -> Withdrawals -> Bool
(Withdrawals -> Withdrawals -> Bool)
-> (Withdrawals -> Withdrawals -> Bool) -> Eq Withdrawals
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Withdrawals -> Withdrawals -> Bool
== :: Withdrawals -> Withdrawals -> Bool
$c/= :: Withdrawals -> Withdrawals -> Bool
/= :: Withdrawals -> Withdrawals -> Bool
Eq, Eq Withdrawals
Eq Withdrawals =>
(Withdrawals -> Withdrawals -> Ordering)
-> (Withdrawals -> Withdrawals -> Bool)
-> (Withdrawals -> Withdrawals -> Bool)
-> (Withdrawals -> Withdrawals -> Bool)
-> (Withdrawals -> Withdrawals -> Bool)
-> (Withdrawals -> Withdrawals -> Withdrawals)
-> (Withdrawals -> Withdrawals -> Withdrawals)
-> Ord Withdrawals
Withdrawals -> Withdrawals -> Bool
Withdrawals -> Withdrawals -> Ordering
Withdrawals -> Withdrawals -> Withdrawals
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 :: Withdrawals -> Withdrawals -> Ordering
compare :: Withdrawals -> Withdrawals -> Ordering
$c< :: Withdrawals -> Withdrawals -> Bool
< :: Withdrawals -> Withdrawals -> Bool
$c<= :: Withdrawals -> Withdrawals -> Bool
<= :: Withdrawals -> Withdrawals -> Bool
$c> :: Withdrawals -> Withdrawals -> Bool
> :: Withdrawals -> Withdrawals -> Bool
$c>= :: Withdrawals -> Withdrawals -> Bool
>= :: Withdrawals -> Withdrawals -> Bool
$cmax :: Withdrawals -> Withdrawals -> Withdrawals
max :: Withdrawals -> Withdrawals -> Withdrawals
$cmin :: Withdrawals -> Withdrawals -> Withdrawals
min :: Withdrawals -> Withdrawals -> Withdrawals
Ord, Int -> Withdrawals -> ShowS
[Withdrawals] -> ShowS
Withdrawals -> String
(Int -> Withdrawals -> ShowS)
-> (Withdrawals -> String)
-> ([Withdrawals] -> ShowS)
-> Show Withdrawals
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Withdrawals -> ShowS
showsPrec :: Int -> Withdrawals -> ShowS
$cshow :: Withdrawals -> String
show :: Withdrawals -> String
$cshowList :: [Withdrawals] -> ShowS
showList :: [Withdrawals] -> ShowS
Show)

-- | A governance action. Optional predecessors reference the preceding action
-- of the corresponding purpose; 'Nothing' starts a chain. Optional script hashes
-- identify guardrails policies. Amounts and expiry epochs use unsigned 64-bit units.
data Action
  = -- | Previous parameter action, sparse changes, optional guardrails policy.
    ParameterChange !(Maybe ActionId) !Update !(Maybe ScriptHash)
  | -- | Previous hard-fork action and proposed protocol version.
    HardForkInitiation !(Maybe ActionId) !ProtocolVersion
  | -- | Reward accounts receiving treasury lovelace and optional policy.
    TreasuryWithdrawals !Withdrawals !(Maybe ScriptHash)
  | -- | Previous committee-confidence action.
    NoConfidence !(Maybe ActionId)
  | -- | Previous committee action, removals, additions with expiry, and quorum.
    UpdateCommittee !(Maybe ActionId) !Removals !Members !UnitInterval
  | -- | Previous constitution action and replacement constitution.
    NewConstitution !(Maybe ActionId) !Constitution
  | -- | Informational action without additional fields.
    InfoAction
  deriving (Action -> Action -> Bool
(Action -> Action -> Bool)
-> (Action -> Action -> Bool) -> Eq Action
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Action -> Action -> Bool
== :: Action -> Action -> Bool
$c/= :: Action -> Action -> Bool
/= :: Action -> Action -> Bool
Eq, Int -> Action -> ShowS
[Action] -> ShowS
Action -> String
(Int -> Action -> ShowS)
-> (Action -> String) -> ([Action] -> ShowS) -> Show Action
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Action -> ShowS
showsPrec :: Int -> Action -> ShowS
$cshow :: Action -> String
show :: Action -> String
$cshowList :: [Action] -> ShowS
showList :: [Action] -> ShowS
Show)

-- | Exact original CBOR plus its semantic action. Equality and ordering compare
-- bytes. Use 'toAction' when comparing action meanings.
data EncodedAction = EncodedAction !ByteString !Action deriving (Int -> EncodedAction -> ShowS
[EncodedAction] -> ShowS
EncodedAction -> String
(Int -> EncodedAction -> ShowS)
-> (EncodedAction -> String)
-> ([EncodedAction] -> ShowS)
-> Show EncodedAction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedAction -> ShowS
showsPrec :: Int -> EncodedAction -> ShowS
$cshow :: EncodedAction -> String
show :: EncodedAction -> String
$cshowList :: [EncodedAction] -> ShowS
showList :: [EncodedAction] -> ShowS
Show)

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

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

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

-- | Sort by credential kind then hash bytes, retaining duplicate credentials.
removalsFromList :: [Credential CommitteeCold] -> Removals
removalsFromList :: [Credential CommitteeCold] -> Removals
removalsFromList = [Credential CommitteeCold] -> Removals
Removals ([Credential CommitteeCold] -> Removals)
-> ([Credential CommitteeCold] -> [Credential CommitteeCold])
-> [Credential CommitteeCold]
-> Removals
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Credential CommitteeCold] -> [Credential CommitteeCold]
forall a. Ord a => [a] -> [a]
sort

-- | Sorted removal credentials, including duplicates.
removalsToList :: Removals -> [Credential CommitteeCold]
removalsToList :: Removals -> [Credential CommitteeCold]
removalsToList (Removals [Credential CommitteeCold]
values) = [Credential CommitteeCold]
values

-- | Fresh tagged definite array of sorted credentials, retaining duplicates.
encodeRemovals :: Removals -> ByteString
encodeRemovals :: Removals -> ByteString
encodeRemovals = Encoding -> ByteString
Cbor.encode (Encoding -> ByteString)
-> (Removals -> Encoding) -> Removals -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Removals -> Encoding
removalsEncoding

-- | Fresh definite array with the requested outer set tag.
encodeRemovalsWith :: SetEncoding -> Removals -> ByteString
encodeRemovalsWith :: SetEncoding -> Removals -> ByteString
encodeRemovalsWith SetEncoding
requested = SetEncoding -> ByteString -> ByteString
SetEncoding.withEncoding SetEncoding
requested (ByteString -> ByteString)
-> (Removals -> ByteString) -> Removals -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Removals -> ByteString
encodeRemovals

-- | Decode a standalone credential collection, retaining all imported bytes.
-- Optional tag 258, definite and indefinite arrays, and duplicate credentials
-- are accepted; invalid fields or trailing bytes return 'InvalidProposalCbor'.
decodeRemovals :: ByteString -> Either Error EncodedRemovals
decodeRemovals :: ByteString -> Either Error EncodedRemovals
decodeRemovals ByteString
bytes = ByteString -> Removals -> EncodedRemovals
EncodedRemovals ByteString
bytes (Removals -> EncodedRemovals)
-> ([Credential CommitteeCold] -> Removals)
-> [Credential CommitteeCold]
-> EncodedRemovals
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Credential CommitteeCold] -> Removals
removalsFromList ([Credential CommitteeCold] -> EncodedRemovals)
-> Either Error [Credential CommitteeCold]
-> Either Error EncodedRemovals
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s [Credential CommitteeCold])
-> ByteString -> Either Error [Credential CommitteeCold]
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode (Decoder s (Credential CommitteeCold)
-> Decoder s [Credential CommitteeCold]
forall s a. Decoder s a -> Decoder s [a]
Cbor.setList Decoder s (Credential CommitteeCold)
forall s purpose. Decoder s (Credential purpose)
Cbor.credentialDecoder) ByteString
bytes

-- | Assign fresh tagged encoding to a removal collection.
fromRemovals :: Removals -> EncodedRemovals
fromRemovals :: Removals -> EncodedRemovals
fromRemovals Removals
value = ByteString -> Removals -> EncodedRemovals
EncodedRemovals (Removals -> ByteString
encodeRemovals Removals
value) Removals
value

-- | Inspect sorted removal credentials without changing retained bytes.
toRemovals :: EncodedRemovals -> Removals
toRemovals :: EncodedRemovals -> Removals
toRemovals (EncodedRemovals ByteString
_ Removals
value) = Removals
value

-- | Exact imported bytes or fresh bytes assigned by 'fromRemovals'.
removalsCbor :: EncodedRemovals -> ByteString
removalsCbor :: EncodedRemovals -> ByteString
removalsCbor (EncodedRemovals ByteString
bytes Removals
_) = ByteString
bytes

-- | Observe the exact standalone removal collection's outer set tag.
removalsSetEncoding :: EncodedRemovals -> SetEncoding
removalsSetEncoding :: EncodedRemovals -> SetEncoding
removalsSetEncoding = ByteString -> SetEncoding
SetEncoding.encoding (ByteString -> SetEncoding)
-> (EncodedRemovals -> ByteString)
-> EncodedRemovals
-> SetEncoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedRemovals -> ByteString
removalsCbor

-- | Change only the outer tag, retaining every array and credential byte.
-- Requesting the current representation preserves the entire encoding exactly.
withRemovalsSetEncoding :: SetEncoding -> EncodedRemovals -> EncodedRemovals
withRemovalsSetEncoding :: SetEncoding -> EncodedRemovals -> EncodedRemovals
withRemovalsSetEncoding SetEncoding
requested (EncodedRemovals ByteString
bytes Removals
values) =
  ByteString -> Removals -> EncodedRemovals
EncodedRemovals (SetEncoding -> ByteString -> ByteString
SetEncoding.withEncoding SetEncoding
requested ByteString
bytes) Removals
values

-- | Reject repeated reward accounts with 'DuplicateTreasuryWithdrawal', even
-- when amounts agree. Values are lovelace; no minimum or positivity is imposed.
withdrawalsFromList :: [(RewardAddress, Word64)] -> Either Error Withdrawals
withdrawalsFromList :: [(RewardAddress, Word64)] -> Either Error Withdrawals
withdrawalsFromList [(RewardAddress, Word64)]
values = Map RewardAddress Word64 -> Withdrawals
Withdrawals (Map RewardAddress Word64 -> Withdrawals)
-> Either Error (Map RewardAddress Word64)
-> Either Error Withdrawals
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Error
-> [(RewardAddress, Word64)]
-> Either Error (Map RewardAddress Word64)
forall key value.
Ord key =>
Error -> [(key, value)] -> Either Error (Map key value)
Cbor.uniqueMap Error
DuplicateTreasuryWithdrawal [(RewardAddress, Word64)]
values

-- | Entries ordered by exact reward-address bytes, matching fresh serialization.
withdrawalsToList :: Withdrawals -> [(RewardAddress, Word64)]
withdrawalsToList :: Withdrawals -> [(RewardAddress, Word64)]
withdrawalsToList (Withdrawals Map RewardAddress Word64
values) = ((RewardAddress, Word64) -> ByteString)
-> [(RewardAddress, Word64)] -> [(RewardAddress, Word64)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (RewardAddress -> ByteString
Reward.toBytes (RewardAddress -> ByteString)
-> ((RewardAddress, Word64) -> RewardAddress)
-> (RewardAddress, Word64)
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RewardAddress, Word64) -> RewardAddress
forall a b. (a, b) -> a
fst) (Map RewardAddress Word64 -> [(RewardAddress, Word64)]
forall k a. Map k a -> [(k, a)]
Map.toList Map RewardAddress Word64
values)

-- | Fresh definite CBOR with shortest integers and sorted maps/removal credentials.
-- Reconstructing semantic data can change transaction identity; 'toCbor' preserves
-- imported bytes instead.
encodeCbor :: Action -> ByteString
encodeCbor :: Action -> ByteString
encodeCbor = Encoding -> ByteString
Cbor.encode (Encoding -> ByteString)
-> (Action -> Encoding) -> Action -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Action -> Encoding
actionEncoding

-- | Decode exactly one action and retain its original bytes. Definite and
-- indefinite containers and nonminimal integers are accepted; unknown variants,
-- wrong arity, invalid components, duplicate map keys and trailing data return
-- 'Cometa.Error.InvalidProposalCbor'.
decodeCbor :: ByteString -> Either Error EncodedAction
decodeCbor :: ByteString -> Either Error EncodedAction
decodeCbor ByteString
bytes = ByteString -> Action -> EncodedAction
EncodedAction ByteString
bytes (Action -> EncodedAction)
-> Either Error Action -> Either Error EncodedAction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s Action) -> ByteString -> Either Error Action
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode Decoder s Action
forall s. Decoder s Action
actionDecoder ByteString
bytes

-- | Assign fresh encoding to semantic data.
fromAction :: Action -> EncodedAction
fromAction :: Action -> EncodedAction
fromAction Action
value = ByteString -> Action -> EncodedAction
EncodedAction (Action -> ByteString
encodeCbor Action
value) Action
value

-- | Inspect semantic data without changing retained bytes.
toAction :: EncodedAction -> Action
toAction :: EncodedAction -> Action
toAction (EncodedAction ByteString
_ Action
value) = Action
value

-- | Exact imported encoding, or the encoding assigned by 'fromAction'.
toCbor :: EncodedAction -> ByteString
toCbor :: EncodedAction -> ByteString
toCbor (EncodedAction ByteString
bytes Action
_) = ByteString
bytes

actionEncoding :: Action -> E.Encoding
actionEncoding :: Action -> Encoding
actionEncoding Action
value = case Action
value of
  ParameterChange Maybe ActionId
previous Update
changes Maybe ScriptHash
policy ->
    Word -> Word -> Encoding
header Word
4 Word
0
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ActionId -> Encoding
predecessor Maybe ActionId
previous
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Update -> ByteString
Update.encodeCbor Update
changes)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ScriptHash -> Encoding
policyEncoding Maybe ScriptHash
policy
  HardForkInitiation Maybe ActionId
previous ProtocolVersion
version -> Word -> Word -> Encoding
header Word
3 Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ActionId -> Encoding
predecessor Maybe ActionId
previous Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (ProtocolVersion -> ByteString
Version.encodeCbor ProtocolVersion
version)
  TreasuryWithdrawals Withdrawals
withdrawals Maybe ScriptHash
policy -> Word -> Word -> Encoding
header Word
3 Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Withdrawals -> Encoding
withdrawalsEncoding Withdrawals
withdrawals Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ScriptHash -> Encoding
policyEncoding Maybe ScriptHash
policy
  NoConfidence Maybe ActionId
previous -> Word -> Word -> Encoding
header Word
2 Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ActionId -> Encoding
predecessor Maybe ActionId
previous
  UpdateCommittee Maybe ActionId
previous Removals
removals Members
members UnitInterval
quorum ->
    Word -> Word -> Encoding
header Word
5 Word
4
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ActionId -> Encoding
predecessor Maybe ActionId
previous
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Removals -> Encoding
removalsEncoding Removals
removals
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Members -> ByteString
Committee.encodeMembers Members
members)
      Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (UnitInterval -> ByteString
Interval.encodeCbor UnitInterval
quorum)
  NewConstitution Maybe ActionId
previous Constitution
constitution -> Word -> Word -> Encoding
header Word
3 Word
5 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Maybe ActionId -> Encoding
predecessor Maybe ActionId
previous Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Constitution -> ByteString
Constitution.encodeCbor Constitution
constitution)
  Action
InfoAction -> Word -> Word -> Encoding
header Word
1 Word
6
  where
    header :: Word -> Word -> Encoding
header Word
size Word
code = Word -> Encoding
E.encodeListLen Word
size Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeWord Word
code
    predecessor :: Maybe ActionId -> Encoding
predecessor = (ActionId -> Encoding) -> Maybe ActionId -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
Cbor.maybeEncoding (ByteString -> Encoding
Cbor.raw (ByteString -> Encoding)
-> (ActionId -> ByteString) -> ActionId -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ActionId -> ByteString
ActionId.encodeCbor)

policyEncoding :: Maybe ScriptHash -> E.Encoding
policyEncoding :: Maybe ScriptHash -> Encoding
policyEncoding = (ScriptHash -> Encoding) -> Maybe ScriptHash -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
Cbor.maybeEncoding (ByteString -> Encoding
E.encodeBytes (ByteString -> Encoding)
-> (ScriptHash -> ByteString) -> ScriptHash -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScriptHash -> ByteString
Credential.scriptHashBytes)

removalsEncoding :: Removals -> E.Encoding
removalsEncoding :: Removals -> Encoding
removalsEncoding (Removals [Credential CommitteeCold]
values) = Word -> Encoding
E.encodeTag Word
258 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Credential CommitteeCold] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Credential CommitteeCold]
values)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Credential CommitteeCold -> Encoding)
-> [Credential CommitteeCold] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Credential CommitteeCold -> Encoding
forall purpose. Credential purpose -> Encoding
Cbor.credentialEncoding [Credential CommitteeCold]
values

withdrawalsEncoding :: Withdrawals -> E.Encoding
withdrawalsEncoding :: Withdrawals -> Encoding
withdrawalsEncoding Withdrawals
value =
  Word -> Encoding
E.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(RewardAddress, Word64)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(RewardAddress, Word64)]
values))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((RewardAddress, Word64) -> Encoding)
-> [(RewardAddress, Word64)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(RewardAddress
account, Word64
amount) -> ByteString -> Encoding
E.encodeBytes (RewardAddress -> ByteString
Reward.toBytes RewardAddress
account) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
E.encodeWord64 Word64
amount) [(RewardAddress, Word64)]
values
  where
    values :: [(RewardAddress, Word64)]
values = Withdrawals -> [(RewardAddress, Word64)]
withdrawalsToList Withdrawals
value

actionDecoder :: D.Decoder s Action
actionDecoder :: forall s. Decoder s Action
actionDecoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
D.decodeListLenOrIndef
  Word
kind <- Decoder s Word
forall s. Decoder s Word
D.decodeWord
  let expected :: Int
expected = case Word
kind of Word
0 -> Int
4; Word
1 -> Int
3; Word
2 -> Int
3; Word
3 -> Int
2; Word
4 -> Int
5; Word
5 -> Int
3; Word
_ -> Int
1
  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 action arity")
    Maybe Int
Nothing -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Action
value <- Word -> Decoder s Action
forall s. Word -> Decoder s Action
variantDecoder Word
kind
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
Cbor.finishArray Maybe Int
size
  Action -> Decoder s Action
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Action
value

variantDecoder :: Word -> D.Decoder s Action
variantDecoder :: forall s. Word -> Decoder s Action
variantDecoder Word
kind = case Word
kind of
  Word
0 -> Maybe ActionId -> Update -> Maybe ScriptHash -> Action
ParameterChange (Maybe ActionId -> Update -> Maybe ScriptHash -> Action)
-> Decoder s (Maybe ActionId)
-> Decoder s (Update -> Maybe ScriptHash -> Action)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Maybe ActionId)
forall {s}. Decoder s (Maybe ActionId)
previous Decoder s (Update -> Maybe ScriptHash -> Action)
-> Decoder s Update -> Decoder s (Maybe ScriptHash -> Action)
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
<*> (EncodedUpdate -> Update
Update.toUpdate (EncodedUpdate -> Update)
-> Decoder s EncodedUpdate -> Decoder s Update
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either Error EncodedUpdate)
-> Decoder s EncodedUpdate
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error EncodedUpdate
Update.decodeCbor) Decoder s (Maybe ScriptHash -> Action)
-> Decoder s (Maybe ScriptHash) -> Decoder s Action
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 (Maybe ScriptHash)
forall {s}. Decoder s (Maybe ScriptHash)
policy
  Word
1 -> Maybe ActionId -> ProtocolVersion -> Action
HardForkInitiation (Maybe ActionId -> ProtocolVersion -> Action)
-> Decoder s (Maybe ActionId)
-> Decoder s (ProtocolVersion -> Action)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Maybe ActionId)
forall {s}. Decoder s (Maybe ActionId)
previous Decoder s (ProtocolVersion -> Action)
-> Decoder s ProtocolVersion -> Decoder s Action
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
<*> (ByteString -> Either Error ProtocolVersion)
-> Decoder s ProtocolVersion
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error ProtocolVersion
Version.decodeCbor
  Word
2 -> Withdrawals -> Maybe ScriptHash -> Action
TreasuryWithdrawals (Withdrawals -> Maybe ScriptHash -> Action)
-> Decoder s Withdrawals -> Decoder s (Maybe ScriptHash -> Action)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Withdrawals
forall s. Decoder s Withdrawals
withdrawalsDecoder Decoder s (Maybe ScriptHash -> Action)
-> Decoder s (Maybe ScriptHash) -> Decoder s Action
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 (Maybe ScriptHash)
forall {s}. Decoder s (Maybe ScriptHash)
policy
  Word
3 -> Maybe ActionId -> Action
NoConfidence (Maybe ActionId -> Action)
-> Decoder s (Maybe ActionId) -> Decoder s Action
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Maybe ActionId)
forall {s}. Decoder s (Maybe ActionId)
previous
  Word
4 ->
    Maybe ActionId -> Removals -> Members -> UnitInterval -> Action
UpdateCommittee
      (Maybe ActionId -> Removals -> Members -> UnitInterval -> Action)
-> Decoder s (Maybe ActionId)
-> Decoder s (Removals -> Members -> UnitInterval -> Action)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Maybe ActionId)
forall {s}. Decoder s (Maybe ActionId)
previous
      Decoder s (Removals -> Members -> UnitInterval -> Action)
-> Decoder s Removals
-> Decoder s (Members -> UnitInterval -> Action)
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
<*> ([Credential CommitteeCold] -> Removals
removalsFromList ([Credential CommitteeCold] -> Removals)
-> Decoder s [Credential CommitteeCold] -> Decoder s Removals
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential CommitteeCold)
-> Decoder s [Credential CommitteeCold]
forall s a. Decoder s a -> Decoder s [a]
Cbor.setList Decoder s (Credential CommitteeCold)
forall s purpose. Decoder s (Credential purpose)
Cbor.credentialDecoder)
      Decoder s (Members -> UnitInterval -> Action)
-> Decoder s Members -> Decoder s (UnitInterval -> Action)
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
<*> (ByteString -> Either Error Members) -> Decoder s Members
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error Members
Committee.decodeMembers
      Decoder s (UnitInterval -> Action)
-> Decoder s UnitInterval -> Decoder s Action
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
<*> (ByteString -> Either Error UnitInterval) -> Decoder s UnitInterval
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error UnitInterval
Interval.decodeCbor
  Word
5 -> Maybe ActionId -> Constitution -> Action
NewConstitution (Maybe ActionId -> Constitution -> Action)
-> Decoder s (Maybe ActionId) -> Decoder s (Constitution -> Action)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Maybe ActionId)
forall {s}. Decoder s (Maybe ActionId)
previous Decoder s (Constitution -> Action)
-> Decoder s Constitution -> Decoder s Action
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
<*> (ByteString -> Either Error Constitution) -> Decoder s Constitution
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error Constitution
Constitution.decodeCbor
  Word
6 -> Action -> Decoder s Action
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Action
InfoAction
  Word
_ -> String -> Decoder s Action
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown governance action"
  where
    previous :: Decoder s (Maybe ActionId)
previous = Decoder s ActionId -> Decoder s (Maybe ActionId)
forall s a. Decoder s a -> Decoder s (Maybe a)
Cbor.optional ((ByteString -> Either Error ActionId) -> Decoder s ActionId
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error ActionId
ActionId.decodeCbor)
    policy :: Decoder s (Maybe ScriptHash)
policy = Decoder s ScriptHash -> Decoder s (Maybe ScriptHash)
forall s a. Decoder s a -> Decoder s (Maybe a)
Cbor.optional (Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s ScriptHash) -> Decoder s ScriptHash
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> Decoder s ScriptHash)
-> (ScriptHash -> Decoder s ScriptHash)
-> Either Error ScriptHash
-> Decoder s ScriptHash
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s ScriptHash
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s ScriptHash)
-> (Error -> String) -> Error -> Decoder s ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) ScriptHash -> Decoder s ScriptHash
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ScriptHash -> Decoder s ScriptHash)
-> (ByteString -> Either Error ScriptHash)
-> ByteString
-> Decoder s ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes)

withdrawalsDecoder :: D.Decoder s Withdrawals
withdrawalsDecoder :: forall s. Decoder s Withdrawals
withdrawalsDecoder = do
  [(RewardAddress, Word64)]
entries <- Decoder s (RewardAddress, Word64)
-> Decoder s [(RewardAddress, Word64)]
forall s a. Decoder s a -> Decoder s [a]
Cbor.entries (Decoder s (RewardAddress, Word64)
 -> Decoder s [(RewardAddress, Word64)])
-> Decoder s (RewardAddress, Word64)
-> Decoder s [(RewardAddress, Word64)]
forall a b. (a -> b) -> a -> b
$ do
    RewardAddress
account <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s RewardAddress)
-> Decoder s RewardAddress
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> Decoder s RewardAddress)
-> (RewardAddress -> Decoder s RewardAddress)
-> Either Error RewardAddress
-> Decoder s RewardAddress
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s RewardAddress
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s RewardAddress)
-> (Error -> String) -> Error -> Decoder s RewardAddress
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) RewardAddress -> Decoder s RewardAddress
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error RewardAddress -> Decoder s RewardAddress)
-> (ByteString -> Either Error RewardAddress)
-> ByteString
-> Decoder s RewardAddress
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error RewardAddress
Reward.fromBytes
    (,) RewardAddress
account (Word64 -> (RewardAddress, Word64))
-> Decoder s Word64 -> Decoder s (RewardAddress, Word64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64
  (Error -> Decoder s Withdrawals)
-> (Withdrawals -> Decoder s Withdrawals)
-> Either Error Withdrawals
-> Decoder s Withdrawals
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s Withdrawals
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s Withdrawals)
-> (Error -> String) -> Error -> Decoder s Withdrawals
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Withdrawals -> Decoder s Withdrawals
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(RewardAddress, Word64)] -> Either Error Withdrawals
withdrawalsFromList [(RewardAddress, Word64)]
entries)