Skip to content

Commit

Permalink
Proposition of changes into Casper Types to support a Rust SDK for 1.6 (
Browse files Browse the repository at this point in the history
#110)

* feat std-fs-io

* Compile wasm

* avoid including dependencies required for bin target only when building the lib

* further restrict functions and tests included when std-fs-io feature is not enabled

* extend CI tests

* update patch section in manifest

* update deploy builder to not return invalid deploy

---------

Co-authored-by: Fraser Hutchison <[email protected]>
  • Loading branch information
gRoussac and Fraser999 authored Dec 18, 2023
1 parent 22f9df3 commit 8f94939
Show file tree
Hide file tree
Showing 46 changed files with 616 additions and 262 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci-casper-client-rs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
toolchain: stable
profile: minimal
components: rustfmt, clippy
target: wasm32-unknown-unknown

- name: Fmt
uses: actions-rs/cargo@v1
Expand All @@ -45,6 +46,12 @@ jobs:
command: clippy
args: --all-targets

- name: Clippy with no features
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets --no-default-features

- name: Doc
uses: actions-rs/cargo@v1
with:
Expand All @@ -55,3 +62,15 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test

- name: Test with no features
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features

- name: Build lib for Wasm with no features
uses: actions-rs/cargo@v1
with:
command: build
args: --lib --target wasm32-unknown-unknown --no-default-features
42 changes: 24 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,56 @@ repository = "https://github.com/casper-ecosystem/casper-client-rs"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib", "rlib"]
name = "casper_client"
path = "lib/lib.rs"

[[bin]]
name = "casper-client"
path = "src/main.rs"
doc = false
required-features = ["async-trait", "clap", "clap_complete", "tokio", "std-fs-io"]

[features]
default = ["async-trait", "clap", "clap_complete", "tokio", "std-fs-io"]
std-fs-io = ["casper-types/std-fs-io"]

[dependencies]
async-trait = "0.1.59"
async-trait = { version = "0.1.74", optional = true }
base16 = "0.2.1"
casper-hashing = "2.0.0"
casper-types = { version = "3.0.0", features = ["std"] }
clap = { version = "4", features = ["cargo", "deprecated", "wrap_help"] }
clap_complete = "4"
casper-hashing = "3.0.0"
casper-types = { version = "4.0.1", features = ["std"] }
clap = { version = "4.4.10", optional = true, features = ["cargo", "deprecated", "wrap_help"] }
clap_complete = { version = "4.4.4", optional = true }
hex-buffer-serde = "0.4.0"
humantime = "2"
itertools = "0.11.0"
humantime = "2.1.0"
itertools = "0.12.0"
jsonrpc-lite = "0.6.0"
num-traits = "0.2.15"
once_cell = "1"
once_cell = "1.18.0"
rand = "0.8.5"
reqwest = { version = "0.11.13", features = ["json"] }
schemars = "0.8"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1", features = ["preserve_order"] }
thiserror = "1.0.34"
tokio = { version = "1.23.0", features = ["macros", "net", "rt-multi-thread", "sync", "time", ]}
uint = "0.9.4"
reqwest = { version = "0.11.22", features = ["json"] }
schemars = "=0.8.5"
serde = { version = "1.0.193", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.108", features = ["preserve_order"] }
thiserror = "1.0.50"
tokio = { version = "1.34.0", optional = true, features = ["macros", "rt", "sync", "time"] }
uint = "0.9.5"

[dev-dependencies]
tempfile = "3.7.1"
tempfile = "3.8.1"

[build-dependencies]
vergen = { version = "7", default-features = false, features = ["git"] }

[patch.crates-io]
casper-hashing = { git = "https://github.com/casper-network/casper-node", branch = "dev" }
casper-types = { git = "https://github.com/casper-network/casper-node", branch = "dev"}
casper-types = { git = "https://github.com/casper-network/casper-node", branch = "dev" }

[package.metadata.deb]
features = ["vendored-openssl"]
revision = "0"
assets = [["./target/release/casper-client", "/usr/bin/casper-client", "755"], ]
assets = [["./target/release/casper-client", "/usr/bin/casper-client", "755"]]
extended-description = """
Package for Casper Client to connect to Casper Node.
Expand Down
79 changes: 50 additions & 29 deletions lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! * `maybe_block_id` - Must be a hex-encoded, 32-byte hash digest or a `u64` representing the
//! [`Block`] height or empty. If empty, the latest `Block` known on the server will be used.
/// Deploy module.
/// Functions for creating Deploys.
pub mod deploy;
mod deploy_str_params;
mod dictionary_item_str_params;
Expand All @@ -33,6 +33,7 @@ mod simple_args;
#[cfg(test)]
mod tests;

#[cfg(feature = "std-fs-io")]
use serde::Serialize;

use casper_hashing::Digest;
Expand All @@ -51,22 +52,25 @@ use crate::{
},
DictionaryItemIdentifier,
},
types::Deploy,
SuccessResponse,
};
#[cfg(doc)]
use crate::{Account, Block, Deploy, Error, StoredValue, Transfer};
use crate::{Account, Block, Error, StoredValue, Transfer};
#[cfg(doc)]
use casper_types::PublicKey;
pub use deploy_str_params::DeployStrParams;
pub use dictionary_item_str_params::DictionaryItemStrParams;
pub use error::CliError;
use json_args::JsonArg;
pub use json_args::{
help as json_args_help, Error as JsonArgsError, ErrorDetails as JsonArgsErrorDetails,
help as json_args_help, Error as JsonArgsError, ErrorDetails as JsonArgsErrorDetails, JsonArg,
};
pub use parse::{
account_identifier as parse_account_identifier, purse_identifier as parse_purse_identifier,
};
pub use payment_str_params::PaymentStrParams;
pub use session_str_params::SessionStrParams;
pub use simple_args::help as simple_args_help;
pub use simple_args::{help as simple_args_help, insert_arg};

/// Creates a [`Deploy`] and sends it to the network for execution.
///
Expand Down Expand Up @@ -111,26 +115,32 @@ pub async fn speculative_put_deploy(
.await
.map_err(CliError::from)
}
/// Creates a [`Deploy`] and outputs it to a file or stdout.
/// Returns a [`Deploy`] and outputs it to a file or stdout if the `std-fs-io` feature is enabled.
///
/// As a file, the `Deploy` can subsequently be signed by other parties using [`sign_deploy_file`]
/// and then sent to the network for execution using [`send_deploy_file`].
///
/// `maybe_output_path` specifies the output file path, or if empty, will print it to `stdout`. If
/// `force` is true, and a file exists at `maybe_output_path`, it will be overwritten. If `force`
/// is false and a file exists at `maybe_output_path`, [`Error::FileAlreadyExists`] is returned
/// and the file will not be written.
/// and then sent to the network for execution using [`send_deploy_file`]. Alternatively, the
/// returned `Deploy` can be signed via the [`Deploy::sign`] method.
///
/// If the `std-fs-io` feature is NOT enabled, `maybe_output_path` and `force` are ignored.
/// Otherwise, `maybe_output_path` specifies the output file path, or if empty, will print it to
/// `stdout`. If `force` is true, and a file exists at `maybe_output_path`, it will be
/// overwritten. If `force` is false and a file exists at `maybe_output_path`,
/// [`Error::FileAlreadyExists`] is returned and the file will not be written.
pub fn make_deploy(
maybe_output_path: &str,
#[allow(unused_variables)] maybe_output_path: &str,
deploy_params: DeployStrParams<'_>,
session_params: SessionStrParams<'_>,
payment_params: PaymentStrParams<'_>,
force: bool,
) -> Result<(), CliError> {
let output = parse::output_kind(maybe_output_path, force);
#[allow(unused_variables)] force: bool,
) -> Result<Deploy, CliError> {
let deploy =
deploy::with_payment_and_session(deploy_params, payment_params, session_params, true)?;
crate::output_deploy(output, &deploy).map_err(CliError::from)
#[cfg(feature = "std-fs-io")]
{
let output = parse::output_kind(maybe_output_path, force);
crate::output_deploy(output, &deploy).map_err(CliError::from)?;
}
Ok(deploy)
}

/// Reads a previously-saved [`Deploy`] from a file, cryptographically signs it, and outputs it to a
Expand All @@ -140,6 +150,7 @@ pub fn make_deploy(
/// `force` is true, and a file exists at `maybe_output_path`, it will be overwritten. If `force`
/// is false and a file exists at `maybe_output_path`, [`Error::FileAlreadyExists`] is returned
/// and the file will not be written.
#[cfg(feature = "std-fs-io")]
pub fn sign_deploy_file(
input_path: &str,
secret_key_path: &str,
Expand All @@ -154,6 +165,7 @@ pub fn sign_deploy_file(
/// Reads a previously-saved [`Deploy`] from a file and sends it to the network for execution.
///
/// For details of the parameters, see [the module docs](crate::cli#common-parameters).
#[cfg(feature = "std-fs-io")]
pub async fn send_deploy_file(
maybe_rpc_id: &str,
node_address: &str,
Expand All @@ -171,6 +183,7 @@ pub async fn send_deploy_file(
/// Reads a previously-saved [`Deploy`] from a file and sends it to the specified node for
/// speculative execution.
/// For details of the parameters, see [the module docs](crate::cli#common-parameters).
#[cfg(feature = "std-fs-io")]
pub async fn speculative_send_deploy_file(
maybe_block_id: &str,
maybe_rpc_id: &str,
Expand Down Expand Up @@ -264,25 +277,27 @@ pub async fn speculative_transfer(
.map_err(CliError::from)
}

/// Creates a transfer [`Deploy`] and outputs it to a file or stdout.
/// Returns a transfer [`Deploy`] and outputs it to a file or stdout if the `std-fs-io` feature is
/// enabled.
///
/// As a file, the `Deploy` can subsequently be signed by other parties using [`sign_deploy_file`]
/// and then sent to the network for execution using [`send_deploy_file`].
///
/// `maybe_output_path` specifies the output file path, or if empty, will print it to `stdout`. If
/// `force` is true, and a file exists at `maybe_output_path`, it will be overwritten. If `force`
/// is false and a file exists at `maybe_output_path`, [`Error::FileAlreadyExists`] is returned
/// and the file will not be written.
/// and then sent to the network for execution using [`send_deploy_file`]. Alternatively, the
/// returned `Deploy` can be signed via the [`Deploy::sign`] method.
///
/// If the `std-fs-io` feature is NOT enabled, `maybe_output_path` and `force` are ignored.
/// Otherwise, `maybe_output_path` specifies the output file path, or if empty, will print it to
/// `stdout`. If `force` is true, and a file exists at `maybe_output_path`, it will be
/// overwritten. If `force` is false and a file exists at `maybe_output_path`,
/// [`Error::FileAlreadyExists`] is returned and the file will not be written.
pub fn make_transfer(
maybe_output_path: &str,
#[allow(unused_variables)] maybe_output_path: &str,
amount: &str,
target_account: &str,
transfer_id: &str,
deploy_params: DeployStrParams<'_>,
payment_params: PaymentStrParams<'_>,
force: bool,
) -> Result<(), CliError> {
let output = parse::output_kind(maybe_output_path, force);
#[allow(unused_variables)] force: bool,
) -> Result<Deploy, CliError> {
let deploy = deploy::new_transfer(
amount,
None,
Expand All @@ -292,7 +307,12 @@ pub fn make_transfer(
payment_params,
true,
)?;
crate::output_deploy(output, &deploy).map_err(CliError::from)
#[cfg(feature = "std-fs-io")]
{
let output = parse::output_kind(maybe_output_path, force);
crate::output_deploy(output, &deploy).map_err(CliError::from)?;
}
Ok(deploy)
}

/// Retrieves a [`Deploy`] from the network.
Expand Down Expand Up @@ -658,6 +678,7 @@ pub async fn list_rpcs(
/// When `verbosity_level` is `0`, nothing is printed. For `1`, the value is printed with long
/// string fields shortened to a string indicating the character count of the field. Greater than
/// `1` is the same as for `1` except without abbreviation of long fields.
#[cfg(feature = "std-fs-io")]
pub fn json_pretty_print<T: ?Sized + Serialize>(
value: &T,
verbosity_level: u64,
Expand Down
Loading

0 comments on commit 8f94939

Please sign in to comment.