Skip to content

Commit

Permalink
wip: separation pact4 and pact5
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Akentev committed Jun 12, 2024
1 parent 2ed7307 commit e33e959
Show file tree
Hide file tree
Showing 7 changed files with 1,424 additions and 837 deletions.
6 changes: 4 additions & 2 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ library
, Chainweb.Pact.Service.PactInProcApi
, Chainweb.Pact.Service.PactQueue
, Chainweb.Pact.Service.Types
, Chainweb.Pact.Templates
, Chainweb.Pact.TransactionExec
, Chainweb.Pact.Templates.Pact4
, Chainweb.Pact.Templates.Pact5
, Chainweb.Pact.TransactionExec.Pact4
, Chainweb.Pact.TransactionExec.Pact5
, Chainweb.Pact.Transactions.FungibleV2Transactions
, Chainweb.Pact.Transactions.CoinV3Transactions
, Chainweb.Pact.Transactions.CoinV4Transactions
Expand Down
8 changes: 6 additions & 2 deletions src/Chainweb/Pact/PactService/ExecBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ import Chainweb.Pact.Backend.Types
import Chainweb.Pact.NoCoinbase
import Chainweb.Pact.Service.Types
import Chainweb.Pact.SPV
import Chainweb.Pact.TransactionExec
import Chainweb.Pact.TransactionExec.Pact4
import qualified Chainweb.Pact.TransactionExec.Pact5 as Pact5
import Chainweb.Pact.Types
import Chainweb.Pact.Validations
import Chainweb.Payload
Expand All @@ -116,6 +117,9 @@ execBlock currHeader payload = do
let plData = checkablePayloadToPayloadData payload
dbEnv <- view psBlockDbEnv
miner <- decodeStrictOrThrow' (_minerData $ _payloadDataMiner plData)

-- if

trans <- liftIO $ pact4TransactionsFromPayload
(pactParserVersion v (_blockChainId currHeader) (_blockHeight currHeader))
plData
Expand Down Expand Up @@ -303,7 +307,7 @@ execTransactions
-> CoinbaseUsePrecompiled
-> Maybe P.Gas
-> Maybe Micros
-> PactBlockM logger tbl (Transactions (Either CommandInvalidError (Either (P.CommandResult [P.TxLogJson]) PCore.CommandResult)))
-> PactBlockM logger tbl (Transactions (Either CommandInvalidError (P.CommandResult [P.TxLogJson])))
execTransactions isGenesis miner ctxs enfCBFail usePrecomp gasLimit timeLimit = do
mc <- initModuleCacheForBlock isGenesis
-- for legacy reasons (ask Emily) we don't use the module cache resulting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@
--
-- Prebuilt Term templates for automated operations (coinbase, gas buy)
--
module Chainweb.Pact.Templates
module Chainweb.Pact.Templates.Pact4
( mkFundTxTerm
, mkBuyGasTerm
, mkRedeemGasTerm
, mkCoinbaseTerm

, mkFundTxCoreTerm
, mkBuyGasCoreTerm
, mkRedeemGasCoreTerm
, mkCoinbaseCoreTerm

, mkCoinbaseCmd
) where

Expand All @@ -48,12 +43,6 @@ import Chainweb.Miner.Pact
import Chainweb.Pact.Types
import Chainweb.Pact.Service.Types

import qualified Pact.Core.Literal as Core
import qualified Pact.Core.Names as Core
import qualified Pact.Core.Info as PCore
import qualified Pact.Core.Syntax.ParseTree as CoreLisp


inf :: Info
inf = Info $ Just (Code "",Parsed (Columns 0 0) 0)
{-# NOINLINE inf #-}
Expand Down Expand Up @@ -100,22 +89,6 @@ buyGasTemplate =
, strArgSetter 0
)

fundTxTemplateCore :: Text -> Text -> CoreLisp.Expr PCore.SpanInfo
fundTxTemplateCore sender mid =
let senderTerm = coreStrLit sender
midTerm = coreStrLit mid
varApp = coreQn "fund-tx" "coin"
rks = coreApp (coreBn "read-keyset") [coreStrLit "miner-keyset"]
rds = coreApp (coreBn "read-decimal") [coreStrLit "total"]
in coreApp varApp [senderTerm, midTerm, rks, rds]

buyGasTemplateCore :: Text -> CoreLisp.Expr PCore.SpanInfo
buyGasTemplateCore sender =
let senderTerm = coreStrLit sender
varApp = coreQn "buy-gas" "coin"
rds = coreApp (coreBn "read-decimal") [coreStrLit "total"]
in coreApp varApp [senderTerm, rds]

redeemGasTemplate :: (Term Name, ASetter' (Term Name) Text, ASetter' (Term Name) Text)
redeemGasTemplate =
( app (qn "coin" "redeem-gas")
Expand All @@ -128,27 +101,6 @@ redeemGasTemplate =
, strArgSetter 0
)

