From d99fca16e62cfc324bfaaaebe943cd4fee05a1ba Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Tue, 29 Mar 2022 09:43:56 +0100 Subject: [PATCH 01/22] Edited gitignore to account for differing nix setups --- .envrc | 1 - .gitignore | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) delete mode 100644 .envrc diff --git a/.envrc b/.envrc deleted file mode 100644 index 051d09d..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -eval "$(lorri direnv)" diff --git a/.gitignore b/.gitignore index e541e83..21d33b5 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,7 @@ Thumbs.db # Local configuration files cabal.project.local* + +# envrc +.envrc +.direnv/ From cdda9e705a4843344111f11d5e50c4d83c73a1e9 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Tue, 29 Mar 2022 09:54:25 +0100 Subject: [PATCH 02/22] Added stub files --- apropos-tx.cabal | 3 +++ hie.yaml | 24 ++++--------------- src/Apropos/Gen/Address.hs | 0 src/Apropos/Gen/Bytes.hs | 0 src/Apropos/Gen/Contexts.hs | 45 +++++++++++++++++++++++++++++++++++ src/Apropos/Gen/Credential.hs | 3 +++ src/Apropos/Gen/Crypto.hs | 0 src/Apropos/Gen/DCert.hs | 0 src/Apropos/Gen/Interval.hs | 0 src/Apropos/Gen/Scripts.hs | 0 src/Apropos/Gen/Time.hs | 0 src/Apropos/Gen/Value.hs | 1 + 12 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 src/Apropos/Gen/Address.hs create mode 100644 src/Apropos/Gen/Bytes.hs create mode 100644 src/Apropos/Gen/Contexts.hs create mode 100644 src/Apropos/Gen/Credential.hs create mode 100644 src/Apropos/Gen/Crypto.hs create mode 100644 src/Apropos/Gen/DCert.hs create mode 100644 src/Apropos/Gen/Interval.hs create mode 100644 src/Apropos/Gen/Scripts.hs create mode 100644 src/Apropos/Gen/Time.hs create mode 100644 src/Apropos/Gen/Value.hs diff --git a/apropos-tx.cabal b/apropos-tx.cabal index 11482fd..fb3d216 100644 --- a/apropos-tx.cabal +++ b/apropos-tx.cabal @@ -64,6 +64,9 @@ library import: lang exposed-modules: Apropos.Script , Apropos.Tx + , Apropos.Gen.Contexts + , Apropos.Gen.Value + , Apropos.Gen.Credential hs-source-dirs: src test-suite examples diff --git a/hie.yaml b/hie.yaml index 15125c3..2fddac5 100644 --- a/hie.yaml +++ b/hie.yaml @@ -1,22 +1,6 @@ cradle: cabal: - - path: "./plutus-extra/src" - component: "lib:plutus-extra" - - path: "./plutus-extra/test" - component: "test:plutus-extra-test" - - path: "./plutus-golden/src" - component: "lib:plutus-golden" - - path: "./plutus-laws/src" - component: "lib:plutus-laws" - - path: "./plutus-numeric/src" - component: "lib:plutus-numeric" - - path: "./plutus-numeric/test/property" - component: "test:property" - - path: "./plutus-numeric/test/golden" - component: "test:golden" - - path: "./plutus-numeric/test/laws" - component: "test:laws" - - path: "./plutus-pretty/src" - component: "lib:plutus-pretty" - - path: "./tasty-plutus/src" - component: "lib:tasty-plutus" + - path: "./src" + component: "lib:apropos-tx" + - path: "./examples" + component: "test:examples" diff --git a/src/Apropos/Gen/Address.hs b/src/Apropos/Gen/Address.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/Bytes.hs b/src/Apropos/Gen/Bytes.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs new file mode 100644 index 0000000..c49dba1 --- /dev/null +++ b/src/Apropos/Gen/Contexts.hs @@ -0,0 +1,45 @@ +{-# OPTIONS_GHC -Wwarn #-} + +module Apropos.Gen.Contexts (scriptContext) where + +import Apropos.Gen (Gen, choice) +import Plutus.V1.Ledger.Contexts ( + ScriptContext (ScriptContext), + ScriptPurpose (Minting, Rewarding, Spending, Certifying), + TxInfo, + TxOutRef, + ) +import Plutus.V1.Ledger.Value (CurrencySymbol) +import Plutus.V1.Ledger.Credential (StakingCredential) + +scriptContext :: Gen ScriptContext +scriptContext = do + i <- txInfo + p <- scriptPurpose + return $ ScriptContext i p + +txInfo :: Gen TxInfo +txInfo = undefined + +scriptPurpose :: Gen ScriptPurpose +scriptPurpose = do + c <- currencySymbol + t <- txOutRef + s <- undefined + d <- undefined + choice + [ return $ Minting c + , return $ Spending t + , return $ Rewarding s + , return $ Certifying d + ] + +currencySymbol :: Gen CurrencySymbol +currencySymbol = undefined + +txOutRef :: Gen TxOutRef +txOutRef = undefined + +stakingCredential :: Gen StakingCredential +stakingCredential = undefined + diff --git a/src/Apropos/Gen/Credential.hs b/src/Apropos/Gen/Credential.hs new file mode 100644 index 0000000..c61a9df --- /dev/null +++ b/src/Apropos/Gen/Credential.hs @@ -0,0 +1,3 @@ +module Apropos.Gen.Credential () where + + diff --git a/src/Apropos/Gen/Crypto.hs b/src/Apropos/Gen/Crypto.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/DCert.hs b/src/Apropos/Gen/DCert.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/Interval.hs b/src/Apropos/Gen/Interval.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/Time.hs b/src/Apropos/Gen/Time.hs new file mode 100644 index 0000000..e69de29 diff --git a/src/Apropos/Gen/Value.hs b/src/Apropos/Gen/Value.hs new file mode 100644 index 0000000..2071d14 --- /dev/null +++ b/src/Apropos/Gen/Value.hs @@ -0,0 +1 @@ +module Apropos.Gen.Value () where From 135161caef5bccf209aaa39a7e0ff3526c4e4cb6 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Tue, 29 Mar 2022 11:00:12 +0100 Subject: [PATCH 03/22] Stubbed out the entirety of contexts --- Makefile | 3 +- apropos-tx.cabal | 95 +++++++++++++++++++---------------- src/Apropos/Gen/Address.hs | 7 +++ src/Apropos/Gen/Contexts.hs | 91 ++++++++++++++++++++++++++------- src/Apropos/Gen/Credential.hs | 6 ++- src/Apropos/Gen/Crypto.hs | 7 +++ src/Apropos/Gen/DCert.hs | 7 +++ src/Apropos/Gen/Extra.hs | 19 +++++++ src/Apropos/Gen/Scripts.hs | 10 ++++ src/Apropos/Gen/Time.hs | 7 +++ src/Apropos/Gen/TxId.hs | 7 +++ src/Apropos/Gen/Value.hs | 14 +++++- 12 files changed, 211 insertions(+), 62 deletions(-) create mode 100644 src/Apropos/Gen/Extra.hs create mode 100644 src/Apropos/Gen/TxId.hs diff --git a/Makefile b/Makefile index 2b1aedc..eba2402 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,8 @@ usage: @echo " update_plutus -- Update plutus version with niv" hoogle: requires_nix_shell - hoogle server --local + pkill hoogle || true + hoogle server --local > /dev/null & STACK_EXE_PATH = $(shell stack $(STACK_FLAGS) path --local-install-root)/bin diff --git a/apropos-tx.cabal b/apropos-tx.cabal index fb3d216..051e504 100644 --- a/apropos-tx.cabal +++ b/apropos-tx.cabal @@ -37,60 +37,71 @@ common lang TypeSynonymInstances UndecidableInstances - build-depends: base ^>=4.14 - , apropos - , containers - , hedgehog - , free - , lens - , minisat-solver - , mtl - , plutarch - , plutus-core - , plutus-ledger-api - , pretty - , pretty-show - , safe - , template-haskell - , text - , transformers + build-depends: + , apropos + , base ^>=4.14 + , containers + , free + , hedgehog + , lens + , minisat-solver + , mtl + , plutarch + , plutus-core + , plutus-ledger-api + , pretty + , pretty-show + , safe + , template-haskell + , text + , transformers + ghc-options: -Wall -Wcompat -Wincomplete-uni-patterns -Wredundant-constraints -Wmissing-export-lists -Werror -Wincomplete-record-updates - -Wmissing-deriving-strategies - -ddump-splices + -Wmissing-deriving-strategies -ddump-splices library import: lang - exposed-modules: Apropos.Script - , Apropos.Tx - , Apropos.Gen.Contexts - , Apropos.Gen.Value - , Apropos.Gen.Credential + exposed-modules: + Apropos.Gen.Address + Apropos.Gen.Contexts + Apropos.Gen.Credential + Apropos.Gen.Crypto + Apropos.Gen.DCert + Apropos.Gen.Extra + Apropos.Gen.Scripts + Apropos.Gen.Time + Apropos.Gen.TxId + Apropos.Gen.Value + Apropos.Script + Apropos.Tx + hs-source-dirs: src test-suite examples - import: lang - type: exitcode-stdio-1.0 - main-is: Main.hs + import: lang + type: exitcode-stdio-1.0 + main-is: Main.hs hs-source-dirs: examples - ghc-options: -threaded -rtsopts -with-rtsopts=-N + ghc-options: -threaded -rtsopts -with-rtsopts=-N other-modules: Spec.Int - Spec.IntPermutationGen Spec.IntPair - Spec.Plutarch.MagicNumber + Spec.IntPermutationGen Spec.Plutarch.CostModel + Spec.Plutarch.MagicNumber + build-depends: - base, - apropos, - apropos-tx, - plutarch, - tasty, - tasty-hedgehog, - hedgehog, - plutus-ledger-api, - plutus-core, - text, - containers, - mtl, + , apropos + , apropos-tx + , base + , containers + , hedgehog + , mtl + , plutarch + , plutus-core + , plutus-ledger-api + , tasty + , tasty-hedgehog + , text diff --git a/src/Apropos/Gen/Address.hs b/src/Apropos/Gen/Address.hs index e69de29..41f4eef 100644 --- a/src/Apropos/Gen/Address.hs +++ b/src/Apropos/Gen/Address.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.Address (address) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Address (Address) + +address :: Gen Address +address = undefined diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index c49dba1..cb56aa0 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -2,15 +2,37 @@ module Apropos.Gen.Contexts (scriptContext) where -import Apropos.Gen (Gen, choice) +import Apropos.Gen (Gen, choice, linear, list) +import Apropos.Gen.Address (address) +import Apropos.Gen.Credential (stakingCredential) +import Apropos.Gen.Crypto (pubKeyHash) +import Apropos.Gen.DCert (dCert) +import Apropos.Gen.Extra (integer, pair) +import Apropos.Gen.Extra qualified as Gen (maybe) +import Apropos.Gen.Scripts (datum, datumHash) +import Apropos.Gen.Time (posixTimeRange) +import Apropos.Gen.TxId (txId) +import Apropos.Gen.Value (currencySymbol, value) import Plutus.V1.Ledger.Contexts ( ScriptContext (ScriptContext), - ScriptPurpose (Minting, Rewarding, Spending, Certifying), - TxInfo, - TxOutRef, + ScriptPurpose (Certifying, Minting, Rewarding, Spending), + TxInInfo (TxInInfo), + TxInfo ( + TxInfo, + txInfoDCert, + txInfoData, + txInfoFee, + txInfoId, + txInfoInputs, + txInfoMint, + txInfoOutputs, + txInfoSignatories, + txInfoValidRange, + txInfoWdrl + ), + TxOut (TxOut), + TxOutRef (TxOutRef), ) -import Plutus.V1.Ledger.Value (CurrencySymbol) -import Plutus.V1.Ledger.Credential (StakingCredential) scriptContext :: Gen ScriptContext scriptContext = do @@ -19,14 +41,53 @@ scriptContext = do return $ ScriptContext i p txInfo :: Gen TxInfo -txInfo = undefined +txInfo = do + ins <- list (linear 1 5) txInInfo + outs <- list (linear 1 5) txOut + fee <- value + mint <- value + dCert' <- list (linear 1 10) dCert + wdrl <- + list (linear 0 10) $ + pair stakingCredential $ + integer (linear 0 50) + range <- posixTimeRange + sigs <- list (linear 0 10) pubKeyHash + data' <- list (linear 1 5) $ pair datumHash datum + id' <- txId + return $ + TxInfo + { txInfoInputs = ins + , txInfoOutputs = outs + , txInfoFee = fee + , txInfoMint = mint + , txInfoDCert = dCert' + , txInfoWdrl = wdrl + , txInfoValidRange = range + , txInfoSignatories = sigs + , txInfoData = data' + , txInfoId = id' + } + +txInInfo :: Gen TxInInfo +txInInfo = do + oRef <- txOutRef + o <- txOut + return $ TxInInfo oRef o + +txOut :: Gen TxOut +txOut = do + a <- address + v <- value + h <- Gen.maybe datumHash + return $ TxOut a v h scriptPurpose :: Gen ScriptPurpose scriptPurpose = do c <- currencySymbol t <- txOutRef - s <- undefined - d <- undefined + s <- stakingCredential + d <- dCert choice [ return $ Minting c , return $ Spending t @@ -34,12 +95,8 @@ scriptPurpose = do , return $ Certifying d ] -currencySymbol :: Gen CurrencySymbol -currencySymbol = undefined - txOutRef :: Gen TxOutRef -txOutRef = undefined - -stakingCredential :: Gen StakingCredential -stakingCredential = undefined - +txOutRef = do + id' <- txId + idx <- choice $ return <$> [0 .. toInteger (maxBound :: Int)] + return $ TxOutRef id' idx diff --git a/src/Apropos/Gen/Credential.hs b/src/Apropos/Gen/Credential.hs index c61a9df..7bd3b4b 100644 --- a/src/Apropos/Gen/Credential.hs +++ b/src/Apropos/Gen/Credential.hs @@ -1,3 +1,7 @@ -module Apropos.Gen.Credential () where +module Apropos.Gen.Credential (stakingCredential) where +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Credential (StakingCredential) +stakingCredential :: Gen StakingCredential +stakingCredential = undefined diff --git a/src/Apropos/Gen/Crypto.hs b/src/Apropos/Gen/Crypto.hs index e69de29..9c37091 100644 --- a/src/Apropos/Gen/Crypto.hs +++ b/src/Apropos/Gen/Crypto.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.Crypto (pubKeyHash) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Crypto (PubKeyHash) + +pubKeyHash :: Gen PubKeyHash +pubKeyHash = undefined diff --git a/src/Apropos/Gen/DCert.hs b/src/Apropos/Gen/DCert.hs index e69de29..f7609ed 100644 --- a/src/Apropos/Gen/DCert.hs +++ b/src/Apropos/Gen/DCert.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.DCert (dCert) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.DCert (DCert) + +dCert :: Gen DCert +dCert = undefined diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs new file mode 100644 index 0000000..cd59f7e --- /dev/null +++ b/src/Apropos/Gen/Extra.hs @@ -0,0 +1,19 @@ +module Apropos.Gen.Extra (pair, integer, maybe) where + +import Apropos.Gen (Gen, choice, int) +import Apropos.Gen.Range (Range) +import Prelude hiding (maybe) + +pair :: Gen a -> Gen b -> Gen (a, b) +pair genA genB = do + a <- genA + b <- genB + return (a, b) + +integer :: Range -> Gen Integer +integer r = toInteger <$> int r + +maybe :: Gen a -> Gen (Maybe a) +maybe genA = do + x <- genA + choice [return (Just x), return Nothing] diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs index e69de29..899311b 100644 --- a/src/Apropos/Gen/Scripts.hs +++ b/src/Apropos/Gen/Scripts.hs @@ -0,0 +1,10 @@ +module Apropos.Gen.Scripts (datum, datumHash) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Scripts (Datum, DatumHash) + +datum :: Gen Datum +datum = undefined + +datumHash :: Gen DatumHash +datumHash = undefined diff --git a/src/Apropos/Gen/Time.hs b/src/Apropos/Gen/Time.hs index e69de29..cb53747 100644 --- a/src/Apropos/Gen/Time.hs +++ b/src/Apropos/Gen/Time.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.Time (posixTimeRange) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Time (POSIXTimeRange) + +posixTimeRange :: Gen POSIXTimeRange +posixTimeRange = undefined diff --git a/src/Apropos/Gen/TxId.hs b/src/Apropos/Gen/TxId.hs new file mode 100644 index 0000000..813c765 --- /dev/null +++ b/src/Apropos/Gen/TxId.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.TxId (txId) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.TxId (TxId) + +txId :: Gen TxId +txId = undefined diff --git a/src/Apropos/Gen/Value.hs b/src/Apropos/Gen/Value.hs index 2071d14..49b29e3 100644 --- a/src/Apropos/Gen/Value.hs +++ b/src/Apropos/Gen/Value.hs @@ -1 +1,13 @@ -module Apropos.Gen.Value () where +module Apropos.Gen.Value ( + currencySymbol, + value +) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Value (Value, CurrencySymbol) + +value :: Gen Value +value = undefined + +currencySymbol :: Gen CurrencySymbol +currencySymbol = undefined From 72b269b80847727fe33323e574bae5b34d258911 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:15:01 +0100 Subject: [PATCH 04/22] continued speccing out components --- apropos-tx.cabal | 1 + src/Apropos/Gen/Address.hs | 13 ++++++++++--- src/Apropos/Gen/Api.hs | 7 +++++++ src/Apropos/Gen/Contexts.hs | 14 +++++++------- src/Apropos/Gen/Credential.hs | 25 +++++++++++++++++++++---- src/Apropos/Gen/Crypto.hs | 7 +++++-- src/Apropos/Gen/Extra.hs | 6 +++--- src/Apropos/Gen/Scripts.hs | 14 ++++++++++++-- src/Apropos/Gen/TxId.hs | 7 +++++-- 9 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 src/Apropos/Gen/Api.hs diff --git a/apropos-tx.cabal b/apropos-tx.cabal index 051e504..ac3602d 100644 --- a/apropos-tx.cabal +++ b/apropos-tx.cabal @@ -65,6 +65,7 @@ library import: lang exposed-modules: Apropos.Gen.Address + Apropos.Gen.Api Apropos.Gen.Contexts Apropos.Gen.Credential Apropos.Gen.Crypto diff --git a/src/Apropos/Gen/Address.hs b/src/Apropos/Gen/Address.hs index 41f4eef..8466b58 100644 --- a/src/Apropos/Gen/Address.hs +++ b/src/Apropos/Gen/Address.hs @@ -1,7 +1,14 @@ module Apropos.Gen.Address (address) where -import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Address (Address) +import Apropos.Gen (Gen, element) +import Apropos.Gen.Credential (credential, stakingCredential) +import Apropos.Gen.Extra qualified as Gen (maybe) +import Plutus.V1.Ledger.Address ( + Address (Address), + ) address :: Gen Address -address = undefined +address = do + cred <- credential + scred <- Gen.maybe stakingCredential + return $ Address cred scred diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs new file mode 100644 index 0000000..8d80431 --- /dev/null +++ b/src/Apropos/Gen/Api.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.Api (builtinByteString) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Api (BuiltinByteString) + +builtinByteString :: Gen BuiltinByteString +builtinByteString = undefined diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index cb56aa0..e521087 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -2,7 +2,7 @@ module Apropos.Gen.Contexts (scriptContext) where -import Apropos.Gen (Gen, choice, linear, list) +import Apropos.Gen (Gen, element, linear, list) import Apropos.Gen.Address (address) import Apropos.Gen.Credential (stakingCredential) import Apropos.Gen.Crypto (pubKeyHash) @@ -88,15 +88,15 @@ scriptPurpose = do t <- txOutRef s <- stakingCredential d <- dCert - choice - [ return $ Minting c - , return $ Spending t - , return $ Rewarding s - , return $ Certifying d + element + [ Minting c + , Spending t + , Rewarding s + , Certifying d ] txOutRef :: Gen TxOutRef txOutRef = do id' <- txId - idx <- choice $ return <$> [0 .. toInteger (maxBound :: Int)] + idx <- integer (linear 0 maxBound) return $ TxOutRef id' idx diff --git a/src/Apropos/Gen/Credential.hs b/src/Apropos/Gen/Credential.hs index 7bd3b4b..d4e85d7 100644 --- a/src/Apropos/Gen/Credential.hs +++ b/src/Apropos/Gen/Credential.hs @@ -1,7 +1,24 @@ -module Apropos.Gen.Credential (stakingCredential) where +module Apropos.Gen.Credential (credential, stakingCredential) where -import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Credential (StakingCredential) +import Apropos.Gen (Gen, element, linear) +import Apropos.Gen.Crypto (pubKeyHash) +import Apropos.Gen.Extra (integer) +import Apropos.Gen.Scripts (validatorHash) +import Plutus.V1.Ledger.Credential ( + Credential (PubKeyCredential, ScriptCredential), + StakingCredential (StakingHash, StakingPtr), + ) + +credential :: Gen Credential +credential = do + pkh <- pubKeyHash + vh <- validatorHash + element [PubKeyCredential pkh, ScriptCredential vh] stakingCredential :: Gen StakingCredential -stakingCredential = undefined +stakingCredential = do + cred <- credential + p0 <- integer $ linear 0 1000 + p1 <- integer $ linear 0 1000 + p2 <- integer $ linear 0 1000 + element [StakingHash cred, StakingPtr p0 p1 p2] diff --git a/src/Apropos/Gen/Crypto.hs b/src/Apropos/Gen/Crypto.hs index 9c37091..832a010 100644 --- a/src/Apropos/Gen/Crypto.hs +++ b/src/Apropos/Gen/Crypto.hs @@ -1,7 +1,10 @@ module Apropos.Gen.Crypto (pubKeyHash) where import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Crypto (PubKeyHash) +import Apropos.Gen.Api (builtinByteString) +import Plutus.V1.Ledger.Crypto (PubKeyHash (PubKeyHash)) pubKeyHash :: Gen PubKeyHash -pubKeyHash = undefined +pubKeyHash = do + bs <- builtinByteString + return $ PubKeyHash bs diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs index cd59f7e..4fc9044 100644 --- a/src/Apropos/Gen/Extra.hs +++ b/src/Apropos/Gen/Extra.hs @@ -1,6 +1,6 @@ module Apropos.Gen.Extra (pair, integer, maybe) where -import Apropos.Gen (Gen, choice, int) +import Apropos.Gen (Gen, element, int) import Apropos.Gen.Range (Range) import Prelude hiding (maybe) @@ -13,7 +13,7 @@ pair genA genB = do integer :: Range -> Gen Integer integer r = toInteger <$> int r -maybe :: Gen a -> Gen (Maybe a) +maybe :: Show a => Gen a -> Gen (Maybe a) maybe genA = do x <- genA - choice [return (Just x), return Nothing] + element [Just x, Nothing] diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs index 899311b..a42aa8d 100644 --- a/src/Apropos/Gen/Scripts.hs +++ b/src/Apropos/Gen/Scripts.hs @@ -1,10 +1,20 @@ -module Apropos.Gen.Scripts (datum, datumHash) where +module Apropos.Gen.Scripts ( + validatorHash, + datum, + datumHash, +) where import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Scripts (Datum, DatumHash) +import Apropos.Gen.Api (builtinByteString) +import Plutus.V1.Ledger.Scripts (Datum, DatumHash, ValidatorHash (ValidatorHash)) datum :: Gen Datum datum = undefined datumHash :: Gen DatumHash datumHash = undefined + +validatorHash :: Gen ValidatorHash +validatorHash = do + bs <- builtinByteString + return $ ValidatorHash bs diff --git a/src/Apropos/Gen/TxId.hs b/src/Apropos/Gen/TxId.hs index 813c765..facb64b 100644 --- a/src/Apropos/Gen/TxId.hs +++ b/src/Apropos/Gen/TxId.hs @@ -1,7 +1,10 @@ module Apropos.Gen.TxId (txId) where import Apropos.Gen (Gen) -import Plutus.V1.Ledger.TxId (TxId) +import Apropos.Gen.Api (builtinByteString) +import Plutus.V1.Ledger.TxId (TxId (TxId)) txId :: Gen TxId -txId = undefined +txId = do + bs <- builtinByteString + return $ TxId bs From 6783cf49d0eff5f5ff29f67bd389dee026efbec9 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Wed, 30 Mar 2022 12:30:22 +0100 Subject: [PATCH 05/22] Added numerous functions to Apropos gen extra --- apropos-tx.cabal | 2 + src/Apropos/Gen/Address.hs | 2 +- src/Apropos/Gen/Api.hs | 25 ++++++++-- src/Apropos/Gen/DCert.hs | 32 ++++++++++-- src/Apropos/Gen/Extra.hs | 99 ++++++++++++++++++++++++++++++++++++-- src/Apropos/Gen/Scripts.hs | 16 ++++-- src/Apropos/Gen/Value.hs | 30 +++++++++--- 7 files changed, 185 insertions(+), 21 deletions(-) diff --git a/apropos-tx.cabal b/apropos-tx.cabal index ac3602d..e844776 100644 --- a/apropos-tx.cabal +++ b/apropos-tx.cabal @@ -49,11 +49,13 @@ common lang , plutarch , plutus-core , plutus-ledger-api + , plutus-tx , pretty , pretty-show , safe , template-haskell , text + , bytestring , transformers ghc-options: diff --git a/src/Apropos/Gen/Address.hs b/src/Apropos/Gen/Address.hs index 8466b58..ccbb509 100644 --- a/src/Apropos/Gen/Address.hs +++ b/src/Apropos/Gen/Address.hs @@ -1,6 +1,6 @@ module Apropos.Gen.Address (address) where -import Apropos.Gen (Gen, element) +import Apropos.Gen (Gen) import Apropos.Gen.Credential (credential, stakingCredential) import Apropos.Gen.Extra qualified as Gen (maybe) import Plutus.V1.Ledger.Address ( diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs index 8d80431..1d0df11 100644 --- a/src/Apropos/Gen/Api.hs +++ b/src/Apropos/Gen/Api.hs @@ -1,7 +1,26 @@ -module Apropos.Gen.Api (builtinByteString) where +module Apropos.Gen.Api ( + builtinByteString, + builtinData, +) where import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Api (BuiltinByteString) +import Apropos.Gen.Extra (sha256) +import PlutusTx (Data) +import PlutusTx.Builtins.Internal ( + BuiltinByteString (BuiltinByteString), + BuiltinData (BuiltinData), + ) builtinByteString :: Gen BuiltinByteString -builtinByteString = undefined +builtinByteString = do + bs <- sha256 + return $ BuiltinByteString bs + +builtinData :: Gen BuiltinData +builtinData = do + d <- data' + return $ BuiltinData d + +data' :: Gen Data +data' = do + undefined diff --git a/src/Apropos/Gen/DCert.hs b/src/Apropos/Gen/DCert.hs index f7609ed..762700f 100644 --- a/src/Apropos/Gen/DCert.hs +++ b/src/Apropos/Gen/DCert.hs @@ -1,7 +1,33 @@ module Apropos.Gen.DCert (dCert) where -import Apropos.Gen (Gen) -import Plutus.V1.Ledger.DCert (DCert) +import Apropos.Gen (Gen, element, linear) +import Apropos.Gen.Credential (stakingCredential) +import Apropos.Gen.Crypto (pubKeyHash) +import Apropos.Gen.Extra (integer) +import Plutus.V1.Ledger.DCert ( + DCert ( + DCertDelegDeRegKey, + DCertDelegDelegate, + DCertDelegRegKey, + DCertGenesis, + DCertMir, + DCertPoolRegister, + DCertPoolRetire + ), + ) dCert :: Gen DCert -dCert = undefined +dCert = do + sc <- stakingCredential + pkh <- pubKeyHash + pkh' <- pubKeyHash + n <- integer (linear 0 300) + element + [ DCertDelegRegKey sc + , DCertDelegDeRegKey sc + , DCertDelegDelegate sc pkh + , DCertPoolRegister pkh pkh' + , DCertPoolRetire pkh n + , DCertGenesis + , DCertMir + ] diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs index 4fc9044..b6d2210 100644 --- a/src/Apropos/Gen/Extra.hs +++ b/src/Apropos/Gen/Extra.hs @@ -1,8 +1,20 @@ -module Apropos.Gen.Extra (pair, integer, maybe) where +{-# OPTIONS_GHC -Wwarn #-} -import Apropos.Gen (Gen, element, int) -import Apropos.Gen.Range (Range) -import Prelude hiding (maybe) +module Apropos.Gen.Extra ( + sha256, + map, + pair, + integer, + maybe, +) where + +import Apropos.Gen (Gen, choice, element, int, list) +import Apropos.Gen.Range (Range, singleton) +import Data.ByteString (ByteString) +import Data.Text (Text, pack) +import Data.Text.Encoding (encodeUtf8) +import PlutusTx.AssocMap (Map, fromList) +import Prelude hiding (map, maybe) pair :: Gen a -> Gen b -> Gen (a, b) pair genA genB = do @@ -17,3 +29,82 @@ maybe :: Show a => Gen a -> Gen (Maybe a) maybe genA = do x <- genA element [Just x, Nothing] + +{- | Given a generator for a key-type k and a value-type v + returns a generator for a `Map` k v. +-} +map :: + -- | `Range` for the number of map entries. + Range -> + -- | Generator for the desired type of the keys. + Gen k -> + -- | Generator for the desired type of the values. + Gen v -> + -- | Generator for a map. + Gen (Map k v) -- +map r genK genV = do + ks <- list r genK + vs <- list r genV + let mapList = zip ks vs + return $ fromList mapList + +upperChar :: Gen Char +upperChar = element ['A' .. 'Z'] + +lowerChar :: Gen Char +lowerChar = element ['a' .. 'z'] + +numChar :: Gen Char +numChar = element ['0' .. '9'] + +hexChar :: Gen Char +hexChar = element $ ['0' .. '9'] ++ ['a' .. 'f'] + +alphaString :: Range -> Gen String +alphaString r = list r gen + where + gen = choice [upperChar, lowerChar] + +alphaNumericString :: Range -> Gen String +alphaNumericString r = list r gen + where + gen = choice [upperChar, lowerChar, numChar] + +numericString :: Range -> Gen String +numericString r = list r numChar + +hexString :: Range -> Gen String +hexString r = list r hexChar + +genText :: (Range -> Gen String) -> (Range -> Gen Text) +genText f r = pack <$> f r + +alphaText :: Range -> Gen Text +alphaText = genText alphaString + +alphaNumericText :: Range -> Gen Text +alphaNumericText = genText alphaNumericString + +numericText :: Range -> Gen Text +numericText = genText alphaString + +hexText :: Range -> Gen Text +hexText = genText hexString + +genBS :: (Range -> Gen Text) -> (Range -> Gen ByteString) +genBS f r = encodeUtf8 <$> f r + +alphaBS :: Range -> Gen ByteString +alphaBS = genBS alphaText + +alphaNumericBS :: Range -> Gen ByteString +alphaNumericBS = genBS alphaNumericText + +numericBS :: Range -> Gen ByteString +numericBS = genBS numericText + +hexBS :: Range -> Gen ByteString +hexBS = genBS hexText + +sha256 :: Gen ByteString +sha256 = hexBS $ singleton 64 diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs index a42aa8d..f456057 100644 --- a/src/Apropos/Gen/Scripts.hs +++ b/src/Apropos/Gen/Scripts.hs @@ -5,14 +5,22 @@ module Apropos.Gen.Scripts ( ) where import Apropos.Gen (Gen) -import Apropos.Gen.Api (builtinByteString) -import Plutus.V1.Ledger.Scripts (Datum, DatumHash, ValidatorHash (ValidatorHash)) +import Apropos.Gen.Api (builtinData, builtinByteString) +import Plutus.V1.Ledger.Scripts ( + Datum (Datum), + DatumHash (DatumHash), + ValidatorHash (ValidatorHash), + ) datum :: Gen Datum -datum = undefined +datum = do + bid <- builtinData + return $ Datum bid datumHash :: Gen DatumHash -datumHash = undefined +datumHash = do + bs <- builtinByteString + return $ DatumHash bs validatorHash :: Gen ValidatorHash validatorHash = do diff --git a/src/Apropos/Gen/Value.hs b/src/Apropos/Gen/Value.hs index 49b29e3..7e0bb4d 100644 --- a/src/Apropos/Gen/Value.hs +++ b/src/Apropos/Gen/Value.hs @@ -1,13 +1,31 @@ module Apropos.Gen.Value ( currencySymbol, - value + value, ) where -import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Value (Value, CurrencySymbol) +import Apropos.Gen (Gen, linear) +import Apropos.Gen.Api (builtinByteString) +import Apropos.Gen.Extra (integer) +import Apropos.Gen.Extra qualified as Gen (map) +import Plutus.V1.Ledger.Value ( + CurrencySymbol (CurrencySymbol), + TokenName (TokenName), + Value (Value), + ) +import PlutusTx.AssocMap (Map) -value :: Gen Value -value = undefined +value :: Gen Value +value = do + let tnvMap = Gen.map (linear 1 5) tokenName $ integer (linear 1 maxBound) + v <- Gen.map (linear 1 5) currencySymbol tnvMap + return $ Value v currencySymbol :: Gen CurrencySymbol -currencySymbol = undefined +currencySymbol = do + bs <- builtinByteString + return $ CurrencySymbol bs + +tokenName :: Gen TokenName +tokenName = do + bs <- builtinByteString + return $ TokenName bs From 4e3ef85f2c649f92d9003bb1112f4df459e2e240 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Mon, 4 Apr 2022 12:39:55 +0100 Subject: [PATCH 06/22] Fixed CI build issue --- apropos-tx.cabal | 1 - 1 file changed, 1 deletion(-) diff --git a/apropos-tx.cabal b/apropos-tx.cabal index 1cdf5c1..a471a24 100644 --- a/apropos-tx.cabal +++ b/apropos-tx.cabal @@ -90,7 +90,6 @@ test-suite examples ghc-options: -threaded -rtsopts -with-rtsopts=-N other-modules: Spec.Int - Spec.IntPair Spec.IntPermutationGen Spec.Plutarch.CostModel Spec.Plutarch.MagicNumber From 7c7a1bfd6bb5761e939918d9f127658cbc9f0cfc Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Mon, 4 Apr 2022 12:40:11 +0100 Subject: [PATCH 07/22] Formatting --- src/Apropos/Gen/Api.hs | 2 +- src/Apropos/Gen/Scripts.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs index 1d0df11..eaf5acb 100644 --- a/src/Apropos/Gen/Api.hs +++ b/src/Apropos/Gen/Api.hs @@ -22,5 +22,5 @@ builtinData = do return $ BuiltinData d data' :: Gen Data -data' = do +data' = do undefined diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs index f456057..28a0ead 100644 --- a/src/Apropos/Gen/Scripts.hs +++ b/src/Apropos/Gen/Scripts.hs @@ -5,7 +5,7 @@ module Apropos.Gen.Scripts ( ) where import Apropos.Gen (Gen) -import Apropos.Gen.Api (builtinData, builtinByteString) +import Apropos.Gen.Api (builtinByteString, builtinData) import Plutus.V1.Ledger.Scripts ( Datum (Datum), DatumHash (DatumHash), From afff43e97f051cf0fe2931e0a3d32ae14fa821ad Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Mon, 4 Apr 2022 12:41:25 +0100 Subject: [PATCH 08/22] linting suggestions --- examples/Spec/Plutarch/CostModel.hs | 2 +- src/Apropos/Script.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Spec/Plutarch/CostModel.hs b/examples/Spec/Plutarch/CostModel.hs index 6326417..5c06fdd 100644 --- a/examples/Spec/Plutarch/CostModel.hs +++ b/examples/Spec/Plutarch/CostModel.hs @@ -62,7 +62,7 @@ addCostPropGenTests = ] instance ScriptModel CostModelProp Integer where - script _ i = addCost i + script _ = addCost expect _ = Yes :: Formula CostModelProp -- This is the cool bit. We can model the cost exactly. Neato. diff --git a/src/Apropos/Script.hs b/src/Apropos/Script.hs index e2d2fc9..6ff54e3 100644 --- a/src/Apropos/Script.hs +++ b/src/Apropos/Script.hs @@ -147,7 +147,7 @@ class (HasLogicalModel p m, HasParameterisedGenerator p m) => ScriptModel p m wh "Parameters" $+$ ppDoc model dumpLogs :: [Text] -> Doc - dumpLogs logs = vcat . fmap go . zip [1 ..] $ logs + dumpLogs = vcat . fmap go . zip [1 ..] go :: (Int, Text) -> Doc go (ix, line) = (PP.int ix <> colon) <+> (text . show $ line) From 258d1e9aaccf6ea7d6732e705c0f33493b4e0081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Wed, 6 Apr 2022 14:06:02 +0300 Subject: [PATCH 09/22] switch to hercules, add fmt checks to flake --- .github/format.sh | 6 --- .github/workflows/integrate.yaml | 78 --------------------------- Makefile | 18 +++---- examples/Spec/Int.hs | 4 +- examples/Spec/IntPermutationGen.hs | 4 +- examples/Spec/Plutarch/CostModel.hs | 2 +- examples/Spec/Plutarch/MagicNumber.hs | 2 +- flake.nix | 73 +++++++++++++++---------- 8 files changed, 59 insertions(+), 128 deletions(-) delete mode 100755 .github/format.sh delete mode 100644 .github/workflows/integrate.yaml diff --git a/.github/format.sh b/.github/format.sh deleted file mode 100755 index db31a21..0000000 --- a/.github/format.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -# Extensions necessary to tell fourmolu about -EXTENSIONS="-o -XTypeApplications -o -XTemplateHaskell -o -XImportQualifiedPost -o -XPatternSynonyms -o -fplugin=RecordDotPreprocessor -o -XBangPatterns" -SOURCES=$(git ls-tree -r HEAD --full-tree --name-only | grep -E '.*\.hs') -fourmolu --mode check --check-idempotence $EXTENSIONS $SOURCES diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml deleted file mode 100644 index dfcc81b..0000000 --- a/.github/workflows/integrate.yaml +++ /dev/null @@ -1,78 +0,0 @@ -on: - push: - paths: - - '**.hs' - - '**.nix' - - 'nix/sources.json' - - 'apropos-tx.cabal' - - '.github/*' - - '.github/workflows/*' - branches: - - main - - master - - staging - pull_request: - paths: - - '**.hs' - - '**.nix' - - 'nix/sources.json' - - 'apropos-tx.cabal' -jobs: - fourmolu-check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: cachix/install-nix-action@v13 - name: Set up nix and IOHK cache - with: - nix_path: nixpkgs=channel:nixos-21.11 - extra_nix_config: | - trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= - substituters = https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/ - experimental-features = nix-command flakes - - uses: cachix/cachix-action@v10 - with: - name: mlabs - authToken: ${{ secrets.CACHIX_KEY }} - - run: nix-env -iA haskellPackages.fourmolu -f '' - name: Install fourmolu - - run: ./.github/format.sh - name: Format - hlint-check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: cachix/install-nix-action@v13 - name: Set up nix and IOHK cache - with: - nix_path: nixpkgs=channel:nixos-21.11 - extra_nix_config: | - trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= - substituters = https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/ - experimental-features = nix-command flakes - - uses: cachix/cachix-action@v10 - with: - name: mlabs - authToken: ${{ secrets.CACHIX_KEY }} - - run: nix-env -iA hlint -f '' - name: Install hlint - - run: hlint $(git ls-tree -r HEAD --full-tree --name-only | grep -E '.*\.hs') - name: Lint - cabal-check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: cachix/install-nix-action@v13 - name: Set up nix and IOHK cache - with: - nix_path: nixpkgs=channel:nixos-21.11 - extra_nix_config: | - trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= - substituters = https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/ - experimental-features = nix-command flakes - - uses: cachix/cachix-action@v10 - with: - name: mlabs - authToken: ${{ secrets.CACHIX_KEY }} - - name: Build the full ci derivation - run: nix build .#check.x86_64-linux diff --git a/Makefile b/Makefile index 2b1aedc..aed85c0 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,6 @@ # are made availible by the nix shell defined in shell.nix. # In most cases you should execute Make after entering nix-shell. -SHELL := /usr/bin/env bash - .PHONY: hoogle build test watch ghci readme_contents \ format lint refactor requires_nix_shell @@ -25,8 +23,8 @@ usage: @echo " format_check -- Check source code formatting without making changes" @echo " cabalfmt -- Apply cabal formatting with cabal-fmt" @echo " cabalfmt_check -- Check cabal files for formatting errors without making changes" - @echo " nixfmt -- Apply nix formatting with nixfmt" - @echo " nixfmt_check -- Check nix files for format errors" + @echo " nixpkgsfmt -- Apply nix formatting with nixpkgs-fmt" + @echo " nixpkgsfmt_check -- Check nix files for format errors" @echo " lint -- Check the sources with hlint" @echo " refactor_cautious -- Automatically apply hlint refactors, with prompt" @echo " refactor -- Automatically apply hlint refactors, without prompt" @@ -71,7 +69,7 @@ format: requires_nix_shell format_check: requires_nix_shell fourmolu --mode check --check-idempotence $(FORMAT_EXTENSIONS) $(FORMAT_SOURCES) -CABAL_SOURCES := $(shell git ls-tree -r HEAD --full-tree --name-only | grep -E '.*\.cabal' ) +CABAL_SOURCES := $(shell fd -ecabal) cabalfmt: requires_nix_shell cabal-fmt --inplace $(CABAL_SOURCES) @@ -80,13 +78,13 @@ cabalfmt_check: requires_nix_shell cabal-fmt --check $(CABAL_SOURCES) # Nix files to format -NIX_SOURCES := $(shell git ls-tree -r HEAD --full-tree --name-only | grep -E '.*\.nix' ) +NIX_SOURCES := $(shell fd -enix) -nixfmt: requires_nix_shell - nixfmt $(NIX_SOURCES) +nixpkgsfmt: requires_nix_shell + nixpkgs-fmt $(NIX_SOURCES) -nixfmt_check: requires_nix_shell - nixfmt --check $(NIX_SOURCES) +nixpkgsfmt_check: requires_nix_shell + nixpkgs-fmt --check $(NIX_SOURCES) # Check with hlint, currently I couldn't get --refactor to work lint: requires_nix_shell diff --git a/examples/Spec/Int.hs b/examples/Spec/Int.hs index e3d6c54..cd8c11a 100644 --- a/examples/Spec/Int.hs +++ b/examples/Spec/Int.hs @@ -56,12 +56,12 @@ instance HasParameterisedGenerator IntProp Int where else if IsMaxBound `elem` s then pure maxBound - else int (linear 11 (maxBound -1)) + else int (linear 11 (maxBound - 1)) if IsNegative `elem` s then if IsMinBound `elem` s then pure minBound - else pure (- i) + else pure (-i) else pure i intGenTests :: TestTree diff --git a/examples/Spec/IntPermutationGen.hs b/examples/Spec/IntPermutationGen.hs index 6565bee..9d1580c 100644 --- a/examples/Spec/IntPermutationGen.hs +++ b/examples/Spec/IntPermutationGen.hs @@ -67,7 +67,7 @@ instance HasPermutationGenerator IntProp Int where { name = "MakeLarge" , match = Not $ Var IsLarge , contract = clear >> addAll [IsLarge, IsPositive] - , morphism = \_ -> int (linear 11 (maxBound -1)) + , morphism = \_ -> int (linear 11 (maxBound - 1)) } , Morphism { name = "MakeSmall" @@ -83,7 +83,7 @@ instance HasPermutationGenerator IntProp Int where [ has IsNegative >> remove IsNegative >> add IsPositive , has IsPositive >> remove IsPositive >> add IsNegative ] - , morphism = \i -> pure (- i) + , morphism = \i -> pure (-i) } ] diff --git a/examples/Spec/Plutarch/CostModel.hs b/examples/Spec/Plutarch/CostModel.hs index 5c06fdd..4c09953 100644 --- a/examples/Spec/Plutarch/CostModel.hs +++ b/examples/Spec/Plutarch/CostModel.hs @@ -24,7 +24,7 @@ numCostModels = 10 peano :: Integer -> Term s PInteger peano 0 = 0 -peano i = papp (plam (+ 1)) (peano (i -1)) +peano i = papp (plam (+ 1)) (peano (i - 1)) addCost :: Integer -> Script addCost i = compile $ peano i diff --git a/examples/Spec/Plutarch/MagicNumber.hs b/examples/Spec/Plutarch/MagicNumber.hs index 376840f..02d5c54 100644 --- a/examples/Spec/Plutarch/MagicNumber.hs +++ b/examples/Spec/Plutarch/MagicNumber.hs @@ -28,7 +28,7 @@ numMagicNumbers = 4 -- accepts a range of numbers determined by a Magic Number magicNumber :: Integer -> Script -magicNumber i = compile $ plam $ \ii -> pif ((pfromData ii #<= (fromInteger i :: Term s PInteger)) #&& ((fromInteger (- i) :: Term s PInteger) #<= pfromData ii)) (pcon PUnit) perror +magicNumber i = compile $ plam $ \ii -> pif ((pfromData ii #<= (fromInteger i :: Term s PInteger)) #&& ((fromInteger (-i) :: Term s PInteger) #<= pfromData ii)) (pcon PUnit) perror data MagicNumberProp = HalfWidth Integer deriving stock (Eq, Ord, Show) diff --git a/flake.nix b/flake.nix index db88fb2..13efedd 100644 --- a/flake.nix +++ b/flake.nix @@ -21,12 +21,14 @@ nixpkgsFor = system: import nixpkgs { inherit system; overlays = [ haskell-nix.overlay ]; inherit (haskell-nix) config; }; + fourmoluFor = system: (nixpkgsFor system).haskell-nix.tool "ghc8107" "fourmolu" { }; + projectFor = system: let deferPluginErrors = true; pkgs = nixpkgsFor system; - fakeSrc = pkgs.runCommand "real-source" {} '' + fakeSrc = pkgs.runCommand "real-source" { } '' cp -rT ${self} $out chmod u+w $out/cabal.project cat $out/cabal-haskell.nix.project >> $out/cabal.project @@ -55,7 +57,7 @@ # We use the ones from Nixpkgs, since they are cached reliably. # Eventually we will probably want to build these with haskell.nix. - nativeBuildInputs = [ pkgs.cabal-install pkgs.hlint pkgs.haskellPackages.fourmolu ]; + nativeBuildInputs = [ pkgs.cabal-install pkgs.hlint (fourmoluFor system) ]; additional = ps: [ ps.plutarch @@ -63,43 +65,58 @@ ]; }; sha256map = { - "https://github.com/Plutonomicon/plutarch"."f8b7eb06184112ae2bebdec5a8156010141a05d5" - = "sha256-y9lalsaTFVEPCoE/P8d4bduJJLSBvVpo/791mI9kcO4="; - "https://github.com/mlabs-haskell/apropos"."3734bb3baa297ed990725a5ef14efcbb6a1c1c23" - = "sha256-C2gQrd5hFvQ+BsjAJs6V0iP9PRzd9dZMKtpk7kOjhwc="; - "https://github.com/input-output-hk/plutus.git"."6d8d25d1e84b2a4278da1036aab23da4161b8df8" - = "o8m86TkI1dTo74YbE9CPPNrBfSDSrf//DMq+v2+woEY="; - "https://github.com/Quid2/flat.git"."ee59880f47ab835dbd73bea0847dab7869fc20d8" - = "lRFND+ZnZvAph6ZYkr9wl9VAx41pb3uSFP8Wc7idP9M="; - "https://github.com/input-output-hk/cardano-crypto.git"."07397f0e50da97eaa0575d93bee7ac4b2b2576ec" - = "oxIOVlgm07FAEmgGRF1C2me9TXqVxQulEOcJ22zpTRs="; - "https://github.com/input-output-hk/cardano-base"."78b3928391b558fb1750228f63301ec371f13528" - = "pBUTTcenaSLMovHKGsaddJ7Jh3okRTrtu5W7Rdu6RM4="; - "https://github.com/input-output-hk/cardano-prelude"."fd773f7a58412131512b9f694ab95653ac430852" - = "BtbT5UxOAADvQD4qTPNrGfnjQNgbYNO4EAJwH2ZsTQo="; - "https://github.com/input-output-hk/Win32-network"."3825d3abf75f83f406c1f7161883c438dac7277d" - = "Hesb5GXSx0IwKSIi42ofisVELcQNX6lwHcoZcbaDiqc="; + "https://github.com/Plutonomicon/plutarch"."f8b7eb06184112ae2bebdec5a8156010141a05d5" = "sha256-y9lalsaTFVEPCoE/P8d4bduJJLSBvVpo/791mI9kcO4="; + "https://github.com/mlabs-haskell/apropos"."3734bb3baa297ed990725a5ef14efcbb6a1c1c23" = "sha256-C2gQrd5hFvQ+BsjAJs6V0iP9PRzd9dZMKtpk7kOjhwc="; + "https://github.com/input-output-hk/plutus.git"."6d8d25d1e84b2a4278da1036aab23da4161b8df8" = "o8m86TkI1dTo74YbE9CPPNrBfSDSrf//DMq+v2+woEY="; + "https://github.com/Quid2/flat.git"."ee59880f47ab835dbd73bea0847dab7869fc20d8" = "lRFND+ZnZvAph6ZYkr9wl9VAx41pb3uSFP8Wc7idP9M="; + "https://github.com/input-output-hk/cardano-crypto.git"."07397f0e50da97eaa0575d93bee7ac4b2b2576ec" = "oxIOVlgm07FAEmgGRF1C2me9TXqVxQulEOcJ22zpTRs="; + "https://github.com/input-output-hk/cardano-base"."78b3928391b558fb1750228f63301ec371f13528" = "pBUTTcenaSLMovHKGsaddJ7Jh3okRTrtu5W7Rdu6RM4="; + "https://github.com/input-output-hk/cardano-prelude"."fd773f7a58412131512b9f694ab95653ac430852" = "BtbT5UxOAADvQD4qTPNrGfnjQNgbYNO4EAJwH2ZsTQo="; + "https://github.com/input-output-hk/Win32-network"."3825d3abf75f83f406c1f7161883c438dac7277d" = "Hesb5GXSx0IwKSIi42ofisVELcQNX6lwHcoZcbaDiqc="; }; }; + formatCheckFor = system: + let + pkgs = nixpkgsFor system; + in + pkgs.runCommand "format-check" + { + nativeBuildInputs = [ + pkgs.coreutils + pkgs.git + pkgs.fd + pkgs.haskellPackages.cabal-fmt + pkgs.nixpkgs-fmt + (fourmoluFor system) + pkgs.hlint + ]; + } '' + export LC_CTYPE=C.UTF-8 + export LC_ALL=C.UTF-8 + export LANG=C.UTF-8 + cd ${self} + IN_NIX_SHELL=true make format_check cabalfmt_check nixpkgsfmt_check lint + mkdir $out + ''; in { - ciNix = flake-compat-ci.lib.recurseIntoFlakeWith { - flake = self; - systems = [ "x86_64-linux" ]; - }; - project = perSystem projectFor; - flake = perSystem (system: (projectFor system).flake {}); + flake = perSystem (system: (projectFor system).flake { }); # this could be done automatically, but would reduce readability packages = perSystem (system: self.flake.${system}.packages); - checks = perSystem (system: self.flake.${system}.checks); + checks = perSystem (system: self.flake.${system}.checks // { + formatCheck = formatCheckFor system; + }); check = perSystem (system: - (nixpkgsFor system).runCommand "combined-test" { - nativeBuildInputs = builtins.attrValues self.checks.${system}; - } "touch $out" + (nixpkgsFor system).runCommand "combined-test" + { + nativeBuildInputs = builtins.attrValues self.checks.${system}; + } "touch $out" ); apps = perSystem (system: self.flake.${system}.apps); devShell = perSystem (system: self.flake.${system}.devShell); + + herculesCI.ciSystems = [ "x86_64-linux" ]; }; } From 81ddfbaff6cacf37f1dd4117e177c4f1b666a85d Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Wed, 6 Apr 2022 12:32:39 +0100 Subject: [PATCH 10/22] Added first round of docstrings --- src/Apropos/Gen/Address.hs | 8 +++++ src/Apropos/Gen/Api.hs | 10 ++++++ src/Apropos/Gen/Contexts.hs | 13 ++++++- src/Apropos/Gen/Credential.hs | 9 +++++ src/Apropos/Gen/Crypto.hs | 8 +++++ src/Apropos/Gen/DCert.hs | 8 +++++ src/Apropos/Gen/Extra.hs | 67 ++++++++++++++++++++++++++++------- 7 files changed, 110 insertions(+), 13 deletions(-) diff --git a/src/Apropos/Gen/Address.hs b/src/Apropos/Gen/Address.hs index ccbb509..268ef95 100644 --- a/src/Apropos/Gen/Address.hs +++ b/src/Apropos/Gen/Address.hs @@ -1,3 +1,10 @@ +{- | +Module: Apropos.Gen.Address +Description: Address generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Address` types. +-} module Apropos.Gen.Address (address) where import Apropos.Gen (Gen) @@ -7,6 +14,7 @@ import Plutus.V1.Ledger.Address ( Address (Address), ) +-- | Generator for Plutus `Address` types. address :: Gen Address address = do cred <- credential diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs index eaf5acb..478b8ec 100644 --- a/src/Apropos/Gen/Api.hs +++ b/src/Apropos/Gen/Api.hs @@ -1,3 +1,10 @@ +{- | +Module: Apropos.Gen.Api +Description: Misc. Plutus generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Api` types. +-} module Apropos.Gen.Api ( builtinByteString, builtinData, @@ -11,16 +18,19 @@ import PlutusTx.Builtins.Internal ( BuiltinData (BuiltinData), ) +-- | `Gen` for Plutus `BuiltinByteString`. builtinByteString :: Gen BuiltinByteString builtinByteString = do bs <- sha256 return $ BuiltinByteString bs +-- | `Gen` for Plutus `BuiltinData`. builtinData :: Gen BuiltinData builtinData = do d <- data' return $ BuiltinData d +-- | TODO: Finish. data' :: Gen Data data' = do undefined diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index e521087..199620e 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -1,5 +1,10 @@ -{-# OPTIONS_GHC -Wwarn #-} +{- | +Module: Apropos.Gen.Context +Description: Plutus context generators. +Maintainer: jack@mlabs.city +`Gen`s for `Plutus.V1.Ledger.Context` types. +-} module Apropos.Gen.Contexts (scriptContext) where import Apropos.Gen (Gen, element, linear, list) @@ -34,12 +39,14 @@ import Plutus.V1.Ledger.Contexts ( TxOutRef (TxOutRef), ) +-- | `Gen` for Plutus `ScriptContext`s. scriptContext :: Gen ScriptContext scriptContext = do i <- txInfo p <- scriptPurpose return $ ScriptContext i p +-- | `Gen` for Plutus `TxInfo`s. txInfo :: Gen TxInfo txInfo = do ins <- list (linear 1 5) txInInfo @@ -69,12 +76,14 @@ txInfo = do , txInfoId = id' } +-- | `Gen` for Plutus `TxInInfo`s. txInInfo :: Gen TxInInfo txInInfo = do oRef <- txOutRef o <- txOut return $ TxInInfo oRef o +-- | `Gen` for Plutus `TxOut`s. txOut :: Gen TxOut txOut = do a <- address @@ -82,6 +91,7 @@ txOut = do h <- Gen.maybe datumHash return $ TxOut a v h +-- | `Gen` for Plutus `ScriptPurpose`s. scriptPurpose :: Gen ScriptPurpose scriptPurpose = do c <- currencySymbol @@ -95,6 +105,7 @@ scriptPurpose = do , Certifying d ] +-- | `Gen` for Plutus `TxOutRef`s. txOutRef :: Gen TxOutRef txOutRef = do id' <- txId diff --git a/src/Apropos/Gen/Credential.hs b/src/Apropos/Gen/Credential.hs index d4e85d7..7c9f95e 100644 --- a/src/Apropos/Gen/Credential.hs +++ b/src/Apropos/Gen/Credential.hs @@ -1,3 +1,10 @@ +{- | +Module: Apropos.Gen.Credential +Description: Plutus credential generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Address` types. +-} module Apropos.Gen.Credential (credential, stakingCredential) where import Apropos.Gen (Gen, element, linear) @@ -9,12 +16,14 @@ import Plutus.V1.Ledger.Credential ( StakingCredential (StakingHash, StakingPtr), ) +-- | `Gen` for Plutus `Credential`s. credential :: Gen Credential credential = do pkh <- pubKeyHash vh <- validatorHash element [PubKeyCredential pkh, ScriptCredential vh] +-- | `Gen` for Plutus `StakingCredential`s. stakingCredential :: Gen StakingCredential stakingCredential = do cred <- credential diff --git a/src/Apropos/Gen/Crypto.hs b/src/Apropos/Gen/Crypto.hs index 832a010..ecd8a40 100644 --- a/src/Apropos/Gen/Crypto.hs +++ b/src/Apropos/Gen/Crypto.hs @@ -1,9 +1,17 @@ +{- | +Module: Apropos.Gen.Crypto +Description: Plutus crypto type generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Crypto` types. +-} module Apropos.Gen.Crypto (pubKeyHash) where import Apropos.Gen (Gen) import Apropos.Gen.Api (builtinByteString) import Plutus.V1.Ledger.Crypto (PubKeyHash (PubKeyHash)) +-- | `Gen` for Plutus `PubKeyHash`s. pubKeyHash :: Gen PubKeyHash pubKeyHash = do bs <- builtinByteString diff --git a/src/Apropos/Gen/DCert.hs b/src/Apropos/Gen/DCert.hs index 762700f..183521c 100644 --- a/src/Apropos/Gen/DCert.hs +++ b/src/Apropos/Gen/DCert.hs @@ -1,3 +1,10 @@ +{- | +Module: Apropos.Gen.DCert +Description: Plutus dcert generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.DCert` types. +-} module Apropos.Gen.DCert (dCert) where import Apropos.Gen (Gen, element, linear) @@ -16,6 +23,7 @@ import Plutus.V1.Ledger.DCert ( ), ) +-- | `Gen` for Plutus `DCert`s. dCert :: Gen DCert dCert = do sc <- stakingCredential diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs index b6d2210..6497538 100644 --- a/src/Apropos/Gen/Extra.hs +++ b/src/Apropos/Gen/Extra.hs @@ -1,5 +1,10 @@ -{-# OPTIONS_GHC -Wwarn #-} +{- | +Module: Apropos.Gen.Extra +Description: Generator helper functions. +Maintainer: jack@mlabs.city +Helper functions for `Apropos.Gen`. +-} module Apropos.Gen.Extra ( sha256, map, @@ -16,16 +21,37 @@ import Data.Text.Encoding (encodeUtf8) import PlutusTx.AssocMap (Map, fromList) import Prelude hiding (map, maybe) -pair :: Gen a -> Gen b -> Gen (a, b) +-- | Creates a generator for a 2-tuple. +pair :: + -- | Generator for the type of the first element. + Gen a -> + -- | Generator for the type of the second element. + Gen b -> + -- | Generator for a pair of a value of the first type and a + -- value of the second type. + Gen (a, b) pair genA genB = do a <- genA b <- genB return (a, b) -integer :: Range -> Gen Integer +-- | Function producing a generator for an `Integer`. +integer :: + -- | Provides min and max bound for the integer to be + -- generated. + Range -> + -- | The `Integer` generator. + Gen Integer integer r = toInteger <$> int r -maybe :: Show a => Gen a -> Gen (Maybe a) +-- | Function producing a generator for a `Maybe` type. +maybe :: + Show a => + -- | A generator for the type to be wrapped by the `Maybe` + -- monad. + Gen a -> + -- | A generator for the desired `Maybe` type. + Gen (Maybe a) maybe genA = do x <- genA element [Just x, Nothing] @@ -48,31 +74,48 @@ map r genK genV = do let mapList = zip ks vs return $ fromList mapList +-- | Generator for an upper case alphabetic character. upperChar :: Gen Char upperChar = element ['A' .. 'Z'] +-- | Generator for a lower case alphaberic character. lowerChar :: Gen Char lowerChar = element ['a' .. 'z'] +-- | Generator for a numeric character. numChar :: Gen Char numChar = element ['0' .. '9'] +-- | Generator for a hexademical character. hexChar :: Gen Char hexChar = element $ ['0' .. '9'] ++ ['a' .. 'f'] -alphaString :: Range -> Gen String +-- | Returns a generator for a mixed case alphabetic string. +alphaString :: + -- | `Range` for the length of the string. + Range -> + Gen String alphaString r = list r gen where gen = choice [upperChar, lowerChar] -alphaNumericString :: Range -> Gen String -alphaNumericString r = list r gen +-- | Returns a generator for a mixed case alphanumeric string. +alphanumericString :: + -- | `Range` for the length of the string. + Range -> + Gen String +alphanumericString r = list r gen where gen = choice [upperChar, lowerChar, numChar] -numericString :: Range -> Gen String +-- | Returns a generator for a numeric string. +numericString :: + -- | `Range` for the length of the string. + Range -> + Gen String numericString r = list r numChar +-- | Return a generator for a hexadecimal string. hexString :: Range -> Gen String hexString r = list r hexChar @@ -82,8 +125,8 @@ genText f r = pack <$> f r alphaText :: Range -> Gen Text alphaText = genText alphaString -alphaNumericText :: Range -> Gen Text -alphaNumericText = genText alphaNumericString +alphanumericText :: Range -> Gen Text +alphanumericText = genText alphanumericString numericText :: Range -> Gen Text numericText = genText alphaString @@ -97,8 +140,8 @@ genBS f r = encodeUtf8 <$> f r alphaBS :: Range -> Gen ByteString alphaBS = genBS alphaText -alphaNumericBS :: Range -> Gen ByteString -alphaNumericBS = genBS alphaNumericText +alphanumericBS :: Range -> Gen ByteString +alphanumericBS = genBS alphanumericText numericBS :: Range -> Gen ByteString numericBS = genBS numericText From 86e32ed0dfc76f2aa5ede3f346f52eba9cc224e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Wed, 6 Apr 2022 15:08:31 +0300 Subject: [PATCH 11/22] use inputs from devShell for check --- flake.nix | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/flake.nix b/flake.nix index 13efedd..3aa676c 100644 --- a/flake.nix +++ b/flake.nix @@ -57,7 +57,7 @@ # We use the ones from Nixpkgs, since they are cached reliably. # Eventually we will probably want to build these with haskell.nix. - nativeBuildInputs = [ pkgs.cabal-install pkgs.hlint (fourmoluFor system) ]; + nativeBuildInputs = with pkgs; [ cabal-install hlint (fourmoluFor system) fd haskellPackages.cabal-fmt nixpkgs-fmt coreutils ]; additional = ps: [ ps.plutarch @@ -81,19 +81,8 @@ in pkgs.runCommand "format-check" { - nativeBuildInputs = [ - pkgs.coreutils - pkgs.git - pkgs.fd - pkgs.haskellPackages.cabal-fmt - pkgs.nixpkgs-fmt - (fourmoluFor system) - pkgs.hlint - ]; + nativeBuildInputs = [ self.devShell.${system}.nativeBuildInputs ]; } '' - export LC_CTYPE=C.UTF-8 - export LC_ALL=C.UTF-8 - export LANG=C.UTF-8 cd ${self} IN_NIX_SHELL=true make format_check cabalfmt_check nixpkgsfmt_check lint mkdir $out From eca0e88b811c27c4b7b9d9fc65ab82fc2fa08005 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Wed, 6 Apr 2022 14:27:54 +0100 Subject: [PATCH 12/22] clear unused top binds warning for gen extra --- src/Apropos/Gen/Credential.hs | 2 +- src/Apropos/Gen/Extra.hs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Apropos/Gen/Credential.hs b/src/Apropos/Gen/Credential.hs index 7c9f95e..3a49f0f 100644 --- a/src/Apropos/Gen/Credential.hs +++ b/src/Apropos/Gen/Credential.hs @@ -3,7 +3,7 @@ Module: Apropos.Gen.Credential Description: Plutus credential generators. Maintainer: jack@mlabs.city -`Gen`s for `Plutus.V1.Ledger.Address` types. +`Gen`s for `Plutus.V1.Ledger.Credential` types. -} module Apropos.Gen.Credential (credential, stakingCredential) where diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs index 6497538..8e62f45 100644 --- a/src/Apropos/Gen/Extra.hs +++ b/src/Apropos/Gen/Extra.hs @@ -11,6 +11,16 @@ module Apropos.Gen.Extra ( pair, integer, maybe, + upperChar, + lowerChar, + numChar, + numericString, + alphaText, + alphanumericText, + numericText, + alphaBS, + alphanumericBS, + numericBS, ) where import Apropos.Gen (Gen, choice, element, int, list) From 589b1f0b03aa9293447bf55004bb7b03bc6554d2 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Wed, 6 Apr 2022 15:26:40 +0100 Subject: [PATCH 13/22] finished documenting gen extra --- src/Apropos/Gen/Extra.hs | 70 +++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs index 8e62f45..5e76cc7 100644 --- a/src/Apropos/Gen/Extra.hs +++ b/src/Apropos/Gen/Extra.hs @@ -126,38 +126,86 @@ numericString :: numericString r = list r numChar -- | Return a generator for a hexadecimal string. -hexString :: Range -> Gen String +hexString :: + -- | `Range` for the length of the string. + Range -> + Gen String hexString r = list r hexChar -genText :: (Range -> Gen String) -> (Range -> Gen Text) +{- | Auxiliary function for creating text generation functions + from string generation functions. +-} +genText :: + -- | Existing string generation function e.g. `alphaString`. + (Range -> Gen String) -> + -- | Function taking a `Range` and producing a `Text` generator. + (Range -> Gen Text) genText f r = pack <$> f r -alphaText :: Range -> Gen Text +-- | Return a generator for an alphabetic `Text` string. +alphaText :: + -- | Length of desired string. + Range -> + Gen Text alphaText = genText alphaString -alphanumericText :: Range -> Gen Text +-- | Return a generator for an alphanumeric `Text` string. +alphanumericText :: + -- | Length of desired string. + Range -> + Gen Text alphanumericText = genText alphanumericString -numericText :: Range -> Gen Text +-- | Return a generator for a numeric `Text` string. +numericText :: + -- | Length of desired string. + Range -> + Gen Text numericText = genText alphaString -hexText :: Range -> Gen Text +-- | Return a generator for a hexadecimal `Text` string. +hexText :: + -- | Length of desired string. + Range -> + Gen Text hexText = genText hexString -genBS :: (Range -> Gen Text) -> (Range -> Gen ByteString) +-- | Auxiliary function for creating `ByteString` generators. +genBS :: + -- | Preexisting `Text` generation function e.g. `alphaText`. + (Range -> Gen Text) -> + -- | Desired `ByteString` generation function. + (Range -> Gen ByteString) genBS f r = encodeUtf8 <$> f r -alphaBS :: Range -> Gen ByteString +-- | Return a generator for an alphabetic `ByteString`. +alphaBS :: + -- | Length of desired string. + Range -> + Gen ByteString alphaBS = genBS alphaText -alphanumericBS :: Range -> Gen ByteString +-- | Return a generator for an alphanumeric `ByteString`. +alphanumericBS :: + -- | Length of desired string. + Range -> + Gen ByteString alphanumericBS = genBS alphanumericText -numericBS :: Range -> Gen ByteString +-- | Return a generator for an numeric `ByteString`. +numericBS :: + -- | Length of desired string. + Range -> + Gen ByteString numericBS = genBS numericText -hexBS :: Range -> Gen ByteString +-- | Return a generator for an hexadecimal `ByteString`. +hexBS :: + -- | Length of desired string. + Range -> + Gen ByteString hexBS = genBS hexText +-- | Return a generator for SHA256 hash. sha256 :: Gen ByteString sha256 = hexBS $ singleton 64 From 567af2838047ce906605c994e9c2b886916b88d9 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Wed, 6 Apr 2022 15:30:59 +0100 Subject: [PATCH 14/22] Set up local hoogle --- .gitignore | 4 ++++ Makefile | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 80a8c65..c1c8fb0 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,7 @@ cabal.project.local* # ctags tags TAGS + +# Local hoogle +.hoogle +.haddock diff --git a/Makefile b/Makefile index 2b1aedc..ae54836 100644 --- a/Makefile +++ b/Makefile @@ -32,9 +32,13 @@ usage: @echo " refactor -- Automatically apply hlint refactors, without prompt" @echo " readme_contents -- Add table of contents to README" @echo " update_plutus -- Update plutus version with niv" + @echo " haddock -- Generate haddock docs. hoogle: requires_nix_shell - hoogle server --local + pkill hoogle || true + hoogle generate --local=.haddock --database=.hoogle/local.hoo + hoogle server --local -p 8080 >> /dev/null & + hoogle server --local --database=.hoogle/local.hoo -p 8081 >> /dev/null & STACK_EXE_PATH = $(shell stack $(STACK_FLAGS) path --local-install-root)/bin @@ -123,3 +127,6 @@ update_plutus: @echo "Make sure to update the plutus rev in cabal.project with:" @echo " commit: $(PLUTUS_REV)" @echo "This may require further resolution of dependency versions." + +haddock: requires_nix_shell + cabal haddock --haddock-html --haddock-hoogle --builddir=.haddock From 8d515ad212ce51dc8279ff49f1ef2b2fa1923455 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Thu, 7 Apr 2022 08:20:00 +0100 Subject: [PATCH 15/22] Added interval to apropos-tx.cabal --- apropos-tx.cabal | 3 ++- src/Apropos/Gen/Interval.hs | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/apropos-tx.cabal b/apropos-tx.cabal index a471a24..f63869a 100644 --- a/apropos-tx.cabal +++ b/apropos-tx.cabal @@ -40,6 +40,7 @@ common lang build-depends: , apropos , base >=4.14 + , bytestring , containers , free , hedgehog @@ -55,7 +56,6 @@ common lang , safe , template-haskell , text - , bytestring , transformers ghc-options: @@ -73,6 +73,7 @@ library Apropos.Gen.Crypto Apropos.Gen.DCert Apropos.Gen.Extra + Apropos.Gen.Interval Apropos.Gen.Scripts Apropos.Gen.Time Apropos.Gen.TxId diff --git a/src/Apropos/Gen/Interval.hs b/src/Apropos/Gen/Interval.hs index e69de29..67c004c 100644 --- a/src/Apropos/Gen/Interval.hs +++ b/src/Apropos/Gen/Interval.hs @@ -0,0 +1,7 @@ +module Apropos.Gen.Interval (interval) where + +import Apropos.Gen (Gen) +import Plutus.V1.Ledger.Interval (Interval) + +interval :: Gen a -> Gen (Interval a) +interval = undefined From fd7f0e49283750a88dfba21ac1c094e44a56c031 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Thu, 7 Apr 2022 09:23:18 +0100 Subject: [PATCH 16/22] Added docstrings to script --- src/Apropos/Gen/Scripts.hs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs index 28a0ead..6d674eb 100644 --- a/src/Apropos/Gen/Scripts.hs +++ b/src/Apropos/Gen/Scripts.hs @@ -1,3 +1,10 @@ +{- | +Module: Apropos.Gen.Scripts +Description: Script generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Scripts` types. +-} module Apropos.Gen.Scripts ( validatorHash, datum, @@ -12,16 +19,19 @@ import Plutus.V1.Ledger.Scripts ( ValidatorHash (ValidatorHash), ) +-- | Generator for Plutus `Datum` type. datum :: Gen Datum datum = do bid <- builtinData return $ Datum bid +-- | Generator for Plutus `DatumHash` type. datumHash :: Gen DatumHash datumHash = do bs <- builtinByteString return $ DatumHash bs +-- | Generator for Plutus `validatorHash` type. validatorHash :: Gen ValidatorHash validatorHash = do bs <- builtinByteString From 16e02741d84a4f782d591e3b26c097fae5cd8436 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:30:27 +0100 Subject: [PATCH 17/22] finished adding docstrings --- src/Apropos/Gen/Api.hs | 23 ++++++++++--- src/Apropos/Gen/Contexts.hs | 2 +- src/Apropos/Gen/Interval.hs | 67 ++++++++++++++++++++++++++++++++++--- src/Apropos/Gen/Time.hs | 32 ++++++++++++++++-- src/Apropos/Gen/TxId.hs | 8 +++++ src/Apropos/Gen/Value.hs | 10 ++++++ 6 files changed, 128 insertions(+), 14 deletions(-) diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs index 478b8ec..02da068 100644 --- a/src/Apropos/Gen/Api.hs +++ b/src/Apropos/Gen/Api.hs @@ -10,9 +10,9 @@ module Apropos.Gen.Api ( builtinData, ) where -import Apropos.Gen (Gen) -import Apropos.Gen.Extra (sha256) -import PlutusTx (Data) +import Apropos.Gen (Gen, element, int, linear, list, singleton) +import Apropos.Gen.Extra (integer, pair, sha256) +import PlutusTx (Data (B, Constr, I, List, Map)) import PlutusTx.Builtins.Internal ( BuiltinByteString (BuiltinByteString), BuiltinData (BuiltinData), @@ -30,7 +30,20 @@ builtinData = do d <- data' return $ BuiltinData d --- | TODO: Finish. +-- | `Gen` for Plutus `Data`. data' :: Gen Data data' = do - undefined + let mapGen :: Gen [(Data, Data)] + mapGen = list (linear 0 20) $ pair data' data' + n <- integer $ linear minBound maxBound + b <- sha256 + m <- mapGen + i <- int $ linear 0 50 + l <- list (singleton i) data' + element + [ Constr (toInteger i) l + , Map m + , List l + , I n + , B b + ] diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index 199620e..3b7c961 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -58,7 +58,7 @@ txInfo = do list (linear 0 10) $ pair stakingCredential $ integer (linear 0 50) - range <- posixTimeRange + range <- posixTimeRange (linear 0 2000) (linear 6000 maxBound) sigs <- list (linear 0 10) pubKeyHash data' <- list (linear 1 5) $ pair datumHash datum id' <- txId diff --git a/src/Apropos/Gen/Interval.hs b/src/Apropos/Gen/Interval.hs index 67c004c..c08e148 100644 --- a/src/Apropos/Gen/Interval.hs +++ b/src/Apropos/Gen/Interval.hs @@ -1,7 +1,64 @@ -module Apropos.Gen.Interval (interval) where +{- | +Module: Apropos.Gen.Interval +Description: Plutus interval generators. +Maintainer: jack@mlabs.city -import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Interval (Interval) +`Gen`s for `Plutus.V1.Ledger.Interval` types. +-} +module Apropos.Gen.Interval (interval, extendedF, extendedI) where -interval :: Gen a -> Gen (Interval a) -interval = undefined +import Apropos.Gen (Gen, element) +import Plutus.V1.Ledger.Interval ( + Extended (Finite, NegInf, PosInf), + Interval (Interval), + LowerBound (LowerBound), + UpperBound (UpperBound), + ) + +{- | Given two generators for a type @a@, will return a generator + for type `Interval` @a@, where the lower bound has been + generated by the first generator and the upper bound + by the second. +-} +interval :: + Show a => + Gen a -> -- Generator for the lower bound. + Gen a -> -- Generator for the upper bound. + Gen (Interval a) -- Generator for the interval. +interval g0 g1 = do + l <- lowerBound g0 + u <- upperBound g1 + return $ Interval l u + +{- | Given a generator for a type @a@, will return a generator + for type `LowerBound` @a@. +-} +lowerBound :: Show a => Gen a -> Gen (LowerBound a) +lowerBound g = do + e <- extendedF g + c <- element [True, False] + return $ LowerBound e c + +{- | Given a generator for a type @a@, will return a generator + for type `UpperBound` @a@. +-} +upperBound :: Show a => Gen a -> Gen (UpperBound a) +upperBound g = do + e <- extendedF g + c <- element [True, False] + return $ UpperBound e c + +-- | Generator for `Extended` @a@ that must be finite. +extendedF :: Show a => Gen a -> Gen (Extended a) +extendedF = extended False + +-- | Generator for `Extended` @a@ that can be infinite. +extendedI :: Show a => Gen a -> Gen (Extended a) +extendedI = extended True + +extended :: Show a => Bool -> Gen a -> Gen (Extended a) +extended allowInfinite g = do + x <- g + if allowInfinite + then element [NegInf, Finite x, PosInf] + else return $ Finite x diff --git a/src/Apropos/Gen/Time.hs b/src/Apropos/Gen/Time.hs index cb53747..8f2ca1f 100644 --- a/src/Apropos/Gen/Time.hs +++ b/src/Apropos/Gen/Time.hs @@ -1,7 +1,33 @@ +{- | +Module: Apropos.Gen.Time +Description: Plutus POSIX time generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Time` types. +-} module Apropos.Gen.Time (posixTimeRange) where import Apropos.Gen (Gen) -import Plutus.V1.Ledger.Time (POSIXTimeRange) +import Apropos.Gen.Extra (integer) +import Apropos.Gen.Interval (interval) +import Apropos.Gen.Range (Range) +import Plutus.V1.Ledger.Time (POSIXTime (POSIXTime), POSIXTimeRange) + +-- | Function for producing `POSIXTimeRange` generators. +posixTimeRange :: + -- | `Range` you wish for the lower bound to take. + Range -> + -- | `Range` you wish for the upper bound to take. + Range -> + -- | A generator for a `POSIXTimeRange`. + Gen POSIXTimeRange +posixTimeRange rL rU = do + let lGen :: Gen POSIXTime = posixTime rL + uGen :: Gen POSIXTime = posixTime rU + interval lGen uGen -posixTimeRange :: Gen POSIXTimeRange -posixTimeRange = undefined +-- | Function for producing `POSIXTime` generators. +posixTime :: Range -> Gen POSIXTime +posixTime r = do + n <- integer r + return $ POSIXTime n diff --git a/src/Apropos/Gen/TxId.hs b/src/Apropos/Gen/TxId.hs index facb64b..1bfa522 100644 --- a/src/Apropos/Gen/TxId.hs +++ b/src/Apropos/Gen/TxId.hs @@ -1,9 +1,17 @@ +{- | +Module: Apropos.Gen.TxId +Description: Plutus credential generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.TxId` types. +-} module Apropos.Gen.TxId (txId) where import Apropos.Gen (Gen) import Apropos.Gen.Api (builtinByteString) import Plutus.V1.Ledger.TxId (TxId (TxId)) +-- | Generator for Plutus `TxId` type. txId :: Gen TxId txId = do bs <- builtinByteString diff --git a/src/Apropos/Gen/Value.hs b/src/Apropos/Gen/Value.hs index b5ef584..65f916a 100644 --- a/src/Apropos/Gen/Value.hs +++ b/src/Apropos/Gen/Value.hs @@ -1,3 +1,10 @@ +{- | +Module: Apropos.Gen.Value +Description: Plutus value generators. +Maintainer: jack@mlabs.city + +`Gen`s for `Plutus.V1.Ledger.Value` types. +-} module Apropos.Gen.Value ( currencySymbol, value, @@ -13,17 +20,20 @@ import Plutus.V1.Ledger.Value ( Value (Value), ) +-- | Generator for a Plutus `Value`. value :: Gen Value value = do let tnvMap = Gen.map (linear 1 5) tokenName $ integer (linear 1 maxBound) v <- Gen.map (linear 1 5) currencySymbol tnvMap return $ Value v +-- | Generator for a Plutus `CurrencySymbol`. currencySymbol :: Gen CurrencySymbol currencySymbol = do bs <- builtinByteString return $ CurrencySymbol bs +-- | Generator for a Plutus `TokenName`. tokenName :: Gen TokenName tokenName = do bs <- builtinByteString From d435de5b8669e5f777016922d0fb1d92b324ff53 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:37:39 +0100 Subject: [PATCH 18/22] Exported more from modules --- src/Apropos/Gen/Bytes.hs | 0 src/Apropos/Gen/Contexts.hs | 9 ++++++++- src/Apropos/Gen/Interval.hs | 9 ++++++--- src/Apropos/Gen/Time.hs | 2 +- src/Apropos/Gen/TxId.hs | 2 +- src/Apropos/Gen/Value.hs | 9 +++++---- 6 files changed, 21 insertions(+), 10 deletions(-) delete mode 100644 src/Apropos/Gen/Bytes.hs diff --git a/src/Apropos/Gen/Bytes.hs b/src/Apropos/Gen/Bytes.hs deleted file mode 100644 index e69de29..0000000 diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index 3b7c961..2dad285 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -5,7 +5,14 @@ Maintainer: jack@mlabs.city `Gen`s for `Plutus.V1.Ledger.Context` types. -} -module Apropos.Gen.Contexts (scriptContext) where +module Apropos.Gen.Contexts ( + scriptContext, + txInfo, + txInInfo, + txOut, + scriptPurpose, + txOutRef, +) where import Apropos.Gen (Gen, element, linear, list) import Apropos.Gen.Address (address) diff --git a/src/Apropos/Gen/Interval.hs b/src/Apropos/Gen/Interval.hs index c08e148..994e6fc 100644 --- a/src/Apropos/Gen/Interval.hs +++ b/src/Apropos/Gen/Interval.hs @@ -22,9 +22,12 @@ import Plutus.V1.Ledger.Interval ( -} interval :: Show a => - Gen a -> -- Generator for the lower bound. - Gen a -> -- Generator for the upper bound. - Gen (Interval a) -- Generator for the interval. + -- | Generator for the lower bound. + Gen a -> + -- | Generator for the upper bound. + Gen a -> + -- | Generator for the interval. + Gen (Interval a) interval g0 g1 = do l <- lowerBound g0 u <- upperBound g1 diff --git a/src/Apropos/Gen/Time.hs b/src/Apropos/Gen/Time.hs index 8f2ca1f..fce0caa 100644 --- a/src/Apropos/Gen/Time.hs +++ b/src/Apropos/Gen/Time.hs @@ -5,7 +5,7 @@ Maintainer: jack@mlabs.city `Gen`s for `Plutus.V1.Ledger.Time` types. -} -module Apropos.Gen.Time (posixTimeRange) where +module Apropos.Gen.Time (posixTimeRange, posixTime) where import Apropos.Gen (Gen) import Apropos.Gen.Extra (integer) diff --git a/src/Apropos/Gen/TxId.hs b/src/Apropos/Gen/TxId.hs index 1bfa522..4468285 100644 --- a/src/Apropos/Gen/TxId.hs +++ b/src/Apropos/Gen/TxId.hs @@ -1,6 +1,6 @@ {- | Module: Apropos.Gen.TxId -Description: Plutus credential generators. +Description: Plutus `TxId` generators. Maintainer: jack@mlabs.city `Gen`s for `Plutus.V1.Ledger.TxId` types. diff --git a/src/Apropos/Gen/Value.hs b/src/Apropos/Gen/Value.hs index 65f916a..15cca7a 100644 --- a/src/Apropos/Gen/Value.hs +++ b/src/Apropos/Gen/Value.hs @@ -6,8 +6,9 @@ Maintainer: jack@mlabs.city `Gen`s for `Plutus.V1.Ledger.Value` types. -} module Apropos.Gen.Value ( - currencySymbol, value, + currencySymbol, + tokenName, ) where import Apropos.Gen (Gen, linear) @@ -20,20 +21,20 @@ import Plutus.V1.Ledger.Value ( Value (Value), ) --- | Generator for a Plutus `Value`. +-- | Generator for a Plutus `Value`. value :: Gen Value value = do let tnvMap = Gen.map (linear 1 5) tokenName $ integer (linear 1 maxBound) v <- Gen.map (linear 1 5) currencySymbol tnvMap return $ Value v --- | Generator for a Plutus `CurrencySymbol`. +-- | Generator for a Plutus `CurrencySymbol`. currencySymbol :: Gen CurrencySymbol currencySymbol = do bs <- builtinByteString return $ CurrencySymbol bs --- | Generator for a Plutus `TokenName`. +-- | Generator for a Plutus `TokenName`. tokenName :: Gen TokenName tokenName = do bs <- builtinByteString From 5257325e3dd2603d09c0ebe468eec676eeb233b8 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Fri, 8 Apr 2022 13:42:18 +0100 Subject: [PATCH 19/22] Updated txid ref for compatibility --- src/Apropos/Gen/TxId.hs | 2 +- src/Apropos/Gen/Value.hs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Apropos/Gen/TxId.hs b/src/Apropos/Gen/TxId.hs index 4468285..9a0b012 100644 --- a/src/Apropos/Gen/TxId.hs +++ b/src/Apropos/Gen/TxId.hs @@ -9,7 +9,7 @@ module Apropos.Gen.TxId (txId) where import Apropos.Gen (Gen) import Apropos.Gen.Api (builtinByteString) -import Plutus.V1.Ledger.TxId (TxId (TxId)) +import Plutus.V1.Ledger.Api (TxId (TxId)) -- | Generator for Plutus `TxId` type. txId :: Gen TxId diff --git a/src/Apropos/Gen/Value.hs b/src/Apropos/Gen/Value.hs index 15cca7a..054437f 100644 --- a/src/Apropos/Gen/Value.hs +++ b/src/Apropos/Gen/Value.hs @@ -6,7 +6,9 @@ Maintainer: jack@mlabs.city `Gen`s for `Plutus.V1.Ledger.Value` types. -} module Apropos.Gen.Value ( + ada, value, + valueWithAda, currencySymbol, tokenName, ) where @@ -15,11 +17,24 @@ import Apropos.Gen (Gen, linear) import Apropos.Gen.Api (builtinByteString) import Apropos.Gen.Extra (integer) import Apropos.Gen.Extra qualified as Gen (map) +import Apropos.Gen.Range (Range) import Plutus.V1.Ledger.Value ( CurrencySymbol (CurrencySymbol), TokenName (TokenName), Value (Value), + unionWith, ) +import PlutusTx.AssocMap (Map) +import PlutusTx.AssocMap qualified as AssocMap (singleton) + +-- | Generator for some amount of Ada +ada :: Range -> Gen Value +ada r = do + n <- integer r + let m :: Map CurrencySymbol (Map TokenName Integer) = + AssocMap.singleton (CurrencySymbol "") $ + AssocMap.singleton (TokenName "") n + return $ Value m -- | Generator for a Plutus `Value`. value :: Gen Value @@ -28,6 +43,15 @@ value = do v <- Gen.map (linear 1 5) currencySymbol tnvMap return $ Value v +{- | Generator for a Plutus `Value`. Guaranteed to contain some + Ada. +-} +valueWithAda :: Gen Value +valueWithAda = do + v <- value + a <- ada $ linear 1 1000 + return $ unionWith (+) v a + -- | Generator for a Plutus `CurrencySymbol`. currencySymbol :: Gen CurrencySymbol currencySymbol = do From dd292b49a29f8a259bdc3e35cf4ab1dbbc73582f Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Mon, 11 Apr 2022 11:44:40 +0100 Subject: [PATCH 20/22] Removed data' function as was v. slow --- src/Apropos/Gen/Api.hs | 6 ++++-- src/Apropos/Gen/Contexts.hs | 11 ++++++++--- src/Apropos/Gen/Scripts.hs | 18 +++++++++++------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs index 02da068..e6f413a 100644 --- a/src/Apropos/Gen/Api.hs +++ b/src/Apropos/Gen/Api.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wwarn #-} + {- | Module: Apropos.Gen.Api Description: Misc. Plutus generators. @@ -7,7 +9,7 @@ Maintainer: jack@mlabs.city -} module Apropos.Gen.Api ( builtinByteString, - builtinData, + -- builtinData, ) where import Apropos.Gen (Gen, element, int, linear, list, singleton) @@ -30,7 +32,7 @@ builtinData = do d <- data' return $ BuiltinData d --- | `Gen` for Plutus `Data`. +-- | TODO: Re-do; too slow. `Gen` for Plutus `Data`. data' :: Gen Data data' = do let mapGen :: Gen [(Data, Data)] diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index 2dad285..014c49a 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wwarn #-} + {- | Module: Apropos.Gen.Context Description: Plutus context generators. @@ -21,7 +23,9 @@ import Apropos.Gen.Crypto (pubKeyHash) import Apropos.Gen.DCert (dCert) import Apropos.Gen.Extra (integer, pair) import Apropos.Gen.Extra qualified as Gen (maybe) -import Apropos.Gen.Scripts (datum, datumHash) + +-- import Apropos.Gen.Scripts (datum, datumHash) +import Apropos.Gen.Scripts (datumHash) import Apropos.Gen.Time (posixTimeRange) import Apropos.Gen.TxId (txId) import Apropos.Gen.Value (currencySymbol, value) @@ -67,7 +71,7 @@ txInfo = do integer (linear 0 50) range <- posixTimeRange (linear 0 2000) (linear 6000 maxBound) sigs <- list (linear 0 10) pubKeyHash - data' <- list (linear 1 5) $ pair datumHash datum + -- data' <- list (linear 1 5) $ pair datumHash datum id' <- txId return $ TxInfo @@ -79,7 +83,8 @@ txInfo = do , txInfoWdrl = wdrl , txInfoValidRange = range , txInfoSignatories = sigs - , txInfoData = data' + , -- , txInfoData = data' + txInfoData = [] , txInfoId = id' } diff --git a/src/Apropos/Gen/Scripts.hs b/src/Apropos/Gen/Scripts.hs index 6d674eb..6bd05a7 100644 --- a/src/Apropos/Gen/Scripts.hs +++ b/src/Apropos/Gen/Scripts.hs @@ -1,3 +1,5 @@ +{-# OPTIONS_GHC -Wwarn #-} + {- | Module: Apropos.Gen.Scripts Description: Script generators. @@ -7,23 +9,25 @@ Maintainer: jack@mlabs.city -} module Apropos.Gen.Scripts ( validatorHash, - datum, + -- datum, datumHash, ) where import Apropos.Gen (Gen) -import Apropos.Gen.Api (builtinByteString, builtinData) + +-- import Apropos.Gen.Api (builtinByteString, builtinData) +import Apropos.Gen.Api (builtinByteString) import Plutus.V1.Ledger.Scripts ( Datum (Datum), DatumHash (DatumHash), ValidatorHash (ValidatorHash), ) --- | Generator for Plutus `Datum` type. -datum :: Gen Datum -datum = do - bid <- builtinData - return $ Datum bid +-- -- | Generator for Plutus `Datum` type. +-- datum :: Gen Datum +-- datum = do +-- bid <- builtinData +-- return $ Datum bid -- | Generator for Plutus `DatumHash` type. datumHash :: Gen DatumHash From 58148d56b45a730eedf8c496c9b6a64bb803bb0c Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Thu, 28 Apr 2022 13:42:16 +0100 Subject: [PATCH 21/22] Replaced inefficient calls to element with choice --- src/Apropos/Gen/Api.hs | 26 ++++++++++++-------------- src/Apropos/Gen/Contexts.hs | 16 ++++++---------- src/Apropos/Gen/Credential.hs | 23 ++++++++++++----------- src/Apropos/Gen/DCert.hs | 23 ++++++++++------------- src/Apropos/Gen/Extra.hs | 5 +---- 5 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/Apropos/Gen/Api.hs b/src/Apropos/Gen/Api.hs index e6f413a..4da4a42 100644 --- a/src/Apropos/Gen/Api.hs +++ b/src/Apropos/Gen/Api.hs @@ -12,7 +12,7 @@ module Apropos.Gen.Api ( -- builtinData, ) where -import Apropos.Gen (Gen, element, int, linear, list, singleton) +import Apropos.Gen (Gen, choice, int, linear, list, singleton) import Apropos.Gen.Extra (integer, pair, sha256) import PlutusTx (Data (B, Constr, I, List, Map)) import PlutusTx.Builtins.Internal ( @@ -35,17 +35,15 @@ builtinData = do -- | TODO: Re-do; too slow. `Gen` for Plutus `Data`. data' :: Gen Data data' = do - let mapGen :: Gen [(Data, Data)] - mapGen = list (linear 0 20) $ pair data' data' - n <- integer $ linear minBound maxBound - b <- sha256 - m <- mapGen - i <- int $ linear 0 50 - l <- list (singleton i) data' - element - [ Constr (toInteger i) l - , Map m - , List l - , I n - , B b + let constrGen = do + len <- int (linear 0 50) + l <- list (singleton len) data' + let i = toInteger len + return $ Constr i l + choice + [ constrGen + , Map <$> list (linear 0 20) (pair data' data') + , List <$> list (linear 0 10) data' + , I <$> integer (linear minBound maxBound) + , B <$> sha256 ] diff --git a/src/Apropos/Gen/Contexts.hs b/src/Apropos/Gen/Contexts.hs index 014c49a..da534bd 100644 --- a/src/Apropos/Gen/Contexts.hs +++ b/src/Apropos/Gen/Contexts.hs @@ -16,7 +16,7 @@ module Apropos.Gen.Contexts ( txOutRef, ) where -import Apropos.Gen (Gen, element, linear, list) +import Apropos.Gen (Gen, choice, element, linear, list) import Apropos.Gen.Address (address) import Apropos.Gen.Credential (stakingCredential) import Apropos.Gen.Crypto (pubKeyHash) @@ -106,15 +106,11 @@ txOut = do -- | `Gen` for Plutus `ScriptPurpose`s. scriptPurpose :: Gen ScriptPurpose scriptPurpose = do - c <- currencySymbol - t <- txOutRef - s <- stakingCredential - d <- dCert - element - [ Minting c - , Spending t - , Rewarding s - , Certifying d + choice + [ Minting <$> currencySymbol + , Spending <$> txOutRef + , Rewarding <$> stakingCredential + , Certifying <$> dCert ] -- | `Gen` for Plutus `TxOutRef`s. diff --git a/src/Apropos/Gen/Credential.hs b/src/Apropos/Gen/Credential.hs index 3a49f0f..a3f5e83 100644 --- a/src/Apropos/Gen/Credential.hs +++ b/src/Apropos/Gen/Credential.hs @@ -7,7 +7,7 @@ Maintainer: jack@mlabs.city -} module Apropos.Gen.Credential (credential, stakingCredential) where -import Apropos.Gen (Gen, element, linear) +import Apropos.Gen (Gen, choice, linear) import Apropos.Gen.Crypto (pubKeyHash) import Apropos.Gen.Extra (integer) import Apropos.Gen.Scripts (validatorHash) @@ -18,16 +18,17 @@ import Plutus.V1.Ledger.Credential ( -- | `Gen` for Plutus `Credential`s. credential :: Gen Credential -credential = do - pkh <- pubKeyHash - vh <- validatorHash - element [PubKeyCredential pkh, ScriptCredential vh] +credential = + choice + [ PubKeyCredential <$> pubKeyHash + , ScriptCredential <$> validatorHash + ] -- | `Gen` for Plutus `StakingCredential`s. stakingCredential :: Gen StakingCredential -stakingCredential = do - cred <- credential - p0 <- integer $ linear 0 1000 - p1 <- integer $ linear 0 1000 - p2 <- integer $ linear 0 1000 - element [StakingHash cred, StakingPtr p0 p1 p2] +stakingCredential = + let p = integer $ linear 0 1000 + in choice + [ StakingHash <$> credential + , StakingPtr <$> p <*> p <*> p + ] diff --git a/src/Apropos/Gen/DCert.hs b/src/Apropos/Gen/DCert.hs index 183521c..327ceaf 100644 --- a/src/Apropos/Gen/DCert.hs +++ b/src/Apropos/Gen/DCert.hs @@ -7,7 +7,7 @@ Maintainer: jack@mlabs.city -} module Apropos.Gen.DCert (dCert) where -import Apropos.Gen (Gen, element, linear) +import Apropos.Gen (Gen, choice, linear) import Apropos.Gen.Credential (stakingCredential) import Apropos.Gen.Crypto (pubKeyHash) import Apropos.Gen.Extra (integer) @@ -26,16 +26,13 @@ import Plutus.V1.Ledger.DCert ( -- | `Gen` for Plutus `DCert`s. dCert :: Gen DCert dCert = do - sc <- stakingCredential - pkh <- pubKeyHash - pkh' <- pubKeyHash - n <- integer (linear 0 300) - element - [ DCertDelegRegKey sc - , DCertDelegDeRegKey sc - , DCertDelegDelegate sc pkh - , DCertPoolRegister pkh pkh' - , DCertPoolRetire pkh n - , DCertGenesis - , DCertMir + choice + [ DCertDelegRegKey <$> stakingCredential + , DCertDelegDeRegKey <$> stakingCredential + , DCertDelegDelegate <$> stakingCredential <*> pubKeyHash + , DCertPoolRegister <$> pubKeyHash <*> pubKeyHash + , DCertPoolRetire <$> pubKeyHash <*> integer (linear 0 300) + , return DCertGenesis + , return DCertMir ] + diff --git a/src/Apropos/Gen/Extra.hs b/src/Apropos/Gen/Extra.hs index 5e76cc7..11c758a 100644 --- a/src/Apropos/Gen/Extra.hs +++ b/src/Apropos/Gen/Extra.hs @@ -56,15 +56,12 @@ integer r = toInteger <$> int r -- | Function producing a generator for a `Maybe` type. maybe :: - Show a => -- | A generator for the type to be wrapped by the `Maybe` -- monad. Gen a -> -- | A generator for the desired `Maybe` type. Gen (Maybe a) -maybe genA = do - x <- genA - element [Just x, Nothing] +maybe genA = choice [Just <$> genA, return Nothing] {- | Given a generator for a key-type k and a value-type v returns a generator for a `Map` k v. From 23fb121905502a9b2903cf07a1413fb4731660e9 Mon Sep 17 00:00:00 2001 From: Jack Hodgkinson <30505104+jhodgdev@users.noreply.github.com> Date: Thu, 28 Apr 2022 15:07:46 +0100 Subject: [PATCH 22/22] Formatting --- src/Apropos/Gen/DCert.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Apropos/Gen/DCert.hs b/src/Apropos/Gen/DCert.hs index 327ceaf..9025466 100644 --- a/src/Apropos/Gen/DCert.hs +++ b/src/Apropos/Gen/DCert.hs @@ -28,11 +28,10 @@ dCert :: Gen DCert dCert = do choice [ DCertDelegRegKey <$> stakingCredential - , DCertDelegDeRegKey <$> stakingCredential + , DCertDelegDeRegKey <$> stakingCredential , DCertDelegDelegate <$> stakingCredential <*> pubKeyHash , DCertPoolRegister <$> pubKeyHash <*> pubKeyHash , DCertPoolRetire <$> pubKeyHash <*> integer (linear 0 300) , return DCertGenesis , return DCertMir ] -