Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Refactored the tx constraints library to prepare for PlutusV2 #625

Merged
merged 1 commit into from
Jul 28, 2022

Conversation

koslambrou
Copy link
Contributor

@koslambrou koslambrou commented Jul 25, 2022

  • Changed functions names in plutus-ledger-constraints to reflect the Plutus version

    • typedValidatorLookups -> plutusV1TypedValidatorLookups
    • mintingPolicy -> plutusV1MintingPolicy
    • otherScript -> plutusV1OtherScript
  • Removed reexport of Ledger.Constraints.OnChain.V1.checkScriptContext from module Ledger.Constraints.

  • Moved functions in Plutus.Contract.Typed.Tx and Plutus.Contract.Tx
    to plutus-ledger-constraints.

  • Changed 'Ledger.Tx.ChainIndexTxOut' from Either to Tuple for the datum
    and validator. This prevents from having to recompute the hash from
    the value when we have Right ... since the hashing function of
    PlutusV1 is not the same as the one in PlutusV2 (for scripts, not for
    datums, but for consistency we apply the same refactoring for datums).

  • Backported some refactoring changes from next-node such as the
    function 'Plutus.ChainIndex.Handlers.makeChainIndexTxOut' and the
    refactoring surrounding Ledger.Tx.ChainIndexTxOut.

Pre-submit checklist:

  • Branch
    • Tests are provided (if possible)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
    • Formatting, PNG optimization, etc. are updated
  • PR
    • Self-reviewed the diff
    • Useful pull request description
    • Reference the ADR in the PR and reference the PR in the ADR (if revelant)
    • Reviewer requested

@koslambrou koslambrou requested review from sjoerdvisscher, a user and andreabedini and removed request for sjoerdvisscher and a user July 25, 2022 12:29
@andreabedini
Copy link
Contributor

Damn, I should have published my refactoring branch. Changes to ChainIndexTxOut will be a pain to merge :)

@koslambrou
Copy link
Contributor Author

@andreabedini No problem. I can adapt my code considering your changes to ChainIndexTxOut. The only difference I added in ChainIndexTxOut in main was to use a Tuple instead of Either for the datum and validator.

@andreabedini
Copy link
Contributor

@andreabedini No problem. I can adapt my code considering your changes to ChainIndexTxOut. The only difference I added in ChainIndexTxOut in main was to use a Tuple instead of Either for the datum and validator.

I'll book some time to discuss both our chages ASAP. I'm still doing minor fixes all through the code base 🤯

@koslambrou koslambrou force-pushed the kll/make-constraints-plutusv1-aware branch from 52043f4 to 5f6b8a8 Compare July 25, 2022 18:11
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but I have a comment about the naming.

I dislike the usage of plutusV prefix as a namespace approach because there are modules for this task.

Actually, we already have started mixing them:

import Ledger.Constraints.OnChain.V1 as Constraints

...

Constraints.checkPlutusV1OwnOutputConstraint 

I think it's tedious, as a user I would need to remember when a function starts with plutusV1 or there is plutusV1 somewhere in the name. Also, we might be inconsistent in the future or just not careful as with otherPlutusV1Script and that might lead to many functions with different names.

I think that modules here is a better approach to deal with namespacing and scopes.

  1. We still can use one version in one module:
import Ledger.Constraints.OnChain.V1 as Constraints

...

Constraints.checkOwnOutputConstraint 

and it will be easier to switch to a different version by changing the import.

  1. We can mix the versions in one module without polluting the scope:
import Ledger.Constraints.OnChain.V1 as Constraints.V1
import Ledger.Constraints.OnChain.V2 as Constraints.V2

...

Constraints.V1.checkOwnOutputConstraint 

The names of functions will be the same, we don't need to change them, only the name of the module.

Constraints.otherScript usScript <>
Constraints.mintingPolicy (liquidityPolicy us) <>
lookups = Constraints.plutusV1TypedValidatorLookups usInst <>
Constraints.otherPlutusV1Script usScript <>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should be plutusV1OtherScript? To be consistent in naming.

@koslambrou
Copy link
Contributor Author

@ak3n Very good point. One of the reason I decided to go with plutusV... was to keep global modules which reexport everything like Ledger.Constraints. How would this module reexport a function which has the same name, but is in two different modules? Unless we should split it with Ledger.Constraints.V1 and Ledger.Constraints.V2?

@ghost
Copy link

ghost commented Jul 26, 2022

@koslambrou Yeah, should we split like in #628?

@koslambrou koslambrou force-pushed the kll/make-constraints-plutusv1-aware branch 2 times, most recently from b28ed75 to 0d0327d Compare July 26, 2022 18:37
@koslambrou
Copy link
Contributor Author

@ak3n For now, I went back to the old naming forcheckScriptContext, checkOwnInputConstraint and checkOwnOutputConstraint, and removed the reexport in Ledger.Constraints. Therefore, something wanting to use this on-chain bit should import Ledger.Constraints.OnChain.V1.checkScriptContext or Ledger.Constraints.OnChain.V1.checkScriptContext in the future.

@koslambrou koslambrou force-pushed the kll/make-constraints-plutusv1-aware branch from 0d0327d to 7cb2c16 Compare July 26, 2022 19:40
@koslambrou koslambrou marked this pull request as ready for review July 27, 2022 01:04
Copy link
Contributor

@sjoerdvisscher sjoerdvisscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point I guess it will be useful to have a TypedValidator that can contain a script in any plutus version, and then maybe this can use that? We'll learn by experience when it's useful to split code into one for each plutus version and when a sum type will do.

@koslambrou
Copy link
Contributor Author

@sjoerdvisscher Yep, I agree

* Changed functions names in plutus-ledger-constraints to reflect the Plutus version
  * `typedValidatorLookups` -> `plutusV1TypedValidatorLookups`
  * `mintingPolicy` -> `plutusV1MintingPolicy`
  * `otherScript` -> `plutusV1OtherScript`

* Removed reexport of `Ledger.Constraints.OnChain.V1.checkScriptContext` from module `Ledger.Constraints`

* Moved functions in `Plutus.Contract.Typed.Tx` and `Plutus.Contract.Tx`
  to plutus-ledger-constraints.

* Changed 'Ledger.Tx.ChainIndexTxOut' from Either to Tuple for the datum
  and validator. This prevents from having to recompute the hash from
  the value when we have `Right ...` since the hashing function of
  PlutusV1 is not the same as the one in PlutusV2 (for scripts, not for
  datums, but for consistency we apply the same refactoring for datums).

* Backported some refactoring changes from `next-node` such as the
  function 'Plutus.ChainIndex.Handlers.makeChainIndexTxOut' and the
  refactoring surrounding `Ledger.Tx.ChainIndexTxOut`.
@koslambrou koslambrou force-pushed the kll/make-constraints-plutusv1-aware branch from 7cb2c16 to 691ceba Compare July 27, 2022 12:50
@koslambrou koslambrou merged commit a5ab40d into main Jul 28, 2022
@koslambrou koslambrou deleted the kll/make-constraints-plutusv1-aware branch July 28, 2022 17:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants