Skip to content

Commit

Permalink
WIP apply jose's stuff
Browse files Browse the repository at this point in the history
Change-Id: I0a43fe7a86423a70d6cee719fe662c985fa9abc8
  • Loading branch information
chessai committed Oct 11, 2024
1 parent 928f604 commit 68f8237
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/kadena-io/pact-5.git
tag: 053fe5416b9048c7a12ea0c0e7f8e79c7f3c8dd3
--sha256: 1gyghzk9cajg3br0gwfpr2ci43k4m76rhwg66rhgb2jskq32f38v
tag: c51e26607b662e8201b417951c7ee53d711760e2
--sha256: 1qypag6dlm5f922km5bnp9a96rsaqlig66d6d6csnjad6c4x4f40

source-repository-package
type: git
Expand Down
1 change: 1 addition & 0 deletions src/Chainweb/Pact/Backend/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ instance AsString (PCore.Domain k v b i) where
asString PCore.DModules = "SYS:Modules"
asString PCore.DNamespaces = "SYS:Namespaces"
asString PCore.DDefPacts = "SYS:Pacts"
asString PCore.DModuleSource = "SYS:ModuleSources"


instance AsString (PCore.TableName) where
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact/PactService/Pact5/ExecBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ continueBlock mpAccess blockInProgress = do
txs
pbBlockHandle .= finalBlockHandle
let (invalidTxHashes, completedTxs) = partitionEithers txResults
let p4FinalRemainingGas = fromIntegral @Word64 @Pact4.GasLimit $ finalRemainingGas ^. Pact5._GasLimit . to Pact5._gas
let p4FinalRemainingGas = fromIntegral @Word64 @Pact4.GasLimit $ finalRemainingGas ^. Pact5._GasLimit . to (P.fromSatWord . Pact5._gas)
return (completedTxs, Pact5.RequestKey <$> invalidTxHashes, p4FinalRemainingGas, timedOut)

getBlockTxs :: BlockFill -> PactBlockM logger tbl (Vector Pact5.Transaction)
Expand Down
4 changes: 4 additions & 0 deletions src/Chainweb/Pact4/Backend/ChainwebPactDb.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
Expand Down Expand Up @@ -57,6 +58,7 @@ module Chainweb.Pact4.Backend.ChainwebPactDb
, mkBlockHandlerEnv
) where

import Pact.Core.Persistence.Types qualified as Pact5
import Control.Applicative
import Control.Lens
import Control.Monad
Expand Down Expand Up @@ -881,6 +883,8 @@ initSchema logger sql =
create (domainTableName Modules)
create (domainTableName Namespaces)
create (domainTableName Pacts)
-- TODO: migrate this logic to the checkpointer itself?
create (toUtf8 $ Pact5.renderDomain Pact5.DModuleSource)
where
create tablename = do
logDebug_ logger $ "initSchema: " <> fromUtf8 tablename
Expand Down
3 changes: 3 additions & 0 deletions src/Chainweb/Pact5/Backend/ChainwebPactDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ doReadRow mlim d k = forModuleNameFix $ \mnFix ->
lookupWithKey (convRowKeyCore k) f (noCache f)
DDefPacts -> let f = (\v -> (view document <$> _decodeDefPactExec serialisePact_lineinfo v)) in
lookupWithKey (convPactIdCore k) f (noCache f)
DModuleSource -> internalError "doReadRow: DModuleSource not supported"
where
tablename@(Utf8 tableNameBS) = domainTableNameCore d

Expand Down Expand Up @@ -371,6 +372,7 @@ writeSys d k v = do
DModules -> (convModuleNameCore mnFix k, _encodeModuleData serialisePact_lineinfo v)
DNamespaces -> (convNamespaceNameCore k, _encodeNamespace serialisePact_lineinfo v)
DDefPacts -> (convPactIdCore k, _encodeDefPactExec serialisePact_lineinfo v)
DModuleSource -> error "writeSys: DModuleSource not supported"
DUserTables _ -> error "impossible"
recordPendingUpdate kk (toUtf8 tablename) txid vv
recordTxLog d kk vv
Expand Down Expand Up @@ -490,6 +492,7 @@ doKeys mlim d = do
Just v -> pure v
DNamespaces -> pure $ map NamespaceName allKeys
DDefPacts -> pure $ map DefPactId allKeys
DModuleSource -> internalError "doKeys: DModuleSource not supported"
DUserTables _ -> pure $ map RowKey allKeys

where
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact5/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fromPact4Command cmd4 = Command
| otherwise = fromIntegral i

fromPact4GasLimit :: Pact4.GasLimit -> GasLimit
fromPact4GasLimit (Pact4.GasLimit i) = GasLimit (Gas (fromPact4ParsedInteger i))
fromPact4GasLimit (Pact4.GasLimit i) = GasLimit (Gas (fromIntegral (fromPact4ParsedInteger i)))

fromPact4GasPrice :: Pact4.GasPrice -> GasPrice
fromPact4GasPrice (Pact4.GasPrice (Pact4.ParsedDecimal d)) = GasPrice d
Expand Down
84 changes: 52 additions & 32 deletions src/Chainweb/Pact5/TransactionExec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}

{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}

-- |
-- Module : Chainweb.Pact.TransactionExec
-- Copyright : Copyright © 2018 Kadena LLC.
Expand Down Expand Up @@ -216,7 +213,7 @@ runVerifiers txCtx cmd = do
verifierResult <- liftIO $ runVerifierPlugins
(_chainwebVersion txCtx, _chainId txCtx, ctxCurrentBlockHeight txCtx) logger
allVerifiers
(Pact4.Gas $ fromIntegral @Word64 @Int64 $ _gas $ milliGasToGas $ initGasRemaining)
(Pact4.Gas $ fromIntegral @SatWord @Int64 $ _gas $ milliGasToGas $ initGasRemaining)
pact4TxVerifiers
case verifierResult of
Left err -> do
Expand All @@ -227,7 +224,7 @@ runVerifiers txCtx cmd = do
-- NB: this is not nice.
-- TODO: better gas info here
chargeGas def $ GAConstant $ gasToMilliGas $ Gas $
verifierGasRemaining - min (_gas (milliGasToGas initGasRemaining)) verifierGasRemaining
fromIntegral @Word64 @SatWord verifierGasRemaining - min (_gas (milliGasToGas initGasRemaining)) (fromIntegral @Word64 @SatWord verifierGasRemaining)

applyLocal
:: (Logger logger)
Expand Down Expand Up @@ -470,10 +467,9 @@ applyCoinbase logger db reward txCtx = do
-- applyCoinbase is when upgrades happen, so we call applyUpgrades first
applyUpgrades logger db txCtx
-- we construct the coinbase term and evaluate it
let
(coinbaseTerm, coinbaseData) = mkCoinbaseTerm mid mks reward
let (coinbaseTerm, coinbaseData) = mkCoinbaseTerm mid mks reward
eCoinbaseTxResult <-
evalExec Transactional
evalExec (RawCode mempty) Transactional
db noSPVSupport freeGasModel (Set.fromList [FlagDisableRuntimeRTC]) SimpleNamespacePolicy
(ctxToPublicData def txCtx)
MsgData
Expand Down Expand Up @@ -608,7 +604,7 @@ runPayload execMode execFlags db spv specialCaps namespacePolicy gasModel txCtx
(either throwError return =<<) $ liftIO $
case payload ^. pPayload of
Exec ExecMsg {..} ->
evalExec execMode
evalExec (RawCode (_pcCode _pmCode)) execMode
db spv gasModel execFlags namespacePolicy
(ctxToPublicData publicMeta txCtx)
MsgData
Expand All @@ -619,9 +615,7 @@ runPayload execMode execFlags db spv specialCaps namespacePolicy gasModel txCtx
}
(def @(CapState _ _)
& csSlots .~ [CapSlot cap [] | cap <- specialCaps])
-- Note: we delete the information here from spans for
-- smaller db footprints
((fmap.fmap) spanInfoToLineInfo (_pcExps _pmCode))
(_pcExps _pmCode)
Continuation ContMsg {..} ->
evalContinuation execMode
db spv gasModel execFlags namespacePolicy
Expand Down Expand Up @@ -660,7 +654,7 @@ runUpgrade
-> IO ()
runUpgrade _logger db txContext cmd = case payload ^. pPayload of
Exec pm ->
evalExec Transactional
evalExec (RawCode (_pcCode (_pmCode pm))) Transactional
db noSPVSupport freeGasModel (Set.fromList [FlagDisableRuntimeRTC])
-- allow installing to root namespace
SimpleNamespacePolicy
Expand Down Expand Up @@ -739,30 +733,50 @@ buyGas logger db txCtx cmd = do
else mkFundTxTerm mid mks sender supply

runExceptT $ do
gasPayerCap <- case gasPayerCaps of
mGasPayerCap <- case gasPayerCaps of
[gasPayerCap] -> pure (Just gasPayerCap)
[] -> pure Nothing
_ -> do
throwError BuyGasMultipleGasPayerCaps

