Skip to content

Commit

Permalink
Changing TransactionV1 structure follwing casper-network/casper-node#…
Browse files Browse the repository at this point in the history
…4890 (#191)

* merge changes

* Cargo types

* is_install_upgrade / additional_computation_factor wip

* Fix tests and additional_computation_factor

* Update json-schema
  • Loading branch information
gRoussac authored Oct 17, 2024
1 parent c2780cd commit 99be816
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 191 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ default = ["async-trait", "clap", "clap_complete", "std-fs-io"]
std-fs-io = ["casper-types/std-fs-io"]

[dependencies]
async-trait = { version = "0.1.59", default-features = false, optional = true }
async-trait = { version = "0.1.74", default-features = false, optional = true }
base16 = "0.2.1"
casper-types = { version = "5.0.0", features = ["std", "json-schema"] }
clap = { version = "~4.4", features = ["cargo", "deprecated"], optional = true }
clap_complete = { version = "~4.4", default-features = false, 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.12.5", features = ["json"] }
schemars = "0.8.13"
schemars = "0.8.18"
serde = { version = "1", default-features = false, features = ["derive"] }
serde-map-to-array = "1.1.1"
serde_json = { version = "1", features = ["preserve_order"] }
thiserror = "1"
tokio = { version = "1.38.0", features = ["macros", "rt", "sync", "time"] }
uint = "0.9.4"
tokio = { version = "1.39.3", features = ["macros", "rt", "sync", "time"] }
uint = "0.9.5"

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

[patch.crates-io]
casper-types = { git = "https://github.com/casper-network/casper-node.git", branch = "feat-2.0" }
Expand Down
4 changes: 0 additions & 4 deletions lib/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ pub enum CliError {
#[error("Failed to parse a transfer target")]
FailedToParseTransferTarget,

/// Failed to parse transaction category.
#[error("Failed to parse a transaction category")]
FailedToParseTransactionCategory,

/// Failed to parse a validator public key.
#[error("Failed to parse a validator public key")]
FailedToParseValidatorPublicKey,
Expand Down
117 changes: 93 additions & 24 deletions lib/cli/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,72 +824,85 @@ pub(super) fn public_key(public_key: &str) -> Result<Option<PublicKey>, CliError

pub(super) fn pricing_mode(
pricing_mode_identifier_str: &str,
maybe_payment_amount_str: &str,
maybe_gas_price_tolerance_str: &str,
maybe_standard_payment_str: &str,
payment_amount_str: &str,
gas_price_tolerance_str: &str,
additional_computation_factor_str: &str,
standard_payment_str: &str,
maybe_receipt: Option<Digest>,
) -> Result<PricingMode, CliError> {
match pricing_mode_identifier_str.to_lowercase().as_str() {
"classic" => {
if maybe_gas_price_tolerance_str.is_empty() {
if gas_price_tolerance_str.is_empty() {
return Err(CliError::InvalidArgument {
context: "gas_price_tolerance",
error: "Gas price tolerance is required".to_string(),
});
}
if maybe_payment_amount_str.is_empty() {
if payment_amount_str.is_empty() {
return Err(CliError::InvalidArgument {
context: "payment_amount",
error: "Payment amount is required".to_string(),
});
}
if maybe_standard_payment_str.is_empty() {
if standard_payment_str.is_empty() {
return Err(CliError::InvalidArgument {
context: "standard_payment",
error: "Standard payment flag is required".to_string(),
});
}
let gas_price_tolerance =
maybe_gas_price_tolerance_str
.parse::<u8>()
.map_err(|error| CliError::FailedToParseInt {
context: "gas_price_tolerance",
error,
})?;
let payment_amount = maybe_payment_amount_str.parse::<u64>().map_err(|error| {
let gas_price_tolerance = gas_price_tolerance_str.parse::<u8>().map_err(|error| {
CliError::FailedToParseInt {
context: "payment_amount",
context: "gas_price_tolerance",
error,
}
})?;
let standard_payment = maybe_standard_payment_str
.parse::<bool>()
.map_err(|error| CliError::FailedToParseBool {
let payment_amount =
payment_amount_str
.parse::<u64>()
.map_err(|error| CliError::FailedToParseInt {
context: "payment_amount",
error,
})?;
let standard_payment = standard_payment_str.parse::<bool>().map_err(|error| {
CliError::FailedToParseBool {
context: "standard_payment",
error,
})?;
}
})?;
Ok(PricingMode::Classic {
payment_amount,
gas_price_tolerance,
standard_payment,
})
}
"fixed" => {
if maybe_gas_price_tolerance_str.is_empty() {
if gas_price_tolerance_str.is_empty() {
return Err(CliError::InvalidArgument {
context: "gas_price_tolerance",
error: "Gas price tolerance is required".to_string(),
});
}
let gas_price_tolerance =
maybe_gas_price_tolerance_str
let gas_price_tolerance = gas_price_tolerance_str.parse::<u8>().map_err(|error| {
CliError::FailedToParseInt {
context: "gas_price_tolerance",
error,
}
})?;

// Additional Computation Factor defaults to 0 if the string is empty
let additional_computation_factor = if additional_computation_factor_str.is_empty() {
u8::default()
} else {
additional_computation_factor_str
.parse::<u8>()
.map_err(|error| CliError::FailedToParseInt {
context: "gas_price_tolerance",
context: "additional_computation_factor",
error,
})?;
})?
};
Ok(PricingMode::Fixed {
gas_price_tolerance,
additional_computation_factor,
})
}
"reserved" => {
Expand Down Expand Up @@ -1689,32 +1702,63 @@ mod tests {
let pricing_mode_str = "fixed";
let payment_amount = "";
let gas_price_tolerance = "10";
let additional_computation_factor = "1";
let standard_payment = "";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
None,
)
.unwrap();
assert_eq!(
parsed,
PricingMode::Fixed {
additional_computation_factor: 1,
gas_price_tolerance: 10,
}
);
}

#[test]
fn should_parse_fixed_pricing_mode_identifier_without_additional_computation_factor() {
let pricing_mode_str = "fixed";
let payment_amount = "";
let gas_price_tolerance = "10";
let additional_computation_factor = "";
let standard_payment = "";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
None,
)
.unwrap();
assert_eq!(
parsed,
PricingMode::Fixed {
additional_computation_factor: 0,
gas_price_tolerance: 10,
}
);
}

#[test]
fn should_parse_reserved_pricing_mode() {
let pricing_mode_str = "reserved";
let payment_amount = "";
let gas_price_tolerance = "";
let additional_computation_factor = "0";
let standard_payment = "";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
Some(Digest::from_hex(VALID_HASH).unwrap()),
)
Expand All @@ -1732,10 +1776,12 @@ mod tests {
let payment_amount = "10";
let standard_payment = "true";
let gas_price_tolerance = "10";
let additional_computation_factor = "0";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
None,
)
Expand All @@ -1756,27 +1802,50 @@ mod tests {
let payment_amount = "10";
let standard_payment = "true";
let gas_price_tolerance = "10";
let additional_computation_factor = "0";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
None,
);
assert!(parsed.is_err());
assert!(matches!(parsed, Err(CliError::InvalidArgument { .. })));
}

#[test]
fn should_fail_to_parse_invalid_additional_computation_factor() {
let pricing_mode_str = "fixed";
let payment_amount = "10";
let standard_payment = "true";
let gas_price_tolerance = "10";
let additional_computation_factor = "invalid";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
None,
);
assert!(parsed.is_err());
assert!(matches!(parsed, Err(CliError::FailedToParseInt { .. })));
}

#[test]
fn should_fail_to_parse_classic_without_amount() {
let pricing_mode_str = "classic";
let payment_amount = "";
let standard_payment = "true";
let gas_price_tolerance = "10";
let additional_computation_factor = "0";
let parsed = pricing_mode(
pricing_mode_str,
payment_amount,
gas_price_tolerance,
additional_computation_factor,
standard_payment,
None,
);
Expand Down
Loading

0 comments on commit 99be816

Please sign in to comment.