diff --git a/agora-specs/Sample/Proposal/UnlockStake.hs b/agora-specs/Sample/Proposal/UnlockStake.hs index e961009..d8ac396 100644 --- a/agora-specs/Sample/Proposal/UnlockStake.hs +++ b/agora-specs/Sample/Proposal/UnlockStake.hs @@ -12,14 +12,10 @@ module Sample.Proposal.UnlockStake ( -------------------------------------------------------------------------------- import PlutusLedgerApi.V1 ( - Datum (Datum), DatumHash, ScriptContext (..), ScriptPurpose (Spending), - ToData (toBuiltinData), - TxInInfo (TxInInfo), TxInfo (..), - TxOut (TxOut, txOutAddress, txOutDatumHash, txOutValue), TxOutRef (..), ValidatorHash, ) @@ -41,19 +37,22 @@ import Agora.Stake (ProposalLock (ProposalLock), Stake (..), StakeDatum (..)) import Sample.Shared ( minAda, proposalPolicySymbol, - proposalValidatorAddress, + proposalValidatorHash, signer, stake, stakeAssetClass, + stakeValidatorHash, ) -import Test.Util (closedBoundedInterval, datumPair, sortValue, toDatumHash, updateMap) +import Test.Util (sortValue, updateMap) -------------------------------------------------------------------------------- import Agora.Proposal.Scripts (proposalValidator) import Control.Monad (join) +import Data.Coerce (coerce) import Data.Default.Class (Default (def)) import Data.Tagged (Tagged (..), untag) +import Plutarch.Context (BaseBuilder, buildTxInfoUnsafe, input, output, script, txId, withDatum, withRefIndex, withTxId, withValue) import Sample.Proposal.Shared (proposalRef, stakeRef) import Sample.Shared qualified as Shared import Test.Specification (SpecificationTree, validatorFailsWith, validatorSucceedsWith) @@ -124,25 +123,14 @@ instance Show UnlockStakeParameters where in mconcat [proposalInfo, ", ", role, ", ", action, ", ", while] -- | Generate some input proposals and their corresponding output proposals. -mkProposals :: UnlockStakeParameters -> ([ProposalDatum], [ProposalDatum]) -mkProposals p = unzip $ forEachProposalId p.proposalCount $ mkProposalDatumPair p +mkProposals :: UnlockStakeParameters -> [(ProposalDatum, ProposalDatum)] +mkProposals p = forEachProposalId p.proposalCount $ mkProposalDatumPair p -- | Iterate over the proposal id of every proposal, given the number of proposals. forEachProposalId :: Integer -> (ProposalId -> a) -> [a] forEachProposalId 0 _ = error "zero proposal" forEachProposalId n f = f . ProposalId <$> [0 .. n - 1] --- | Create a valid stake 'TxOut' given the stake datum. -mkStakeTxOut :: StakeDatum -> TxOut -mkStakeTxOut sd = - let sst = Value.assetClassValue stakeAssetClass 1 - gts = Value.assetClassValue (untag stake.gtClassRef) (untag sd.stakedAmount) - in TxOut - { txOutAddress = proposalValidatorAddress - , txOutValue = sortValue $ sst <> minAda <> gts - , txOutDatumHash = Just $ toDatumHash sd - } - -- | Create the input stake and its corresponding output stake. mkStakeDatumPair :: UnlockStakeParameters -> (StakeDatum, StakeDatum) mkStakeDatumPair c = @@ -165,15 +153,13 @@ mkStakeDatumPair c = AssocMap.keys $ getProposalVotes votesTemplate mkStakeLocks _ _ = [] --- | Create a valid proposal 'TxOut' given the proposal datum. -mkProposalTxOut :: ProposalDatum -> TxOut -mkProposalTxOut pd = - let pst = Value.singleton proposalPolicySymbol "" 1 - in TxOut - { txOutAddress = proposalValidatorAddress - , txOutValue = sortValue $ pst <> minAda - , txOutDatumHash = Just $ toDatumHash pd - } +-- | Create the input proposal datum. +mkProposalInputDatum :: UnlockStakeParameters -> ProposalId -> ProposalDatum +mkProposalInputDatum p pid = fst $ mkProposalDatumPair p pid + +-- | Create the input stake datum. +mkStakeInputDatum :: UnlockStakeParameters -> StakeDatum +mkStakeInputDatum = fst . mkStakeDatumPair -- | Create a input proposal and its corresponding output proposal. mkProposalDatumPair :: @@ -222,47 +208,62 @@ mkProposalDatumPair params pid = -- | Create a 'TxInfo' that tries to unlock a stake. unlockStake :: UnlockStakeParameters -> TxInfo unlockStake p = - let (pInDatums, pOutDatums) = mkProposals p + let pst = Value.singleton proposalPolicySymbol "" 1 + sst = Value.assetClassValue stakeAssetClass 1 + + pIODatums = mkProposals p (sInDatum, sOutDatum) = mkStakeDatumPair p - pIns = - zipWith - ( \i d -> - ( let txOut = mkProposalTxOut d - ref = proposalRef {txOutRefIdx = i} - in TxInInfo ref txOut - ) + proposals = + foldMap + ( \(i, o) -> + mconcat + @BaseBuilder + [ input $ + script proposalValidatorHash + . withValue pst + . withDatum i + . withTxId (txOutRefId proposalRef) + . withRefIndex (txOutRefIdx proposalRef + coerce i.proposalId) + , output $ + script proposalValidatorHash + . withValue (sortValue $ pst <> minAda) + . withDatum o + ] ) - [1 ..] - pInDatums - pOuts = map mkProposalTxOut pOutDatums + pIODatums - sIn = TxInInfo stakeRef $ mkStakeTxOut sInDatum - sOut = mkStakeTxOut sOutDatum + stakeValue = + sortValue $ + mconcat + [ Value.assetClassValue + (untag stake.gtClassRef) + (untag defaultStakedGTs) + , sst + , minAda + ] - mkDatum :: forall d. (ToData d) => d -> Datum - mkDatum = Datum . toBuiltinData - in TxInfo - { txInfoInputs = sIn : pIns - , txInfoOutputs = sOut : pOuts - , txInfoFee = Value.singleton "" "" 2 - , txInfoMint = mempty - , txInfoDCert = [] - , txInfoWdrl = [] - , -- Time doesn't matter int this case. - txInfoValidRange = closedBoundedInterval 0 100 - , txInfoSignatories = [signer] - , txInfoData = datumPair <$> (mkDatum <$> [sInDatum, sOutDatum]) <> (mkDatum <$> pInDatums <> pOutDatums) - , txInfoId = "95ba4015e30aef16a3461ea97a779f814aeea6b8009d99a94add4b8293be737a" - } + stakes = + mconcat @BaseBuilder + [ input $ + script stakeValidatorHash + . withValue stakeValue + . withDatum sInDatum + . withTxId (txOutRefId stakeRef) + . withRefIndex (txOutRefIdx stakeRef) + , output $ + script stakeValidatorHash + . withValue stakeValue + . withDatum sOutDatum + ] --- | Create the input proposal datum. -mkProposalInputDatum :: UnlockStakeParameters -> ProposalId -> ProposalDatum -mkProposalInputDatum p pid = fst $ mkProposalDatumPair p pid - --- | Create the input stake datum. -mkStakeInputDatum :: UnlockStakeParameters -> StakeDatum -mkStakeInputDatum = fst . mkStakeDatumPair + builder = + mconcat @BaseBuilder + [ txId "388bc0b897b3dadcd479da4c88291de4113a50b72ddbed001faf7fc03f11bc52" + , proposals + , stakes + ] + in buildTxInfoUnsafe builder -- | Create a test case that tests the proposal validator's @'Unlock' _@ redeemer. mkProposalValidatorTestCase :: UnlockStakeParameters -> Bool -> SpecificationTree diff --git a/bench.csv b/bench.csv index 3f4d4b5..e69de29 100644 --- a/bench.csv +++ b/bench.csv @@ -1,37 +0,0 @@ -name,cpu,mem,size -Agora/Effects/Treasury Withdrawal Effect/effect/Simple,289254528,702155,3182 -Agora/Effects/Treasury Withdrawal Effect/effect/Simple with multiple treasuries ,448314458,1069267,3509 -Agora/Effects/Treasury Withdrawal Effect/effect/Mixed Assets,407878321,965148,3374 -Agora/Effects/Governor Mutation Effect/validator/valid new governor datum/governor validator should pass,83758582,229228,7665 -Agora/Effects/Governor Mutation Effect/validator/valid new governor datum/effect validator should pass,97345575,266935,3358 -Agora/Stake/policy/stakeCreation,43114795,124549,2156 -Agora/Stake/validator/stakeDepositWithdraw deposit,171823342,464745,4189 -Agora/Stake/validator/stakeDepositWithdraw withdraw,171823342,464745,4177 -Agora/Proposal/policy/proposalCreation,23140177,69194,1518 -Agora/Proposal/validator/cosignature/proposal,204468349,563576,6644 -Agora/Proposal/validator/cosignature/stake,114125937,284821,4726 -Agora/Proposal/validator/voting/proposal,165922664,436410,6573 -Agora/Proposal/validator/voting/stake,107127768,275725,4700 -Agora/Proposal/validator/advancing/successfully advance to next state/Draft -> VotringReady,161811766,432942,6471 -Agora/Proposal/validator/advancing/successfully advance to next state/VotingReady -> Locked,160968344,431439,6474 -Agora/Proposal/validator/advancing/successfully advance to next state/Locked -> Finished,162664811,435045,6474 -Agora/Proposal/validator/advancing/successfully advance to failed state: timeout/Draft -> Finished,160681965,430212,6473 -Agora/Proposal/validator/advancing/successfully advance to failed state: timeout/VotingReady -> Finished,159273054,427507,6474 -Agora/Proposal/validator/advancing/successfully advance to failed state: timeout/Locked -> Finished,160404032,429911,6474 -"Agora/Proposal/validator/unlocking/1 proposals/legal/retract votes and unlock stake while voting/1 proposals, voter, unlock stake + retract votes, VotingReady",188845005,491991,6573 -"Agora/Proposal/validator/unlocking/1 proposals/legal/unlock the stake that has been used to create the proposal/1 proposals, creator, unlock stake, Finished",167379302,437931,6577 -"Agora/Proposal/validator/unlocking/1 proposals/legal/unlock stake after voting/1 proposals, voter, unlock stake, Finished",166446391,438226,6577 -"Agora/Proposal/validator/unlocking/1 proposals/legal/unlock stake after voting/1 proposals, voter, unlock stake, Locked",166446391,438226,6577 -"Agora/Proposal/validator/unlocking/25 proposals/legal/retract votes and unlock stake while voting/25 proposals, voter, unlock stake + retract votes, VotingReady",1105617237,3029775,19323 -"Agora/Proposal/validator/unlocking/25 proposals/legal/unlock the stake that has been used to create the proposal/25 proposals, creator, unlock stake, Finished",935473982,2548251,19473 -"Agora/Proposal/validator/unlocking/25 proposals/legal/unlock stake after voting/25 proposals, voter, unlock stake, Finished",934541071,2548546,19424 -"Agora/Proposal/validator/unlocking/25 proposals/legal/unlock stake after voting/25 proposals, voter, unlock stake, Locked",934541071,2548546,19424 -Agora/AuthorityToken/singleAuthorityTokenBurned/Correct simple,21017788,55883,806 -Agora/AuthorityToken/singleAuthorityTokenBurned/Correct many inputs,33204186,88241,900 -Agora/Treasury/Validator/Positive/Allows for effect changes,29938856,79744,1390 -Agora/AuthorityToken/singleAuthorityTokenBurned/Correct simple,21017788,55883,806 -Agora/AuthorityToken/singleAuthorityTokenBurned/Correct many inputs,33204186,88241,900 -Agora/Governor/policy/GST minting,43087287,120125,1829 -Agora/Governor/validator/proposal creation,261928725,689487,8181 -Agora/Governor/validator/GATs minting,352305185,937264,8302 -Agora/Governor/validator/mutate governor state,84905433,234687,7766