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

Allow passing args to the contract's init function #84

Merged
merged 2 commits into from
Sep 16, 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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ $(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
--output kore \
--output-file [email protected] \
-cTEST='$(shell cat $<)' \
-pTEST=$(CURDIR)/parse-mx-rust-contract-test.sh
-pTEST=$(CURDIR)/parse-mx-rust-contract-test.sh \
-cARGS='$(shell cat $(patsubst %.run,%.args,$<))' \
-pARGS=$(CURDIR)/parse-mx-rust-contract-args.sh
cat [email protected] | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f [email protected] $@
33 changes: 27 additions & 6 deletions mx-rust-semantics/main/calls/implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ module MX-RUST-CALLS-IMPLEMENTATION
<k>
host.mkCall(FunctionName:String)
=> MxRust#newContractObject(TraitName)
~> MxRust#partialMethodCall(Endpoint, mxArgsToRustArgs(Args))
~> mxArgsToRustArgs(Args, Nfp, .CallParamsList)
~> MxRust#partialMethodCall(Endpoint)
...
</k>
<mx-rust-endpoint-to-function>
Expand All @@ -72,18 +73,38 @@ module MX-RUST-CALLS-IMPLEMENTATION
=> (#token("no#path", "Identifier"):Identifier):TypePath
</mx-rust-last-trait-name>
<mx-call-args> Args:MxValueList </mx-call-args>
<trait-path> TraitName </trait-path>
<method-name> Endpoint </method-name>
<method-params> _:SelfSort : _ , Nfp:NormalizedFunctionParameterList </method-params>

rule ptrValue(...) #as SelfValue:Expression
~> MxRust#partialMethodCall(Method:Identifier, Params:CallParamsList)
rule (ptrValue(...) #as SelfValue:Expression) , Params:CallParamsList
~> MxRust#partialMethodCall(Method:Identifier)
=> methodCall(... self: SelfValue, method: Method, params: Params)

syntax MxRustInstruction ::= "MxRust#newContractObject" "(" TypePath ")"
rule MxRust#newContractObject(P:TypePath) => Rust#newStruct(P, .Map)

syntax MxRustInstruction ::= "MxRust#partialMethodCall" "(" Identifier "," CallParamsList ")"
syntax MxRustInstruction ::= "MxRust#partialMethodCall" "(" Identifier ")"

syntax CallParamsList ::= mxArgsToRustArgs(MxValueList) [function, total]
rule mxArgsToRustArgs(.MxValueList) => .CallParamsList
syntax MxRustInstruction ::= mxArgsToRustArgs
( MxValueList
, NormalizedFunctionParameterList
, CallParamsList
)
rule mxArgsToRustArgs(.MxValueList, .NormalizedFunctionParameterList, L:CallParamsList)
=> reverse(L, .CallParamsList)
rule (.K => mxValueToRust(T, V))
~> mxArgsToRustArgs
( (V:MxValue , L:MxValueList) => L
, _ : T:Type , Nfp:NormalizedFunctionParameterList => Nfp
, _:CallParamsList
)
rule (V:PtrValue => .K)
~> mxArgsToRustArgs
( _:MxValueList
, _:NormalizedFunctionParameterList
, L:CallParamsList => V, L
)

rule (ptrValue(_, V:Value) => rustValueToMx(V)) ~> endCall

Expand Down
1 change: 1 addition & 0 deletions mx-rust-semantics/main/glue.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module MX-RUST-GLUE
rule V:MxValue ~> mxValueToRust(T:Type)
=> mxValueToRust(T, V)

rule mxValueToRust(&T => T, _V)
rule mxValueToRust(T:Type, mxIntValue(I:Int))
=> mxRustNewValue(integerToValue(I, T))
requires
Expand Down
6 changes: 5 additions & 1 deletion mx-rust-semantics/main/representation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module MX-RUST-REPRESENTATION
syntax MxRustInstruction ::= "mxRustPreprocessTraits"
| mxRustPreprocessMethods(TypePath)
| mxRustCreateAccount(String)
| mxRustCreateContract(owner: String, contractAccount: String, code: Crate)
| mxRustCreateContract
( owner: String
, contractAccount: String
, code: Crate
, args: MxValueList)
| mxRustNewValue(ValueOrError)
| mxRustEmptyValue(MxRustType)
| mxValueToRust(Type)
Expand Down
3 changes: 2 additions & 1 deletion mx-rust-semantics/setup/mx.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module MX-RUST-SETUP-MX
(... owner: Owner:String
, contractAccount: Contract:String
, code: Code:Crate
, args: Args:MxValueList
)
=> crateParser(Code)
~> mxRustPreprocessTraits
Expand All @@ -33,7 +34,7 @@ module MX-RUST-SETUP-MX
, newAddress: Contract
, egldValue: 0
, gasLimit: 0
, args: .MxValueList
, args: Args
)

// Trying to put the following three rules in one causes this kind of error:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module COMMON-K-CELL
(... owner: "Owner"
, contractAccount: "TestContract"
, code: $PGM:Crate
, args: $ARGS:MxValueList
)
~> ($TEST:MxRustTest):KItem
</k>
Expand Down
8 changes: 8 additions & 0 deletions parse-mx-rust-contract-args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/bash

kast \
--output kore \
--definition .build/mx-rust-contract-testing-kompiled \
--module MX-RUST-SYNTAX \
--sort MxValueList \
$1
2 changes: 0 additions & 2 deletions rust-semantics/expression.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ requires "expression/conditionals.md"
requires "expression/literals.md"
requires "expression/references.md"
requires "expression/struct.md"
requires "expression/tools.md"
requires "expression/variables.md"

module RUST-EXPRESSION
Expand All @@ -21,7 +20,6 @@ module RUST-EXPRESSION
imports private RUST-EXPRESSION-LITERALS
imports private RUST-EXPRESSION-REFERENCES
imports private RUST-EXPRESSION-STRUCT
imports private RUST-EXPRESSION-TOOLS
imports private RUST-EXPRESSION-VARIABLES
imports private RUST-INTEGER-OPERATIONS
imports private RUST-BOOL-OPERATIONS
Expand Down
2 changes: 1 addition & 1 deletion rust-semantics/expression/calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module RUST-EXPRESSION-CALLS
imports private COMMON-K-CELL
imports private RUST-SHARED-SYNTAX
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-EXPRESSION-TOOLS
imports private RUST-PREPROCESSING-TOOLS
imports private RUST-REPRESENTATION
imports private RUST-SHARED-SYNTAX

Expand Down
1 change: 1 addition & 0 deletions rust-semantics/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ module RUST-HELPERS
rule isSameType(u64(_), u64) => true
rule isSameType(i32(_), i32) => true
rule isSameType(u32(_), u32) => true
rule isSameType(struct(T, _), T:Type) => true
endmodule
```
4 changes: 3 additions & 1 deletion rust-semantics/preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ requires "preprocessing/configuration.md"
requires "preprocessing/helpers.md"
requires "preprocessing/initialization.md"
requires "preprocessing/syntax.md"
requires "preprocessing/tools.md"
requires "preprocessing/trait.md"
requires "preprocessing/trait-methods.md"

module RUST-PREPROCESSING
imports private RUST-CONSTANTS
imports private CRATE
imports private INITIALIZATION
imports private RUST-CONSTANTS
imports private RUST-PREPROCESSING-TOOLS
imports private TRAIT
imports private TRAIT-METHODS
endmodule
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```k

module RUST-EXPRESSION-TOOLS
module RUST-PREPROCESSING-TOOLS
imports private BOOL
imports private RUST-REPRESENTATION
imports private RUST-SHARED-SYNTAX
Expand All @@ -10,6 +10,9 @@ module RUST-EXPRESSION-TOOLS
rule isValueWithPtr(_:PtrValue) => true
rule isValueWithPtr(.CallParamsList) => true
rule isValueWithPtr(P:Expression , Ps:CallParamsList) => isValueWithPtr(P) andBool isValueWithPtr(Ps)

rule reverse(.CallParamsList, L:CallParamsList) => L
rule reverse((P , Ps:CallParamsList => Ps), (L:CallParamsList => P, L))
endmodule

```
2 changes: 2 additions & 0 deletions rust-semantics/representation.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ module RUST-REPRESENTATION

syntax String ::= IdentifierToString(Identifier) [function, total, hook(STRING.token2string)]

syntax CallParamsList ::= reverse(CallParamsList, CallParamsList) [function, total]

endmodule

```
15 changes: 5 additions & 10 deletions rust-semantics/rust-common-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ https://doc.rust-lang.org/reference/items/extern-crates.html
| ExpressionWithBlock

syntax Expression ::= LiteralExpression
| GroupedExpression
| ArrayExpression
| AwaitExpression
| TupleExpression
Expand All @@ -331,7 +330,11 @@ https://doc.rust-lang.org/reference/items/extern-crates.html

// Several sub-expressions were included directly in Expression
// to make it easy to disambiguate based on priority
syntax Expression ::= PathExpression
syntax Expression ::=
// https://doc.rust-lang.org/reference/expressions/grouped-expr.html
"(" Expression ")" [bracket]

> PathExpression

// https://doc.rust-lang.org/reference/expressions/method-call-expr.html
> Expression "." PathExprSegment "(" ")"
Expand Down Expand Up @@ -471,14 +474,6 @@ https://doc.rust-lang.org/reference/items/extern-crates.html

syntax TypeCastExpression ::= "TODO: not needed yet, not implementing"

```

https://doc.rust-lang.org/reference/expressions/grouped-expr.html

```k

syntax GroupedExpression ::= "(" Expression ")"

```

https://doc.rust-lang.org/reference/expressions/array-expr.html
Expand Down
Empty file.
34 changes: 34 additions & 0 deletions tests/mx-rust-contracts/call-endpoint-args.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
setCallee("Owner");

push mxListValue();
push mxStringValue("getMyStorage");
push mxIntValue(0);
push mxTransfersValue();
push mxIntValue(0);
push mxStringValue("TestContract");
call 6 MX#managedExecuteOnDestContext;
check_eq mxIntValue(0);

push_return_value;
check_eq mxIntValue(0);

push mxListValue(mxIntValue(8));
push mxStringValue("setMyStorage");
push mxIntValue(0);
push mxTransfersValue();
push mxIntValue(0);
push mxStringValue("TestContract");
call 6 MX#managedExecuteOnDestContext;
check_eq mxIntValue(0);

push mxListValue();
push mxStringValue("getMyStorage");
push mxIntValue(0);
push mxTransfersValue();
push mxIntValue(0);
push mxStringValue("TestContract");
call 6 MX#managedExecuteOnDestContext;
check_eq mxIntValue(0);

push_return_value;
check_eq mxIntValue(8)
27 changes: 27 additions & 0 deletions tests/mx-rust-contracts/call-endpoint-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![no_std]

#[allow(unused_imports)]
use multiversx_sc::imports::*;

#[multiversx_sc::contract]
pub trait Contract {
#[view(noArg)]
#[storage_mapper("my_value")]
fn my_storage(&self) -> SingleValueMapper<BigUint>;

#[init]
fn init(&self) {}

#[upgrade]
fn upgrade(&self) {}

#[endpoint(setMyStorage)]
fn set_my_storage(&self, value: &BigUint) {
self.my_storage().set(value)
}

#[endpoint(getMyStorage)]
fn get_my_storage(&self) -> BigUint {
self.my_storage().get()
}
}
1 change: 1 addition & 0 deletions tests/mx-rust-contracts/contract-setup-args.1.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mxIntValue(13)
14 changes: 14 additions & 0 deletions tests/mx-rust-contracts/contract-setup-args.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
setCallee("Owner");

push mxListValue();
push mxStringValue("getMyStorage");
push mxIntValue(0);
push mxTransfersValue();
push mxIntValue(0);
push mxStringValue("TestContract");
call 6 MX#managedExecuteOnDestContext;
check_eq mxIntValue(0);

push_return_value;
check_eq mxIntValue(13)

24 changes: 24 additions & 0 deletions tests/mx-rust-contracts/contract-setup-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![no_std]

#[allow(unused_imports)]
use multiversx_sc::imports::*;

#[multiversx_sc::contract]
pub trait Storage {
#[view(noArg)]
#[storage_mapper("my_storage")]
fn my_storage(&self) -> SingleValueMapper<BigUint>;

#[init]
fn init(&self, value: &BigUint) {
self.my_storage().set_if_empty(value)
}

#[upgrade]
fn upgrade(&self) {}

#[endpoint(getMyStorage)]
fn get_my_storage(&self) -> BigUint {
self.my_storage().get()
}
}
Empty file.
2 changes: 1 addition & 1 deletion tests/mx-rust-contracts/contract-setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use multiversx_sc::imports::*;
#[multiversx_sc::contract]
pub trait Storage {
#[view(noArg)]
#[storage_mapper("no_arg")]
#[storage_mapper("my_value")]
fn my_storage(&self) -> SingleValueMapper<BigUint>;

#[init]
Expand Down
Loading