Skip to content
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

wip: redelegate batch #24

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/checks/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::str::FromStr;

use async_trait::async_trait;

use namada_sdk::rpc;
use namada_sdk::token::Amount;
use namada_sdk::{rpc};
use serde::{Deserialize, Serialize};

use crate::entity::address::{AccountIndentifier, ADDRESS_PREFIX};
Expand Down
2 changes: 1 addition & 1 deletion src/checks/bonds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;

use namada_sdk::{rpc};
use namada_sdk::rpc;
use serde::{Deserialize, Serialize};

use crate::entity::address::{AccountIndentifier, ADDRESS_PREFIX};
Expand Down
6 changes: 4 additions & 2 deletions src/gen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct Args {
withdraw: u64,
#[arg(long, default_value_t = 0)]
vote_proposal: u64,
#[arg(long, default_value_t = 4)]
#[arg(long, default_value_t = 0)]
redelegate: u64,
#[arg(long, default_value_t = 0)]
init_default_proposal: u64,
Expand All @@ -110,6 +110,8 @@ struct Args {
transparent_transfer_batch: u64,
#[arg(long, default_value_t = 6)]
bond_batch: u64,
#[arg(long, default_value_t = 6)]
redelegate_batch: u64,
}

fn main() {
Expand Down Expand Up @@ -165,6 +167,7 @@ fn main() {
args.transparent_transfer_batch.into(),
),
(TaskType::BondBatch, args.bond_batch.into()),
(TaskType::RedelegateBatch, args.redelegate_batch.into()),
]);

