Skip to content

Commit 8065a2a

Browse files
Merge pull request #2013 from multiversx/check-state-partial-keys
fix for checking partial keys with checkState
2 parents 950fd83 + 8ed1d1c commit 8065a2a

File tree

8 files changed

+158
-2
lines changed

8 files changed

+158
-2
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"steps": [
3+
{
4+
"step": "setState",
5+
"accounts": {
6+
"address:owner": {
7+
"nonce": "1"
8+
}
9+
}
10+
},
11+
{
12+
"step": "setState",
13+
"newAddresses": [
14+
{
15+
"creatorAddress": "address:owner",
16+
"creatorNonce": "1",
17+
"newAddress": "sc:scenario-tester"
18+
}
19+
]
20+
},
21+
{
22+
"step": "scDeploy",
23+
"id": "",
24+
"tx": {
25+
"from": "address:owner",
26+
"contractCode": "mxsc:../output/scenario-tester.mxsc.json",
27+
"arguments": [
28+
"0x01"
29+
],
30+
"gasLimit": "5,000,000"
31+
},
32+
"expect": {
33+
"out": [
34+
"0x696e69742d726573756c74"
35+
],
36+
"status": "0"
37+
}
38+
},
39+
{
40+
"step": "scCall",
41+
"id": "",
42+
"tx": {
43+
"from": "address:owner",
44+
"to": "sc:scenario-tester",
45+
"function": "set_other_mapper",
46+
"arguments": [
47+
"0x536f6d6556616c7565496e53746f72616765"
48+
],
49+
"gasLimit": "5,000,000"
50+
},
51+
"expect": {
52+
"out": [],
53+
"status": "0"
54+
}
55+
},
56+
{
57+
"step": "checkState",
58+
"accounts": {
59+
"address:owner": {
60+
"nonce": "*",
61+
"balance": "0",
62+
"storage": {},
63+
"code": ""
64+
},
65+
"sc:scenario-tester": {
66+
"storage": {
67+
"str:otherMapper": "str:SomeValueInStorage",
68+
"+": ""
69+
},
70+
"code": "*",
71+
"owner": "address:owner"
72+
}
73+
}
74+
}
75+
]
76+
}

contracts/feature-tests/scenario-tester/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub trait ScenarioTester {
1212
#[storage_mapper("sum")]
1313
fn sum(&self) -> SingleValueMapper<BigUint>;
1414

15+
#[view(getOtherMapper)]
16+
#[storage_mapper("otherMapper")]
17+
fn other_mapper(&self) -> SingleValueMapper<ManagedBuffer>;
18+
1519
/// Return value for testing reasons.
1620
#[init]
1721
fn init(&self, initial_value: BigUint) -> &'static str {
@@ -30,6 +34,12 @@ pub trait ScenarioTester {
3034
self.sum().update(|sum| *sum += value);
3135
}
3236

37+
/// Sets a value at another key
38+
#[endpoint]
39+
fn set_other_mapper(&self, value: ManagedBuffer) {
40+
self.other_mapper().set(value);
41+
}
42+
3343
/// Tests "from" conversion for MultiValueN parameters
3444
#[endpoint]
3545
fn multi_param(&self, _value: MultiValue2<BigUint, BigUint>) {}

contracts/feature-tests/scenario-tester/src/scenario_tester_proxy.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ where
9999
.original_result()
100100
}
101101

102+
pub fn other_mapper(
103+
self,
104+
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedBuffer<Env::Api>> {
105+
self.wrapped_tx
106+
.payment(NotPayable)
107+
.raw_call("getOtherMapper")
108+
.original_result()
109+
}
110+
102111
/// Add desired amount to the storage variable.
103112
pub fn add<
104113
Arg0: ProxyArg<BigUint<Env::Api>>,
@@ -113,6 +122,20 @@ where
113122
.original_result()
114123
}
115124

