From bb22e43258d1392f30c4192bd1fa73bf31c8bfff Mon Sep 17 00:00:00 2001 From: tommytrg Date: Fri, 3 May 2024 18:13:11 +0200 Subject: [PATCH] feat(staking): implement unstaking CLI method --- build.dockerfile | 13 ++++++++++ src/cli/node/json_rpc_client.rs | 45 ++++++++++++++++++++++++++++++--- src/cli/node/with_node.rs | 25 ++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 build.dockerfile diff --git a/build.dockerfile b/build.dockerfile new file mode 100644 index 000000000..a5cfc5145 --- /dev/null +++ b/build.dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu:22.04 + +# Install needed dependencies +RUN apt update && \ + apt upgrade -y && \ + apt install -y protobuf-compiler wget curl build-essential openssl libssl-dev pkg-config libclang-dev + +# Configure openssl +RUN pkg-config openssl + +# Install Rust +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y +RUN echo 'source $HOME/.cargo/env' >> $HOME/.bashrc \ No newline at end of file diff --git a/src/cli/node/json_rpc_client.rs b/src/cli/node/json_rpc_client.rs index f82db9ea3..38094b119 100644 --- a/src/cli/node/json_rpc_client.rs +++ b/src/cli/node/json_rpc_client.rs @@ -33,7 +33,9 @@ use witnet_data_structures::{ fee::Fee, get_environment, proto::ProtobufConvert, - transaction::{DRTransaction, StakeTransaction, Transaction, VTTransaction}, + transaction::{ + DRTransaction, StakeTransaction, Transaction, UnstakeTransaction, VTTransaction, + }, transaction_factory::NodeBalance, types::SequentialId, utxo_pool::{UtxoInfo, UtxoSelectionStrategy}, @@ -45,8 +47,9 @@ use witnet_node::actors::{ AddrType, GetBlockChainParams, GetTransactionOutput, PeersResult, QueryStakesArgument, }, messages::{ - AuthorizeStake, BuildDrt, BuildStakeParams, BuildStakeResponse, BuildVtt, GetBalanceTarget, - GetReputationResult, MagicEither, SignalingInfo, StakeAuthorization, + AuthorizeStake, BuildDrt, BuildStakeParams, BuildStakeResponse, BuildUnstakeParams, + BuildVtt, GetBalanceTarget, GetReputationResult, MagicEither, SignalingInfo, + StakeAuthorization, }, }; use witnet_rad::types::RadonTypes; @@ -1052,6 +1055,42 @@ pub fn authorize_st(addr: SocketAddr, withdrawer: Option) -> Result<(), Ok(()) } +#[allow(clippy::too_many_arguments)] +pub fn send_ut( + addr: SocketAddr, + value: u64, + operator: MagicEither, + dry_run: bool, +) -> Result<(), failure::Error> { + let mut stream = start_client(addr)?; + let mut id = SequentialId::initialize(1u8); + + let build_unstake_params = BuildUnstakeParams { + operator, + value, + dry_run, + }; + + // Finally ask the node to create the transaction. + let (_, (request, response)): (UnstakeTransaction, _) = issue_method( + "unstake", + Some(build_unstake_params), + &mut stream, + id.next(), + )?; + + // On dry run mode, print the request, otherwise, print the response. + // This is kept like this strictly for backwards compatibility. + // TODO: wouldn't it be better to always print the response or both? + if dry_run { + println!("{}", request); + } else { + println!("{}", response); + } + + Ok(()) +} + pub fn master_key_export( addr: SocketAddr, write_to_path: Option<&Path>, diff --git a/src/cli/node/with_node.rs b/src/cli/node/with_node.rs index 315c80aef..450543755 100644 --- a/src/cli/node/with_node.rs +++ b/src/cli/node/with_node.rs @@ -297,6 +297,17 @@ pub fn exec_cmd( validator, withdrawer, } => rpc::query_stakes(node.unwrap_or(default_jsonrpc), validator, withdrawer), + Command::Unstake { + node, + value, + operator, + dry_run, + } => rpc::send_ut( + node.unwrap_or(default_jsonrpc), + value, + MagicEither::Left(operator), + dry_run, + ), } } @@ -804,6 +815,20 @@ pub enum Command { #[structopt(short = "w", long = "withdrawer")] withdrawer: Option, }, + Unstake { + /// Socket address of the Witnet node to query + #[structopt(short = "n", long = "node")] + node: Option, + /// Value + #[structopt(long = "value")] + value: u64, + /// Node address operating the staked coins + #[structopt(long = "operator")] + operator: String, + /// Print the request that would be sent to the node and exit without doing anything + #[structopt(long = "dry-run")] + dry_run: bool, + }, } #[derive(Debug, StructOpt)]