Skip to content

Commit

Permalink
Partial implementation of the blockchain() function (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta authored Sep 30, 2024
1 parent db6c0ef commit 950fb75
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mx-rust-semantics/main/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
requires "modules/address.md"
requires "modules/biguint.md"
requires "modules/blockchain.md"
requires "modules/call-value.md"
requires "modules/hooks.md"
requires "modules/managed-vec.md"
Expand All @@ -13,6 +14,7 @@ requires "modules/token-identifier.md"
module MX-RUST-MODULES
imports private MX-RUST-MODULES-ADDRESS
imports private MX-RUST-MODULES-BIGUINT
imports private MX-RUST-MODULES-BLOCKCHAIN
imports private MX-RUST-MODULES-CALL-VALUE
imports private MX-RUST-MODULES-HOOKS
imports private MX-RUST-MODULES-MANAGED-VEC
Expand Down
55 changes: 55 additions & 0 deletions mx-rust-semantics/main/modules/blockchain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```k
module MX-RUST-MODULES-BLOCKCHAIN
imports private COMMON-K-CELL
imports private MX-COMMON-SYNTAX
imports private MX-RUST-REPRESENTATION
imports private MX-RUST-REPRESENTATION-CONVERSIONS
imports private RUST-EXECUTION-CONFIGURATION
imports private RUST-SHARED-SYNTAX
syntax Identifier ::= "MxRust#Blockchain" [token]
syntax MxRustStructType ::= "blockchainType" [function, total]
rule blockchainType
=> rustStructType
( MxRust#Blockchain
, .MxRustStructFields
)
rule
normalizedMethodCall
( MxRust#Blockchain
, #token("new", "Identifier"):Identifier
, .PtrList
)
=> mxRustNewStruct
( blockchainType
, .CallParamsList
)
rule
normalizedMethodCall
( MxRust#Blockchain
, #token("get_caller", "Identifier"):Identifier
, ( ptr(_SelfId:Int)
, .PtrList
)
)
=> MX#getCaller ( .MxValueList )
~> mxValueToRust(#token("ManagedAddress", "Identifier"):Identifier)
rule
normalizedMethodCall
( MxRust#Blockchain
, #token("get_block_timestamp", "Identifier"):Identifier
, ( ptr(_SelfId:Int)
, .PtrList
)
)
=> MX#getBlockTimestamp ( .MxValueList )
~> mxValueToRust(u64)
endmodule
```
9 changes: 9 additions & 0 deletions mx-rust-semantics/main/preprocessing/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module MX-RUST-PREPROCESSING-CONTRACT
syntax MxRustInstruction ::= rustMxAddContractSend(TypePath)
| rustMxAddContractCallValue(TypePath)
| rustMxAddContractBlockchain(TypePath)
| rustMxAddContractGenericMethod
( trait: TypePath
, method: Identifier
Expand All @@ -16,6 +17,7 @@ module MX-RUST-PREPROCESSING-CONTRACT
rule rustMxAddContractMethods(Trait:TypePath)
=> rustMxAddContractSend(Trait:TypePath)
~> rustMxAddContractCallValue(Trait:TypePath)
~> rustMxAddContractBlockchain(Trait:TypePath)
rule rustMxAddContractSend(Trait:TypePath)
=> rustMxAddContractGenericMethod
Expand All @@ -31,6 +33,13 @@ module MX-RUST-PREPROCESSING-CONTRACT
, struct: #token("MxRust#CallValue", "Identifier")
)
rule rustMxAddContractBlockchain(Trait:TypePath)
=> rustMxAddContractGenericMethod
(... trait: Trait
, method: #token("blockchain", "Identifier")
, struct: #token("MxRust#Blockchain", "Identifier")
)
rule
<k>
rustMxAddContractGenericMethod
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions tests/mx-rust-contracts/blockchain.caller.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
setCallee("Owner");

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

push_return_value;
check_eq mxStringValue("Owner")
23 changes: 23 additions & 0 deletions tests/mx-rust-contracts/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_std]

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

#[multiversx_sc::contract]
pub trait Blockchain {
#[init]
fn init(&self) {}

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

#[endpoint(getCaller)]
fn get_caller(&self) -> ManagedAddress {
self.blockchain().get_caller()
}

#[endpoint(getBlockTimestamp)]
fn get_block_timestamp(&self) -> u64 {
self.blockchain().get_block_timestamp()
}
}
Empty file.
14 changes: 14 additions & 0 deletions tests/mx-rust-contracts/blockchain.timestamp.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
setCallee("Owner");
setBlockTimestamp(123);

push mxListValue();
push mxStringValue("getBlockTimestamp");
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(123)

0 comments on commit 950fb75

Please sign in to comment.