fix existing tests

This commit is contained in:
Hongrui Fang 2022-07-08 20:38:38 +08:00
parent 5dee613dd4
commit b76b5c6ec6
11 changed files with 143 additions and 80 deletions

View file

@ -106,6 +106,7 @@ mkEffectTxInfo newGovDatum =
, nextProposalId = ProposalId 0
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
governorInputDatum :: Datum
governorInputDatum = Datum $ toBuiltinData governorInputDatum'
@ -168,6 +169,7 @@ validNewGovernorDatum =
, nextProposalId = ProposalId 42
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
invalidNewGovernorDatum :: GovernorDatum
@ -180,4 +182,5 @@ invalidNewGovernorDatum =
, nextProposalId = ProposalId 42
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}

View file

@ -119,6 +119,7 @@ mintGST =
, nextProposalId = ProposalId 0
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
witness :: ValidatorHash
@ -183,6 +184,7 @@ createProposal =
, nextProposalId = thisProposalId
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
effects =
@ -216,8 +218,7 @@ createProposal =
proposalLocks :: [ProposalLock]
proposalLocks =
[ ProposalLock (ResultTag 0) thisProposalId
, ProposalLock (ResultTag 1) thisProposalId
[ Created thisProposalId
]
stakeOutputDatum :: StakeDatum
stakeOutputDatum = stakeInputDatum {lockedBy = proposalLocks}
@ -304,6 +305,7 @@ mintGATs =
, nextProposalId = ProposalId 5
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
effects =
@ -423,6 +425,7 @@ mutateState =
, nextProposalId = ProposalId 5
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
governorOutputDatum :: GovernorDatum

View file

@ -81,6 +81,7 @@ proposalCreation =
, nextProposalId = ProposalId 0
, proposalTimings = def
, createProposalTimeRangeMaxWidth = def
, maximumProposalsPerStake = 3
}
govAfter :: GovernorDatum

View file

