fix tests

This commit is contained in:
Hongrui Fang 2022-09-06 21:00:12 +08:00 committed by 方泓睿
parent f6b36abfc6
commit 3e7916ba58
12 changed files with 554 additions and 804 deletions

View file

@ -74,11 +74,17 @@ atAssetClass = assetClass authorityTokenSymbol tokenName
-- | The mock reference of the governor state UTXO. -- | The mock reference of the governor state UTXO.
govRef :: TxOutRef govRef :: TxOutRef
govRef = TxOutRef "1475e1ee22330dfc55430980e5a6b100ec9d9249bb4b462256a79559" 1 govRef =
TxOutRef
"d63fe09e6ac6e55dea82291149085d0a9b901df65087b83965188ee92fb25aef"
1
-- | The mock reference of the effect UTXO. -- | The mock reference of the effect UTXO.
effectRef :: TxOutRef effectRef :: TxOutRef
effectRef = TxOutRef "a302d327d8e5553d50b9d017475369753f723d7e999ac1b68da8ad52" 1 effectRef =
TxOutRef
"3ca6864670aae61a9f3e63064284cec00bd983d77cf4e1ab1e26bef34cafb0a9"
1
-- | The input effect datum in 'mkEffectTransaction'. -- | The input effect datum in 'mkEffectTransaction'.
mkEffectDatum :: GovernorDatum -> MutateGovernorDatum mkEffectDatum :: GovernorDatum -> MutateGovernorDatum

View file

