Skip to content

Commit

Permalink
fix build issues due to new type system
Browse files Browse the repository at this point in the history
  • Loading branch information
colll78 committed Jan 31, 2025
1 parent e227907 commit cfd6eb2
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 130 deletions.
4 changes: 2 additions & 2 deletions DesignDocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Modules like `Plutarch.Core.List` provide functions such as `pelemAtFast'` to ac
### 4. `Plutarch.Core.Eval` (`Eval.hs`)
- **Purpose**: Evaluating and serializing Plutarch terms.

### 5. `Plutarch.Core.FieldBinds` (`FieldBinds.hs`)
- **Purpose**: abstractions for handling field bindings in records.
### 5. `Plutarch.Core.Integrity` (`Integrity.hs`)
- **Purpose**: Utilities for verifying the correctness of the BuiltinData encoding of various types.

### 6. `Plutarch.Core.List` (`List.hs`)
- **Purpose**: Specialized high efficiency list operations.
Expand Down
2 changes: 2 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cradle:
cabal:
- path: "./"
component: "plutarch-onchain-lib:plutarch-onchain-lib"
- path: "./"
component: "plutarch-onchain-lib:plutarch-onchain-lib-tests"
plugins:
eval:
globalOn: true
Expand Down
73 changes: 0 additions & 73 deletions src/plutarch-onchain-lib/lib/Plutarch/Core/FieldBinds.hs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use camelCase" #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-|
Module : Plutarch.MerkleTree.PatriciaForestry
Description : Merkle trees in Plutarch
Expand Down Expand Up @@ -45,18 +45,19 @@ module Plutarch.MerkleTree.PatriciaForestry(
) where

import Data.ByteString (ByteString)
import Generics.SOP qualified as SOP
import GHC.Generics (Generic)
import Plutarch.Builtin.Crypto (pblake2b_256)
import Plutarch.Core.Internal.Builtins (pconsBS')
import Plutarch.DataRepr
import Plutarch.Internal.Lift
import Plutarch.MerkleTree.Helpers (pcombine, pnibble, pnibbles, psuffix)
import Plutarch.MerkleTree.Merkling (pmerkle_16, pnull_hash, psparse_merkle_16)
import Plutarch.Prelude
import Plutarch.Repr.Data
import PlutusTx qualified
import PlutusTx.Builtins as Builtins
import PlutusTx.Builtins.Internal (BuiltinByteString (BuiltinByteString))
import GHC.Generics (Generic)
import Generics.SOP qualified as SOP
import Plutarch.Repr.Data
import PlutusTx.Builtins.Internal qualified as BI

-- Constants

Expand Down Expand Up @@ -113,7 +114,40 @@ data Neighbor = Neighbor
, root :: BuiltinByteString
}
deriving stock (Show, Eq, Generic)
PlutusTx.unstableMakeIsData ''Neighbor

