Skip to content

Commit aed4d4c

Browse files
sanitynetsirius
andauthored
Various changes required for river integration (#27)
* refactor: Improve error handling for unsupported API versions in contract loading * undo change to error handling, was breaking build and unnecessary for work on river * fix: Handle Result type in version comparison and parsing * chore: Fix version parsing error handling in contract code deserialization * APIVersion::from_u64 no-longer wraps result * format * resolved IDE issue * feat: Add optional arbitrary dependency for testing feature * add subscribe flag to contract requests and responses * update dependencies and version numbers * merge * lint * wip * improve operations testing --------- Co-authored-by: hsantos <netsirius@gmail.com>
1 parent aea0cc3 commit aed4d4c

File tree

93 files changed

+974
-1130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+974
-1130
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Freenet Standard Library
22

3-
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.
3+
Please see https://docs.freenet.org/ for more information. Bug reports, feature requests, questions
4+
should be created in the [freenet-core](https://github.com/freenet/freenet-core/issues/) repo.

Diff for: rust/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "freenet-stdlib"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55
rust-version = "1.71.1"
66
publish = true
@@ -57,7 +57,6 @@ version = "0.3"
5757
optional = true
5858

5959
[target.'cfg(any(unix, windows))'.dev-dependencies]
60-
arbitrary = { version = "1", features = ["derive"] }
6160
bincode = "1"
6261
wasmer = { version = "5.0.4", features = [ "sys-default"] }
6362
rand = { version = "0.8", features = ["small_rng"] }
@@ -68,5 +67,5 @@ contract = []
6867
unstable = []
6968
freenet-main-contract = []
7069
net = ["dep:tokio", "dep:tokio-tungstenite", "dep:wasm-bindgen", "dep:web-sys", "dep:js-sys", "dep:serde-wasm-bindgen"]
71-
testing = ["arbitrary"]
70+
testing = ["dep:arbitrary"]
7271
trace = []

Diff for: rust/examples/typed_contract.rs

-76
This file was deleted.

Diff for: rust/src/client_api.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ mod regular;
1717
#[cfg(all(any(unix, windows), feature = "net"))]
1818
pub use regular::*;
1919

20-
#[cfg(all(target_family = "wasm", feature = "net", not(feature = "contract")))]
20+
#[cfg(all(target_family = "wasm", feature = "net"))]
2121
mod browser;
22-
#[cfg(all(target_family = "wasm", feature = "net", not(feature = "contract")))]
22+
#[cfg(all(target_family = "wasm", feature = "net"))]
2323
pub use browser::*;
2424

2525
pub use client_events::*;

Diff for: rust/src/client_api/client_events.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,14 @@ impl ClientRequest<'_> {
270270
contract,
271271
state,
272272
related_contracts,
273+
subscribe,
273274
} => {
274275
let related_contracts = related_contracts.into_owned();
275276
ContractRequest::Put {
276277
contract,
277278
state,
278279
related_contracts,
280+
subscribe,
279281
}
280282
}
281283
ContractRequest::Update { key, data } => {
@@ -285,9 +287,11 @@ impl ClientRequest<'_> {
285287
ContractRequest::Get {
286288
key,
287289
return_contract_code,
290+
subscribe,
288291
} => ContractRequest::Get {
289292
key,
290293
return_contract_code,
294+
subscribe,
291295
},
292296
ContractRequest::Subscribe { key, summary } => ContractRequest::Subscribe {
293297
key,
@@ -364,6 +368,8 @@ pub enum ContractRequest<'a> {
364368
/// Related contracts.
365369
#[serde(borrow)]
366370
related_contracts: RelatedContracts<'a>,
371+
/// If this flag is set then subscribe to updates for this contract.
372+
subscribe: bool,
367373
},
368374
/// Update an existing contract corresponding with the provided key.
369375
Update {
@@ -377,6 +383,8 @@ pub enum ContractRequest<'a> {
377383
key: ContractKey,
378384
/// If this flag is set then fetch also the contract itself.
379385
return_contract_code: bool,
386+
/// If this flag is set then subscribe to updates for this contract.
387+
subscribe: bool,
380388
},
381389
/// Subscribe to the changes in a given contract. Implicitly starts a get operation
382390
/// if the contract is not present yet.
@@ -393,10 +401,12 @@ impl ContractRequest<'_> {
393401
contract,
394402
state,
395403
related_contracts,
404+
subscribe,
396405
} => ContractRequest::Put {
397406
contract,
398407
state,
399408
related_contracts: related_contracts.into_owned(),
409+
subscribe,
400410
},
401411
Self::Update { key, data } => ContractRequest::Update {
402412
key,
@@ -405,9 +415,11 @@ impl ContractRequest<'_> {
405415
Self::Get {
406416
key,
407417
return_contract_code: fetch_contract,
418+
subscribe,
408419
} => ContractRequest::Get {
409420
key,
410421
return_contract_code: fetch_contract,
422+
subscribe,
411423
},
412424
Self::Subscribe { key, summary } => ContractRequest::Subscribe {
413425
key,
@@ -432,9 +444,11 @@ impl<'a> TryFromFbs<&FbsContractRequest<'a>> for ContractRequest<'a> {
432444
let get = request.contract_request_as_get().unwrap();
433445
let key = ContractKey::try_decode_fbs(&get.key())?;
434446
let fetch_contract = get.fetch_contract();
447+
let subscribe = get.subscribe();
435448
ContractRequest::Get {
436449
key,
437450
return_contract_code: fetch_contract,
451+
subscribe,
438452
}
439453
}
440454
ContractRequestType::Put => {
@@ -443,10 +457,12 @@ impl<'a> TryFromFbs<&FbsContractRequest<'a>> for ContractRequest<'a> {
443457
let state = WrappedState::new(put.wrapped_state().bytes().to_vec());
444458
let related_contracts =
445459
RelatedContracts::try_decode_fbs(&put.related_contracts())?.into_owned();
460+
let subscribe = put.subscribe();
446461
ContractRequest::Put {
447462
contract,
448463
state,
449464
related_contracts,
465+
subscribe,
450466
}
451467
}
452468
ContractRequestType::Update => {
@@ -1342,7 +1358,7 @@ impl HostResponse {
13421358
}
13431359
}
13441360

1345-
impl std::fmt::Display for HostResponse {
1361+
impl Display for HostResponse {
13461362
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13471363
match self {
13481364
HostResponse::ContractResponse(res) => match res {
@@ -1439,13 +1455,15 @@ mod client_request_test {
14391455
contract,
14401456
state,
14411457
related_contracts: _,
1458+
subscribe,
14421459
} => {
14431460
assert_eq!(
14441461
contract.to_string(),
14451462
"WasmContainer([api=0.0.1](D8fdVLbRyMLw5mZtPRpWMFcrXGN2z8Nq8UGcLGPFBg2W))"
14461463
);
14471464
assert_eq!(contract.unwrap_v1().data.data(), &[1, 2, 3, 4, 5, 6, 7, 8]);
14481465
assert_eq!(state.to_vec(), &[1, 2, 3, 4, 5, 6, 7, 8]);
1466+
assert!(!subscribe);
14491467
}
14501468
_ => panic!("wrong contract request type"),
14511469
}
@@ -1473,9 +1491,11 @@ mod client_request_test {
14731491
ContractRequest::Get {
14741492
key,
14751493
return_contract_code: fetch_contract,
1494+
subscribe,
14761495
} => {
14771496
assert_eq!(key.encoded_contract_id(), EXPECTED_ENCODED_CONTRACT_ID);
14781497
assert!(!fetch_contract);
1498+
assert!(!subscribe);
14791499
}
14801500
_ => panic!("wrong contract request type"),
14811501
}

Diff for: rust/src/code_hash.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const CONTRACT_KEY_SIZE: usize = 32;
88

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

1414
impl CodeHash {

Diff for: rust/src/contract_interface.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ pub trait ContractInterface {
472472
/// A complete contract specification requires a `parameters` section
473473
/// and a `contract` section.
474474
#[derive(Debug, Serialize, Deserialize, Clone)]
475-
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
475+
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
476476
pub struct Contract<'a> {
477477
#[serde(borrow)]
478478
pub parameters: Parameters<'a>,
@@ -561,7 +561,7 @@ impl std::fmt::Display for Contract<'_> {
561561
/// For efficiency and flexibility, contract state is represented as a simple [u8] byte array.
562562
#[serde_as]
563563
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
564-
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
564+
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
565565
pub struct State<'a>(
566566
// TODO: conver this to Arc<[u8]> instead
567567
#[serde_as(as = "serde_with::Bytes")]
@@ -639,7 +639,7 @@ impl std::io::Read for State<'_> {
639639
/// Synchronization mechanism.
640640
#[serde_as]
641641
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
642-
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
642+
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
643643
pub struct StateDelta<'a>(
644644
// TODO: conver this to Arc<[u8]> instead
645645
#[serde_as(as = "serde_with::Bytes")]
@@ -705,7 +705,7 @@ impl DerefMut for StateDelta<'_> {
705705
/// summary is determined by the state's contract.
706706
#[serde_as]
707707
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
708-
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
708+
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
709709
pub struct StateSummary<'a>(
710710
// TODO: conver this to Arc<[u8]> instead
711711
#[serde_as(as = "serde_with::Bytes")]
@@ -778,7 +778,7 @@ impl DerefMut for StateSummary<'_> {
778778
/// and does not include any other metadata (like the parameters).
779779
#[serde_as]
780780
#[derive(Serialize, Deserialize, Clone)]
781-
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
781+
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
782782
pub struct ContractCode<'a> {
783783
// TODO: conver this to Arc<[u8]> instead
784784
#[serde_as(as = "serde_with::Bytes")]
@@ -911,7 +911,7 @@ impl std::fmt::Debug for ContractCode<'_> {
911911
/// The key representing the hash of the contract executable code hash and a set of `parameters`.
912912
#[serde_as]
913913
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
914-
#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
914+
#[cfg_attr(feature = "testing", derive(arbitrary::Arbitrary))]
915915
#[repr(transparent)]
916916
pub struct ContractInstanceId(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] [u8; CONTRACT_KEY_SIZE]);
917917

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

0 commit comments

Comments
 (0)