Skip to content

Commit 84c5668

Browse files
committed
Fixing TrackingCopy caching mechanism so it doesn't return Some(_) when a key was pruned. Fixing Vector implementation.
1 parent 5b14ced commit 84c5668

File tree

10 files changed

+238
-120
lines changed

10 files changed

+238
-120
lines changed

executor/wasm/tests/collections.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ fn inserting_into_non_existing_vec_index_fails() {
152152
.expect("should build");
153153
let res = run_wasm_session(&mut executor, &global_state, state_root_hash, run_method);
154154
assert!(res.is_ok());
155+
155156
if let Ok(res) = res {
156-
let host_error = res.host_error;
157-
assert!(host_error.is_some());
158-
if let Some(err) = host_error {
159-
println!("XXX {:?}", err);
160-
}
157+
assert!(matches!(
158+
res.host_error,
159+
Some(casper_executor_wasm_common::error::CallError::NotCallable)
160+
));
161161
}
162162
}

executor/wasm_host/src/host/global_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ pub(crate) fn host_write<S: GlobalStateReader + 'static>(
354354
Keyspace::EntryPoint(_) => return Ok(HOST_ERROR_INVALID_INPUT),
355355
};
356356

357+
error!("XXXX2");
357358
metered_write(caller, global_state_key, stored_value)?;
358359

359360
Ok(HOST_ERROR_SUCCESS)

smart_contracts/contracts/vm2/vm2-collections-test/src/vector/assert.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::{panic, ptr::NonNull};
1+
use core::ptr::NonNull;
22

