Skip to content

Add sdk feature into feat 2.0 #106

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

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1d5778d
Removing wrap_help + sdk feat
gRoussac Jul 25, 2023
ba71bd9
Removing multi-thread
gRoussac Jul 25, 2023
0077f2a
schemars bug
gRoussac Jul 25, 2023
5c4db67
update
gRoussac Jul 26, 2023
475e1e9
move node to ecosystem from Fraser branch
gRoussac Jul 26, 2023
271e2f8
Update cargo
gRoussac Jul 26, 2023
bdb7107
Expose cli to sdk
gRoussac Aug 1, 2023
32b7599
Try to load pem key from string
gRoussac Aug 2, 2023
df7ac7d
Return deploys for make_transfer make_deploy
gRoussac Aug 3, 2023
0b89f95
Expose simple_args::insert_arg
gRoussac Aug 15, 2023
dc84933
Merge branch 'feat-track-node-2.0' into rustSDK
gRoussac Aug 15, 2023
7a66891
little change
gRoussac Aug 15, 2023
287bdaf
Missing flag
gRoussac Aug 15, 2023
0d71d92
Remove flag
gRoussac Aug 15, 2023
5c1999b
small change
gRoussac Aug 15, 2023
6b06a8d
Expose json args
gRoussac Aug 15, 2023
6552d8d
Add doc
gRoussac Aug 16, 2023
d9742ad
Unreachable on simple args + json args
gRoussac Aug 16, 2023
8c3adb4
Add check_exactly_one_args_type_not_empty to parse.rs
gRoussac Aug 16, 2023
b298a36
no args is valid !
gRoussac Aug 16, 2023
4b21d82
changing name
gRoussac Aug 16, 2023
e827153
ConflictingArguments to context: String
gRoussac Aug 19, 2023
57d9199
little clone on DictionaryItemIdentifierget
gRoussac Aug 24, 2023
d6d8711
Hacking parse for sdk deploy module bytes
gRoussac Aug 24, 2023
66d7856
Fix import and sdk
gRoussac Aug 27, 2023
5fc8490
parse purse identifier
gRoussac Aug 27, 2023
0db6fbe
session_bytes
gRoussac Sep 1, 2023
ba04f27
Add some clone
gRoussac Sep 3, 2023
76e5a1a
remove call rpc pretty printed to std in sdk
gRoussac Sep 7, 2023
f5e6c7e
Merge branch 'feat-track-node-2.0' of ssh://github.com:/casper-ecosys…
gRoussac Sep 8, 2023
791eb49
Update for account identifier
gRoussac Sep 8, 2023
316a525
update types to feat 2.0
gRoussac Sep 14, 2023
90aefd5
Missing dep ?
gRoussac Sep 16, 2023
35954de
revert
gRoussac Sep 16, 2023
aa0a674
Remove json-schema
gRoussac Sep 19, 2023
e91cb76
getrandom js only for sdk feature
gRoussac Sep 20, 2023
5d4c2ad
getrandom was removed
gRoussac Sep 20, 2023
d2aa514
Cargo doc
gRoussac Sep 20, 2023
834032c
Sort back use in parse.rs
gRoussac Sep 21, 2023
4855e7b
Duplicate get_maybe_secret_key
gRoussac Sep 23, 2023
c02cf9a
Update cli.rs
gRoussac Sep 28, 2023
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
25 changes: 16 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "casper-client"
version = "2.0.0" # when updating, also update 'html_root_url' in lib.rs
authors = ["Marc Brinkmann <[email protected]>", "Fraser Hutchison <[email protected]>", "Zachary Showalter <[email protected]>"]
authors = [
"Marc Brinkmann <[email protected]>",
"Fraser Hutchison <[email protected]>",
"Zachary Showalter <[email protected]>",
]
edition = "2021"
description = "A client library and binary for interacting with the Casper network"
documentation = "https://docs.rs/casper-client"
Expand All @@ -11,6 +15,7 @@ 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"

Expand All @@ -22,8 +27,10 @@ doc = false
[dependencies]
async-trait = "0.1.59"
base16 = "0.2.1"
casper-types = { version = "3.0.0", features = ["std"] }
clap = { version = "4", features = ["cargo", "deprecated", "wrap_help"] }
casper-types = { version = "3.0.0", git = "https://github.com/casper-network/casper-node.git", branch = "feat-2.0", features = [
"std",
] }
clap = { version = "4", features = ["cargo", "deprecated"] }
clap_complete = "4"
hex-buffer-serde = "0.4.0"
humantime = "2"
Expand All @@ -33,27 +40,27 @@ num-traits = "0.2.15"
once_cell = "1"
rand = "0.8.5"
reqwest = { version = "0.11.13", features = ["json"] }
schemars = "0.8"
schemars = "0.8.13"
serde = { version = "1", default-features = false, features = ["derive"] }
serde-map-to-array = "1.1.0"
serde_json = { version = "1", features = ["preserve_order"] }
thiserror = "1.0.34"
tokio = { version = "1.23.0", features = ["macros", "net", "rt-multi-thread", "sync", "time", ]}
tokio = { version = "1.23.0", features = ["macros", "rt", "sync", "time"] }
uint = "0.9.4"

