From 55e41bb061d9b82ace73f79810b942fddb0a6f79 Mon Sep 17 00:00:00 2001 From: Jakub Zajkowski Date: Wed, 4 Dec 2024 15:48:16 +0100 Subject: [PATCH] Reflecting clients code to the fact that "arg_handling" module is now private to the node repository --- lib/cli.rs | 1 + lib/cli/arg_handling.rs | 236 ++++++++++++++++++++++++++++++ lib/cli/deploy.rs | 2 + lib/cli/transaction_v1_builder.rs | 5 +- 4 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 lib/cli/arg_handling.rs diff --git a/lib/cli.rs b/lib/cli.rs index c0b24b7..a19f54d 100644 --- a/lib/cli.rs +++ b/lib/cli.rs @@ -21,6 +21,7 @@ //! [`Block`] height or empty. If empty, the latest `Block` known on the server will be used. /// Functions for creating Deploys. +mod arg_handling; pub mod deploy; mod deploy_builder; mod deploy_str_params; diff --git a/lib/cli/arg_handling.rs b/lib/cli/arg_handling.rs new file mode 100644 index 0000000..ee205cd --- /dev/null +++ b/lib/cli/arg_handling.rs @@ -0,0 +1,236 @@ +use core::marker::PhantomData; + +use casper_types::system::auction::{Reservation, ARG_VALIDATOR}; +use casper_types::TransferTarget; +use casper_types::{bytesrepr::ToBytes, CLTyped, CLValueError, PublicKey, RuntimeArgs, URef, U512}; + +const TRANSFER_ARG_AMOUNT: RequiredArg = RequiredArg::new("amount"); + +const TRANSFER_ARG_SOURCE: OptionalArg = OptionalArg::new("source"); +const TRANSFER_ARG_TARGET: &str = "target"; + +const TRANSFER_ARG_ID: OptionalArg = OptionalArg::new("id"); + +const ADD_BID_ARG_PUBLIC_KEY: RequiredArg = RequiredArg::new("public_key"); +const ADD_BID_ARG_DELEGATION_RATE: RequiredArg = RequiredArg::new("delegation_rate"); +const ADD_BID_ARG_AMOUNT: RequiredArg = RequiredArg::new("amount"); + +const ADD_BID_ARG_MINIMUM_DELEGATION_AMOUNT: OptionalArg = + OptionalArg::new("minimum_delegation_amount"); + +const ADD_BID_ARG_MAXIMUM_DELEGATION_AMOUNT: OptionalArg = + OptionalArg::new("maximum_delegation_amount"); + +const ADD_BID_ARG_RESERVED_SLOTS: OptionalArg = OptionalArg::new("reserved_slots"); + +const WITHDRAW_BID_ARG_PUBLIC_KEY: RequiredArg = RequiredArg::new("public_key"); +const WITHDRAW_BID_ARG_AMOUNT: RequiredArg = RequiredArg::new("amount"); + +const DELEGATE_ARG_DELEGATOR: RequiredArg = RequiredArg::new("delegator"); +const DELEGATE_ARG_VALIDATOR: RequiredArg = RequiredArg::new("validator"); +const DELEGATE_ARG_AMOUNT: RequiredArg = RequiredArg::new("amount"); + +const UNDELEGATE_ARG_DELEGATOR: RequiredArg = RequiredArg::new("delegator"); +const UNDELEGATE_ARG_VALIDATOR: RequiredArg = RequiredArg::new("validator"); +const UNDELEGATE_ARG_AMOUNT: RequiredArg = RequiredArg::new("amount"); + +const REDELEGATE_ARG_DELEGATOR: RequiredArg = RequiredArg::new("delegator"); +const REDELEGATE_ARG_VALIDATOR: RequiredArg = RequiredArg::new("validator"); +const REDELEGATE_ARG_AMOUNT: RequiredArg = RequiredArg::new("amount"); +const REDELEGATE_ARG_NEW_VALIDATOR: RequiredArg = RequiredArg::new("new_validator"); + +const ACTIVATE_BID_ARG_VALIDATOR: RequiredArg = RequiredArg::new(ARG_VALIDATOR); +const CHANGE_BID_PUBLIC_KEY_ARG_PUBLIC_KEY: RequiredArg = RequiredArg::new("public_key"); +const CHANGE_BID_PUBLIC_KEY_ARG_NEW_PUBLIC_KEY: RequiredArg = + RequiredArg::new("new_public_key"); +const ADD_RESERVATIONS_ARG_RESERVATIONS: RequiredArg> = + RequiredArg::new("reservations"); + +const CANCEL_RESERVATIONS_ARG_VALIDATOR: RequiredArg = RequiredArg::new("validator"); +const CANCEL_RESERVATIONS_ARG_DELEGATORS: RequiredArg> = + RequiredArg::new("delegators"); + +struct RequiredArg { + name: &'static str, + _phantom: PhantomData, +} + +impl RequiredArg { + const fn new(name: &'static str) -> Self { + Self { + name, + _phantom: PhantomData, + } + } + + fn insert(&self, args: &mut RuntimeArgs, value: T) -> Result<(), CLValueError> + where + T: CLTyped + ToBytes, + { + args.insert(self.name, value) + } +} + +struct OptionalArg { + name: &'static str, + _phantom: PhantomData, +} + +impl OptionalArg { + const fn new(name: &'static str) -> Self { + Self { + name, + _phantom: PhantomData, + } + } + + fn insert(&self, args: &mut RuntimeArgs, value: T) -> Result<(), CLValueError> + where + T: CLTyped + ToBytes, + { + args.insert(self.name, Some(value)) + } +} + +/// Creates a `RuntimeArgs` suitable for use in a transfer transaction. + +pub(crate) fn new_transfer_args, T: Into>( + amount: A, + maybe_source: Option, + target: T, + maybe_id: Option, +) -> Result { + let mut args = RuntimeArgs::new(); + if let Some(source) = maybe_source { + TRANSFER_ARG_SOURCE.insert(&mut args, source)?; + } + match target.into() { + TransferTarget::PublicKey(public_key) => args.insert(TRANSFER_ARG_TARGET, public_key)?, + TransferTarget::AccountHash(account_hash) => { + args.insert(TRANSFER_ARG_TARGET, account_hash)? + } + TransferTarget::URef(uref) => args.insert(TRANSFER_ARG_TARGET, uref)?, + } + TRANSFER_ARG_AMOUNT.insert(&mut args, amount.into())?; + if let Some(id) = maybe_id { + TRANSFER_ARG_ID.insert(&mut args, id)?; + } + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a activate bid transaction. +pub fn new_activate_bid_args(validator: PublicKey) -> Result { + let mut args = RuntimeArgs::new(); + ACTIVATE_BID_ARG_VALIDATOR.insert(&mut args, validator)?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a change bid public key transaction. +pub fn new_change_bid_public_key_args( + public_key: PublicKey, + new_public_key: PublicKey, +) -> Result { + let mut args = RuntimeArgs::new(); + CHANGE_BID_PUBLIC_KEY_ARG_PUBLIC_KEY.insert(&mut args, public_key)?; + CHANGE_BID_PUBLIC_KEY_ARG_NEW_PUBLIC_KEY.insert(&mut args, new_public_key)?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a add resrvations transaction. +pub fn new_add_reservations_args( + reservations: Vec, +) -> Result { + let mut args = RuntimeArgs::new(); + ADD_RESERVATIONS_ARG_RESERVATIONS.insert(&mut args, reservations)?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a cancel reservations transaction. +pub fn new_cancel_reservations_args( + validator: PublicKey, + delegators: Vec, +) -> Result { + let mut args = RuntimeArgs::new(); + CANCEL_RESERVATIONS_ARG_VALIDATOR.insert(&mut args, validator)?; + CANCEL_RESERVATIONS_ARG_DELEGATORS.insert(&mut args, delegators)?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in an add_bid transaction. + +pub(crate) fn new_add_bid_args>( + public_key: PublicKey, + delegation_rate: u8, + amount: A, + maybe_minimum_delegation_amount: Option, + maybe_maximum_delegation_amount: Option, + maybe_reserved_slots: Option, +) -> Result { + let mut args = RuntimeArgs::new(); + ADD_BID_ARG_PUBLIC_KEY.insert(&mut args, public_key)?; + ADD_BID_ARG_DELEGATION_RATE.insert(&mut args, delegation_rate)?; + ADD_BID_ARG_AMOUNT.insert(&mut args, amount.into())?; + if let Some(minimum_delegation_amount) = maybe_minimum_delegation_amount { + ADD_BID_ARG_MINIMUM_DELEGATION_AMOUNT.insert(&mut args, minimum_delegation_amount)?; + }; + if let Some(maximum_delegation_amount) = maybe_maximum_delegation_amount { + ADD_BID_ARG_MAXIMUM_DELEGATION_AMOUNT.insert(&mut args, maximum_delegation_amount)?; + }; + if let Some(reserved_slots) = maybe_reserved_slots { + ADD_BID_ARG_RESERVED_SLOTS.insert(&mut args, reserved_slots)?; + }; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a withdraw_bid transaction. +pub fn new_withdraw_bid_args>( + public_key: PublicKey, + amount: A, +) -> Result { + let mut args = RuntimeArgs::new(); + WITHDRAW_BID_ARG_PUBLIC_KEY.insert(&mut args, public_key)?; + WITHDRAW_BID_ARG_AMOUNT.insert(&mut args, amount.into())?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a delegate transaction. +pub(crate) fn new_delegate_args>( + delegator: PublicKey, + validator: PublicKey, + amount: A, +) -> Result { + let mut args = RuntimeArgs::new(); + DELEGATE_ARG_DELEGATOR.insert(&mut args, delegator)?; + DELEGATE_ARG_VALIDATOR.insert(&mut args, validator)?; + DELEGATE_ARG_AMOUNT.insert(&mut args, amount.into())?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in an undelegate transaction. + +pub(crate) fn new_undelegate_args>( + delegator: PublicKey, + validator: PublicKey, + amount: A, +) -> Result { + let mut args = RuntimeArgs::new(); + UNDELEGATE_ARG_DELEGATOR.insert(&mut args, delegator)?; + UNDELEGATE_ARG_VALIDATOR.insert(&mut args, validator)?; + UNDELEGATE_ARG_AMOUNT.insert(&mut args, amount.into())?; + Ok(args) +} + +/// Creates a `RuntimeArgs` suitable for use in a redelegate transaction. +pub(crate) fn new_redelegate_args>( + delegator: PublicKey, + validator: PublicKey, + amount: A, + new_validator: PublicKey, +) -> Result { + let mut args = RuntimeArgs::new(); + REDELEGATE_ARG_DELEGATOR.insert(&mut args, delegator)?; + REDELEGATE_ARG_VALIDATOR.insert(&mut args, validator)?; + REDELEGATE_ARG_AMOUNT.insert(&mut args, amount.into())?; + REDELEGATE_ARG_NEW_VALIDATOR.insert(&mut args, new_validator)?; + Ok(args) +} diff --git a/lib/cli/deploy.rs b/lib/cli/deploy.rs index 2745c7b..9e3c0b6 100644 --- a/lib/cli/deploy.rs +++ b/lib/cli/deploy.rs @@ -1,3 +1,5 @@ +//! Functions facilitating sending of [`Deploy`]s to the network + use casper_types::{ account::AccountHash, AsymmetricType, Deploy, PublicKey, TransferTarget, UIntParseError, URef, U512, diff --git a/lib/cli/transaction_v1_builder.rs b/lib/cli/transaction_v1_builder.rs index 2733646..ecb3861 100644 --- a/lib/cli/transaction_v1_builder.rs +++ b/lib/cli/transaction_v1_builder.rs @@ -1,12 +1,13 @@ pub mod error; +use super::arg_handling; use crate::{ cli::{FieldsContainer, FieldsContainerError}, types::InitiatorAddrAndSecretKey, }; +use alloc::collections::BTreeMap; use alloc::collections::BTreeSet; use alloc::vec::Vec; use casper_types::{ - arg_handling, bytesrepr::{Bytes, ToBytes}, system::auction::Reservation, AddressableEntityHash, CLValueError, Digest, EntityVersion, InitiatorAddr, PackageHash, @@ -15,8 +16,6 @@ use casper_types::{ TransactionTarget, TransactionV1, TransactionV1Payload, TransferTarget, URef, U512, }; use core::marker::PhantomData; - -use alloc::collections::BTreeMap; pub use error::TransactionV1BuilderError; /// A builder for constructing `TransactionV1` instances with various configuration options.