2

In the PPP lecture 7 state machines are introduced. They are used to make the guessing game more concise.

One of the situations is where the first player has won and needs to prove their guess by providing the nonce they originally used to hash the guess. A function was written for this named check

check :: ByteString -> ByteString -> GameDatum -> GameRedeemer -> ScriptContext -> Bool
check bsZero' bsOne' (GameDatum bs (Just c)) (Reveal nonce) _ =
    sha2_256 (nonce `concatenate` if c == Zero then bsZero' else bsOne') == bs
check _       _      _                       _              _ = True

This function is then assigned in the StateMachine definition as the smCheck.

Is this function then used as a sort of predicate on every attempted transition to a new state?

Additionally, is this an example of pattern matching to the specific case where the transaction includes a redeemer and nonce?

Thanks!

1

Is this function then used as a sort of predicate on every attempted transition to a new state?

Yes, this is why we include the following line

check _ _ _ _ _ = True

You can find docs for smCheck here https://playground.plutus.iohkdev.io/tutorial/haddock/plutus-contract/html/Language-Plutus-Contract-StateMachine-OnChain.html#v:smCheck

Additionally, is this an example of pattern matching to the specific case where the transaction includes a redeemer and nonce?

Yes, the logic will be run only if both players made their moves (1st player put hashed version of his move, 2nd player Zero or One) and if the first player wants to prove that he can withdraw the money.

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.