From 55435d601f88416ec04248452a03ccf94fe06484 Mon Sep 17 00:00:00 2001 From: fanghr Date: Wed, 25 May 2022 17:52:05 +0800 Subject: [PATCH] fix broken tests/samples --- .../Sample/Effect/GovernorMutation.hs | 8 +++++ agora-sample/Sample/Governor.hs | 32 +++++++++++++++---- agora-sample/Sample/Proposal.hs | 15 +++++---- agora-sample/Sample/Shared.hs | 25 ++++++++++----- agora-test/Spec/Effect/GovernorMutation.hs | 14 ++++---- agora-test/Spec/Governor.hs | 21 ++++++++++-- agora-test/Spec/Proposal.hs | 5 +-- agora.cabal | 11 +++---- 8 files changed, 93 insertions(+), 38 deletions(-) diff --git a/agora-sample/Sample/Effect/GovernorMutation.hs b/agora-sample/Sample/Effect/GovernorMutation.hs index 7d41cb7..e8c7b79 100644 --- a/agora-sample/Sample/Effect/GovernorMutation.hs +++ b/agora-sample/Sample/Effect/GovernorMutation.hs @@ -37,7 +37,9 @@ import Plutus.V1.Ledger.Value (AssetClass, assetClass) import Plutus.V1.Ledger.Value qualified as Value import Sample.Shared ( authorityTokenSymbol, + defaultCreateProposalTimeRangeMaxDuration, defaultProposalThresholds, + defaultProposalTimingConfig, govAssetClass, govValidatorAddress, governor, @@ -101,6 +103,8 @@ mkEffectTxInfo newGovDatum = GovernorDatum { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 0 + , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } governorInputDatum :: Datum governorInputDatum = Datum $ toBuiltinData governorInputDatum' @@ -161,6 +165,8 @@ validNewGovernorDatum = GovernorDatum { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 42 + , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } invalidNewGovernorDatum :: GovernorDatum @@ -171,4 +177,6 @@ invalidNewGovernorDatum = { countVoting = Tagged (-1) } , nextProposalId = ProposalId 42 + , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } diff --git a/agora-sample/Sample/Governor.hs b/agora-sample/Sample/Governor.hs index 29fcd2d..dcc53e2 100644 --- a/agora-sample/Sample/Governor.hs +++ b/agora-sample/Sample/Governor.hs @@ -64,8 +64,13 @@ import Agora.Stake ( -------------------------------------------------------------------------------- +import Agora.Proposal.Time ( + ProposalStartingTime (ProposalStartingTime), + ProposalTimingConfig (..), + ) import Sample.Shared ( authorityTokenSymbol, + defaultCreateProposalTimeRangeMaxDuration, defaultProposalThresholds, defaultProposalTimingConfig, govAssetClass, @@ -74,15 +79,15 @@ import Sample.Shared ( gstUTXORef, minAda, proposalPolicySymbol, + proposalStartingTimeFromTimeRange, proposalValidatorAddress, signer, signer2, stake, stakeAddress, stakeAssetClass, - tmpProposalStartingTime, ) -import Test.Util (datumPair, toDatumHash) +import Test.Util (closedBoundedInterval, datumPair, toDatumHash) -------------------------------------------------------------------------------- @@ -115,6 +120,7 @@ mintGST = { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 0 , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } governorOutputDatum :: Datum governorOutputDatum = Datum $ toBuiltinData governorOutputDatum' @@ -209,6 +215,7 @@ createProposal = { proposalThresholds = defaultProposalThresholds , nextProposalId = thisProposalId , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } governorInputDatum :: Datum governorInputDatum = Datum $ toBuiltinData governorInputDatum' @@ -239,7 +246,7 @@ createProposal = , thresholds = defaultProposalThresholds , votes = emptyVotesFor effects , timingConfig = defaultProposalTimingConfig - , startingTime = tmpProposalStartingTime + , startingTime = proposalStartingTimeFromTimeRange validTimeRange } ) proposalOutput :: TxOut @@ -300,8 +307,13 @@ createProposal = } --- + ownInputRef :: TxOutRef ownInputRef = TxOutRef "4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865" 1 + + --- + + validTimeRange = closedBoundedInterval 10 15 in ScriptContext { scriptContextTxInfo = TxInfo @@ -318,7 +330,7 @@ createProposal = , txInfoMint = pst , txInfoDCert = [] , txInfoWdrl = [] - , txInfoValidRange = Interval.always + , txInfoValidRange = validTimeRange , txInfoSignatories = [signer] , txInfoData = datumPair @@ -381,6 +393,7 @@ mintGATs = { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 5 , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } governorInputDatum :: Datum governorInputDatum = Datum $ toBuiltinData governorInputDatum' @@ -416,7 +429,7 @@ mintGATs = , thresholds = defaultProposalThresholds , votes = proposalVotes , timingConfig = defaultProposalTimingConfig - , startingTime = tmpProposalStartingTime + , startingTime = ProposalStartingTime 10 } proposalInputDatum :: Datum proposalInputDatum = Datum $ toBuiltinData proposalInputDatum' @@ -468,6 +481,12 @@ mintGATs = ownInputRef :: TxOutRef ownInputRef = TxOutRef "4355a46b19d348dc2f57c046f8ef63d4538ebb936000f3c9ee954a27460dd865" 1 + + -- + validTimeRange = + closedBoundedInterval + (defaultProposalTimingConfig.lockingTime + 11) + (defaultProposalTimingConfig.executingTime - 11) in ScriptContext { scriptContextTxInfo = TxInfo @@ -486,7 +505,7 @@ mintGATs = , txInfoMint = gat , txInfoDCert = [] , txInfoWdrl = [] - , txInfoValidRange = Interval.always + , txInfoValidRange = validTimeRange , txInfoSignatories = [signer, signer2] , txInfoData = datumPair @@ -569,6 +588,7 @@ mutateState = { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 5 , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } governorInputDatum :: Datum governorInputDatum = Datum $ toBuiltinData governorInputDatum' diff --git a/agora-sample/Sample/Proposal.hs b/agora-sample/Sample/Proposal.hs index 2338939..cea8be0 100644 --- a/agora-sample/Sample/Proposal.hs +++ b/agora-sample/Sample/Proposal.hs @@ -33,7 +33,6 @@ import Plutus.V1.Ledger.Api ( TxOut (TxOut, txOutAddress, txOutDatumHash, txOutValue), TxOutRef (TxOutRef), ) -import Plutus.V1.Ledger.Interval qualified as Interval import Plutus.V1.Ledger.Value qualified as Value -------------------------------------------------------------------------------- @@ -50,7 +49,7 @@ import Agora.Proposal ( ResultTag (..), emptyVotesFor, ) -import Agora.Proposal.Time (ProposalTimingConfig (..)) +import Agora.Proposal.Time (ProposalStartingTime (ProposalStartingTime), ProposalTimingConfig (..)) import Agora.Stake (ProposalLock (ProposalLock), Stake (..), StakeDatum (..)) import Plutarch.SafeMoney (Tagged (Tagged), untag) import PlutusTx.AssocMap qualified as AssocMap @@ -80,7 +79,7 @@ proposalCreation = , thresholds = defaultProposalThresholds , votes = emptyVotesFor effects , timingConfig = defaultProposalTimingConfig - , startingTime = tmpProposalStartingTime + , startingTime = proposalStartingTimeFromTimeRange validTimeRange } ) @@ -92,6 +91,7 @@ proposalCreation = { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 0 , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } ) govAfter :: Datum @@ -102,8 +102,11 @@ proposalCreation = { proposalThresholds = defaultProposalThresholds , nextProposalId = ProposalId 1 , proposalTimings = defaultProposalTimingConfig + , createProposalTimeRangeMaxDuration = defaultCreateProposalTimeRangeMaxDuration } ) + + validTimeRange = closedBoundedInterval 10 15 in ScriptContext { scriptContextTxInfo = TxInfo @@ -140,7 +143,7 @@ proposalCreation = , txInfoMint = st , txInfoDCert = [] , txInfoWdrl = [] - , txInfoValidRange = Interval.always + , txInfoValidRange = validTimeRange , txInfoSignatories = [signer] , txInfoData = [ datumPair proposalDatum @@ -177,7 +180,7 @@ cosignProposal newSigners = , thresholds = defaultProposalThresholds , votes = emptyVotesFor effects , timingConfig = defaultProposalTimingConfig - , startingTime = tmpProposalStartingTime + , startingTime = ProposalStartingTime 0 } stakeDatum :: StakeDatum stakeDatum = StakeDatum (Tagged 50_000_000) signer2 [] @@ -298,7 +301,7 @@ voteOnProposal params = , thresholds = defaultProposalThresholds , votes = ProposalVotes initialVotes , timingConfig = defaultProposalTimingConfig - , startingTime = tmpProposalStartingTime + , startingTime = ProposalStartingTime 0 } proposalInputDatum :: Datum proposalInputDatum = Datum $ toBuiltinData proposalInputDatum' diff --git a/agora-sample/Sample/Shared.hs b/agora-sample/Sample/Shared.hs index c100378..8d44130 100644 --- a/agora-sample/Sample/Shared.hs +++ b/agora-sample/Sample/Shared.hs @@ -37,7 +37,8 @@ module Sample.Shared ( proposalValidatorHash, proposalValidatorAddress, defaultProposalTimingConfig, - tmpProposalStartingTime, + defaultCreateProposalTimeRangeMaxDuration, + proposalStartingTimeFromTimeRange, -- ** Authority authorityToken, @@ -77,7 +78,7 @@ import Agora.Proposal ( ProposalThresholds (..), ) import Agora.Proposal.Time ( - ProposalStartingTime (..), + ProposalStartingTime (ProposalStartingTime), ProposalTimingConfig (..), ) import Agora.Stake (Stake (..)) @@ -95,9 +96,15 @@ import Plutus.V1.Ledger.Api ( Address (Address), Credential (ScriptCredential), CurrencySymbol, + Extended (..), + Interval (..), + LowerBound (..), MintingPolicy (..), + POSIXTime, + POSIXTimeRange, PubKeyHash, TxOutRef (TxOutRef), + UpperBound (..), Value, ) import Plutus.V1.Ledger.Contexts ( @@ -199,12 +206,14 @@ defaultProposalTimingConfig = , executingTime = 3000 } -{- | Hard coded starting time of every propoal. - This will be calculated by the governor in the future. - FIXME: Remove this. --} -tmpProposalStartingTime :: ProposalStartingTime -tmpProposalStartingTime = ProposalStartingTime 0 +defaultCreateProposalTimeRangeMaxDuration :: POSIXTime +defaultCreateProposalTimeRangeMaxDuration = 10 + +proposalStartingTimeFromTimeRange :: POSIXTimeRange -> ProposalStartingTime +proposalStartingTimeFromTimeRange + (Interval (LowerBound (Finite l) True) (UpperBound (Finite u) True)) = + ProposalStartingTime $ (l + u) `div` 2 +proposalStartingTimeFromTimeRange _ = error "Given time range should be finite and closed" ------------------------------------------------------------------ diff --git a/agora-test/Spec/Effect/GovernorMutation.hs b/agora-test/Spec/Effect/GovernorMutation.hs index 1436b5a..fd70f34 100644 --- a/agora-test/Spec/Effect/GovernorMutation.hs +++ b/agora-test/Spec/Effect/GovernorMutation.hs @@ -27,9 +27,10 @@ tests = "governor validator should pass" (governorValidator Shared.governor) ( GovernorDatum - { proposalThresholds = Shared.defaultProposalThresholds - , nextProposalId = ProposalId 0 - } + Shared.defaultProposalThresholds + (ProposalId 0) + Shared.defaultProposalTimingConfig + Shared.defaultCreateProposalTimeRangeMaxDuration ) MutateGovernor ( ScriptContext @@ -48,9 +49,10 @@ tests = "governor validator should fail" (governorValidator Shared.governor) ( GovernorDatum - { proposalThresholds = Shared.defaultProposalThresholds - , nextProposalId = ProposalId 0 - } + Shared.defaultProposalThresholds + (ProposalId 0) + Shared.defaultProposalTimingConfig + Shared.defaultCreateProposalTimeRangeMaxDuration ) MutateGovernor ( ScriptContext diff --git a/agora-test/Spec/Governor.hs b/agora-test/Spec/Governor.hs index 0d7d827..7d324fb 100644 --- a/agora-test/Spec/Governor.hs +++ b/agora-test/Spec/Governor.hs @@ -38,19 +38,34 @@ tests = [ validatorSucceedsWith "proposal creation" (governorValidator Shared.governor) - (GovernorDatum Shared.defaultProposalThresholds (ProposalId 0) Shared.defaultProposalTimingConfig) + ( GovernorDatum + Shared.defaultProposalThresholds + (ProposalId 0) + Shared.defaultProposalTimingConfig + Shared.defaultCreateProposalTimeRangeMaxDuration + ) CreateProposal createProposal , validatorSucceedsWith "GATs minting" (governorValidator Shared.governor) - (GovernorDatum Shared.defaultProposalThresholds (ProposalId 5) Shared.defaultProposalTimingConfig) + ( GovernorDatum + Shared.defaultProposalThresholds + (ProposalId 5) + Shared.defaultProposalTimingConfig + Shared.defaultCreateProposalTimeRangeMaxDuration + ) MintGATs mintGATs , validatorSucceedsWith "mutate governor state" (governorValidator Shared.governor) - (GovernorDatum Shared.defaultProposalThresholds (ProposalId 5) Shared.defaultProposalTimingConfig) + ( GovernorDatum + Shared.defaultProposalThresholds + (ProposalId 5) + Shared.defaultProposalTimingConfig + Shared.defaultCreateProposalTimeRangeMaxDuration + ) MutateGovernor mutateState ] diff --git a/agora-test/Spec/Proposal.hs b/agora-test/Spec/Proposal.hs index 5eab61d..dc0f1cc 100644 --- a/agora-test/Spec/Proposal.hs +++ b/agora-test/Spec/Proposal.hs @@ -31,6 +31,7 @@ import Agora.Proposal.Scripts ( proposalPolicy, proposalValidator, ) +import Agora.Proposal.Time (ProposalStartingTime (ProposalStartingTime)) import Agora.Stake ( ProposalLock (ProposalLock), StakeDatum (StakeDatum), @@ -83,7 +84,7 @@ tests = , (ResultTag 1, AssocMap.empty) ] , timingConfig = Shared.defaultProposalTimingConfig - , startingTime = Shared.tmpProposalStartingTime + , startingTime = ProposalStartingTime 0 } ) (Cosign [signer2]) @@ -118,7 +119,7 @@ tests = ] ) , timingConfig = Shared.defaultProposalTimingConfig - , startingTime = Shared.tmpProposalStartingTime + , startingTime = ProposalStartingTime 0 } ) (Vote (ResultTag 0)) diff --git a/agora.cabal b/agora.cabal index 9014c21..95189c6 100644 --- a/agora.cabal +++ b/agora.cabal @@ -141,22 +141,20 @@ library Agora.Proposal.Time Agora.Record Agora.SafeMoney + Agora.ScriptInfo Agora.Stake Agora.Stake.Scripts Agora.Treasury Agora.Utils Agora.Utils.Value - Agora.ScriptInfo - other-modules: - Agora.Aeson.Orphans + other-modules: Agora.Aeson.Orphans hs-source-dirs: agora library pprelude default-language: Haskell2010 exposed-modules: PPrelude hs-source-dirs: agora - build-depends: , base , plutarch @@ -176,8 +174,8 @@ library agora-sample Sample.Shared Sample.Stake Sample.Treasury - hs-source-dirs: agora-sample + hs-source-dirs: agora-sample build-depends: agora-testlib test-suite agora-test @@ -214,8 +212,7 @@ executable agora-scripts import: lang, deps, exe-opts main-is: Scripts.hs hs-source-dirs: agora-scripts - other-modules: - Options + other-modules: Options build-depends: , agora , optparse-applicative