[dev-dependencies]
tempfile = "3.7.1"

[features]
sdk = ["casper-types/sdk"]

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

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

[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
43 changes: 29 additions & 14 deletions lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ mod parse;
mod payment_str_params;
mod session_str_params;
mod simple_args;
#[cfg(feature = "sdk")]
pub use parse::account_identifier as parse_account_identifier;
#[cfg(feature = "sdk")]
pub use parse::purse_identifier as parse_purse_identifier;
#[cfg(feature = "sdk")]
pub use simple_args::insert_arg;

#[cfg(test)]
mod tests;

use serde::Serialize;

#[cfg(doc)]
use casper_types::{account::AccountHash, Key};
use casper_types::{Digest, URef};
use casper_types::{Deploy, Digest, URef};

use crate::{
rpcs::{
Expand All @@ -53,15 +60,14 @@ use crate::{
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 payment_str_params::PaymentStrParams;
pub use session_str_params::SessionStrParams;
Expand Down Expand Up @@ -120,16 +126,20 @@ pub async fn speculative_put_deploy(
/// 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(not(any(feature = "sdk")))]
{
let output = parse::output_kind(maybe_output_path, force);
let _ = 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 @@ -139,6 +149,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(not(any(feature = "sdk")))]
pub fn sign_deploy_file(
input_path: &str,
secret_key_path: &str,
Expand Down Expand Up @@ -273,15 +284,14 @@ pub async fn speculative_transfer(
/// 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 @@ -291,7 +301,12 @@ pub fn make_transfer(
payment_params,
true,
)?;
crate::output_deploy(output, &deploy).map_err(CliError::from)
#[cfg(not(any(feature = "sdk")))]
{
let output = parse::output_kind(maybe_output_path, force);
let _ = crate::output_deploy(output, &deploy).map_err(CliError::from);
}
Ok(deploy)
}

/// Retrieves a [`Deploy`] from the network.
Expand Down
86 changes: 55 additions & 31 deletions lib/cli/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{parse, CliError, DeployStrParams, PaymentStrParams, SessionStrParams};
use crate::MAX_SERIALIZED_SIZE_OF_DEPLOY;
use casper_types::SecretKey;
use casper_types::{
account::AccountHash, AsymmetricType, Deploy, DeployBuilder, PublicKey, TransferTarget,
UIntParseError, URef, U512,
};

use super::{parse, CliError, DeployStrParams, PaymentStrParams, SessionStrParams};
use crate::MAX_SERIALIZED_SIZE_OF_DEPLOY;

/// Creates new Deploy with specified payment and session data.
pub fn with_payment_and_session(
deploy_params: DeployStrParams,
Expand All @@ -15,19 +15,6 @@ pub fn with_payment_and_session(
) -> Result<Deploy, CliError> {
let chain_name = deploy_params.chain_name.to_string();
let session = parse::session_executable_deploy_item(session_params)?;
let maybe_secret_key = if allow_unsigned_deploy && deploy_params.secret_key.is_empty() {
None
} else if deploy_params.secret_key.is_empty() && !allow_unsigned_deploy {
return Err(CliError::InvalidArgument {
context: "with_payment_and_session (secret_key, allow_unsigned_deploy)",
error: format!(
"allow_unsigned_deploy was {}, but no secret key was provided",
allow_unsigned_deploy
),
});
} else {
Some(parse::secret_key_from_file(deploy_params.secret_key)?)
};
let payment = parse::payment_executable_deploy_item(payment_params)?;
let timestamp = parse::timestamp(deploy_params.timestamp)?;
let ttl = parse::ttl(deploy_params.ttl)?;
Expand All @@ -37,7 +24,7 @@ pub fn with_payment_and_session(
.with_payment(payment)
.with_timestamp(timestamp)
.with_ttl(ttl);

let maybe_secret_key = get_maybe_secret_key(deploy_params.secret_key, allow_unsigned_deploy)?;
if let Some(secret_key) = &maybe_secret_key {
deploy_builder = deploy_builder.with_secret_key(secret_key);
}
Expand All @@ -63,21 +50,7 @@ pub fn new_transfer(
allow_unsigned_deploy: bool,
) -> Result<Deploy, CliError> {
let chain_name = deploy_params.chain_name.to_string();
let maybe_secret_key = if allow_unsigned_deploy && deploy_params.secret_key.is_empty() {
None
} else if deploy_params.secret_key.is_empty() && !allow_unsigned_deploy {
return Err(CliError::InvalidArgument {
context: "new_transfer (secret_key, allow_unsigned_deploy)",
error: format!(
"allow_unsigned_deploy was {}, but no secret key was provided",
allow_unsigned_deploy
),
});
} else {
Some(parse::secret_key_from_file(deploy_params.secret_key)?)
};
let payment = parse::payment_executable_deploy_item(payment_params)?;

let amount = U512::from_dec_str(amount).map_err(|err| CliError::FailedToParseUint {
context: "new_transfer amount",
error: UIntParseError::FromDecStr(err),
Expand Down Expand Up @@ -111,6 +84,8 @@ pub fn new_transfer(
.with_payment(payment)
.with_timestamp(timestamp)
.with_ttl(ttl);

let maybe_secret_key = get_maybe_secret_key(deploy_params.secret_key, allow_unsigned_deploy)?;
if let Some(secret_key) = &maybe_secret_key {
deploy_builder = deploy_builder.with_secret_key(secret_key);
}
Expand All @@ -123,3 +98,52 @@ pub fn new_transfer(
.map_err(crate::Error::from)?;
Ok(deploy)
}

#[cfg(not(feature = "sdk"))]
fn get_maybe_secret_key(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to this function is interesting.

Perhaps we could look to overload the function by conditionally compiling 1 of 2 functions of the same name behind feature flags as opposed to modifying the body of one function for readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well mostly this code was repeated two times in the file and we already discussed this condition

 let maybe_secret_key = if allow_unsigned_deploy && deploy_params.secret_key.is_empty() {
        None
    } else if deploy_params.secret_key.is_empty() && !allow_unsigned_deploy {
   

is imo more idiomatic in if A / else if B / else. It's quite complicate to have cargo check and cargo clippy accepting overloads, I tried to have 1/2 methods with both config accordingly and that never passed cargo check/clippy. Most of times the only accepted way is to simply have #[cfg] working like if statements. Otherwise most of the times a function will be missing on compilation if you have like --features-all

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure of the if else is fine, It's functionally equivalent and should work fine.
I do think that if you like the style suggestion I put on the sister PR against the node I would love to see the same style applied to the feature flag here. If you don't want to go with that style, consider the PR approved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casper client do not seem to have cargo clippy so I can duplicate this method in the client without side effects apparently.

Please check that commit 4855e7b thanks

secret_key: &str,
allow_unsigned_deploy: bool,
) -> Result<Option<SecretKey>, CliError> {
if !secret_key.is_empty() {
Ok(Some(parse::secret_key_from_file(secret_key)?))
} else if !allow_unsigned_deploy {
Err(CliError::InvalidArgument {
context: "with_payment_and_session (secret_key, allow_unsigned_deploy)",
error: format!(
"allow_unsigned_deploy was {}, but no secret key was provided",
allow_unsigned_deploy
),
})
} else {
Ok(None)
}
}

#[cfg(feature = "sdk")]
fn get_maybe_secret_key(
secret_key: &str,
allow_unsigned_deploy: bool,
) -> Result<Option<SecretKey>, CliError> {
if !secret_key.is_empty() {
let secret_key: SecretKey = match SecretKey::from_pem(secret_key) {
Ok(key) => key,
Err(error) => {
return Err(CliError::Core(crate::Error::CryptoError {
context: "secret key",
error,
}));
}
};
Ok(Some(secret_key))
} else if !allow_unsigned_deploy {
Err(CliError::InvalidArgument {
context: "with_payment_and_session (secret_key, allow_unsigned_deploy)",
error: format!(
"allow_unsigned_deploy was {}, but no secret key was provided",
allow_unsigned_deploy
),
})
} else {
Ok(None)
}
}
2 changes: 1 addition & 1 deletion lib/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub enum CliError {
ConflictingArguments {
/// Contextual description of where this error occurred including relevant paths,
/// filenames, etc.
context: &'static str,
context: String,
/// Arguments passed, with their values.
args: Vec<String>,
},
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/json_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use casper_types::{
use crate::cli::CliError;
pub use error::{Error, ErrorDetails};

/// Represents a JSON argument with a name, type, and value.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub(super) struct JsonArg {
pub struct JsonArg {
name: String,
#[serde(rename = "type")]
cl_type: CLType,
Expand Down
Loading