redeemGasTemplateCore :: Text -> Text -> CoreLisp.Expr PCore.SpanInfo
redeemGasTemplateCore mid sender =
let midTerm = coreStrLit mid
senderTerm = coreStrLit sender
varApp = coreQn "redeem-gas" "coin"
rks = coreApp (coreBn "read-keyset") [coreStrLit "miner-keyset"]
rds = coreApp (coreBn "read-decimal") [coreStrLit "total"]
in coreApp varApp [midTerm, rks, senderTerm, rds]

coreApp :: CoreLisp.Expr PCore.SpanInfo -> [CoreLisp.Expr PCore.SpanInfo] -> CoreLisp.Expr PCore.SpanInfo
coreApp arg args = CoreLisp.App arg args def

coreStrLit :: Text -> CoreLisp.Expr PCore.SpanInfo
coreStrLit txt = CoreLisp.Constant (Core.LString txt) def

coreQn :: Text -> Text -> CoreLisp.Expr PCore.SpanInfo
coreQn name modname = CoreLisp.Var (Core.QN (Core.QualifiedName name (Core.ModuleName modname Nothing))) def

coreBn :: Text -> CoreLisp.Expr PCore.SpanInfo
coreBn name = CoreLisp.Var (Core.BN (Core.BareName name)) def

