Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strip ALL pact value infos, recursively as well, add tests #1287

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Pact/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ enforcePactValue :: Pretty n => (Term n) -> Eval e PactValue
enforcePactValue t = case toPactValue t of
Left s -> evalError' t $ "Only value-level terms permitted: " <> pretty s
Right v -> do
elide <- ifExecutionFlagSet' FlagDisablePact48 id elideModRefInfo
elide <- ifExecutionFlagSet' FlagDisablePact48 id stripAllPactValueInfo
return (elide v)

reduceApp :: App (Term Ref) -> Eval e (Term Name)
Expand Down
9 changes: 9 additions & 0 deletions src/Pact/Types/PactValue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Pact.Types.PactValue
, _PObject
, _PModRef
, stripPactValueInfo
, stripAllPactValueInfo
) where

import Control.Applicative ((<|>))
Expand Down Expand Up @@ -129,6 +130,14 @@ stripPactValueInfo = \case
PGuard gu -> PGuard gu
PModRef mr -> PModRef mr{_modRefInfo = def }

stripAllPactValueInfo :: PactValue -> PactValue
stripAllPactValueInfo = \case
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: stripPactValueInfo above isn't even enough, the author of that code did not cover PGuard which can recursively contain pact values, the tests in this PR in hash.repl have a case with a user guard which does indeed fail if not stripped recursively.

PLiteral lit -> PLiteral lit
PList vec -> PList (stripAllPactValueInfo <$> vec)
PObject om -> PObject (stripAllPactValueInfo <$> om)
PGuard gu -> PGuard (stripAllPactValueInfo <$> gu)
PModRef mr -> PModRef mr{_modRefInfo = def }

-- | Lenient conversion, implying that conversion back won't necc. succeed.
-- Integers are coerced to Decimal for simple representation.
-- Non-value types are turned into their String representation.
Expand Down
30 changes: 24 additions & 6 deletions tests/pact/hash.repl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

(begin-tx)
(env-exec-config ["DisablePact48"])
(interface iface
(defun f:bool (a:module{iface}))
)

(module my-mod G
(defcap G() true)

(defschema hashes h:string)
(deftable hashes-table:{hashes})
(implements iface)

(defun get-hash (k:string)
(at "h" (read hashes-table k)))

(defun f:bool (a:module{iface}) true)

(defun insert-hash (k:string h:string)
(write hashes-table k {"h":h})
(concat ["added hash ", h, " to table"])
Expand All @@ -25,27 +31,39 @@
; pre fork module hashing
(insert-hash "a" (hash my-mod))
(insert-hash "b" (hash my-mod))
(insert-hash "c" (hash [my-mod, {'a:my-mod}, (create-user-guard (f my-mod))]))
Copy link
Member Author

Choose a reason for hiding this comment

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

this covers all cases recursively for my-mod.

PactValues are:

  • lists
  • objects
  • guards (can contain them recursively too)
  • literals
  • modrefs

Literals cannot contain any pactvalues, so it's just the other 3 cases we must check.

(insert-hash "d" (hash [my-mod, {'a:my-mod}, (create-user-guard (f my-mod))]))
(let*
( (h1 (get-hash "a"))
(h2 (get-hash "b"))
(h3 (get-hash "c"))
(h4 (get-hash "d"))
)
(enforce (= h1 "eU1QsrHzLyYN9620ongvIlpxzzX1KiVGbTDBT6zbh14") "h1 does not match expected value")
(enforce (= h2 "q9JZXDohMARxsVUtQWCiK7APdaiYpvqfJyq-aF3LhAA") "h2 does not match expected value")
(expect-failure "hashes do not match pre-fork" (enforce (= h1 h2) "boom"))
(enforce (= h1 "orgMn9G2BN4Mvq4IX7XbF016YdAhoLLtEIpUPglM3-c") "h1 does not match expected value")
(enforce (= h2 "A7RKCqSxlJMPSoZshF2Rviny30yVUXK6CDnjfwKc-dU") "h2 does not match expected value")
(enforce (= h3 "2Hic2Iy60yTYtCn1Ih6J7X359KAjPjdOkyEUGbR9pa8") "h3 does not match expected value")
(enforce (= h4 "ltxrif1Y_w9qg2pM-V93lMjU15HIA48WBqp3RzlZ0cU") "h4 does not match expected value")
(expect-failure "hashes do not match pre-fork - simple case" (enforce (= h1 h2) "boom"))
(expect-failure "hashes do not match pre-fork - recursive case" (enforce (= h3 h4) "boom"))
)


(env-exec-config [])
; post fork module hashing
(insert-hash "a" (hash my-mod))
(insert-hash "b" (hash my-mod))

(insert-hash "c" (hash [my-mod, {'a:my-mod}, (create-user-guard (f my-mod))]))
(insert-hash "d" (hash [my-mod, {'a:my-mod}, (create-user-guard (f my-mod))]))

(let*
( (h1 (get-hash "a"))
(h2 (get-hash "b"))
(h3 (get-hash "c"))
(h4 (get-hash "d"))
)
(enforce (= h1 "0j95GFheG-uAWbGAjvTqV4QSGE74ZY38jxnNuHJ2p8A") "h1 does not match expected value")
(expect "hashes match post-fork" true (enforce (= h1 h2) "boom"))
(enforce (= h1 "vediBPdnKkzahPDZY2UF_hkS8i7pIXqwsCj925gLng8") "h1 does not match expected value")
(enforce (= h3 "_c98nMfdnxKUdjoE7EQR9RUHfqJDJjlljL2JGGwUqiA") "h3 does not match expected value")
(expect "hashes match post-fork - simple case" true (enforce (= h1 h2) "boom"))
(expect "hashes match post-fork - recursive case" true (enforce (= h1 h2) "boom"))
)
(commit-tx)