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

Bump integer thresholds #1272

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 10 additions & 10 deletions src/Pact/Gas/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ tableGasModel gasConfig =
GUserApp t -> case t of
Defpact -> gasToMilliGas $ _gasCostConfig_defPactCost gasConfig * _gasCostConfig_functionApplicationCost gasConfig
_ -> gasToMilliGas $ _gasCostConfig_functionApplicationCost gasConfig
GIntegerOpCost i j ->
gasToMilliGas $ intCost (fst i) + intCost (fst j)
GIntegerOpCost i j ts ->
gasToMilliGas $ intCost ts (fst i) + intCost ts (fst j)
GDecimalOpCost _ _ -> mempty
GMakeList v -> gasToMilliGas $ expLengthPenalty v
GSort len -> gasToMilliGas $ expLengthPenalty len
Expand Down Expand Up @@ -310,9 +310,9 @@ tableGasModel gasConfig =
GInterfaceDecl _interfaceName _iCode -> gasToMilliGas (_gasCostConfig_interfaceCost gasConfig)
GModuleMemory i -> gasToMilliGas $ moduleMemoryCost i
GPrincipal g -> gasToMilliGas $ fromIntegral g * _gasCostConfig_principalCost gasConfig
GMakeList2 len msz ->
GMakeList2 len msz ts ->
let glen = fromIntegral len
in gasToMilliGas $ glen + maybe 0 ((* glen) . intCost) msz
in gasToMilliGas $ glen + maybe 0 ((* glen) . intCost ts) msz
GZKArgs arg -> gasToMilliGas $ case arg of
PointAdd g -> pointAddGas g
ScalarMult g -> scalarMulGas g
Expand Down Expand Up @@ -374,16 +374,16 @@ defaultGasModel = tableGasModel defaultGasConfig

#if !defined(ghcjs_HOST_OS)
-- | Costing function for binary integer ops
intCost :: Integer -> Gas
intCost !a
| (abs a) < threshold = 0
intCost :: IntOpThreshold -> Integer -> Gas
intCost ts !a
| (abs a) < threshold ts = 0
| 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)

threshold :: IntOpThreshold -> Integer
threshold Pact43IntThreshold = (10 :: Integer) ^ (30 :: Integer)
threshold Pact48IntThreshold = (10 :: Integer) ^ (80 :: Integer)

_intCost :: Integer -> Int
_intCost !a =
Expand Down
9 changes: 6 additions & 3 deletions src/Pact/Native.hs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,8 @@ list i as = return $ TList (V.fromList as) TyAny (_faInfo i) -- TODO, could set
makeList :: GasRNativeFun e
makeList i [TLitInteger len,value] = case typeof value of
Right ty -> do
ga <- ifExecutionFlagSet' FlagDisablePact45 (GMakeList len) (GMakeList2 len Nothing)
ts <- ifExecutionFlagSet' FlagDisablePact48 Pact43IntThreshold Pact48IntThreshold
ga <- ifExecutionFlagSet' FlagDisablePact45 (GMakeList len) (GMakeList2 len Nothing ts)
computeGas' i ga $ return $
toTListV ty def $ V.replicate (fromIntegral len) value
Left ty -> evalError' i $ "make-list: invalid value type: " <> pretty ty
Expand All @@ -936,7 +937,8 @@ enumerate i = \case
-- ^ The generated vector
-> Eval e (Term Name)
computeList len sz v = do
ga <- ifExecutionFlagSet' FlagDisablePact45 (GMakeList len) (GMakeList2 len (Just sz))
ts <- ifExecutionFlagSet' FlagDisablePact48 Pact43IntThreshold Pact48IntThreshold
ga <- ifExecutionFlagSet' FlagDisablePact45 (GMakeList len) (GMakeList2 len (Just sz) ts)
computeGas' i ga $ pure $ toTListV tTyInteger def $ fmap toTerm v

step to' inc acc
Expand Down Expand Up @@ -1315,7 +1317,8 @@ stringToCharList t = V.fromList $ tLit . LString . T.singleton <$> T.unpack t
strToList :: GasRNativeFun e
strToList i [TLitString s] = do
let len = fromIntegral $ T.length s
ga <- ifExecutionFlagSet' FlagDisablePact45 (GMakeList len) (GMakeList2 len Nothing)
ts <- ifExecutionFlagSet' FlagDisablePact48 Pact43IntThreshold Pact48IntThreshold
ga <- ifExecutionFlagSet' FlagDisablePact45 (GMakeList len) (GMakeList2 len Nothing ts)
computeGas' i ga $ return $ toTListV tTyString def $ stringToCharList s
strToList i as = argsError i as

