-
Notifications
You must be signed in to change notification settings - Fork 106
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
Conversation
There was a problem hiding this 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`? |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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" #-} |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
trans/src/Pact/Trans/Dec.hs
Outdated
(gasContext gp) of | ||
(Left err, ctx) | ||
| exceptionSignal err == GasExceeded -> TransGasExceeded (ctxGas ctx) | ||
| otherwise -> error $ "ln error: " ++ show err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is pow error
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
trans/src/Pact/Trans/Dec.hs
Outdated
(gasContext gp) of | ||
(Left err, ctx) | ||
| exceptionSignal err == GasExceeded -> TransGasExceeded (ctxGas ctx) | ||
| otherwise -> error $ "ln error: " ++ show err |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should just remove error
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tag: 18eb743b8cf4be3904427f82566c5b10fa9a38e4 | |
tag: 18eb743b8cf4be3904427f82566c5b10fa9a38e4 | |
--sha256: sha256-Vro3wB22oPJDiZw0k8MonwPmoDZFt2rAGU709p9Cfqk= |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
threshold = (10 :: Integer) ^ (80 :: Integer) | ||
intValue :: Integer | ||
intValue = fromIntegral coeff | ||
-- Todo: should there be a penalty on `NaN` or `Inf`? |
There was a problem hiding this comment.
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.
src/Pact/Types/Gas.hs
Outdated
@@ -4,6 +4,7 @@ | |||
{-# LANGUAGE OverloadedStrings #-} | |||
{-# LANGUAGE TypeFamilies #-} | |||
{-# LANGUAGE LambdaCase #-} | |||
{-# LANGUAGE InstanceSigs #-} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
, gasModelType :: GasModelType | |
, gasModelType :: !GasModelType |
trans/src/Pact/Trans/Dec.hs
Outdated
(gasContext gp) of | ||
(Left err, ctx) | ||
| exceptionSignal err == GasExceeded -> TransGasExceeded (ctxGas ctx) | ||
| otherwise -> error $ "ln error: " ++ show err |
There was a problem hiding this comment.
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
.
With MPFR making it into Pact-5, this can be safely closed. |
No description provided.