Skip to content

Commit

Permalink
Use Int64 in CostModelParams
Browse files Browse the repository at this point in the history
  • Loading branch information
zliu41 committed Apr 23, 2024
1 parent 2e2a7fb commit d41e4a2
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Control.Monad.Writer (runWriterT)
import Data.Bifunctor (bimap, second)
import Data.ByteString.Lazy qualified as LBS (readFile)
import Data.Either.Extras (unsafeFromEither)
import Data.Int (Int64)
import Data.List (isSuffixOf)
import PlutusBenchmark.Common (getDataDir)
import PlutusBenchmark.Marlowe.Core.V1.Semantics (MarloweData)
Expand Down Expand Up @@ -307,7 +308,7 @@ evaluationContext =


-- | Cost model, hardwired for testing and fair benchmarking.
testCostModel :: [(String, Integer)]
testCostModel :: [(String, Int64)]
testCostModel =
[
("addInteger-cpu-arguments-intercept", 205665)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Data.Aeson
import Data.Aeson.Flatten
import Data.Data (Data)
import Data.HashMap.Strict qualified as HM
import Data.Int (Int64)
import Data.Map qualified as Map
import Data.Map.Merge.Lazy qualified as Map
import Data.Text qualified as Text
Expand Down Expand Up @@ -149,7 +150,7 @@ The associated keys/names to the parameter values are arbitrarily set by the plu
See Note [Cost model parameters]
-}
type CostModelParams = Map.Map Text.Text Integer
type CostModelParams = Map.Map Text.Text Int64

-- See Note [Cost model parameters]
-- | Extract the model parameters from a model.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

### Changed

- `mkEvaluationContext` now takes `[Int64]` (instead of `[Integer]`).
7 changes: 4 additions & 3 deletions plutus-ledger-api/exe/analyse-script-events/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import UntypedPlutusCore as UPLC
import Control.Lens hiding (List)
import Control.Monad.Primitive (PrimState)
import Control.Monad.Writer.Strict
import Data.Int (Int64)
import Data.List (find, intercalate)
import Data.Primitive.PrimArray qualified as P
import Data.SatInt (fromSatInt)
Expand All @@ -42,7 +43,7 @@ import Text.Printf (hPrintf, printf)
-- | The type of a generic analysis function
type EventAnalyser
= EvaluationContext
-> [Integer] -- cost parameters
-> [Int64] -- cost parameters
-> ScriptEvaluationEvent
-> IO ()

Expand Down Expand Up @@ -336,8 +337,8 @@ analyseOneFile analyse eventFile = do
Just costParams -> Just . (,costParams) . fst <$> runWriterT (f costParams)

runSingleEvent
:: Maybe (EvaluationContext, [Integer])
-> Maybe (EvaluationContext, [Integer])
:: Maybe (EvaluationContext, [Int64])
-> Maybe (EvaluationContext, [Int64])
-> ScriptEvaluationEvent
-> IO ()
runSingleEvent ctxV1 ctxV2 event =
Expand Down
5 changes: 3 additions & 2 deletions plutus-ledger-api/exe/common/LoadScriptEvents.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PlutusLedgerApi.Common
import PlutusLedgerApi.Test.EvaluationEvent

import Codec.Serialise (Serialise, readFileDeserialise)
import Data.Int (Int64)
import Data.List.NonEmpty (NonEmpty, toList)
import GHC.Generics (Generic)

Expand Down Expand Up @@ -48,9 +49,9 @@ data ScriptEvaluationEvent2
deriving anyclass (Serialise)

data ScriptEvaluationEvents2 = ScriptEvaluationEvents2
{ eventsCostParamsV1' :: Maybe [Integer]
{ eventsCostParamsV1' :: Maybe [Int64]
-- ^ Cost parameters shared by all PlutusV1 evaluation events in `eventsEvents`, if any.
, eventsCostParamsV2' :: Maybe [Integer]
, eventsCostParamsV2' :: Maybe [Int64]
-- ^ Cost parameters shared by all PlutusV2 evaluation events in `eventsEvents`, if any.
, eventsEvents2 :: NonEmpty ScriptEvaluationEvent2
}
Expand Down
5 changes: 3 additions & 2 deletions plutus-ledger-api/src/PlutusLedgerApi/Common/ParamName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import PlutusCore.Evaluation.Machine.CostModelInterface
import Control.Monad.Except
import Control.Monad.Writer.Strict
import Data.Char (toLower)
import Data.Int (Int64)
import Data.List as List (lookup)
import Data.Map qualified as Map
import Data.Text qualified as Text
Expand Down Expand Up @@ -86,7 +87,7 @@ tagWithParamNames :: forall k m. (Enum k, Bounded k,
-- OPTIMIZE: MonadWriter.CPS is probably better than MonadWriter.Strict but needs mtl>=2.3
-- OPTIMIZE: using List [] as the log datatype is worse than others (DList/Endo) but does not matter much here
MonadWriter [CostModelApplyWarn] m)
=> [Integer] -> m [(k, Integer)]
=> [Int64] -> m [(k, Int64)]
tagWithParamNames ledgerParams =
let paramNames = enumerate @k
lenExpected = length paramNames
Expand All @@ -105,5 +106,5 @@ tagWithParamNames ledgerParams =

-- | Untags the plutus version from the typed cost model parameters and returns their raw textual form
-- (internally used by CostModelInterface).
toCostModelParams :: IsParamName p => [(p, Integer)] -> CostModelParams
toCostModelParams :: IsParamName p => [(p, Int64)] -> CostModelParams
toCostModelParams = Map.fromList . fmap (first showParamName)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PlutusCore.Default as Plutus (BuiltinSemanticsVariant (DefaultFunSemantic
import Control.Monad
import Control.Monad.Except
import Control.Monad.Writer.Strict
import Data.Int (Int64)

{-| Build the 'EvaluationContext'.
Expand All @@ -30,7 +31,7 @@ IMPORTANT: The evaluation context of every Plutus version must be recreated upon
a protocol update with the updated cost model parameters.
-}
mkEvaluationContext :: (MonadError CostModelApplyError m, MonadWriter [CostModelApplyWarn] m)
=> [Integer] -- ^ the (updated) cost model parameters of the protocol
=> [Int64] -- ^ the (updated) cost model parameters of the protocol
-> m EvaluationContext
mkEvaluationContext = tagWithParamNames @V1.ParamName
>=> pure . toCostModelParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PlutusCore.Default as Plutus (BuiltinSemanticsVariant (DefaultFunSemantic
import Control.Monad
import Control.Monad.Except
import Control.Monad.Writer.Strict
import Data.Int (Int64)

{-| Build the 'EvaluationContext'.
Expand All @@ -30,7 +31,7 @@ IMPORTANT: The evaluation context of every Plutus version must be recreated upon
a protocol update with the updated cost model parameters.
-}
mkEvaluationContext :: (MonadError CostModelApplyError m, MonadWriter [CostModelApplyWarn] m)
=> [Integer] -- ^ the (updated) cost model parameters of the protocol
=> [Int64] -- ^ the (updated) cost model parameters of the protocol
-> m EvaluationContext
mkEvaluationContext = tagWithParamNames @V2.ParamName
>=> pure . toCostModelParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PlutusCore.Default as Plutus (BuiltinSemanticsVariant (DefaultFunSemantic
import Control.Monad
import Control.Monad.Except
import Control.Monad.Writer.Strict
import Data.Int (Int64)

{-| Build the 'EvaluationContext'.
Expand All @@ -30,7 +31,7 @@ IMPORTANT: The evaluation context of every Plutus version must be recreated upon
a protocol update with the updated cost model parameters.
-}
mkEvaluationContext :: (MonadError CostModelApplyError m, MonadWriter [CostModelApplyWarn] m)
=> [Integer] -- ^ the (updated) cost model parameters of the protocol
=> [Int64] -- ^ the (updated) cost model parameters of the protocol
-> m EvaluationContext
mkEvaluationContext = tagWithParamNames @V3.ParamName
>=> pure . toCostModelParams
Expand Down
3 changes: 2 additions & 1 deletion plutus-ledger-api/test/Spec/NoThunks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import PlutusCore.Pretty
import Control.Monad.Except
import Control.Monad.Extra (whenJust)
import Control.Monad.Writer.Strict
import Data.Int (Int64)
import Data.List.Extra (enumerate)
import Data.Map qualified as Map
import Data.Maybe (fromJust)
Expand All @@ -30,7 +31,7 @@ tests =
, testCase "EvaluationContext V3" evaluationContextV3
]

costParams :: [Integer]
costParams :: [Int64]
costParams = Map.elems (fromJust defaultCostModelParams)

evaluationContextV1 :: Assertion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import PlutusLedgerApi.Common as Common

import Barbies
import Data.Functor.Identity
import Data.Int (Int64)
import Data.Map qualified as Map

-- A lifted cost model to `Maybe`, so we can easily clear some of its fields when extracting JSON.
Expand Down Expand Up @@ -46,7 +47,7 @@ Here, overconstrained to `MCostModel`, but it could also work with `CostModel mc
-}
extractCostModelParamsLedgerOrder :: (Common.IsParamName p, Ord p)
=> MCostModel
-> Maybe (Map.Map p Integer)
-> Maybe (Map.Map p Int64)
extractCostModelParamsLedgerOrder =
extractInAlphaOrder
>=> toLedgerOrder
Expand All @@ -59,5 +60,3 @@ extractCostModelParamsLedgerOrder =

viaListM op = fmap Map.fromList . op . Map.toList
firstM f (k,v) = (,v) <$> f k


Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import PlutusLedgerApi.V2 qualified as V2
import Codec.Serialise (Serialise (..))
import Data.ByteString.Base64 qualified as Base64
import Data.ByteString.Short qualified as BS
import Data.Int (Int64)
import Data.List.NonEmpty (NonEmpty, toList)
import Data.Text.Encoding qualified as Text
import GHC.Generics (Generic)
Expand Down Expand Up @@ -93,9 +94,9 @@ instance Pretty ScriptEvaluationEvent where
each `ScriptEvaluationEvent`.
-}
data ScriptEvaluationEvents = ScriptEvaluationEvents
{ eventsCostParamsV1 :: Maybe [Integer]
{ eventsCostParamsV1 :: Maybe [Int64]
-- ^ Cost parameters shared by all PlutusV1 evaluation events in `eventsEvents`, if any.
, eventsCostParamsV2 :: Maybe [Integer]
, eventsCostParamsV2 :: Maybe [Int64]
-- ^ Cost parameters shared by all PlutusV2 evaluation events in `eventsEvents`, if any.
, eventsEvents :: NonEmpty ScriptEvaluationEvent
}
Expand All @@ -106,13 +107,13 @@ data ScriptEvaluationEvents = ScriptEvaluationEvents
data UnexpectedEvaluationResult
= UnexpectedEvaluationSuccess
ScriptEvaluationEvent
[Integer]
[Int64]
-- ^ Cost parameters
ExBudget
-- ^ Actual budget consumed
| UnexpectedEvaluationFailure
ScriptEvaluationEvent
[Integer]
[Int64]
-- ^ Cost parameters
EvaluationError
| DecodeError ScriptDecodeError
Expand Down Expand Up @@ -166,7 +167,7 @@ renderTestFailures xs = [fmt|
checkEvaluationEvent ::
EvaluationContext ->
-- | Cost parameters
[Integer] ->
[Int64] ->
ScriptEvaluationEvent ->
Maybe UnexpectedEvaluationResult
checkEvaluationEvent ctx params ev = case ev of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import PlutusLedgerApi.Test.V2.EvaluationContext qualified as V2
import PlutusLedgerApi.V1 qualified as V1
import PlutusPrelude

import Data.Int (Int64)
import Data.Map qualified as Map
import Data.Maybe

-- | Example values of costs for @PlutusV1@, in expected ledger order.
-- Suitable to be used in testing.
costModelParamsForTesting :: [(V1.ParamName, Integer)]
costModelParamsForTesting :: [(V1.ParamName, Int64)]
costModelParamsForTesting = Map.toList $ fromJust $
Common.extractCostModelParamsLedgerOrder mCostModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import PlutusLedgerApi.Test.V3.EvaluationContext qualified as V3
import PlutusLedgerApi.V2 qualified as V2
import PlutusPrelude

import Data.Int (Int64)
import Data.Map qualified as Map
import Data.Maybe

-- | Example values of costs for @PlutusV2@, in expected ledger order.
-- Suitable to be used in testing.
costModelParamsForTesting :: [(V2.ParamName, Integer)]
costModelParamsForTesting :: [(V2.ParamName, Int64)]
costModelParamsForTesting = Map.toList $ fromJust $
Common.extractCostModelParamsLedgerOrder mCostModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import PlutusLedgerApi.Test.Common.EvaluationContext as Common
import PlutusLedgerApi.V3 qualified as V3
import UntypedPlutusCore.Evaluation.Machine.Cek.CekMachineCosts

import Data.Int (Int64)
import Data.Map qualified as Map
import Data.Maybe

-- | Example values of costs for @PlutusV3@, in expected ledger order.
-- Suitable to be used in testing.
costModelParamsForTesting :: [(V3.ParamName, Integer)]
costModelParamsForTesting :: [(V3.ParamName, Int64)]
costModelParamsForTesting = Map.toList $ fromJust $
Common.extractCostModelParamsLedgerOrder mCostModel

Expand Down

0 comments on commit d41e4a2

Please sign in to comment.