@ -29,7 +29,6 @@ module Sample.Proposal.Advance (
mkFromFinishedBundles, mkFromFinishedBundles,
mkInsufficientCosignsBundle, mkInsufficientCosignsBundle,
mkToNextStateTooLateBundles, mkToNextStateTooLateBundles,
mkInvalidOutputStakeBundles,
mkMintGATsForWrongEffectsBundle, mkMintGATsForWrongEffectsBundle,
mkNoGATMintedBundle, mkNoGATMintedBundle,
mkGATsWithWrongDatumBundle, mkGATsWithWrongDatumBundle,
@ -46,6 +45,7 @@ import Agora.Governor (
import Agora.Proposal ( import Agora.Proposal (
ProposalDatum (..), ProposalDatum (..),
ProposalEffectGroup, ProposalEffectGroup,
ProposalEffectMetadata (ProposalEffectMetadata),
ProposalId (ProposalId), ProposalId (ProposalId),
ProposalRedeemer (AdvanceProposal), ProposalRedeemer (AdvanceProposal),
ProposalStatus (..), ProposalStatus (..),
@ -66,7 +66,6 @@ import Agora.Proposal.Time (
import Agora.Scripts (AgoraScripts (..)) import Agora.Scripts (AgoraScripts (..))
import Agora.Stake ( import Agora.Stake (
StakeDatum (..), StakeDatum (..),
StakeRedeemer (WitnessStake),
) )
import Agora.Utils (scriptHashToTokenName) import Agora.Utils (scriptHashToTokenName)
import Control.Applicative (liftA2) import Control.Applicative (liftA2)
@ -75,15 +74,17 @@ import Data.Default (def)
import Data.List (singleton, sort) import Data.List (singleton, sort)
import Data.Map.Strict qualified as StrictMap import Data.Map.Strict qualified as StrictMap
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.Tagged (Tagged (..), untag) import Data.Tagged (untag)
import Plutarch.Context ( import Plutarch.Context (
input, input,
mint, mint,
output, output,
referenceInput,
script, script,
signedWith, signedWith,
timeRange, timeRange,
withDatum, withDatum,
withInlineDatum,
withRef, withRef,
withValue, withValue,
) )
@ -217,7 +218,6 @@ data StakeParameters = StakeParameters
{ numStake :: NumStake { numStake :: NumStake
, perStakeGTs :: Integer , perStakeGTs :: Integer
, transactionSignedByOwners :: Bool , transactionSignedByOwners :: Bool
, invalidStakeOutputDatum :: Bool
} }
-- | Represent the number of stakes or the number of the cosigners. -- | Represent the number of stakes or the number of the cosigners.
@ -355,7 +355,7 @@ mkStakeInputDatums :: StakeParameters -> [StakeDatum]
mkStakeInputDatums ps = mkStakeInputDatums ps =
let template = let template =
StakeDatum StakeDatum
{ stakedAmount = Tagged ps.perStakeGTs { stakedAmount = fromInteger ps.perStakeGTs
, owner = PubKeyCredential "" , owner = PubKeyCredential ""
, delegatedTo = Nothing , delegatedTo = Nothing
, lockedBy = [] , lockedBy = []
@ -363,24 +363,6 @@ mkStakeInputDatums ps =
in (\owner -> template {owner = owner}) in (\owner -> template {owner = owner})
<$> mkStakeOwners ps.numStake <$> mkStakeOwners ps.numStake
-- | Create the output stake datums given the parameters.
mkStakeOutputDatums :: StakeParameters -> [StakeDatum]
mkStakeOutputDatums ps =
let inputDatums = mkStakeInputDatums ps
outputStakedAmount =
Tagged $
if ps.invalidStakeOutputDatum
then ps.perStakeGTs * 10
else ps.perStakeGTs
modify inp = inp {stakedAmount = outputStakedAmount}
in modify <$> inputDatums
{- | Get the input stake datum given the index. The range of the index is
@[0, 'StakeParameters.numStake - 1']@
-}
getStakeInputDatumAt :: StakeParameters -> Index -> StakeDatum
getStakeInputDatumAt ps = (!!) (mkStakeInputDatums ps)
-- | Create the reference to a particular stake UTXO. -- | Create the reference to a particular stake UTXO.
mkStakeRef :: Index -> TxOutRef mkStakeRef :: Index -> TxOutRef
mkStakeRef = TxOutRef stakeTxRef . (+ 3) . fromIntegral mkStakeRef = TxOutRef stakeTxRef . (+ 3) . fromIntegral
@ -397,39 +379,26 @@ mkStakeBuilder ps =
<> Value.assetClassValue <> Value.assetClassValue
(untag governor.gtClassRef) (untag governor.gtClassRef)
ps.perStakeGTs ps.perStakeGTs
perStake idx i o = perStake idx i =
let withSig = let withSig =
case (i.owner, ps.transactionSignedByOwners) of case (i.owner, ps.transactionSignedByOwners) of
(PubKeyCredential owner, True) -> signedWith owner (PubKeyCredential owner, True) -> signedWith owner
_ -> mempty _ -> mempty
in mconcat in mconcat
[ withSig [ withSig
, input $ , referenceInput $
mconcat mconcat
[ script stakeValidatorHash [ script stakeValidatorHash
, withRef (mkStakeRef idx) , withRef (mkStakeRef idx)
, withValue perStakeValue , withValue perStakeValue
, withDatum i , withInlineDatum i
]
, output $
mconcat
[ script stakeValidatorHash
, withValue perStakeValue
, withDatum o
] ]
] ]
in mconcat $ in mconcat $
zipWith3 zipWith
perStake perStake
[0 :: Index ..] [0 :: Index ..]
(mkStakeInputDatums ps) (mkStakeInputDatums ps)
(mkStakeOutputDatums ps)
{- | The proposal redeemer used to spend the stake UTXO, which is always
'WitnessStake' in this case.
-}
stakeRedeemer :: StakeRedeemer
stakeRedeemer = WitnessStake
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -553,7 +522,7 @@ mkTestTree ::
Validity -> Validity ->
SpecificationTree SpecificationTree
mkTestTree name pb val = mkTestTree name pb val =
group name $ mconcat [proposal, stake, governor, authority] group name $ mconcat [proposal, governor, authority]
where where
spend = mkSpending advance pb spend = mkSpending advance pb
@ -567,22 +536,6 @@ mkTestTree name pb val =
proposalInputDatum proposalInputDatum
proposalRedeemer proposalRedeemer
(spend proposalRef) (spend proposalRef)
stake =
if pb.stakeParameters.numStake == 0
then mempty
else
let idx = 0
in singleton $
testValidator
val.forStakeValidator
"stake"
agoraScripts.compiledStakeValidator
(getStakeInputDatumAt pb.stakeParameters idx)
stakeRedeemer
( spend (mkStakeRef idx)
)
governor = governor =
maybe [] singleton $ maybe [] singleton $
testValidator testValidator
@ -747,7 +700,7 @@ mkMockEffects useAuthScript n = effects
datums = repeat dummyDatumHash datums = repeat dummyDatumHash
effectMetadata = zip datums authScripts effectMetadata = zipWith ProposalEffectMetadata datums authScripts
effectScripts = validatorHashes effectScripts = validatorHashes
effects = effects =
@ -822,7 +775,6 @@ mkValidToNextStateBundle nCosigners nEffects authScript from =
compPerStakeGTsForDraft $ compPerStakeGTsForDraft $
fromIntegral nCosigners fromIntegral nCosigners
, transactionSignedByOwners = False , transactionSignedByOwners = False
, invalidStakeOutputDatum = False
} }
, governorParameters = Nothing , governorParameters = Nothing
, authorityTokenParameters = [] , authorityTokenParameters = []
@ -857,7 +809,7 @@ mkValidToNextStateBundle nCosigners nEffects authScript from =
let aut = let aut =
StrictMap.elems $ StrictMap.elems $
StrictMap.mapWithKey StrictMap.mapWithKey
( \vh (_, authScript) -> ( \vh (ProposalEffectMetadata _ authScript) ->
AuthorityTokenParameters AuthorityTokenParameters
{ mintGATsFor = vh { mintGATsFor = vh
, carryDatum = Just dummyDatum , carryDatum = Just dummyDatum
@ -920,7 +872,6 @@ mkValidToFailedStateBundles nCosigners nEffects =
compPerStakeGTsForDraft $ compPerStakeGTsForDraft $
fromIntegral nCosigners fromIntegral nCosigners
, transactionSignedByOwners = False , transactionSignedByOwners = False
, invalidStakeOutputDatum = False
} }
, governorParameters = Nothing , governorParameters = Nothing
, authorityTokenParameters = [] , authorityTokenParameters = []
@ -965,22 +916,6 @@ mkToNextStateTooLateBundles nCosigners nEffects =
{ transactionTimeRange = mkTooLateTimeRange from { transactionTimeRange = mkTooLateTimeRange from
} }
mkInvalidOutputStakeBundles :: Word -> Word -> [ParameterBundle]
mkInvalidOutputStakeBundles nCosigners nEffects =
liftA2
mkBundle
[True, False]
[Draft]
where
mkBundle authScript from =
let template = mkValidToNextStateBundle nCosigners nEffects authScript from
in template
{ stakeParameters =
template.stakeParameters
{ invalidStakeOutputDatum = True
}
}
mkUnexpectedOutputStakeBundles :: Word -> Word -> [ParameterBundle] mkUnexpectedOutputStakeBundles :: Word -> Word -> [ParameterBundle]
mkUnexpectedOutputStakeBundles nCosigners nEffects = mkUnexpectedOutputStakeBundles nCosigners nEffects =
liftA2 liftA2

View file

@ -10,7 +10,6 @@ module Sample.Proposal.Cosign (
validCosignNParameters, validCosignNParameters,
duplicateCosignersParameters, duplicateCosignersParameters,
statusNotDraftCosignNParameters, statusNotDraftCosignNParameters,
invalidStakeOutputParameters,
mkTestTree, mkTestTree,
) where ) where
@ -31,25 +30,26 @@ import Agora.SafeMoney (GTTag)
import Agora.Scripts (AgoraScripts (..)) import Agora.Scripts (AgoraScripts (..))
import Agora.Stake ( import Agora.Stake (
StakeDatum (StakeDatum, owner), StakeDatum (StakeDatum, owner),
StakeRedeemer (WitnessStake),
stakedAmount,
) )
import Data.Coerce (coerce) import Data.Coerce (coerce)
import Data.Default (def) import Data.Default (def)
import Data.List (sort) import Data.List (sort)
import Data.Map.Strict qualified as StrictMap import Data.Map.Strict qualified as StrictMap
import Data.Tagged (Tagged, untag) import Data.Tagged (untag)
import Plutarch.Context ( import Plutarch.Context (
input, input,
output, output,
referenceInput,
script, script,
signedWith, signedWith,
timeRange, timeRange,
txId, txId,
withDatum, withDatum,
withInlineDatum,
withRef, withRef,
withValue, withValue,
) )
import Plutarch.SafeMoney (Discrete)
import PlutusLedgerApi.V1.Value qualified as Value import PlutusLedgerApi.V1.Value qualified as Value
import PlutusLedgerApi.V2 ( import PlutusLedgerApi.V2 (
Credential (PubKeyCredential), Credential (PubKeyCredential),
@ -61,6 +61,7 @@ import PlutusLedgerApi.V2 (
import Sample.Proposal.Shared (proposalTxRef, stakeTxRef) import Sample.Proposal.Shared (proposalTxRef, stakeTxRef)
import Sample.Shared ( import Sample.Shared (
agoraScripts, agoraScripts,
fromDiscrete,
governor, governor,
minAda, minAda,
proposalPolicySymbol, proposalPolicySymbol,
@ -71,7 +72,6 @@ import Sample.Shared (
) )
import Test.Specification ( import Test.Specification (
SpecificationTree, SpecificationTree,
group,
testValidator, testValidator,
) )
import Test.Util (CombinableBuilder, closedBoundedInterval, mkSpending, pubKeyHashes, sortValue) import Test.Util (CombinableBuilder, closedBoundedInterval, mkSpending, pubKeyHashes, sortValue)
@ -82,9 +82,6 @@ data Parameters = Parameters
-- ^ New cosigners to be added, and the owners of the generated stakes. -- ^ New cosigners to be added, and the owners of the generated stakes.
, proposalStatus :: ProposalStatus , proposalStatus :: ProposalStatus
-- ^ Current state of the proposal. -- ^ Current state of the proposal.
, alterOutputStakes :: Bool
-- ^ Whether to generate invalid stake outputs.
-- In particular, the 'stakedAmount' of all the stake datums will be set to zero.
} }
-- | Owner of the creator stake, doesn't really matter in this case. -- | Owner of the creator stake, doesn't really matter in this case.
@ -92,7 +89,7 @@ proposalCreator :: PubKeyHash
proposalCreator = signer proposalCreator = signer
-- | The amount of GTs every generated stake has, doesn't really matter in this case. -- | The amount of GTs every generated stake has, doesn't really matter in this case.
perStakedGTs :: Tagged GTTag Integer perStakedGTs :: Discrete GTTag
perStakedGTs = 5 perStakedGTs = 5
{- | Create input proposal datum given the parameters. {- | Create input proposal datum given the parameters.
@ -151,34 +148,24 @@ cosign ps = builder
minAda minAda
<> Value.assetClassValue <> Value.assetClassValue
(untag governor.gtClassRef) (untag governor.gtClassRef)
(untag perStakedGTs) (fromDiscrete perStakedGTs)
<> sst <> sst
stakeBuilder = stakeBuilder =
foldMap foldMap
( \(stakeDatum, refIdx) -> ( \(stakeDatum, refIdx) ->
let stakeOutputDatum = mconcat
if ps.alterOutputStakes [ referenceInput $
then stakeDatum {stakedAmount = 0} mconcat
else stakeDatum [ script stakeValidatorHash
in mconcat , withValue stakeValue
[ input $ , withInlineDatum stakeDatum
mconcat , withRef (mkStakeRef refIdx)
[ script stakeValidatorHash ]
, withValue stakeValue , case stakeDatum.owner of
, withDatum stakeDatum PubKeyCredential k -> signedWith k
, withRef (mkStakeRef refIdx) _ -> mempty
] ]
, output $
mconcat
[ script stakeValidatorHash
, withValue stakeValue
, withDatum stakeOutputDatum
]
, case stakeDatum.owner of
PubKeyCredential k -> signedWith k
_ -> mempty
]
) )
$ zip $ zip
stakeInputDatums stakeInputDatums
@ -246,10 +233,6 @@ mkStakeRef idx =
mkProposalRedeemer :: Parameters -> ProposalRedeemer mkProposalRedeemer :: Parameters -> ProposalRedeemer
mkProposalRedeemer = Cosign . sort . newCosigners mkProposalRedeemer = Cosign . sort . newCosigners
-- | Stake redeemer for cosuming all the stakes generated in the module.
stakeRedeemer :: StakeRedeemer
stakeRedeemer = WitnessStake
--- ---
-- | Create a valid parameters that cosign the proposal with a given number of cosigners. -- | Create a valid parameters that cosign the proposal with a given number of cosigners.
@ -259,7 +242,6 @@ validCosignNParameters n
Parameters Parameters
{ newCosigners = take n (fmap PubKeyCredential pubKeyHashes) { newCosigners = take n (fmap PubKeyCredential pubKeyHashes)
, proposalStatus = Draft , proposalStatus = Draft
, alterOutputStakes = False
} }
| otherwise = error "Number of cosigners should be positive" | otherwise = error "Number of cosigners should be positive"
@ -273,7 +255,6 @@ duplicateCosignersParameters =
Parameters Parameters
{ newCosigners = [PubKeyCredential proposalCreator] { newCosigners = [PubKeyCredential proposalCreator]
, proposalStatus = Draft , proposalStatus = Draft
, alterOutputStakes = False
} }
--- ---
@ -288,24 +269,12 @@ statusNotDraftCosignNParameters n =
Parameters Parameters
{ newCosigners = take n (fmap PubKeyCredential pubKeyHashes) { newCosigners = take n (fmap PubKeyCredential pubKeyHashes)
, proposalStatus = st , proposalStatus = st
, alterOutputStakes = False
} }
) )
[VotingReady, Locked, Finished] [VotingReady, Locked, Finished]
--- ---
{- | Parameters thet change the output stake datums.
Invalid for both proposal validator and stake validator.
-}
invalidStakeOutputParameters :: Parameters
invalidStakeOutputParameters =
(validCosignNParameters 2)
{ alterOutputStakes = True
}
---
-- | Create a test tree given the parameters. Both the proposal validator and stake validator will be run. -- | Create a test tree given the parameters. Both the proposal validator and stake validator will be run.
mkTestTree :: mkTestTree ::
-- | The name of the test group. -- | The name of the test group.
@ -314,7 +283,7 @@ mkTestTree ::
-- | Are the parameters valid for the proposal validator? -- | Are the parameters valid for the proposal validator?
Bool -> Bool ->
SpecificationTree SpecificationTree
mkTestTree name ps isValid = group name [proposal, stake] mkTestTree name ps isValid = proposal
where where
spend = mkSpending cosign ps spend = mkSpending cosign ps
@ -322,20 +291,8 @@ mkTestTree name ps isValid = group name [proposal, stake]
let proposalInputDatum = mkProposalInputDatum ps let proposalInputDatum = mkProposalInputDatum ps
in testValidator in testValidator
isValid isValid
"proposal" (name <> ": proposal")
agoraScripts.compiledProposalValidator agoraScripts.compiledProposalValidator
proposalInputDatum proposalInputDatum
(mkProposalRedeemer ps) (mkProposalRedeemer ps)
(spend proposalRef) (spend proposalRef)
stake =
let idx = 0
stakeInputDatum = mkStakeInputDatums ps !! idx
isValid = not ps.alterOutputStakes
in testValidator
isValid
"stake"
agoraScripts.compiledStakeValidator
stakeInputDatum
stakeRedeemer
(spend $ mkStakeRef idx)

View file

@ -38,6 +38,7 @@ import Agora.Proposal.Time (
), ),
ProposalStartingTime (..), ProposalStartingTime (..),
) )
import Agora.SafeMoney (GTTag)
import Agora.Scripts (AgoraScripts (..)) import Agora.Scripts (AgoraScripts (..))
import Agora.Stake ( import Agora.Stake (
ProposalLock (..), ProposalLock (..),
@ -47,7 +48,7 @@ import Agora.Stake (
import Data.Coerce (coerce) import Data.Coerce (coerce)
import Data.Default (Default (def)) import Data.Default (Default (def))
import Data.Map.Strict qualified as StrictMap import Data.Map.Strict qualified as StrictMap
import Data.Tagged (Tagged, untag) import Data.Tagged (untag)
import Plutarch.Context ( import Plutarch.Context (
input, input,
mint, mint,
@ -60,6 +61,7 @@ import Plutarch.Context (
withRef, withRef,
withValue, withValue,
) )
import Plutarch.SafeMoney (Discrete)
import PlutusLedgerApi.V1.Value qualified as Value import PlutusLedgerApi.V1.Value qualified as Value
import PlutusLedgerApi.V2 ( import PlutusLedgerApi.V2 (
Credential (PubKeyCredential), Credential (PubKeyCredential),
@ -71,6 +73,7 @@ import PlutusLedgerApi.V2 (
import Sample.Proposal.Shared (stakeTxRef) import Sample.Proposal.Shared (stakeTxRef)
import Sample.Shared ( import Sample.Shared (
agoraScripts, agoraScripts,
fromDiscrete,
govAssetClass, govAssetClass,
govValidatorHash, govValidatorHash,
governor, governor,
@ -123,7 +126,7 @@ thisProposalId :: ProposalId
thisProposalId = ProposalId 25 thisProposalId = ProposalId 25
-- | The arbitrary staked amount. Doesn;t really matter in this case. -- | The arbitrary staked amount. Doesn;t really matter in this case.
stakedGTs :: Tagged _ Integer stakedGTs :: Discrete GTTag
stakedGTs = 5 stakedGTs = 5
-- | The owner of the stake. -- | The owner of the stake.
@ -289,7 +292,7 @@ createProposal ps = builder
sortValue $ sortValue $
sortValue $ sortValue $
sst sst
<> Value.assetClassValue (untag governor.gtClassRef) (untag stakedGTs) <> Value.assetClassValue (untag governor.gtClassRef) (fromDiscrete stakedGTs)
<> minAda <> minAda
proposalValue = sortValue $ pst <> minAda proposalValue = sortValue $ pst <> minAda

View file

@ -36,6 +36,7 @@ import Agora.Proposal (
ResultTag (..), ResultTag (..),
) )
import Agora.Proposal.Time (ProposalStartingTime (ProposalStartingTime)) import Agora.Proposal.Time (ProposalStartingTime (ProposalStartingTime))
import Agora.SafeMoney (GTTag)
import Agora.Scripts (AgoraScripts (..)) import Agora.Scripts (AgoraScripts (..))
import Agora.Stake ( import Agora.Stake (
ProposalLock (..), ProposalLock (..),
@ -44,7 +45,7 @@ import Agora.Stake (
) )
import Data.Default.Class (Default (def)) import Data.Default.Class (Default (def))
import Data.Map.Strict qualified as StrictMap import Data.Map.Strict qualified as StrictMap
import Data.Tagged (Tagged (..), untag) import Data.Tagged (untag)
import Plutarch.Context ( import Plutarch.Context (
input, input,
output, output,
@ -52,9 +53,11 @@ import Plutarch.Context (
signedWith, signedWith,
txId, txId,
withDatum, withDatum,
withRedeemer,
withRef, withRef,
withValue, withValue,
) )
import Plutarch.SafeMoney (Discrete)
import PlutusLedgerApi.V1.Value qualified as Value import PlutusLedgerApi.V1.Value qualified as Value
import PlutusLedgerApi.V2 ( import PlutusLedgerApi.V2 (
Credential (PubKeyCredential), Credential (PubKeyCredential),
@ -64,6 +67,7 @@ import PlutusLedgerApi.V2 (
import Sample.Proposal.Shared (stakeTxRef) import Sample.Proposal.Shared (stakeTxRef)
import Sample.Shared ( import Sample.Shared (
agoraScripts, agoraScripts,
fromDiscrete,
governor, governor,
minAda, minAda,
proposalPolicySymbol, proposalPolicySymbol,
@ -99,13 +103,13 @@ defVoteFor :: ResultTag
defVoteFor = ResultTag 0 defVoteFor = ResultTag 0
-- | The default number of GTs the stake will have. -- | The default number of GTs the stake will have.
defStakedGTs :: Tagged _ Integer defStakedGTs :: Discrete GTTag
defStakedGTs = 100000 defStakedGTs = 100000
{- | If 'Parameters.alterOutputStake' is set to true, the {- | If 'Parameters.alterOutputStake' is set to true, the
'StakeDatum.stakedAmount' will be set to this. 'StakeDatum.stakedAmount' will be set to this.
-} -}
alteredStakedGTs :: Tagged _ Integer alteredStakedGTs :: Discrete GTTag
alteredStakedGTs = 100 alteredStakedGTs = 100
-- | Default owner of the stakes. -- | Default owner of the stakes.
@ -212,7 +216,7 @@ mkProposalDatumPair ::
ProposalId -> ProposalId ->
(ProposalDatum, ProposalDatum) (ProposalDatum, ProposalDatum)
mkProposalDatumPair params pid = mkProposalDatumPair params pid =
let inputVotes = mkInputVotes params.stakeRole $ untag defStakedGTs let inputVotes = mkInputVotes params.stakeRole $ fromDiscrete defStakedGTs
input = input =
ProposalDatum ProposalDatum
@ -266,6 +270,7 @@ unlockStake ps =
, withValue pst , withValue pst
, withDatum i , withDatum i
, withRef (mkProposalRef idx) , withRef (mkProposalRef idx)
, withRedeemer proposalRedeemer
] ]
, output $ , output $
mconcat mconcat
@ -282,7 +287,7 @@ unlockStake ps =
mconcat mconcat
[ Value.assetClassValue [ Value.assetClassValue
(untag governor.gtClassRef) (untag governor.gtClassRef)
(untag defStakedGTs) (fromDiscrete defStakedGTs)
, sst , sst
, minAda , minAda
] ]

View file

@ -32,7 +32,7 @@ import Agora.Stake (
) )
import Data.Default (Default (def)) import Data.Default (Default (def))
import Data.Map.Strict qualified as StrictMap import Data.Map.Strict qualified as StrictMap
import Data.Tagged (Tagged (Tagged), untag) import Data.Tagged (untag)
import Plutarch.Context ( import Plutarch.Context (
input, input,
output, output,
@ -41,6 +41,7 @@ import Plutarch.Context (
timeRange, timeRange,
txId, txId,
withDatum, withDatum,
withRedeemer,
withRef, withRef,
withValue, withValue,
) )
@ -139,7 +140,7 @@ delegate = head pubKeyHashes
mkStakeInputDatum :: Parameters -> StakeDatum mkStakeInputDatum :: Parameters -> StakeDatum
mkStakeInputDatum params = mkStakeInputDatum params =
StakeDatum StakeDatum
{ stakedAmount = Tagged params.voteCount { stakedAmount = fromInteger params.voteCount
, owner = PubKeyCredential stakeOwner , owner = PubKeyCredential stakeOwner
, delegatedTo = , delegatedTo =
if params.voteAsDelegate if params.voteAsDelegate
@ -230,6 +231,7 @@ vote params =
, withValue pst , withValue pst
, withDatum proposalInputDatum , withDatum proposalInputDatum
, withRef proposalRef , withRef proposalRef
, withRedeemer $ mkProposalRedeemer params
] ]
, input $ , input $
mconcat mconcat

View file

@ -15,6 +15,7 @@ module Sample.Shared (
deterministicTracingConfing, deterministicTracingConfing,
mkEffect, mkEffect,
mkRedeemer, mkRedeemer,
fromDiscrete,
-- * Agora Scripts -- * Agora Scripts
agoraScripts, agoraScripts,
@ -73,6 +74,7 @@ import Agora.Utils (
CompiledValidator (getCompiledValidator), CompiledValidator (getCompiledValidator),
validatorHashToTokenName, validatorHashToTokenName,
) )
import Data.Coerce (coerce)
import Data.Default.Class (Default (..)) import Data.Default.Class (Default (..))
import Data.Tagged (Tagged (..)) import Data.Tagged (Tagged (..))
import Plutarch (Config (..), TracingMode (DetTracing)) import Plutarch (Config (..), TracingMode (DetTracing))
@ -82,6 +84,7 @@ import Plutarch.Api.V2 (
mkValidator, mkValidator,
validatorHash, validatorHash,
) )
import Plutarch.SafeMoney (Discrete (Discrete))
import PlutusLedgerApi.V1.Address (scriptHashAddress) import PlutusLedgerApi.V1.Address (scriptHashAddress)
import PlutusLedgerApi.V1.Contexts (TxOut (..)) import PlutusLedgerApi.V1.Contexts (TxOut (..))
import PlutusLedgerApi.V1.Scripts (Validator, ValidatorHash (..)) import PlutusLedgerApi.V1.Scripts (Validator, ValidatorHash (..))
@ -225,6 +228,9 @@ mkEffect v = CompiledEffect $ mkValidator deterministicTracingConfing v
mkRedeemer :: forall redeemer. PlutusTx.ToData redeemer => redeemer -> Redeemer mkRedeemer :: forall redeemer. PlutusTx.ToData redeemer => redeemer -> Redeemer
mkRedeemer = Redeemer . toBuiltinData mkRedeemer = Redeemer . toBuiltinData
fromDiscrete :: forall tag. Discrete tag -> Integer
fromDiscrete = coerce
------------------------------------------------------------------ ------------------------------------------------------------------
treasuryOut :: TxOut treasuryOut :: TxOut

View file

@ -23,7 +23,7 @@ import Agora.SafeMoney (GTTag)
import Agora.Stake ( import Agora.Stake (
StakeDatum (StakeDatum, stakedAmount), StakeDatum (StakeDatum, stakedAmount),
) )
import Data.Tagged (Tagged, untag) import Data.Tagged (untag)
import Plutarch.Context ( import Plutarch.Context (
MintingBuilder, MintingBuilder,
SpendingBuilder, SpendingBuilder,
@ -41,6 +41,7 @@ import Plutarch.Context (
withSpendingOutRef, withSpendingOutRef,
withValue, withValue,
) )
import Plutarch.SafeMoney (Discrete)
import PlutusLedgerApi.V1.Contexts (TxOutRef (..)) import PlutusLedgerApi.V1.Contexts (TxOutRef (..))
import PlutusLedgerApi.V1.Value qualified as Value ( import PlutusLedgerApi.V1.Value qualified as Value (
assetClassValue, assetClassValue,
@ -56,6 +57,7 @@ import PlutusLedgerApi.V2 (
) )
import PlutusTx.AssocMap qualified as AssocMap import PlutusTx.AssocMap qualified as AssocMap
import Sample.Shared ( import Sample.Shared (
fromDiscrete,
governor, governor,
signer, signer,
stakeAssetClass, stakeAssetClass,
@ -112,9 +114,9 @@ stakeCreationUnsigned =
-- | Config for creating a ScriptContext that deposits or withdraws. -- | Config for creating a ScriptContext that deposits or withdraws.
data DepositWithdrawExample = DepositWithdrawExample data DepositWithdrawExample = DepositWithdrawExample
{ startAmount :: Tagged GTTag Integer { startAmount :: Discrete GTTag
-- ^ The amount of GT stored before the transaction. -- ^ The amount of GT stored before the transaction.
, delta :: Tagged GTTag Integer , delta :: Discrete GTTag
-- ^ The amount of GT deposited or withdrawn from the Stake. -- ^ The amount of GT deposited or withdrawn from the Stake.
} }
@ -143,7 +145,7 @@ stakeDepositWithdraw config =
, withValue , withValue
( sortValue $ ( sortValue $
st st
<> Value.assetClassValue (untag governor.gtClassRef) (untag stakeBefore.stakedAmount) <> Value.assetClassValue (untag governor.gtClassRef) (fromDiscrete stakeBefore.stakedAmount)
) )
, withDatum stakeAfter , withDatum stakeAfter
, withRef stakeRef , withRef stakeRef
@ -154,7 +156,7 @@ stakeDepositWithdraw config =
, withValue , withValue
( sortValue $ ( sortValue $
st st
<> Value.assetClassValue (untag governor.gtClassRef) (untag stakeAfter.stakedAmount) <> Value.assetClassValue (untag governor.gtClassRef) (fromDiscrete stakeAfter.stakedAmount)
) )
, withDatum stakeAfter , withDatum stakeAfter
] ]

View file

@ -48,6 +48,7 @@ import PlutusLedgerApi.V2 (
) )
import Sample.Shared ( import Sample.Shared (
agoraScripts, agoraScripts,
fromDiscrete,
governor, governor,
minAda, minAda,
signer, signer,
@ -123,7 +124,7 @@ setDelegate ps = buildSpending' builder
[ st [ st
, Value.assetClassValue , Value.assetClassValue
(untag governor.gtClassRef) (untag governor.gtClassRef)
(untag stakeInput.stakedAmount) (fromDiscrete stakeInput.stakedAmount)
, minAda , minAda
] ]

View file

@ -121,10 +121,6 @@ specs =
"duplicate cosigners" "duplicate cosigners"
Cosign.duplicateCosignersParameters Cosign.duplicateCosignersParameters
False False
, Cosign.mkTestTree
"altered output stake"
Cosign.invalidStakeOutputParameters
False
, illegalStatusNotDraftGroup , illegalStatusNotDraftGroup
] ]
in [legalGroup, illegalGroup] in [legalGroup, illegalGroup]
@ -234,26 +230,6 @@ specs =
, forGovernorValidator = Just True , forGovernorValidator = Just True
, forAuthorityTokenPolicy = Just True , forAuthorityTokenPolicy = Just True
} }
, Advance.mkTestTree'
"altered output stake datum"
(\b -> unwords ["from", show b.proposalParameters.fromStatus])
(Advance.mkInvalidOutputStakeBundles cs es)
Advance.Validity
{ forProposalValidator = False
, forStakeValidator = False
, forGovernorValidator = Just True
, forAuthorityTokenPolicy = Just True
}
, Advance.mkTestTree'
"unexpected stake datum"
(\b -> unwords ["from", show b.proposalParameters.fromStatus])
(Advance.mkUnexpectedOutputStakeBundles cs es)
Advance.Validity
{ forProposalValidator = False
, forStakeValidator = True
, forGovernorValidator = Just True
, forAuthorityTokenPolicy = Just True
}
, Advance.mkTestTree , Advance.mkTestTree
"forget to mint GATs" "forget to mint GATs"
(Advance.mkNoGATMintedBundle cs es) (Advance.mkNoGATMintedBundle cs es)

View file

@ -56,6 +56,7 @@ import Agora.Utils (
) )
import Control.Composition ((.**), (.***)) import Control.Composition ((.**), (.***))
import Data.Coerce (coerce) import Data.Coerce (coerce)
import Data.Text qualified as Text
import Plutarch.Evaluate (evalScript) import Plutarch.Evaluate (evalScript)
import PlutusLedgerApi.V1.Scripts ( import PlutusLedgerApi.V1.Scripts (
Context (..), Context (..),
@ -137,8 +138,12 @@ toTestTree (Terminal (Specification name expectation script)) =
Failure -> onFailure Failure -> onFailure
FailureWith s -> onFailureWith s FailureWith s -> onFailureWith s
where where
beautifyTraces =
Text.unpack
. Text.intercalate "\n"
. map (" " <>)
(res, _budget, traces) = evalScript script (res, _budget, traces) = evalScript script
ts = " Traces: " <> show traces ts = " Traces:\n" <> beautifyTraces traces
onSuccess = case res of onSuccess = case res of
Left e -> Left e ->
assertFailure $ assertFailure $

1092
bench.csv

File diff suppressed because it is too large Load diff