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

Principals #22

Merged
merged 45 commits into from
Nov 14, 2023
Merged

Principals #22

merged 45 commits into from
Nov 14, 2023

Conversation

0xd34df00d
Copy link
Contributor

@0xd34df00d 0xd34df00d commented Oct 20, 2023

  • create-principal
  • is-principal
  • typeof-principal
  • validate-principal

@0xd34df00d 0xd34df00d marked this pull request as ready for review October 27, 2023 04:35
Comment on lines 41 to 42
coreGasPerLegacyGas :: Semiring r => r
coreGasPerLegacyGas = fromNatural 1000
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is super ugly and I'd like this to go away, but we'll need to settle down with proper gas accounting first.

Copy link
Member

@jmcardon jmcardon left a comment

Choose a reason for hiding this comment

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

Good work so far!

In general, the main changes are:

  • Removing principals are pactvalues. They are not. I like the idea though!!! I think we need to pitch it to the team and discuss how we could work with it moving forward.
  • Remove all uses of gas. Gas is a task on its own. We will work on gas overall for pact-core as a large task.

@@ -111,6 +114,9 @@ pattern TyBool = TyPrim PrimBool
pattern TyString :: Type
pattern TyString = TyPrim PrimString

pattern TyPrincipal :: Type
pattern TyPrincipal = TyPrim PrimPrincipal
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be removed until we settle on things actually returning principals as a type

@@ -53,6 +54,7 @@ data PrimType =
PrimString |
PrimGuard |
PrimTime |
PrimPrincipal |
Copy link
Member

Choose a reason for hiding this comment

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

Ditto here, delete this

@@ -16,6 +16,7 @@ module Pact.Core.Type
, pattern TyTime
, pattern TyBool
, pattern TyString
, pattern TyPrincipal
Copy link
Member

Choose a reason for hiding this comment

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

Principal "type" needs to be removed, at least for now.

createPrincipalForGuard :: (MonadGas m) => Guard FullyQualifiedName PactValue -> m Pr.Principal
createPrincipalForGuard g = do
-- TODO make gas charging actually go through the model
chargeGas coreGasPerLegacyGas
Copy link
Member

Choose a reason for hiding this comment

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

Remove all uses of gas. Gas in core is not implemented yet.

Moreover, this is my fault, I should clarify: we do not have to match legacy on gas.

mkHash bss = do
let bs = mconcat bss
-- the original pact impl charged 1 gas per 64 bytes of hashing,
void $ chargeGas $ coreGasPerLegacyGas <> Gas (fromIntegral (BS.length bs) * coreGasPerLegacyGas `quot` 64)
Copy link
Member

Choose a reason for hiding this comment

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

Here as well, remove the gas code.

chargeGas coreGasPerLegacyGas
case g of
GKeyset (KeySet ks pf) -> case (toList ks, predicateToString pf) of
([k], "keys-all") -> pure $ Pr.K k
Copy link
Member

Choose a reason for hiding this comment

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

no need to predicate-to-string, you can match on the predicate itself.

@@ -184,6 +198,7 @@ instance J.Encode (StableEncoding PactValue) where
PModRef mr -> J.build (StableEncoding mr)
PCapToken _ct -> error "not implemented"
PTime pt -> J.build (StableEncoding pt)
PPrincipal pr -> J.build (StableEncoding pr)
Copy link
Member

Choose a reason for hiding this comment

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

Remove, principals are not pact values yet. We don't produce principals are their own pactValues

@@ -37,6 +38,9 @@ newtype Gas
deriving (Semigroup, Monoid) via (Sum Word64)
deriving (Semiring, Enum) via Word64

coreGasPerLegacyGas :: Semiring r => r
Copy link
Member

Choose a reason for hiding this comment

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

No gas just yet. Remove.

@@ -231,6 +232,7 @@ instance Pretty ArgTypeError where
ATETable -> "[table]"
ATEClosure -> "[closure]"
ATEModRef -> "[modref]"
ATEPrincipal -> "[principal]"
Copy link
Member

Choose a reason for hiding this comment

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

Remove here as well.

@0xd34df00d
Copy link
Contributor Author

@jmcardon addressed, thanks for the review! De-gassified and de-pactvalue-tized.

@@ -28,12 +32,18 @@ newtype PublicKeyText = PublicKeyText { _pubKey :: Text }
instance Pretty PublicKeyText where
pretty (PublicKeyText t) = pretty t

renderPublicKeyText :: PublicKeyText -> Text
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this is unnecessary and prefer the usage of _pubKey which we might rename as _publicKeyText ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to make the intent of (stable-)rendering it explicit. And shall we ever change/extend the representation, that'd be easier to achieve.

