Skip to content

Commit

Permalink
Contract proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Sep 23, 2024
1 parent b303afe commit 7d3a018
Show file tree
Hide file tree
Showing 55 changed files with 1,355 additions and 101 deletions.
58 changes: 50 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,28 @@ MX_RUST_SEMANTICS_FILES ::= $(shell find mx-rust-semantics/ -type f -a '(' -name
MX_RUST_KOMPILED ::= .build/mx-rust-kompiled
MX_RUST_TIMESTAMP ::= $(MX_RUST_KOMPILED)/timestamp

MX_RUST_TESTING_SEMANTICS_FILES ::= $(shell find mx-rust-semantics/ -type f -a '(' -name '*.md' -or -name '*.k' ')')
MX_RUST_TESTING_KOMPILED ::= .build/mx-rust-testing-kompiled
MX_RUST_TESTING_TIMESTAMP ::= $(MX_RUST_TESTING_KOMPILED)/timestamp
MX_RUST_TESTING_INPUT_DIR ::= tests/mx-rust
MX_RUST_TESTING_OUTPUT_DIR ::= .build/mx-rust/output
MX_RUST_TESTING_INPUTS ::= $(wildcard $(MX_RUST_TESTING_INPUT_DIR)/*.run)
MX_RUST_TESTING_OUTPUTS ::= $(patsubst $(MX_RUST_TESTING_INPUT_DIR)/%,$(MX_RUST_TESTING_OUTPUT_DIR)/%.executed.kore,$(MX_RUST_TESTING_INPUTS))

MX_RUST_CONTRACT_TESTING_SEMANTICS_FILES ::= $(shell find mx-rust-semantics/ -type f -a '(' -name '*.md' -or -name '*.k' ')')
MX_RUST_CONTRACT_TESTING_KOMPILED ::= .build/mx-rust-contract-testing-kompiled
MX_RUST_CONTRACT_TESTING_TIMESTAMP ::= $(MX_RUST_CONTRACT_TESTING_KOMPILED)/timestamp
MX_RUST_CONTRACT_TESTING_INPUT_DIR ::= tests/mx-rust-contracts
MX_RUST_CONTRACT_TESTING_OUTPUT_DIR ::= .build/mx-rust-contarcts/output
MX_RUST_CONTRACT_TESTING_OUTPUT_DIR ::= .build/mx-rust-contracts/output
MX_RUST_CONTRACT_TESTING_INPUTS ::= $(wildcard $(MX_RUST_CONTRACT_TESTING_INPUT_DIR)/*.run)
MX_RUST_CONTRACT_TESTING_OUTPUTS ::= $(patsubst $(MX_RUST_CONTRACT_TESTING_INPUT_DIR)/%,$(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)/%.executed.kore,$(MX_RUST_CONTRACT_TESTING_INPUTS))

.PHONY: clean build test syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test
MX_RUST_TWO_CONTRACTS_TESTING_KOMPILED ::= .build/mx-rust-two-contracts-testing-kompiled
MX_RUST_TWO_CONTRACTS_TESTING_TIMESTAMP ::= $(MX_RUST_TWO_CONTRACTS_TESTING_KOMPILED)/timestamp
MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR ::= tests/mx-rust-two-contracts
MX_RUST_TWO_CONTRACTS_TESTING_OUTPUT_DIR ::= .build/mx-rust-two-contracts/output
MX_RUST_TWO_CONTRACTS_TESTING_INPUTS ::= $(wildcard $(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/*.run)
MX_RUST_TWO_CONTRACTS_TESTING_OUTPUTS ::= $(patsubst $(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/%,$(MX_RUST_TWO_CONTRACTS_TESTING_OUTPUT_DIR)/%.executed.kore,$(MX_RUST_TWO_CONTRACTS_TESTING_INPUTS))

.PHONY: clean build test syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test mx-rust-two-contracts-test

clean:
rm -r .build
Expand All @@ -57,9 +62,10 @@ build: $(RUST_PREPROCESSING_TIMESTAMP) \
$(MX_TESTING_TIMESTAMP) \
$(MX_RUST_TIMESTAMP) \
$(MX_RUST_TESTING_TIMESTAMP) \
$(MX_RUST_CONTRACT_TESTING_TIMESTAMP)
$(MX_RUST_CONTRACT_TESTING_TIMESTAMP) \
$(MX_RUST_TWO_CONTRACTS_TESTING_TIMESTAMP)

test: build syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test
test: build syntax-test preprocessing-test execution-test mx-test mx-rust-test mx-rust-contract-test mx-rust-two-contracts-test

syntax-test: $(SYNTAX_OUTPUTS)

Expand All @@ -73,6 +79,8 @@ mx-rust-test: $(MX_RUST_TESTING_OUTPUTS)

mx-rust-contract-test: $(MX_RUST_CONTRACT_TESTING_OUTPUTS)

mx-rust-two-contracts-test: $(MX_RUST_TWO_CONTRACTS_TESTING_OUTPUTS)

$(RUST_PREPROCESSING_TIMESTAMP): $(RUST_SEMANTICS_FILES)
# Workaround for https://github.com/runtimeverification/k/issues/4141
-rm -r $(RUST_PREPROCESSING_KOMPILED)
Expand Down Expand Up @@ -104,7 +112,6 @@ $(MX_RUST_TESTING_TIMESTAMP): $(MX_SEMANTICS_FILES) $(RUST_SEMANTICS_FILES) $(MX
-I . \
--debug


$(MX_RUST_CONTRACT_TESTING_TIMESTAMP): $(MX_SEMANTICS_FILES) $(RUST_SEMANTICS_FILES) $(MX_RUST_SEMANTICS_FILES)
# Workaround for https://github.com/runtimeverification/k/issues/4141
-rm -r $(MX_RUST_CONTRACT_TESTING_KOMPILED)
Expand All @@ -113,6 +120,14 @@ $(MX_RUST_CONTRACT_TESTING_TIMESTAMP): $(MX_SEMANTICS_FILES) $(RUST_SEMANTICS_FI
-I . \
--debug

$(MX_RUST_TWO_CONTRACTS_TESTING_TIMESTAMP): $(MX_SEMANTICS_FILES) $(RUST_SEMANTICS_FILES) $(MX_RUST_SEMANTICS_FILES)
# Workaround for https://github.com/runtimeverification/k/issues/4141
-rm -r $(MX_RUST_TWO_CONTRACTS_TESTING_KOMPILED)
$$(which kompile) mx-rust-semantics/targets/two-contracts-testing/mx-rust.md \
-o $(MX_RUST_TWO_CONTRACTS_TESTING_KOMPILED) \
-I . \
--debug

$(RUST_SYNTAX_OUTPUT_DIR)/%.rs-parsed: $(RUST_SYNTAX_INPUT_DIR)/%.rs $(RUST_PREPROCESSING_TIMESTAMP)
mkdir -p $(RUST_SYNTAX_OUTPUT_DIR)
kast --definition $(RUST_PREPROCESSING_KOMPILED) $< --sort Crate > $@.tmp && mv -f $@.tmp $@
Expand Down Expand Up @@ -177,7 +192,6 @@ $(MX_RUST_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@


# TODO: Add $(shell echo "$<" | sed 's/\.[^.]*.run$$//').rs
# as a dependency
$(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
Expand All @@ -200,3 +214,31 @@ $(MX_RUST_CONTRACT_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
-pARGS=$(CURDIR)/parsers/args-mx-rust-contract.sh
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@

$(MX_RUST_TWO_CONTRACTS_TESTING_OUTPUT_DIR)/%.run.executed.kore: \
$(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/%.run \
$(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/%.1.rs \
$(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/%.1.args \
$(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/%.2.rs \
$(MX_RUST_TWO_CONTRACTS_TESTING_INPUT_DIR)/%.2.args \
$(MX_RUST_TWO_CONTRACTS_TESTING_TIMESTAMP) \
parsers/contract-mx-rust-two-contracts.sh \
parsers/args-mx-rust-two-contracts.sh \
parsers/test-mx-rust-two-contracts.sh
mkdir -p $(MX_RUST_TWO_CONTRACTS_TESTING_OUTPUT_DIR)
krun \
--definition $(MX_RUST_TWO_CONTRACTS_TESTING_KOMPILED) \
--output kore \
--output-file $@.tmp \
-cPGM1='$(shell cat $(patsubst %.run,%.1.rs,$<))' \
-pPGM1=$(CURDIR)/parsers/contract-mx-rust-two-contracts.sh \
-cPGM2='$(shell cat $(patsubst %.run,%.2.rs,$<))' \
-pPGM2=$(CURDIR)/parsers/contract-mx-rust-two-contracts.sh \
-cTEST='$(shell cat $<)' \
-pTEST=$(CURDIR)/parsers/test-mx-rust-two-contracts.sh \
-cARGS1='$(shell cat $(patsubst %.run,%.1.args,$<))' \
-pARGS1=$(CURDIR)/parsers/args-mx-rust-two-contracts.sh \
-cARGS2='$(shell cat $(patsubst %.run,%.2.args,$<))' \
-pARGS2=$(CURDIR)/parsers/args-mx-rust-two-contracts.sh
cat $@.tmp | grep -q "Lbl'-LT-'k'-GT-'{}(dotk{}())"
mv -f $@.tmp $@
1 change: 0 additions & 1 deletion mx-rust-semantics/main/calls/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module MX-RUST-CALLS-CONFIGURATION
imports LIST
imports RUST-SHARED-SYNTAX
configuration
<mx-rust-calls>
Expand Down
2 changes: 2 additions & 0 deletions mx-rust-semantics/main/calls/implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module MX-RUST-CALLS-IMPLEMENTATION
imports private RUST-REPRESENTATION
imports private RUST-SHARED-SYNTAX
rule rustValueToMx(tuple(.ValueList)) => mxUnitValue()
rule (.K => rustValueToMx(V))
~> rustValuesToMx((V:Value , L:ValueList => L), _:MxValueList)
rule (V:MxValue => .K)
Expand Down
8 changes: 8 additions & 0 deletions mx-rust-semantics/main/expression.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
```k
requires "expression/mx-to-rust.md"
requires "expression/raw-value.md"
requires "expression/rust-to-mx.md"
requires "expression/strings.md"
requires "expression/struct.md"
module MX-RUST-EXPRESSION
imports private MX-RUST-EXPRESSION-MX-TO-RUST
imports private MX-RUST-EXPRESSION-RAW-VALUE
imports private MX-RUST-EXPRESSION-RUST-TO-MX
imports private MX-RUST-EXPRESSION-STRINGS
imports private MX-RUST-EXPRESSION-STRUCT
endmodule
```
85 changes: 85 additions & 0 deletions mx-rust-semantics/main/expression/mx-to-rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
```k
module MX-RUST-EXPRESSION-MX-TO-RUST
imports private K-EQUAL-SYNTAX
imports private MX-COMMON-SYNTAX
imports private MX-RUST-REPRESENTATION
imports private RUST-HELPERS
imports private RUST-VALUE-SYNTAX
syntax Bool ::= isMxToRustValue(K) [function, total, symbol(isMxToRustValue)]
rule isMxToRustValue(_:K) => false [owise]
rule isMxToRustValue(_:PtrValue) => true
rule isMxToRustValue(mxToRustField(_, V:MxToRust)) => isMxToRustValue(V)
syntax Bool ::= isMxToRustFieldValue(K) [function, total, symbol(isMxToRustFieldValue)]
rule isMxToRustFieldValue(_:K) => false [owise]
rule isMxToRustFieldValue(_:PtrValue) => true
rule isMxToRustFieldValue(mxToRustField(_, V:MxToRust)) => isMxToRustValue(V)
rule isMxToRustFieldValue(.MxRustFieldValues) => true
rule isMxToRustFieldValue(A:MxRustFieldValue , As:MxRustFieldValues)
=> isMxToRustFieldValue(A) andBool isMxToRustFieldValue(As)
rule V:MxValue ~> mxToRustTyped(T:Type) => mxToRustTyped(T, V)
rule mxToRustTyped(T:Type, mxIntValue(I:Int)) => mxRustNewValue(integerToValue(I, T))
requires
(T ==K i32 orBool T ==K u32)
orBool (T ==K i64 orBool T ==K u64)
rule mxToRustTyped(str, mxStringValue(S:String)) => mxRustNewValue(S)
rule mxToRustTyped
( rustStructType(StructName:TypePath, Fields:MxRustStructFields)
, mxListValue(Values:MxValueList)
)
=> mxToRustStruct(StructName, pairFields(Fields, Values))
rule mxToRustTyped(() , mxUnitValue()) => ptrValue(null, tuple(.ValueList))
context HOLE:MxRustFieldValue , _:MxRustFieldValues [result(MxToRustFieldValue)]
context V:MxRustFieldValue , HOLE:MxRustFieldValues requires isMxToRustFieldValue(V)
[result(MxToRustFieldValue)]
syntax MxRustFieldValues ::= pairFields(MxRustStructFields, MxValueList) [function, total]
rule pairFields(.MxRustStructFields, .MxValueList) => .MxRustFieldValues
rule pairFields
( (mxRustStructField(Name:Identifier, T:MxRustType) , Fs:MxRustStructFields)
, (V:MxValue , Vs:MxValueList)
)
=> mxToRustField(Name, mxToRustTyped(T, V)) , pairFields(Fs, Vs)
rule pairFields(.MxRustStructFields, (_:MxValue , _:MxValueList) #as L:MxValueList)
=> error("Not enough fields", ListItem(L))
rule pairFields((_ , _:MxRustStructFields) #as F, .MxValueList)
=> error("Not enough values", ListItem(F))
rule pairFields(F:MxRustStructFields, V:MxValueList)
=> error("Should not happen (pairFields)", ListItem(F) ListItem(V))
[owise]
rule (.K => fieldsToMap(Fields, .Map))
~> mxToRustStruct(_StructName:TypePath, Fields:MxRustFieldValues)
requires isMxToRustFieldValue(Fields)
rule M:Map ~> mxToRustStruct(StructName:TypePath, _Fields:MxRustFieldValues)
=> mxRustNewValue(struct(StructName, M))
syntax MapOrError ::= fieldsToMap(MxRustFieldValues, Map) [function, total]
rule fieldsToMap(.MxRustFieldValues, M:Map) => M
rule fieldsToMap
( mxToRustField(Name:Identifier, ptrValue(ptr(I:Int), _:Value))
, Fields:MxRustFieldValues
, M
)
=> fieldsToMap(Fields, M[Name <- I])
requires notBool Name in_keys(M)
rule fieldsToMap
( ( (mxToRustField(Name:Identifier, _) #as Field:MxRustFieldValue)
, _Fields:MxRustFieldValues
)
, M
)
=> error("Field name already in map", ListItem(Field) ListItem(M))
requires Name in_keys(M)
rule fieldsToMap((Field , _:MxRustFieldValues), _:Map)
=> error("Unexpected field", ListItem(Field))
[owise]
endmodule
```
10 changes: 10 additions & 0 deletions mx-rust-semantics/main/expression/raw-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```k
module MX-RUST-EXPRESSION-RAW-VALUE
imports private MX-RUST-REPRESENTATION
rule rawValue(V:Value) => mxRustNewValue(V)
endmodule
```
37 changes: 37 additions & 0 deletions mx-rust-semantics/main/expression/rust-to-mx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
```k
module MX-RUST-EXPRESSION-RUST-TO-MX
imports private COMMON-K-CELL
imports private K-EQUAL-SYNTAX
imports private LIST
imports private MAP
imports private MX-COMMON-SYNTAX
imports private MX-RUST-REPRESENTATION-CONVERSIONS
imports private RUST-CONVERSIONS-SYNTAX
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-REPRESENTATION
rule V:MxValue ~> rustToMx => rustToMx(V)
rule
<k>
rustToMx(struct (_, Fields:Map))
=> rustValuesToMxListValue
( ptrListToValueList(listToPtrList(values(Fields)), Values)
, .MxValueList
)
...
</k>
<values> Values:Map </values>
[priority(200)]
rule rustToMx(S:String => mxStringValue(S))
rule rustToMx(tuple(V:ValueList)) => rustValuesToMxListValue(V, .MxValueList)
syntax RustMxInstruction ::= rustValuesToMxListValue(ValueListOrError, MxValueList)
rule rustValuesToMxListValue(.ValueList, L:MxValueList)
=> rustToMx(mxListValue(reverse(L, .MxValueList)))
rule (.K => rustToMx(HOLE)) ~> rustValuesToMxListValue(((HOLE:Value , V:ValueList) => V), _:MxValueList)
rule (rustToMx(HOLE:MxValue) => .K) ~> rustValuesToMxListValue(_:ValueList, (L:MxValueList => (HOLE, L)))
endmodule
```
40 changes: 40 additions & 0 deletions mx-rust-semantics/main/expression/struct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
```k
module MX-RUST-EXPRESSION-STRUCT
imports private MX-RUST-REPRESENTATION
syntax MapOrError ::= pair(MxRustStructFields, PtrList, Map) [function, total]
rule (.K => reverseNormalizeParams(Args, .PtrList))
~> mxRustNewStruct
( _
, Args:CallParamsList
)
rule reverseNormalizeParams(.CallParamsList, Ptrs:PtrList)
~> mxRustNewStruct
( rustStructType
( StructName:TypePath
, Fields:MxRustStructFields
)
, _:CallParamsList
)
=> Rust#newStruct(StructName, pair(Fields, reverse(Ptrs, .PtrList), .Map))
rule pair(.MxRustStructFields, .PtrList, M:Map) => M
rule pair
( (mxRustStructField(Name:Identifier, _) , Fs:MxRustStructFields) => Fs
, (ptr(P:Int) , Ps:PtrList) => Ps
, M:Map => M[Name <- P]
)
requires notBool Name in_keys(M)
rule pair(Fs, Ps, M)
=> error
( "Unspecified error (pair)"
, ListItem(Fs) ListItem(Ps) ListItem(M)
)
[owise]
endmodule
```
6 changes: 6 additions & 0 deletions mx-rust-semantics/main/glue.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module MX-RUST-GLUE
(T ==K i32 orBool T ==K u32)
orBool (T ==K i64 orBool T ==K u64)
rule
<k> ptr(I:Int) => ptrValue(ptr(I), V) ... </k>
<values> I |-> V:Value ... </values>
rule ptrValue(_, V) ~> rustValueToMx => rustValueToMx(V)
endmodule
```
6 changes: 6 additions & 0 deletions mx-rust-semantics/main/modules.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
```k
requires "modules/address.md"
requires "modules/biguint.md"
requires "modules/hooks.md"
requires "modules/proxy.md"
requires "modules/storage.md"
module MX-RUST-MODULES
imports private MX-RUST-MODULES-ADDRESS
imports private MX-RUST-MODULES-BIGUINT
imports private MX-RUST-MODULES-HOOKS
imports private MX-RUST-MODULES-PROXY
imports private MX-RUST-MODULES-STORAGE
endmodule
Expand Down
38 changes: 38 additions & 0 deletions mx-rust-semantics/main/modules/address.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
```k
module MX-RUST-MODULES-ADDRESS
imports private MX-COMMON-SYNTAX
imports private MX-RUST-REPRESENTATION
imports private MX-RUST-REPRESENTATION-CONVERSIONS
imports private RUST-SHARED-SYNTAX
syntax MxRustType ::= "addressType" [function, total]
rule addressType
=> rustStructType
( #token("ManagedAddress", "Identifier"):Identifier
, ( mxRustStructField
( #token("mx_address_value", "Identifier"):Identifier
, str
)
, .MxRustStructFields
)
)
rule mxRustEmptyValue(rustType(#token("ManagedAddress", "Identifier")))
=> mxToRustTyped(addressType, mxListValue(mxStringValue("")))
rule mxValueToRust(#token("ManagedAddress", "Identifier"), V:MxValue)
=> mxToRustTyped(addressType, mxListValue(V))
rule rustValueToMx
( struct
( #token("ManagedAddress", "Identifier"):Identifier
, #token("mx_address_value", "Identifier"):Identifier |-> AddressValueId:Int
_:Map
)
)
=> ptr(AddressValueId) ~> rustValueToMx
endmodule
```
Loading

0 comments on commit 7d3a018

Please sign in to comment.