From db92986c6d06e57cc1d5f5bfeb4f6224b4c1b696 Mon Sep 17 00:00:00 2001 From: fanghr Date: Fri, 20 May 2022 16:34:03 +0800 Subject: [PATCH] pull own output filtering out of the pmatch block --- agora/Agora/Proposal/Scripts.hs | 40 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/agora/Agora/Proposal/Scripts.hs b/agora/Agora/Proposal/Scripts.hs index 3fe0244..282ba26 100644 --- a/agora/Agora/Proposal/Scripts.hs +++ b/agora/Agora/Proposal/Scripts.hs @@ -186,6 +186,28 @@ proposalValidator proposal = currentTime <- tclet $ currentProposalTime # txInfoF.validRange + -- Filter out own output with own address and PST. + -- Delay the evaluation cause in some cases there won't be any continuing output. + ownOutputD <- + tclet $ + pdelay $ + mustBePJust # "Own output should be present" #$ pfind + # plam + ( \input -> unTermCont $ do + inputF <- tcont $ pletFields @'["address", "value"] input + pure $ + inputF.address #== ownAddress + #&& psymbolValueOf # stCurrencySymbol # inputF.value #== 1 + ) + # pfromData txInfoF.outputs + + proposalOutD <- + tclet $ + pdelay $ + mustFindDatum' @PProposalDatum + # (pfield @"datumHash" # pforce ownOutputD) + # txInfoF.datums + pure $ pmatch proposalRedeemer $ \case PVote r -> unTermCont $ do @@ -228,22 +250,6 @@ proposalValidator proposal = ) # pfromData stakeInF.lockedBy - -- TODO: maybe we can move this outside of the pmatch block. - -- Filter out own output with own address and PST. - let ownOutput = - mustBePJust # "Own output should be present" #$ pfind - # plam - ( \input -> unTermCont $ do - inputF <- tcont $ pletFields @'["address", "value"] input - pure $ - inputF.address #== ownAddress - #&& psymbolValueOf # stCurrencySymbol # inputF.value #== 1 - ) - # pfromData txInfoF.outputs - - proposalOut :: Term _ PProposalDatum - proposalOut = mustFindDatum' # (pfield @"datumHash" # ownOutput) # txInfoF.datums - let -- Update the vote counter of the proposal, and leave other stuff as is. expectedNewVotes = pmatch (pfromData proposalF.votes) $ \(PProposalVotes m) -> pcon $ @@ -268,7 +274,7 @@ proposalValidator proposal = .& #startingTime .= proposalF.startingTime ) - tcassert "Output proposal should be valid" $ proposalOut #== expectedProposalOut + tcassert "Output proposal should be valid" $ pforce proposalOutD #== expectedProposalOut -- We validate the output stake datum here as well: We need the vote option -- to create a valid 'ProposalLock', however the vote option is encoded