Expand Down
15 changes: 9 additions & 6 deletions src/Pact/Native/Ops.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,18 @@ powImpl i as@[TLiteral a _,TLiteral b _] = do
powImpl i as = argsError i as

twoArgIntOpGas :: Integer -> Integer -> Eval e ()
twoArgIntOpGas loperand roperand =
computeGasCommit def "" (GIntegerOpCost (loperand, Nothing) (roperand, Nothing))
twoArgIntOpGas loperand roperand = do
ts <- ifExecutionFlagSet' FlagDisablePact48 Pact43IntThreshold Pact48IntThreshold
computeGasCommit def "" (GIntegerOpCost (loperand, Nothing) (roperand, Nothing) ts)

twoArgDecOpGas :: Decimal -> Decimal -> Eval e ()
twoArgDecOpGas loperand roperand =
twoArgDecOpGas loperand roperand = do
ts <- ifExecutionFlagSet' FlagDisablePact48 Pact43IntThreshold Pact48IntThreshold
computeGasCommit def ""
(GIntegerOpCost
(decimalMantissa loperand, Just (fromIntegral (decimalPlaces loperand)))
(decimalMantissa roperand, Just (fromIntegral (decimalPlaces roperand))))
(GIntegerOpCost
(decimalMantissa loperand, Just (fromIntegral (decimalPlaces loperand)))
(decimalMantissa roperand, Just (fromIntegral (decimalPlaces roperand)))
ts)

legalLogArg :: Literal -> Bool
legalLogArg = \case
Expand Down
20 changes: 16 additions & 4 deletions src/Pact/Types/Gas.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Pact.Types.Gas
, MilliGasLimit(..)
, ZKGroup(..)
, ZKArg(..)
, IntOpThreshold(..)
, gasLimitToMilliGasLimit
-- * optics
, geGasLimit
Expand Down Expand Up @@ -166,18 +167,29 @@ data GasArgs
-- ^ The cost of the in-memory representation of the module
| GPrincipal !Int
-- ^ the cost of principal creation and validation
| GIntegerOpCost !(Integer, Maybe Integer) !(Integer, Maybe Integer)
| GIntegerOpCost !(Integer, Maybe Integer) !(Integer, Maybe Integer) IntOpThreshold
-- ^ Integer costs
| GDecimalOpCost !Decimal !Decimal
-- ^ Decimal costs
| GMakeList2 !Integer !(Maybe Integer)
| GMakeList2 !Integer !(Maybe Integer) IntOpThreshold
-- ^ List versioning 2
| GZKArgs !ZKArg
| GReverse !Int
-- ^ Cost of reversing a list of a given length
| GFormatValues !Text !(V.Vector PactValue)
-- ^ Cost of formatting with the given format string and args

data IntOpThreshold
= Pact43IntThreshold
| Pact48IntThreshold
deriving (Eq, Show, Enum, Bounded)

instance Pretty IntOpThreshold where
pretty = \case
Pact43IntThreshold -> "Pact43IntThreshold"
Pact48IntThreshold -> "Pact48IntThreshold"


-- | The elliptic curve pairing group we are
-- handling
data ZKGroup
Expand Down Expand Up @@ -227,9 +239,9 @@ 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)
GMakeList2 i k -> "GMakeList2:" <> pretty i <> colon <> pretty k
GMakeList2 i k ts -> "GMakeList2:" <> pretty i <> colon <> pretty k <> colon <> pretty ts
GZKArgs arg -> "GZKArgs:" <> pretty arg
GReverse len -> "GReverse:" <> pretty len
GFormatValues s args -> "GFormatValues:" <> pretty s <> pretty (V.toList args)
Expand Down
28 changes: 28 additions & 0 deletions tests/pact/gas.repl
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,31 @@ d.G3
(expect "gas of formatting a sample list" 10014 (env-gas))

(commit-tx)

; tests for pre/post integer ops
(begin-tx)
(module m G (defcap G () true)
(defconst i79:integer (+ (^ 10 79) 1))
(defconst i80:integer (+ (^ 10 80) 1))
)
(env-exec-config ["DisablePact48"])
(env-gas 0)
(+ i79 i79)
(expect "gas of + pre-fork 10^79" (env-gas) 21)

(env-gas 0)
(+ i80 i80)
(expect "gas of + pre-fork 10^80" (env-gas) 21)

; post-fork
(env-gas 0)
(env-exec-config [])
(env-gas 0)
(+ i79 i79)
(expect "gas of + post-fork 10^79" (env-gas) 1)

(env-gas 0)
(+ i80 i80)
(expect "gas of + post-fork 10^80" (env-gas) 21)

(commit-tx)