125+
/// Sets a value at another key
126+
pub fn set_other_mapper<
127+
Arg0: ProxyArg<ManagedBuffer<Env::Api>>,
128+
>(
129+
self,
130+
value: Arg0,
131+
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
132+
self.wrapped_tx
133+
.payment(NotPayable)
134+
.raw_call("set_other_mapper")
135+
.argument(&value)
136+
.original_result()
137+
}
138+
116139
/// Tests "from" conversion for MultiValueN parameters
117140
pub fn multi_param<
118141
Arg0: ProxyArg<MultiValue2<BigUint<Env::Api>, BigUint<Env::Api>>>,

contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,37 @@ fn st_blackbox_returns_result_or_error() {
387387
))
388388
);
389389
}
390+
391+
#[test]
392+
fn st_blackbox_storage_check_test() {
393+
let mut world = world();
394+
395+
world.account(OWNER_ADDRESS).nonce(1);
396+
397+
// set value for sum in storage
398+
let new_address = world
399+
.tx()
400+
.from(OWNER_ADDRESS)
401+
.typed(scenario_tester_proxy::ScenarioTesterProxy)
402+
.init(BigUint::from(1u64))
403+
.code(CODE_PATH)
404+
.new_address(ST_ADDRESS)
405+
.returns(ReturnsNewAddress)
406+
.run();
407+
408+
assert_eq!(new_address, ST_ADDRESS.to_address());
409+
410+
// set value for otherMapper in storage
411+
world
412+
.tx()
413+
.from(OWNER_ADDRESS)
414+
.to(ST_ADDRESS)
415+
.typed(scenario_tester_proxy::ScenarioTesterProxy)
416+
.set_other_mapper(b"SomeValueInStorage")
417+
.run();
418+
419+
// only check value for one key (partial check)
420+
world
421+
.check_account(ST_ADDRESS)
422+
.check_storage("str:otherMapper", "str:SomeValueInStorage");
423+
}

contracts/feature-tests/scenario-tester/tests/st_scenario_go_test.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ fn interactor_trace_go() {
1313
fn st_adder_go() {
1414
world().run("scenarios/st-adder.scen.json");
1515
}
16+
17+
#[test]
18+
fn st_partial_key_check_go() {
19+
world().run("scenarios/st-partial-key-check.scen.json");
20+
}

contracts/feature-tests/scenario-tester/tests/st_scenario_rs_test.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ fn interactor_trace_rs() {
2020
fn st_adder_rs() {
2121
world().run("scenarios/st-adder.scen.json");
2222
}
23+
24+
#[test]
25+
fn st_partial_key_check_rs() {
26+
world().run("scenarios/st-partial-key-check.scen.json");
27+
}

contracts/feature-tests/scenario-tester/wasm/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
// Init: 1
88
// Upgrade: 1
9-
// Endpoints: 5
9+
// Endpoints: 7
1010
// Async Callback (empty): 1
11-
// Total number of exported functions: 8
11+
// Total number of exported functions: 10
1212

1313
#![no_std]
1414

@@ -21,7 +21,9 @@ multiversx_sc_wasm_adapter::endpoints! {
2121
init => init
2222
upgrade => upgrade
2323
getSum => sum
24+
getOtherMapper => other_mapper
2425
add => add
26+
set_other_mapper => set_other_mapper
2527
multi_param => multi_param
2628
multi_return => multi_return
2729
sc_panic => sc_panic

framework/scenario/src/facade/world_tx/scenario_check_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ impl<'w> CheckStateBuilder<'w> {
218218
CheckStorage::Star => CheckStorageDetails::default(),
219219
CheckStorage::Equal(details) => details.clone(),
220220
};
221+
details.other_storages_allowed = true;
221222
details.storages.insert(
222223
BytesKey::interpret_from(key, &InterpreterContext::default()),
223224
CheckValue::Equal(BytesValue::interpret_from(

0 commit comments

Comments
 (0)