Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4445: Split 'std' feature into 'std' and 'std-fs-io' in casper-types r=Fraser999 a=Fraser999

This PR restricts the functionality of the `std` feature of `casper-types` to exclude methods unsuitable when compiling the crate to Wasm.  It also adds a `std-fs-io` feature to include this functionality.

It borrows the concept and supersedes casper-network#4348 by `@gRoussac.`

casper-ecosystem/rustSDK#8
casper-ecosystem/rustSDK#10

Co-authored-by: Fraser Hutchison <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and Fraser999 authored Dec 18, 2023
2 parents 1bbb58e + bc3f9eb commit 8ca9001
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -157,6 +161,7 @@ check-rs: \
doc \
lint \
audit \
check-no-default-features \
check-std-features \
test-rs \
test-rs-no-default-features \
Expand Down
4 changes: 2 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bytes = "1.0.1"
casper-execution-engine = { version = "7.0.0", path = "../execution_engine" }
casper-hashing = { version = "3.0.0", path = "../hashing" }
casper-json-rpc = { version = "1.1.0", path = "../json_rpc" }
casper-types = { version = "4.0.1", path = "../types", features = ["datasize", "json-schema", "std"] }
casper-types = { version = "4.0.1", path = "../types", features = ["datasize", "json-schema", "std-fs-io"] }
datasize = { version = "0.2.11", features = ["detailed", "fake_clock-types", "futures-types", "smallvec-types"] }
derive_more = "0.99.7"
either = { version = "1", features = ["serde"] }
Expand Down Expand Up @@ -97,7 +97,7 @@ vergen = { version = "8.2.1", default-features = false, features = ["git", "gito
[dev-dependencies]
assert-json-diff = "2.0.1"
assert_matches = "1.5.0"
casper-types = { path = "../types", features = ["datasize", "json-schema", "std", "testing"] }
casper-types = { path = "../types", features = ["datasize", "json-schema", "std-fs-io", "testing"] }
fake_instant = "0.4.0"
pnet = "0.28.0"
pretty_assertions = "0.7.2"
Expand Down
7 changes: 7 additions & 0 deletions types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ All notable changes to this project will be documented in this file. The format



## Unreleased

### Changed
* Remove filesystem I/O functionality from the `std` feature, and gated this behind a new feature `std-fs-io` which depends upon `std`.



## 4.0.1

### Added
Expand Down
7 changes: 5 additions & 2 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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"] }
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 }
Expand Down Expand Up @@ -67,7 +67,10 @@ untrusted = "0.7.1"

[features]
json-schema = ["once_cell", "schemars"]
std = ["derp", "getrandom/std", "humantime", "once_cell", "pem", "serde_json/preserve_order", "thiserror", "untrusted"]
# Includes a restricted set of std lib functionality suitable for usage e.g. in a JS environment when compiled to Wasm.
std = ["base16/std", "derp", "getrandom/std", "humantime", "once_cell", "pem", "serde_json/preserve_order", "thiserror", "untrusted"]
# Includes a complete set of std lib functionality, including filesystem I/O operations.
std-fs-io = ["std"]
testing = ["proptest", "proptest-derive", "rand_pcg", "strum"]
# DEPRECATED - use "testing" instead of "gens".
gens = ["testing"]
Expand Down
2 changes: 1 addition & 1 deletion types/src/cl_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
bytesrepr::{self, Bytes, FromBytes, ToBytes, U32_SERIALIZED_LENGTH},
checksummed_hex, CLType, CLTyped,
};

pub use jsonrepr::cl_value_to_json;
mod jsonrepr;

/// Error while converting a [`CLValue`] into a given type.
Expand Down
13 changes: 8 additions & 5 deletions types/src/crypto/asymmetric_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ use serde_json::json;
#[cfg(any(feature = "std", test))]
use untrusted::Input;

#[cfg(any(feature = "std", test))]
use crate::crypto::ErrorExt;
#[cfg(any(feature = "std-fs-io", test))]
use crate::file_utils::{read_file, write_file, write_private_file};
#[cfg(any(all(feature = "std", feature = "testing"), test))]
use crate::testing::TestRng;
use crate::{
Expand All @@ -55,11 +59,6 @@ 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},
};

#[cfg(any(feature = "testing", test))]
pub mod gens;
Expand Down Expand Up @@ -247,11 +246,13 @@ impl SecretKey {
}

/// Attempts to write the key bytes to the configured file path.
#[cfg(any(feature = "std-fs-io", test))]
pub fn to_file<P: AsRef<Path>>(&self, file: P) -> Result<(), ErrorExt> {
write_private_file(file, self.to_pem()?).map_err(ErrorExt::SecretKeySave)
}

/// Attempts to read the key bytes from configured file path.
#[cfg(any(feature = "std-fs-io", test))]
pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self, ErrorExt> {
let data = read_file(file).map_err(ErrorExt::SecretKeyLoad)?;
Self::from_pem(data)
Expand Down Expand Up @@ -528,11 +529,13 @@ impl PublicKey {
}

/// Attempts to write the key bytes to the configured file path.
#[cfg(any(feature = "std-fs-io", test))]
pub fn to_file<P: AsRef<Path>>(&self, file: P) -> Result<(), ErrorExt> {
write_file(file, self.to_pem()?).map_err(ErrorExt::PublicKeySave)
}

/// Attempts to read the key bytes from configured file path.
#[cfg(any(feature = "std-fs-io", test))]
pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self, ErrorExt> {
let data = read_file(file).map_err(ErrorExt::PublicKeyLoad)?;
Self::from_pem(data)
Expand Down
6 changes: 5 additions & 1 deletion types/src/crypto/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use pem::PemError;
#[cfg(any(feature = "std", test))]
use thiserror::Error;

#[cfg(any(feature = "std", test))]
#[cfg(any(feature = "std-fs-io", test))]
use crate::file_utils::{ReadFileError, WriteFileError};

/// Cryptographic errors.
Expand Down Expand Up @@ -75,18 +75,22 @@ pub enum ErrorExt {
CryptoError(#[from] Error),

/// Error trying to read a secret key.
#[cfg(any(feature = "std-fs-io", test))]
#[error("secret key load failed: {0}")]
SecretKeyLoad(ReadFileError),

/// Error trying to read a public key.
#[cfg(any(feature = "std-fs-io", test))]
#[error("public key load failed: {0}")]
PublicKeyLoad(ReadFileError),

/// Error trying to write a secret key.
#[cfg(any(feature = "std-fs-io", test))]
#[error("secret key save failed: {0}")]
SecretKeySave(WriteFileError),

/// Error trying to write a public key.
#[cfg(any(feature = "std-fs-io", test))]
#[error("public key save failed: {0}")]
PublicKeySave(WriteFileError),

Expand Down
4 changes: 2 additions & 2 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod crypto;
mod deploy_info;
mod era_id;
mod execution_result;
#[cfg(any(feature = "std", test))]
#[cfg(any(feature = "std-fs-io", test))]
pub mod file_utils;
mod gas;
#[cfg(any(feature = "testing", feature = "gens", test))]
Expand Down Expand Up @@ -66,7 +66,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};
pub use cl_value::{CLTypeMismatch, CLValue, CLValueError};
pub use cl_value::{cl_value_to_json, CLTypeMismatch, CLValue, CLValueError};
pub use contract_wasm::{ContractWasm, ContractWasmHash};
#[doc(inline)]
pub use contracts::{
Expand Down
6 changes: 6 additions & 0 deletions types/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8ca9001

Please sign in to comment.