allow multiple proposals in the samples of unlocking stake

This commit is contained in:
Hongrui Fang 2022-06-17 23:01:44 +08:00
parent 76a9cf79ba
commit 36a558bf30
7 changed files with 422 additions and 560 deletions

View file

@ -36,34 +36,13 @@ import Agora.Stake (
StakeRedeemer (PermitVote, WitnessStake),
)
import Agora.Stake.Scripts (stakeValidator)
import Control.Monad (join)
import Data.Default.Class (Default (def))
import Data.Tagged (Tagged (Tagged), untag)
import PlutusLedgerApi.V1 (ScriptContext (..), ScriptPurpose (..))
import PlutusTx.AssocMap qualified as AssocMap (empty, fromList)
import Sample.Proposal qualified as Proposal (
TransitionParameters (
TransitionParameters,
initialProposalStatus,
proposalStartingTime
),
VotingParameters (VotingParameters, voteCount, voteFor),
advanceFinishedPropsoal,
advanceProposalFailureTimeout,
advanceProposalInsufficientVotes,
advanceProposalSuccess,
advancePropsoalWithInvalidOutputStake,
cosignProposal,
creatorRetractVotesWhile,
creatorUnlockStakeWhile,
proposalCreation,
proposalRef,
stakeRef,
unlockStakeAndRetractVotesUsingIrrelevantStakeWhile,
unlockStakeUsingIrrelevantStakeWhile,
voteOnProposal,
voterUnlockStakeAndRetractVotesWhile,
voterUnlockStakeWhile,
)
import PlutusTx.AssocMap qualified as AssocMap
import Sample.Proposal qualified as Proposal
import Sample.Proposal.UnlockStake qualified as UnlockStake
import Sample.Shared (signer, signer2)
import Sample.Shared qualified as Shared (proposal, stake)
import Test.Specification (
@ -364,247 +343,110 @@ specs =
]
, group
"unlocking"
[ group
"legal"
[ validatorSucceedsWith
"retract votes and unlock stake while voting"
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = VotingReady
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
$ map
( \pc ->
group
(show pc <> " proposals")
[ group
"legal"
[ group
"retract votes and unlock stake while voting"
[ UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Voter
True
VotingReady
)
True
]
, group
"unlock the stake that has been used to create the proposal"
[ UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Creator
False
Finished
)
True
]
, group "unlock stake after voting" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Voter
False
ps
)
True
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(Proposal.voterUnlockStakeAndRetractVotesWhile VotingReady)
(Spending Proposal.proposalRef)
)
, validatorSucceedsWith
"unlock the stake that has been used to create the proposal"
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = Finished
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
[Finished, Locked]
]
, group
"illegal"
[ group "retract votes while the proposal is not voting ready" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Voter
True
ps
)
False
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(Proposal.creatorUnlockStakeWhile Finished)
(Spending Proposal.proposalRef)
)
, group "unlock stake after voting" $
map
( \ps ->
validatorSucceedsWith
(show ps)
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = ps
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(Proposal.voterUnlockStakeWhile ps)
(Spending Proposal.proposalRef)
)
)
[Locked, Finished]
]
, group
"illegal"
[ group "retract votes while the proposal is not voting ready" $
map
( \ps ->
validatorFailsWith
(show ps)
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = ps
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(Proposal.voterUnlockStakeAndRetractVotesWhile ps)
(Spending Proposal.proposalRef)
)
)
[Draft, Locked, Finished]
, group
"irrelevant stake"
$ foldMap
( \(f, s) ->
map
( \ps ->
validatorFailsWith
(s <> " (" <> show ps <> ")")
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = ps
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
[Draft, Locked, Finished]
, group "irrelevant stake" $
join $
map
( \rv ->
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Irrelevant
rv
ps
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(f ps)
(Spending Proposal.proposalRef)
)
)
[Draft, VotingReady, Locked, Finished]
)
[ (Proposal.unlockStakeAndRetractVotesUsingIrrelevantStakeWhile, "unlock stake + retract votes")
, (Proposal.unlockStakeUsingIrrelevantStakeWhile, "unlock stake")
]
, group "unlock stake that has been used to create the proposal before finished" $
map
( \ps ->
validatorFailsWith
(show ps)
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = ps
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
False
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(Proposal.creatorUnlockStakeWhile ps)
(Spending Proposal.proposalRef)
)
)
[Draft, VotingReady, Locked]
, group "creator stake retract votes" $
map
( \ps ->
validatorFailsWith
(show ps)
(proposalValidator Shared.proposal)
( ProposalDatum
{ proposalId = ProposalId 0
, effects =
AssocMap.fromList
[ (ResultTag 0, AssocMap.empty)
, (ResultTag 1, AssocMap.empty)
]
, status = ps
, cosigners = [signer]
, thresholds = def
, votes =
ProposalVotes
( AssocMap.fromList
[ (ResultTag 0, 42)
, (ResultTag 1, 0)
]
)
, timingConfig = def
, startingTime = ProposalStartingTime 0
}
)
(Unlock (ResultTag 0))
( ScriptContext
(Proposal.creatorRetractVotesWhile ps)
(Spending Proposal.proposalRef)
)
)
[Draft, VotingReady, Locked, Finished]
]
]
[Draft, VotingReady, Locked, Finished]
)
[True, False]
, group "unlock stake that has been used to create the proposal before finished" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Creator
False
ps
)
False
)
[Draft, VotingReady, Locked]
, group "creator stake retract votes" $
map
( \ps ->
UnlockStake.mkProposalValidatorTestCase
( UnlockStake.UnlockStakeParameters
pc
UnlockStake.Creator
True
ps
)
False
)
[Draft, VotingReady, Locked, Finished]
]
]
)
[1, 25]
]
]