fix threshold check (VotingReady -> Locked)

This commit is contained in:
Hongrui Fang 2022-06-28 17:48:11 +08:00
parent 435050f204
commit 89e9196983
2 changed files with 35 additions and 8 deletions

View file

@ -32,6 +32,7 @@ module Agora.Proposal (
proposalDatumValid, proposalDatumValid,
pemptyVotesFor, pemptyVotesFor,
pwinner, pwinner,
pwinner',
pneutralOption, pneutralOption,
pretractVotes, pretractVotes,
) where ) where
@ -709,10 +710,8 @@ proposalDatumValid proposal =
, ptraceIfFalse "Proposal votes and effects are compatible with each other" $ PUM.pkeysEqual # datum.effects # pto (pfromData datum.votes) , ptraceIfFalse "Proposal votes and effects are compatible with each other" $ PUM.pkeysEqual # datum.effects # pto (pfromData datum.votes)
] ]
{- | Find the winner result tag, given the votes, the quorum the "neutral" result tag. {- | Wrapper for 'pwinner''. When the winner cannot be found,
the 'neutral' option will be returned.
The winner should be unambiguous, meaning that if two options have the same highest votes,
the "neutral" option will be the winner.
@since 0.1.0 @since 0.1.0
-} -}
@ -725,7 +724,26 @@ pwinner ::
:--> PResultTag :--> PResultTag
) )
pwinner = phoistAcyclic $ pwinner = phoistAcyclic $
plam $ \votes quorum neutral -> unTermCont $ do plam $ \votes quorum neutral -> pmatch (pwinner' # votes # quorum) $ \case
PNothing -> neutral
PJust winner -> winner
{- | Find the winner result tag, given the votes and the quorum.
The winner should be unambiguous, meaning that if two options have the same highest votes,
the function will return 'PNothing'.
@since 0.1.0
-}
pwinner' ::
Term
s
( PProposalVotes
:--> PInteger
:--> PMaybe PResultTag
)
pwinner' = phoistAcyclic $
plam $ \votes quorum -> unTermCont $ do
winner <- pletC $ phighestVotes # votes winner <- pletC $ phighestVotes # votes
winnerResultTag <- pletC $ pfromData $ pfstBuiltin # winner winnerResultTag <- pletC $ pfromData $ pfstBuiltin # winner
highestVotes <- pletC $ pfromData $ psndBuiltin # winner highestVotes <- pletC $ pfromData $ psndBuiltin # winner
@ -757,10 +775,10 @@ pwinner = phoistAcyclic $
pure $ pure $
pif pif
(noDuplicateHighestVotes #&& exceedQuorum) (noDuplicateHighestVotes #&& exceedQuorum)
winnerResultTag (pcon $ PJust winnerResultTag)
neutral (pcon PNothing)
{- | Find the winning outcome (and the corresponding vote count) given the votes. {- | Find the outcome with the highest vote count given the votes.
@since 0.1.0 @since 0.1.0
-} -}

View file

@ -18,6 +18,7 @@ import Agora.Proposal (
Proposal (governorSTAssetClass, stakeSTAssetClass), Proposal (governorSTAssetClass, stakeSTAssetClass),
ProposalStatus (..), ProposalStatus (..),
pretractVotes, pretractVotes,
pwinner',
) )
import Agora.Proposal.Time ( import Agora.Proposal.Time (
currentProposalTime, currentProposalTime,
@ -66,6 +67,7 @@ import Plutarch.Extra.TermCont (
ptryFromC, ptryFromC,
) )
import Plutarch.SafeMoney (PDiscrete (..)) import Plutarch.SafeMoney (PDiscrete (..))
import Plutarch.Unsafe (punsafeCoerce)
import PlutusLedgerApi.V1.Value (AssetClass (AssetClass)) import PlutusLedgerApi.V1.Value (AssetClass (AssetClass))
{- | Policy for Proposals. {- | Policy for Proposals.
@ -528,6 +530,8 @@ proposalValidator proposal =
pguardC "Cannot advance ahead of time" notTooEarly pguardC "Cannot advance ahead of time" notTooEarly
pguardC "Finished proposals cannot be advanced" $ pnot # isFinished pguardC "Finished proposals cannot be advanced" $ pnot # isFinished
thresholdsF <- pletFieldsC @'["execute"] proposalF.thresholds
pure $ pure $
pif pif
notTooLate notTooLate
@ -546,6 +550,11 @@ proposalValidator proposal =
pguardC "Proposal status set to Locked" $ pguardC "Proposal status set to Locked" $
proposalOutStatus #== pconstantData Locked proposalOutStatus #== pconstantData Locked
pguardC "Winner outcome not found" $
pisJust #$ pwinner' # proposalF.votes
#$ punsafeCoerce
$ pfromData thresholdsF.execute
pure $ popaque (pconstant ()) pure $ popaque (pconstant ())
PLocked _ -> unTermCont $ do PLocked _ -> unTermCont $ do
-- 'Locked' -> 'Finished' -- 'Locked' -> 'Finished'