e <- liftIO $ maybe (evalExecTerm Transactional) evalGasPayerCap gasPayerCap
db
noSPVSupport
-- TODO: magic constant, 1500 max gas limit for buyGas?
(tableGasModel (MilliGasLimit $ gasToMilliGas $ min (Gas 3000) (gasLimit ^. _GasLimit)))
(Set.fromList [FlagDisableRuntimeRTC]) SimpleNamespacePolicy
(ctxToPublicData publicMeta txCtx)
MsgData
-- Note: in the case of gaspayer, buyGas is given extra metadata that comes from
-- the Command
{ mdData = maybe (PObject buyGasData) (const $ enrichedMsgBodyForGasPayer buyGasData cmd) gasPayerCap
, mdHash = bgHash
, mdSigners = signersWithDebit
-- no verifiers are allowed in buy gas
, mdVerifiers = []
}
(def & csSlots .~ [CapSlot (coinCap "GAS" []) []])
(def <$ buyGasTerm)
e <- liftIO $ case mGasPayerCap of
Nothing -> evalExecTerm Transactional
db
noSPVSupport
-- TODO: magic constant, 1500 max gas limit for buyGas?
(tableGasModel (MilliGasLimit $ gasToMilliGas $ min (Gas 3000) (gasLimit ^. _GasLimit)))
(Set.fromList [FlagDisableRuntimeRTC]) SimpleNamespacePolicy
(ctxToPublicData publicMeta txCtx)
MsgData
-- Note: in the case of gaspayer, buyGas is given extra metadata that comes from
-- the Command
{ mdData = PObject buyGasData
, mdHash = bgHash
, mdSigners = signersWithDebit
-- no verifiers are allowed in buy gas
, mdVerifiers = []
}
(def & csSlots .~ [CapSlot (coinCap "GAS" []) []])
(def <$ buyGasTerm)
Just gasPayerCap -> evalGasPayerCap
gasPayerCap
db
noSPVSupport
-- TODO: magic constant, 1500 max gas limit for buyGas?
(tableGasModel (MilliGasLimit $ gasToMilliGas $ min (Gas 3000) (gasLimit ^. _GasLimit)))
(Set.fromList [FlagDisableRuntimeRTC]) SimpleNamespacePolicy
(ctxToPublicData publicMeta txCtx)
MsgData
-- Note: in the case of gaspayer, buyGas is given extra metadata that comes from
-- the Command
{ mdData = enrichedMsgBodyForGasPayer buyGasData cmd
, mdHash = bgHash
, mdSigners = signersWithDebit
-- no verifiers are allowed in buy gas
, mdVerifiers = []
}
(def & csSlots .~ [CapSlot (coinCap "GAS" []) []])
(def <$ buyGasTerm)

case e of
Right er' -> do
Expand Down Expand Up @@ -820,6 +834,7 @@ redeemGas logger db txCtx gasUsed maybeFundTxPactId cmd
let (redeemGasTerm, redeemGasData) = mkRedeemGasTerm mid mks sender gasTotal gasFee

evalExec
(commandToRawCode cmd)
Transactional
-- TODO: more execution flags?
db noSPVSupport freeGasModel (Set.fromList [FlagDisableRuntimeRTC]) SimpleNamespacePolicy
Expand Down Expand Up @@ -934,3 +949,8 @@ gasSupplyOf (Gas gas) (GasPrice gp) = GasSupply gs
toCoinUnit :: Decimal -> Decimal
toCoinUnit = roundTo 12
{-# INLINE toCoinUnit #-}

commandToRawCode :: Command (Payload PublicMeta ParsedCode) -> RawCode
commandToRawCode cmd = RawCode $ case _pPayload $ _cmdPayload cmd of
Exec exec -> _pcCode $ _pmCode exec
Continuation _cont -> mempty
4 changes: 2 additions & 2 deletions src/Chainweb/Pact5/Validations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import Data.Either (isRight)
import Data.List.NonEmpty (NonEmpty, nonEmpty)
import Data.Text (Text)
import qualified Data.ByteString.Short as SBS
import Data.Word (Word8, Word64)
import Data.Word (Word8)

-- internal modules

Expand Down Expand Up @@ -82,7 +82,7 @@ assertLocalMetadata cmd@(P.Command pay sigs hsh) txCtx sigVerify = do
[ eUnless "Chain id mismatch" $ assertChainId cid pcid
-- TODO: use failing conversion
, eUnless "Transaction Gas limit exceeds block gas limit"
$ assertBlockGasLimit (P.GasLimit $ P.Gas (fromIntegral @Integer @Word64 bgl)) gl
$ assertBlockGasLimit (P.GasLimit $ P.Gas (fromIntegral @Integer @P.SatWord bgl)) gl
, eUnless "Gas price decimal precision too high" $ assertGasPrice gp
, eUnless "Network id mismatch" $ assertNetworkId v nid
, eUnless "Signature list size too big" $ assertSigSize sigs
Expand Down

0 comments on commit 68f8237

Please sign in to comment.