Skip to content

Commit

Permalink
Update to latest pact pin, fix gas and tests
Browse files Browse the repository at this point in the history
co-authored-by: chessai<[email protected]>
  • Loading branch information
jmcardon committed Oct 12, 2024
1 parent 68f8237 commit 139c4b8
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 92 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: c51e26607b662e8201b417951c7ee53d711760e2
--sha256: 1qypag6dlm5f922km5bnp9a96rsaqlig66d6d6csnjad6c4x4f40
tag: 0aff0651cf37d463d6d4534a42c037cb00c0bf20
--sha256: 0z6zl9fzsv3c3ig5lxcj6jvffllvg20x21rbvvw8sh4g7rbjs0xc

source-repository-package
type: git
Expand Down
20 changes: 10 additions & 10 deletions src/Chainweb/BlockHeader/Genesis/Pact5Development0Payload.hs

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/Chainweb/BlockHeader/Genesis/Pact5Development1to19Payload.hs

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/Chainweb/BlockHeader/Genesis/Pact5InstantTimedCPM0Payload.hs

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/Chainweb/BlockHeader/Genesis/Pact5InstantTimedCPM1to9Payload.hs

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/Chainweb/BlockHeader/Genesis/Testnet050Payload.hs

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/Chainweb/BlockHeader/Genesis/Testnet051to19Payload.hs

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions src/Chainweb/Pact/Backend/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Chainweb.Pact.Backend.Utils
, convRowKeyCore
, convPactId
, convPactIdCore
, convHashedModuleName
, convSavepointName
, expectSingleRowCol
, expectSingle
Expand Down Expand Up @@ -127,17 +128,11 @@ import qualified Pact.Types.Persistence as Pact4
-- -------------------------------------------------------------------------- --
-- SQ3.Utf8 Encodings
instance AsString (PCore.Domain k v b i) where
asString (PCore.DUserTables t) = asString t
asString PCore.DKeySets = "SYS:KeySets"
asString PCore.DModules = "SYS:Modules"
asString PCore.DNamespaces = "SYS:Namespaces"
asString PCore.DDefPacts = "SYS:Pacts"
asString PCore.DModuleSource = "SYS:ModuleSources"
asString t = PCore.renderDomain t


instance AsString (PCore.TableName) where
asString (PCore.TableName tn (PCore.ModuleName mn ns)) =
maybe "" (\(PCore.NamespaceName n) -> n <> ".") ns <> mn <> "_" <> tn
asString t = PCore.renderTableName t

toUtf8 :: T.Text -> SQ3.Utf8
toUtf8 = SQ3.Utf8 . T.encodeUtf8
Expand Down Expand Up @@ -209,6 +204,9 @@ convPactIdCore pid = "PactId \"" <> toUtf8 (PCore.renderDefPactId pid) <> "\""
convSavepointName :: SavepointName -> SQ3.Utf8
convSavepointName = toTextUtf8

convHashedModuleName :: PCore.HashedModuleName -> SQ3.Utf8
convHashedModuleName = toUtf8 . PCore.renderHashedModuleName

-- -------------------------------------------------------------------------- --
--

Expand Down
4 changes: 3 additions & 1 deletion src/Chainweb/Pact4/Backend/ChainwebPactDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ forModuleNameFix f = view blockHandlerModuleNameFix >>= f
tableExistsInDbAtHeight :: Utf8 -> BlockHeight -> BlockHandler logger Bool
tableExistsInDbAtHeight tableName bh = do
let knownTbls =
["SYS:Pacts", "SYS:Modules", "SYS:KeySets", "SYS:Namespaces"]
["SYS:Pacts", "SYS:Modules", "SYS:KeySets", "SYS:Namespaces", "SYS:ModuleSources"]
if tableName `elem` knownTbls
then return True
else callDb "tableExists" $ \db -> do
Expand Down Expand Up @@ -793,6 +793,7 @@ rewindDbToGenesis db = do
exec_ db "DELETE FROM [SYS:Modules];"
exec_ db "DELETE FROM [SYS:Namespaces];"
exec_ db "DELETE FROM [SYS:Pacts];"
exec_ db "DELETE FROM [SYS:ModuleSources];"
tblNames <- qry_ db "SELECT tablename FROM VersionedTableCreation;" [RText]
forM_ tblNames $ \t -> case t of
[SText tn] -> exec_ db ("DROP TABLE [" <> tn <> "];")
Expand Down Expand Up @@ -862,6 +863,7 @@ rewindDbToBlock db bh endingTxId = do
exec' db "DELETE FROM [SYS:Modules] WHERE txid >= ?" tx
exec' db "DELETE FROM [SYS:Namespaces] WHERE txid >= ?" tx
exec' db "DELETE FROM [SYS:Pacts] WHERE txid >= ?" tx
exec' db "DELETE FROM [SYS:ModuleSources] WHERE txid >= ?" tx
where
tx = [SInt $! fromIntegral endingTxId]

Expand Down
23 changes: 19 additions & 4 deletions src/Chainweb/Pact5/Backend/ChainwebPactDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import Pact.Core.Names
import Pact.Core.Builtin
import Pact.Core.Guards
import Pact.Core.Errors
import Pact.Core.Gas

