From e407b2113467cbe8b2cadd4631c9a32df6782858 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 28 Sep 2023 19:21:56 +0200 Subject: [PATCH 01/15] sdk feature --- Makefile | 13 ++++++++++++- types/Cargo.toml | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 5b43a3a406..1738774dd5 100644 --- a/Makefile +++ b/Makefile @@ -107,6 +107,11 @@ check-std-features: cd smart_contracts/contract && $(CARGO) check --all-targets --no-default-features --features=std cd smart_contracts/contract && $(CARGO) check --all-targets --features=std +.PHONY: check-sdk-features +check-sdk-features: + $(CARGO) check --all-targets --no-default-features --features=sdk + $(CARGO) check --all-targets --features=sdk + .PHONY: check-format check-format: $(CARGO_PINNED_NIGHTLY) fmt --all -- --check @@ -119,12 +124,17 @@ lint-contracts-rs: cd smart_contracts/contracts && $(CARGO) clippy $(patsubst %, -p %, $(ALL_CONTRACTS)) -- -D warnings -A renamed_and_removed_lints .PHONY: lint -lint: lint-contracts-rs lint-default-features lint-all-features lint-smart-contracts +lint: lint-contracts-rs lint-default-features lint-all-features lint-smart-contracts lint-sdk-features .PHONY: lint-default-features lint-default-features: $(CARGO) clippy --all-targets -- -D warnings -A renamed_and_removed_lints +.PHONY: lint-sdk-features +lint-sdk-features: + $(CARGO) clippy --all-targets --features=sdk -- -D warnings + + .PHONY: lint-all-features lint-all-features: $(CARGO) clippy --all-targets --all-features -- -D warnings -A renamed_and_removed_lints @@ -157,6 +167,7 @@ check-rs: \ lint \ audit \ check-std-features \ + check-sdk-features \ test-rs \ test-rs-no-default-features \ test-contracts-rs diff --git a/types/Cargo.toml b/types/Cargo.toml index f469d68ee1..5e4da0af7c 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "casper-types" -version = "3.0.0" # when updating, also update 'html_root_url' in lib.rs +# when updating, also update 'html_root_url' in lib.rs +version = "3.0.0" authors = ["Fraser Hutchison "] edition = "2018" description = "Types shared by many casper crates for use on the Casper network." @@ -17,12 +18,18 @@ bitflags = "1" blake2 = { version = "0.9.0", default-features = false } datasize = { version = "0.2.4", optional = true } derp = { version = "0.0.14", optional = true } -ed25519-dalek = { version = "2.0.0", default-features = false, features = ["alloc", "zeroize"] } +ed25519-dalek = { version = "2.0.0", default-features = false, features = [ + "alloc", + "zeroize", +] } getrandom = { version = "0.2.0", features = ["rdrand"], optional = true } hex = { version = "0.4.2", default-features = false, features = ["alloc"] } hex_fmt = "0.3.0" humantime = { version = "2", optional = true } -k256 = { version = "0.13.1", default-features = false, features = ["ecdsa", "sha256"] } +k256 = { version = "0.13.1", default-features = false, features = [ + "ecdsa", + "sha256", +] } num = { version = "0.4.0", default-features = false, features = ["alloc"] } num-derive = { version = "0.3.0", default-features = false } num-integer = { version = "0.1.42", default-features = false } @@ -34,12 +41,21 @@ proptest = { version = "1.0.0", optional = true } proptest-derive = { version = "0.3.0", optional = true } rand = { version = "0.8.3", default-features = false, features = ["small_rng"] } rand_pcg = { version = "0.3.0", optional = true } -schemars = { version = "=0.8.5", features = ["preserve_order"], optional = true } -serde = { version = "1", default-features = false, features = ["alloc", "derive"] } -serde_bytes = { version = "0.11.5", default-features = false, features = ["alloc"] } -serde_json = { version = "1.0.59", default-features = false, features = ["alloc"] } +schemars = { version = "=0.8.5", features = [ + "preserve_order", +], optional = true } +serde = { version = "1", default-features = false, features = [ + "alloc", + "derive", +] } +serde_bytes = { version = "0.11.5", default-features = false, features = [ + "alloc", +] } +serde_json = { version = "1.0.59", default-features = false, features = [ + "alloc", +] } strum = { version = "0.24", features = ["derive"], optional = true } -thiserror = {version = "1", optional = true } +thiserror = { version = "1", optional = true } uint = { version = "0.9.0", default-features = false } untrusted = { version = "0.7.1", optional = true } version-sync = { version = "0.9", optional = true } @@ -67,10 +83,20 @@ untrusted = "0.7.1" [features] json-schema = ["once_cell", "schemars"] -std = ["derp", "getrandom/std", "humantime", "once_cell", "pem", "serde_json/preserve_order", "thiserror", "untrusted"] +std = [ + "derp", + "getrandom/std", + "humantime", + "once_cell", + "pem", + "serde_json/preserve_order", + "thiserror", + "untrusted", +] testing = ["proptest", "proptest-derive", "rand_pcg", "strum"] # DEPRECATED - use "testing" instead of "gens". gens = ["testing"] +sdk = [] [[bench]] name = "bytesrepr_bench" From a8594d466b9e686c03ac2ef00838dcdce4acdd3e Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 28 Sep 2023 19:47:03 +0200 Subject: [PATCH 02/15] getrandom/js --- types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/Cargo.toml b/types/Cargo.toml index 5e4da0af7c..3e2cf4b369 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -96,7 +96,7 @@ std = [ testing = ["proptest", "proptest-derive", "rand_pcg", "strum"] # DEPRECATED - use "testing" instead of "gens". gens = ["testing"] -sdk = [] +sdk = ["getrandom/js"] [[bench]] name = "bytesrepr_bench" From 852c0c76414b624b1548d5c5e18059fff6554587 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 28 Sep 2023 21:32:21 +0200 Subject: [PATCH 03/15] Compile wasm --- types/CHANGELOG.md | 3 + types/src/cl_value.rs | 4 ++ types/src/crypto/asymmetric_key.rs | 56 +++++++++++++---- types/src/crypto/asymmetric_key/tests.rs | 6 ++ types/src/file_utils.rs | 76 ++++++++++++++++-------- types/src/lib.rs | 5 ++ 6 files changed, 115 insertions(+), 35 deletions(-) diff --git a/types/CHANGELOG.md b/types/CHANGELOG.md index 61f7b33e9a..5aeb05c59b 100644 --- a/types/CHANGELOG.md +++ b/types/CHANGELOG.md @@ -17,6 +17,9 @@ All notable changes to this project will be documented in this file. The format * Update `ed25519-dalek` to version 2.0.0 as mitigation for [RUSTSEC-2022-0093](https://rustsec.org/advisories/RUSTSEC-2022-0093) +### Changed + +- Add "sdk" feature config checks to compile to wasm ## 3.0.0 diff --git a/types/src/cl_value.rs b/types/src/cl_value.rs index 1dc1bee5b7..bdc61dd826 100644 --- a/types/src/cl_value.rs +++ b/types/src/cl_value.rs @@ -1,3 +1,4 @@ +//! CLvalue type // TODO - remove once schemars stops causing warning. #![allow(clippy::field_reassign_with_default)] @@ -18,6 +19,9 @@ use crate::{ mod jsonrepr; +#[cfg(feature = "sdk")] +pub use jsonrepr::cl_value_to_json; + /// Error while converting a [`CLValue`] into a given type. #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "datasize", derive(DataSize))] diff --git a/types/src/crypto/asymmetric_key.rs b/types/src/crypto/asymmetric_key.rs index c340ffad33..b1bcddac61 100644 --- a/types/src/crypto/asymmetric_key.rs +++ b/types/src/crypto/asymmetric_key.rs @@ -53,11 +53,13 @@ use crate::{ crypto::Error, CLType, CLTyped, Tagged, }; + #[cfg(any(feature = "std", test))] -use crate::{ - crypto::ErrorExt, - file_utils::{read_file, write_file, write_private_file}, -}; +use crate::crypto::ErrorExt; + +#[cfg(any(feature = "std", test))] +#[cfg(not(any(feature = "sdk")))] +use crate::file_utils::{read_file, write_file, write_private_file}; #[cfg(any(feature = "testing", test))] pub mod gens; @@ -245,14 +247,31 @@ impl SecretKey { } /// Attempts to write the key bytes to the configured file path. + #[allow(unused_variables)] pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { - write_private_file(file, self.to_pem()?).map_err(ErrorExt::SecretKeySave) + #[cfg(not(any(feature = "sdk")))] + { + write_private_file(file, self.to_pem()?).map_err(ErrorExt::SecretKeySave) + } + #[cfg(feature = "sdk")] + { + Ok(()) + } } /// Attempts to read the key bytes from configured file path. + #[allow(unused_variables)] pub fn from_file>(file: P) -> Result { - let data = read_file(file).map_err(ErrorExt::SecretKeyLoad)?; - Self::from_pem(data) + #[cfg(not(any(feature = "sdk")))] + { + let data = read_file(file).map_err(ErrorExt::SecretKeyLoad)?; + Self::from_pem(data) + } + #[cfg(feature = "sdk")] + { + let data = vec![]; + Self::from_pem(data) + } } /// DER encodes a key. @@ -526,14 +545,31 @@ impl PublicKey { } /// Attempts to write the key bytes to the configured file path. + #[allow(unused_variables)] pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { - write_file(file, self.to_pem()?).map_err(ErrorExt::PublicKeySave) + #[cfg(not(any(feature = "sdk")))] + { + write_file(file, self.to_pem()?).map_err(ErrorExt::PublicKeySave) + } + #[cfg(feature = "sdk")] + { + Ok(()) + } } /// Attempts to read the key bytes from configured file path. + #[allow(unused_variables)] pub fn from_file>(file: P) -> Result { - let data = read_file(file).map_err(ErrorExt::PublicKeyLoad)?; - Self::from_pem(data) + #[cfg(not(any(feature = "sdk")))] + { + let data = read_file(file).map_err(ErrorExt::PublicKeyLoad)?; + Self::from_pem(data) + } + #[cfg(feature = "sdk")] + { + let data = vec![]; + Self::from_pem(data) + } } /// DER encodes a key. diff --git a/types/src/crypto/asymmetric_key/tests.rs b/types/src/crypto/asymmetric_key/tests.rs index 545b8dad00..41eb89af3c 100644 --- a/types/src/crypto/asymmetric_key/tests.rs +++ b/types/src/crypto/asymmetric_key/tests.rs @@ -82,6 +82,7 @@ fn known_secret_key_to_pem(expected_key: &SecretKey, known_key_pem: &str, expect assert_eq!(expected_tag, decoded.tag()); } +#[cfg(not(any(feature = "sdk")))] fn secret_key_file_roundtrip(secret_key: SecretKey) { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("test_secret_key.pem"); @@ -140,6 +141,7 @@ fn known_public_key_to_pem(known_key_hex: &str, known_key_pem: &str) { assert_eq!(key_bytes, Into::>::into(decoded)); } +#[cfg(not(any(feature = "sdk")))] fn public_key_file_roundtrip(public_key: PublicKey) { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("test_public_key.pem"); @@ -341,6 +343,7 @@ MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC super::known_secret_key_to_pem(&expected_key, KNOWN_KEY_PEM, ED25519_TAG); } + #[cfg(not(any(feature = "sdk")))] #[test] fn secret_key_to_and_from_file() { let mut rng = TestRng::new(); @@ -397,6 +400,7 @@ MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE= super::known_public_key_to_pem(KNOWN_KEY_HEX, KNOWN_KEY_PEM); } + #[cfg(not(any(feature = "sdk")))] #[test] fn public_key_to_and_from_file() { let mut rng = TestRng::new(); @@ -583,6 +587,7 @@ Yj9oTB9fx9+vvQdxJOhMtu46kGo0Uw== super::known_secret_key_to_pem(&expected_key, KNOWN_KEY_PEM, SECP256K1_TAG); } + #[cfg(not(any(feature = "sdk")))] #[test] fn secret_key_to_and_from_file() { let mut rng = TestRng::new(); @@ -640,6 +645,7 @@ kv+kBR5u4ISEAkuc2TFWQHX0Yj9oTB9fx9+vvQdxJOhMtu46kGo0Uw== super::known_public_key_to_pem(KNOWN_KEY_HEX, KNOWN_KEY_PEM); } + #[cfg(not(any(feature = "sdk")))] #[test] fn public_key_to_and_from_file() { let mut rng = TestRng::new(); diff --git a/types/src/file_utils.rs b/types/src/file_utils.rs index 775a7315fa..c85b67e402 100644 --- a/types/src/file_utils.rs +++ b/types/src/file_utils.rs @@ -1,10 +1,12 @@ //! Utilities for handling reading from and writing to files. +#[cfg(feature = "sdk")] +use std::path::Path; +#[cfg(not(any(feature = "sdk")))] +use std::{fs, io::Write, os::unix::fs::OpenOptionsExt, path::Path}; use std::{ - fs, - io::{self, Write}, - os::unix::fs::OpenOptionsExt, - path::{Path, PathBuf}, + io::{self}, + path::PathBuf, }; use thiserror::Error; @@ -34,44 +36,68 @@ pub struct WriteFileError { /// Read complete at `path` into memory. /// /// Wraps `fs::read`, but preserves the filename for better error printing. +#[allow(unused_variables)] pub fn read_file>(filename: P) -> Result, ReadFileError> { - let path = filename.as_ref(); - fs::read(path).map_err(|error| ReadFileError { - path: path.to_owned(), - error, - }) + #[cfg(feature = "sdk")] + { + Ok(vec![]) + } + #[cfg(not(any(feature = "sdk")))] + { + let path = filename.as_ref(); + fs::read(path).map_err(|error| ReadFileError { + path: path.to_owned(), + error, + }) + } } /// Write data to `path`. /// /// Wraps `fs::write`, but preserves the filename for better error printing. +#[allow(unused_variables, unused)] pub(crate) fn write_file, B: AsRef<[u8]>>( filename: P, data: B, ) -> Result<(), WriteFileError> { - let path = filename.as_ref(); - fs::write(path, data.as_ref()).map_err(|error| WriteFileError { - path: path.to_owned(), - error, - }) + #[cfg(feature = "sdk")] + { + Ok(()) + } + #[cfg(not(any(feature = "sdk")))] + { + let path = filename.as_ref(); + fs::write(path, data.as_ref()).map_err(|error| WriteFileError { + path: path.to_owned(), + error, + }) + } } /// Writes data to `path`, ensuring only the owner can read or write it. /// /// Otherwise functions like [`write_file`]. +#[allow(unused_variables, unused)] pub(crate) fn write_private_file, B: AsRef<[u8]>>( filename: P, data: B, ) -> Result<(), WriteFileError> { - let path = filename.as_ref(); - fs::OpenOptions::new() - .write(true) - .create(true) - .mode(0o600) - .open(path) - .and_then(|mut file| file.write_all(data.as_ref())) - .map_err(|error| WriteFileError { - path: path.to_owned(), - error, - }) + #[cfg(feature = "sdk")] + { + Ok(()) + } + #[cfg(not(any(feature = "sdk")))] + { + let path = filename.as_ref(); + fs::OpenOptions::new() + .write(true) + .create(true) + .mode(0o600) + .open(path) + .and_then(|mut file| file.write_all(data.as_ref())) + .map_err(|error| WriteFileError { + path: path.to_owned(), + error, + }) + } } diff --git a/types/src/lib.rs b/types/src/lib.rs index b763c41ea3..798c44dd21 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -28,7 +28,10 @@ mod block_time; pub mod bytesrepr; pub mod checksummed_hex; mod cl_type; +#[cfg(not(any(feature = "sdk")))] mod cl_value; +#[cfg(feature = "sdk")] +pub mod cl_value; mod contract_wasm; pub mod contracts; pub mod crypto; @@ -66,6 +69,8 @@ pub use access_rights::{ pub use api_error::ApiError; pub use block_time::{BlockTime, BLOCKTIME_SERIALIZED_LENGTH}; pub use cl_type::{named_key_type, CLType, CLTyped}; +#[cfg(feature = "sdk")] +pub use cl_value::cl_value_to_json; pub use cl_value::{CLTypeMismatch, CLValue, CLValueError}; pub use contract_wasm::{ContractWasm, ContractWasmHash}; #[doc(inline)] From ece7a9e509935dfd4e922cc400003ebfe628a95a Mon Sep 17 00:00:00 2001 From: gRoussac Date: Fri, 29 Sep 2023 15:12:24 +0200 Subject: [PATCH 04/15] Add AsRef on DeployHash for sdk --- types/src/transfer.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/types/src/transfer.rs b/types/src/transfer.rs index 23f51df809..c751736dcb 100644 --- a/types/src/transfer.rs +++ b/types/src/transfer.rs @@ -53,6 +53,12 @@ impl DeployHash { } } +impl AsRef<[u8]> for DeployHash { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + #[cfg(feature = "json-schema")] impl JsonSchema for DeployHash { fn schema_name() -> String { From fe57d2557d9ff22d873ae953318add275def45ce Mon Sep 17 00:00:00 2001 From: gRoussac Date: Wed, 18 Oct 2023 18:37:59 +0200 Subject: [PATCH 05/15] Add some comments for sdk feature Noop --- types/src/crypto/asymmetric_key.rs | 4 ++++ types/src/file_utils.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/types/src/crypto/asymmetric_key.rs b/types/src/crypto/asymmetric_key.rs index b1bcddac61..8d61f7f661 100644 --- a/types/src/crypto/asymmetric_key.rs +++ b/types/src/crypto/asymmetric_key.rs @@ -247,6 +247,7 @@ impl SecretKey { } /// Attempts to write the key bytes to the configured file path. + /// No operation the sdk feature #[allow(unused_variables)] pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { #[cfg(not(any(feature = "sdk")))] @@ -260,6 +261,7 @@ impl SecretKey { } /// Attempts to read the key bytes from configured file path. + /// No operation the sdk feature, use SecretKey::from_pem #[allow(unused_variables)] pub fn from_file>(file: P) -> Result { #[cfg(not(any(feature = "sdk")))] @@ -545,6 +547,7 @@ impl PublicKey { } /// Attempts to write the key bytes to the configured file path. + /// No operation the sdk feature #[allow(unused_variables)] pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { #[cfg(not(any(feature = "sdk")))] @@ -558,6 +561,7 @@ impl PublicKey { } /// Attempts to read the key bytes from configured file path. + /// No operation the sdk feature, use SecretKey::from_pem #[allow(unused_variables)] pub fn from_file>(file: P) -> Result { #[cfg(not(any(feature = "sdk")))] diff --git a/types/src/file_utils.rs b/types/src/file_utils.rs index c85b67e402..ce72d1afae 100644 --- a/types/src/file_utils.rs +++ b/types/src/file_utils.rs @@ -36,6 +36,7 @@ pub struct WriteFileError { /// Read complete at `path` into memory. /// /// Wraps `fs::read`, but preserves the filename for better error printing. +/// No operation the sdk feature #[allow(unused_variables)] pub fn read_file>(filename: P) -> Result, ReadFileError> { #[cfg(feature = "sdk")] @@ -55,6 +56,7 @@ pub fn read_file>(filename: P) -> Result, ReadFileError> /// Write data to `path`. /// /// Wraps `fs::write`, but preserves the filename for better error printing. +/// No operation the sdk feature #[allow(unused_variables, unused)] pub(crate) fn write_file, B: AsRef<[u8]>>( filename: P, @@ -77,6 +79,7 @@ pub(crate) fn write_file, B: AsRef<[u8]>>( /// Writes data to `path`, ensuring only the owner can read or write it. /// /// Otherwise functions like [`write_file`]. +/// No operation the sdk feature #[allow(unused_variables, unused)] pub(crate) fn write_private_file, B: AsRef<[u8]>>( filename: P, From d7a879ee49c56ca6e622afe54cbda731bb2628a8 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 19 Oct 2023 11:38:55 +0200 Subject: [PATCH 06/15] Duplicate import --- types/src/file_utils.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/src/file_utils.rs b/types/src/file_utils.rs index ce72d1afae..886ffd24d5 100644 --- a/types/src/file_utils.rs +++ b/types/src/file_utils.rs @@ -1,9 +1,8 @@ //! Utilities for handling reading from and writing to files. -#[cfg(feature = "sdk")] use std::path::Path; #[cfg(not(any(feature = "sdk")))] -use std::{fs, io::Write, os::unix::fs::OpenOptionsExt, path::Path}; +use std::{fs, io::Write, os::unix::fs::OpenOptionsExt}; use std::{ io::{self}, path::PathBuf, From 9221be681d391a3ae860366fb1507ba88100cd99 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 26 Oct 2023 00:00:07 +0200 Subject: [PATCH 07/15] Bad merge --- types/src/file_utils.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/src/file_utils.rs b/types/src/file_utils.rs index 886ffd24d5..27fb36bb1c 100644 --- a/types/src/file_utils.rs +++ b/types/src/file_utils.rs @@ -1,11 +1,10 @@ //! Utilities for handling reading from and writing to files. -use std::path::Path; #[cfg(not(any(feature = "sdk")))] use std::{fs, io::Write, os::unix::fs::OpenOptionsExt}; use std::{ io::{self}, - path::PathBuf, + path::{Path, PathBuf}, }; use thiserror::Error; From b2dc763ed9fe23c590d9fa8336baf5202c252b68 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 26 Oct 2023 00:19:17 +0200 Subject: [PATCH 08/15] strange lint issue --- execution_engine/src/storage/trie_store/operations/mod.rs | 5 +++-- node/src/components/network/outgoing.rs | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/execution_engine/src/storage/trie_store/operations/mod.rs b/execution_engine/src/storage/trie_store/operations/mod.rs index bbc9c83c90..726143dc99 100644 --- a/execution_engine/src/storage/trie_store/operations/mod.rs +++ b/execution_engine/src/storage/trie_store/operations/mod.rs @@ -593,8 +593,9 @@ where let trie_key = new_extension.trie_hash()?; new_elements.push((trie_key, new_extension)) } - // The single sibling is an extension. We output an extension to replace - // the parent, prepending the sibling index to the sibling's affix. In + // The single sibling is an extension. We output an extension to + // replace the parent, prepending the + // sibling index to the sibling's affix. In // the next loop iteration, we will handle the case where this extension // might need to be combined with a grandparent extension. Trie::Extension { diff --git a/node/src/components/network/outgoing.rs b/node/src/components/network/outgoing.rs index ce5d87944c..871a968a48 100644 --- a/node/src/components/network/outgoing.rs +++ b/node/src/components/network/outgoing.rs @@ -27,8 +27,8 @@ //! * The `Dialer` is expected to produce `DialOutcomes` for every dial [`DialRequest::Dial`] //! eventually. These must be forwarded to the `OutgoingManager` via the `handle_dial_outcome` //! function. -//! * The `perform_housekeeping` method must be called periodically to give the -//! `OutgoingManager` a chance to initiate reconnections and collect garbage. +//! * The `perform_housekeeping` method must be called periodically to give the `OutgoingManager` a +//! chance to initiate reconnections and collect garbage. //! * When a connection is dropped, the connection manager must be notified via //! `handle_connection_drop`. //! From ba737a814ec0a17ad8549e6d52b3e3c45d37db0f Mon Sep 17 00:00:00 2001 From: gRoussac Date: Thu, 26 Oct 2023 17:53:36 +0200 Subject: [PATCH 09/15] Revert --- Cargo.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 09875157c2..7c0303c9de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2187,8 +2187,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if 1.0.0", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] From 23ce565d3eb4cd366532f78ed44b5b3beb5a8eed Mon Sep 17 00:00:00 2001 From: gRoussac Date: Fri, 17 Nov 2023 00:32:48 +0100 Subject: [PATCH 10/15] Additive feature --- Makefile | 18 +-- hashing/Cargo.toml | 5 +- types/CHANGELOG.md | 140 ++++++++++------------- types/Cargo.toml | 5 +- types/src/cl_value.rs | 3 +- types/src/crypto/asymmetric_key.rs | 80 ++++++------- types/src/crypto/asymmetric_key/tests.rs | 6 - types/src/crypto/error.rs | 6 +- types/src/file_utils.rs | 86 +++++--------- types/src/lib.rs | 8 +- 10 files changed, 154 insertions(+), 203 deletions(-) diff --git a/Makefile b/Makefile index d70d9d44da..f141d387ed 100644 --- a/Makefile +++ b/Makefile @@ -101,6 +101,10 @@ test-contracts-as: build-contracts-rs build-contracts-as .PHONY: test-contracts test-contracts: test-contracts-rs +.PHONY: check-no-default-features +check-no-default-features: + cd types && $(CARGO) check --all-targets --no-default-features + .PHONY: check-std-features check-std-features: cd types && $(CARGO) check --all-targets --no-default-features --features=std @@ -108,11 +112,6 @@ check-std-features: cd smart_contracts/contract && $(CARGO) check --all-targets --no-default-features --features=std cd smart_contracts/contract && $(CARGO) check --all-targets --features=std -.PHONY: check-sdk-features -check-sdk-features: - $(CARGO) check --all-targets --no-default-features --features=sdk - $(CARGO) check --all-targets --features=sdk - .PHONY: check-format check-format: $(CARGO_PINNED_NIGHTLY) fmt --all -- --check @@ -125,17 +124,12 @@ lint-contracts-rs: cd smart_contracts/contracts && $(CARGO) clippy $(patsubst %, -p %, $(ALL_CONTRACTS)) -- -D warnings -A renamed_and_removed_lints .PHONY: lint -lint: lint-contracts-rs lint-default-features lint-all-features lint-smart-contracts lint-sdk-features +lint: lint-contracts-rs lint-default-features lint-all-features lint-smart-contracts .PHONY: lint-default-features lint-default-features: $(CARGO) clippy --all-targets -- -D warnings -A renamed_and_removed_lints -.PHONY: lint-sdk-features -lint-sdk-features: - $(CARGO) clippy --all-targets --features=sdk -- -D warnings - - .PHONY: lint-all-features lint-all-features: $(CARGO) clippy --all-targets --all-features -- -D warnings -A renamed_and_removed_lints @@ -168,7 +162,7 @@ check-rs: \ lint \ audit \ check-std-features \ - check-sdk-features \ + check-no-default-features \ test-rs \ test-rs-no-default-features \ test-contracts-rs diff --git a/hashing/Cargo.toml b/hashing/Cargo.toml index 9018216ca9..e92248926e 100644 --- a/hashing/Cargo.toml +++ b/hashing/Cargo.toml @@ -12,7 +12,10 @@ license = "Apache-2.0" [dependencies] blake2 = "0.9.0" base16 = "0.2.1" -casper-types = { version = "3.0.0", path = "../types", features = ["datasize", "std"] } +casper-types = { version = "3.0.0", path = "../types", default-features = false, features = [ + "datasize", + "std", +] } datasize = "0.2.9" hex = { version = "0.4.2", default-features = false, features = ["serde"] } hex-buffer-serde = "0.3.0" diff --git a/types/CHANGELOG.md b/types/CHANGELOG.md index 790bce5937..df1ff646a3 100644 --- a/types/CHANGELOG.md +++ b/types/CHANGELOG.md @@ -1,190 +1,174 @@ # Changelog -All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog]. +All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog]. -[comment]: <> (Added: new features) -[comment]: <> (Changed: changes in existing functionality) +[comment]: <> (Added: new features) +[comment]: <> (Changed: changes in existing functionality) [comment]: <> (Deprecated: soon-to-be removed features) -[comment]: <> (Removed: now removed features) -[comment]: <> (Fixed: any bug fixes) -[comment]: <> (Security: in case of vulnerabilities) - - +[comment]: <> (Removed: now removed features) +[comment]: <> (Fixed: any bug fixes) +[comment]: <> (Security: in case of vulnerabilities) ## Unreleased ### Added -* Add a new `SyncHandling` enum, which allows a node to opt out of historical sync. -### Changed -* Update `k256` to version 0.13.1. +- Add a new `SyncHandling` enum, which allows a node to opt out of historical sync. +- Add default "std-fs-io" feature to compile to wasm -### Security -* Update `ed25519-dalek` to version 2.0.0 as mitigation for [RUSTSEC-2022-0093](https://rustsec.org/advisories/RUSTSEC-2022-0093) +### Changed +- Update `k256` to version 0.13.1. -### Changed +### Security -- Add "sdk" feature config checks to compile to wasm +- Update `ed25519-dalek` to version 2.0.0 as mitigation for [RUSTSEC-2022-0093](https://rustsec.org/advisories/RUSTSEC-2022-0093) ## 3.0.0 ### Added -* Add new `bytesrepr::Error::NotRepresentable` error variant that represents values that are not representable by the serialization format. -* Add new `Key::Unbond` key variant under which the new unbonding information (to support redelegation) is written. -* Add new `Key::ChainspecRegistry` key variant under which the `ChainspecRegistry` is written. -* Add new `Key::ChecksumRegistry` key variant under which a registry of checksums for a given block is written. There are two checksums in the registry, one for the execution results and the other for the approvals of all deploys in the block. -* Add new `StoredValue::Unbonding` variant to support redelegating. -* Add a new type `WithdrawPurses` which is meant to represent `UnbondingPurses` as they exist in current live networks. + +- Add new `bytesrepr::Error::NotRepresentable` error variant that represents values that are not representable by the serialization format. +- Add new `Key::Unbond` key variant under which the new unbonding information (to support redelegation) is written. +- Add new `Key::ChainspecRegistry` key variant under which the `ChainspecRegistry` is written. +- Add new `Key::ChecksumRegistry` key variant under which a registry of checksums for a given block is written. There are two checksums in the registry, one for the execution results and the other for the approvals of all deploys in the block. +- Add new `StoredValue::Unbonding` variant to support redelegating. +- Add a new type `WithdrawPurses` which is meant to represent `UnbondingPurses` as they exist in current live networks. ### Changed -* Extend `UnbondingPurse` to take a new field `new_validator` which represents the validator to whom tokens will be re-delegated. -* Increase `DICTIONARY_ITEM_KEY_MAX_LENGTH` to 128. -* Change prefix of formatted string representation of `ContractPackageHash` from "contract-package-wasm" to "contract-package-". Parsing from the old format is still supported. -* Apply `#[non_exhaustive]` to error enums. -* Change Debug output of `DeployHash` to hex-encoded string rather than a list of integers. -### Fixed -* Fix some integer casts, where failure is now detected and reported via new error variant `NotRepresentable`. +- Extend `UnbondingPurse` to take a new field `new_validator` which represents the validator to whom tokens will be re-delegated. +- Increase `DICTIONARY_ITEM_KEY_MAX_LENGTH` to 128. +- Change prefix of formatted string representation of `ContractPackageHash` from "contract-package-wasm" to "contract-package-". Parsing from the old format is still supported. +- Apply `#[non_exhaustive]` to error enums. +- Change Debug output of `DeployHash` to hex-encoded string rather than a list of integers. +### Fixed +- Fix some integer casts, where failure is now detected and reported via new error variant `NotRepresentable`. ## 2.0.0 ### Fixed -* Republish v1.6.0 as v2.0.0 due to missed breaking change in API (addition of new variant to `Key`). - +- Republish v1.6.0 as v2.0.0 due to missed breaking change in API (addition of new variant to `Key`). ## 1.6.0 [YANKED] ### Added -* Extend asymmetric key functionality, available via feature `std` (moved from `casper-nodes` crate). -* Provide `Timestamp` and `TimeDiff` types for time operations, with extended functionality available via feature `std` (moved from `casper-nodes` crate). -* Provide test-only functionality, in particular a seedable RNG `TestRng` which outputs its seed on test failure. Available via a new feature `testing`. -* Add new `Key::EraSummary` key variant under which the era summary info is written on each switch block execution. -### Deprecated -* Deprecate `gens` feature: its functionality is included in the new `testing` feature. +- Extend asymmetric key functionality, available via feature `std` (moved from `casper-nodes` crate). +- Provide `Timestamp` and `TimeDiff` types for time operations, with extended functionality available via feature `std` (moved from `casper-nodes` crate). +- Provide test-only functionality, in particular a seedable RNG `TestRng` which outputs its seed on test failure. Available via a new feature `testing`. +- Add new `Key::EraSummary` key variant under which the era summary info is written on each switch block execution. +### Deprecated +- Deprecate `gens` feature: its functionality is included in the new `testing` feature. ## 1.5.0 ### Added -* Provide types and functionality to support improved access control inside execution engine. -* Provide `CLTyped` impl for `ContractPackage` to allow it to be passed into contracts. -### Fixed -* Limit parsing of CLTyped objects to a maximum of 50 types deep. +- Provide types and functionality to support improved access control inside execution engine. +- Provide `CLTyped` impl for `ContractPackage` to allow it to be passed into contracts. +### Fixed +- Limit parsing of CLTyped objects to a maximum of 50 types deep. ## 1.4.6 - 2021-12-29 ### Changed -* Disable checksummed-hex encoding, but leave checksummed-hex decoding in place. - +- Disable checksummed-hex encoding, but leave checksummed-hex decoding in place. ## 1.4.5 - 2021-12-06 ### Added -* Add function to `auction::MintProvider` trait to support minting into an existing purse. -### Changed -* Change checksummed hex implementation to use 32 byte rather than 64 byte blake2b digests. +- Add function to `auction::MintProvider` trait to support minting into an existing purse. +### Changed +- Change checksummed hex implementation to use 32 byte rather than 64 byte blake2b digests. ## [1.4.4] - 2021-11-18 ### Fixed -* Revert the accidental change to the `std` feature causing a broken build when this feature is enabled. - +- Revert the accidental change to the `std` feature causing a broken build when this feature is enabled. ## [1.4.3] - 2021-11-17 [YANKED] - - ## [1.4.2] - 2021-11-13 [YANKED] ### Added -* Add checksummed hex encoding following a scheme similar to [EIP-55](https://eips.ethereum.org/EIPS/eip-55). - +- Add checksummed hex encoding following a scheme similar to [EIP-55](https://eips.ethereum.org/EIPS/eip-55). ## [1.4.1] - 2021-10-23 No changes. - - ## [1.4.0] - 2021-10-21 [YANKED] ### Added -* Add `json-schema` feature, disabled by default, to enable many types to be used to produce JSON-schema data. -* Add implicit `datasize` feature, disabled by default, to enable many types to derive the `DataSize` trait. -* Add `StoredValue` types to this crate. + +- Add `json-schema` feature, disabled by default, to enable many types to be used to produce JSON-schema data. +- Add implicit `datasize` feature, disabled by default, to enable many types to derive the `DataSize` trait. +- Add `StoredValue` types to this crate. ### Changed -* Support building and testing using stable Rust. -* Allow longer hex string to be presented in `json` files. Current maximum is increased from 100 to 150 characters. -* Improve documentation and `Debug` impls for `ApiError`. -### Deprecated -* Feature `std` is deprecated as it is now a no-op, since there is no benefit to linking the std lib via this crate. +- Support building and testing using stable Rust. +- Allow longer hex string to be presented in `json` files. Current maximum is increased from 100 to 150 characters. +- Improve documentation and `Debug` impls for `ApiError`. +### Deprecated +- Feature `std` is deprecated as it is now a no-op, since there is no benefit to linking the std lib via this crate. ## [1.3.0] - 2021-07-19 ### Changed -* Restrict summarization when JSON pretty-printing to contiguous long hex strings. -* Update pinned version of Rust to `nightly-2021-06-17`. -### Removed -* Remove ability to clone `SecretKey`s. +- Restrict summarization when JSON pretty-printing to contiguous long hex strings. +- Update pinned version of Rust to `nightly-2021-06-17`. +### Removed +- Remove ability to clone `SecretKey`s. ## [1.2.0] - 2021-05-27 ### Changed -* Change to Apache 2.0 license. -* Return a `Result` from the constructor of `SecretKey` rather than potentially panicking. -* Improve `Key` error reporting and tests. -### Fixed -* Fix `Key` deserialization. +- Change to Apache 2.0 license. +- Return a `Result` from the constructor of `SecretKey` rather than potentially panicking. +- Improve `Key` error reporting and tests. +### Fixed +- Fix `Key` deserialization. ## [1.1.1] - 2021-04-19 No changes. - - ## [1.1.0] - 2021-04-13 [YANKED] No changes. - - ## [1.0.1] - 2021-04-08 No changes. - - ## [1.0.0] - 2021-03-30 ### Added -* Initial release of types for use by software compatible with Casper mainnet. - +- Initial release of types for use by software compatible with Casper mainnet. [Keep a Changelog]: https://keepachangelog.com/en/1.0.0 [unreleased]: https://github.com/casper-network/casper-node/compare/24fc4027a...dev diff --git a/types/Cargo.toml b/types/Cargo.toml index f9307ee8b9..854df18c57 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -22,7 +22,7 @@ ed25519-dalek = { version = "2.0.0", default-features = false, features = [ "alloc", "zeroize", ] } -getrandom = { version = "0.2.0", features = ["rdrand"], optional = true } +getrandom = { version = "0.2.0", features = ["rdrand", "js"], optional = true } hex = { version = "0.4.2", default-features = false, features = ["alloc"] } hex_fmt = "0.3.0" humantime = { version = "2", optional = true } @@ -82,6 +82,8 @@ thiserror = "1" untrusted = "0.7.1" [features] +default = ["std-fs-io"] +std-fs-io = [] json-schema = ["once_cell", "schemars"] std = [ "derp", @@ -96,7 +98,6 @@ std = [ testing = ["proptest", "proptest-derive", "rand_pcg", "strum"] # DEPRECATED - use "testing" instead of "gens". gens = ["testing"] -sdk = ["getrandom/js"] [[bench]] name = "bytesrepr_bench" diff --git a/types/src/cl_value.rs b/types/src/cl_value.rs index bdc61dd826..e39ec46a12 100644 --- a/types/src/cl_value.rs +++ b/types/src/cl_value.rs @@ -18,8 +18,7 @@ use crate::{ }; mod jsonrepr; - -#[cfg(feature = "sdk")] +#[cfg(not(feature = "std-fs-io"))] pub use jsonrepr::cl_value_to_json; /// Error while converting a [`CLValue`] into a given type. diff --git a/types/src/crypto/asymmetric_key.rs b/types/src/crypto/asymmetric_key.rs index 6a39717089..368b7e718a 100644 --- a/types/src/crypto/asymmetric_key.rs +++ b/types/src/crypto/asymmetric_key.rs @@ -59,8 +59,7 @@ use crate::{ #[cfg(any(feature = "std", test))] use crate::crypto::ErrorExt; -#[cfg(any(feature = "std", test))] -#[cfg(not(any(feature = "sdk")))] +#[cfg(all(feature = "std", feature = "std-fs-io"))] use crate::file_utils::{read_file, write_file, write_private_file}; #[cfg(any(feature = "testing", test))] @@ -249,33 +248,32 @@ impl SecretKey { } /// Attempts to write the key bytes to the configured file path. - /// No operation the sdk feature + #[cfg(feature = "std-fs-io")] + pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { + write_private_file(file, self.to_pem()?).map_err(ErrorExt::SecretKeySave) + } + + /// No operation without std-fs-io + #[cfg(not(feature = "std-fs-io"))] #[allow(unused_variables)] pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { - #[cfg(not(any(feature = "sdk")))] - { - write_private_file(file, self.to_pem()?).map_err(ErrorExt::SecretKeySave) - } - #[cfg(feature = "sdk")] - { - Ok(()) - } + Ok(()) + } + + /// Attempts to read the key bytes from configured file path. + #[cfg(feature = "std-fs-io")] + pub fn from_file>(file: P) -> Result { + let data = read_file(file).map_err(ErrorExt::SecretKeyLoad)?; + Self::from_pem(data) } /// Attempts to read the key bytes from configured file path. - /// No operation the sdk feature, use SecretKey::from_pem + /// No operation without std-fs-io, use SecretKey::from_pem + #[cfg(not(feature = "std-fs-io"))] #[allow(unused_variables)] pub fn from_file>(file: P) -> Result { - #[cfg(not(any(feature = "sdk")))] - { - let data = read_file(file).map_err(ErrorExt::SecretKeyLoad)?; - Self::from_pem(data) - } - #[cfg(feature = "sdk")] - { - let data = vec![]; - Self::from_pem(data) - } + let data = vec![]; + Self::from_pem(data) } /// DER encodes a key. @@ -549,33 +547,31 @@ impl PublicKey { } /// Attempts to write the key bytes to the configured file path. - /// No operation the sdk feature + #[cfg(feature = "std-fs-io")] + pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { + write_file(file, self.to_pem()?).map_err(ErrorExt::PublicKeySave) + } + + /// No operation without std-fs-io + #[cfg(not(feature = "std-fs-io"))] #[allow(unused_variables)] pub fn to_file>(&self, file: P) -> Result<(), ErrorExt> { - #[cfg(not(any(feature = "sdk")))] - { - write_file(file, self.to_pem()?).map_err(ErrorExt::PublicKeySave) - } - #[cfg(feature = "sdk")] - { - Ok(()) - } + Ok(()) } /// Attempts to read the key bytes from configured file path. - /// No operation the sdk feature, use SecretKey::from_pem + #[cfg(feature = "std-fs-io")] + pub fn from_file>(file: P) -> Result { + let data = read_file(file).map_err(ErrorExt::PublicKeyLoad)?; + Self::from_pem(data) + } + + /// No operation without std-fs-io, use SecretKey::from_pem + #[cfg(not(feature = "std-fs-io"))] #[allow(unused_variables)] pub fn from_file>(file: P) -> Result { - #[cfg(not(any(feature = "sdk")))] - { - let data = read_file(file).map_err(ErrorExt::PublicKeyLoad)?; - Self::from_pem(data) - } - #[cfg(feature = "sdk")] - { - let data = vec![]; - Self::from_pem(data) - } + let data = vec![]; + Self::from_pem(data) } /// DER encodes a key. diff --git a/types/src/crypto/asymmetric_key/tests.rs b/types/src/crypto/asymmetric_key/tests.rs index 41eb89af3c..545b8dad00 100644 --- a/types/src/crypto/asymmetric_key/tests.rs +++ b/types/src/crypto/asymmetric_key/tests.rs @@ -82,7 +82,6 @@ fn known_secret_key_to_pem(expected_key: &SecretKey, known_key_pem: &str, expect assert_eq!(expected_tag, decoded.tag()); } -#[cfg(not(any(feature = "sdk")))] fn secret_key_file_roundtrip(secret_key: SecretKey) { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("test_secret_key.pem"); @@ -141,7 +140,6 @@ fn known_public_key_to_pem(known_key_hex: &str, known_key_pem: &str) { assert_eq!(key_bytes, Into::>::into(decoded)); } -#[cfg(not(any(feature = "sdk")))] fn public_key_file_roundtrip(public_key: PublicKey) { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("test_public_key.pem"); @@ -343,7 +341,6 @@ MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC super::known_secret_key_to_pem(&expected_key, KNOWN_KEY_PEM, ED25519_TAG); } - #[cfg(not(any(feature = "sdk")))] #[test] fn secret_key_to_and_from_file() { let mut rng = TestRng::new(); @@ -400,7 +397,6 @@ MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE= super::known_public_key_to_pem(KNOWN_KEY_HEX, KNOWN_KEY_PEM); } - #[cfg(not(any(feature = "sdk")))] #[test] fn public_key_to_and_from_file() { let mut rng = TestRng::new(); @@ -587,7 +583,6 @@ Yj9oTB9fx9+vvQdxJOhMtu46kGo0Uw== super::known_secret_key_to_pem(&expected_key, KNOWN_KEY_PEM, SECP256K1_TAG); } - #[cfg(not(any(feature = "sdk")))] #[test] fn secret_key_to_and_from_file() { let mut rng = TestRng::new(); @@ -645,7 +640,6 @@ kv+kBR5u4ISEAkuc2TFWQHX0Yj9oTB9fx9+vvQdxJOhMtu46kGo0Uw== super::known_public_key_to_pem(KNOWN_KEY_HEX, KNOWN_KEY_PEM); } - #[cfg(not(any(feature = "sdk")))] #[test] fn public_key_to_and_from_file() { let mut rng = TestRng::new(); diff --git a/types/src/crypto/error.rs b/types/src/crypto/error.rs index 6750e61f01..e749897cdc 100644 --- a/types/src/crypto/error.rs +++ b/types/src/crypto/error.rs @@ -11,7 +11,7 @@ use pem::PemError; #[cfg(any(feature = "std", test))] use thiserror::Error; -#[cfg(any(feature = "std", test))] +#[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] use crate::file_utils::{ReadFileError, WriteFileError}; /// Cryptographic errors. @@ -75,18 +75,22 @@ pub enum ErrorExt { CryptoError(#[from] Error), /// Error trying to read a secret key. + #[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] #[error("secret key load failed: {0}")] SecretKeyLoad(ReadFileError), /// Error trying to read a public key. + #[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] #[error("public key load failed: {0}")] PublicKeyLoad(ReadFileError), /// Error trying to write a secret key. + #[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] #[error("secret key save failed: {0}")] SecretKeySave(WriteFileError), /// Error trying to write a public key. + #[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] #[error("public key save failed: {0}")] PublicKeySave(WriteFileError), diff --git a/types/src/file_utils.rs b/types/src/file_utils.rs index 27fb36bb1c..814063aad6 100644 --- a/types/src/file_utils.rs +++ b/types/src/file_utils.rs @@ -1,15 +1,14 @@ //! Utilities for handling reading from and writing to files. -#[cfg(not(any(feature = "sdk")))] -use std::{fs, io::Write, os::unix::fs::OpenOptionsExt}; -use std::{ - io::{self}, - path::{Path, PathBuf}, -}; - +#[cfg(all(feature = "std", feature = "std-fs-io"))] +use std::{fs, io::Write, os::unix::fs::OpenOptionsExt, path::Path}; +#[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] +use std::{io, path::PathBuf}; +#[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] use thiserror::Error; /// Error reading a file. +#[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] #[derive(Debug, Error)] #[error("could not read '{0}': {error}", .path.display())] pub struct ReadFileError { @@ -21,6 +20,7 @@ pub struct ReadFileError { } /// Error writing a file +#[cfg(any(all(feature = "std", feature = "std-fs-io"), test))] #[derive(Debug, Error)] #[error("could not write to '{0}': {error}", .path.display())] pub struct WriteFileError { @@ -34,71 +34,47 @@ pub struct WriteFileError { /// Read complete at `path` into memory. /// /// Wraps `fs::read`, but preserves the filename for better error printing. -/// No operation the sdk feature -#[allow(unused_variables)] +#[cfg(all(feature = "std", feature = "std-fs-io"))] pub fn read_file>(filename: P) -> Result, ReadFileError> { - #[cfg(feature = "sdk")] - { - Ok(vec![]) - } - #[cfg(not(any(feature = "sdk")))] - { - let path = filename.as_ref(); - fs::read(path).map_err(|error| ReadFileError { - path: path.to_owned(), - error, - }) - } + let path = filename.as_ref(); + fs::read(path).map_err(|error| ReadFileError { + path: path.to_owned(), + error, + }) } /// Write data to `path`. /// /// Wraps `fs::write`, but preserves the filename for better error printing. -/// No operation the sdk feature -#[allow(unused_variables, unused)] +#[cfg(all(feature = "std", feature = "std-fs-io"))] pub(crate) fn write_file, B: AsRef<[u8]>>( filename: P, data: B, ) -> Result<(), WriteFileError> { - #[cfg(feature = "sdk")] - { - Ok(()) - } - #[cfg(not(any(feature = "sdk")))] - { - let path = filename.as_ref(); - fs::write(path, data.as_ref()).map_err(|error| WriteFileError { - path: path.to_owned(), - error, - }) - } + let path = filename.as_ref(); + fs::write(path, data.as_ref()).map_err(|error| WriteFileError { + path: path.to_owned(), + error, + }) } /// Writes data to `path`, ensuring only the owner can read or write it. /// /// Otherwise functions like [`write_file`]. -/// No operation the sdk feature -#[allow(unused_variables, unused)] +#[cfg(all(feature = "std", feature = "std-fs-io"))] pub(crate) fn write_private_file, B: AsRef<[u8]>>( filename: P, data: B, ) -> Result<(), WriteFileError> { - #[cfg(feature = "sdk")] - { - Ok(()) - } - #[cfg(not(any(feature = "sdk")))] - { - let path = filename.as_ref(); - fs::OpenOptions::new() - .write(true) - .create(true) - .mode(0o600) - .open(path) - .and_then(|mut file| file.write_all(data.as_ref())) - .map_err(|error| WriteFileError { - path: path.to_owned(), - error, - }) - } + let path = filename.as_ref(); + fs::OpenOptions::new() + .write(true) + .create(true) + .mode(0o600) + .open(path) + .and_then(|mut file| file.write_all(data.as_ref())) + .map_err(|error| WriteFileError { + path: path.to_owned(), + error, + }) } diff --git a/types/src/lib.rs b/types/src/lib.rs index 798c44dd21..c0414c5529 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -28,9 +28,9 @@ mod block_time; pub mod bytesrepr; pub mod checksummed_hex; mod cl_type; -#[cfg(not(any(feature = "sdk")))] +#[cfg(feature = "std-fs-io")] mod cl_value; -#[cfg(feature = "sdk")] +#[cfg(not(feature = "std-fs-io"))] pub mod cl_value; mod contract_wasm; pub mod contracts; @@ -38,7 +38,7 @@ pub mod crypto; mod deploy_info; mod era_id; mod execution_result; -#[cfg(any(feature = "std", test))] +#[cfg(any(feature = "std", feature = "std-fs-io", test))] pub mod file_utils; mod gas; #[cfg(any(feature = "testing", feature = "gens", test))] @@ -69,7 +69,7 @@ pub use access_rights::{ pub use api_error::ApiError; pub use block_time::{BlockTime, BLOCKTIME_SERIALIZED_LENGTH}; pub use cl_type::{named_key_type, CLType, CLTyped}; -#[cfg(feature = "sdk")] +#[cfg(not(feature = "std-fs-io"))] pub use cl_value::cl_value_to_json; pub use cl_value::{CLTypeMismatch, CLValue, CLValueError}; pub use contract_wasm::{ContractWasm, ContractWasmHash}; From b9855f2fc20a53adcde0a6e2e2a40123335cb1e8 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Fri, 17 Nov 2023 01:42:15 +0100 Subject: [PATCH 11/15] comment out --- types/src/crypto/asymmetric_key.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/types/src/crypto/asymmetric_key.rs b/types/src/crypto/asymmetric_key.rs index 368b7e718a..39ef55a537 100644 --- a/types/src/crypto/asymmetric_key.rs +++ b/types/src/crypto/asymmetric_key.rs @@ -267,7 +267,6 @@ impl SecretKey { Self::from_pem(data) } - /// Attempts to read the key bytes from configured file path. /// No operation without std-fs-io, use SecretKey::from_pem #[cfg(not(feature = "std-fs-io"))] #[allow(unused_variables)] From 1545cde68f9685f3acf44c9d3932eaa5f46ad7e4 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 28 Nov 2023 18:54:48 +0100 Subject: [PATCH 12/15] Remove some formatting --- hashing/Cargo.toml | 5 +---- types/Cargo.toml | 43 +++++++++---------------------------------- 2 files changed, 10 insertions(+), 38 deletions(-) diff --git a/hashing/Cargo.toml b/hashing/Cargo.toml index e92248926e..0b4f5f88cd 100644 --- a/hashing/Cargo.toml +++ b/hashing/Cargo.toml @@ -12,10 +12,7 @@ license = "Apache-2.0" [dependencies] blake2 = "0.9.0" base16 = "0.2.1" -casper-types = { version = "3.0.0", path = "../types", default-features = false, features = [ - "datasize", - "std", -] } +casper-types = { version = "3.0.0", path = "../types", default-features = false, features = ["datasize", "std"] } datasize = "0.2.9" hex = { version = "0.4.2", default-features = false, features = ["serde"] } hex-buffer-serde = "0.3.0" diff --git a/types/Cargo.toml b/types/Cargo.toml index 854df18c57..fdd9ff2993 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "casper-types" -# when updating, also update 'html_root_url' in lib.rs -version = "3.0.0" +version = "4.0.1" # when updating, also update 'html_root_url' in lib.rs authors = ["Fraser Hutchison "] edition = "2021" description = "Types shared by many casper crates for use on the Casper network." @@ -18,18 +17,12 @@ bitflags = "1" blake2 = { version = "0.9.0", default-features = false } datasize = { version = "0.2.4", optional = true } derp = { version = "0.0.14", optional = true } -ed25519-dalek = { version = "2.0.0", default-features = false, features = [ - "alloc", - "zeroize", -] } +ed25519-dalek = { version = "2.0.0", default-features = false, features = ["alloc", "zeroize"] } getrandom = { version = "0.2.0", features = ["rdrand", "js"], optional = true } hex = { version = "0.4.2", default-features = false, features = ["alloc"] } hex_fmt = "0.3.0" humantime = { version = "2", optional = true } -k256 = { version = "0.13.1", default-features = false, features = [ - "ecdsa", - "sha256", -] } +k256 = { version = "0.13.1", default-features = false, features = ["ecdsa", "sha256"] } num = { version = "0.4.0", default-features = false, features = ["alloc"] } num-derive = { version = "0.3.0", default-features = false } num-integer = { version = "0.1.42", default-features = false } @@ -41,19 +34,10 @@ proptest = { version = "1.0.0", optional = true } proptest-derive = { version = "0.3.0", optional = true } rand = { version = "0.8.3", default-features = false, features = ["small_rng"] } rand_pcg = { version = "0.3.0", optional = true } -schemars = { version = "=0.8.5", features = [ - "preserve_order", -], optional = true } -serde = { version = "1", default-features = false, features = [ - "alloc", - "derive", -] } -serde_bytes = { version = "0.11.5", default-features = false, features = [ - "alloc", -] } -serde_json = { version = "1.0.59", default-features = false, features = [ - "alloc", -] } +schemars = { version = "=0.8.5", features = ["preserve_order"], optional = true } +serde = { version = "1", default-features = false, features = ["alloc", "derive"] } +serde_bytes = { version = "0.11.5", default-features = false, features = ["alloc"] } +serde_json = { version = "1.0.59", default-features = false, features = ["alloc"] } strum = { version = "0.24", features = ["derive"], optional = true } thiserror = { version = "1", optional = true } uint = { version = "0.9.0", default-features = false } @@ -85,20 +69,11 @@ untrusted = "0.7.1" default = ["std-fs-io"] std-fs-io = [] json-schema = ["once_cell", "schemars"] -std = [ - "derp", - "getrandom/std", - "humantime", - "once_cell", - "pem", - "serde_json/preserve_order", - "thiserror", - "untrusted", -] +std = ["derp", "getrandom/std", "humantime", "once_cell", "pem", "serde_json/preserve_order", "thiserror", "untrusted"] testing = ["proptest", "proptest-derive", "rand_pcg", "strum"] # DEPRECATED - use "testing" instead of "gens". gens = ["testing"] [[bench]] name = "bytesrepr_bench" -harness = false +harness = false \ No newline at end of file From 5cf4043308585c5f54542a8d1648d8cdf18a6fa9 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 28 Nov 2023 18:57:36 +0100 Subject: [PATCH 13/15] Revert --- types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/Cargo.toml b/types/Cargo.toml index fdd9ff2993..d744e9a973 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "casper-types" -version = "4.0.1" # when updating, also update 'html_root_url' in lib.rs +version = "3.0.0" # when updating, also update 'html_root_url' in lib.rs authors = ["Fraser Hutchison "] edition = "2021" description = "Types shared by many casper crates for use on the Casper network." From 5f47f6fd2b79de0e04875b14416956cc454171ba Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 28 Nov 2023 19:05:53 +0100 Subject: [PATCH 14/15] Remove md formatting --- types/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/types/CHANGELOG.md b/types/CHANGELOG.md index df1ff646a3..c0dd9804a2 100644 --- a/types/CHANGELOG.md +++ b/types/CHANGELOG.md @@ -11,6 +11,10 @@ All notable changes to this project will be documented in this file. The format ## Unreleased +### Changed + +- Add "std-fs-io" feature and config checks to compile to wasm + ### Added - Add a new `SyncHandling` enum, which allows a node to opt out of historical sync. @@ -181,4 +185,4 @@ No changes. [1.1.1]: https://github.com/casper-network/casper-node/compare/v1.0.1...v1.1.1 [1.1.0]: https://github.com/casper-network/casper-node/compare/v1.0.1...v1.1.1 [1.0.1]: https://github.com/casper-network/casper-node/compare/v1.0.0...v1.0.1 -[1.0.0]: https://github.com/casper-network/casper-node/releases/tag/v1.0.0 +[1.0.0]: https://github.com/casper-network/casper-node/releases/tag/v1.0.0 \ No newline at end of file From e1b9866d500939f569acaa9830737ef375ec3a02 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 28 Nov 2023 19:35:48 +0100 Subject: [PATCH 15/15] revert again --- types/CHANGELOG.md | 140 +++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 62 deletions(-) diff --git a/types/CHANGELOG.md b/types/CHANGELOG.md index c0dd9804a2..e8ebab7053 100644 --- a/types/CHANGELOG.md +++ b/types/CHANGELOG.md @@ -1,178 +1,194 @@ # Changelog -All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog]. +All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog]. -[comment]: <> (Added: new features) -[comment]: <> (Changed: changes in existing functionality) +[comment]: <> (Added: new features) +[comment]: <> (Changed: changes in existing functionality) [comment]: <> (Deprecated: soon-to-be removed features) -[comment]: <> (Removed: now removed features) -[comment]: <> (Fixed: any bug fixes) -[comment]: <> (Security: in case of vulnerabilities) +[comment]: <> (Removed: now removed features) +[comment]: <> (Fixed: any bug fixes) +[comment]: <> (Security: in case of vulnerabilities) ## Unreleased -### Changed +### Added - Add "std-fs-io" feature and config checks to compile to wasm -### Added +## 4.0.1 -- Add a new `SyncHandling` enum, which allows a node to opt out of historical sync. -- Add default "std-fs-io" feature to compile to wasm +### Added +* Add a new `SyncHandling` enum, which allows a node to opt out of historical sync. ### Changed +* Update `k256` to version 0.13.1. -- Update `k256` to version 0.13.1. +### Removed +* Remove `ExecutionResult::successful_transfers`. ### Security +* Update `ed25519-dalek` to version 2.0.0 as mitigation for [RUSTSEC-2022-0093](https://rustsec.org/advisories/RUSTSEC-2022-0093) + -- Update `ed25519-dalek` to version 2.0.0 as mitigation for [RUSTSEC-2022-0093](https://rustsec.org/advisories/RUSTSEC-2022-0093) ## 3.0.0 ### Added - -- Add new `bytesrepr::Error::NotRepresentable` error variant that represents values that are not representable by the serialization format. -- Add new `Key::Unbond` key variant under which the new unbonding information (to support redelegation) is written. -- Add new `Key::ChainspecRegistry` key variant under which the `ChainspecRegistry` is written. -- Add new `Key::ChecksumRegistry` key variant under which a registry of checksums for a given block is written. There are two checksums in the registry, one for the execution results and the other for the approvals of all deploys in the block. -- Add new `StoredValue::Unbonding` variant to support redelegating. -- Add a new type `WithdrawPurses` which is meant to represent `UnbondingPurses` as they exist in current live networks. +* Add new `bytesrepr::Error::NotRepresentable` error variant that represents values that are not representable by the serialization format. +* Add new `Key::Unbond` key variant under which the new unbonding information (to support redelegation) is written. +* Add new `Key::ChainspecRegistry` key variant under which the `ChainspecRegistry` is written. +* Add new `Key::ChecksumRegistry` key variant under which a registry of checksums for a given block is written. There are two checksums in the registry, one for the execution results and the other for the approvals of all deploys in the block. +* Add new `StoredValue::Unbonding` variant to support redelegating. +* Add a new type `WithdrawPurses` which is meant to represent `UnbondingPurses` as they exist in current live networks. ### Changed - -- Extend `UnbondingPurse` to take a new field `new_validator` which represents the validator to whom tokens will be re-delegated. -- Increase `DICTIONARY_ITEM_KEY_MAX_LENGTH` to 128. -- Change prefix of formatted string representation of `ContractPackageHash` from "contract-package-wasm" to "contract-package-". Parsing from the old format is still supported. -- Apply `#[non_exhaustive]` to error enums. -- Change Debug output of `DeployHash` to hex-encoded string rather than a list of integers. +* Extend `UnbondingPurse` to take a new field `new_validator` which represents the validator to whom tokens will be re-delegated. +* Increase `DICTIONARY_ITEM_KEY_MAX_LENGTH` to 128. +* Change prefix of formatted string representation of `ContractPackageHash` from "contract-package-wasm" to "contract-package-". Parsing from the old format is still supported. +* Apply `#[non_exhaustive]` to error enums. +* Change Debug output of `DeployHash` to hex-encoded string rather than a list of integers. ### Fixed +* Fix some integer casts, where failure is now detected and reported via new error variant `NotRepresentable`. + -- Fix some integer casts, where failure is now detected and reported via new error variant `NotRepresentable`. ## 2.0.0 ### Fixed +* Republish v1.6.0 as v2.0.0 due to missed breaking change in API (addition of new variant to `Key`). + -- Republish v1.6.0 as v2.0.0 due to missed breaking change in API (addition of new variant to `Key`). ## 1.6.0 [YANKED] ### Added - -- Extend asymmetric key functionality, available via feature `std` (moved from `casper-nodes` crate). -- Provide `Timestamp` and `TimeDiff` types for time operations, with extended functionality available via feature `std` (moved from `casper-nodes` crate). -- Provide test-only functionality, in particular a seedable RNG `TestRng` which outputs its seed on test failure. Available via a new feature `testing`. -- Add new `Key::EraSummary` key variant under which the era summary info is written on each switch block execution. +* Extend asymmetric key functionality, available via feature `std` (moved from `casper-nodes` crate). +* Provide `Timestamp` and `TimeDiff` types for time operations, with extended functionality available via feature `std` (moved from `casper-nodes` crate). +* Provide test-only functionality, in particular a seedable RNG `TestRng` which outputs its seed on test failure. Available via a new feature `testing`. +* Add new `Key::EraSummary` key variant under which the era summary info is written on each switch block execution. ### Deprecated +* Deprecate `gens` feature: its functionality is included in the new `testing` feature. + -- Deprecate `gens` feature: its functionality is included in the new `testing` feature. ## 1.5.0 ### Added - -- Provide types and functionality to support improved access control inside execution engine. -- Provide `CLTyped` impl for `ContractPackage` to allow it to be passed into contracts. +* Provide types and functionality to support improved access control inside execution engine. +* Provide `CLTyped` impl for `ContractPackage` to allow it to be passed into contracts. ### Fixed +* Limit parsing of CLTyped objects to a maximum of 50 types deep. + -- Limit parsing of CLTyped objects to a maximum of 50 types deep. ## 1.4.6 - 2021-12-29 ### Changed +* Disable checksummed-hex encoding, but leave checksummed-hex decoding in place. + -- Disable checksummed-hex encoding, but leave checksummed-hex decoding in place. ## 1.4.5 - 2021-12-06 ### Added - -- Add function to `auction::MintProvider` trait to support minting into an existing purse. +* Add function to `auction::MintProvider` trait to support minting into an existing purse. ### Changed +* Change checksummed hex implementation to use 32 byte rather than 64 byte blake2b digests. + -- Change checksummed hex implementation to use 32 byte rather than 64 byte blake2b digests. ## [1.4.4] - 2021-11-18 ### Fixed +* Revert the accidental change to the `std` feature causing a broken build when this feature is enabled. + -- Revert the accidental change to the `std` feature causing a broken build when this feature is enabled. ## [1.4.3] - 2021-11-17 [YANKED] + + ## [1.4.2] - 2021-11-13 [YANKED] ### Added +* Add checksummed hex encoding following a scheme similar to [EIP-55](https://eips.ethereum.org/EIPS/eip-55). + -- Add checksummed hex encoding following a scheme similar to [EIP-55](https://eips.ethereum.org/EIPS/eip-55). ## [1.4.1] - 2021-10-23 No changes. + + ## [1.4.0] - 2021-10-21 [YANKED] ### Added - -- Add `json-schema` feature, disabled by default, to enable many types to be used to produce JSON-schema data. -- Add implicit `datasize` feature, disabled by default, to enable many types to derive the `DataSize` trait. -- Add `StoredValue` types to this crate. +* Add `json-schema` feature, disabled by default, to enable many types to be used to produce JSON-schema data. +* Add implicit `datasize` feature, disabled by default, to enable many types to derive the `DataSize` trait. +* Add `StoredValue` types to this crate. ### Changed - -- Support building and testing using stable Rust. -- Allow longer hex string to be presented in `json` files. Current maximum is increased from 100 to 150 characters. -- Improve documentation and `Debug` impls for `ApiError`. +* Support building and testing using stable Rust. +* Allow longer hex string to be presented in `json` files. Current maximum is increased from 100 to 150 characters. +* Improve documentation and `Debug` impls for `ApiError`. ### Deprecated +* Feature `std` is deprecated as it is now a no-op, since there is no benefit to linking the std lib via this crate. + -- Feature `std` is deprecated as it is now a no-op, since there is no benefit to linking the std lib via this crate. ## [1.3.0] - 2021-07-19 ### Changed - -- Restrict summarization when JSON pretty-printing to contiguous long hex strings. -- Update pinned version of Rust to `nightly-2021-06-17`. +* Restrict summarization when JSON pretty-printing to contiguous long hex strings. +* Update pinned version of Rust to `nightly-2021-06-17`. ### Removed +* Remove ability to clone `SecretKey`s. + -- Remove ability to clone `SecretKey`s. ## [1.2.0] - 2021-05-27 ### Changed - -- Change to Apache 2.0 license. -- Return a `Result` from the constructor of `SecretKey` rather than potentially panicking. -- Improve `Key` error reporting and tests. +* Change to Apache 2.0 license. +* Return a `Result` from the constructor of `SecretKey` rather than potentially panicking. +* Improve `Key` error reporting and tests. ### Fixed +* Fix `Key` deserialization. + -- Fix `Key` deserialization. ## [1.1.1] - 2021-04-19 No changes. + + ## [1.1.0] - 2021-04-13 [YANKED] No changes. + + ## [1.0.1] - 2021-04-08 No changes. + + ## [1.0.0] - 2021-03-30 ### Added +* Initial release of types for use by software compatible with Casper mainnet. + -- Initial release of types for use by software compatible with Casper mainnet. [Keep a Changelog]: https://keepachangelog.com/en/1.0.0 [unreleased]: https://github.com/casper-network/casper-node/compare/24fc4027a...dev