let mut scenario_builder = ScenarioBuilder::new(
Expand All @@ -190,7 +193,6 @@ fn main() {

#[cfg(test)]
mod test {


use super::*;

Expand Down
10 changes: 5 additions & 5 deletions src/gen/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl State {
if alias.to_string().ends_with("-pa") {
return acc;
}
if blacklist.contains(&alias) {
if blacklist.contains(alias) {
return acc;
}
if let Some(balance) = token_balances.get(&Alias::native_token()) {
Expand Down Expand Up @@ -414,9 +414,9 @@ impl State {
return acc;
}
if alias.to_string().contains("-enst") {
return acc
return acc;
}
if blacklist.contains(&alias) {
if blacklist.contains(alias) {
return acc;
}
if let Some(balance) = token_balances.get(&Alias::native_token()) {
Expand All @@ -436,8 +436,8 @@ impl State {
amount: u64,
blacklist: Vec<Alias>,
) -> Account {
let all_implicit_addresses_with_native_token_balance =
self.implicit_addresses_with_at_least_native_token_balance_and_blacklist(amount, blacklist);
let all_implicit_addresses_with_native_token_balance = self
.implicit_addresses_with_at_least_native_token_balance_and_blacklist(amount, blacklist);
all_implicit_addresses_with_native_token_balance
.choose(&mut rand::thread_rng())
.unwrap()
Expand Down
41 changes: 40 additions & 1 deletion src/gen/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{
init_default_proposal::InitDefaultProposalBuilder,
init_funding_proposal::InitPgfFundingProposalBuilder,
init_steward_proposal::InitPgfStewardProposalBuilder, new_wallet_key::NewWalletStepBuilder,
redelegate::RedelegateBuilder, shielding_transfer::ShieldingTransferBuilder,
redelegate::RedelegateBuilder, redelegate_batch::RedelegateBatchBuilder,
shielding_transfer::ShieldingTransferBuilder,
transparent_transfer::TransparentTransferBuilder,
transparent_transfer_batch::TransparentTransferBatchBuilder, unbond::UnbondBuilder,
unshielding_transfer::UnshieldingTransferBuilder, update_account::UpdateAccountBuilder,
Expand Down Expand Up @@ -53,6 +54,7 @@ pub enum TaskType {
ClaimRewards,
TransparentTransferBatch,
BondBatch,
RedelegateBatch,
}

impl TaskType {
Expand Down Expand Up @@ -187,6 +189,13 @@ impl TaskType {
.len()
> 2
}
TaskType::RedelegateBatch => {
!state.any_bond().is_empty()
&& !state.any_active_validator_address().is_empty()
&& !state
.implicit_addresses_with_at_least_native_token_balance(MIN_FEE * 6)
.is_empty()
}
}
}

Expand Down Expand Up @@ -834,6 +843,36 @@ impl TaskType {

Box::new(step)
}
TaskType::RedelegateBatch => {
let total_batched_txs = utils::random_between(2, 4);

let bond = state.random_bond();
let account = state.get_account_from_alias(&bond.source);

let gas_payer = state
.random_implicit_account_with_at_least_native_token_balance(MIN_FEE * 6)
.alias;

let tx_settings = TxSettings {
signers: account.implicit_addresses,
broadcast_only: false,
gas_limit: MIN_FEE * 6,
gas_payer,
};

let step = RedelegateBatchBuilder::default()
.sources(vec![bond.source; total_batched_txs as usize])
.source_validators(vec![bond.step_id; total_batched_txs as usize])
.amounts(vec![
1;
total_batched_txs as usize
])
.tx_settings(tx_settings)
.build()
.unwrap();

Box::new(step)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/gen/steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod init_funding_proposal;
pub mod init_steward_proposal;
pub mod new_wallet_key;
pub mod redelegate;
pub mod redelegate_batch;
pub mod shielding_transfer;
pub mod transparent_transfer;
pub mod transparent_transfer_batch;
Expand Down
2 changes: 1 addition & 1 deletion src/gen/steps/redelegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Step for Redelegate {
source: Value::v(self.source.to_string()),
src_validator: Value::r(self.source_validator, "validator-0-address".to_string()),
dest_validator: Value::f(Some(step_index - 1)),
amount: Value::r(self.source_validator, "amount-0".to_string()),
amount: Value::v(self.amount.to_string()), // amount: Value::r(self.source_validator, "amount-0".to_string()),
},
settings: Some(self.tx_settings.clone().into()),
}
Expand Down
85 changes: 85 additions & 0 deletions src/gen/steps/redelegate_batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::fmt::Display;

use derive_builder::Builder;
use namada_scenario_tester::{
scenario::StepType, tasks::redelegate_batch::TxRedelegateBatchParametersDto,
utils::value::Value,
};

use crate::{
entity::{Alias, TxSettings},
hooks::{check_step::CheckStep, query_validators::QueryValidatorSet},
state::State,
step::Step,
};

#[derive(Clone, Debug, PartialEq, Eq, Builder)]
pub struct RedelegateBatch {
pub sources: Vec<Alias>,
pub source_validators: Vec<u64>, // step id of a bond step
pub amounts: Vec<u64>,
pub tx_settings: TxSettings,
}

impl Step for RedelegateBatch {
fn to_step_type(&self, step_index: u64) -> StepType {
StepType::RedelegateBatch {
parameters: TxRedelegateBatchParametersDto {
sources: self
.sources
.iter()
.map(|source| Value::v(source.to_string()))
.collect(),
src_validators: self
.source_validators
.iter()
.map(|source| Value::r(*source, "validator-0-address".to_string()))
.collect(),
dest_validators: self
.sources
.iter()
.map(|_| Value::f(Some(step_index - 1)))
.collect(),
amounts: self
.amounts
.iter()
.map(|amount| Value::v(amount.to_string()))
.collect(),
},
settings: Some(self.tx_settings.clone().into()),
}
}

fn update_state(&self, state: &mut crate::state::State) {
for idx in 0..self.sources.len() {
state.insert_redelegation_and_update_bonds(
&self.sources[idx],
self.source_validators[idx],
self.amounts[idx],
);
}
state.decrease_account_fees(&self.tx_settings);
}

fn post_hooks(&self, step_index: u64, _state: &State) -> Vec<Box<dyn crate::step::Hook>> {
vec![Box::new(CheckStep::new(step_index))]
}

fn pre_hooks(&self, _state: &State) -> Vec<Box<dyn crate::step::Hook>> {
vec![Box::new(QueryValidatorSet::new())]
}

fn total_post_hooks(&self) -> u64 {
1
}

fn total_pre_hooks(&self) -> u64 {
1
}
}

impl Display for RedelegateBatch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "bond batch of size {}", self.sources.len())
}
}
2 changes: 1 addition & 1 deletion src/queries/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;

use namada_sdk::{rpc};
use namada_sdk::rpc;
use serde::{Deserialize, Serialize};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/queries/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use namada_sdk::{rpc};
use namada_sdk::rpc;
use serde::{Deserialize, Serialize};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/queries/proposals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use namada_sdk::{rpc};
use namada_sdk::rpc;
use serde::{Deserialize, Serialize};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/queries/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeSet;

use async_trait::async_trait;

use namada_sdk::{rpc};
use namada_sdk::rpc;
use serde::{Deserialize, Serialize};

use crate::{
Expand Down
15 changes: 15 additions & 0 deletions src/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
},
reactivate_validator::{ReactivateValidatorParametersDto, TxReactivateValidator},
redelegate::{TxRedelegate, TxRedelegateParametersDto},
redelegate_batch::{TxRedelegateBatch, TxRedelegateBatchParametersDto},
reveal_pk::{RevealPkParametersDto, TxRevealPk},
shielded_sync::{ShieldedSync, ShieldedSyncParametersDto},
transparent_transfer_batch::{
Expand Down Expand Up @@ -233,6 +234,11 @@ pub enum StepType {
parameters: TxBondBatchParametersDto,
settings: Option<TxSettingsDto>,
},
#[serde(rename = "tx-redelegate-batch")]
RedelegateBatch {
parameters: TxRedelegateBatchParametersDto,
settings: Option<TxSettingsDto>,
},
}

impl Display for StepType {
Expand Down Expand Up @@ -276,6 +282,7 @@ impl Display for StepType {
StepType::CheckRevealPk { .. } => write!(f, "check-reveal-pk"),
StepType::TransparentTransferBatch { .. } => write!(f, "transparent-transfer-batch"),
StepType::BondBatch { .. } => write!(f, "bond-batch"),
StepType::RedelegateBatch { .. } => write!(f, "redelegate-batch"),
}
}
}
Expand Down Expand Up @@ -523,6 +530,14 @@ impl Step {
.run(sdk, parameters, settings, storage)
.await
}
StepType::RedelegateBatch {
parameters,
settings,
} => {
TxRedelegateBatch::default()
.run(sdk, parameters, settings, storage)
.await
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use namada_sdk::{
chain::ChainId,
io::NullIo,
key::common::{PublicKey, SecretKey},
masp::{fs::FsShieldedUtils},
masp::fs::FsShieldedUtils,
rpc,
wallet::{fs::FsWalletUtils, Wallet},
Namada, NamadaImpl, ShieldedWallet,
Expand Down
8 changes: 7 additions & 1 deletion src/tasks/bond_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ impl ToString for TxBondBatchStorageKeys {
fn to_string(&self) -> String {
match self {
TxBondBatchStorageKeys::SourceAddress(entry) => format!("source-{}-address", entry),
TxBondBatchStorageKeys::ValidatorAddress(entry) => format!("validator-{}-address", entry),
TxBondBatchStorageKeys::ValidatorAddress(entry) => {
format!("validator-{}-address", entry)
}
TxBondBatchStorageKeys::Amount(entry) => format!("amount-{}", entry),
TxBondBatchStorageKeys::BatchSize => "batch-size".to_string(),
TxBondBatchStorageKeys::AtomicBatch => "batch-atomic".to_string(),
Expand Down Expand Up @@ -110,6 +112,10 @@ impl Task for TxBondBatch {
.filter_map(|res| res.ok())
.collect::<Vec<(Tx, SigningTxData)>>();

if txs.is_empty() {
return Ok(StepResult::no_op());
}

let tx_args = Self::default_tx_arg(sdk).await;
let gas_payer = settings.clone().gas_payer.unwrap().to_public_key(sdk).await;
let tx_args = tx_args.gas_limit(GasLimit::from(
Expand Down
1 change: 1 addition & 0 deletions src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub mod init_pgf_funding_proposal;
pub mod init_pgf_steward_proposal;
pub mod reactivate_validator;
pub mod redelegate;
pub mod redelegate_batch;
pub mod reveal_pk;
pub mod shielded_sync;
pub mod transparent_transfer_batch;
Expand Down
Loading
Loading