Skip to content

Commit

Permalink
Merge #4714
Browse files Browse the repository at this point in the history
4714: Complete feature `std-fs-io` in casper-types for wasm compilation r=zajko a=gRoussac

Remove filesystem I/O functionality from the `std` feature, and gate this behind a new feature `std-fs-io` which depends upon `std`.
 
Allows to compile types without `from_file/to_file/sysconf/os clock`
 
Remove compilation warnings for feat `testing`

Add `lint-no-default-features/check-no-default-features`

Add documentation on `deploy_hash_arb`


Co-authored-by: gRoussac <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and gRoussac authored Oct 21, 2024
2 parents ce03bbf + d0d5788 commit 1ab1ff8
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 32 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ 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-testing-features
check-std-fs-io-features:
cd types && $(CARGO) check --all-targets --features=std-fs-io
cd types && $(CARGO) check --lib --features=std-fs-io

check-testing-features:
cd types && $(CARGO) check --all-targets --no-default-features --features=testing
cd types && $(CARGO) check --all-targets --features=testing
Expand All @@ -129,12 +132,16 @@ 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-no-default-features

.PHONY: lint-default-features
lint-default-features:
$(CARGO) clippy --all-targets -- -D warnings

.PHONY: lint-no-default-features
lint-no-default-features:
$(CARGO) clippy --all-targets --no-default-features -- -D warnings

.PHONY: lint-all-features
lint-all-features:
$(CARGO) clippy --all-targets --all-features -- -D warnings
Expand Down Expand Up @@ -168,6 +175,7 @@ check-rs: \
audit \
check-no-default-features \
check-std-features \
check-std-fs-io-features \
check-testing-features \
test-rs \
test-rs-no-default-features \
Expand Down
2 changes: 1 addition & 1 deletion node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,4 @@ All notable changes to this project will be documented in this file. The format
[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
3 changes: 1 addition & 2 deletions types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ All notable changes to this project will be documented in this file. The format




## [Unreleased] (node 1.5.4)
## [Unreleased] (node 1.6)

### Changed
* Remove filesystem I/O functionality from the `std` feature, and gated this behind a new feature `std-fs-io` which depends upon `std`.
Expand Down
5 changes: 5 additions & 0 deletions types/src/account/account_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ impl AccountHash {
)
}

/// Hexadecimal representation of the hash.
pub fn to_hex_string(&self) -> String {
base16::encode_lower(&self.0)
}

/// Parses a string formatted as per `Self::to_formatted_string()` into an `AccountHash`.
pub fn from_formatted_str(input: &str) -> Result<Self, FromStrError> {
let remainder = input
Expand Down
30 changes: 23 additions & 7 deletions types/src/addressable_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,24 @@ pub const MAX_GROUPS: u8 = 10;
/// Maximum number of URefs which can be assigned across all user groups.
pub const MAX_TOTAL_UREFS: usize = 100;

const ADDRESSABLE_ENTITY_STRING_PREFIX: &str = "addressable-entity-";

const ENTITY_PREFIX: &str = "entity-";
const ACCOUNT_ENTITY_PREFIX: &str = "account-";
const CONTRACT_ENTITY_PREFIX: &str = "contract-";
const SYSTEM_ENTITY_PREFIX: &str = "system-";
const NAMED_KEY_PREFIX: &str = "named-key-";
/// The prefix applied to the hex-encoded `Addressable Entity` to produce a formatted string
/// representation.
pub const ADDRESSABLE_ENTITY_STRING_PREFIX: &str = "addressable-entity-";
/// The prefix applied to the hex-encoded `Entity` to produce a formatted string
/// representation.
pub const ENTITY_PREFIX: &str = "entity-";
/// The prefix applied to the hex-encoded `Account` to produce a formatted string
/// representation.
pub const ACCOUNT_ENTITY_PREFIX: &str = "account-";
/// The prefix applied to the hex-encoded `Smart contract` to produce a formatted string
/// representation.
pub const CONTRACT_ENTITY_PREFIX: &str = "contract-";
/// The prefix applied to the hex-encoded `System entity account or contract` to produce a formatted
/// string representation.
pub const SYSTEM_ENTITY_PREFIX: &str = "system-";
/// The prefix applied to the hex-encoded `Named Key` to produce a formatted string
/// representation.
pub const NAMED_KEY_PREFIX: &str = "named-key-";

/// Set of errors which may happen when working with contract headers.
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -279,6 +290,11 @@ impl AddressableEntityHash {
)
}

/// Hexadecimal representation of the hash.
pub fn to_hex_string(&self) -> String {
base16::encode_lower(&self.0)
}

/// Parses a string formatted as per `Self::to_formatted_string()` into a
/// `AddressableEntityHash`.
pub fn from_formatted_str(input: &str) -> Result<Self, FromStrError> {
Expand Down
7 changes: 6 additions & 1 deletion types/src/block/block_hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::vec::Vec;
use alloc::{string::String, vec::Vec};
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "datasize")]
Expand Down Expand Up @@ -53,6 +53,11 @@ impl BlockHash {
&self.0
}