-- chainweb

Expand Down Expand Up @@ -281,14 +282,15 @@ doReadRow mlim d k = forModuleNameFix $ \mnFix ->
-- TODO: This is incomplete (the modules case), due to namespace
-- resolution concerns
DModules -> let f = (\v -> (view document <$> _decodeModuleData serialisePact_lineinfo v)) in
lookupWithKey (convModuleNameCore mnFix k) f (noCache f)
lookupWithKey (convModuleNameCore mnFix k) f (noCacheChargeModuleSize f)
DNamespaces -> let f = (\v -> (view document <$> _decodeNamespace serialisePact_lineinfo v)) in
lookupWithKey (convNamespaceNameCore k) f (noCache f)
DUserTables _ -> let f = (\v -> (view document <$> _decodeRowData serialisePact_lineinfo v)) in
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"
DModuleSource -> let f = (\v -> (view document <$> _decodeModuleCode serialisePact_lineinfo v)) in
lookupWithKey (convHashedModuleName k) f (noCache f)
where
tablename@(Utf8 tableNameBS) = domainTableNameCore d

Expand Down Expand Up @@ -343,6 +345,15 @@ doReadRow mlim d k = forModuleNameFix $ \mnFix ->
"doReadRow: Expected (at most) a single result, but got: " <>
T.pack (show err)

noCacheChargeModuleSize
:: (BS.ByteString -> Maybe (ModuleData CoreBuiltin Info))
-> Utf8
-> BS.ByteString
-> MaybeT (BlockHandler logger) (ModuleData CoreBuiltin Info)
noCacheChargeModuleSize f _key rowdata = do
lift $ BlockHandler $ lift $ lift (chargeGasM (GModuleOp (MOpLoadModule (BS.length rowdata))))
MaybeT $ return $! f rowdata

noCache
:: (BS.ByteString -> Maybe v)
-> Utf8
Expand Down Expand Up @@ -372,7 +383,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"
DModuleSource -> (convHashedModuleName k, _encodeModuleCode serialisePact_lineinfo v)
DUserTables _ -> error "impossible"
recordPendingUpdate kk (toUtf8 tablename) txid vv
recordTxLog d kk vv
Expand Down Expand Up @@ -492,7 +503,11 @@ doKeys mlim d = do
Just v -> pure v
DNamespaces -> pure $ map NamespaceName allKeys
DDefPacts -> pure $ map DefPactId allKeys
DModuleSource -> internalError "doKeys: DModuleSource not supported"
DModuleSource -> do
let parsed = map parseHashedModuleName allKeys
case sequence parsed of
Just v -> pure v
Nothing -> internalError $ "doKeys.DModuleSources: unexpected decoding"
DUserTables _ -> pure $ map RowKey allKeys

where
Expand Down
34 changes: 17 additions & 17 deletions test/Chainweb/Test/Pact5/TransactionExecTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ runPayloadShouldReturnEvalResultRelatedToTheInputCommand rdb = readFromAfterGene

