From 9e6f79c291638def06f201f641ed5cfd6c6c2aa4 Mon Sep 17 00:00:00 2001 From: Jakub Zajkowski Date: Tue, 10 Dec 2024 23:21:10 +0100 Subject: [PATCH] Aligning client code with recent node changes --- lib/cli/tests.rs | 47 ++++++++-------- lib/cli/transaction.rs | 23 ++------ lib/cli/transaction_builder_params.rs | 30 +++------- lib/cli/transaction_v1_builder.rs | 40 +++++--------- src/transaction/creation_common.rs | 80 +++++++++------------------ 5 files changed, 75 insertions(+), 145 deletions(-) diff --git a/lib/cli/tests.rs b/lib/cli/tests.rs index f5187ea..93f6575 100644 --- a/lib/cli/tests.rs +++ b/lib/cli/tests.rs @@ -448,8 +448,8 @@ mod transaction { use crate::{cli::TransactionV1BuilderError, Error::TransactionBuild}; use casper_types::{ bytesrepr::Bytes, system::auction::Reservation, PackageAddr, TransactionArgs, - TransactionEntryPoint, TransactionInvocationTarget, TransactionRuntime, TransactionTarget, - TransferTarget, + TransactionEntryPoint, TransactionInvocationTarget, TransactionRuntimeParams, + TransactionTarget, TransferTarget, }; use once_cell::sync::Lazy; use rand::{thread_rng, Rng}; @@ -1156,10 +1156,10 @@ mod transaction { let entity_addr: EntityAddr = EntityAddr::new_account([0u8; 32]); let entity_hash = entity_addr.value(); let entry_point = String::from("test-entry-point"); + let params = TransactionRuntimeParams::VmCasperV1; let target = &TransactionTarget::Stored { id: TransactionInvocationTarget::ByHash(entity_hash), - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let entry_point_ref = &TransactionEntryPoint::Custom(entry_point); @@ -1184,11 +1184,11 @@ mod transaction { chunked_args: None, }; + let params = TransactionRuntimeParams::VmCasperV1; let transaction_builder_params = TransactionBuilderParams::InvocableEntity { entity_hash: entity_hash.into(), entry_point: "test-entry-point", - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction = create_transaction(transaction_builder_params, transaction_string_params, true); @@ -1212,10 +1212,10 @@ mod transaction { #[test] fn should_create_invocable_entity_alias_transaction() { let alias = String::from("alias"); + let params = TransactionRuntimeParams::VmCasperV1; let target = &TransactionTarget::Stored { id: TransactionInvocationTarget::ByName(alias), - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction_string_params = TransactionStrParams { secret_key: "", @@ -1237,11 +1237,11 @@ mod transaction { chunked_args: None, }; + let params = TransactionRuntimeParams::VmCasperV1; let transaction_builder_params = TransactionBuilderParams::InvocableEntityAlias { entity_alias: "alias", entry_point: "entry-point-alias", - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction = create_transaction(transaction_builder_params, transaction_string_params, true); @@ -1267,13 +1267,13 @@ mod transaction { let package_addr: PackageAddr = vec![0u8; 32].as_slice().try_into().unwrap(); let entry_point = "test-entry-point-package"; let maybe_entity_version = Some(23); + let params = TransactionRuntimeParams::VmCasperV1; let target = &TransactionTarget::Stored { id: TransactionInvocationTarget::ByPackageHash { addr: package_addr, version: maybe_entity_version, }, - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction_string_params = TransactionStrParams { secret_key: "", @@ -1294,13 +1294,12 @@ mod transaction { session_entry_point: None, chunked_args: None, }; - + let params = TransactionRuntimeParams::VmCasperV1; let transaction_builder_params = TransactionBuilderParams::Package { package_hash: package_addr.into(), entry_point, maybe_entity_version, - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction = create_transaction(transaction_builder_params, transaction_string_params, true); @@ -1326,13 +1325,13 @@ mod transaction { let package_name = String::from("package-name"); let entry_point = "test-entry-point-package"; let maybe_entity_version = Some(23); + let params = TransactionRuntimeParams::VmCasperV1; let target = &TransactionTarget::Stored { id: TransactionInvocationTarget::ByPackageName { name: package_name.clone(), version: maybe_entity_version, }, - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction_string_params = TransactionStrParams { secret_key: "", @@ -1354,12 +1353,12 @@ mod transaction { chunked_args: None, }; + let params = TransactionRuntimeParams::VmCasperV1; let transaction_builder_params = TransactionBuilderParams::PackageAlias { package_alias: &package_name, entry_point, maybe_entity_version, - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, + runtime: params, }; let transaction = create_transaction(transaction_builder_params, transaction_string_params, true); @@ -1383,12 +1382,11 @@ mod transaction { fn should_create_session_transaction() { let transaction_bytes = Bytes::from(vec![1u8; 32]); let is_install_upgrade = true; + let params = TransactionRuntimeParams::VmCasperV1; let target = &TransactionTarget::Session { is_install_upgrade, - runtime: TransactionRuntime::VmCasperV1, + runtime: params, module_bytes: transaction_bytes.clone(), - transferred_value: 0, - seed: None, }; let transaction_string_params = TransactionStrParams { secret_key: "", @@ -1410,12 +1408,11 @@ mod transaction { chunked_args: None, }; + let params = TransactionRuntimeParams::VmCasperV1; let transaction_builder_params = TransactionBuilderParams::Session { is_install_upgrade, transaction_bytes, - runtime: TransactionRuntime::VmCasperV1, - transferred_value: 0, - seed: None, + runtime: params, }; let transaction = create_transaction(transaction_builder_params, transaction_string_params, true); diff --git a/lib/cli/transaction.rs b/lib/cli/transaction.rs index 096fbf6..5a413c0 100644 --- a/lib/cli/transaction.rs +++ b/lib/cli/transaction.rs @@ -12,7 +12,7 @@ use crate::{ }; use casper_types::{ Digest, InitiatorAddr, SecretKey, Transaction, TransactionArgs, TransactionEntryPoint, - TransactionRuntime, + TransactionRuntimeParams, }; pub fn create_transaction( @@ -32,7 +32,7 @@ pub fn create_transaction( let ttl = parse::ttl(transaction_params.ttl)?; let maybe_session_account = parse::session_account(&transaction_params.initiator_addr)?; - let is_v2_wasm = matches!(&builder_params, TransactionBuilderParams::Session { runtime, .. } if runtime == &TransactionRuntime::VmCasperV2); + let is_v2_wasm = matches!(&builder_params, TransactionBuilderParams::Session { runtime, .. } if matches!(runtime, &TransactionRuntimeParams::VmCasperV2 { .. })); let mut transaction_builder = make_transaction_builder(builder_params)?; @@ -272,13 +272,11 @@ pub fn make_transaction_builder( entity_hash, entry_point, runtime, - transferred_value, } => { let transaction_builder = TransactionV1Builder::new_targeting_invocable_entity( entity_hash, entry_point, runtime, - transferred_value, ); Ok(transaction_builder) } @@ -286,14 +284,12 @@ pub fn make_transaction_builder( entity_alias, entry_point, runtime, - transferred_value, } => { let transaction_builder = TransactionV1Builder::new_targeting_invocable_entity_via_alias( entity_alias, entry_point, runtime, - transferred_value, ); Ok(transaction_builder) } @@ -302,14 +298,12 @@ pub fn make_transaction_builder( maybe_entity_version, entry_point, runtime, - transferred_value, } => { let transaction_builder = TransactionV1Builder::new_targeting_package( package_hash, maybe_entity_version, entry_point, runtime, - transferred_value, ); Ok(transaction_builder) } @@ -318,7 +312,6 @@ pub fn make_transaction_builder( maybe_entity_version, entry_point, runtime, - transferred_value, } => { let new_targeting_package_via_alias = TransactionV1Builder::new_targeting_package_via_alias( @@ -326,7 +319,6 @@ pub fn make_transaction_builder( maybe_entity_version, entry_point, runtime, - transferred_value, ); let transaction_builder = new_targeting_package_via_alias; Ok(transaction_builder) @@ -335,16 +327,9 @@ pub fn make_transaction_builder( is_install_upgrade, transaction_bytes, runtime, - transferred_value, - seed, } => { - let transaction_builder = TransactionV1Builder::new_session( - is_install_upgrade, - transaction_bytes, - runtime, - transferred_value, - seed, - ); + let transaction_builder = + TransactionV1Builder::new_session(is_install_upgrade, transaction_bytes, runtime); Ok(transaction_builder) } TransactionBuilderParams::Transfer { diff --git a/lib/cli/transaction_builder_params.rs b/lib/cli/transaction_builder_params.rs index a9582b5..9c3aca3 100644 --- a/lib/cli/transaction_builder_params.rs +++ b/lib/cli/transaction_builder_params.rs @@ -1,6 +1,6 @@ use casper_types::{ bytesrepr::Bytes, system::auction::Reservation, AddressableEntityHash, PackageHash, PublicKey, - TransactionRuntime, TransferTarget, URef, U512, + TransactionRuntimeParams, TransferTarget, URef, U512, }; /// An enum representing the parameters needed to construct a transaction builder @@ -77,10 +77,8 @@ pub enum TransactionBuilderParams<'a> { entity_hash: AddressableEntityHash, /// The entry point for the invocable entity transaction entry_point: &'a str, - /// Transaction Runtime. - runtime: TransactionRuntime, - /// The amount to be transferred in the invocable entity transaction. - transferred_value: u64, + /// Transaction Runtime params. + runtime: TransactionRuntimeParams, }, /// Parameters for the invocable entity alias variant of the transaction builder InvocableEntityAlias { @@ -88,10 +86,8 @@ pub enum TransactionBuilderParams<'a> { entity_alias: &'a str, /// The entry_point for the invocable entity alias transaction entry_point: &'a str, - /// Transaction Runtime. - runtime: TransactionRuntime, - /// Transferred value. - transferred_value: u64, + /// Transaction Runtime params. + runtime: TransactionRuntimeParams, }, /// Parameters for the package variant of the transaction builder Package { @@ -102,9 +98,7 @@ pub enum TransactionBuilderParams<'a> { /// The entry_point for the package transaction entry_point: &'a str, /// Transaction Runtime. - runtime: TransactionRuntime, - /// Transferred value. - transferred_value: u64, + runtime: TransactionRuntimeParams, }, /// Parameters for the package alias variant of the transaction builder PackageAlias { @@ -114,10 +108,8 @@ pub enum TransactionBuilderParams<'a> { maybe_entity_version: Option, /// The entry point for the package alias transaction entry_point: &'a str, - /// Transaction Runtime. - runtime: TransactionRuntime, - /// Transferred value. - transferred_value: u64, + /// Transaction Runtime params. + runtime: TransactionRuntimeParams, }, /// Parameters for the session variant of the transaction builder Session { @@ -126,11 +118,7 @@ pub enum TransactionBuilderParams<'a> { /// The Bytes to be run by the execution engine for the session transaction transaction_bytes: Bytes, /// Transaction Runtime. - runtime: TransactionRuntime, - /// Transferred value. - transferred_value: u64, - /// The optional seed for the session transaction - seed: Option<[u8; 32]>, + runtime: TransactionRuntimeParams, }, /// Parameters for the transfer variant of the transaction builder Transfer { diff --git a/lib/cli/transaction_v1_builder.rs b/lib/cli/transaction_v1_builder.rs index ecb3861..a3831eb 100644 --- a/lib/cli/transaction_v1_builder.rs +++ b/lib/cli/transaction_v1_builder.rs @@ -12,8 +12,9 @@ use casper_types::{ system::auction::Reservation, AddressableEntityHash, CLValueError, Digest, EntityVersion, InitiatorAddr, PackageHash, PricingMode, PublicKey, RuntimeArgs, SecretKey, TimeDiff, Timestamp, TransactionArgs, - TransactionEntryPoint, TransactionInvocationTarget, TransactionRuntime, TransactionScheduling, - TransactionTarget, TransactionV1, TransactionV1Payload, TransferTarget, URef, U512, + TransactionEntryPoint, TransactionInvocationTarget, TransactionRuntimeParams, + TransactionScheduling, TransactionTarget, TransactionV1, TransactionV1Payload, TransferTarget, + URef, U512, }; use core::marker::PhantomData; pub use error::TransactionV1BuilderError; @@ -316,14 +317,9 @@ impl<'a> TransactionV1Builder<'a> { fn new_targeting_stored>( id: TransactionInvocationTarget, entry_point: E, - runtime: TransactionRuntime, - transferred_value: u64, + runtime: TransactionRuntimeParams, ) -> Self { - let target = TransactionTarget::Stored { - id, - runtime, - transferred_value, - }; + let target = TransactionTarget::Stored { id, runtime }; let mut builder = TransactionV1Builder::new(); builder.args = TransactionArgs::Named(RuntimeArgs::new()); builder.target = target; @@ -337,11 +333,10 @@ impl<'a> TransactionV1Builder<'a> { pub fn new_targeting_invocable_entity>( hash: AddressableEntityHash, entry_point: E, - runtime: TransactionRuntime, - transferred_value: u64, + runtime: TransactionRuntimeParams, ) -> Self { let id = TransactionInvocationTarget::new_invocable_entity(hash); - Self::new_targeting_stored(id, entry_point, runtime, transferred_value) + Self::new_targeting_stored(id, entry_point, runtime) } /// Returns a new `TransactionV1Builder` suitable for building a transaction targeting a stored @@ -349,11 +344,10 @@ impl<'a> TransactionV1Builder<'a> { pub fn new_targeting_invocable_entity_via_alias, E: Into>( alias: A, entry_point: E, - runtime: TransactionRuntime, - transferred_value: u64, + runtime: TransactionRuntimeParams, ) -> Self { let id = TransactionInvocationTarget::new_invocable_entity_alias(alias.into()); - Self::new_targeting_stored(id, entry_point, runtime, transferred_value) + Self::new_targeting_stored(id, entry_point, runtime) } /// Returns a new `TransactionV1Builder` suitable for building a transaction targeting a @@ -362,11 +356,10 @@ impl<'a> TransactionV1Builder<'a> { hash: PackageHash, version: Option, entry_point: E, - runtime: TransactionRuntime, - transferred_value: u64, + runtime: TransactionRuntimeParams, ) -> Self { let id = TransactionInvocationTarget::new_package(hash, version); - Self::new_targeting_stored(id, entry_point, runtime, transferred_value) + Self::new_targeting_stored(id, entry_point, runtime) } /// Returns a new `TransactionV1Builder` suitable for building a transaction targeting a @@ -375,11 +368,10 @@ impl<'a> TransactionV1Builder<'a> { alias: A, version: Option, entry_point: E, - runtime: TransactionRuntime, - transferred_value: u64, + runtime: TransactionRuntimeParams, ) -> Self { let id = TransactionInvocationTarget::new_package_alias(alias.into(), version); - Self::new_targeting_stored(id, entry_point, runtime, transferred_value) + Self::new_targeting_stored(id, entry_point, runtime) } /// Returns a new `TransactionV1Builder` suitable for building a transaction for running session @@ -387,16 +379,12 @@ impl<'a> TransactionV1Builder<'a> { pub fn new_session( is_install_upgrade: bool, module_bytes: Bytes, - runtime: TransactionRuntime, - transferred_value: u64, - seed: Option<[u8; 32]>, + runtime: TransactionRuntimeParams, ) -> Self { let target = TransactionTarget::Session { is_install_upgrade, module_bytes, runtime, - transferred_value, - seed, }; let mut builder = TransactionV1Builder::new(); builder.args = TransactionArgs::Named(RuntimeArgs::new()); diff --git a/src/transaction/creation_common.rs b/src/transaction/creation_common.rs index 31351dc..e53296e 100644 --- a/src/transaction/creation_common.rs +++ b/src/transaction/creation_common.rs @@ -8,7 +8,8 @@ use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command}; use casper_client::cli::{ json_args_help, simple_args_help, CliError, TransactionBuilderParams, TransactionStrParams, }; -use casper_types::TransactionRuntime; +use casper_types::TransactionRuntimeParams; +use transaction_runtime::TransactionRuntime; use crate::common; @@ -608,11 +609,11 @@ pub(super) mod transaction_runtime { VmCasperV2, } - impl From for casper_types::TransactionRuntime { + impl From for casper_types::ContractRuntimeTag { fn from(runtime: TransactionRuntime) -> Self { match runtime { - TransactionRuntime::VmCasperV1 => casper_types::TransactionRuntime::VmCasperV1, - TransactionRuntime::VmCasperV2 => casper_types::TransactionRuntime::VmCasperV2, + TransactionRuntime::VmCasperV1 => casper_types::ContractRuntimeTag::VmCasperV1, + TransactionRuntime::VmCasperV2 => casper_types::ContractRuntimeTag::VmCasperV2, } } } @@ -1986,18 +1987,13 @@ pub(super) mod invocable_entity { let entity_addr_str = entity_addr::get(matches)?; let entity_addr = entity_addr::parse_entity_addr(entity_addr_str)?; - let runtime = transaction_runtime::get(matches); let entry_point = session_entry_point::get(matches).unwrap_or_default(); - let maybe_transferred_value = transferred_value::get(matches)?; - - let runtime = runtime.cloned().unwrap_or_default().into(); - let transferred_value = map_maybe_transferred(maybe_transferred_value, &runtime)?; + let runtime = get_transaction_runtime(matches)?; let params = TransactionBuilderParams::InvocableEntity { entity_hash: entity_addr.into(), // TODO: Skip `entity_addr` and match directly for hash? entry_point, runtime, - transferred_value, }; let transaction_str_params = build_transaction_str_params(matches, ACCEPT_SESSION_ARGS); Ok((params, transaction_str_params)) @@ -2044,17 +2040,11 @@ pub(super) mod invocable_entity_alias { let entity_alias = entity_alias_arg::get(matches); let entry_point = session_entry_point::get(matches).unwrap_or_default(); - let runtime = transaction_runtime::get(matches) - .cloned() - .unwrap_or_default() - .into(); - let maybe_transferred_value = transferred_value::get(matches)?; - let transferred_value = map_maybe_transferred(maybe_transferred_value, &runtime)?; + let runtime = get_transaction_runtime(matches)?; let params = TransactionBuilderParams::InvocableEntityAlias { entity_alias, entry_point, runtime, - transferred_value, }; let transaction_str_params = build_transaction_str_params(matches, ACCEPT_SESSION_ARGS); Ok((params, transaction_str_params)) @@ -2099,20 +2089,14 @@ pub(super) mod package { let maybe_package_addr_str = package_addr::get(matches); let package_addr = package_addr::parse_package_addr(maybe_package_addr_str)?; let maybe_entity_version = session_version::get(matches); - let runtime = transaction_runtime::get(matches) - .cloned() - .unwrap_or_default() - .into(); + let runtime = get_transaction_runtime(matches)?; let entry_point = session_entry_point::get(matches).unwrap_or_default(); - let maybe_transferred_value = transferred_value::get(matches)?; - let transferred_value = map_maybe_transferred(maybe_transferred_value, &runtime)?; let params = TransactionBuilderParams::Package { package_hash: package_addr.into(), // TODO: Skip `package_addr` and match directly for hash? maybe_entity_version, entry_point, runtime, - transferred_value, }; let transaction_str_params = build_transaction_str_params(matches, ACCEPT_SESSION_ARGS); Ok((params, transaction_str_params)) @@ -2160,19 +2144,13 @@ pub(super) mod package_alias { let maybe_entity_version = session_version::get(matches); let entry_point = session_entry_point::get(matches).unwrap_or_default(); - let runtime = transaction_runtime::get(matches) - .cloned() - .unwrap_or_default() - .into(); - let maybe_transferred_value = transferred_value::get(matches)?; - let transferred_value = map_maybe_transferred(maybe_transferred_value, &runtime)?; + let runtime = get_transaction_runtime(matches)?; let params = TransactionBuilderParams::PackageAlias { package_alias, maybe_entity_version, entry_point, runtime, - transferred_value, }; let transaction_str_params = build_transaction_str_params(matches, ACCEPT_SESSION_ARGS); Ok((params, transaction_str_params)) @@ -2229,21 +2207,12 @@ pub(super) mod session { parse::transaction_module_bytes(transaction_path_str.unwrap_or_default())?; let is_install_upgrade: bool = is_install_upgrade::get(matches); - let runtime = transaction_runtime::get(matches) - .cloned() - .unwrap_or_default() - .into(); - - let maybe_transferred_value = transferred_value::get(matches)?; - let transferred_value = map_maybe_transferred(maybe_transferred_value, &runtime)?; - let seed = None; // TODO: support seeds + let runtime = get_transaction_runtime(matches)?; let params = TransactionBuilderParams::Session { is_install_upgrade, transaction_bytes, runtime, - transferred_value, - seed, }; let transaction_str_params = build_transaction_str_params(matches, ACCEPT_SESSION_ARGS); Ok((params, transaction_str_params)) @@ -2526,32 +2495,35 @@ pub(super) fn parse_rpc_args_and_run( )) } -fn map_maybe_transferred( - maybe_transferred_value: Option, - runtime: &TransactionRuntime, -) -> Result { - match runtime { +fn get_transaction_runtime(matches: &ArgMatches) -> Result { + let runtime_tag = transaction_runtime::get(matches) + .cloned() + .unwrap_or_default(); + let maybe_transferred_value = transferred_value::get(matches)?; + let runtime = match runtime_tag { TransactionRuntime::VmCasperV1 => { if maybe_transferred_value.is_some() { Err(CliError::InvalidArgument { context: "transferred_value", error: "Argument `transferred-value` has no usage in V1 execution engine VM (if you provided no execution engine vm parameter, V1 is the default)".to_string(), - }) - } else { - Ok(0) + })?; } + TransactionRuntimeParams::VmCasperV1 } TransactionRuntime::VmCasperV2 => { - if let Some(transferred_value) = maybe_transferred_value { - Ok(transferred_value) - } else { + if maybe_transferred_value.is_none() { Err(CliError::InvalidArgument { context: "transferred_value", error: "VmCasperV2 requires `transferred-value` argument".to_string(), - }) + })?; + } + TransactionRuntimeParams::VmCasperV2 { + transferred_value: maybe_transferred_value.unwrap(), + seed: None, } } - } + }; + Ok(runtime) } #[cfg(test)]