Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit a0c040b

Browse files
Implement MultiEncodedVec (#111)
1 parent 950fb75 commit a0c040b

17 files changed

+194
-12
lines changed

mx-rust-semantics/main/modules.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ requires "modules/blockchain.md"
66
requires "modules/call-value.md"
77
requires "modules/hooks.md"
88
requires "modules/managed-vec.md"
9+
requires "modules/multi-value-encoded.md"
910
requires "modules/proxy.md"
1011
requires "modules/send.md"
1112
requires "modules/storage.md"
@@ -18,6 +19,7 @@ module MX-RUST-MODULES
1819
imports private MX-RUST-MODULES-CALL-VALUE
1920
imports private MX-RUST-MODULES-HOOKS
2021
imports private MX-RUST-MODULES-MANAGED-VEC
22+
imports private MX-RUST-MODULES-MULTI-VALUE-ENCODED
2123
imports private MX-RUST-MODULES-PROXY
2224
imports private MX-RUST-MODULES-SEND
2325
imports private MX-RUST-MODULES-STORAGE

mx-rust-semantics/main/modules/managed-vec.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ module MX-RUST-MODULES-MANAGED-VEC
8181
~> clearValue
8282
~> ptrValue(null, tuple(.ValueList))
8383
84+
rule
85+
normalizedMethodCall
86+
( #token("ManagedVec", "Identifier"):Identifier
87+
, #token("into", "Identifier"):Identifier
88+
, ( ptr(SelfId) , .PtrList)
89+
)
90+
=> into(ptr(SelfId))
91+
8492
// --------------------------------------
8593
8694
syntax MxRustType ::= "managedVecType" [function, total]
@@ -102,7 +110,7 @@ module MX-RUST-MODULES-MANAGED-VEC
102110
rule mxRustEmptyValue(rustType(#token("ManagedVec", "Identifier")))
103111
=> mxToRustTyped
104112
( managedVecType
105-
, mxListValue(mxUnitValue() , mxRustType(()) , .MxValueList)
113+
, mxListValue(mxListValue(.MxValueList) , mxRustType(()) , .MxValueList)
106114
)
107115
108116
rule mxValueToRust(#token("ManagedVec", "Identifier"), V:MxValue)
@@ -144,6 +152,25 @@ module MX-RUST-MODULES-MANAGED-VEC
144152
...
145153
</values>
146154
[priority(20), label(xyzzy)]
155+
156+
// --------------------------------------
157+
158+
syntax MxRustInstruction ::= into(Expression) [strict]
159+
160+
rule into
161+
( ptrValue
162+
( _
163+
, struct
164+
( _
165+
, ( #token("mx_buffer_id", "Identifier"):Identifier |-> _
166+
#token("value_type", "Identifier"):Identifier |-> _
167+
.Map
168+
) #as M:Map
169+
)
170+
)
171+
)
172+
=> ptrValue(null, struct(#token("MultiValueEncoded", "Identifier"):Identifier , M))
173+
147174
endmodule
148175
149176
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
```k
2+
3+
module MX-RUST-MODULES-MULTI-VALUE-ENCODED
4+
imports private MX-RUST-REPRESENTATION
5+
imports private RUST-SHARED-SYNTAX
6+
7+
syntax MxRustInstruction ::= toVec(Expression) [strict]
8+
9+
rule
10+
normalizedMethodCall
11+
( #token("MultiValueEncoded", "Identifier"):Identifier
12+
, #token("to_vec", "Identifier"):Identifier
13+
, ( ptr(SelfId) , .PtrList)
14+
)
15+
=> toVec(ptr(SelfId))
16+
17+
rule toVec
18+
( ptrValue
19+
( _
20+
, struct
21+
( _
22+
, ( #token("mx_buffer_id", "Identifier"):Identifier |-> _
23+
#token("value_type", "Identifier"):Identifier |-> _
24+
.Map
25+
) #as M:Map
26+
)
27+
)
28+
)
29+
=> ptrValue(null, struct(#token("ManagedVec", "Identifier"):Identifier , M))
30+
31+
// --------------------------------------
32+
33+
syntax MxRustType ::= "multiValueEncodedType" [function, total]
34+
rule multiValueEncodedType
35+
=> rustStructType
36+
( #token("MultiValueEncoded", "Identifier"):Identifier
37+
, ( mxRustStructField
38+
( #token("mx_buffer_id", "Identifier"):Identifier
39+
, MxRust#buffer
40+
)
41+
, mxRustStructField
42+
( #token("value_type", "Identifier"):Identifier
43+
, MxRust#Type
44+
)
45+
, .MxRustStructFields
46+
)
47+
)
48+
49+
rule mxValueToRust
50+
( #token("MultiValueEncoded", "Identifier") < ValueType:Type , .GenericArgList >
51+
, V:MxValue
52+
)
53+
=> mxToRustTyped(multiValueEncodedType, mxListValue(V , mxRustType(ValueType) , .MxValueList))
54+
55+
rule rustValueToMx
56+
( struct
57+
( #token("MultiValueEncoded", "Identifier"):Identifier
58+
, #token("mx_buffer_id", "Identifier"):Identifier |-> VecValueId:Int
59+
#token("value_type", "Identifier"):Identifier |-> _:Int
60+
.Map
61+
)
62+
)
63+
=> mxRustGetBuffer(ptr(VecValueId))
64+
65+
syntax MxRustInstruction ::= mxRustGetBuffer(Expression) [strict]
66+
rule mxRustGetBuffer(ptrValue(_, i32(BufferId:MInt{32})))
67+
=> mxGetBuffer(MInt2Unsigned(BufferId))
68+
69+
// --------------------------------------
70+
71+
endmodule
72+
73+
```

mx-semantics/main/biguint/tools.md

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ module MX-BIGUINT-TOOLS
55
imports private MX-BIGUINT-CONFIGURATION
66
imports private MX-COMMON-SYNTAX
77
8-
rule
9-
<k> clearBigInts => .K ... </k>
10-
<bigIntHeap> _ => .Map </bigIntHeap>
11-
<bigIntHeapNextId> _ => 0 </bigIntHeapNextId>
12-
138
rule
149
<k> mxGetBigInt(IntId:Int) => mxIntValue(V) ... </k>
1510
<bigIntHeap> IntId |-> V:Int ... </bigIntHeap>

mx-semantics/main/buffers/hooks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ module MX-BUFFERS-HOOKS
2020
2121
// extern int32_t mBufferNewFromBytes(void* context, int32_t dataOffset, int32_t dataLength);
2222
rule
23-
<k> MX#mBufferNewFromValue(V:MxValue, .MxValueList) => mxIntValue(NextId) ... </k>
24-
<buffer-heap> Values:Map => Values[NextId <- mxListValue(V, .MxValueList)] </buffer-heap>
23+
<k> MX#mBufferNewFromValue((mxListValue(_) #as V:MxValue), .MxValueList) => mxIntValue(NextId) ... </k>
24+
<buffer-heap> Values:Map => Values[NextId <- V] </buffer-heap>
2525
<buffer-heap-next-id> NextId => NextId +Int 1 </buffer-heap-next-id>
2626
2727
// extern int32_t mBufferSetBytes(void* context, int32_t mBufferHandle, int32_t dataOffset, int32_t dataLength);

mx-semantics/main/buffers/tools.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```k
2+
3+
module MX-BUFFER-TOOLS
4+
imports private COMMON-K-CELL
5+
imports private MX-BUFFERS-CONFIGURATION
6+
imports private MX-COMMON-SYNTAX
7+
8+
rule
9+
<k> mxGetBuffer(BufferId:Int) => V ... </k>
10+
<buffer-heap> BufferId |-> V:MxValue ... </buffer-heap>
11+
endmodule
12+
13+
```

mx-semantics/main/call-state/configuration.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ module MX-CALL-STATE-CONFIGURATION
1313
<mx-call-state>
1414
<mx-call-data/>
1515
<mx-return-values/>
16-
<mx-biguint/>
17-
<mx-buffers/>
16+
<mx-internal-state>
17+
<mx-biguint/>
18+
<mx-buffers/>
19+
</mx-internal-state>
1820
</mx-call-state>
1921
endmodule
2022

mx-semantics/main/call-state/tools.md

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ module MX-CALL-STATE-TOOLS
3434
</mx-call-state>
3535
)
3636
37+
rule
38+
<k> clearMxInternalState => .K ... </k>
39+
(_:MxInternalStateCell =>
40+
<mx-internal-state>
41+
<mx-biguint>
42+
<bigIntHeapNextId> 0 </bigIntHeapNextId>
43+
...
44+
</mx-biguint>
45+
...
46+
</mx-internal-state>
47+
)
48+
3749
endmodule
3850
3951
```

mx-semantics/main/calls/tools.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ module MX-CALLS-TOOLS
360360
(... function: FunctionName:String
361361
, callData: CallData:MxCallDataCell
362362
)
363-
=> clearBigInts ~> host.mkCall(FunctionName)
363+
=> clearMxInternalState ~> host.mkCall(FunctionName)
364364
...
365365
</k>
366366
(_:MxCallDataCell => CallData)

mx-semantics/main/mx-common.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ requires "biguint/hooks.md"
99
requires "biguint/tools.md"
1010
requires "blocks/hooks.md"
1111
requires "buffers/hooks.md"
12+
requires "buffers/tools.md"
1213
requires "call-state/tools.md"
1314
requires "calls/hooks.md"
1415
requires "calls/tools.md"
@@ -22,6 +23,7 @@ module MX-COMMON
2223
imports private MX-ACCOUNTS-TOOLS
2324
imports private MX-BIGUINT-HOOKS
2425
imports private MX-BIGUINT-TOOLS
26+
imports private MX-BUFFER-TOOLS
2527
imports private MX-BLOCKS-HOOKS
2628
imports private MX-BUFFERS-HOOKS
2729
imports private MX-CALL-STATE-TOOLS

mx-semantics/main/syntax.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module MX-COMMON-SYNTAX
5050
| "pushWorldState" [symbol(pushWorldState)]
5151
| "dropWorldState" [symbol(dropWorldState)]
5252
| "popWorldState" [symbol(popWorldState)]
53-
| "clearBigInts" [symbol(clearBigInts)]
53+
| "clearMxInternalState" [symbol(clearMxInternalState)]
5454
| "endCall" [symbol(endCall)]
5555
| "finishExecuteOnDestContext" [symbol(finishExecuteOnDestContext)]
5656
| processBuiltinFunction(BuiltinFunction, String, String, MxCallDataCell)
@@ -60,6 +60,7 @@ module MX-COMMON-SYNTAX
6060
| storeHostValue(destination: MxValue, value: MxValue)
6161
| returnCallData(MxValue) [symbol(returnCallData)]
6262
| mxGetBigInt(Int) [symbol(mxGetBigInt)]
63+
| mxGetBuffer(Int) [symbol(mxGetBuffer)]
6364
| callContract(function: String, input: MxCallDataCell )
6465
[symbol(callContractString)]
6566

rust-semantics/helpers.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ module RUST-HELPERS
1717
rule isSameType(i32(_), i32) => true
1818
rule isSameType(u32(_), u32) => true
1919
rule isSameType(struct(T, _), T:Type) => true
20+
rule isSameType(struct(T, _), T:Identifier _:GenericArgs ) => true
21+
2022
endmodule
2123
```

tests/mx-rust-contracts/multi-value-encoded.1.args

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
setCallee("Owner");
2+
3+
push mxListValue(mxListValue(mxIntValue(123), mxIntValue(456)), mxIntValue(1));
4+
push mxStringValue("toVecGet");
5+
push mxIntValue(0);
6+
push mxTransfersValue();
7+
push mxIntValue(0);
8+
push mxStringValue("TestContract");
9+
call 6 MX#managedExecuteOnDestContext;
10+
check_eq mxIntValue(0);
11+
12+
push_return_value;
13+
check_eq mxIntValue(456)

tests/mx-rust-contracts/multi-value-encoded.2.args

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
setCallee("Owner");
2+
3+
push mxListValue(mxIntValue(123), mxIntValue(456));
4+
push mxStringValue("intoMve");
5+
push mxIntValue(0);
6+
push mxTransfersValue();
7+
push mxIntValue(0);
8+
push mxStringValue("TestContract");
9+
call 6 MX#managedExecuteOnDestContext;
10+
check_eq mxIntValue(0);
11+
12+
push_return_value;
13+
check_eq mxListValue(mxIntValue(123), mxIntValue(456))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![no_std]
2+
3+
#[allow(unused_imports)]
4+
use multiversx_sc::imports::*;
5+
6+
#[multiversx_sc::contract]
7+
pub trait Contract {
8+
#[init]
9+
fn init(&self) {}
10+
11+
#[upgrade]
12+
fn upgrade(&self) {}
13+
14+
#[endpoint(toVecGet)]
15+
fn to_vec_get(&self, v: MultiValueEncoded<BigUint>, idx: u64) -> BigUint {
16+
let mut values:ManagedVec<BigUint> = v.to_vec();
17+
values.get(idx)
18+
}
19+
20+
#[endpoint(intoMve)]
21+
fn into_mve(&self, a: u64, b:u64) -> MultiValueEncoded<BigUint> {
22+
let mut values:ManagedVec<BigUint> = ManagedVec::new();
23+
values.push(BigUint::from(a));
24+
values.push(BigUint::from(b));
25+
values.into()
26+
}
27+
}

0 commit comments

Comments
 (0)