@ -31,7 +31,7 @@ import Agora.Proposal.Time (
)
import Agora.SafeMoney (GTTag)
import Agora.Stake (
ProposalLock (ProposalLock),
ProposalLock (..),
Stake (gtClassRef),
StakeDatum (..),
StakeRedeemer (WitnessStake),
@ -99,19 +99,24 @@ data Parameters = Parameters
, stakeCount :: Integer
-- ^ The number of stakes.
, signByAllCosigners :: Bool
-- ^ Whether the transaction is signed by all the cosigners.
, perStakeGTs :: Tagged GTTag Integer
-- ^ The staked amount of each stake.
}
---
-- | Reference to the proposal UTXO.
proposalRef :: TxOutRef
proposalRef = TxOutRef proposalTxRef 1
-- | Create the reference to a particular stake UTXO.
mkStakeRef :: Int -> TxOutRef
mkStakeRef = TxOutRef stakeTxRef . (+ 2) . fromIntegral
---
-- | Default effects of the propsoal.
defEffects :: AssocMap.Map ResultTag (AssocMap.Map ValidatorHash DatumHash)
defEffects =
AssocMap.fromList
@ -119,14 +124,19 @@ defEffects =
, (ResultTag 1, AssocMap.empty)
]
-- | Empty votes for the default effects.
emptyVotes :: ProposalVotes
emptyVotes = emptyVotesFor defEffects
{- | The default proposal statring time, which doesn't really matter in this
case.
-}
proposalStartingTime :: POSIXTime
proposalStartingTime = 0
---
-- | Create the input proposal datum given the parameters.
mkProposalInputDatum :: Parameters -> ProposalDatum
mkProposalInputDatum ps =
ProposalDatum
@ -140,6 +150,7 @@ mkProposalInputDatum ps =
, startingTime = ProposalStartingTime proposalStartingTime
}
-- | Create the input stake datums given the parameters.
mkStakeInputDatums :: Parameters -> [StakeDatum]
mkStakeInputDatums ps =
map
@ -154,28 +165,37 @@ mkStakeInputDatums ps =
where
existingLocks :: [ProposalLock]
existingLocks =
[ ProposalLock (ResultTag 0) (ProposalId 0)
, ProposalLock (ResultTag 2) (ProposalId 1)
[ Voted (ProposalId 0) (ResultTag 0)
, Voted (ProposalId 1) (ResultTag 2)
]
---
-- | Script purpose of the proposal validator.
proposalScriptPurpose :: ScriptPurpose
proposalScriptPurpose = Spending proposalRef
-- | Script purpose of the stake validator, given which stake we want to spend.
mkStakeScriptPurpose :: Int -> ScriptPurpose
mkStakeScriptPurpose = Spending . mkStakeRef
---
{- | The propsoal redeemer used to spend the proposal UTXO, which is always
'AdvanceProposal' in this case.
-}
proposalRedeemer :: ProposalRedeemer
proposalRedeemer = AdvanceProposal
{- | The propsoal redeemer used to spend the stake UTXO, which is always
'WitnessStake' in this case.
-}
stakeRedeemer :: StakeRedeemer
stakeRedeemer = WitnessStake
---
-- | Create some valid stake owners.
mkStakeOwners :: Parameters -> [PubKeyHash]
mkStakeOwners ps =
sort $
@ -276,6 +296,9 @@ advance ps =
---
{- | Given the proposal status, create a time range that is in time for
advacing to the next state.
-}
mkInTimeTimeRange :: ProposalStatus -> POSIXTimeRange
mkInTimeTimeRange advanceFrom =
case advanceFrom of
@ -315,6 +338,9 @@ mkInTimeTimeRange advanceFrom =
)
Finished -> error "Cannot advance 'Finished' proposal"
{- | Given the proposal status, create a time range that is too time for
advacing to the next state.
-}
mkTooLateTimeRange :: ProposalStatus -> POSIXTimeRange
mkTooLateTimeRange advanceFrom =
case advanceFrom of
@ -363,6 +389,7 @@ mkTooLateTimeRange advanceFrom =
---
-- | Next state of the given proposal status.
getNextState :: ProposalStatus -> ProposalStatus
getNextState = \case
Draft -> VotingReady
@ -475,6 +502,9 @@ invalidOutputStakeParameters nCosigners =
---
{- | Create a test tree that runs the stake validator and proposal validator to
test the advancing functionalities.
-}
mkTestTree :: String -> Parameters -> Bool -> SpecificationTree
mkTestTree name ps isValidForProposalValidator = group name [proposal, stake]
where

View file

@ -21,7 +21,7 @@ import Agora.Proposal (
)
import Agora.Proposal.Scripts (proposalValidator)
import Agora.Proposal.Time (ProposalStartingTime (ProposalStartingTime))
import Agora.Stake (ProposalLock (ProposalLock), Stake (..), StakeDatum (..))
import Agora.Stake (ProposalLock (..), Stake (..), StakeDatum (..))
import Control.Monad (join)
import Data.Coerce (coerce)
import Data.Default.Class (Default (def))
@ -152,10 +152,8 @@ mkStakeDatumPair c =
in (input, output)
where
mkStakeLocks :: StakeRole -> ProposalId -> [ProposalLock]
mkStakeLocks Voter pid = [ProposalLock defaultVoteFor pid]
mkStakeLocks Creator pid =
map (`ProposalLock` pid) $
AssocMap.keys $ getProposalVotes votesTemplate
mkStakeLocks Voter pid = [Voted pid defaultVoteFor]
mkStakeLocks Creator pid = [Created pid]
mkStakeLocks _ _ = []
-- | Create the input proposal datum.
@ -274,7 +272,7 @@ unlockStake p =
mkProposalValidatorTestCase :: UnlockStakeParameters -> Bool -> SpecificationTree
mkProposalValidatorTestCase p shouldSucceed =
let datum = mkProposalInputDatum p $ ProposalId 0
redeemer = Unlock (ResultTag 0)
redeemer = Unlock
name = show p
scriptContext =
ScriptContext