33
use alloc::{vec, vec::Vec};
44
use casper_contract_sdk::{
@@ -54,7 +54,21 @@ pub(crate) fn should_retain_assert(vec: &mut Vector<u64>) {
5454
assert_eq!(vec, vec![2, 4]);
5555
}
5656

57-
pub(crate) fn test_vec_assert(vec: &mut Vector<u64>) {
57+
// Assert for scenario in which deletes happen in previous
58+
// state hash
59+
pub(crate) fn test_vec_1_assert() {
60+
assert_eq!(
61+
get_vec_elements_from_storage("test_vec_1"),
62+
vec![41, 43, 42, 111, 222, 333]
63+
);
64+
65+
let vec2 = Vector::<u64>::new("test1");
66+
assert_eq!(vec2.get(0), None);
67+
68+
assert_eq!(get_vec_elements_from_storage("test1"), Vec::<u64>::new());
69+
}
70+
71+
pub(crate) fn test_vec_2_assert(vec: &mut Vector<u64>) {
5872
assert_eq!(vec.remove(5), Some(334));
5973
assert_eq!(vec.remove(55), None);
6074

@@ -81,7 +95,7 @@ pub(crate) fn test_vec_assert(vec: &mut Vector<u64>) {
8195
}
8296

8397
assert_eq!(
84-
get_vec_elements_from_storage("test_vec"),
98+
get_vec_elements_from_storage("test_vec_2"),
8599
vec![41, 43, 42, 111, 222, 333]
86100
);
87101

smart_contracts/contracts/vm2/vm2-collections-test/src/vector/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ use prepare::*;
99

1010
#[casper]
1111
pub(crate) struct VectorTestData {
12-
should_not_panic_with_empty: Vector<u64>,
1312
should_retain: Vector<u64>,
14-
test_vec: Vector<u64>,
13+
should_not_panic_with_empty: Vector<u64>,
14+
test_vec_1: Vector<u64>,
15+
test_vec_2: Vector<u64>,
1516
test_pop: Vector<u64>,
1617
test_contains: Vector<u64>,
1718
test_clear: Vector<u64>,
@@ -26,9 +27,10 @@ pub(crate) struct VectorTestData {
2627
impl VectorTestData {
2728
pub(crate) fn new() -> Self {
2829
Self {
29-
should_not_panic_with_empty: should_not_panic_with_empty_prepare(),
3030
should_retain: should_retain_prepare(),
31-
test_vec: test_vec_prepare(),
31+
should_not_panic_with_empty: should_not_panic_with_empty_prepare(),
32+
test_vec_1: test_vec_1_prepare(),
33+
test_vec_2: test_vec_2_prepare(),
3234
test_pop: test_pop_prepare(),
3335
test_contains: test_contains_prepare(),
3436
test_clear: test_clear_prepare(),
@@ -44,7 +46,7 @@ impl VectorTestData {
4446
pub(crate) fn do_assertions(&mut self) {
4547
let should_not_panic_with_empty = &mut self.should_not_panic_with_empty;
4648
let should_retain = &mut self.should_retain;
47-
let test_vec = &mut self.test_vec;
49+
let test_vec_2 = &mut self.test_vec_2;
4850
let test_pop = &mut self.test_pop;
4951
let test_contains = &mut self.test_contains;
5052
let test_clear = &mut self.test_clear;
@@ -56,7 +58,8 @@ impl VectorTestData {
5658
let test_remove_invalid_index = &mut self.test_remove_invalid_index;
5759
should_not_panic_with_empty_assert(should_not_panic_with_empty);
5860
should_retain_assert(should_retain);
59-
test_vec_assert(test_vec);
61+
test_vec_1_assert();
62+
test_vec_2_assert(test_vec_2);
6063
test_pop_assert(test_pop);
6164
test_contains_assert(test_contains);
6265
test_clear_assert(test_clear);

smart_contracts/contracts/vm2/vm2-collections-test/src/vector/prepare.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use casper_contract_sdk::{casper, collections::Vector};
1+
use casper_contract_sdk::collections::Vector;
22

33
use crate::types::VectorTestStruct;
44

@@ -22,8 +22,51 @@ pub(crate) fn should_retain_prepare() -> Vector<u64> {
2222
vec
2323
}
2424

25-
pub(crate) fn test_vec_prepare() -> Vector<u64> {
26-
let mut vec = Vector::new("test_vec");
25+
pub(crate) fn test_vec_1_prepare() -> Vector<u64> {
26+
let mut vec = Vector::new("test_vec_1");
27+
28+
assert!(vec.get(0).is_none());
29+
vec.push(111);
30+
assert_eq!(vec.get(0), Some(111));
31+
vec.push(222);
32+
assert_eq!(vec.get(1), Some(222));
33+
34+
vec.insert(0, 42);
35+
vec.insert(0, 41);
36+
vec.insert(1, 43);
37+
vec.insert(5, 333);
38+
vec.insert(5, 334);
39+
assert_eq!(vec.remove(5), Some(334));
40+
assert_eq!(vec.remove(55), None);
41+
42+
/*let to_return = vec.clone();
43+
let mut iter = (&vec).iter();
44+
assert_eq!(iter.next(), Some(41));
45+
assert_eq!(iter.next(), Some(43));
46+
assert_eq!(iter.next(), Some(42));
47+
assert_eq!(iter.next(), Some(111));
48+
assert_eq!(iter.next(), Some(222));
49+
assert_eq!(iter.next(), Some(333));
50+
assert_eq!(iter.next(), None);
51+
*/
52+
{
53+
let ser = borsh::to_vec(&vec).unwrap();
54+
let deser: Vector<u64> = borsh::from_slice(&ser).unwrap();
55+
let mut iter = deser.iter();
56+
assert_eq!(iter.next(), Some(41));
57+
assert_eq!(iter.next(), Some(43));
58+
assert_eq!(iter.next(), Some(42));
59+
assert_eq!(iter.next(), Some(111));
60+
assert_eq!(iter.next(), Some(222));
61+
assert_eq!(iter.next(), Some(333));
62+
assert_eq!(iter.next(), None);
63+
}
64+
65+
vec
66+
}
67+
68+
pub(crate) fn test_vec_2_prepare() -> Vector<u64> {
69+
let mut vec = Vector::new("test_vec_2");
2770

2871
assert!(vec.get(0).is_none());
2972
vec.push(111);

smart_contracts/vm2/sdk/src/casper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ pub enum GetEnvInfoError {
368368

369369
thread_local! {
370370
/// Env info cache for the current execution
371-
static ENV_INFO: RefCell<Option<EnvInfo>> = RefCell::new(None);
371+
static ENV_INFO: RefCell<Option<EnvInfo>> = const {RefCell::new(None)};
372372
}
373373

374374
/// Get the environment info.

smart_contracts/vm2/sdk/src/casper/native.rs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ pub struct ExpectedCall {
149149
result_code: u32,
150150
}
151151

152+
type CreateInputExpectationTuple<'a> = (
153+
Option<&'a [u8]>,
154+
u64,
155+
Option<&'a str>,
156+
Option<&'a [u8]>,
157+
Option<&'a [u8; 32]>,
158+
Option<&'a [u8]>,
159+
);
160+
161+
type UpgradeInputExpectationTuple<'a> = (&'a [u8], Option<&'a str>, Option<&'a [u8]>);
162+
152163
impl ExpectedCall {
153164
pub fn new(
154165
input_match: Option<Vec<u8>>,
@@ -362,33 +373,22 @@ impl ExpectedCall {
362373
)
363374
}
364375

365-
pub fn expect_read(key: Option<&Keyspace>, output: &[u8]) -> Self {
366-
Self::expect_read_with_result_code(key, output, HOST_ERROR_SUCCESS)
367-
}
368-
369-
pub fn expect_read_with_result_code(
376+
pub fn expect_read(
370377
input_expectation: Option<&Keyspace>,
371-
output: &[u8],
378+
maybe_output: Option<&[u8]>,
372379
result_code: u32,
373380
) -> Self {
374381
let input_data = input_expectation.map(|x| x.to_host_input_data().unwrap());
375382
Self::new(
376383
input_data,
377384
Some(GlobalStateFunctionOption::Read as u32),
378-
Some(output.to_vec()),
385+
maybe_output.map(|x| x.to_vec()),
379386
result_code,
380387
)
381388
}
382389

383390
pub fn expect_create(
384-
input_expectation: Option<(
385-
Option<&[u8]>,
386-
u64,
387-
Option<&str>,
388-
Option<&[u8]>,
389-
Option<&[u8; 32]>,
390-
Option<&[u8]>,
391-
)>,
391+
input_expectation: Option<CreateInputExpectationTuple>,
392392
output: Option<CreateResult>,
393393
result_code: u32,
394394
) -> Self {
@@ -469,7 +469,7 @@ impl ExpectedCall {
469469
}
470470

471471
pub fn expect_upgrade(
472-
input_expectation: Option<(&[u8], Option<&str>, Option<&[u8]>)>,
472+
input_expectation: Option<UpgradeInputExpectationTuple>,
473473
return_code: u32,
474474
) -> Self {
475475
Self::new(
@@ -573,13 +573,12 @@ impl Environment for EnvironmentMock {
573573
alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8,
574574
alloc_ctx: *const core::ffi::c_void,
575575
) -> u32 {
576-
let expectation = self.deque_expectation().expect(
577-
format!(
576+
let expectation = self.deque_expectation().unwrap_or_else(|| {
577+
panic!(
578578
"Trying to call `casper_ffi` (ffi_opt={}) without enqueued mock results",
579579
ffi_opt
580580
)
581-
.as_str(),
582-
);
581+
});
583582
if let Some(expected_ffi_opt) = expectation.ffi_opt_match {
584583
assert_eq!(
585584
expected_ffi_opt, ffi_opt,
@@ -674,11 +673,10 @@ where
674673
{
675674
use std::panic;
676675
let call_result = panic::catch_unwind(func);
677-
let res = match call_result {
676+
match call_result {
678677
Ok(t) => Ok(t),
679678
Err(error) => Err(NativeTrap::Panic(error)),
680-
};
681-
res
679+
}
682680
}
683681

684682
pub fn set_env<T: Environment + 'static>(stub: Arc<T>) {
@@ -736,12 +734,28 @@ mod tests {
736734
);
737735

738736
let key_3 = Keyspace::NamedKey("abc");
739-
env.add_expectation(ExpectedCall::expect_read(Some(&key_3), b"value 2"));
737+
env.add_expectation(ExpectedCall::expect_read(
738+
Some(&key_3),
739+
Some(b"value 2"),
740+
HOST_ERROR_SUCCESS,
741+
));
740742
assert_eq!(casper::read_into_vec(key_3), Ok(Some(b"value 2".to_vec())));
741743

742-
let key_3 = Keyspace::NamedKey("abc");
743-
env.add_expectation(ExpectedCall::expect_read(Some(&key_3), &[]));
744-
assert_eq!(casper::read_into_vec(key_3), Err(HostResult::InvalidInput));
744+
let key_4 = Keyspace::NamedKey("abc2");
745+
env.add_expectation(ExpectedCall::expect_read(
746+
Some(&key_4),
747+
Some(&[5]),
748+
HOST_ERROR_SUCCESS,
749+
));
750+
assert_eq!(casper::read_into_vec(key_4), Ok(Some(vec![5])));
751+
752+
let key_5 = Keyspace::NamedKey("abc3");
753+
env.add_expectation(ExpectedCall::expect_read(
754+
Some(&key_5),
755+
None,
756+
HOST_ERROR_INVALID_INPUT,
757+
));
758+
assert_eq!(casper::read_into_vec(key_5), Err(HostResult::InvalidInput));
745759

746760
env.add_expectation(ExpectedCall::expect_get_info(Some(EnvInfo {
747761
protocol_version_major: 2,

0 commit comments

Comments
 (0)