Skip to content

Commit

Permalink
Merge branch 'main' into snyk-upgrade-9fa09018da46a5ae8bdf51f10fc5b155
Browse files Browse the repository at this point in the history
  • Loading branch information
iduartgomez authored Oct 10, 2023
2 parents 81aadc8 + 7d85d79 commit 39a5a66
Show file tree
Hide file tree
Showing 24 changed files with 206 additions and 179 deletions.
256 changes: 122 additions & 134 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ tracing = "0.1"
tracing-subscriber = "0.3"
wasmer = "4.2.0"

# freenet-stdlib = { path = "./stdlib/rust/", version = "0.0.8" }
freenet-stdlib = { version = "0.0.7" }
freenet-stdlib = { path = "./stdlib/rust/", version = "0.0.8", features = ["unstable"] }
# freenet-stdlib = { version = "0.0.7" }

[profile.dev.package."*"]
opt-level = 3
Expand Down
3 changes: 2 additions & 1 deletion apps/freenet-email-app/contracts/inbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ thiserror = "1"
crate-type = ["cdylib", "rlib"]

[features]
default = []
default = ["freenet-main-contract"]
contract = []
freenet-main-contract = []
wasmbind = ["chrono/wasmbind"]
4 changes: 4 additions & 0 deletions apps/freenet-email-app/web/container/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ freenet-stdlib = { workspace = true }

[lib]
crate-type = ["cdylib"]

[features]
default = ["freenet-main-contract"]
freenet-main-contract = []
4 changes: 4 additions & 0 deletions apps/freenet-microblogging/contracts/posts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ crate-type = ["cdylib"]
[build-dependencies]
serde = "1"
serde_json = "1"

[features]
default = ["freenet-main-contract"]
freenet-main-contract = []
3 changes: 3 additions & 0 deletions apps/freenet-microblogging/web/container/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ freenet-stdlib = { workspace = true }

[lib]
crate-type = ["cdylib"]

[features]
freenet-main-contract = []
2 changes: 1 addition & 1 deletion crates/core/src/contract/storages/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl StateStorage for RocksDb {
"failed getting contract: `{key}` {}",
key.encoded_code_hash()
.map(|ch| format!("(with code hash: `{ch}`)"))
.unwrap_or(String::new())
.unwrap_or_default()
);
Ok(None)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//! - final location
use std::{
borrow::Borrow,
collections::BTreeMap,
convert::TryFrom,
fmt::Display,
Expand Down Expand Up @@ -426,7 +425,7 @@ impl From<&ContractKey> for Location {
fn from(key: &ContractKey) -> Self {
let mut value = 0.0;
let mut divisor = 256.0;
for byte in key.borrow().bytes().iter().take(7) {
for byte in key.bytes().iter().take(7) {
value += *byte as f64 / divisor;
divisor *= 256.0;
}
Expand Down Expand Up @@ -460,7 +459,7 @@ impl Ord for Location {

impl PartialOrd for Location {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(&other.0)
Some(self.cmp(other))
}
}

Expand Down Expand Up @@ -510,15 +509,16 @@ impl PartialEq for Distance {

impl PartialOrd for Distance {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(&other.0)
Some(self.cmp(other))
}
}

impl Eq for Distance {}

impl Ord for Distance {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other)
self.0
.partial_cmp(&other.0)
.expect("always should return a cmp value")
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/router/isotonic_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl IsotonicEstimator {

self.peer_adjustments
.entry(event.peer)
.or_insert_with(Adjustment::default)
.or_default()
.add(adjustment);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/runtime/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ macro_rules! impl_err {
}

impl_err!(Box<dyn std::error::Error + Send + Sync>);
impl_err!(freenet_stdlib::buf::Error);
impl_err!(freenet_stdlib::memory::buf::Error);
impl_err!(std::io::Error);
impl_err!(secrets_store::SecretStoreError);
impl_err!(bincode::Error);
Expand All @@ -100,7 +100,7 @@ pub(crate) enum RuntimeInnerError {
Any(#[from] Box<dyn std::error::Error + Send + Sync>),

#[error(transparent)]
BufferError(#[from] freenet_stdlib::buf::Error),
BufferError(#[from] freenet_stdlib::memory::buf::Error),

#[error(transparent)]
IOError(#[from] std::io::Error),
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/runtime/wasm_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{collections::HashMap, sync::atomic::AtomicI64};

use freenet_stdlib::{
buf::{BufferBuilder, BufferMut},
memory::{
buf::{BufferBuilder, BufferMut},
WasmLinearMem,
},
prelude::*,
};
use wasmer::{imports, Bytes, Imports, Instance, Memory, MemoryType, Module, Store, TypedFunction};
Expand Down Expand Up @@ -148,7 +151,7 @@ impl Runtime {
let data = data.as_ref();
let initiate_buffer: TypedFunction<u32, i64> = instance
.exports
.get_typed_function(&self.wasm_store, "initiate_buffer")?;
.get_typed_function(&self.wasm_store, "__frnt__initiate_buffer")?;
let builder_ptr = initiate_buffer.call(&mut self.wasm_store, data.len() as u32)?;
let linear_mem = self.linear_mem(instance)?;
unsafe {
Expand All @@ -166,10 +169,7 @@ impl Runtime {
.map(Ok)
.unwrap_or_else(|| instance.exports.get_memory("memory"))?
.view(&self.wasm_store);
Ok(WasmLinearMem {
start_ptr: memory.data_ptr() as *const _,
size: memory.data_size(),
})
Ok(unsafe { WasmLinearMem::new(memory.data_ptr() as *const _, memory.data_size()) })
}

pub(super) fn prepare_contract_call(
Expand Down
33 changes: 18 additions & 15 deletions crates/fdev/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,42 @@ pub fn build_package(cli_config: BuildToolCliConfig, cwd: &Path) -> Result<(), D
}
}

fn compile_rust_wasm_lib(cli_config: &BuildToolCliConfig, work_dir: &Path) -> Result<(), DynError> {
let package_type = cli_config.package_type;
const RUST_TARGET_ARGS: &[&str] = &["build", "--lib", "--target"];
fn compile_options(cli_config: &BuildToolCliConfig) -> impl Iterator<Item = &str> {
let release: &[&str] = if cli_config.debug {
&[]
} else {
&["--release"]
};
let target = WASM_TARGET;
let cmd_args = cli_config
.features
.as_ref()
.iter()
.flat_map(|x| ["--features", x.as_str()])
.chain(release.iter().copied())
.collect::<Vec<_>>();
let feature_list = cli_config.features.iter().flat_map(|s| {
s.split(',')
.filter(|p| *p != cli_config.package_type.feature())
});
let features = ["--features", cli_config.package_type.feature()]
.into_iter()
.chain(feature_list);
features.chain(release.iter().copied())
}

fn compile_rust_wasm_lib(cli_config: &BuildToolCliConfig, work_dir: &Path) -> Result<(), DynError> {
const RUST_TARGET_ARGS: &[&str] = &["build", "--lib", "--target"];
use std::io::IsTerminal;
let cmd_args = if std::io::stdout().is_terminal() && std::io::stderr().is_terminal() {
RUST_TARGET_ARGS
.iter()
.copied()
.chain([target, "--color", "always"])
.chain(cmd_args)
.chain([WASM_TARGET, "--color", "always"])
.chain(compile_options(cli_config))
.collect::<Vec<_>>()
} else {
RUST_TARGET_ARGS
.iter()
.copied()
.chain([target])
.chain(cmd_args)
.chain([WASM_TARGET])
.chain(compile_options(cli_config))
.collect::<Vec<_>>()
};

let package_type = cli_config.package_type;
println!("Compiling {package_type} with rust");
let child = Command::new("cargo")
.args(&cmd_args)
Expand Down
9 changes: 9 additions & 0 deletions crates/fdev/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ pub(crate) enum PackageType {
Delegate,
}

impl PackageType {
pub fn feature(&self) -> &'static str {
match self {
PackageType::Contract => "freenet-main-contract",
PackageType::Delegate => "freenet-main-delegate",
}
}
}

impl Display for PackageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ freenet-aft-interface = { path = "../../interfaces" }

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["freenet-main-contract"]
freenet-main-contract = []
4 changes: 4 additions & 0 deletions modules/antiflood-tokens/delegates/token-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ tracing-subscriber = { version = "0.3.16", features = ["env-filter", "fmt"] }

[lib]
crate-type = ["cdylib"]

[features]
default = ["freenet-main-delegate"]
freenet-main-delegate = []
1 change: 1 addition & 0 deletions modules/identity-management/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ crate-type = ["cdylib", "rlib"]
[features]
default = []
contract = ["freenet-stdlib/contract"]
freenet-main-delegate = []
6 changes: 3 additions & 3 deletions modules/identity-management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl DelegateInterface for IdentityManagement {
let msg = IdentityMsg::try_from(&*payload)?;
let action = match msg {
IdentityMsg::CreateIdentity { alias, key, extra } => {
#[cfg(all(target_family = "wasm", feature = "contract"))]
#[cfg(feature = "contract")]
{
freenet_stdlib::log::info(&format!(
"create alias new {alias} for {}",
Expand All @@ -136,7 +136,7 @@ impl DelegateInterface for IdentityManagement {
serde_json::to_vec(&IdentityMsg::DeleteIdentity { alias }).unwrap()
}
IdentityMsg::Init => {
#[cfg(all(target_family = "wasm", feature = "contract"))]
#[cfg(feature = "contract")]
{
freenet_stdlib::log::info(&format!(
"initialize secret {}",
Expand Down Expand Up @@ -165,7 +165,7 @@ impl DelegateInterface for IdentityManagement {
context,
..
}) => {
#[cfg(all(target_family = "wasm", feature = "contract"))]
#[cfg(feature = "contract")]
{
freenet_stdlib::log::info(&format!(
"got request for {}",
Expand Down
4 changes: 2 additions & 2 deletions tests/test-contract-1/Cargo.lock

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

4 changes: 3 additions & 1 deletion tests/test-contract-1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
freenet-stdlib = { path = "../../stdlib/rust" }
freenet-stdlib = { path = "../../stdlib/rust", features = ["contract"] }

[features]
default = ["freenet-main-contract"]
freenet-main-contract = []
trace = ["freenet-stdlib/trace"]
4 changes: 2 additions & 2 deletions tests/test-contract-2/Cargo.lock

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

2 changes: 2 additions & 0 deletions tests/test-contract-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ crate-type = ["cdylib"]
freenet-stdlib = { path = "../../stdlib/rust", features = ["contract"] }

[features]
default = ["freenet-main-contract"]
freenet-main-contract = []
trace = ["freenet-stdlib/trace"]
4 changes: 2 additions & 2 deletions tests/test-delegate-1/Cargo.lock

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

4 changes: 3 additions & 1 deletion tests/test-delegate-1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
freenet-stdlib = { path = "../../stdlib/rust" }
freenet-stdlib = { path = "../../stdlib/rust", features = ["contract"]}
serde = "1"
serde_json = "1"
bincode = "1"

[features]
default = ["freenet-main-delegate"]
freenet-main-delegate = []
trace = ["freenet-stdlib/trace"]

0 comments on commit 39a5a66

Please sign in to comment.