test that govenor will reject fake stakes

This commit is contained in:
Hongrui Fang 2022-11-02 00:12:56 +08:00
parent 7f80db7af5
commit 996797995a
3 changed files with 80 additions and 16 deletions

View file

@ -17,6 +17,7 @@ module Sample.Proposal.Create (
timeRangeNotTightParameters, timeRangeNotTightParameters,
timeRangeNotClosedParameters, timeRangeNotClosedParameters,
invalidProposalStatusParameters, invalidProposalStatusParameters,
fakeSSTParameters,
) where ) where
import Agora.Governor ( import Agora.Governor (
@ -44,6 +45,7 @@ import Agora.Stake (
StakeDatum (..), StakeDatum (..),
StakeRedeemer (PermitVote), StakeRedeemer (PermitVote),
) )
import Agora.Utils (validatorHashToTokenName)
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
@ -63,6 +65,7 @@ import Plutarch.Context (
withValue, withValue,
) )
import Plutarch.Extra.AssetClass (assetClassValue) import Plutarch.Extra.AssetClass (assetClassValue)
import PlutusLedgerApi.V1.Value qualified as Value
import PlutusLedgerApi.V2 ( import PlutusLedgerApi.V2 (
Credential (PubKeyCredential), Credential (PubKeyCredential),
POSIXTime (POSIXTime), POSIXTime (POSIXTime),
@ -85,6 +88,7 @@ import Sample.Shared (
signer, signer,
signer2, signer2,
stakeAssetClass, stakeAssetClass,
stakeSymbol,
stakeValidator, stakeValidator,
stakeValidatorHash, stakeValidatorHash,
) )
@ -95,6 +99,7 @@ import Test.Util (
mkMinting, mkMinting,
mkSpending, mkSpending,
sortValue, sortValue,
validatorHashes,
) )
-- | Parameters for creating a proposal. -- | Parameters for creating a proposal.
@ -115,6 +120,8 @@ data Parameters = Parameters
-- ^ Is 'TxInfo.validTimeRange' closed? -- ^ Is 'TxInfo.validTimeRange' closed?
, proposalStatus :: ProposalStatus , proposalStatus :: ProposalStatus
-- ^ The status of the newly created proposal. -- ^ The status of the newly created proposal.
, fakeSST :: Bool
-- ^ Whether to use SST that doesn't belong to the stake validator.
} }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -289,6 +296,30 @@ createProposal ps = builder
--- ---
attacker = head validatorHashes
fakeStakeBuilder =
if ps.fakeSST
then
mconcat
[ input @b $
mconcat
[ script attacker
, withValue $
Value.singleton
stakeSymbol
(validatorHashToTokenName attacker)
1
, withDatum $
(mkStakeInputDatum ps)
{ stakedAmount = 10000000000
}
]
]
else mempty
---
governorValue = sortValue $ gst <> minAda governorValue = sortValue $ gst <> minAda
stakeValue = stakeValue =
sortValue $ sortValue $
@ -334,19 +365,39 @@ createProposal ps = builder
, withDatum (mkGovernorOutputDatum ps) , withDatum (mkGovernorOutputDatum ps)
] ]
, --- , ---
input $ if ps.fakeSST
mconcat then
[ script stakeValidatorHash mconcat
, withValue stakeValue [ input @b $
, withDatum (mkStakeInputDatum ps) mconcat
, withRef stakeRef [ script attacker
] , withValue $
, output $ Value.singleton
mconcat stakeSymbol
[ script stakeValidatorHash (validatorHashToTokenName attacker)
, withValue stakeValue 1
, withDatum (mkStakeOutputDatum ps) , withDatum $
] (mkStakeInputDatum ps)
{ stakedAmount = 10000000000
}
]
]
else
mconcat
[ input $
mconcat
[ script stakeValidatorHash
, withValue stakeValue
, withDatum (mkStakeInputDatum ps)
, withRef stakeRef
]
, output $
mconcat
[ script stakeValidatorHash
, withValue stakeValue
, withDatum (mkStakeOutputDatum ps)
]
]
, --- , ---
output $ output $
mconcat mconcat
@ -354,6 +405,8 @@ createProposal ps = builder
, withValue proposalValue , withValue proposalValue
, withDatum (mkProposalOutputDatum ps) , withDatum (mkProposalOutputDatum ps)
] ]
, ---
fakeStakeBuilder
] ]
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -383,6 +436,7 @@ totallyValidParameters =
, timeRangeTightEnough = True , timeRangeTightEnough = True
, timeRangeClosed = True , timeRangeClosed = True
, proposalStatus = Draft , proposalStatus = Draft
, fakeSST = False
} }
invalidOutputGovernorDatumParameters :: Parameters invalidOutputGovernorDatumParameters :: Parameters
@ -435,6 +489,12 @@ invalidProposalStatusParameters =
) )
[VotingReady, Locked, Finished] [VotingReady, Locked, Finished]
fakeSSTParameters :: Parameters
fakeSSTParameters =
totallyValidParameters
{ fakeSST = True
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
{- | Create a test tree that runs the proposal minting policy, the governor {- | Create a test tree that runs the proposal minting policy, the governor

View file

@ -85,6 +85,12 @@ specs =
True True
) )
Create.invalidProposalStatusParameters Create.invalidProposalStatusParameters
, Create.mkTestTree
"fake SST"
Create.fakeSSTParameters
True
False
False
] ]
] ]
, group , group

View file

@ -334,9 +334,7 @@ validateProposalStartingTime ::
validateProposalStartingTime = phoistAcyclic $ validateProposalStartingTime = phoistAcyclic $
plam $ \(pto -> maxDuration) iv (pto -> st) -> plam $ \(pto -> maxDuration) iv (pto -> st) ->
pmaybe pmaybe
# ptrace # pconstant False
"validateProposalStartingTime: unable to get current time"
(pconstant False)
# plam # plam
( \ct -> ( \ct ->
let duration = pcurrentTimeDuration # ct let duration = pcurrentTimeDuration # ct