Skip to content

Commit

Permalink
Merge branch '184058031-contract-resource-usage' of github.com:freene…
Browse files Browse the repository at this point in the history
…t/locutus into 184058031-contract-resource-usage
  • Loading branch information
sanity committed Nov 13, 2023
2 parents 63ae386 + e6422b0 commit be751b4
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ target/
.idea/*
**/.vscode/*

# Other config files
config.toml
.env
.rustc*
expanded_code.rs

4 changes: 1 addition & 3 deletions crates/core/src/runtime/contract_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,11 @@ impl ContractStore {

#[cfg(test)]
mod test {
use tempfile::TempDir;

use super::*;

#[test]
fn store_and_load() -> Result<(), Box<dyn std::error::Error>> {
let contract_dir = TempDir::new()?;
let contract_dir = crate::util::tests_util::get_temp_dir();
std::fs::create_dir_all(contract_dir.path())?;
let mut store = ContractStore::new(contract_dir.path().into(), 10_000)?;
let contract = WrappedContract::new(
Expand Down
10 changes: 6 additions & 4 deletions crates/core/src/runtime/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ mod test {
use freenet_stdlib::prelude::*;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tempfile::TempDir;

use crate::util::tests_util::get_temp_dir;

use super::super::{delegate_store::DelegateStore, ContractStore, SecretsStore};
use super::*;
Expand All @@ -416,9 +417,9 @@ mod test {

fn setup_runtime(
name: &str,
) -> Result<(DelegateContainer, Runtime, TempDir), Box<dyn std::error::Error>> {
) -> Result<(DelegateContainer, Runtime, tempfile::TempDir), Box<dyn std::error::Error>> {
// let _ = tracing_subscriber::fmt().with_env_filter("info").try_init();
let temp_dir = TempDir::new().expect("Failed to create a temporary directory");
let temp_dir = get_temp_dir();
let contracts_dir = temp_dir.path().join("contracts");
let delegates_dir = temp_dir.path().join("delegates");
let secrets_dir = temp_dir.path().join("secrets");
Expand Down Expand Up @@ -455,7 +456,7 @@ mod test {
Arc::new(ContractCode::from(vec![1])),
Parameters::from(vec![]),
);
let (delegate, mut runtime, _temp_dir) = setup_runtime(TEST_DELEGATE_1)?;
let (delegate, mut runtime, temp_dir) = setup_runtime(TEST_DELEGATE_1)?;
let app = ContractInstanceId::try_from(contract.key.to_string()).unwrap();

// CreateInboxRequest message parts
Expand Down Expand Up @@ -489,6 +490,7 @@ mod test {
outbound.get(0),
Some(OutboundDelegateMsg::ApplicationMessage(msg)) if *msg.payload == expected_payload
));
std::mem::drop(temp_dir);
Ok(())
}
}
9 changes: 5 additions & 4 deletions crates/core/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,10 @@ mod tests {
sync::{Arc, Barrier},
};

use crate::util::tests_util::get_temp_dir;

use super::*;
use dashmap::DashMap;
use tempfile::TempDir;

struct TestStore1;

Expand Down Expand Up @@ -375,7 +376,7 @@ mod tests {

#[test]
fn test_store() {
let temp_dir = TempDir::new().expect("Failed to create a temporary directory");
let temp_dir = crate::util::tests_util::get_temp_dir();
let contract_keys_file_path = temp_dir.path().join("contract_keys");
let delegate_keys_file_path = temp_dir.path().join("delegate_keys");

Expand Down Expand Up @@ -453,7 +454,7 @@ mod tests {
fn test_concurrent_updates() {
const NUM_THREADS: usize = 4;

let temp_dir = TempDir::new().expect("Failed to create a temporary directory");
let temp_dir = get_temp_dir();
let contract_keys_file_path = temp_dir.path().join("contract_keys");
std::fs::File::create(&contract_keys_file_path).expect("Failed to create file");

Expand Down Expand Up @@ -513,7 +514,7 @@ mod tests {

#[test]
fn test_concurrent_compaction() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let temp_dir = get_temp_dir();
let key_file_path = temp_dir.path().join("data.dat");
std::fs::File::create(&key_file_path).expect("Failed to create file");

Expand Down
17 changes: 10 additions & 7 deletions crates/core/src/runtime/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn validate_state() -> Result<(), Box<dyn std::error::Error>> {
delegate_store,
secrets_store,
contract_key,
temp_dir: _,
temp_dir,
} = super::setup_test_contract(TEST_CONTRACT_1)?;
let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap();

Expand All @@ -33,7 +33,7 @@ fn validate_state() -> Result<(), Box<dyn std::error::Error>> {
&Default::default(),
)?;
assert!(matches!(not_valid, ValidateResult::RequestRelated(_)));

std::mem::drop(temp_dir);
Ok(())
}

Expand All @@ -44,7 +44,7 @@ fn validate_delta() -> Result<(), Box<dyn std::error::Error>> {
delegate_store,
secrets_store,
contract_key,
temp_dir: _,
temp_dir,
} = super::setup_test_contract(TEST_CONTRACT_1)?;
let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap();

Expand All @@ -61,7 +61,7 @@ fn validate_delta() -> Result<(), Box<dyn std::error::Error>> {
&StateDelta::from([1, 0, 0, 1].as_ref()),
)?;
assert!(not_valid);

std::mem::drop(temp_dir);
Ok(())
}

Expand All @@ -72,7 +72,7 @@ fn update_state() -> Result<(), Box<dyn std::error::Error>> {
delegate_store,
secrets_store,
contract_key,
temp_dir: _,
temp_dir,
} = super::setup_test_contract(TEST_CONTRACT_1)?;
let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap();

Expand All @@ -86,6 +86,7 @@ fn update_state() -> Result<(), Box<dyn std::error::Error>> {
.unwrap_valid();
assert!(new_state.as_ref().len() == 4);
assert!(new_state.as_ref()[3] == 4);
std::mem::drop(temp_dir);
Ok(())
}

Expand All @@ -96,7 +97,7 @@ fn summarize_state() -> Result<(), Box<dyn std::error::Error>> {
delegate_store,
secrets_store,
contract_key,
temp_dir: _,
temp_dir,
} = super::setup_test_contract(TEST_CONTRACT_1)?;
let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap();

Expand All @@ -106,6 +107,7 @@ fn summarize_state() -> Result<(), Box<dyn std::error::Error>> {
&WrappedState::new(vec![5, 2, 3, 4]),
)?;
assert_eq!(summary.as_ref(), &[5, 2, 3]);
std::mem::drop(temp_dir);
Ok(())
}

Expand All @@ -116,7 +118,7 @@ fn get_state_delta() -> Result<(), Box<dyn std::error::Error>> {
delegate_store,
secrets_store,
contract_key,
temp_dir: _,
temp_dir,
} = super::setup_test_contract(TEST_CONTRACT_1)?;
let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap();

Expand All @@ -128,5 +130,6 @@ fn get_state_delta() -> Result<(), Box<dyn std::error::Error>> {
)?;
assert!(delta.as_ref().len() == 1);
assert!(delta.as_ref()[0] == 4);
std::mem::drop(temp_dir);
Ok(())
}
7 changes: 4 additions & 3 deletions crates/core/src/runtime/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::{
use freenet_stdlib::prelude::{
ContractCode, ContractContainer, ContractKey, ContractWasmAPIVersion, WrappedContract,
};
use tempfile::TempDir;

use crate::util::tests_util::get_temp_dir;

use super::{ContractStore, DelegateStore, SecretsStore};

Expand Down Expand Up @@ -49,7 +50,7 @@ pub(crate) fn get_test_module(name: &str) -> Result<Vec<u8>, Box<dyn std::error:

pub(crate) struct TestSetup {
#[allow(unused)]
temp_dir: TempDir,
temp_dir: tempfile::TempDir,
contract_store: ContractStore,
delegate_store: DelegateStore,
secrets_store: SecretsStore,
Expand All @@ -58,7 +59,7 @@ pub(crate) struct TestSetup {

pub(crate) fn setup_test_contract(name: &str) -> Result<TestSetup, Box<dyn std::error::Error>> {
// let _ = tracing_subscriber::fmt().with_env_filter("info").try_init();
let temp_dir = TempDir::new().expect("Failed to create a temporary directory");
let temp_dir = get_temp_dir();

let mut contract_store = ContractStore::new(temp_dir.path().join("contract"), 10_000)?;
let delegate_store = DelegateStore::new(temp_dir.path().join("delegate"), 10_000)?;
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/runtime/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn now() -> Result<(), Box<dyn std::error::Error>> {
delegate_store,
secrets_store,
contract_key,
temp_dir: _,
temp_dir,
} = super::setup_test_contract("test_contract_2")?;
let mut runtime = Runtime::build(contract_store, delegate_store, secrets_store, false).unwrap();

Expand All @@ -22,5 +22,6 @@ fn now() -> Result<(), Box<dyn std::error::Error>> {
.get_function("time_func")?
.typed(&runtime.wasm_store)?;
f.call(&mut runtime.wasm_store)?;
std::mem::drop(temp_dir);
Ok(())
}
18 changes: 18 additions & 0 deletions crates/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,21 @@ impl<'x> Contains<PeerKey> for &'x [&PeerKey] {
self.contains(&target)
}
}

#[cfg(test)]
pub mod tests_util {
use rand::Rng;
use tempfile::TempDir;

/// Use this to guarantee unique directory names in case you are running multiple tests in parallel.
pub fn get_temp_dir() -> TempDir {
tempfile::Builder::new()
.suffix(
&(0..8)
.map(|_| rand::thread_rng().gen::<char>())
.collect::<String>(),
)
.tempdir()
.expect("Failed to create a temporary directory")
}
}
4 changes: 0 additions & 4 deletions tests/test-contract-2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit be751b4

Please sign in to comment.