Skip to content

Commit

Permalink
Calculate minimal dependency set on module deploys
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcardon committed Feb 14, 2025
1 parent 6a0cf3e commit ed950a5
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 90 deletions.
11 changes: 0 additions & 11 deletions pact-repl/Pact/Core/IR/Eval/Direct/ReplBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Control.Lens
import Control.Monad(when)
import Control.Monad.State.Strict
import Control.Monad.Except
import Data.Default
import Data.Either(partitionEithers)
import Data.Text(Text)
import Data.Maybe(fromMaybe)
Expand Down Expand Up @@ -323,16 +322,6 @@ begin' info mt = do
replTx .== ((,mt) <$> mTxId)
return ((,mt) <$> mTxId)

emptyTxState :: ReplM b ()
emptyTxState = do
fqdefs <- use (esLoaded . loAllLoaded)
cs <- use esStack
esc <- use esCheckRecursion
let newEvalState =
set esStack cs
$ set (esLoaded . loAllLoaded) fqdefs
$ set esCheckRecursion esc def
put newEvalState

envSetDebug :: NativeFunction 'ReplRuntime ReplCoreBuiltin FileLocSpanInfo
envSetDebug info b _env = \case
Expand Down
13 changes: 0 additions & 13 deletions pact-repl/Pact/Core/Repl/Runtime/ReplBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Control.Lens
import Control.Monad(when)
import Control.Monad.Except
import Control.Monad.State.Strict
import Data.Default
import Data.Text(Text)
import Data.Maybe(fromMaybe)
import Data.Either (partitionEithers)
Expand Down Expand Up @@ -373,18 +372,6 @@ begin' info mt = do
replTx .== ((,mt) <$> mTxId)
return ((,mt) <$> mTxId)

emptyTxState :: ReplM b ()
emptyTxState = do
fqdefs <- use (esLoaded . loAllLoaded)
cs <- use esStack
esc <- use esCheckRecursion
let newEvalState =
set esStack cs
$ set (esLoaded . loAllLoaded) fqdefs
$ set esCheckRecursion esc def
put newEvalState


commitTx :: NativeFunction 'ReplRuntime ReplCoreBuiltin FileLocSpanInfo
commitTx info b cont handler _env = \case
[] -> do
Expand Down
15 changes: 15 additions & 0 deletions pact-repl/Pact/Core/Repl/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ module Pact.Core.Repl.Utils
, replPrintLn'
, recordTestSuccess
, recordTestFailure
, emptyTxState
) where

import Control.Lens
import Control.Monad ( when, unless )
import Control.Monad.Reader
import Control.Monad.State.Strict(put)

import Data.Void
import Data.IORef
Expand Down Expand Up @@ -179,6 +181,19 @@ unlessReplFlagSet :: ReplDebugFlag -> ReplM b () -> ReplM b ()
unlessReplFlagSet flag ma =
replFlagSet flag >>= \b -> unless b ma

emptyTxState :: ReplM b ()
emptyTxState = do
cs <- use esStack
esc <- use esCheckRecursion
lo <- preserveReplDefuns
put $ set esLoaded lo $ set esStack cs $ set esCheckRecursion esc $ def
where
preserveReplDefuns = do
lo <- use loaded
let preservedTLReplDefuns = M.filter (\(fqn, _) -> _fqModule fqn == replModuleName) (_loToplevel lo)
let preservedFQReplDefuns = M.filterWithKey (\fqn _ -> _fqModule fqn == replModuleName) (_loAllLoaded lo)
pure $ set loToplevel preservedTLReplDefuns $ set loAllLoaded preservedFQReplDefuns $ def