instance PlutusTx.ToData Neighbor where
{-# INLINABLE toBuiltinData #-}
toBuiltinData :: Neighbor -> PlutusTx.BuiltinData
toBuiltinData (Neighbor{nibble, prefix, root}) = PlutusTx.toBuiltinData [PlutusTx.toBuiltinData nibble, PlutusTx.toBuiltinData prefix, PlutusTx.toBuiltinData root]

instance PlutusTx.FromData Neighbor where
{-# INLINABLE fromBuiltinData #-}
fromBuiltinData :: PlutusTx.BuiltinData -> Maybe Neighbor
fromBuiltinData neighbor =
let toNeighbor :: BI.BuiltinData -> Maybe Neighbor
toNeighbor neighborList' = do
let neighborList = BI.unsafeDataAsList neighborList'
nibble <- PlutusTx.fromBuiltinData $ BI.head neighborList
prefix <- PlutusTx.fromBuiltinData $ BI.head (BI.tail neighborList)
root <- PlutusTx.fromBuiltinData $ BI.head (BI.tail $ BI.tail neighborList)
return Neighbor{nibble, prefix, root}
in
BI.chooseData neighbor
Nothing
Nothing
(toNeighbor neighbor)
Nothing
Nothing

instance PlutusTx.UnsafeFromData Neighbor where
{-# INLINABLE unsafeFromBuiltinData #-}
unsafeFromBuiltinData :: PlutusTx.BuiltinData -> Neighbor
unsafeFromBuiltinData neighbor =
let bd = BI.unsafeDataAsList neighbor
nibble = PlutusTx.unsafeFromBuiltinData $ BI.head bd
prefix = PlutusTx.unsafeFromBuiltinData $ BI.head $ BI.tail bd
root = PlutusTx.unsafeFromBuiltinData $ BI.head $ BI.tail $ BI.tail bd
in Neighbor{nibble, prefix, root}

data ProofStep
= Branch
Expand All @@ -132,17 +166,17 @@ data ProofStep
deriving stock (Show, Eq, Generic)
PlutusTx.unstableMakeIsData ''ProofStep

data PProofStep (s :: S)
= PBranch
data PProofStep (s :: S)
= PBranch
{ pproofStep'skip :: Term s (PAsData PInteger)
, pproofStep'neighbors :: Term s (PAsData PByteString)
}
| PFork
| PFork
{ pproofStep'skip :: Term s (PAsData PInteger)
, pproofStep'neighbor :: Term s (PAsData PNeighbor)
}
| PLeaf
{ pproofStep'skip :: Term s (PAsData PInteger)
| PLeaf
{ pproofStep'skip :: Term s (PAsData PInteger)
, pproofStep'key :: Term s (PAsData PByteString)
, pproofStep'value :: Term s (PAsData PByteString)
}
Expand All @@ -155,25 +189,26 @@ deriving via
instance
PLiftable PProofStep

newtype PProof (s :: S) = PProof (Term s (PBuiltinList PProofStep))
newtype PProof (s :: S) = PProof (Term s (PBuiltinList (PAsData PProofStep)))
deriving stock (Generic)
deriving anyclass (PlutusType, PIsData)

instance DerivePlutusType PProof where type DPTStrat _ = PlutusTypeNewtype

data PNeighbor (s :: S) = PNeighbor
data PNeighbor (s :: S) = PNeighbor
{ pneighbor'nibble :: Term s (PAsData PInteger)
, pneighbor'prefix :: Term s (PAsData PByteString)
, pneighbor'root :: Term s (PAsData PByteString)
}
deriving stock (Generic)
deriving anyclass (SOP.Generic, PIsData, PEq, PShow)
deriving (PlutusType) via (DeriveAsDataRec PNeighbor)
deriving anyclass (SOP.Generic, PIsData, PEq, PShow)
deriving (PlutusType) via (DeriveAsDataRec PNeighbor)

deriving via
DeriveDataPLiftable PNeighbor Neighbor
instance
PLiftable PNeighbor

-- deriving via
-- DeriveDataPLiftable PNeighbor Neighbor
-- instance
-- PLiftable PNeighbor

-- Test whether an element is present in the trie with a specific value. This
-- requires a Proof of inclusion for the element. The latter can be
Expand Down Expand Up @@ -217,27 +252,26 @@ pupdate = phoistAcyclic $ plam $ \self key_ proof oldValue newValue ->

pexcluding :: Term s (PByteString :--> PProof :--> PByteString)
pexcluding = phoistAcyclic $ plam $ \((pblake2b_256 #) -> path) proof ->
let go :: Term _ (PInteger :--> PBuiltinList PProofStep :--> PByteString)
let go :: Term _ (PInteger :--> PBuiltinList (PAsData PProofStep) :--> PByteString)
go = pfix #$ plam $ \self cursor steps ->
pmatch steps $ \case
PNil -> pnull_hash
PCons x xs ->
pmatch x $ \case
pmatch (pfromData x) $ \case
PBranch {pproofStep'skip, pproofStep'neighbors} ->
pletFields @'["skip", "neighbors"] fields $ \branchF ->
plet (cursor + 1 + branchF.skip) $ \nextCursor ->
plet (cursor + 1 + pfromData pproofStep'skip) $ \nextCursor ->
let root_ = (self # nextCursor # xs)
in pdo_branch # path # cursor # nextCursor # root_ # branchF.neighbors
in pdo_branch # path # cursor # nextCursor # root_ # pfromData pproofStep'neighbors
PFork {pproofStep'skip, pproofStep'neighbor} ->
pmatch xs $ \case
PNil ->
pmatch pproofStep'neighbor $ \(PNeighbor {pneighbor'nibble, pneighbor'prefix, pneighbor'root}) ->
pmatch (pfromData pproofStep'neighbor) $ \(PNeighbor {pneighbor'nibble, pneighbor'prefix, pneighbor'root}) ->
let prefix_ = pconsBS' # pfromData pneighbor'nibble # pfromData pneighbor'prefix
in pcombine # prefix_ # pfromData pneighbor'root
PCons _ _ ->
plet (cursor + 1 + forkF.skip) $ \nextCursor ->
plet (cursor + 1 + pfromData pproofStep'skip) $ \nextCursor ->
let root_ = (self # nextCursor # xs)
in pdo_fork # path # cursor # nextCursor # root_ # forkF.neighbor
in pdo_fork # path # cursor # nextCursor # root_ # pfromData pproofStep'neighbor
PLeaf {pproofStep'skip, pproofStep'key, pproofStep'value} ->
pmatch xs $ \case
PNil ->
Expand All @@ -263,18 +297,18 @@ pexcluding = phoistAcyclic $ plam $ \((pblake2b_256 #) -> path) proof ->
--
pincluding :: Term s (PByteString :--> PByteString :--> PProof :--> PByteString)
pincluding = phoistAcyclic $ plam $ \((pblake2b_256 #) -> path) ((pblake2b_256 #) -> value_) proof ->
let go :: Term _ (PInteger :--> PBuiltinList PProofStep :--> PByteString)
let go :: Term _ (PInteger :--> PBuiltinList (PAsData PProofStep) :--> PByteString)
go = pfix #$ plam $ \self cursor steps ->
pelimList (\proofStep ys ->
pmatch proofStep $ \case
pmatch (pfromData proofStep) $ \case
PBranch {pproofStep'skip, pproofStep'neighbors} ->
plet (cursor + 1 + pfromData pproofStep'skip) $ \nextCursor ->
let root_ = self # nextCursor # ys
in pdo_branch # path # cursor # nextCursor # root_ # pfromData pproofStep'neighbors
PFork {pproofStep'skip, pproofStep'neighbor} ->
plet (cursor + 1 + pfromData pproofStep'skip) $ \nextCursor ->
let root_ = self # nextCursor # ys
in pdo_fork # path # pfromData cursor # nextCursor # root_ # pproofStep'neighbor
in pdo_fork # path # cursor # nextCursor # root_ # pfromData pproofStep'neighbor
PLeaf {pproofStep'skip, pproofStep'key, pproofStep'value} ->
plet (cursor + 1 + pfromData pproofStep'skip) $ \nextCursor ->
plet pproofStep'key $ \key_ ->
Expand Down
10 changes: 6 additions & 4 deletions src/plutarch-onchain-lib/plutarch-onchain-lib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ common lang
library
import: lang
exposed-modules:
Plutarch.Core.Crypto
Plutarch.Core.Integrity
Plutarch.Core.Context
Plutarch.Core.Crypto
Plutarch.Core.Data
Plutarch.Core.Eval
Plutarch.Core.FieldBinds
Plutarch.Core.Integrity
Plutarch.Core.Internal.Builtins
Plutarch.Core.List
Plutarch.Core.PByteString
Expand All @@ -79,13 +78,13 @@ library
, base16-bytestring
, bytestring
, cardano-binary
, generics-sop
, plutarch
, plutarch-ledger-api
, plutus-core
, plutus-ledger-api
, plutus-tx
, prettyprinter
, generics-sop
, tasty-hunit
, text

Expand All @@ -96,6 +95,7 @@ test-suite plutarch-onchain-lib-tests
other-modules:
Plutarch.List.Test
Plutarch.MerkleTree.Test
TestUtils

hs-source-dirs: test
build-depends:
Expand All @@ -110,7 +110,9 @@ test-suite plutarch-onchain-lib-tests
, plutus-ledger-api
, quickcheck-instances
, tasty
, tasty-hunit
, tasty-quickcheck
, text

test-suite plutarch-onchain-lib-bench
import: lang
Expand Down
5 changes: 3 additions & 2 deletions src/plutarch-onchain-lib/test/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

module Main (main) where

import Plutarch.Core.Context (ptxInInfoResolved, ptxOutCredential)
import Plutarch.Core.List
import Plutarch.Core.Unroll
import Plutarch.Core.ValidationLogic
Expand Down Expand Up @@ -158,7 +159,7 @@ punrolledCountScriptInputs = punrollBound' 100 (const def) go () # 0
go self () = plam $ \n ->
pelimList
(\x xs' ->
let cred = pfield @"credential" # (pfield @"address" # (pfield @"resolved" # x))
let cred = ptxOutCredential $ ptxInInfoResolved x --pfield @"credential" # (pfield @"address" # (pfield @"resolved" # x))
count = pmatch cred $ \case
PScriptCredential _ -> (n + 1)
_ -> n
Expand All @@ -173,7 +174,7 @@ punrolledCountScriptInputsUnboundWhole = punrollUnboundWhole 20 go #$ 0
go self = plam $ \n ->
pelimList
(\x xs' ->
let cred = pfield @"credential" # (pfield @"address" # (pfield @"resolved" # x))
let cred = ptxOutCredential $ ptxInInfoResolved x
count = pmatch cred $ \case
PScriptCredential _ -> (n + 1)
_ -> n
Expand Down
Loading

0 comments on commit cfd6eb2

Please sign in to comment.