3

I'm working through the Plutus Pioneer program and I have a bug in my implementation of the validator for homework one. I'd like to add some logging to the validator to get more insight into what I'm doing wrong.

I've seen that the logInfo function is used in the wallet code and I've tried adding that to the validator like:

mkValidator dat _ ctx = do
    logInfo @P.String $ printf "deadline %s" (P.show $ deadline dat)
    False -- dummy value to figure out how to get logging to work

but when I do that I get the following error:

• Couldn't match expected type ‘Bool’
              with actual type ‘Contract w0 s0 e0 b0’
• In a stmt of a 'do' block:
    logInfo @P.String $ printf "deadline %s" (P.show $ deadline dat)
  In the expression:
    do logInfo @P.String $ printf "deadline %s" (P.show $ deadline dat)
       False
  In an equation for ‘mkValidator’:
      mkValidator dat _ ctx
        = do logInfo @P.String
               $ printf "deadline %s" (P.show $ deadline dat)
             False

Upon checking the type of logInfo I see that it has the type logInfo :: forall a w (s :: Row *) e. ToJSON a => a -> Contract w s e () which seems to match up with the error that's given.

How come I can't use this logging function if I'm later returning False? I suspect I'm misunderstanding something fairly basic about how haskell works.

5

You're running into trouble because logInfo is meant to be used inside the Contract monad. The Wallet code uses the Contract monad but the validator code doesn't.

If you're not familiar with how monads and do blocks work, I'd check out Lecture #4

As for logging inside of validators, I'd look at using trace, traceIfTrue, or traceIfFalse. I've only used traceIfFalse though. Remember, validators are compiled into Plutus and run on the chain, so they aren't going to log like normal code. Whereas the wallet code is run natively on your machine.

Hope that helps.

2
  • Is it possible to use that combined with an arbitrary variable? For example, if I want to dynamically display the number of tokens that are missing for the script to validate.
    – Mateus
    Sep 30 '21 at 13:08
  • 1
    Thanks for the great answer! I stumbled on that myself but it's good to know what I found myself is accurate. @Mateus I don't think you can log arbitrary variables within trace or it's derivatives because the Plutus Prelude doesn't offer a show function. Oct 1 '21 at 14:57
1

I'll use this answer to log my journey as I try things out.

Tentative Solution/Answer

Looks like P.show can't be used in a validator based on https://discord.com/channels/826816523368005654/826829805387120690/892870961353879592

I'll wait for more confirmation before accepting this. I hope that's not the case.

Attempt 1

I've also tried using trace with

mkValidator dat _ ctx = do
    trace $ printf "deadline %s" (P.show $ deadline dat)
    False
but that also results in an error:

• Couldn't match expected type ‘Bool’ with actual type ‘a0 -> b0’
• In a stmt of a 'do' block:
    trace $ printf "deadline %s" (P.show $ deadline dat)
  In the expression:
    do trace $ printf "deadline %s" (P.show $ deadline dat)
       False
  In an equation for ‘mkValidator’:
      mkValidator dat _ ctx
        = do trace $ printf "deadline %s" (P.show $ deadline dat)
             False

Attempt 2

Looks like using do in a validator is a fools errand. Now I'm trying to add debug info to the trace message.

mkValidator :: VestingDatum -> () -> ScriptContext -> Bool
mkValidator dat _ ctx  = traceIfFalse (P.show $ deadline dat) False

but it turns out that P.show doesn't return the correct type (it returns [P.Char] when a BuiltinString is required). Trying to coerce [P.Char] to a BuiltinString using stringToBuiltinString from https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Builtins-Class.html#v:stringToBuiltinString results in a very large error that looks like linker problems:

GHC Core to PLC plugin: E043:Error: Reference to a name which is not a local, a builtin, or an external INLINABLE function: Variable GHC.CString.unpackAppendCString# No unfolding Context: Compiling expr: GHC.CString.unpackAppendCString# Context: Compiling expr: GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 Context: Compiling expr: GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 (GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime3 (case GHC.Show.$w$cshowsPrec4 0# (x `cast` (Plutus.V1.Ledger.Time.N:POSIXTime[0] :: GHC.Types.Coercible Plutus.V1.Ledger.Time.POSIXTime GHC.Integer.Type.Integer)) Plutus.V1.Ledger.Time.$fShowPOSIXTime5 of { (# ww3 [Occ=Once], ww4 [Occ=Once] #) -> GHC.Types.: @ GHC.Types.Char ww3 ww4 })) Context: Compiling expr: \ (x [Occ=Once] :: Plutus.V1.Ledger.Time.POSIXTime) -> GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 (GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime3 (case GHC.Show.$w$cshowsPrec4 0# (x `cast` (Plutus.V1.Ledger.Time.N:POSIXTime[0] :: GHC.Types.Coercible Plutus.V1.Ledger.Time.POSIXTime GHC.Integer.Type.Integer)) Plutus.V1.Ledger.Time.$fShowPOSIXTime5 of { (# ww3 [Occ=Once], ww4 [Occ=Once] #) -> GHC.Types.: @ GHC.Types.Char ww3 ww4 })) Context: Compiling definition of: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow Context: Compiling expr: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow Context: Compiling expr: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow (case dat of { Main.VestingDatum _ [Occ=Dead] _ [Occ=Dead] ds_dLa0 [Occ=Once] -> ds_dLa0 }) Context: Compiling expr:

3
  • P.show will always fail inside a validator, because you can't use functions defined in Haskell Prelude, you need to use instead PlutusTx.Prelude
    – Mateus
    Sep 30 '21 at 13:06
  • Does PlutusTx.Prelude include a show function? I couldn't find one but maybe I wasn't looking in the right place. Oct 1 '21 at 16:24
  • 1
    There isn't, since show transforms anything into a String and PlutusTx.Prelude only works with BuiltinString or BuiltinByteString
    – Mateus
    Oct 13 '21 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.