View file

@ -17,7 +17,7 @@ import Agora.Proposal.Time (
ProposalTimingConfig (draftTime, votingTime),
)
import Agora.Stake (
ProposalLock (ProposalLock),
ProposalLock (..),
Stake (gtClassRef),
StakeDatum (..),
StakeRedeemer (PermitVote),
@ -65,9 +65,11 @@ import Test.Specification (
)
import Test.Util (closedBoundedInterval, sortValue, updateMap)
-- | Reference to the proposal UTXO.
proposalRef :: TxOutRef
proposalRef = TxOutRef proposalTxRef 0
-- | Reference to the stake UTXO.
stakeRef :: TxOutRef
stakeRef = TxOutRef stakeTxRef 1
@ -79,9 +81,11 @@ data Parameters = Parameters
-- ^ The count of votes.
}
-- | The public key hash of the stake owner.
stakeOwner :: PubKeyHash
stakeOwner = signer
-- | The votes of the input proposals.
initialVotes :: AssocMap.Map ResultTag Integer
initialVotes =
AssocMap.fromList
@ -89,6 +93,7 @@ initialVotes =
, (ResultTag 1, 4242)
]
-- | The input proposal datum.
proposalInputDatum :: ProposalDatum
proposalInputDatum =
ProposalDatum
@ -106,12 +111,16 @@ proposalInputDatum =
, startingTime = ProposalStartingTime 0
}
-- | The locks of the input stake.
existingLocks :: [ProposalLock]
existingLocks =
[ ProposalLock (ResultTag 0) (ProposalId 0)
, ProposalLock (ResultTag 2) (ProposalId 1)
[ Voted (ProposalId 0) (ResultTag 0)
, Voted (ProposalId 1) (ResultTag 2)
]
{- | Set the 'StakeDatum.stakedAmount' according to the number of votes being
casted.
-}
mkStakeInputDatum :: Parameters -> StakeDatum
mkStakeInputDatum params =
StakeDatum
@ -120,14 +129,19 @@ mkStakeInputDatum params =
, lockedBy = existingLocks
}
-- | Create the proposal redeemer. In this case @'Vote' _@ will always be used.
mkProposalRedeemer :: Parameters -> ProposalRedeemer
mkProposalRedeemer = Vote . voteFor
-- | Place new proposal locks on the stake.
mkNewLock :: Parameters -> ProposalLock
mkNewLock ps = ProposalLock ps.voteFor proposalInputDatum.proposalId
mkNewLock = Voted proposalInputDatum.proposalId . voteFor
mkStakeRedeemer :: Parameters -> StakeRedeemer
mkStakeRedeemer = PermitVote . mkNewLock
{- | The stake redeemer that is used in 'mkTestTree'. In this case it'll always be
'PermitVote'.
-}
stakeRedeemer :: StakeRedeemer
stakeRedeemer = PermitVote
-- | Create a valid transaction that votes on a propsal, given the parameters.
vote :: Parameters -> TxInfo
@ -210,6 +224,7 @@ vote params =
---
-- | Valida parameters that vote on the proposal.
validVoteParameters :: Parameters
validVoteParameters =
Parameters
@ -219,6 +234,9 @@ validVoteParameters =
---
{- | Create a test tree that runs the stake validator and proposal validator to
test the voting functionalities.
-}
mkTestTree :: String -> Parameters -> Bool -> SpecificationTree
mkTestTree name ps isValid = group name [proposal, stake]
where
@ -242,7 +260,7 @@ mkTestTree name ps isValid = group name [proposal, stake]
"stake"
(stakeValidator Shared.stake)
stakeInputDatum
(mkStakeRedeemer ps)
stakeRedeemer
( ScriptContext
txInfo
(Spending stakeRef)