runtimeverification / plutus-core-semantics

BSD 3-Clause "New" or "Revised" License
27 stars 5 forks source link

Notes on running Plutus #233

Open ChristianoBraga opened 2 years ago

ChristianoBraga commented 2 years ago

Sucessful install but MyPlutusStarter does not work.

  1. Install nix https://nixos.org/download.html
  2. Edit /etc/nix/nix.conf to include IOHK binary caches. (https://github.com/input-output-hk/plutus/blob/master/README.adoc#iohk-binary-cache)
  3. Instantiate the GitHub template repo https://github.com/input-output-hk/plutus-starter.
  4. Run nix-shell from MyPlutusStarter.
  5. Run cabal update and cabal build in the nix-shell.
  6. Run cabal build plutus-starter-pab in the nix-shell.
  7. Run cabal exec -- plutus-starter-pab in the nix-shell.
  8. In a different terminal, run curl -s http://localhost:9080/api/contract/definitions | jq. You should see:
    [
    {
    "csrSchemas": [
      {
        "argument": {
          "contents": [
            [
              "guessWord",
              {
                "tag": "FormSchemaString"
              }
            ]
          ],
          "tag": "FormSchemaObject"
        },
        "endpointDescription": {
          "getEndpointDescription": "guess"
        }
      },
      {
        "argument": {
          "contents": [
            [
              "secretWord",
              {
                "tag": "FormSchemaString"
              }
            ],
            [
              "amount",
              {
                "tag": "FormSchemaValue"
              }
            ]
          ],
          "tag": "FormSchemaObject"
        },
        "endpointDescription": {
          "getEndpointDescription": "lock"
        }
      }
    ],
    "csrDefinition": "GameContract"
    }
    ]
  9. However, I do not manage to create a wallet. Moreover, when the server starts, wallet 2 does not finish balacing.

Other things that do not work

Vscode and devcontainer

  1. Instantiate the GitHub template repo https://github.com/input-output-hk/plutus-starter.
  2. VSCode devcontainer does not work See https://issuemode.com/issues/input-output-hk/plutus-starter/73685764. a. I am following the steps with vscode and docker for now. Don't forget to follow https://docs.docker.com/engine/install/linux-postinstall/. If you do not get your user to be added to docker group, follow https://stackoverflow.com/questions/56305613/cant-add-user-to-docker-group/66297855#66297855. b. Should you need to move your directory to another place, I recommend cleaning up and going through all steps once again.

Cabal+Nix build with nix running in a docker image

  1. I am trying to use nix inside a Docker container: https://nixos.org/download.html#nix-install-docker
  2. Run docker run -it -v /data/IOG/MyPlutusStarter/:/MyPlutusStarter nixos/nix where /data/IOG/MyPlutusStarter/ is the absolute path to your plutus-starter clone directory. /MyPlutusStarter is where it will be mounted in within your Docker image fs.
  3. There are no editors avaiable in the container. You may want to run: nix-env -iA nixpkgs.emacs or a similar command for your preferred editor.
  4. Edit /etc/nix/nix.conf to include IOHK binary caches. (https://github.com/input-output-hk/plutus/blob/master/README.adoc#iohk-binary-cache)
  5. Run nix-shell from MyPlutusStarter.
  6. Run cabal update and cabal build.
  7. These steps work. However, while trying to run the PAB application, even though the server starts, I did not manage to access it from another terminal outside the docker shell. The documentation says it should be possible. And my docker shell was bare-bones. Didn't have curl and other commands. My docker image was taking all my root space so I had to relocate it. After that, I have lost all my images. I will restart by installing everything locally.
qian-hu commented 2 years ago

Instructions: Build Plutus Playground

  1. Install nix.
  2. Edit /etc/nix/nix.conf to include IOHK binary caches. (https://github.com/input-output-hk/plutus/blob/master/README.adoc#iohk-binary-cache)
  3. git clone plutus-apps : https://github.com/input-output-hk/plutus-apps.
  4. cd into the plutus-apps repository and build the Plutus playground client/server: nix-build -A plutus-playground.client nix-build -A plutus-playground.server
  5. Build other dependencies: nix-build -A plutus-playground.generate-purescript nix-build -A plutus-playground.start-backend
  6. Go into nix-shell, cd into the plutus-playground-client folder and start the playground server as below. [nix-shell:~/plutus-apps/plutus-playground-client]$ plutus-playground-server -i 120s
  7. Open up another nix-shell at the same folder, and start the playground client: [nix-shell:~/plutus-apps/plutus-playground-client]$ npm run start
  8. Open your web-browser and navigate to https://localhost:8009 where you will see the Plutus playground. You can modify the default example, and compile your own codes as the online version of the playground.
ChristianoBraga commented 2 years ago

Runs like a charm! Except for the fact that I needed to invoke nix-shell using nix-shell --extra-experimental-features flakes.

SchmErik commented 2 years ago

From @ChristianoBraga:

So if you guys want to run the example I have shown today to generate the UPLC code,

  1. follow the instructions on Issue #233,
  2. go to plutus-apps/plutus-contract.
  3. cabal update ; cabal build ; cabal repl
  4. import PlutusTx
  5. import Prettyprinter
  6. :l test.hs, where test.hs is the code below
-- Necessary language extensions for the Plutus Tx compiler to work.
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE NoImplicitPrelude   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE ImportQualifiedPost #-}
module BasicPlutusTx where

import PlutusCore.Default qualified as PLC
-- Main Plutus Tx module.
import PlutusTx
-- Additional support for lifting.
import PlutusTx.Lift
-- Builtin functions.
import PlutusTx.Builtins
-- The Plutus Tx Prelude, discussed further below.
import PlutusTx.Prelude

-- Setup for doctest examples.

-- $setup
-- >>> import Tutorial.PlutusTx
-- >>> import PlutusTx
-- >>> import PlutusCore
-- >>> import PlutusCore.Evaluation.Machine.Ck
-- >>> import Data.Text.Prettyprint.Doc

integerOne :: CompiledCode Integer
{- 'compile' turns the 'TExpQ Integer' into a
  'TExpQ (CompiledCode Integer)' and the splice
  inserts it into the program. -}
integerOne = $$(compile
    {- The quote has type 'TExpQ Integer'.
      We always use unbounded integers in Plutus Core, so we have to pin
      down this numeric literal to an ``Integer`` rather than an ``Int``. -}
    [|| (1 :: Integer) ||])
  1. pretty $ getPlc integerOne
ChristianoBraga commented 2 years ago

Instructions: Build Plutus Playground

  1. git clone plutus-apps : https://github.com/input-output-hk/plutus-apps.

Most of the apps failed to build in my environment. I only managed to build plutus-contract and plutus-playground-client.

ChristianoBraga commented 2 years ago

To run plutus-pioneer-program

  1. Choose the week.
  2. Go to cabal.project of that week and look for the hash of plutus-apps commit to be used.
  3. Go to plutus-apps and checkout that commit.
  4. Run nix-shell on plutus-apps dir.
  5. Run build-and-serve-docs. This will allow to browse haddock doc locally (http://0.0.0.0:8002/haddock/). And possibly consistent with the version (commit) of plutus-apps being used.
  6. Go back to the week you want at the pultus-pioneer-program.
  7. Run cabal build and cabal repl on it.
ChristianoBraga commented 2 years ago

To get the UPLC code of the most basic minting policy

  1. Get Week05 of PPP working.
  2. Add prettyprinter ^>=1.7.1 to section build-depends to file plutus-pioneer-program-week05.cabal.
  3. Edit plutus-pioneer-program/code/week05/src/Week05/Free.hs, and add the following lines after mkPolicy () _ = True.
    compiledPolicy :: PlutusTx.CompiledCode (BuiltinData -> BuiltinData -> ())
    compiledPolicy = $$(PlutusTx.compile [|| Scripts.wrapMintingPolicy mkPolicy ||])
  4. Run cabal repl. It will start the Haskell interpeter.
  5. On the interpreter run: a. import Prettyprinter b. import PlutusTx c. pretty $ getPlc $ compiledPolicy d. pretty $ simplifyProgram $ getPlc $ compiledPolicy gets rid of (force _ (delay _)).
ChristianoBraga commented 2 years ago

To print the trivial policy

Take Week05 of Plutus Pioneer Program as basis, considering the previous comment adding compiledPolicy to Free.hs.

Cabal project

Add the following to plutus-pioneer-program-week05.cabal. (Note: this not optimal. There may be some unnecessary imports.)

executable trivpol
  main-is: main.hs
  hs-source-dirs:      src,.
  exposed-modules:     Week05.Free
                     , Week05.Homework1
                     , Week05.Homework2
                     , Week05.NFT
                     , Week05.Signed
                     , Week05.Solution1
                     , Week05.Solution2
  build-depends:       aeson
                     , base ^>=4.14.1.0
                     , containers
                     , data-default
                     , freer-extras
                     , playground-common
                     , plutus-contract
                     , plutus-ledger
                     , plutus-ledger-api
                     , plutus-ledger-constraints
                     , plutus-tx-plugin
                     , plutus-tx
                     , text
                     , prettyprinter ^>=1.7.1
                     , plutus-core ^>=0.1.0.0
                     , prettyprinter-configurable ^>=0.1.0.0
  default-language: Haskell2010

Main.hs

Create the file main.hs at .../plutus-pioneer-program/code/week05 with the following code:

import Week05.Free
import PlutusTx
import Prettyprinter
import PlutusPrelude
import UntypedPlutusCore

main :: IO()

main = do
  putStrLn $ show $ pretty $ simplifyProgram $ getPlc $ compiledPolicy
ChristianoBraga commented 2 years ago

The Plutus IR version of the policy is way smaller. The UPLC code has 1.2M whereas its PIR counter part has 204K.

import Week05.Free
import PlutusTx
import Prettyprinter
import PlutusPrelude
import UntypedPlutusCore

main :: IO()

main = do
  writeFile "trivial-policy.uplc" $ show $ pretty $ simplifyProgram $ getPlc $ compiledPolicy
  writeFile "trivial-policy.pir" $ show $ pretty $ getPir compiledPolicy
ChristianoBraga commented 2 years ago

The command to generate the uplc code should be this one:

writeFile "trivial-true-policy-print-classic-debug.uplc" $ show $ prettyPlcClassicDebug $ simplifyProgram $ getPlc $ compiledPolicyTrue