Skip to content

Various changes required for river integration #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Freenet Standard Library

Please see https://docs.freenet.org/ for more information. Bug reports, feature requests, questions should be created in the [freenet-core](https://github.com/freenet/freenet-core/issues/) repo.
Please see https://docs.freenet.org/ for more information. Bug reports, feature requests, questions
should be created in the [freenet-core](https://github.com/freenet/freenet-core/issues/) repo.
5 changes: 2 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "freenet-stdlib"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
rust-version = "1.71.1"
publish = true
Expand Down Expand Up @@ -57,7 +57,6 @@ version = "0.3"
optional = true

[target.'cfg(any(unix, windows))'.dev-dependencies]
arbitrary = { version = "1", features = ["derive"] }
bincode = "1"
wasmer = { version = "5.0.4", features = [ "sys-default"] }
rand = { version = "0.8", features = ["small_rng"] }
Expand All @@ -68,5 +67,5 @@ contract = []
unstable = []
freenet-main-contract = []
net = ["dep:tokio", "dep:tokio-tungstenite", "dep:wasm-bindgen", "dep:web-sys", "dep:js-sys", "dep:serde-wasm-bindgen"]
testing = ["arbitrary"]
testing = ["dep:arbitrary"]
trace = []
76 changes: 0 additions & 76 deletions rust/examples/typed_contract.rs

This file was deleted.

4 changes: 2 additions & 2 deletions rust/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ mod regular;
#[cfg(all(any(unix, windows), feature = "net"))]
pub use regular::*;

#[cfg(all(target_family = "wasm", feature = "net", not(feature = "contract")))]
#[cfg(all(target_family = "wasm", feature = "net"))]
mod browser;
#[cfg(all(target_family = "wasm", feature = "net", not(feature = "contract")))]
#[cfg(all(target_family = "wasm", feature = "net"))]
pub use browser::*;

pub use client_events::*;
Expand Down
22 changes: 21 additions & 1 deletion rust/src/client_api/client_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,14 @@ impl ClientRequest<'_> {
contract,
state,
related_contracts,
subscribe,
} => {
let related_contracts = related_contracts.into_owned();
ContractRequest::Put {
contract,
state,
related_contracts,
subscribe,
}
}
ContractRequest::Update { key, data } => {
Expand All @@ -285,9 +287,11 @@ impl ClientRequest<'_> {
ContractRequest::Get {
key,
return_contract_code,
subscribe,
} => ContractRequest::Get {
key,
return_contract_code,
subscribe,
},
ContractRequest::Subscribe { key, summary } => ContractRequest::Subscribe {
key,
Expand Down Expand Up @@ -364,6 +368,8 @@ pub enum ContractRequest<'a> {
/// Related contracts.
#[serde(borrow)]
related_contracts: RelatedContracts<'a>,
/// If this flag is set then subscribe to updates for this contract.
subscribe: bool,
},
/// Update an existing contract corresponding with the provided key.
Update {
Expand All @@ -377,6 +383,8 @@ pub enum ContractRequest<'a> {
key: ContractKey,
/// If this flag is set then fetch also the contract itself.
return_contract_code: bool,
/// If this flag is set then subscribe to updates for this contract.
subscribe: bool,
},
/// Subscribe to the changes in a given contract. Implicitly starts a get operation
/// if the contract is not present yet.
Expand All @@ -393,10 +401,12 @@ impl ContractRequest<'_> {
contract,
state,
related_contracts,
subscribe,
} => ContractRequest::Put {
contract,
state,
related_contracts: related_contracts.into_owned(),
subscribe,
},
Self::Update { key, data } => ContractRequest::Update {
key,
Expand All @@ -405,9 +415,11 @@ impl ContractRequest<'_> {
Self::Get {
key,
return_contract_code: fetch_contract,
subscribe,
} => ContractRequest::Get {
key,
return_contract_code: fetch_contract,
subscribe,
},
Self::Subscribe { key, summary } => ContractRequest::Subscribe {
key,
Expand All @@ -432,9 +444,11 @@ impl<'a> TryFromFbs<&FbsContractRequest<'a>> for ContractRequest<'a> {
let get = request.contract_request_as_get().unwrap();
let key = ContractKey::try_decode_fbs(&get.key())?;
let fetch_contract = get.fetch_contract();
let subscribe = get.subscribe();
ContractRequest::Get {
key,
return_contract_code: fetch_contract,
subscribe,
}
}
ContractRequestType::Put => {
Expand All @@ -443,10 +457,12 @@ impl<'a> TryFromFbs<&FbsContractRequest<'a>> for ContractRequest<'a> {
let state = WrappedState::new(put.wrapped_state().bytes().to_vec());
let related_contracts =
RelatedContracts::try_decode_fbs(&put.related_contracts())?.into_owned();
let subscribe = put.subscribe();
ContractRequest::Put {
contract,
state,
related_contracts,
subscribe,
}
}
ContractRequestType::Update => {
Expand Down Expand Up @@ -1342,7 +1358,7 @@ impl HostResponse {
}
}

impl std::fmt::Display for HostResponse {
impl Display for HostResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HostResponse::ContractResponse(res) => match res {
Expand Down Expand Up @@ -1439,13 +1455,15 @@ mod client_request_test {
contract,
state,
related_contracts: _,
subscribe,
} => {
assert_eq!(
contract.to_string(),
"WasmContainer([api=0.0.1](D8fdVLbRyMLw5mZtPRpWMFcrXGN2z8Nq8UGcLGPFBg2W))"
);
assert_eq!(contract.unwrap_v1().data.data(), &[1, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(state.to_vec(), &[1, 2, 3, 4, 5, 6, 7, 8]);
assert!(!subscribe);
}
_ => panic!("wrong contract request type"),
}
Expand Down Expand Up @@ -1473,9 +1491,11 @@ mod client_request_test {
ContractRequest::Get {
key,
return_contract_code: fetch_contract,
subscribe,
} => {
assert_eq!(key.encoded_contract_id(), EXPECTED_ENCODED_CONTRACT_ID);
assert!(!fetch_contract);
assert!(!subscribe);
}
_ => panic!("wrong contract request type"),
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/code_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const CONTRACT_KEY_SIZE: usize = 32;

#[serde_as]
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct CodeHash(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] pub(crate) [u8; CONTRACT_KEY_SIZE]);

impl CodeHash {
Expand Down
16 changes: 8 additions & 8 deletions rust/src/contract_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ pub trait ContractInterface {
/// A complete contract specification requires a `parameters` section
/// and a `contract` section.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct Contract<'a> {
#[serde(borrow)]
pub parameters: Parameters<'a>,
Expand Down Expand Up @@ -561,7 +561,7 @@ impl std::fmt::Display for Contract<'_> {
/// For efficiency and flexibility, contract state is represented as a simple [u8] byte array.
#[serde_as]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct State<'a>(
// TODO: conver this to Arc<[u8]> instead
#[serde_as(as = "serde_with::Bytes")]
Expand Down Expand Up @@ -639,7 +639,7 @@ impl std::io::Read for State<'_> {
/// Synchronization mechanism.
#[serde_as]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct StateDelta<'a>(
// TODO: conver this to Arc<[u8]> instead
#[serde_as(as = "serde_with::Bytes")]
Expand Down Expand Up @@ -705,7 +705,7 @@ impl DerefMut for StateDelta<'_> {
/// summary is determined by the state's contract.
#[serde_as]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct StateSummary<'a>(
// TODO: conver this to Arc<[u8]> instead
#[serde_as(as = "serde_with::Bytes")]
Expand Down Expand Up @@ -778,7 +778,7 @@ impl DerefMut for StateSummary<'_> {
/// and does not include any other metadata (like the parameters).
#[serde_as]
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct ContractCode<'a> {
// TODO: conver this to Arc<[u8]> instead
#[serde_as(as = "serde_with::Bytes")]
Expand Down Expand Up @@ -911,7 +911,7 @@ impl std::fmt::Debug for ContractCode<'_> {
/// The key representing the hash of the contract executable code hash and a set of `parameters`.
#[serde_as]
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
#[repr(transparent)]
pub struct ContractInstanceId(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] [u8; CONTRACT_KEY_SIZE]);

Expand Down Expand Up @@ -989,7 +989,7 @@ impl std::fmt::Debug for ContractInstanceId {
/// A complete key specification, that represents a cryptographic hash that identifies the contract.
#[serde_as]
#[derive(Debug, Eq, Copy, Clone, Serialize, Deserialize)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct ContractKey {
instance: ContractInstanceId,
code: Option<CodeHash>,
Expand Down Expand Up @@ -1154,7 +1154,7 @@ fn internal_fmt_key(
// TODO: get rid of this when State is internally an Arc<[u8]>
/// The state for a contract.
#[derive(PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
pub struct WrappedState(
#[serde(
serialize_with = "WrappedState::ser_state",
Expand Down
Loading