From c98f1caa20a4c1362400a7e233c4c2cdde461617 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 28 Aug 2024 18:54:02 +0300 Subject: [PATCH 1/3] MX getCaller hook --- mx-semantics/main/biguint/hooks.md | 10 +++++----- mx-semantics/main/calls/configuration.md | 12 ++++++++++++ mx-semantics/main/calls/hooks.md | 14 ++++++++++++++ mx-semantics/main/configuration.md | 3 +++ mx-semantics/main/mx-common.md | 2 ++ mx-semantics/main/syntax.md | 4 +++- mx-semantics/test/execution.md | 13 +++++++++++++ tests/mx/blockchain/get-caller.mx | 3 +++ 8 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 mx-semantics/main/calls/configuration.md create mode 100644 mx-semantics/main/calls/hooks.md create mode 100644 tests/mx/blockchain/get-caller.mx diff --git a/mx-semantics/main/biguint/hooks.md b/mx-semantics/main/biguint/hooks.md index 53697e6d..c7ba438 100644 --- a/mx-semantics/main/biguint/hooks.md +++ b/mx-semantics/main/biguint/hooks.md @@ -1,11 +1,11 @@ ```k module MX-BIGUINT-HOOKS - imports BOOL - imports COMMON-K-CELL - imports K-EQUAL-SYNTAX - imports MX-BIGUINT-CONFIGURATION - imports MX-COMMON-SYNTAX + imports private BOOL + imports private COMMON-K-CELL + imports private K-EQUAL-SYNTAX + imports private MX-BIGUINT-CONFIGURATION + imports private MX-COMMON-SYNTAX rule MX#bigIntNew(mxIntValue(Value:Int)) => mxIntValue(NextId) ... diff --git a/mx-semantics/main/calls/configuration.md b/mx-semantics/main/calls/configuration.md new file mode 100644 index 0000000..c509fd8 --- /dev/null +++ b/mx-semantics/main/calls/configuration.md @@ -0,0 +1,12 @@ +```k + +module MX-CALL-CONFIGURATION + imports STRING + + configuration + + "" + +endmodule + +``` \ No newline at end of file diff --git a/mx-semantics/main/calls/hooks.md b/mx-semantics/main/calls/hooks.md new file mode 100644 index 0000000..c4076e0 --- /dev/null +++ b/mx-semantics/main/calls/hooks.md @@ -0,0 +1,14 @@ +```k + +module MX-CALLS-HOOKS + imports private COMMON-K-CELL + imports private MX-CALL-CONFIGURATION + imports private MX-COMMON-SYNTAX + + rule + MX#getCaller ( .MxHookArgs ) => mxStringValue(Caller) ... + Caller:String + +endmodule + +``` \ No newline at end of file diff --git a/mx-semantics/main/configuration.md b/mx-semantics/main/configuration.md index 3301e9f..6d0125a 100644 --- a/mx-semantics/main/configuration.md +++ b/mx-semantics/main/configuration.md @@ -1,12 +1,15 @@ ```k requires "biguint/configuration.md" +requires "calls/configuration.md" module MX-COMMON-CONFIGURATION imports MX-BIGUINT-CONFIGURATION + imports MX-CALL-CONFIGURATION configuration + endmodule diff --git a/mx-semantics/main/mx-common.md b/mx-semantics/main/mx-common.md index 65f5201..354111c 100644 --- a/mx-semantics/main/mx-common.md +++ b/mx-semantics/main/mx-common.md @@ -1,9 +1,11 @@ ```k requires "biguint/hooks.md" +requires "calls/hooks.md" module MX-COMMON imports private MX-BIGUINT-HOOKS + imports private MX-CALLS-HOOKS endmodule ``` \ No newline at end of file diff --git a/mx-semantics/main/syntax.md b/mx-semantics/main/syntax.md index fdff7ae..66b1315 100644 --- a/mx-semantics/main/syntax.md +++ b/mx-semantics/main/syntax.md @@ -2,8 +2,10 @@ module MX-COMMON-SYNTAX imports INT-SYNTAX + imports STRING-SYNTAX - syntax MxValue ::= mxIntValue(Int) + syntax MxValue ::= mxIntValue(Int) + | mxStringValue(String) syntax MxHookName ::= r"MX#[a-zA-Z][a-zA-Z0-9]*" [token] syntax MxHookArgs ::= List{MxValue, ","} syntax HookCall ::= MxHookName "(" MxHookArgs ")" diff --git a/mx-semantics/test/execution.md b/mx-semantics/test/execution.md index 0b714aa..21eb6aa 100644 --- a/mx-semantics/test/execution.md +++ b/mx-semantics/test/execution.md @@ -3,12 +3,14 @@ module MX-TEST-EXECUTION-PARSING-SYNTAX imports INT-SYNTAX imports MX-COMMON-SYNTAX + imports STRING-SYNTAX syntax TestInstruction ::= "push" MxValue | "call" argcount:Int MxHookName | "get_big_int" | getBigint(Int) | "check_eq" MxValue + | setCaller(String) syntax MxTest ::= NeList{TestInstruction, ";"} @@ -19,6 +21,7 @@ module MX-TEST-EXECUTION imports private COMMON-K-CELL imports private INT imports private MX-BIGUINT-TEST + imports private MX-CALL-TEST imports private MX-TEST-CONFIGURATION imports private MX-TEST-EXECUTION-PARSING-SYNTAX @@ -79,4 +82,14 @@ module MX-BIGUINT-TEST endmodule +module MX-CALL-TEST + imports private COMMON-K-CELL + imports private MX-CALL-CONFIGURATION + imports private MX-TEST-EXECUTION-PARSING-SYNTAX + + rule + setCaller(S:String) => .K ... + _ => S +endmodule + ``` diff --git a/tests/mx/blockchain/get-caller.mx b/tests/mx/blockchain/get-caller.mx new file mode 100644 index 0000000..99fa7ae --- /dev/null +++ b/tests/mx/blockchain/get-caller.mx @@ -0,0 +1,3 @@ +setCaller("Owner"); +call 0 MX#getCaller; +check_eq mxStringValue("Owner") From f5f918581e3bb92ee23637ae7c48e12069acc12e Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 28 Aug 2024 19:48:45 +0300 Subject: [PATCH 2/3] Mx get-sc-balance hook --- mx-semantics/main/accounts/configuration.md | 27 ++++++++++ mx-semantics/main/accounts/hooks.md | 35 ++++++++++++ mx-semantics/main/configuration.md | 3 ++ mx-semantics/main/mx-common.md | 2 + mx-semantics/test/execution.md | 59 +++++++++++++++++++++ tests/mx/blockchain/get-sc-balance.mx | 17 ++++++ 6 files changed, 143 insertions(+) create mode 100644 mx-semantics/main/accounts/configuration.md create mode 100644 mx-semantics/main/accounts/hooks.md create mode 100644 tests/mx/blockchain/get-sc-balance.mx diff --git a/mx-semantics/main/accounts/configuration.md b/mx-semantics/main/accounts/configuration.md new file mode 100644 index 0000000..992f016 --- /dev/null +++ b/mx-semantics/main/accounts/configuration.md @@ -0,0 +1,27 @@ +```k + +module MX-ACCOUNTS-CONFIGURATION + imports INT-SYNTAX + imports STRING-SYNTAX + + configuration + + + // TODO: The address should be bytes. + "" + + + // TODO: The esdt-id should be bytes. + + "" + 0 + + 0 + + + + + +endmodule + +``` diff --git a/mx-semantics/main/accounts/hooks.md b/mx-semantics/main/accounts/hooks.md new file mode 100644 index 0000000..bd9440e --- /dev/null +++ b/mx-semantics/main/accounts/hooks.md @@ -0,0 +1,35 @@ +```k + +module MX-ACCOUNTS-HOOKS + imports private COMMON-K-CELL + imports private MX-ACCOUNTS-CONFIGURATION + imports private MX-COMMON-SYNTAX + + rule + MX#bigIntGetESDTExternalBalance + ( mxStringValue(Owner:String) + , mxStringValue(TokenId:String) + , mxIntValue(Nonce:Int) + , .MxHookArgs + ) => MX#bigIntNew(mxIntValue(Balance)) ... + Owner + + TokenId + Nonce + + Balance:Int + [priority(50)] + + rule + MX#bigIntGetESDTExternalBalance + ( mxStringValue(Owner:String) + , mxStringValue(TokenId:String) + , mxIntValue(Nonce:Int) + , .MxHookArgs + ) => MX#bigIntNew(mxIntValue(0)) ... + Owner + [priority(100)] + +endmodule + +``` \ No newline at end of file diff --git a/mx-semantics/main/configuration.md b/mx-semantics/main/configuration.md index 6d0125a..668158a 100644 --- a/mx-semantics/main/configuration.md +++ b/mx-semantics/main/configuration.md @@ -1,9 +1,11 @@ ```k +requires "accounts/configuration.md" requires "biguint/configuration.md" requires "calls/configuration.md" module MX-COMMON-CONFIGURATION + imports MX-ACCOUNTS-CONFIGURATION imports MX-BIGUINT-CONFIGURATION imports MX-CALL-CONFIGURATION @@ -11,6 +13,7 @@ module MX-COMMON-CONFIGURATION + endmodule diff --git a/mx-semantics/main/mx-common.md b/mx-semantics/main/mx-common.md index 354111c..275e749 100644 --- a/mx-semantics/main/mx-common.md +++ b/mx-semantics/main/mx-common.md @@ -1,9 +1,11 @@ ```k +requires "accounts/hooks.md" requires "biguint/hooks.md" requires "calls/hooks.md" module MX-COMMON + imports private MX-ACCOUNTS-HOOKS imports private MX-BIGUINT-HOOKS imports private MX-CALLS-HOOKS endmodule diff --git a/mx-semantics/test/execution.md b/mx-semantics/test/execution.md index 21eb6aa..42546cc 100644 --- a/mx-semantics/test/execution.md +++ b/mx-semantics/test/execution.md @@ -11,6 +11,8 @@ module MX-TEST-EXECUTION-PARSING-SYNTAX | getBigint(Int) | "check_eq" MxValue | setCaller(String) + | addAccount(String) + | setBalance(account:String, token:String, nonce:Int, value:Int) syntax MxTest ::= NeList{TestInstruction, ";"} @@ -20,6 +22,7 @@ endmodule module MX-TEST-EXECUTION imports private COMMON-K-CELL imports private INT + imports private MX-ACCOUNTS-TEST imports private MX-BIGUINT-TEST imports private MX-CALL-TEST imports private MX-TEST-CONFIGURATION @@ -92,4 +95,60 @@ module MX-CALL-TEST _ => S endmodule +module MX-ACCOUNTS-TEST + imports private COMMON-K-CELL + imports private MX-ACCOUNTS-CONFIGURATION + imports private MX-TEST-EXECUTION-PARSING-SYNTAX + + rule + addAccount(S:String) => .K ... + + .Bag + => + S + .Bag + + + + rule + setBalance + (... account: Account:String + , token: TokenName:String + , nonce: Nonce:Int + , value: Value:Int + ) => .K + ... + + Account + + TokenName + Nonce + + _ => Value + [priority(50)] + + rule + setBalance + (... account: Account:String + , token: TokenName:String + , nonce: Nonce:Int + , value: Value:Int + ) => .K + ... + + Account + + .Bag => + + + TokenName + Nonce + + Value + + + [priority(100)] + +endmodule + ``` diff --git a/tests/mx/blockchain/get-sc-balance.mx b/tests/mx/blockchain/get-sc-balance.mx new file mode 100644 index 0000000..8b03c0c --- /dev/null +++ b/tests/mx/blockchain/get-sc-balance.mx @@ -0,0 +1,17 @@ +addAccount("Owner"); + +push mxIntValue(0); +push mxStringValue("MyToken"); +push mxStringValue("Owner"); +call 3 MX#bigIntGetESDTExternalBalance; +get_big_int; +check_eq mxIntValue(0); + +setBalance("Owner", "MyToken", 0, 1234); + +push mxIntValue(0); +push mxStringValue("MyToken"); +push mxStringValue("Owner"); +call 3 MX#bigIntGetESDTExternalBalance; +get_big_int; +check_eq mxIntValue(1234) From c2082375ca0cffd3f22fecab277a52bef017c80c Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 28 Aug 2024 20:06:31 +0300 Subject: [PATCH 3/3] MX getBlockTimestamp hook --- mx-semantics/main/blocks/configuration.md | 15 +++++++++++++++ mx-semantics/main/blocks/hooks.md | 14 ++++++++++++++ mx-semantics/main/configuration.md | 3 +++ mx-semantics/main/mx-common.md | 2 ++ mx-semantics/test/execution.md | 13 +++++++++++++ tests/mx/blockchain/get-block-timestamp.mx | 3 +++ 6 files changed, 50 insertions(+) create mode 100644 mx-semantics/main/blocks/configuration.md create mode 100644 mx-semantics/main/blocks/hooks.md create mode 100644 tests/mx/blockchain/get-block-timestamp.mx diff --git a/mx-semantics/main/blocks/configuration.md b/mx-semantics/main/blocks/configuration.md new file mode 100644 index 0000000..e41e471 --- /dev/null +++ b/mx-semantics/main/blocks/configuration.md @@ -0,0 +1,15 @@ +```k + +module MX-BLOCKS-CONFIGURATION + imports INT-SYNTAX + + configuration + + + 0 + + + +endmodule + +``` diff --git a/mx-semantics/main/blocks/hooks.md b/mx-semantics/main/blocks/hooks.md new file mode 100644 index 0000000..22c0362 --- /dev/null +++ b/mx-semantics/main/blocks/hooks.md @@ -0,0 +1,14 @@ +```k + +module MX-BLOCKS-HOOKS + imports private COMMON-K-CELL + imports private MX-BLOCKS-CONFIGURATION + imports private MX-COMMON-SYNTAX + + rule + MX#getBlockTimestamp ( .MxHookArgs ) => mxIntValue(T) ... + T + +endmodule + +``` diff --git a/mx-semantics/main/configuration.md b/mx-semantics/main/configuration.md index 668158a..c6f0195 100644 --- a/mx-semantics/main/configuration.md +++ b/mx-semantics/main/configuration.md @@ -2,17 +2,20 @@ requires "accounts/configuration.md" requires "biguint/configuration.md" +requires "blocks/configuration.md" requires "calls/configuration.md" module MX-COMMON-CONFIGURATION imports MX-ACCOUNTS-CONFIGURATION imports MX-BIGUINT-CONFIGURATION + imports MX-BLOCKS-CONFIGURATION imports MX-CALL-CONFIGURATION configuration + endmodule diff --git a/mx-semantics/main/mx-common.md b/mx-semantics/main/mx-common.md index 275e749..27bb479 100644 --- a/mx-semantics/main/mx-common.md +++ b/mx-semantics/main/mx-common.md @@ -2,11 +2,13 @@ requires "accounts/hooks.md" requires "biguint/hooks.md" +requires "blocks/hooks.md" requires "calls/hooks.md" module MX-COMMON imports private MX-ACCOUNTS-HOOKS imports private MX-BIGUINT-HOOKS + imports private MX-BLOCKS-HOOKS imports private MX-CALLS-HOOKS endmodule diff --git a/mx-semantics/test/execution.md b/mx-semantics/test/execution.md index 42546cc..cef680c 100644 --- a/mx-semantics/test/execution.md +++ b/mx-semantics/test/execution.md @@ -13,6 +13,7 @@ module MX-TEST-EXECUTION-PARSING-SYNTAX | setCaller(String) | addAccount(String) | setBalance(account:String, token:String, nonce:Int, value:Int) + | setBlockTimestamp(Int) syntax MxTest ::= NeList{TestInstruction, ";"} @@ -24,6 +25,7 @@ module MX-TEST-EXECUTION imports private INT imports private MX-ACCOUNTS-TEST imports private MX-BIGUINT-TEST + imports private MX-BLOCKS-TEST imports private MX-CALL-TEST imports private MX-TEST-CONFIGURATION imports private MX-TEST-EXECUTION-PARSING-SYNTAX @@ -151,4 +153,15 @@ module MX-ACCOUNTS-TEST endmodule +module MX-BLOCKS-TEST + imports private COMMON-K-CELL + imports private MX-BLOCKS-CONFIGURATION + imports private MX-TEST-EXECUTION-PARSING-SYNTAX + + rule + setBlockTimestamp(T:Int) => .K ... + _ => T + +endmodule + ``` diff --git a/tests/mx/blockchain/get-block-timestamp.mx b/tests/mx/blockchain/get-block-timestamp.mx new file mode 100644 index 0000000..fc9a5c7 --- /dev/null +++ b/tests/mx/blockchain/get-block-timestamp.mx @@ -0,0 +1,3 @@ +setBlockTimestamp(1234); +call 0 MX#getBlockTimestamp; +check_eq mxIntValue(1234)