/// Hexadecimal representation of the hash.
pub fn to_hex_string(&self) -> String {
base16::encode_lower(self.inner())
}

// This method is not intended to be used by third party crates.
#[doc(hidden)]
#[cfg(feature = "json-schema")]
Expand Down
10 changes: 10 additions & 0 deletions types/src/crypto/asymmetric_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ impl PublicKey {
AccountHash::from(self)
}

/// Hexadecimal representation of the key.
pub fn to_hex_string(&self) -> String {
self.to_hex()
}

/// Returns `true` if this public key is of the `System` variant.
pub fn is_system(&self) -> bool {
matches!(self, PublicKey::System)
Expand Down Expand Up @@ -967,6 +972,11 @@ impl Signature {
Signature::Secp256k1(_) => SECP256K1,
}
}

/// Hexadecimal representation of the signature.
pub fn to_hex_string(&self) -> String {
self.to_hex()
}
}

impl AsymmetricType<'_> for Signature {
Expand Down
9 changes: 9 additions & 0 deletions types/src/crypto/asymmetric_key/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(any(feature = "std-fs-io", test))]
fn secret_key_file_roundtrip(secret_key: SecretKey) {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("test_secret_key.pem");
Expand Down Expand Up @@ -140,6 +141,7 @@ fn known_public_key_to_pem(known_key_hex: &str, known_key_pem: &str) {
assert_eq!(key_bytes, Into::<Vec<u8>>::into(decoded));
}

#[cfg(any(feature = "std-fs-io", test))]
fn public_key_file_roundtrip(public_key: PublicKey) {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("test_public_key.pem");
Expand Down Expand Up @@ -214,6 +216,7 @@ fn check_ord_and_hash<T: Clone + Ord + PartialOrd + Hash>(low: T, high: T) {
}

mod system {
#[cfg(any(feature = "std-fs-io", test))]
use std::path::Path;

use super::{sign, verify};
Expand All @@ -229,6 +232,7 @@ mod system {
assert!(SecretKey::system().to_pem().is_err());
}

#[cfg(any(feature = "std-fs-io", test))]
#[test]
fn secret_key_to_file_should_error() {
assert!(SecretKey::system().to_file(Path::new("/dev/null")).is_err());
Expand All @@ -249,6 +253,7 @@ mod system {
assert!(PublicKey::system().to_pem().is_err());
}

#[cfg(any(feature = "std-fs-io", test))]
#[test]
fn public_key_to_file_should_error() {
assert!(PublicKey::system().to_file(Path::new("/dev/null")).is_err());
Expand Down Expand Up @@ -341,6 +346,7 @@ MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC
super::known_secret_key_to_pem(&expected_key, KNOWN_KEY_PEM, ED25519_TAG);
}

#[cfg(any(feature = "std-fs-io", test))]
#[test]
fn secret_key_to_and_from_file() {
let mut rng = TestRng::new();
Expand Down Expand Up @@ -397,6 +403,7 @@ MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=
super::known_public_key_to_pem(KNOWN_KEY_HEX, KNOWN_KEY_PEM);
}

#[cfg(any(feature = "std-fs-io", test))]
#[test]
fn public_key_to_and_from_file() {
let mut rng = TestRng::new();
Expand Down Expand Up @@ -584,6 +591,7 @@ Yj9oTB9fx9+vvQdxJOhMtu46kGo0Uw==
}

#[test]
#[cfg(any(feature = "std-fs-io", test))]
fn secret_key_to_and_from_file() {
let mut rng = TestRng::new();
let secret_key = SecretKey::random_secp256k1(&mut rng);
Expand Down Expand Up @@ -640,6 +648,7 @@ kv+kBR5u4ISEAkuc2TFWQHX0Yj9oTB9fx9+vvQdxJOhMtu46kGo0Uw==
super::known_public_key_to_pem(KNOWN_KEY_HEX, KNOWN_KEY_PEM);
}

#[cfg(any(feature = "std-fs-io", test))]
#[test]
fn public_key_to_and_from_file() {
let mut rng = TestRng::new();
Expand Down
7 changes: 6 additions & 1 deletion types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mod uint;
mod uref;
mod validator_change;

#[cfg(feature = "std")]
#[cfg(all(feature = "std", any(feature = "std-fs-io", test)))]
use libc::{c_long, sysconf, _SC_PAGESIZE};
#[cfg(feature = "std")]
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -231,8 +231,13 @@ pub static OS_PAGE_SIZE: Lazy<usize> = Lazy::new(|| {
/// Sensible default for many if not all systems.
const DEFAULT_PAGE_SIZE: usize = 4096;

#[cfg(any(feature = "std-fs-io", test))]
// https://www.gnu.org/software/libc/manual/html_node/Sysconf.html
let value: c_long = unsafe { sysconf(_SC_PAGESIZE) };

#[cfg(not(any(feature = "std-fs-io", test)))]
let value = 0;

if value <= 0 {
DEFAULT_PAGE_SIZE
} else {
Expand Down
38 changes: 36 additions & 2 deletions types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ mod transaction_target;
mod transaction_v1;
mod transfer_target;

use alloc::{collections::BTreeSet, vec::Vec};
use alloc::{
collections::BTreeSet,
string::{String, ToString},
vec::Vec,
};
use core::fmt::{self, Debug, Display, Formatter};
#[cfg(any(feature = "std", test))]
use std::hash::Hash;
Expand All @@ -45,7 +49,7 @@ use crate::testing::TestRng;
use crate::{
account::AccountHash,
bytesrepr::{self, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
Digest, SecretKey, TimeDiff, Timestamp,
Digest, Phase, SecretKey, TimeDiff, Timestamp,
};
#[cfg(any(feature = "std", test))]
use crate::{Chainspec, Gas, Motes};
Expand Down Expand Up @@ -196,6 +200,31 @@ impl Transaction {
Ok(approvals_hash)
}

/// Returns the chain name for the transaction, whether it's a `Deploy` or `V1` transaction.
pub fn chain_name(&self) -> String {
match self {
Transaction::Deploy(txn) => txn.chain_name().to_string(),
Transaction::V1(txn) => txn.chain_name().to_string(),
}
}

/// Checks if the transaction is a standard payment.
///
/// For `Deploy` transactions, it checks if the session is a standard payment
/// in the payment phase. For `V1` transactions, it returns the value of
/// `standard_payment` if the pricing mode is `Classic`, otherwise it returns `true`.
pub fn is_standard_payment(&self) -> bool {
match self {
Transaction::Deploy(txn) => txn.session().is_standard_payment(Phase::Payment),
Transaction::V1(txn) => match txn.pricing_mode() {
PricingMode::Classic {
standard_payment, ..
} => *standard_payment,
_ => true,
},
}
}

/// Returns the computed `TransactionId` uniquely identifying this transaction and its
/// approvals.
pub fn compute_id(&self) -> TransactionId {
Expand Down Expand Up @@ -439,6 +468,11 @@ pub mod gens {
prelude::{Arbitrary, Strategy},
};

/// Generates a random `DeployHash` for testing purposes.
///
/// This function is used to generate random `DeployHash` values for testing purposes.
/// It produces a proptest `Strategy` that can be used to generate arbitrary `DeployHash`
/// values.
pub fn deploy_hash_arb() -> impl Strategy<Value = DeployHash> {
array::uniform32(<u8>::arbitrary()).prop_map(DeployHash::from_raw)
}
Expand Down
7 changes: 6 additions & 1 deletion types/src/transaction/deploy/deploy_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ impl<'a> DeployBuilder<'a> {
/// [`with_standard_payment`](Self::with_standard_payment) or
/// [`with_payment`](Self::with_payment)
pub fn new<C: Into<String>>(chain_name: C, session: ExecutableDeployItem) -> Self {
#[cfg(any(feature = "std-fs-io", test))]
let timestamp = Timestamp::now();
#[cfg(not(any(feature = "std-fs-io", test)))]
let timestamp = Timestamp::zero();

DeployBuilder {
account: None,
secret_key: None,
timestamp: Timestamp::now(),
timestamp,
ttl: Self::DEFAULT_TTL,
gas_price: Self::DEFAULT_GAS_PRICE,
dependencies: vec![],
Expand Down
7 changes: 6 additions & 1 deletion types/src/transaction/deploy/deploy_hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::vec::Vec;
use alloc::{string::String, vec::Vec};
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "datasize")]
Expand Down Expand Up @@ -45,6 +45,11 @@ impl DeployHash {
&self.0
}

/// Hexadecimal representation of the hash.
pub fn to_hex_string(&self) -> String {
base16::encode_lower(self.inner())
}

/// Returns a new `DeployHash` directly initialized with the provided bytes; no hashing is done.
#[cfg(any(feature = "testing", test))]
pub const fn from_raw(raw_digest: [u8; Self::LENGTH]) -> Self {
Expand Down
7 changes: 6 additions & 1 deletion types/src/transaction/transaction_hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::vec::Vec;
use alloc::{string::String, vec::Vec};
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "datasize")]
Expand Down Expand Up @@ -45,6 +45,11 @@ impl TransactionHash {
}
}

/// Hexadecimal representation of the hash.
pub fn to_hex_string(&self) -> String {
base16::encode_lower(&self.digest())
}

/// Returns a random `TransactionHash`.
#[cfg(any(feature = "testing", test))]
pub fn random(rng: &mut TestRng) -> Self {
Expand Down
Loading

0 comments on commit 1ab1ff8

Please sign in to comment.