Skip to content

Commit

Permalink
WIP gasM
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcardon committed May 15, 2024
1 parent 2d05576 commit 3d8ea62
Show file tree
Hide file tree
Showing 12 changed files with 454 additions and 350 deletions.
5 changes: 4 additions & 1 deletion pact-tng.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ common pact-common
FlexibleInstances
NumericUnderscores
TypeOperators
PartialTypeSignatures

-- internal crypto lirbary
library pact-crypto
Expand Down Expand Up @@ -156,6 +157,8 @@ library
Pact.Core.Info
Pact.Core.Errors
Pact.Core.Persistence
Pact.Core.Persistence.Types
Pact.Core.Persistence.Utils
Pact.Core.Persistence.SQLite
Pact.Core.Persistence.MockPersistence
Pact.Core.PactValue
Expand Down Expand Up @@ -278,7 +281,7 @@ benchmark bench
, mtl
, pact-tng
, text
ghc-options: -WAll -threaded -rtsopts "-with-rtsopts=-N"
ghc-options: -WAll -threaded -rtsopts "-with-rtsopts=-N"
hs-source-dirs: bench
default-language: Haskell2010

Expand Down
6 changes: 3 additions & 3 deletions pact/Pact/Core/Environment/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import Control.Monad.Catch as Exceptions
import qualified Data.Text as T
import qualified Data.Map.Strict as M

import Pact.Core.Persistence
import Pact.Core.Persistence.Types
import Pact.Core.Pretty
import Pact.Core.Capabilities
import Pact.Core.Guards
Expand Down Expand Up @@ -241,9 +241,9 @@ defaultEvalEnv pdb m = do

type Bytes = Word64

newtype SizeOfByteLimit
newtype SizeOfByteLimit
= SizeOfByteLimit Bytes
deriving Show

instance Pretty SizeOfByteLimit where
pretty = pretty . show
pretty = pretty . show
2 changes: 1 addition & 1 deletion pact/Pact/Core/Environment/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import qualified Data.Map.Strict as M
import qualified Data.Set as S

import Pact.Core.Names
import Pact.Core.Persistence
import Pact.Core.Persistence.Types
import Pact.Core.IR.Term
import Pact.Core.Errors
import Pact.Core.Environment.Types
Expand Down
34 changes: 33 additions & 1 deletion pact/Pact/Core/Gas.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}

module Pact.Core.Gas
( MilliGas(..)
Expand Down Expand Up @@ -35,10 +36,16 @@ module Pact.Core.Gas
, GasObjectSize(..)
, ComparisonType(..)
, SearchType(..)
, GasMEnv(..)
, GasM(..)
, runGasM
) where

import Control.Lens
import Control.DeepSeq
import Control.Monad.Reader
import Control.Monad.Except
import Data.IORef
import Data.Decimal(Decimal)
import Data.Word(Word64)
import Data.Monoid(Sum(..))
Expand Down Expand Up @@ -121,7 +128,9 @@ data ZKGroup
-- ^ Group one, that is Fq in Pairing
| ZKG2
-- ^ Group two, that is, Fq2 Pairing
deriving (Show, Generic, NFData)
deriving (Show, Generic)

instance NFData ZKGroup

data ZKArg
= PointAdd !ZKGroup
Expand Down Expand Up @@ -262,3 +271,26 @@ gasToMilliGas (Gas n) = MilliGas (n * millisPerGas)
milliGasToGas :: MilliGas -> Gas
milliGasToGas (MilliGas n) = Gas (n `quot` millisPerGas)
{-# INLINE milliGasToGas #-}

data GasMEnv
= GasMEnv
{ _gasMRef :: IORef MilliGas
, _gasMLimit :: MilliGasLimit
}

newtype GasM e a
= GasM (ReaderT GasMEnv (ExceptT e IO) a)
deriving
( Functor
, Applicative
, Monad
, MonadReader GasMEnv
, MonadError e
, MonadIO) via (ReaderT GasMEnv (ExceptT e IO))

runGasM
:: GasMEnv
-> GasM e a
-> IO (Either e a)
runGasM env (GasM m) =
runExceptT $ runReaderT m env
3 changes: 2 additions & 1 deletion pact/Pact/Core/IR/Eval/Runtime/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ readOnlyEnv e
PactDb
{ _pdbPurity = PReadOnly
, _pdbRead = _pdbRead pdb
, _pdbWrite = \_ _ _ _ _ -> dbOpDisallowed2
, _pdbWrite = \info _wt _d _k _v ->
throwError (PEExecutionError (DbOpFailure OpDisallowed) info)
, _pdbKeys = \_ -> dbOpDisallowed
, _pdbCreateUserTable = \_ -> dbOpDisallowed
, _pdbBeginTx = \_ -> dbOpDisallowed
Expand Down

0 comments on commit 3d8ea62

Please sign in to comment.