@@ -8,6 +8,7 @@
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE ImportQualifiedPost #-}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{-# LANGUAGE ImportQualifiedPost #-}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personal preference — I find this syntax more readable, especially where otherwise you'd have

import Data.Vector(Vector)
import Data.Text(Text)
-- bunch of other unqualified imports
import qualified Data.Vector as V
import qualified Data.Text as T
-- bunch of other qualified imports

while with the post-syntax it's easier to see what gets imported from a given package/module when everything's grouped by the actual module name.

In fact, I did think of making a pass converting everything to ImportQualifiedPost (of course, after discussing this with the rest of the team when we'll have some time), so I can surely remove this for now for consistency.

@@ -55,6 +58,7 @@ import Pact.Core.Persistence
import Pact.Core.DefPacts.Types
import Pact.Core.Environment
import Pact.Core.Capabilities
import Pact.Core.Principal qualified as Pr
Copy link
Member

Choose a reason for hiding this comment

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

I think throughout the codebase, we have not used it and I would personally prefer the standard way

Suggested change
import Pact.Core.Principal qualified as Pr
import qualified Pact.Core.Principal as Pr

also is this actually needed? Most of pact-core can be just imported without name clash

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, the C ctor clashes with Pact.Core.IR.Eval.Runtime.C, for example. I could've renamed the constructors themselves into PrC/etc, but I wanted to keep consistent with the old version, and personally I prefer disambiguating at module import time (oh, I wish haskell had agda's import renames!)

GUserGuard (UserGuard f args) -> do
h <- mkHash $ map encodeStable args
pure $ Pr.U (Pretty.renderText f) (hashToText h)
-- TODO orig pact gets here ^^^^ a Name
Copy link
Member

Choose a reason for hiding this comment

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

Todo still pending?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. I think we agreed to sort this out later on when dealing with Names more thoroughly.

let args' = map encodeStable args
f' = T.encodeUtf8 $ renderQualName $ fqnToQualName f
pid' = T.encodeUtf8 . renderDefPactId <$> pid
h <- mkHash $ f' : args' ++ maybe [] pure pid'
Copy link
Member

Choose a reason for hiding this comment

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

Could be expressed using maybeToList?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great suggestion, thanks!

h <- mkHash $ f' : args' ++ maybe [] pure pid'
pure $ Pr.C $ hashToText h
where
mkHash bss = pure $ pactHash $ mconcat bss
Copy link
Member

Choose a reason for hiding this comment

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

why the Monad constraint and use of pure? I feel like here a type signature would be beneficial

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's for when we'll eventually have gas. It was all in MonadGas originally, but MonadGas was removed recently, so I changed that to Monad, while still keeping monadic syntax, so that (re)introducing gas would require less changes later on.

LMK if you'd nevertheless like to purify this function (for now?)

Copy link
Member

Choose a reason for hiding this comment

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

Could just make it pure for now.

@@ -0,0 +1,175 @@
{-# LANGUAGE ImportQualifiedPost #-}
Copy link
Member

Choose a reason for hiding this comment

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

same

nameMatcher = asMatcher $ qualifiedNameMatcher
<|> bareNameMatcher
where
bareNameMatcher :: Parser ()
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason, we use the Parsers package and not plain attoparsec here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are some things missing in atto. Some are easy to reimplement (like oneOf), others are a tad more involved (like ident). Since the legacy pact used parsers, depending on that package in pact-core as well both simplifies the implementation and helps ensure backwards compatible behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Tbh, we don't need parsers either to parse an ident either but we can punt this to later.

GModuleGuard (ModuleGuard mn n) -> pure $ Pr.M mn n
GUserGuard (UserGuard f args) -> do
h <- mkHash $ map encodeStable args
pure $ Pr.U (Pretty.renderText f) (hashToText h)
Copy link
Member

Choose a reason for hiding this comment

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

Remove Pretty.renderText here and replace with renderQualName (fqnToQualName f)

h <- mkHash $ f' : args' ++ maybe [] pure pid'
pure $ Pr.C $ hashToText h
where
mkHash bss = pure $ pactHash $ mconcat bss
Copy link
Member

Choose a reason for hiding this comment

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

Could just make it pure for now.

nameMatcher = asMatcher $ qualifiedNameMatcher
<|> bareNameMatcher
where
bareNameMatcher :: Parser ()
Copy link
Member

Choose a reason for hiding this comment

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

Tbh, we don't need parsers either to parse an ident either but we can punt this to later.

@0xd34df00d
Copy link
Contributor Author

@jmcardon done! Except for the parsers — let's indeed do this later.

@0xd34df00d 0xd34df00d merged commit f274e5a into master Nov 14, 2023
@0xd34df00d 0xd34df00d deleted the gr/principals branch November 14, 2023 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants