Skip to content
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

Proposition of changes into Casper Types to support a Rust SDK for 1.6 #4348

Closed
wants to merge 18 commits into from
Closed
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
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 @@ -158,6 +162,7 @@ check-rs: \
lint \
audit \
check-std-features \
check-no-default-features \
test-rs \
test-rs-no-default-features \
test-contracts-rs
Expand Down
2 changes: 1 addition & 1 deletion hashing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "Apache-2.0"
[dependencies]
blake2 = "0.9.0"
base16 = "0.2.1"
casper-types = { version = "4.0.1", path = "../types", features = ["datasize", "std"] }
casper-types = { version = "4.0.1", 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"
Expand Down
6 changes: 5 additions & 1 deletion types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ All notable changes to this project will be documented in this file. The format
[comment]: <> (Fixed: any bug fixes)
[comment]: <> (Security: in case of vulnerabilities)

## Unreleased

### Added

- Add "std-fs-io" feature and config checks to compile to wasm

## 4.0.1

Expand Down Expand Up @@ -197,4 +201,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
8 changes: 5 additions & 3 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 All @@ -39,7 +39,7 @@ 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 }
Expand All @@ -66,6 +66,8 @@ thiserror = "1"
untrusted = "0.7.1"

[features]
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"]
testing = ["proptest", "proptest-derive", "rand_pcg", "strum"]
Expand All @@ -74,4 +76,4 @@ gens = ["testing"]

[[bench]]
name = "bytesrepr_bench"
harness = false
harness = false
3 changes: 3 additions & 0 deletions types/src/cl_value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! CLvalue type
// TODO - remove once schemars stops causing warning.
#![allow(clippy::field_reassign_with_default)]

Expand All @@ -17,6 +18,8 @@ use crate::{
};

mod jsonrepr;
#[cfg(not(feature = "std-fs-io"))]
pub use jsonrepr::cl_value_to_json;

/// Error while converting a [`CLValue`] into a given type.
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
Expand Down
43 changes: 39 additions & 4 deletions types/src/crypto/asymmetric_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ 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(all(feature = "std", feature = "std-fs-io"))]
use crate::file_utils::{read_file, write_file, write_private_file};

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

/// Attempts to write the key bytes to the configured file path.
#[cfg(feature = "std-fs-io")]
pub fn to_file<P: AsRef<Path>>(&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<P: AsRef<Path>>(&self, file: P) -> Result<(), ErrorExt> {
Ok(())
}

/// Attempts to read the key bytes from configured file path.
#[cfg(feature = "std-fs-io")]
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)
}

/// No operation without std-fs-io, use SecretKey::from_pem
#[cfg(not(feature = "std-fs-io"))]
#[allow(unused_variables)]
pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self, ErrorExt> {
let data = vec![];
Self::from_pem(data)
}

/// DER encodes a key.
pub fn to_der(&self) -> Result<Vec<u8>, ErrorExt> {
match self {
Expand Down Expand Up @@ -528,16 +546,33 @@ impl PublicKey {
}

/// Attempts to write the key bytes to the configured file path.
#[cfg(feature = "std-fs-io")]
pub fn to_file<P: AsRef<Path>>(&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<P: AsRef<Path>>(&self, file: P) -> Result<(), ErrorExt> {
Ok(())
}

/// Attempts to read the key bytes from configured file path.
#[cfg(feature = "std-fs-io")]
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)
}

/// No operation without std-fs-io, use SecretKey::from_pem
#[cfg(not(feature = "std-fs-io"))]
#[allow(unused_variables)]
pub fn from_file<P: AsRef<Path>>(file: P) -> Result<Self, ErrorExt> {
let data = vec![];
Self::from_pem(data)
}

/// DER encodes a key.
pub fn to_der(&self) -> Result<Vec<u8>, ErrorExt> {
match self {
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(all(feature = "std", 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(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),

Expand Down
17 changes: 10 additions & 7 deletions types/src/file_utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Utilities for handling reading from and writing to files.

use std::{
fs,
io::{self, Write},
os::unix::fs::OpenOptionsExt,
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 {
Expand All @@ -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 {
Expand All @@ -34,6 +34,7 @@ pub struct WriteFileError {
/// Read complete at `path` into memory.
///
/// Wraps `fs::read`, but preserves the filename for better error printing.
#[cfg(all(feature = "std", feature = "std-fs-io"))]
pub fn read_file<P: AsRef<Path>>(filename: P) -> Result<Vec<u8>, ReadFileError> {
let path = filename.as_ref();
fs::read(path).map_err(|error| ReadFileError {
Expand All @@ -45,6 +46,7 @@ pub fn read_file<P: AsRef<Path>>(filename: P) -> Result<Vec<u8>, ReadFileError>
/// Write data to `path`.
///
/// Wraps `fs::write`, but preserves the filename for better error printing.
#[cfg(all(feature = "std", feature = "std-fs-io"))]
pub(crate) fn write_file<P: AsRef<Path>, B: AsRef<[u8]>>(
filename: P,
data: B,
Expand All @@ -59,6 +61,7 @@ pub(crate) fn write_file<P: AsRef<Path>, B: AsRef<[u8]>>(
/// Writes data to `path`, ensuring only the owner can read or write it.
///
/// Otherwise functions like [`write_file`].
#[cfg(all(feature = "std", feature = "std-fs-io"))]
pub(crate) fn write_private_file<P: AsRef<Path>, B: AsRef<[u8]>>(
filename: P,
data: B,
Expand Down
7 changes: 6 additions & 1 deletion types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ mod block_time;
pub mod bytesrepr;
pub mod checksummed_hex;
mod cl_type;
#[cfg(feature = "std-fs-io")]
mod cl_value;
#[cfg(not(feature = "std-fs-io"))]
pub mod cl_value;
mod contract_wasm;
pub mod contracts;
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))]
Expand Down Expand Up @@ -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(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};
#[doc(inline)]
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