Skip to content

Commit

Permalink
Storage hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Sep 2, 2024
1 parent d4f008f commit 3014724
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 3 deletions.
14 changes: 14 additions & 0 deletions mx-semantics/main/accounts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module MX-ACCOUNTS-CONFIGURATION
imports INT-SYNTAX
imports STRING-SYNTAX
imports MX-STORAGE-CONFIGURATION
configuration
<mx-accounts>
Expand All @@ -19,9 +20,22 @@ module MX-ACCOUNTS-CONFIGURATION
<mx-esdt-balance> 0 </mx-esdt-balance>
</mx-esdt-data>
</mx-esdt-datas>
<mx-account-storage/>
</mx-account>
</mx-accounts>
endmodule
module MX-STORAGE-CONFIGURATION
imports MX-COMMON-SYNTAX
configuration
<mx-account-storage>
<mx-account-storage-item multiplicity="*" type="Map">
<mx-account-storage-key> "" </mx-account-storage-key>
<mx-account-storage-value> mxWrappedEmpty </mx-account-storage-value>
</mx-account-storage-item>
</mx-account-storage>
endmodule
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module MX-ACCOUNTS-HOOKS
rule
<k> MX#bigIntGetESDTExternalBalance
( mxStringValue(Owner:String)
, mxStringValue(TokenId:String)
, mxIntValue(Nonce:Int)
, mxStringValue(_TokenId:String)
, mxIntValue(_Nonce:Int)
, .MxHookArgs
) => MX#bigIntNew(mxIntValue(0)) ... </k>
<mx-account-address> Owner </mx-account-address>
Expand Down
12 changes: 12 additions & 0 deletions mx-semantics/main/accounts/storage-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```k
module MX-STORAGE-HOOKS
imports MX-COMMON-SYNTAX
imports MX-STORAGE-TOOLS-SYNTAX
rule MX#storageLoad(mxStringValue(Key:String), Destination:MxValue )
=> storageLoad(getCallee(), Key, Destination)
endmodule
```
28 changes: 28 additions & 0 deletions mx-semantics/main/accounts/storage-tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```k
module MX-STORAGE-TOOLS-SYNTAX
imports MX-COMMON-SYNTAX
imports STRING-SYNTAX
syntax MxInstructions ::= storageLoad(address: String, key: String, destination: MxValue)
endmodule
module MX-STORAGE-TOOLS
imports private COMMON-K-CELL
imports private MX-ACCOUNTS-CONFIGURATION
imports private MX-COMMON-SYNTAX
imports private MX-STORAGE-TOOLS-SYNTAX
imports private STRING-SYNTAX
rule
<k>
storageLoad(... address: Address:String, key: _Key:String, destination: Destination:MxValue)
=> storeHostValue(Destination, mxWrappedEmpty)
...
</k>
<mx-account-address> Address </mx-account-address>
[priority(100)]
endmodule
```
1 change: 1 addition & 0 deletions mx-semantics/main/calls/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module MX-CALL-CONFIGURATION
configuration
<mx-call-data>
<mx-caller> "" </mx-caller>
<mx-callee> "" </mx-callee>
</mx-call-data>
endmodule
Expand Down
11 changes: 11 additions & 0 deletions mx-semantics/main/calls/tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```k
module MX-CALLS-TOOLS
imports MX-COMMON-SYNTAX
imports MX-CALL-CONFIGURATION
rule [[getCallee() => Callee]]
<mx-callee> Callee:String </mx-callee>
endmodule
```
8 changes: 7 additions & 1 deletion mx-semantics/main/mx-common.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
```k
requires "accounts/hooks.md"
requires "accounts/esdt-hooks.md"
requires "accounts/storage-hooks.md"
requires "accounts/storage-tools.md"
requires "biguint/hooks.md"
requires "blocks/hooks.md"
requires "calls/hooks.md"
requires "calls/tools.md"
module MX-COMMON
imports private MX-ACCOUNTS-HOOKS
imports private MX-BIGUINT-HOOKS
imports private MX-BLOCKS-HOOKS
imports private MX-CALLS-HOOKS
imports private MX-CALLS-TOOLS
imports private MX-STORAGE-HOOKS
imports private MX-STORAGE-TOOLS
endmodule
```
4 changes: 4 additions & 0 deletions mx-semantics/main/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ module MX-COMMON-SYNTAX
syntax MxValue ::= mxIntValue(Int)
| mxStringValue(String)
| MxWrappedValue
syntax MxWrappedValue ::= "mxWrappedEmpty"
syntax MxInstructions ::= storeHostValue(destination: MxValue, value: MxValue)
syntax MxHookName ::= r"MX#[a-zA-Z][a-zA-Z0-9]*" [token]
syntax MxHookArgs ::= List{MxValue, ","}
syntax HookCall ::= MxHookName "(" MxHookArgs ")"
syntax String ::= getCallee() [function, total]
endmodule
```
49 changes: 49 additions & 0 deletions mx-semantics/test/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ module MX-TEST-EXECUTION-PARSING-SYNTAX
syntax TestInstruction ::= "push" MxValue
| "call" argcount:Int MxHookName
| "get_big_int"
| "push_store_data"
| getBigint(Int)
| "check_eq" MxValue
| setCaller(String)
| setCallee(String)
| addAccount(String)
| setBalance(account:String, token:String, nonce:Int, value:Int)
| setStorage(account:String, key:String, value:MxValue)
| setBlockTimestamp(Int)
syntax MxTest ::= NeList{TestInstruction, ";"}
syntax MxValueStack ::= List{MxValue, ","}
syntax MxWrappedValue ::= wrappedMx(MxValue)
endmodule
module MX-TEST-EXECUTION
Expand Down Expand Up @@ -49,6 +54,14 @@ module MX-TEST-EXECUTION
<k> get_big_int => testGetBigInt(IntId) ... </k>
<mx-test-stack> mxIntValue(IntId) , L:MxValueStack => L </mx-test-stack>
rule
<k> storeHostValue (... destination: Destination:MxValue, value: Value:MxValue)
~> push_store_data ; Is
=> Is
...
</k>
<mx-test-stack> L:MxValueStack => Destination, Value, L </mx-test-stack>
rule
<k> check_eq V => .K ... </k>
<mx-test-stack> V , L:MxValueStack => L </mx-test-stack>
Expand Down Expand Up @@ -95,6 +108,10 @@ module MX-CALL-TEST
rule
<k> setCaller(S:String) => .K ... </k>
<mx-caller> _ => S </mx-caller>
rule
<k> setCallee(S:String) => .K ... </k>
<mx-callee> _ => S </mx-callee>
endmodule
module MX-ACCOUNTS-TEST
Expand All @@ -109,6 +126,7 @@ module MX-ACCOUNTS-TEST
=> <mx-account>
<mx-account-address> S </mx-account-address>
<mx-esdt-datas> .Bag </mx-esdt-datas>
<mx-account-storage> .Bag </mx-account-storage>
</mx-account>
</mx-accounts>
Expand Down Expand Up @@ -151,6 +169,37 @@ module MX-ACCOUNTS-TEST
</mx-esdt-datas>
[priority(100)]
rule
<k> setStorage
(... account: Account:String
, key: Key:String
, value: Value:MxValue
) => .K
...
</k>
<mx-account-address> Account </mx-account-address>
<mx-account-storage-key> Key </mx-account-storage-key>
<mx-account-storage-value> _ => Value </mx-account-storage-value>
[priority(50)]
rule
<k> setStorage
(... account: Account:String
, key: Key:String
, value: Value:MxValue
) => .K
...
</k>
<mx-account-address> Account </mx-account-address>
<mx-account-storage>
.Bag =>
<mx-account-storage-item>
<mx-account-storage-key> Key </mx-account-storage-key>
<mx-account-storage-value> wrappedMx(Value) </mx-account-storage-value>
</mx-account-storage-item>
</mx-account-storage>
[priority(100)]
endmodule
module MX-BLOCKS-TEST
Expand Down
10 changes: 10 additions & 0 deletions tests/mx/storage/data-storage.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
addAccount("Owner");
setCallee("Owner");
setStorage("Owner", "MyKey", mxStringValue("Hello"));

push mxIntValue(12);
push mxStringValue("MyKey");
call 2 MX#storageLoad;
push_store_data;
check_eq mxIntValue(12);
check_eq mxWrappedEmpty
9 changes: 9 additions & 0 deletions tests/mx/storage/empty-storage.mx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
addAccount("Owner");
setCallee("Owner");

push mxIntValue(12);
push mxStringValue("MyKey");
call 2 MX#storageLoad;
push_store_data;
check_eq mxIntValue(12);
check_eq mxWrappedEmpty

0 comments on commit 3014724

Please sign in to comment.