dummyParsedCode :: ParsedCode
dummyParsedCode = ParsedCode "1" [ELiteral $ LiteralExp (LInteger 1) def]
{-# NOINLINE dummyParsedCode #-}
Expand Down Expand Up @@ -200,26 +152,6 @@ mkRedeemGasTerm (MinerId mid) (MinerKeys ks) sender total fee = (populatedTerm,
]
{-# INLINABLE mkRedeemGasTerm #-}

mkFundTxCoreTerm
:: MinerId -- ^ Id of the miner to fund
-> Text -- ^ Address of the sender from the command
-> CoreLisp.Expr PCore.SpanInfo
mkFundTxCoreTerm (MinerId mid) sender = fundTxTemplateCore sender mid
{-# INLINABLE mkFundTxCoreTerm #-}

mkBuyGasCoreTerm
:: Text -- ^ Address of the sender from the command
-> CoreLisp.Expr PCore.SpanInfo
mkBuyGasCoreTerm sender = buyGasTemplateCore sender
{-# INLINABLE mkBuyGasCoreTerm #-}

mkRedeemGasCoreTerm
:: MinerId -- ^ Id of the miner to fund
-> Text -- ^ Address of the sender from the command
-> CoreLisp.Expr PCore.SpanInfo
mkRedeemGasCoreTerm (MinerId mid) sender = redeemGasTemplateCore mid sender
{-# INLINABLE mkRedeemGasCoreTerm #-}

coinbaseTemplate :: (Term Name,ASetter' (Term Name) Text)
coinbaseTemplate =
( app (qn "coin" "coinbase")
Expand All @@ -231,14 +163,6 @@ coinbaseTemplate =
)
{-# NOINLINE coinbaseTemplate #-}

coinbaseTemplateCore :: Text -> CoreLisp.Expr PCore.SpanInfo
coinbaseTemplateCore mid =
let midTerm = coreStrLit mid
varApp = coreQn "coinbase" "coin"
rks = coreApp (coreBn "read-keyset") [coreStrLit "miner-keyset"]
rds = coreApp (coreBn "read-decimal") [coreStrLit "reward"]
in coreApp varApp [midTerm, rks, rds]

mkCoinbaseTerm :: MinerId -> MinerKeys -> ParsedDecimal -> (Term Name,ExecMsg ParsedCode)
mkCoinbaseTerm (MinerId mid) (MinerKeys ks) reward = (populatedTerm, execMsg)
where
Expand All @@ -251,12 +175,6 @@ mkCoinbaseTerm (MinerId mid) (MinerKeys ks) reward = (populatedTerm, execMsg)
]
{-# INLINABLE mkCoinbaseTerm #-}

mkCoinbaseCoreTerm
:: MinerId -- ^ Id of the miner to fund
-> CoreLisp.Expr PCore.SpanInfo
mkCoinbaseCoreTerm (MinerId mid) = coinbaseTemplateCore mid
{-# INLINABLE mkCoinbaseCoreTerm #-}

-- | "Old method" to build a coinbase 'ExecMsg' for back-compat.
--
mkCoinbaseCmd :: MinerId -> MinerKeys -> ParsedDecimal -> IO (ExecMsg ParsedCode)
Expand Down
146 changes: 146 additions & 0 deletions src/Chainweb/Pact/Templates/Pact5.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Module : Chainweb.Pact.Templates
-- Copyright : Copyright © 2010 Kadena LLC.
-- License : (see the file LICENSE)
-- Maintainer : Stuart Popejoy
-- Stability : experimental
--
-- Prebuilt Term templates for automated operations (coinbase, gas buy)
--
module Chainweb.Pact.Templates.Pact5
( mkFundTxTerm
, mkBuyGasTerm
, mkRedeemGasTerm
, mkCoinbaseTerm
) where


import Control.Lens
import Data.Default (def)
import Data.Decimal
import Data.Text (Text, pack)

import Text.Trifecta.Delta (Delta(..))

-- internal modules

import qualified Pact.Types.RPC as Pact4
import qualified Pact.JSON.Encode as J
import qualified Pact.JSON.Legacy.Value as J

import Chainweb.Miner.Pact
import Chainweb.Pact.Types
import Chainweb.Pact.Service.Types

import Pact.Core.Evaluate
import Pact.Core.Literal
import Pact.Core.Names
import Pact.Core.Info
import Pact.Core.Syntax.ParseTree

fundTxTemplate :: Text -> Text -> Expr SpanInfo
fundTxTemplate sender mid =
let senderTerm = strLit sender
midTerm = strLit mid
varApp = qn "fund-tx" "coin"
rks = app (bn "read-keyset") [strLit "miner-keyset"]
rds = app (bn "read-decimal") [strLit "total"]
in app varApp [senderTerm, midTerm, rks, rds]

buyGasTemplate :: Text -> Expr SpanInfo
buyGasTemplate sender =
let senderTerm = strLit sender
varApp = qn "buy-gas" "coin"
rds = app (bn "read-decimal") [strLit "total"]
in app varApp [senderTerm, rds]

redeemGasTemplate :: Text -> Text -> Expr SpanInfo
redeemGasTemplate mid sender =
let midTerm = strLit mid
senderTerm = strLit sender
varApp = qn "redeem-gas" "coin"
rks = app (bn "read-keyset") [strLit "miner-keyset"]
rds = app (bn "read-decimal") [strLit "total"]
in app varApp [midTerm, rks, senderTerm, rds]

app :: Expr SpanInfo -> [Expr SpanInfo] -> Expr SpanInfo
app arg args = App arg args def

strLit :: Text -> Expr SpanInfo
strLit txt = Constant (LString txt) def

qn :: Text -> Text -> Expr SpanInfo
qn name modname = Var (QN (QualifiedName name (ModuleName modname Nothing))) def

bn :: Text -> Expr SpanInfo
bn name = Var (BN (BareName name)) def

mkFundTxTerm
:: MinerId -- ^ Id of the miner to fund
-> MinerKeys
-> Text -- ^ Address of the sender from the command
-> GasSupply
-> (Expr SpanInfo, Pact4.ExecMsg RawCode)
mkFundTxTerm (MinerId mid) (MinerKeys ks) sender total =
let
term = fundTxTemplate sender mid
buyGasData = J.object
[ "miner-keyset" J..= ks
, "total" J..= total
]
execMsg = Pact4.ExecMsg (RawCode "") (J.toLegacyJsonViaEncode buyGasData)
in (term, execMsg)
{-# INLINABLE mkFundTxTerm #-}

mkBuyGasTerm
:: Text -- ^ Address of the sender from the command
-> GasSupply
-> (Expr SpanInfo, Pact4.ExecMsg RawCode)
mkBuyGasTerm sender total = (buyGasTemplate sender, execMsg)
where
execMsg = Pact4.ExecMsg (RawCode "") (J.toLegacyJsonViaEncode buyGasData)
buyGasData = J.object
[ "total" J..= total ]
{-# INLINABLE mkBuyGasTerm #-}

mkRedeemGasTerm
:: MinerId -- ^ Id of the miner to fund
-> MinerKeys -- ^ Miner keyset
-> Text -- ^ Address of the sender from the command
-> GasSupply -- ^ The gas limit total * price
-> GasSupply -- ^ The gas used * price
-> (Expr SpanInfo, Pact4.ExecMsg RawCode)
mkRedeemGasTerm (MinerId mid) (MinerKeys ks) sender total fee = (redeemGasTemplate mid sender, execMsg)
where
execMsg = Pact4.ExecMsg (RawCode "") (J.toLegacyJsonViaEncode redeemGasData)
redeemGasData = J.object
[ "total" J..= total
, "fee" J..= J.toJsonViaEncode fee
, "miner-keyset" J..= ks
]
{-# INLINABLE mkRedeemGasTerm #-}

coinbaseTemplate :: Text -> Expr SpanInfo
coinbaseTemplate mid =
let midTerm = strLit mid
varApp = qn "coinbase" "coin"
rks = app (bn "read-keyset") [strLit "miner-keyset"]
rds = app (bn "read-decimal") [strLit "reward"]
in app varApp [midTerm, rks, rds]

mkCoinbaseTerm :: MinerId -> MinerKeys -> GasSupply -> (Expr SpanInfo, Pact4.ExecMsg RawCode)
mkCoinbaseTerm (MinerId mid) (MinerKeys ks) reward = (coinbaseTemplate mid, execMsg)
where
execMsg = Pact4.ExecMsg (RawCode "") (J.toLegacyJsonViaEncode coinbaseData)
coinbaseData = J.object
[ "miner-keyset" J..= ks
, "reward" J..= reward
]
{-# INLINABLE mkCoinbaseTerm #-}
Loading

0 comments on commit e33e959

Please sign in to comment.