Skip to content

Add gas to decimal-arithmetic #1217

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

Closed
wants to merge 24 commits into from
Closed

Add gas to decimal-arithmetic #1217

wants to merge 24 commits into from

Conversation

jmcardon
Copy link
Member

@jmcardon jmcardon commented May 3, 2023

No description provided.

@jmcardon jmcardon requested review from jwiegley and emilypi as code owners May 3, 2023 23:29
Copy link
Member

@rsoeldner rsoeldner left a comment

Choose a reason for hiding this comment

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

Great work

threshold = (10 :: Integer) ^ (80 :: Integer)
intValue :: Integer
intValue = fromIntegral coeff
-- Todo: should there be a penalty on `NaN` or `Inf`?
Copy link
Member

Choose a reason for hiding this comment

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

I would argue no, as long as we account a constant factor for it. Do you have a particular benefit in mind?

Copy link
Member Author

Choose a reason for hiding this comment

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

We throw errors on NaN and Inf. That's why this comment.

Copy link
Member

Choose a reason for hiding this comment

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

There should be a penalty imo. NaN and Inf are only seen in degenerate contexts and the user should be paying to check that they're not, say, dividing by 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah i'm just not sure. They're non-observable values, and Data.Decimal can't express NaN or Inf.

in fromIntegral (nbytes * nbytes `quot` 100)
where
threshold :: Integer
threshold = (10 :: Integer) ^ (80 :: Integer)
Copy link
Member

Choose a reason for hiding this comment

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

Basically, decimal with a coefficient < 10^80 is free of charge? Shouldn't our accounting incorporate the exponent?

Also, shouldn't we account for everything (gas > 0), including the case intValue < threshold?

Copy link
Member Author

Choose a reason for hiding this comment

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

above 10^80 it's a penalty. We do charge 1 gas for integers larger than this. It will become more granular in the future.

We don't account for the exponent atm, but there's not all that much arithmetic around it.


thresholdToInteger :: IntThreshold -> Integer
thresholdToInteger = \case
ThresholdPrePact47 -> (10 :: Integer) ^ (30 :: Integer)
Copy link
Member

Choose a reason for hiding this comment

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

Can we comment on the rational\origins of these constants? They are unclear to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

10^30 was an arbitrary constant from before.
10^80 is just a close number to 2^256. Essentially, allowing for cheap math on numbers below that threshold.

Copy link
Contributor

Choose a reason for hiding this comment

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

😃
Thank you @jmcardon

#1152

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you were the motivation for increasing this limit, @CryptoPascal31 :)


{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use camelCase" #-}
Copy link
Member

Choose a reason for hiding this comment

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

Relict

@@ -206,7 +223,7 @@ instance Pretty GasArgs where
GFoldDB -> "GFoldDB"
GModuleMemory i -> "GModuleMemory: " <> pretty i
GPrincipal i -> "GPrincipal: " <> pretty i
GIntegerOpCost i j -> "GIntegerOpCost:" <> pretty i <> colon <> pretty j
GIntegerOpCost i j ts -> "GIntegerOpCost:" <> pretty i <> colon <> pretty j <> colon <> pretty ts
GDecimalOpCost i j -> "GDecimalOpCost:" <> pretty (show i) <> colon <> pretty (show j)
Copy link
Member

Choose a reason for hiding this comment

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

To we need the show here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I don't see why leave it out of the debug info of what threshold we used for gas.

(gasContext gp) of
(Left err, ctx)
| exceptionSignal err == GasExceeded -> TransGasExceeded (ctxGas ctx)
| otherwise -> error $ "ln error: " ++ show err
Copy link
Member

Choose a reason for hiding this comment

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

this is pow error

Copy link
Member Author

Choose a reason for hiding this comment

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

honestly we need to remove all uses of error

Copy link
Member

Choose a reason for hiding this comment

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

why are we erroring here? That's a pretty hard kill. I'd prefer being in a monad here before using error.

Copy link
Member Author

Choose a reason for hiding this comment

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

great question, we're erroring here because this was from john's pr. We shouldn't error anymore.

(gasContext gp) of
(Left err, ctx)
| exceptionSignal err == GasExceeded -> TransGasExceeded (ctxGas ctx)
| otherwise -> error $ "ln error: " ++ show err
Copy link
Member

Choose a reason for hiding this comment

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

this should be sqrt error

Copy link
Member Author

Choose a reason for hiding this comment

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

should just remove error

@jmcardon jmcardon requested a review from sirlensalot as a code owner May 4, 2023 16:36
cabal.project Outdated
@@ -19,5 +19,4 @@ constraints: hspec-golden <0.2,
source-repository-package
type: git
location: https://github.com/kadena-io/decimal-arithmetic.git
tag: 5de7f3c9c93344b3932604827147ed5824fd8f45
--sha256: sha256-+jyOtjJIJo7ggQsNqBS/re36fXom00M71neLa8dm/dM=
tag: 18eb743b8cf4be3904427f82566c5b10fa9a38e4
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
tag: 18eb743b8cf4be3904427f82566c5b10fa9a38e4
tag: 18eb743b8cf4be3904427f82566c5b10fa9a38e4
--sha256: sha256-Vro3wB22oPJDiZw0k8MonwPmoDZFt2rAGU709p9Cfqk=

Copy link
Member Author

Choose a reason for hiding this comment

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

already on a different tag lol

@@ -95,5 +95,6 @@ freeGasEnv = GasEnv 0 0.0 (constGasModel 0)
constGasModel :: Word64 -> GasModel
constGasModel r = GasModel
{ gasModelName = "fixed " <> tShow r
, gasModelType = ConstantGasModel (fromIntegral r)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you say more about how this addition relates to the decimal arithmetic work? It looks like you're adding new functionality that is unrelated to the decimal work per se.

Copy link
Member Author

@jmcardon jmcardon May 4, 2023

Choose a reason for hiding this comment

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

See: https://github.com/kadena-io/pact/pull/1217/files#diff-cab0577f6cd5a3953dabcc04e946f3c74429edb0f0a0dea8088cba1e4bdc7061R65

or, in code:

getTransGasParams = do
  gUsed <- view eeGas >>= liftIO . readIORef
  gEnv <- view eeGasEnv
  let chargeFn = getCharge (view (geGasModel . to gasModelType) gEnv)
      gLim = view geGasLimit gEnv
  pure (Dec.TransGasParams (fromIntegral gUsed) (fromIntegral gLim) chargeFn)
  where
  getCharge (ConstantGasModel g) = const (chargeArithGas (fromIntegral g))
  getCharge _ = chargePactArith

I think I can maybe undo this and simply use runGasModel, but I need to add an entry for the GasArgs for arithmetic functions, and the type parameters there are a bit funky. I will investigate this

| otherwise =
let !nbytes = (I# (IntLog.integerLog2# (abs a)) + 1) `quot` 8
in fromIntegral (nbytes * nbytes `quot` 100)
where
threshold :: Integer
threshold = (10 :: Integer) ^ (30 :: Integer)
Copy link
Contributor

Choose a reason for hiding this comment

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

This constant should receive a clearer name, and perhaps be lifted out and up to the top of the file.

Copy link
Member Author

Choose a reason for hiding this comment

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

arbitraryConstant420

I disagree with retroactively changing this. I'd rather simply use the Threshold type and annotate that.

@@ -154,16 +154,22 @@ powImpl i as@[TLiteral a _,TLiteral b _] = do
| otherwise = twoArgIntOpGas x x *> odds (x * x) (y `quot` 2) (x * z)
powImpl i as = argsError i as

getThreshold :: Eval e IntThreshold
getThreshold =
ifExecutionFlagSet' FlagDisablePact47 ThresholdPrePact47 ThresholdPact47
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a comment here to describe how exactly these flags have changed the threshold?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

@jwiegley jwiegley mentioned this pull request May 4, 2023
4 tasks
threshold = (10 :: Integer) ^ (80 :: Integer)
intValue :: Integer
intValue = fromIntegral coeff
-- Todo: should there be a penalty on `NaN` or `Inf`?
Copy link
Member

Choose a reason for hiding this comment

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

There should be a penalty imo. NaN and Inf are only seen in degenerate contexts and the user should be paying to check that they're not, say, dividing by 0.

@@ -4,6 +4,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE InstanceSigs #-}
Copy link
Member

Choose a reason for hiding this comment

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

unused

data GasModel = GasModel
{ gasModelName :: !Text
, gasModelType :: GasModelType
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
, gasModelType :: GasModelType
, gasModelType :: !GasModelType

(gasContext gp) of
(Left err, ctx)
| exceptionSignal err == GasExceeded -> TransGasExceeded (ctxGas ctx)
| otherwise -> error $ "ln error: " ++ show err
Copy link
Member

Choose a reason for hiding this comment

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

why are we erroring here? That's a pretty hard kill. I'd prefer being in a monad here before using error.

@jmcardon jmcardon changed the base branch from johnw/decimal to master November 15, 2023 15:52
@jmcardon
Copy link
Member Author

With MPFR making it into Pact-5, this can be safely closed.

@jmcardon jmcardon closed this Oct 29, 2024
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.

5 participants