replCompletion
:: [Text]
-- ^ natives
Expand Down
7 changes: 4 additions & 3 deletions pact-tests/Pact/Core/Test/GasGolden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ gasGoldenTestsWithFlags flags natives interp = do
let enabledNatives = S.fromList $ fmap lookupFileNameOp $ M.keys $ versionedNatives flags
let filteredTestsToRun = filter ((`S.member` enabledNatives) . fst) natives
gasOutputs <- forM filteredTestsToRun $ \(fn, fp) -> do
mGas <- runGasTest (gasTestDir </> fp) interp
mGas <- runGasTest flags (gasTestDir </> fp) interp
case mGas of
Nothing -> fail $ "Could not execute the gas tests for: " <> show fp
Just (MilliGas consumed) -> pure $ BS.fromStrict $ T.encodeUtf8 (lookupOp fn <> ": " <> T.pack (show consumed))
Expand Down Expand Up @@ -116,12 +116,13 @@ opToFileName = M.fromList
fileNameToOp :: M.Map Text Text
fileNameToOp = M.fromList [(v,k) | (k, v) <- M.toList opToFileName]

runGasTest :: FilePath -> ReplInterpreter -> IO (Maybe MilliGas)
runGasTest file interpret = do
runGasTest :: S.Set ExecutionFlag -> FilePath -> ReplInterpreter -> IO (Maybe MilliGas)
runGasTest flags file interpret = do
src <- T.readFile file
pdb <- mockPactDb serialisePact_repl_fileLocSpanInfo
ee <- defaultEvalEnv pdb replBuiltinMap
let ee' = ee & eeGasEnv . geGasModel .~ replTableGasModel (Just (maxBound :: MilliGasLimit))
& eeFlags .~ flags
gasRef = ee' ^. eeGasEnv . geGasRef
let source = SourceCode file src
let rstate = mkReplState ee' (const (const (pure ()))) (\f r -> void (loadFile interpret f r)) & replCurrSource .~ source
Expand Down
54 changes: 27 additions & 27 deletions pact-tests/gas-goldens/goldens/builtinGas.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
>=: 264
^: 868
abs: 100
acquire-module-admin: 295598
acquire-module-admin: 299909
add-time: 750
and?: 628
at: 706
Expand All @@ -20,38 +20,38 @@ base64-encode: 311
bind: 477
ceiling: 200
chain-data: 500
compose-capability: 514500
compose-capability: 524975
compose: 2760
concat: 820
cond: 602
contains: 405
continue: 441150
create-capability-guard: 227750
create-capability-pact-guard: 246700
create-module-guard: 188100
create-pact-guard: 209850
continue: 447093
create-capability-guard: 231142
create-capability-pact-guard: 249932
create-module-guard: 190692
create-pact-guard: 212262
create-principal: 2002
create-table: 466600
create-table: 467471
days: 278
dec: 100
define-keyset: 8404
define-namespace: 43812
describe-keyset: 108404
describe-module: 262700
describe-module: 264752
describe-namespace: 149724
describe-table: 566600
describe-table: 567471
diff-time: 1414
distinct: 2176
drop: 1000
emit-event: 263650
emit-event: 268003
enforce-guard: 3166
enforce-keyset: 3166
enforce-verifier: 10150
enumerate: 524
exp: 4534
filter: 2760
floor: 200
fold-db: 40500850
fold-db: 40501721
fold: 1090
format-time: 1041
format: 1900
Expand All @@ -63,12 +63,12 @@ hyperlane-decode-token-message: 2175
hyperlane-encode-token-message: 2475
hyperlane-message-id: 2743
identity: 100
insert: 500650
install-capability: 670289
insert: 501521
install-capability: 684794
int-to-str: 800
is-charset: 1788
is-principal: 597
keys: 40500650
keys: 40501521
keyset-ref-guard: 10425
length: 801
list-modules: 100000
Expand All @@ -83,8 +83,8 @@ negate: 100
not: 364
not?: 364
or?: 364
pact-id: 225850
pairing-check: 12003058
pact-id: 227962
pairing-check: 12054531
parse-time: 602
point-add: 5500
poseidon-hash-hack-a-chain: 6393700
Expand All @@ -93,34 +93,34 @@ read-integer: 303
read-keyset: 8608
read-msg: 303
read-string: 303
read: 507550
read: 508421
remove: 360
require-capability: 402750
resume: 515256
require-capability: 410584
resume: 523958
reverse: 700
round: 200
scalar-mult: 360300
select: 40500800
select: 40501671
shift: 886
show: 1300
sort: 1200
sqrt: 2022
static-redeploy: 298800
static-redeploy: 301983
str-to-int: 408
str-to-list: 751
take: 1800
time: 500
tx-hash: 100
typeof-principal: 797
typeof: 100
update: 534750
update: 535621
validate-principal: 3940
where: 1640
with-default-read: 510816
with-read: 507733
write: 500650
with-default-read: 511687
with-read: 508604
write: 501521
xor: 500
yield: 328075
yield: 332307
zip: 4320
|: 500
~: 250
2 changes: 1 addition & 1 deletion pact-tests/gas-goldens/goldens/builtinGas50.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
>=: 264
^: 868
abs: 100
acquire-module-admin: 295598
acquire-module-admin: 295588
add-time: 750
and?: 628
at: 706
Expand Down
2 changes: 1 addition & 1 deletion pact-tests/legacy-serial-tests/coin-v5/coin-v5.repl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"Gas cost of using the coin contract"
; Note: this one is cheaper than the repl test
; coin-v5.repl because this one does not store line infos
23 (env-gas))
1 (env-gas))

