Skip to content

Commit

Permalink
Merge pull request #317 from Plutonomicon/staging
Browse files Browse the repository at this point in the history
Plutarch 1.2
  • Loading branch information
L-as authored Aug 1, 2022
2 parents e7907e3 + 45b7c77 commit 3140f47
Show file tree
Hide file tree
Showing 472 changed files with 14,196 additions and 6,159 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flake.lock linguist-generated=true
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/result*
/dist-newstyle
/dist-*
.direnv
bench.csv
110 changes: 108 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
# Revision history for plutarch

# Unreleased
# 1.2 (WIP changelog)

- Changed fields of `PTxInfo` whose only representation is data to not be wrapped by `PAsData`.

Module: `Plutarch.Api.V1.Contexts`; `Plutarch.Api.V2.Contexts`

- Added `plistData` builtin function wrapper.

Module: `Plutarch.Builtin`

- Added `PEq` superclass constraint to `POrd`

Included by [#326](https://github.com/Plutonomicon/plutarch/pull/326)

- Added module `Plutarch.Show` with the `PShow` type class, as well as functions `pshow` and `ptraceShowId`.

Started by [#352](https://github.com/Plutonomicon/plutarch/pull/352)

- Add `puncons` and `ptryUncons` functions for deconstructing list.

Started by: [#333](https://github.com/Plutonomicon/plutarch/pull/333)

- Add generic deriving for `PEq`

Started by [#335](https://github.com/Plutonomicon/plutarch/pull/335)
- `Plutarch.Prelude` and `Plutarch.List` now export pfind, pelemAt, preverse and pcheckSroted.

Started by: [#306](https://github.com/Plutonomicon/plutarch/pull/306)

- Added module `Plutarch.FFI` for interoperability with PlutusTx.

- Added `DerivePConstantViaBuiltin`, deprecating `DerivePConstantViaNewtype`.

- `TermCont`: Parametrize by result type; add `MonadFail` instance; etc.

Expand Down Expand Up @@ -41,7 +72,7 @@
- Added APIs for constructing, compiling, serialising & hashing Plutarch scripts.

Type synonyms for Plutarch-typed scripts `PValidator`,`PMintingPolicy` & `PStakeValidator`.

`mkValidator`, `mkStakeValidator` & `mkMintingPolicy` functions, for creating Plutus API compatible scripts.

`validatorHash`, `mintingPolicySymbol` & `stakeValidatorHash` to obtain script hashes.
Expand Down Expand Up @@ -71,6 +102,81 @@

Added by: [#235](https://github.com/Plutonomicon/plutarch/pull/270)

- Add `Plutarch.Test` for testing Plutarch code with goldens for UPLC printing and Plutus benchmarks.

- Add Conversion types `PTryFrom`, `PMaybeFrom` and `PFrom`

Module: `Plutarch.TryFrom`

Added by: [#326](https://github.com/Plutonomicon/plutarch/pull/326)

- `plutarch-extra`: Add a new directory scaffold "`plutarch-extra`" which will be home to everything too specific to not be in the
main Plutarch repo. Also refactored the test library.

Directory: `plutarch-extra`

Added by: [#329](https://github.com/Plutonomicon/plutarch/pull/329)

- `plutarch-extra` export merged Prelude

Module: `Plutarch.PPrelude`

Added by: [#356](https://github.com/Plutonomicon/plutarch/pull/356)

- Add `PConstant` instance for `Maybe`, with corresponding `PLift` instance for `PMaybeData`.

Added by: [#371](https://github.com/Plutonomicon/plutarch/pull/371)

- Add `POrd` and `PEq` derivation for data encoded types via `PIsDataReprInstances`.

Added by: [#371](https://github.com/Plutonomicon/plutarch/pull/371)

- Make `PRational` construction machinery fail when the denominator is 0.

Fixed by: [#299](https://github.com/Plutonomicon/plutarch/pull/299)

- Rename `PConstant` (the typeclass) to `PConstantDecl`. `PConstant` is now a type alias with extra constraints for better type checking.

Add `PLiftData` and `PConstantData` type aliases.

Added by: [#354](https://github.com/Plutonomicon/plutarch/pull/354)

- Remove `hrecField` export. Use `getField` instead.

Removed by: [#415](https://github.com/Plutonomicon/plutarch/pull/415)

- Rename the `"data"` field of `PTxInfo` to `"datums"`.

Renamed by: [#415](https://github.com/Plutonomicon/plutarch/pull/415)

- Add `Num` instance for `PPOSIXTime` and export its constructor.

Added by: [#415](https://github.com/Plutonomicon/plutarch/pull/415)

- `PlutusType` is now a superclass of `PIsDataRepr`, strengthening the existing `PMatch` superclass constraint.

Added by: [#415](https://github.com/Plutonomicon/plutarch/pull/415)

- Add `PlutusType` instance for `PDataSum`. `PDataSum` can now be hand-constructed.

Added by: [#345](https://github.com/Plutonomicon/plutarch/pull/345)

- Add `HRecOf`, `PMemberFields`, and `PMemberField` utility types.

Module: `Plutarch.DataRepr`.

Added by: [#466](https://github.com/Plutonomicon/plutarch/pull/466)

- Move `Plutarch.ListUtils` to `Plutarch.Extra.List`.

Added by: [#466](https://github.com/Plutonomicon/plutarch/pull/466)

- Add various `TermCont` utilities: `ptraceC`, `pletFieldsC`, `ptryFromC`, `pguardC`, and `pguardC'`.

Module: `Plutarch.Extra.TermCont`.

Added by: [#466](https://github.com/Plutonomicon/plutarch/pull/466)

# 1.1.0

- General repository changes.
Expand Down
92 changes: 61 additions & 31 deletions Plutarch.hs
Original file line number Diff line number Diff line change
@@ -1,40 +1,70 @@
module Plutarch (
(:-->),
ClosedTerm,
compile,
Dig,
hashTerm,
papp,
pdelay,
PDelayed,
perror,
pforce,
phoistAcyclic,
plam',
plet,
Term,
S,
PType,
PlutusType (..),
printTerm,
printScript,
(#$),
(#),
pinl,
PCon (..),
PMatch (..),
pto,
pfix,
POpaque (..),
popaque,
plam,
DerivePNewtype (DerivePNewtype),
(PI.:-->),
PI.ClosedTerm,
PI.compile,
PI.Dig,
PI.hashTerm,
PI.papp,
PI.pdelay,
PI.PDelayed,
PI.perror,
PI.pforce,
PI.phoistAcyclic,
PI.plet,
PI.pthrow,
PI.Term,
PI.S,
PI.PType,
PP.PlutusType,
PP.PInner,
PP.pcon,
PP.pmatch,
PP.PCon,
PP.PMatch,
PPR.prettyTerm,
PPR.prettyScript,
PO.printTerm,
PO.printScript,
(PL.#$),
(PL.#),
PL.pinl,
PO.pto,
PO.pfix,
PO.POpaque (PO.POpaque),
PO.popaque,
PL.plam,
PT.TermCont (TermCont),
PT.hashOpenTerm,
PT.runTermCont,
PT.unTermCont,
PI.Config (Config, tracingMode),
PI.TracingMode (NoTracing, DoTracing, DetTracing),
PI.pgetConfig,
PQ.PForall (PForall),
PQ.PSome (PSome),
PS.PScottEncoded (PScottEncoded),
PS.PlutusTypeScott,
PN.PlutusTypeNewtype,
PP.DerivePlutusType,
PP.DPTStrat,
PP.PCovariant,
PP.PCovariant',
PP.PContravariant,
PP.PContravariant',
PP.PVariant,
PP.PVariant',
) where

import Plutarch.Internal.Other
import qualified Plutarch.Internal as PI
import qualified Plutarch.Internal.Newtype as PN
import qualified Plutarch.Internal.Other as PO
import qualified Plutarch.Internal.PLam as PL
import qualified Plutarch.Internal.PlutusType as PP
import qualified Plutarch.Internal.Quantification as PQ
import qualified Plutarch.Internal.ScottEncoding as PS
import Plutarch.Num ()
import qualified Plutarch.Pretty as PPR
import qualified Plutarch.TermCont as PT

-- import orphan instances
import Prelude ()
44 changes: 44 additions & 0 deletions Plutarch/Api/Internal/Hashing.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Plutarch.Api.Internal.Hashing (
hashScriptWithPrefix,
hashData,
hashLedgerBytes,
) where

import Codec.Serialise (serialise)
import Crypto.Hash (hashWith)
import Crypto.Hash.Algorithms (
Blake2b_224 (Blake2b_224),
Blake2b_256 (Blake2b_256),
HashAlgorithm,
)
import Data.ByteArray (convert)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as Lazy

import qualified PlutusLedgerApi.V1 as Plutus
import qualified PlutusLedgerApi.V1.Scripts as Plutus
import qualified PlutusTx.Builtins as PlutusTx

_plutusHashWith :: HashAlgorithm alg => alg -> ByteString -> PlutusTx.BuiltinByteString
_plutusHashWith alg = PlutusTx.toBuiltin . convert @_ @ByteString . hashWith alg

hashBlake2b_224 :: ByteString -> PlutusTx.BuiltinByteString
hashBlake2b_224 = _plutusHashWith Blake2b_224

hashBlake2b_256 :: ByteString -> PlutusTx.BuiltinByteString
hashBlake2b_256 = _plutusHashWith Blake2b_256

-- | Hash a Script with the given version prefix
hashScriptWithPrefix :: ByteString -> Plutus.Script -> Plutus.ScriptHash
hashScriptWithPrefix prefix scr =
Plutus.ScriptHash
. hashBlake2b_224
$ prefix <> Lazy.toStrict (serialise scr)

-- | Hash Plutus 'Data'.
hashData :: Plutus.Data -> PlutusTx.BuiltinByteString
hashData = hashBlake2b_256 . Lazy.toStrict . serialise

-- | Hash 'LedgerBytes'.
hashLedgerBytes :: Plutus.LedgerBytes -> PlutusTx.BuiltinByteString
hashLedgerBytes = hashBlake2b_224 . Plutus.fromBuiltin . Plutus.getLedgerBytes
24 changes: 0 additions & 24 deletions Plutarch/Api/Internal/Scripts.hs

This file was deleted.

44 changes: 32 additions & 12 deletions Plutarch/Api/V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module Plutarch.Api.V1 (
mintingPolicySymbol,
stakeValidatorHash,
scriptHash,
datumHash,
redeemerHash,
dataHash,
mkValidator,
mkStakeValidator,
mkMintingPolicy,
Expand All @@ -28,11 +31,12 @@ module Plutarch.Api.V1 (
Value.PValue (PValue),
Value.PCurrencySymbol (PCurrencySymbol),
Value.PTokenName (PTokenName),
Value.KeyGuarantees (Unsorted, Sorted),
Value.AmountGuarantees (NoGuarantees, NonZero, Positive),

-- ** Crypto
Crypto.PPubKeyHash (PPubKeyHash),
Crypto.PPubKey (PPubKey),
Crypto.PSignature (PSignature),
Crypto.pubKeyHash,

-- ** DCert
DCert.PDCert (
Expand Down Expand Up @@ -95,14 +99,18 @@ import qualified Plutarch.Api.V1.Value as Value

import Data.Coerce (coerce)

import qualified Plutus.V1.Ledger.Api as Plutus
import qualified Plutus.V1.Ledger.Scripts as Plutus
-- note about V2: This should there are no changes in Scripts or V1 itself that affect this module
import qualified PlutusLedgerApi.V1 as Plutus
import qualified PlutusLedgerApi.V1.Scripts as Plutus

import Plutarch (ClosedTerm, POpaque, compile)
import Plutarch.Api.Internal.Scripts (hashScriptWithPrefix)
import Plutarch (Config, compile)
import Plutarch.Api.Internal.Hashing (hashData, hashScriptWithPrefix)
import Plutarch.Api.V1.Contexts (PScriptContext)
import Plutarch.Prelude

import qualified Data.Text as T
import GHC.Stack (HasCallStack)

-- On-chain Script Types

-- | a Validator Term
Expand All @@ -115,16 +123,16 @@ type PMintingPolicy = PData :--> PScriptContext :--> POpaque
type PStakeValidator = PData :--> PScriptContext :--> POpaque

-- | Compile a Validator
mkValidator :: ClosedTerm PValidator -> Plutus.Validator
mkValidator s = Plutus.Validator $ compile s
mkValidator :: HasCallStack => Config -> ClosedTerm PValidator -> Plutus.Validator
mkValidator config s = Plutus.Validator $ either (error . T.unpack) id $ compile config s

-- | Compile a MintingPolicy
mkMintingPolicy :: ClosedTerm PMintingPolicy -> Plutus.MintingPolicy
mkMintingPolicy s = Plutus.MintingPolicy $ compile s
mkMintingPolicy :: HasCallStack => Config -> ClosedTerm PMintingPolicy -> Plutus.MintingPolicy
mkMintingPolicy config s = Plutus.MintingPolicy $ either (error . T.unpack) id $ compile config s

-- | Compile a StakeValidator
mkStakeValidator :: ClosedTerm PStakeValidator -> Plutus.StakeValidator
mkStakeValidator s = Plutus.StakeValidator $ compile s
mkStakeValidator :: HasCallStack => Config -> ClosedTerm PStakeValidator -> Plutus.StakeValidator
mkStakeValidator config s = Plutus.StakeValidator $ either (error . T.unpack) id $ compile config s

-- | Hash a Script, with the correct prefix for Plutus V1
scriptHash :: Plutus.Script -> Plutus.ScriptHash
Expand All @@ -141,3 +149,15 @@ mintingPolicySymbol = coerce scriptHash
-- | Hash a StakeValidator, with the correct prefix for Plutus V1
stakeValidatorHash :: Plutus.StakeValidator -> Plutus.StakeValidatorHash
stakeValidatorHash = coerce scriptHash

-- | Hash a Datum.
datumHash :: Plutus.Datum -> Plutus.DatumHash
datumHash = coerce . dataHash

-- | Hash a Redeemer.
redeemerHash :: Plutus.Redeemer -> Plutus.RedeemerHash
redeemerHash = coerce . dataHash

-- | Hash the data encoded representation of given argument.
dataHash :: Plutus.ToData a => a -> Plutus.BuiltinByteString
dataHash = hashData . Plutus.toData
Loading

0 comments on commit 3140f47

Please sign in to comment.