From 51c4955a378001f9ff6192f035cd89a50ab84032 Mon Sep 17 00:00:00 2001 From: Hongrui Fang Date: Sun, 17 Jul 2022 21:56:29 +0800 Subject: [PATCH] add tests for minting GST --- agora-specs/Sample/Governor/Initialize.hs | 263 ++++++++++++++++++++++ agora-specs/Spec/Governor.hs | 37 ++- agora.cabal | 1 + bench.csv | 3 +- 4 files changed, 282 insertions(+), 22 deletions(-) create mode 100644 agora-specs/Sample/Governor/Initialize.hs diff --git a/agora-specs/Sample/Governor/Initialize.hs b/agora-specs/Sample/Governor/Initialize.hs new file mode 100644 index 0000000..db7f1d7 --- /dev/null +++ b/agora-specs/Sample/Governor/Initialize.hs @@ -0,0 +1,263 @@ +{- | +Module : Sample.Governor.Initialize +Maintainer : connor@mlabs.city +Description: Generate sample data for testing the functionalities of minting GST. + +Sample and utilities for testing the functionalities of minting GST. +-} +module Sample.Governor.Initialize ( + mintGST, + Parameters (..), + totallyValidParameters, + invalidDatumTimingConfigParameters, + invalidDatumMaxTimeRangeWidthParameters, + invalidDatumThresholdsParameters, + withoutGovernorDatumParameters, + witnessNotPresentedParameters, + mintMoreThanOneGSTParameters, + mintGSTWithNoneEmptyNameParameters, + mkTestCase, +) where + +import Agora.Governor (Governor (..), GovernorDatum (..)) +import Agora.Governor.Scripts ( + governorPolicy, + governorSTAssetClassFromGovernor, + governorValidatorHash, + ) +import Agora.Proposal (ProposalId (..), ProposalThresholds (..)) +import Agora.Proposal.Time (MaxTimeRangeWidth (MaxTimeRangeWidth), ProposalTimingConfig (ProposalTimingConfig)) +import Data.Default (Default (..)) +import Plutarch.Api.V1 (mintingPolicySymbol, mkMintingPolicy) +import Plutarch.Context ( + BaseBuilder, + buildTxInfoUnsafe, + input, + mint, + output, + pubKey, + script, + signedWith, + txId, + withDatum, + withOutRef, + withValue, + ) +import PlutusLedgerApi.V1 ( + CurrencySymbol, + MintingPolicy, + ScriptContext (..), + ScriptPurpose (Minting), + TxInfo, + TxOutRef (TxOutRef), + ValidatorHash, + ) +import PlutusLedgerApi.V1.Value (AssetClass (..)) +import PlutusLedgerApi.V1.Value qualified as Value +import Sample.Shared ( + minAda, + ) +import Sample.Shared qualified as Shared +import Test.Specification (SpecificationTree, testPolicy) +import Test.Util (pubKeyHashes, sortValue) + +data Parameters = Parameters + { datumThresholdsValid :: Bool + , datumMaxTimeRangeWidthValid :: Bool + , datumTimingConfigValid :: Bool + , withGovernorDatum :: Bool + , presentWitness :: Bool + , mintMoreThanOneStateToken :: Bool + , mintStateTokenWithName :: Bool + } + +-------------------------------------------------------------------------------- + +validGovernorOutputDatum :: GovernorDatum +validGovernorOutputDatum = + GovernorDatum + { proposalThresholds = def + , nextProposalId = ProposalId 0 + , proposalTimings = def + , createProposalTimeRangeMaxWidth = def + , maximumProposalsPerStake = 3 + } + +invalidProposalThresholds :: ProposalThresholds +invalidProposalThresholds = ProposalThresholds (-1) (-1) (-1) + +invalidMaxTimeRangeWidth :: MaxTimeRangeWidth +invalidMaxTimeRangeWidth = MaxTimeRangeWidth 0 + +invalidProposalTimings :: ProposalTimingConfig +invalidProposalTimings = ProposalTimingConfig (-1) (-1) (-1) (-1) + +witnessRef :: TxOutRef +witnessRef = TxOutRef "b0353c22b0bd6c5296a8eef160ba25d90b5dc82a9bb8bdaa6823ffc19515d6ad" 0 + +governor :: Governor +governor = + Shared.governor + { gstOutRef = witnessRef + } + +govAssetClass :: AssetClass +govAssetClass = governorSTAssetClassFromGovernor governor + +govValidatorHash :: ValidatorHash +govValidatorHash = governorValidatorHash governor + +govPolicy :: MintingPolicy +govPolicy = mkMintingPolicy (governorPolicy governor) + +govSymbol :: CurrencySymbol +govSymbol = mintingPolicySymbol govPolicy + +-------------------------------------------------------------------------------- + +mintGST :: Parameters -> TxInfo +mintGST ps = buildTxInfoUnsafe builder + where + gstAC = + if ps.mintStateTokenWithName + then AssetClass (govSymbol, "12345") + else govAssetClass + gstCount = + if ps.mintMoreThanOneStateToken + then 10 + else 1 + gst = Value.assetClassValue gstAC gstCount + + --- + + governorOutputDatum = + let th = if ps.datumThresholdsValid then def else invalidProposalThresholds + trw = if ps.datumMaxTimeRangeWidthValid then def else invalidMaxTimeRangeWidth + ptc = if ps.datumTimingConfigValid then def else invalidProposalTimings + in validGovernorOutputDatum + { proposalThresholds = th + , proposalTimings = ptc + , createProposalTimeRangeMaxWidth = trw + } + + governorValue = sortValue $ gst <> minAda + + --- + + witnessValue = minAda + witnessPubKey = head pubKeyHashes + + --- + + witnessBuilder :: BaseBuilder + witnessBuilder = + if ps.presentWitness + then + mconcat + [ input $ + pubKey witnessPubKey + . withValue witnessValue + . withOutRef witnessRef + , output $ + pubKey witnessPubKey + . withValue witnessValue + ] + else mempty + + --- + + govBuilder :: BaseBuilder + govBuilder = + let datum = + if ps.withGovernorDatum + then withDatum governorOutputDatum + else id + in output $ + script govValidatorHash + . withValue governorValue + . datum + -- + + builder :: BaseBuilder + builder = + mconcat + [ txId "986b756ffb1c9839fc8d0b22a308ac91d5b5d0ebbfa683a47588c8a5cf70b5af" + , signedWith (pubKeyHashes !! 1) + , mint gst + , govBuilder + , witnessBuilder + ] + +-------------------------------------------------------------------------------- + +totallyValidParameters :: Parameters +totallyValidParameters = + Parameters + { datumThresholdsValid = True + , datumMaxTimeRangeWidthValid = True + , datumTimingConfigValid = True + , withGovernorDatum = True + , presentWitness = True + , mintMoreThanOneStateToken = False + , mintStateTokenWithName = False + } + +invalidDatumThresholdsParameters :: Parameters +invalidDatumThresholdsParameters = + totallyValidParameters + { datumThresholdsValid = False + } + +invalidDatumMaxTimeRangeWidthParameters :: Parameters +invalidDatumMaxTimeRangeWidthParameters = + totallyValidParameters + { datumMaxTimeRangeWidthValid = False + } + +invalidDatumTimingConfigParameters :: Parameters +invalidDatumTimingConfigParameters = + totallyValidParameters + { datumTimingConfigValid = False + } + +withoutGovernorDatumParameters :: Parameters +withoutGovernorDatumParameters = + totallyValidParameters + { withGovernorDatum = False + } + +witnessNotPresentedParameters :: Parameters +witnessNotPresentedParameters = + totallyValidParameters + { presentWitness = False + } + +mintMoreThanOneGSTParameters :: Parameters +mintMoreThanOneGSTParameters = + totallyValidParameters + { mintMoreThanOneStateToken = True + } + +mintGSTWithNoneEmptyNameParameters :: Parameters +mintGSTWithNoneEmptyNameParameters = + totallyValidParameters + { mintStateTokenWithName = True + } + +-------------------------------------------------------------------------------- + +mkTestCase :: String -> Parameters -> Bool -> SpecificationTree +mkTestCase + name + ps + valid = policyTest + where + txInfo = mintGST ps + + policyTest = + testPolicy + valid + name + (governorPolicy governor) + () + (ScriptContext txInfo (Minting govSymbol)) diff --git a/agora-specs/Spec/Governor.hs b/agora-specs/Spec/Governor.hs index cb905de..532dd2e 100644 --- a/agora-specs/Spec/Governor.hs +++ b/agora-specs/Spec/Governor.hs @@ -14,15 +14,15 @@ TODO: Add negative test cases, see [#76](https://github.com/Liqwid-Labs/agora/is module Spec.Governor (specs) where import Agora.Governor (GovernorDatum (..), GovernorRedeemer (..)) -import Agora.Governor.Scripts (governorPolicy, governorValidator) +import Agora.Governor.Scripts (governorValidator) import Agora.Proposal (ProposalId (..)) import Data.Default.Class (Default (def)) -import Sample.Governor (createProposal, mintGATs, mintGST, mutateState) +import Sample.Governor (mintGATs, mutateState) +import Sample.Governor.Initialize qualified as GST import Sample.Shared qualified as Shared import Test.Specification ( SpecificationTree, group, - policySucceedsWith, validatorSucceedsWith, ) @@ -31,27 +31,24 @@ specs :: [SpecificationTree] specs = [ group "policy" - [ policySucceedsWith - "GST minting" - (governorPolicy Shared.governor) - () - mintGST + [ GST.mkTestCase "totally legal" GST.totallyValidParameters True + , group + "illegal" + [ GST.mkTestCase "invalid thresholds" GST.invalidDatumThresholdsParameters False + , GST.mkTestCase + "invalid max time range width for proposal creation" + GST.invalidDatumMaxTimeRangeWidthParameters + False + , GST.mkTestCase "invalid timings" GST.invalidDatumTimingConfigParameters False + , GST.mkTestCase "no governor datum" GST.withoutGovernorDatumParameters False + , GST.mkTestCase "no witness UTXO" GST.witnessNotPresentedParameters False + , GST.mkTestCase "mint more than one GST" GST.mintMoreThanOneGSTParameters False + , GST.mkTestCase "GST has non-empty name" GST.mintGSTWithNoneEmptyNameParameters False + ] ] , group "validator" [ validatorSucceedsWith - "proposal creation" - (governorValidator Shared.governor) - ( GovernorDatum - def - (ProposalId 0) - def - def - 3 - ) - CreateProposal - createProposal - , validatorSucceedsWith "GATs minting" (governorValidator Shared.governor) ( GovernorDatum diff --git a/agora.cabal b/agora.cabal index cb0283c..6485c15 100644 --- a/agora.cabal +++ b/agora.cabal @@ -188,6 +188,7 @@ library agora-specs Sample.Effect.GovernorMutation Sample.Effect.TreasuryWithdrawal Sample.Governor + Sample.Governor.Initialize Sample.Proposal.Advance Sample.Proposal.Cosign Sample.Proposal.Create diff --git a/bench.csv b/bench.csv index 4ea97f2..6579952 100644 --- a/bench.csv +++ b/bench.csv @@ -232,7 +232,6 @@ Agora/AuthorityToken/singleAuthorityTokenBurned/Correct many inputs,32757063,870 Agora/Treasury/Validator/Positive/Allows for effect changes,31277082,80782,1450 Agora/AuthorityToken/singleAuthorityTokenBurned/Correct simple,20570665,54655,725 Agora/AuthorityToken/singleAuthorityTokenBurned/Correct many inputs,32757063,87013,825 -Agora/Governor/policy/GST minting,57584660,163806,2205 -Agora/Governor/validator/proposal creation,302549314,814787,8701 +Agora/Governor/policy/totally legal,60002734,167736,2267 Agora/Governor/validator/GATs minting,255328308,682125,8827 Agora/Governor/validator/mutate governor state,93971983,261537,8301