;; account balance for emily does not exist, because account does not exist yet
(expect-failure
Expand Down
4 changes: 2 additions & 2 deletions pact-tests/pact-tests/coin-v1.repl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(env-gaslog)
(expect
"Gas cost of loading coin contract"
16389 (env-gas))
16727 (env-gas))
(env-gas 0)
(commit-tx)

Expand All @@ -37,7 +37,7 @@
(expect
"Gas cost of using the coin contract"
; todo: use gas in core
15 (env-gas))
1 (env-gas))

;; account balance for emily does not exist, because account does not exist yet
(expect-failure
Expand Down
4 changes: 2 additions & 2 deletions pact-tests/pact-tests/coin-v5.repl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
(env-gaslog)
(expect
"Gas cost of loading coin contract"
20537
20983
(env-gas))

(create-table coin.coin-table)
Expand All @@ -50,7 +50,7 @@
(env-gaslog)
(expect
"Gas cost of using the coin contract"
24 (env-gas))
1 (env-gas))

;; account balance for emily does not exist, because account does not exist yet
(expect-failure
Expand Down
42 changes: 21 additions & 21 deletions pact-tests/pact-tests/db.repl
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,26 @@

;; test nested commits

(begin-tx)
(env-enable-repl-natives true)
(module nested-tx G
(defcap G () true)
(defschema s x:integer)
(deftable t:{s})
(defun test-nested-tx ()
(begin-tx)
(insert t "a" { 'x: 1 })
(commit-tx)
(begin-tx)
(insert t "b" { 'x: 2 })
(rollback-tx)
(expect "2nd insert rolled back" ["a"]
(keys t))))

(create-table t)
(commit-tx)

(nested-tx.test-nested-tx)
; (begin-tx)
; (env-enable-repl-natives true)
; (module nested-tx G
; (defcap G () true)
; (defschema s x:integer)
; (deftable t:{s})
; (defun test-nested-tx ()
; (begin-tx)
; (insert t "a" { 'x: 1 })
; (commit-tx)
; (begin-tx)
; (insert t "b" { 'x: 2 })
; (rollback-tx)
; (expect "2nd insert rolled back" ["a"]
; (keys t))))

; (create-table t)
; (commit-tx)

; (nested-tx.test-nested-tx)

;; fold-db tests + key sort guarantees
(env-exec-config [])
Expand Down Expand Up @@ -336,6 +336,6 @@
(expect "can write to table tbl3: different schema, same structure" () (write-to-table tbl3 "jose" {"a": 4, "b": 2}))

(expect "can read from tables with the same schema" [{"a": 1,"b": 2} {"a": 3,"b": 2} {"a": 4,"b": 2}] (read-from-tables [tbl, tbl4, tbl3] "jose"))
(expect-failure "Cannot read from tables with different schema" "Runtime typecheck failure" (read-from-tables [tbl, tbl2] "jose"))
(expect-failure "Cannot read from tables with different schema" "Runtime typecheck failure" (read-from-tables [tbl, tbl2] "jose"))

(commit-tx)
Loading

0 comments on commit ed950a5

Please sign in to comment.