Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gas to post-reads, make module admin acquisition explicit. #208

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/builtins/General/acquire-module-admin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## acquire-module-admin

Use `acquire-module-admin` to grant module admin privileges for a particular module `m`. You must already own admin for this particular module; that is, you must either be the owner of the keyset that grants the governance, or be able to pass the governance capability acquisition.

### Basic syntax

Assume you have some module
```pact
(module my-module gov
(defcap gov ()
(do-stuff-for-governance)
)
)
```

To grant module admin, use

```pact
(acquire-module some-module)
```

It will attempt to acquire the governance cap and if successful, it will grant module admin for the rest of the transaction.

### Arguments

Use one of the following argument to define the value you want to retrieve using the `at` Pact function.

| Argument | Type | Description
| -------- | ---- | -----------
| `ref` | modref | Specifies the module to acquire administrative capabilities for

### Return values

Module admin acquisition will either fail, or return "Module admin for module <my-module> acquired"

### Examples

See: Basic Syntax.
6 changes: 3 additions & 3 deletions docs/builtins/Operators/eq.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Use the following arguments to specify the values for comparison using the `=` P

| Argument | Type | Description |
| --- | --- | --- |
| `oper1` | integer, decimal, string, time, bool, object, list, or table | Specifies the first value for comparison. |
| `oper2` | integer, decimal, string, time, bool, object, list, table | Specifies the second value for comparison. |
| `oper1` | integer, decimal, string, time, bool, object, list, modref, guard | Specifies the first value for comparison. |
| `oper2` | integer, decimal, string, time, bool, object, list, modref, guard | Specifies the second value for comparison. |

### Return value

Expand Down Expand Up @@ -70,4 +70,4 @@ For example:

