add tests for minting GST

This commit is contained in:
Hongrui Fang 2022-07-17 21:56:29 +08:00
parent 4317987964
commit 51c4955a37
No known key found for this signature in database
GPG key ID: 1E0454204FC7D755
4 changed files with 282 additions and 22 deletions

View file

@ -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))

View file

@ -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

View file

@ -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

View file

@ -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

1 name cpu mem size
232 Agora/Treasury/Validator/Positive/Allows for effect changes 31277082 80782 1450
233 Agora/AuthorityToken/singleAuthorityTokenBurned/Correct simple 20570665 54655 725
234 Agora/AuthorityToken/singleAuthorityTokenBurned/Correct many inputs 32757063 87013 825
235 Agora/Governor/policy/GST minting Agora/Governor/policy/totally legal 57584660 60002734 163806 167736 2205 2267
Agora/Governor/validator/proposal creation 302549314 814787 8701
236 Agora/Governor/validator/GATs minting 255328308 682125 8827
237 Agora/Governor/validator/mutate governor state 93971983 261537 8301