Skip to content

Commit

Permalink
feat: update merkelized validator sample
Browse files Browse the repository at this point in the history
  • Loading branch information
hadelive committed Apr 27, 2024
1 parent 9698cad commit cc8a9b4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions plutarch-design-pattern.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,6 @@ test-suite plutarch-design-pattern-test
Spec.MerkelizedValidatorSpec
Spec.StakeValidatorSpec
Spec.TxLevelMinterSpec
Spec.Utils

build-depends: plutarch-design-pattern
3 changes: 2 additions & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ main = do
defaultMain $
testGroup
"Unit Test Group"
[ Spec.MerkelizedValidatorSpec.unitTest
[ Spec.MerkelizedValidatorSpec.spendUnitTest
, Spec.MerkelizedValidatorSpec.withdrawUnitTest
]
33 changes: 24 additions & 9 deletions test/Spec/MerkelizedValidatorSpec.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
module Spec.MerkelizedValidatorSpec (
psumOfSquares,
unitTest,
spendUnitTest,
withdrawUnitTest,
withdraw,
) where

import Plutarch.Api.V1.Address (PCredential (..))
import Plutarch.Api.V2 (
PScriptHash (..),
PStakeValidator,
PStakingCredential (..),
PValidator,
)
import Plutarch.Builtin (pasInt)
import Plutarch.MerkelizedValidator (spend)
import Plutarch.Builtin (pasInt, pdataImpl)
import Plutarch.MerkelizedValidator qualified as MerkelizedValidator
import Plutarch.Num ((#*), (#+))
import Plutarch.Prelude
import Plutarch.Test.Precompiled (Expectation (Failure), testEvalCase, tryFromPTerm)
Expand All @@ -22,25 +25,28 @@ import "liqwid-plutarch-extra" Plutarch.Extra.TermCont (
pletFieldsC,
)

psumOfSquares :: (PIsListLike list PInteger) => Term s (list PInteger :--> list PInteger)
psumOfSquares :: (PIsListLike list PData, PIsListLike list PInteger) => Term s (list PData :--> list PData)
psumOfSquares =
plam $ \xs ->
let result = pfoldl # plam (\x y -> (x #* x) #+ y) # 0 # xs
in psingleton # result
let result = pfoldl # plam (\x y -> (x #* x) #+ y) # 0 # (pmap # pasInt # xs)
in psingleton # pdataImpl result

validator :: Term s PStakingCredential -> Term s PValidator
validator stakeCred =
plam $ \x y ctx -> unTermCont $ do
ctxF <- pletFieldsC @'["txInfo"] ctx
txInfoF <- pletFieldsC @'["redeemers"] ctxF.txInfo
sum' <- pletC $ pheadSingleton #$ spend stakeCred (pcons # x # (pcons # y # pnil)) txInfoF.redeemers
sum' <- pletC $ pheadSingleton #$ MerkelizedValidator.spend stakeCred (pcons # x # (pcons # y # pnil)) txInfoF.redeemers
sum <- pletC $ pasInt # sum'
return $
pif
(sum #< 42)
(popaque $ pconstant ())
perror

withdraw :: Term s PStakeValidator
withdraw = MerkelizedValidator.withdraw psumOfSquares

scByteString :: Term s PByteString
scByteString = pconstant "b055a795895b15d9af25acb752ac89c78524acfa387acb626c7e1bc8"

Expand All @@ -49,12 +55,21 @@ stakeCred =
pcon $ PStakingHash $ pdcons @"_0" # (pdata $ pcon $ PScriptCredential $ pdcons @"_0" # (pdata . pcon . PScriptHash) scByteString # pdnil) # pdnil

-- TODO(hadelive)
unitTest :: TestTree
unitTest = tryFromPTerm "Merkelized Validator Unit Test" (validator stakeCred) $ do
spendUnitTest :: TestTree
spendUnitTest = tryFromPTerm "Merkelized Validator Spend Unit Test" (validator stakeCred) $ do
testEvalCase
"Fail"
Failure
[ PlutusTx.toData ()
, PlutusTx.toData ()
, PlutusTx.toData ()
]

withdrawUnitTest :: TestTree
withdrawUnitTest = tryFromPTerm "Merkelized Validator Withdraw Unit Test" withdraw $ do
testEvalCase
"Fail"
Failure
[ PlutusTx.toData ()
, PlutusTx.toData ()
]
23 changes: 23 additions & 0 deletions test/Spec/Utils.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Spec.Utils (
inputValidator,
inputOutputValidator,
collectiveOutputValidator,
) where

import Plutarch.Api.V2 (PTxInInfo, PTxOut)
import Plutarch.Prelude

inputValidator :: Term s (PTxInInfo :--> PBool)
inputValidator = phoistAcyclic $
plam $
\_ -> pcon PTrue

inputOutputValidator :: Term s (PTxOut :--> PTxOut :--> PBool)
inputOutputValidator = phoistAcyclic $
plam $
\_ _ -> pcon PTrue

collectiveOutputValidator :: Term s (PBuiltinList PTxOut :--> PInteger :--> PBool)
collectiveOutputValidator = phoistAcyclic $
plam $
\_ _ -> pcon PTrue

0 comments on commit cc8a9b4

Please sign in to comment.