plutus icon indicating copy to clipboard operation
plutus copied to clipboard

CIP-0057 Plutus Blueprints support: generate blueprint for a contract.

Open Unisay opened this issue 1 year ago • 2 comments
trafficstars

This PR adds a CIP-0057 contract blueprint generation functionality:

  • A Template Haskell macros makeIsDataSchemaIndexed that alongside the IsData instances makes a HasDataSchema instance which is compatible with the generated ToData/FromData instances.
  • A set of data types to build a contract blueprint definition skeleton.
  • A function to derive JSON-schema definitions.
  • A function to make a safe JSON-reference to a JSON-schema definition (e.g. { "$ref": "#/definitions/Foo" })

Unisay avatar Feb 14 '24 10:02 Unisay

@Unisay Sorry for the delay, but I'll try to review this today.

kwxm avatar Feb 23 '24 10:02 kwxm

@Unisay Sorry for the delay, but I'll try to review this today.

🙏🏼 thank you, I will ll be addressing all the comments next week.

Unisay avatar Feb 23 '24 13:02 Unisay

  • I definitely want us to validate the output against the schema! I found at least one bug, there might be more.

I researched JSON Schema validator tools that:

  1. Support Draft 2020-12
  2. Is available in the nix packages (or flakes) and are installable/runnable with nix;

and the result is Ø

Do you think we can relax the requirement 2 and if yes then how would we make such a tool available for CI?

Unisay avatar Feb 27 '24 09:02 Unisay

Do you think we can relax the requirement 2 and if yes then how would we make such a tool available for CI?

It will make our life much harder. What tools are there that satisfy requirement 1? We might just be able to package one.

michaelpj avatar Feb 27 '24 10:02 michaelpj

What tools are there that satisfy requirement 1?

https://ajv.js.org/packages/ajv-cli.html

I use it this way atm:

nix-shell -p nodejs_21

then npx or npm install:

npx ajv-cli test \
  --strict=log \
  --spec=draft2020 \
  -s plutus-blueprint \
  -r plutus-blueprint-argument \
  -r plutus-data \
  -r plutus-blueprint-parameter \
  -r plutus-builtin \
  -d Acme.golden \
  --valid

(it does catch some invalid schemas, but it doesn't catch the error you found s/constructor/index/ 🤷🏼‍♂️ )

Unisay avatar Feb 27 '24 10:02 Unisay

Does this PR include the function for generating the blueprint? I was expecting to see something like mkBlueprint :: CompiledCode (Datum -> Redeemer -> ScriptContext -> Bool) -> ContractBlueprint but I'm not seeing it.

Yes, see the PlutusTx.Blueprint.Write module, its called writeBlueprint. There is also a test case that uses it.

Unisay avatar Mar 01 '24 08:03 Unisay

By the way, (to be done separately) we'd also need to think about how this will interact with AsData - @michaelpj

zliu41 avatar Mar 04 '24 23:03 zliu41

To use it, one must instantiate types with a type-level list, which they have to construct manually.

The idea is that in the minimal implementation users supply a list of types involved into a contract API manually, such that a convenience function (which derives such a list of types from a typed validator function type) could be provided on top of it .

Anyway, only providing CompiledCode (Datum -> Redeemer -> ScriptContext -> Bool) is not enough for a blueprint:

  1. its missing all the meta information: Preamble, titles, descriptions, comments.
  2. CIP-57 describes a contract blueprint that is comprised of potentially more than one validator.

Unisay avatar Mar 05 '24 09:03 Unisay

By the way, (to be done separately) we'd also need to think about how this will interact with AsData

It's a bit awkward. In particular, you would like to use a schema definition based on the "real" datatype definition, which doesn't even exist with AsData. Perhaps we can still emit it with a slightly different name or something :thinking:

michaelpj avatar Mar 05 '24 09:03 michaelpj