```pact
(enforce (= amount 1.0) "Mint can only be 1")
```
```
17 changes: 2 additions & 15 deletions gasmodel/Pact/Core/GasModel/ContractBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ import Pact.Core.SPV
import qualified Pact.Core.Syntax.Parser as Lisp
import qualified Pact.Core.Syntax.Lexer as Lisp
import qualified Pact.Core.Syntax.LexUtils as Lisp
import qualified Pact.Core.IR.Eval.CEK as CEK
import qualified Pact.Core.IR.Eval.CoreBuiltin as CEK
import qualified Pact.Core.IR.Eval.Direct.Evaluator as Direct
import Pact.Core.Gas.TableGasModel
import Pact.Core.Gas
import Pact.Core.Namespace
Expand All @@ -68,20 +65,10 @@ mkKs :: PublicKeyText -> PactValue
mkKs a = PGuard $ GKeyset $ KeySet (S.singleton a) KeysAll

interpretBigStep :: Interpreter ExecRuntime CoreBuiltin SpanInfo
interpretBigStep =
Interpreter runGuard runTerm (\_ _ -> error "unimplemented")
where
runTerm purity term = CEK.eval purity eEnv term
runGuard info g = CEK.interpretGuard info eEnv g
eEnv = CEK.coreBuiltinEnv @ExecRuntime
interpretBigStep = evalInterpreter

interpretDirect :: Interpreter ExecRuntime CoreBuiltin SpanInfo
interpretDirect =
Interpreter runGuard runTerm (\_ _ -> error "unimplemented")
where
runTerm purity term = Direct.eval purity eEnv term
runGuard info g = Direct.interpretGuard info eEnv g
eEnv = Direct.coreBuiltinEnv
interpretDirect = evalDirectInterpreter


data CoinBenchSenders
Expand Down
8 changes: 4 additions & 4 deletions pact-tests/Pact/Core/Test/PersistenceTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ keysetPersistRoundtrip serial builtins keysetGen =
evalEnv <- liftIO $ getEvalEnv serial builtins
writtenKeySetResult <- liftIO $ runEvalM (ExecEnv evalEnv) def $ withSqlitePactDb serial ":memory:" $ \db -> do
evalWrite def db Insert DKeySets keysetName keyset
liftIO $ _pdbRead db DKeySets keysetName
liftGasM def $ _pdbRead db DKeySets keysetName
case writtenKeySetResult of
(Left _, _) -> fail "Unexpected EvalM error"
(Right writtenKeySet, _) -> Just keyset === writtenKeySet
Expand All @@ -74,7 +74,7 @@ moduleDataRoundtrip serial builtins b i = property $ do
evalEnv <- liftIO $ getEvalEnv serial builtins
readResult <- liftIO $ runEvalM (ExecEnv evalEnv) def $ withSqlitePactDb serial ":memory:" $ \db -> do
() <- evalWrite def db Insert DModules moduleName moduleData
liftIO $ _pdbRead db DModules moduleName
liftGasM def $ _pdbRead db DModules moduleName
case readResult of
(Left _, _) -> fail "Unexpected EvalM error"
(Right writtenModuleData, _) ->
Expand All @@ -87,7 +87,7 @@ defPactExecRoundtrip serial builtins _b _i = property $ do
evalEnv <- liftIO $ getEvalEnv serial builtins
writeResult <- liftIO $ runEvalM (ExecEnv evalEnv) def $ withSqlitePactDb serial ":memory:" $ \db -> do
() <- evalWrite def db Insert DDefPacts defPactId defPactExec
liftIO $ _pdbRead db DDefPacts defPactId
liftGasM def $ _pdbRead db DDefPacts defPactId
case writeResult of
(Left _, _) -> fail "Unexpected EvalM error"
(Right writtenDefPactExec, _) ->
Expand All @@ -100,7 +100,7 @@ namespaceRoundtrip serial builtins = property $ do
evalEnv <- liftIO $ getEvalEnv serial builtins
writeResult <- liftIO $ runEvalM (ExecEnv evalEnv) def $ withSqlitePactDb serial ":memory:" $ \db -> do
() <- evalWrite def db Insert DNamespaces ns namespace
liftIO $ _pdbRead db DNamespaces ns
liftGasM def $ _pdbRead db DNamespaces ns
case writeResult of
(Left _, _) -> fail "Unexpected EvalM error"
(Right writtenNamespace, _) ->
Expand Down
8 changes: 1 addition & 7 deletions pact-tests/Pact/Core/Test/StaticErrorTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,7 @@ executionTests =
)
|])

-- CEK errors
, ("modref_no_ns", isExecutionError _ModRefImplementsNoInterfaces, [text|
(module m g (defcap g () true))
m
|])

, ("defpact_not_init", isExecutionError _NoDefPactIdAndExecEnvSupplied, [text|
, ("defpact_not_init", isExecutionError _NoDefPactIdAndExecEnvSupplied, [text|
(module m g (defcap g () true))
(continue-pact 1)
|])
Expand Down
7 changes: 4 additions & 3 deletions pact-tests/constructor-tag-goldens/CoreBuiltin.golden
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
{"conName":"CoreIdentity","conIndex":"7e"}
{"conName":"CoreVerifySPV","conIndex":"7f"}
{"conName":"CoreEnforceVerifier","conIndex":"80"}
{"conName":"CoreHyperlaneMessageId","conIndex":"81"}
{"conName":"CoreHyperlaneDecodeMessage","conIndex":"82"}
{"conName":"CoreHyperlaneEncodeMessage","conIndex":"83"}
{"conName":"CoreAcquireModuleAdmin","conIndex":"81"}
{"conName":"CoreHyperlaneMessageId","conIndex":"82"}
{"conName":"CoreHyperlaneDecodeMessage","conIndex":"83"}
{"conName":"CoreHyperlaneEncodeMessage","conIndex":"84"}

3 changes: 2 additions & 1 deletion pact-tests/constructor-tag-goldens/EvalError.golden
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@
{"conName":"InvalidCustomKeysetPredicate","conIndex":"48"}
{"conName":"HyperlaneError","conIndex":"49"}
{"conName":"HyperlaneDecodeError","conIndex":"4a"}
{"conName":"UnknownException","conIndex":"4b"}
{"conName":"ModuleAdminNotAcquired","conIndex":"4b"}
{"conName":"UnknownException","conIndex":"4c"}

10 changes: 10 additions & 0 deletions pact-tests/gas-goldens/acquire-module-admin.repl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(begin-tx)
(module m g
(defcap g ()
(map (+ 1) (enumerate 0 20))))
(commit-tx)

(begin-tx)
(acquire-module-admin m)
(acquire-module-admin m)
(commit-tx)
7 changes: 4 additions & 3 deletions pact-tests/gas-goldens/builtinGas.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
>=: 114
^: 5000
abs: 50
acquire-module-admin: 60045303
add-time: 5000
and?: 253
at: 2500
Expand Down Expand Up @@ -89,7 +90,7 @@ read-integer: 128
read-keyset: 37239
read-msg: 128
read-string: 128
read: 60393378
read: 60393713
remove: 261
require-capability: 60002677
resume: 60006677
Expand All @@ -111,8 +112,8 @@ typeof: 25
update: 60516583
validate-principal: 4940
where: 1079
with-default-read: 60405928
with-read: 60394453
with-default-read: 60406263
with-read: 60394788
write: 60383378
xor: 2000
yield: 60002449
Expand Down
6 changes: 3 additions & 3 deletions pact-tests/legacy-serial-tests/coin-v5/coin-v5.repl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
(env-gaslog)
(expect
"Gas cost of querying the details of an account"
12 (env-gas))
13 (env-gas))

(commit-tx)

Expand Down Expand Up @@ -394,7 +394,7 @@
(env-gaslog)
(expect
"Gas cost of transfer"
338 (env-gas))
340 (env-gas))

(expect-failure "emily->doug capability used up"
"TRANSFER exceeded"
Expand Down Expand Up @@ -431,7 +431,7 @@
(transfer-create 'doug 'will (read-keyset 'will) 1.0))
(env-gaslog)
(expect
"Gas cost of transfer-create" 332 (env-gas))
"Gas cost of transfer-create" 333 (env-gas))

(expect
"doug now has 0.4 coins"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
(begin-tx)

(load "../../pact-tests/marmalade/pact/ledger/ledger-v2.interface.pact")
(load "../../pact-tests/marmalade/pact/policy-manager/sale.interface.pact")
(load "../../pact-tests/marmalade/pact/policy-manager/sale.interface.pact")
(load "../../pact-tests/marmalade/pact/policy-manager/policy-manager.pact")
(load "../../pact-tests/marmalade/pact/ledger/ledger.pact")
(commit-tx)
Expand Down Expand Up @@ -138,6 +138,7 @@


(use marmalade-v2.ledger)
(acquire-module-admin marmalade-v2.ledger)
(create-table versions)

(commit-tx)
Expand Down
6 changes: 3 additions & 3 deletions pact-tests/pact-tests/coin-v1.repl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
(env-gaslog)
(expect
"Gas cost of querying the details of an account"
12 (env-gas))
13 (env-gas))

(commit-tx)

Expand Down Expand Up @@ -395,7 +395,7 @@
(env-gaslog)
(expect
"Gas cost of transfer"
337 (env-gas))
339 (env-gas))

(expect-failure "emily->doug capability used up"
"TRANSFER exceeded"
Expand Down Expand Up @@ -432,7 +432,7 @@
(transfer-create 'doug 'will (read-keyset 'will) 1.0))
(env-gaslog)
(expect
"Gas cost of transfer-create" 326 (env-gas))
"Gas cost of transfer-create" 327 (env-gas))

(expect
"doug now has 0.4 coins"
Expand Down
6 changes: 3 additions & 3 deletions pact-tests/pact-tests/coin-v5.repl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
(env-gaslog)
(expect
"Gas cost of querying the details of an account"
12 (env-gas))
13 (env-gas))

(commit-tx)

Expand Down Expand Up @@ -408,7 +408,7 @@
(env-gaslog)
(expect
"Gas cost of transfer"
338 (env-gas))
340 (env-gas))

(expect-failure "emily->doug capability used up"
"TRANSFER exceeded"
Expand Down Expand Up @@ -445,7 +445,7 @@
(transfer-create 'doug 'will (read-keyset 'will) 1.0))
(env-gaslog)
(expect
"Gas cost of transfer-create" 332 (env-gas))
"Gas cost of transfer-create" 333 (env-gas))

(expect
"doug now has 0.4 coins"
Expand Down
4 changes: 4 additions & 0 deletions pact-tests/pact-tests/db.repl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@
(use dbtest)
(begin-tx)
(use dbtest)
; Acquire module admin, should pass because admin key is in scope
(expect "Module admin acquired successfully" "Module admin for module dbtest acquired" (acquire-module-admin dbtest))
(insert persons ID_A ROW_A)
(expect-failure "dupe key should fail" (insert persons ID_A ROW_A))
(commit-tx)
(begin-tx)
(use dbtest)
; acquire admin from db test
(acquire-module-admin dbtest)
(expect "keys works" [ID_A] (keys persons))

; (insert stuff "k" { "stuff": { "dec": 1.2, "bool": true, "int": -3, "time": (parse-time "%F" "1970-01-01") } })
Expand Down
6 changes: 3 additions & 3 deletions pact-tests/pact-tests/gov.repl
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@

;; direct read will attempt to grant admin
(env-hash (hash "tx1"))
(expect-failure "admin grant fails for tx1" (read votes "bob"))
(expect-failure "admin grant fails for tx1" (acquire-module-admin govtest))

(env-hash (hash "tx3"))
(expect-failure "admin grant fails for tx3" (read votes "bob"))
(expect-failure "admin grant fails for tx3" (acquire-module-admin govtest))

(env-hash (hash "tx2"))

(expect "admin grant succeeds for tx2" { "vote-hash": (hash "tx2") } (read votes "bob"))
(expect "admin grant succeeds for tx2" "Module admin for module govtest acquired" (acquire-module-admin govtest))

;; prove that admin is granted for rest of tx by resetting tx-hash to non-winning value

Expand Down
2 changes: 2 additions & 0 deletions pact-tests/pact-tests/leftpad.repl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

(expect "leftpad works" " hello" (dep.dep-leftpad))
(dep.dep-impure "a" 1)
(acquire-module-admin impure)
(expect "impure works" { "value": 1 } (read impure.foo "a"))

(rollback-tx)
Expand Down Expand Up @@ -71,4 +72,5 @@
(commit-tx)

(dep.dep-impure "b" 1)
(acquire-module-admin impure)
(expect "impure works with blessed hash" { "value": 1 } (read impure.foo "b"))
27 changes: 26 additions & 1 deletion pact-tests/pact-tests/side-effects.repl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

(begin-tx)
(module m g
(defcap g () true)

Expand Down Expand Up @@ -36,3 +36,28 @@
(expect "counter is zero" 0 (get-counter))
(expect "counter increments twice" 2 (do (increment-counter) (increment-counter) (get-counter)))
(expect "counter decrements twice" 0 (do (decrement-counter) (decrement-counter) (get-counter)))

(commit-tx)

; redeploy m
(begin-tx)

(module m g
(defcap g ()
(map (+ 1) (enumerate 0 100))
))
(commit-tx)

(begin-tx)
(env-gasmodel "table")
(env-gaslimit 100000)
(env-gas 0)
(acquire-module-admin m)
(expect "Acquiring module admin gas cost evaluates enumerate" 44 (env-gas))
(acquire-module-admin m)
; Note: The gas cost here is: the flat cost of acquire-module-admin
; as a native + the gas from the previous test. In other words:
; this is testing that the only gas consumed is from the flat native cost, which means
; we didn't evaluate the cap twice
(expect "Acquiring module admin twice does not evaluate the cap twice" (+ 20 44) (env-gas))
(commit-tx)
1 change: 1 addition & 0 deletions pact-tests/pact-tests/try.repl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
(expect-failure "failure on attempting to write to the db" (failed-write))

;; failure case for reads in pure context
(acquire-module-admin try-module)
(insert test-table "emily" { "test" : true })
(expect "failure when on attempting to reads from the db" true (successful-read))

Expand Down
Loading
Loading