liftIO $ assertEqual
"eval result"
(MilliGas 2_000, Right EvalResult
(MilliGas 3_000, Right EvalResult
{ _erOutput = [InterpretValue (PInteger 15) def]
, _erEvents = []
, _erLogs = []
, _erExec = Nothing
, _erGas = Gas 2
, _erGas = Gas 3
, _erLoadedModules = mempty
, _erTxId = Just (TxId 9)
, _erLogGas = Nothing
Expand Down Expand Up @@ -388,7 +388,7 @@ applyLocalSpec rdb = readFromAfterGenesis v rdb $
[ pt _crEvents ? equals ? []
, pt _crResult ? equals ? PactResultOk (PInteger 15)
-- reflects payload gas usage
, pt _crGas ? equals ? Gas 2
, pt _crGas ? traceFailShow ? equals ? Gas 3
, pt _crContinuation ? equals ? Nothing
, pt _crLogs ? equals ? Just []
, pt _crMetaData ? match _Just continue
Expand Down Expand Up @@ -419,19 +419,19 @@ applyCmdSpec rdb = readFromAfterGenesis v rdb $
, _cbGasLimit = GasLimit (Gas 500)
}
let txCtx = TxContext {_tcParentHeader = ParentHeader (gh v cid), _tcMiner = noMiner}
let expectedGasConsumed = 162
let expectedGasConsumed = 155
applyCmd stdoutDummyLogger Nothing pactDb txCtx noSPVSupport (Gas 1) (view payloadObj <$> cmd)
>>= match _Right
? satAll
-- only the event reflecting the final transfer to the miner for gas used
[ pt _crEvents ? soleElement ?
event
(equals "TRANSFER")
(equals [PString "sender00", PString "NoMiner", PDecimal 324.0])
(traceFailShow $ equals [PString "sender00", PString "NoMiner", PDecimal 310.0])
(equals coinModuleName)
, pt _crResult ? equals ? PactResultOk (PInteger 15)
-- reflects buyGas gas usage, as well as that of the payload
, pt _crGas ? equals ? Gas expectedGasConsumed
, pt _crGas ? traceFailShow ? equals ? Gas expectedGasConsumed
, pt _crContinuation ? equals ? Nothing
, pt _crLogs ? match _Just ?
PT.list
Expand Down Expand Up @@ -490,12 +490,12 @@ applyCmdVerifierSpec rdb = readFromAfterGenesis v rdb $
[ pt _crEvents ? PT.list
[ event
(equals "TRANSFER")
(equals [PString "sender00", PString "NoMiner", PDecimal 120322])
(traceFailShow ? equals [PString "sender00", PString "NoMiner", PDecimal 730])
(equals coinModuleName)
]
, pt _crResult ? equals ? PactResultOk (PString "Loaded module free.m, hash HcrMpd9fcNbb6fRz07frqtmke2FqRvtXTuV-xTE_NIk")
, pt _crResult ? traceFailShow ? equals ? PactResultOk (PString "Loaded module free.m, hash Uj0lQPPu9CKvw13K4VP4DZoaPKOphk_-vuq823hLSLo")
-- reflects buyGas gas usage, as well as that of the payload
, pt _crGas ? equals ? Gas 60161
, pt _crGas ? traceFailShow ? equals ? Gas 365
, pt _crContinuation ? equals ? Nothing
]

Expand Down Expand Up @@ -555,12 +555,12 @@ applyCmdVerifierSpec rdb = readFromAfterGenesis v rdb $
[ pt _crEvents ? PT.list
[ event
(equals "TRANSFER")
(equals [PString "sender00", PString "NoMiner", PDecimal 342])
(traceFailShow ? equals [PString "sender00", PString "NoMiner", PDecimal 326])
(equals coinModuleName)
]
, pt _crResult ? equals ? PactResultOk (PInteger 1)
-- reflects buyGas gas usage, as well as that of the payload
, pt _crGas ? equals ? Gas 171
, pt _crGas ? traceFailShow ? equals ? Gas 163
, pt _crContinuation ? equals ? Nothing
, pt _crMetaData ? equals ? Nothing
]
Expand Down Expand Up @@ -643,7 +643,7 @@ applyCmdCoinTransfer rdb = readFromAfterGenesis v rdb $ do
, _cbGasLimit = GasLimit (Gas 600)
}
-- Note: if/when core changes gas prices, tweak here.
let expectedGasConsumed = 514
let expectedGasConsumed = 420
applyCmd stdoutDummyLogger Nothing pactDb txCtx noSPVSupport (Gas 1) (view payloadObj <$> cmd)
>>= match _Right
? satAll
Expand All @@ -655,12 +655,12 @@ applyCmdCoinTransfer rdb = readFromAfterGenesis v rdb $ do
(equals coinModuleName)
, event
(equals "TRANSFER")
(traceFailShow (equals [PString "sender00", PString "NoMiner", PDecimal 1028]))
(traceFailShow (equals [PString "sender00", PString "NoMiner", PDecimal 840]))
(equals coinModuleName)
]
, pt _crResult ? equals ? PactResultOk (PString "Write succeeded")
-- reflects buyGas gas usage, as well as that of the payload
, pt _crGas ? equals ? Gas expectedGasConsumed
, pt _crGas ? traceFailShow ? equals ? Gas expectedGasConsumed
, pt _crContinuation ? equals ? Nothing
, pt _crLogs ? match _Just ?
PT.list
Expand Down Expand Up @@ -691,7 +691,7 @@ applyCmdCoinTransfer rdb = readFromAfterGenesis v rdb $ do
]

endSender00Bal <- readBal pactDb "sender00"
assertEqual "ending balance should be less gas money" (Just 99_998_552) endSender00Bal
assertEqual "ending balance should be less gas money" (Just 99_998_740) endSender00Bal
endMinerBal <- readBal pactDb "NoMiner"
assertEqual "miner balance after redeeming gas should have increased"
(Just $ fromMaybe 0 startMinerBal + (fromIntegral expectedGasConsumed * 2))
Expand Down Expand Up @@ -736,7 +736,7 @@ testCoinUpgrade rdb = readFromAfterGenesis vUpgrades rdb $ do
applyUpgrades stdoutDummyLogger pactDb txCtx

getCoinModuleHash txCtx pactDb
>>= equals ? PactResultOk (PString "xMiQPBR-dj7VT1RTAM72kyhtz-V-PctgvKsgDRmKUcM")
>>= traceFailShow ? equals ? PactResultOk (PString "3DevS3OdJDZkXO5VwI-PclwK_gTQ37E9Vfl8UmKixfs")
where
getCoinModuleHash txCtx pactDb = do
cmd <- buildCwCmd vUpgrades defaultCmd
Expand Down Expand Up @@ -781,7 +781,7 @@ testEventOrdering rdb = readFromAfterGenesis v rdb $
(equals coinModuleName)
, event
(equals "TRANSFER")
(equals [PString "sender00", PString "NoMiner", PDecimal 1734])
(traceFailShow $ equals [PString "sender00", PString "NoMiner", PDecimal 1376])
(equals coinModuleName)
]
]
Expand Down

0 comments on commit 139c4b8

Please sign in to comment.