cometa-0.1.0.0: Idiomatic Haskell bindings to cardano-c
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cometa.Governance.Voting

Description

Assemble role-aware votes on governance actions. Validate identities once, create a VotingProcedure with an optional explanation, and associate it with a voter/action pair. Repeated pairs fail explicitly rather than lose votes.

import qualified Cometa.Governance.ActionId as ActionId
import qualified Cometa.Governance.Voting as Voting

support :: Voting.Voter -> ActionId.ActionId -> Voting.VotingProcedures
support voter action =
  Voting.singleton voter action (Voting.VotingProcedure Voting.Yes Nothing)
Synopsis

Choices and authorities

data Vote Source #

A governance voting choice. Ordering follows CBOR codes: No=0, Yes=1, Abstain=2. Abstention is an explicit choice, distinct from an absent vote.

Constructors

No 
Yes 
Abstain 

Instances

Instances details
Bounded Vote Source # 
Instance details

Defined in Cometa.Internal.Governance

Enum Vote Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

succ :: Vote -> Vote #

pred :: Vote -> Vote #

toEnum :: Int -> Vote #

fromEnum :: Vote -> Int #

enumFrom :: Vote -> [Vote] #

enumFromThen :: Vote -> Vote -> [Vote] #

enumFromTo :: Vote -> Vote -> [Vote] #

enumFromThenTo :: Vote -> Vote -> Vote -> [Vote] #

Show Vote Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

showsPrec :: Int -> Vote -> ShowS #

show :: Vote -> String #

showList :: [Vote] -> ShowS #

Eq Vote Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

(==) :: Vote -> Vote -> Bool #

(/=) :: Vote -> Vote -> Bool #

Ord Vote Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

compare :: Vote -> Vote -> Ordering #

(<) :: Vote -> Vote -> Bool #

(<=) :: Vote -> Vote -> Bool #

(>) :: Vote -> Vote -> Bool #

(>=) :: Vote -> Vote -> Bool #

max :: Vote -> Vote -> Vote #

min :: Vote -> Vote -> Vote #

data Voter Source #

Authority casting a vote. Committee votes use the hot credential; stake pools have key authorities only. Ordering follows native voter codes (committee keyscript, DRep keyscript, pool key), then exact hash bytes.

Constructors

CommitteeVoter !(Credential CommitteeHot)

Constitutional committee hot key or script.

DRepVoter !(Credential DRep)

DRep key or script; special delegation targets cannot vote.

StakePoolVoter !(KeyHash StakePool)

Stake pool verification-key hash.

Instances

Instances details
Show Voter Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

showsPrec :: Int -> Voter -> ShowS #

show :: Voter -> String #

showList :: [Voter] -> ShowS #

Eq Voter Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

(==) :: Voter -> Voter -> Bool #

(/=) :: Voter -> Voter -> Bool #

Ord Voter Source # 
Instance details

Defined in Cometa.Internal.Governance

Methods

compare :: Voter -> Voter -> Ordering #

(<) :: Voter -> Voter -> Bool #

(<=) :: Voter -> Voter -> Bool #

(>) :: Voter -> Voter -> Bool #

(>=) :: Voter -> Voter -> Bool #

max :: Voter -> Voter -> Voter #

min :: Voter -> Voter -> Voter #

data VotingProcedure Source #

A voting choice and optional explanation. Nothing encodes as CBOR null; a present anchor carries a validated metadata location and payload hash.

Constructors

VotingProcedure 

Fields

Instances

Instances details
Show VotingProcedure Source # 
Instance details

Defined in Cometa.Internal.Governance

Eq VotingProcedure Source # 
Instance details

Defined in Cometa.Internal.Governance

Ord VotingProcedure Source # 
Instance details

Defined in Cometa.Internal.Governance

encodeVoter :: Voter -> ByteString Source #

Encode a voter as definite CBOR [kind, hash bytes]. Kinds are 0 committee key, 1 committee script, 2 DRep key, 3 DRep script, and 4 stake pool key.

decodeVoter :: ByteString -> Either Error Voter Source #

Decode exactly one voter, enforcing kind, arity, and 28-byte hash length. Definite/indefinite arrays and nonminimal codes are accepted. Invalid input or trailing bytes returns InvalidGovernanceCbor. The original encoding is not retained.

encodeProcedure :: VotingProcedure -> ByteString Source #

Encode a procedure as definite CBOR [choice, anchor or null], using choice codes 0 (No), 1 (Yes) and 2 (Abstain).

decodeProcedure :: ByteString -> Either Error VotingProcedure Source #

Decode exactly one procedure with a validated optional anchor. Invalid choice, arity, anchor, or trailing bytes returns InvalidGovernanceCbor. Definite/indefinite arrays and nonminimal codes are accepted; original encoding is not retained.

Unique voter/action collections

data VotingProcedures Source #

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.

Instances

Instances details
Show VotingProcedures Source # 
Instance details

Defined in Cometa.Internal.Governance

Eq VotingProcedures Source # 
Instance details

Defined in Cometa.Internal.Governance

Ord VotingProcedures Source # 
Instance details

Defined in Cometa.Internal.Governance

singleton :: Voter -> ActionId -> VotingProcedure -> VotingProcedures Source #

One voter casting one procedure on one action. All arguments are validated domain values, so assembly cannot fail.

fromList :: [(Voter, ActionId, VotingProcedure)] -> Either Error VotingProcedures Source #

Assemble triples in any order. Repeating a voter/action pair returns DuplicateGovernanceVote, even when the procedures agree. No vote is discarded.

toList :: VotingProcedures -> [(Voter, ActionId, VotingProcedure)] Source #

Entries sorted by voter kindhash, then transaction hashindex. Input insertion order is not retained by this semantic collection.

insert :: Voter -> ActionId -> VotingProcedure -> VotingProcedures -> Either Error VotingProcedures Source #

Add a vote, rejecting an existing voter/action pair with DuplicateGovernanceVote. Other votes remain unchanged.

lookup :: Voter -> ActionId -> VotingProcedures -> Maybe VotingProcedure Source #

Find the procedure for a voter/action pair; absence is legitimate.

merge :: VotingProcedures -> VotingProcedures -> Either Error VotingProcedures Source #

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.

Encoding identity

data EncodedVotingProcedures Source #

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.

encodeCbor :: VotingProcedures -> ByteString Source #

Encode fresh nested maps ordered by voter kind/hash, then transaction hash/index, using definite containers and shortest integers. Re-encoding inspected external data can change its containing transaction identity; toCbor preserves the imported encoding instead.

decodeCbor :: ByteString -> Either Error EncodedVotingProcedures Source #

Decode exactly one nested voting map, preserving its exact input bytes. Definite/indefinite containers and nonminimal encodings are accepted. Duplicate voter keys, duplicate action keys within a voter, and empty inner maps are rejected. Malformed CBOR, invalid schema, bounds, UTF-8, or trailing bytes returns InvalidGovernanceCbor.

fromVotingProcedures :: VotingProcedures -> EncodedVotingProcedures Source #

Assign fresh minimal, sorted CBOR to a semantic collection.

toVotingProcedures :: EncodedVotingProcedures -> VotingProcedures Source #

Inspect votes without changing their preserved encoding.

toCbor :: EncodedVotingProcedures -> ByteString Source #

The exact imported encoding, or the fresh encoding assigned by fromVotingProcedures.