Skip to content

Commit

Permalink
improve bond batch tx
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Oct 21, 2024
1 parent ea46929 commit fc3ed8a
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 102 deletions.
124 changes: 62 additions & 62 deletions src/gen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,100 +18,100 @@ pub mod step;
pub mod steps;
pub mod utils;

// #[derive(Parser, Debug)]
// #[command(version, about, long_about = None)]
// struct Args {
// #[arg(long)]
// steps: u64,
// #[arg(long, default_value_t = 1)]
// total: u64,
// #[arg(long, default_value_t = 5)]
// transparent_transfers: u64,
// #[arg(long, default_value_t = 3)]
// shielding_transfer: u64,
// #[arg(long, default_value_t = 3)]
// unshielding_transfer: u64,
// #[arg(long, default_value_t = 3)]
// init_account: u64,
// #[arg(long, default_value_t = 4)]
// bond: u64,
// #[arg(long, default_value_t = 4)]
// unbond: u64,
// #[arg(long, default_value_t = 0)]
// withdraw: u64,
// #[arg(long, default_value_t = 20)]
// vote_proposal: u64,
// #[arg(long, default_value_t = 4)]
// redelegate: u64,
// #[arg(long, default_value_t = 80)]
// init_default_proposal: u64,
// #[arg(long, default_value_t = 40)]
// init_pgf_steward_proposal: u64,
// #[arg(long, default_value_t = 40)]
// init_pgf_funding_proposal: u64,
// #[arg(long, default_value_t = 2)]
// become_validator: u64,
// #[arg(long, default_value_t = 3)]
// update_account: u64,
// #[arg(long, default_value_t = 1)]
// deactivate_validator: u64,
// #[arg(long, default_value_t = 2)]
// change_metadata: u64,
// #[arg(long, default_value_t = 4)]
// claim_rewards: u64,
// #[arg(long, default_value_t = 6)]
// transparent_transfer_batch: u64,
// #[arg(long, default_value_t = 6)]
// bond_batch: u64,
// }

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(long)]
steps: u64,
#[arg(long, default_value_t = 1)]
total: u64,
#[arg(long, default_value_t = 5)]
#[arg(long, default_value_t = 0)]
transparent_transfers: u64,
#[arg(long, default_value_t = 3)]
#[arg(long, default_value_t = 0)]
shielding_transfer: u64,
#[arg(long, default_value_t = 3)]
#[arg(long, default_value_t = 0)]
unshielding_transfer: u64,
#[arg(long, default_value_t = 3)]
#[arg(long, default_value_t = 2)]
init_account: u64,
#[arg(long, default_value_t = 4)]
#[arg(long, default_value_t = 0)]
bond: u64,
#[arg(long, default_value_t = 4)]
#[arg(long, default_value_t = 0)]
unbond: u64,
#[arg(long, default_value_t = 0)]
withdraw: u64,
#[arg(long, default_value_t = 20)]
#[arg(long, default_value_t = 0)]
vote_proposal: u64,
#[arg(long, default_value_t = 4)]
redelegate: u64,
#[arg(long, default_value_t = 80)]
#[arg(long, default_value_t = 0)]
init_default_proposal: u64,
#[arg(long, default_value_t = 40)]
#[arg(long, default_value_t = 0)]
init_pgf_steward_proposal: u64,
#[arg(long, default_value_t = 40)]
#[arg(long, default_value_t = 0)]
init_pgf_funding_proposal: u64,
#[arg(long, default_value_t = 2)]
become_validator: u64,
#[arg(long, default_value_t = 3)]
#[arg(long, default_value_t = 0)]
update_account: u64,
#[arg(long, default_value_t = 1)]
#[arg(long, default_value_t = 0)]
deactivate_validator: u64,
#[arg(long, default_value_t = 2)]
#[arg(long, default_value_t = 0)]
change_metadata: u64,
#[arg(long, default_value_t = 4)]
#[arg(long, default_value_t = 0)]
claim_rewards: u64,
#[arg(long, default_value_t = 6)]
#[arg(long, default_value_t = 0)]
transparent_transfer_batch: u64,
#[arg(long, default_value_t = 6)]
bond_batch: u64,
}

// #[derive(Parser, Debug)]
// #[command(version, about, long_about = None)]
// struct Args {
// #[arg(long)]
// steps: u64,
// #[arg(long, default_value_t = 1)]
// total: u64,
// #[arg(long, default_value_t = 0)]
// transparent_transfers: u64,
// #[arg(long, default_value_t = 0)]
// shielding_transfer: u64,
// #[arg(long, default_value_t = 0)]
// unshielding_transfer: u64,
// #[arg(long, default_value_t = 0)]
// init_account: u64,
// #[arg(long, default_value_t = 0)]
// bond: u64,
// #[arg(long, default_value_t = 0)]
// unbond: u64,
// #[arg(long, default_value_t = 0)]
// withdraw: u64,
// #[arg(long, default_value_t = 0)]
// vote_proposal: u64,
// #[arg(long, default_value_t = 0)]
// redelegate: u64,
// #[arg(long, default_value_t = 0)]
// init_default_proposal: u64,
// #[arg(long, default_value_t = 0)]
// init_pgf_steward_proposal: u64,
// #[arg(long, default_value_t = 0)]
// init_pgf_funding_proposal: u64,
// #[arg(long, default_value_t = 0)]
// become_validator: u64,
// #[arg(long, default_value_t = 0)]
// update_account: u64,
// #[arg(long, default_value_t = 0)]
// deactivate_validator: u64,
// #[arg(long, default_value_t = 0)]
// change_metadata: u64,
// #[arg(long, default_value_t = 0)]
// claim_rewards: u64,
// #[arg(long, default_value_t = 6)]
// transparent_transfer_batch: u64,
// #[arg(long, default_value_t = 6)]
// bond_batch: u64,
// }

fn main() {
let args = Args::parse();

Expand Down
31 changes: 30 additions & 1 deletion src/gen/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,42 @@ impl State {
.clone()
}

pub fn implicit_addresses_with_at_least_native_token_balance_and_blacklist(
&self,
amount: u64,
blacklist: Vec<Alias>,
) -> Vec<Account> {
self.balances
.iter()
.fold(vec![], |mut acc, (alias, token_balances)| {
if alias.to_string().ends_with("-pa") {
return acc;
}
if alias.to_string().contains("-enst") {
return acc
}
if blacklist.contains(&alias) {
return acc;
}
if let Some(balance) = token_balances.get(&Alias::native_token()) {
if *balance > amount {
let account = self.get_account_from_alias(alias);
acc.push(account);
}
acc
} else {
acc
}
})
}

pub fn random_implicit_account_with_at_least_native_token_balance_with_blacklist(
&self,
amount: u64,
blacklist: Vec<Alias>,
) -> Account {
let all_implicit_addresses_with_native_token_balance =
self.addresses_with_at_least_native_token_balance_and_blacklist(amount, blacklist);
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
69 changes: 46 additions & 23 deletions src/gen/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use namada_scenario_tester::scenario::StepType;
use namada_sdk::token::NATIVE_SCALE;

use crate::{
constants::{MAX_PGF_ACTIONS, MIN_FEE, PROPOSAL_FUNDS},
constants::{DEFAULT_GAS_LIMIT, MAX_PGF_ACTIONS, MIN_FEE, PROPOSAL_FUNDS},
entity::{Alias, TxSettings},
state::State,
steps::{
Expand Down Expand Up @@ -171,22 +171,21 @@ impl TaskType {
TaskType::TransparentTransferBatch => {
!state
.implicit_addresses_with_at_least_native_token_balance(MIN_FEE * 10)
.len()
> 2
&& !state
.is_empty()
&& state
.addresses_with_at_least_native_token_balance(MIN_FEE * 5)
.is_empty()
.len()
> 2
&& state.any_address().len() > 5
}
TaskType::BondBatch => {
!state
.implicit_addresses_with_at_least_native_token_balance(MIN_FEE * 5)
.len()
> 2
.is_empty()
&& state
.addresses_with_at_least_native_token_balance(MIN_FEE * 3)
.implicit_addresses_with_at_least_native_token_balance(MIN_FEE * 3)
.len()
> 5
> 2
}
}
}
Expand Down Expand Up @@ -606,7 +605,7 @@ impl TaskType {
let source_bond_balance =
state.get_alias_token_balance(&bond.source, &Alias::native_token());

let tx_settings =
let mut tx_settings =
if bond.source.clone().is_implicit() && source_bond_balance > MIN_FEE {
let gas_payer = bond.source.clone();
TxSettings::default_from_implicit(gas_payer)
Expand All @@ -617,6 +616,7 @@ impl TaskType {
let account = state.get_account_from_alias(&bond.source);
TxSettings::default_from_enstablished(account.implicit_addresses, gas_payer)
};
tx_settings.gas_limit = DEFAULT_GAS_LIMIT + DEFAULT_GAS_LIMIT / 2;

let amount = utils::random_between(1, bond.amount);
let step = RedelegateBuilder::default()
Expand Down Expand Up @@ -759,14 +759,25 @@ impl TaskType {
let gas_payer = state
.random_implicit_account_with_at_least_native_token_balance_with_blacklist(
MIN_FEE * total_batched_txs,
vec![source.alias.clone()]
vec![source.alias.clone()],
)
.alias;
let mut tx_settings = TxSettings::default_from_enstablished(
source.clone().implicit_addresses,
gas_payer,
);
tx_settings.gas_limit = MIN_FEE * total_batched_txs;

let tx_settings = if source.address_type.is_implicit() {
TxSettings {
signers: BTreeSet::from_iter([source.alias]),
broadcast_only: false,
gas_limit: MIN_FEE * total_batched_txs,
gas_payer,
}
} else {
TxSettings {
signers: source.implicit_addresses,
broadcast_only: false,
gas_limit: MIN_FEE * total_batched_txs,
gas_payer,
}
};

let step = TransparentTransferBatchBuilder::default()
.sources(sources)
Expand All @@ -782,7 +793,8 @@ impl TaskType {
TaskType::BondBatch => {
let total_batched_txs = utils::random_between(2, 5);

let source = state.random_account_with_at_least_native_token_balance(MIN_FEE * 5);
let source =
state.random_non_validator_address_with_at_least_native_token(MIN_FEE + 1);
let balance = state.random_token_balance_for_alias(&source.alias);

let sources = vec![source.clone().alias; total_batched_txs as usize];
Expand All @@ -793,14 +805,25 @@ impl TaskType {
let gas_payer = state
.random_implicit_account_with_at_least_native_token_balance_with_blacklist(
MIN_FEE * total_batched_txs,
vec![source.alias.clone()]
vec![source.alias.clone()],
)
.alias;
let mut tx_settings = TxSettings::default_from_enstablished(
source.clone().implicit_addresses,
gas_payer,
);
tx_settings.gas_limit = MIN_FEE * total_batched_txs;

let tx_settings = if source.address_type.is_implicit() {
TxSettings {
signers: BTreeSet::from_iter([source.alias]),
broadcast_only: false,
gas_limit: MIN_FEE * total_batched_txs,
gas_payer,
}
} else {
TxSettings {
signers: source.implicit_addresses,
broadcast_only: false,
gas_limit: MIN_FEE * total_batched_txs,
gas_payer,
}
};

let step = BondBatchBuilder::default()
.sources(sources)
Expand Down
4 changes: 4 additions & 0 deletions src/gen/steps/bond_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl Step for BondBatch {
state.decrease_account_token_balance(&source, &Alias::native_token(), amount);
state.insert_bond(&source, amount);
}
// let source = self.sources[0].clone();
// let amount = self.amounts[0];
// state.decrease_account_token_balance(&source, &Alias::native_token(), amount);
// state.insert_bond(&source, amount);
state.decrease_account_fees(&self.tx_settings);
}

Expand Down
4 changes: 2 additions & 2 deletions src/gen/steps/redelegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl Step for Redelegate {
StepType::Redelegate {
parameters: TxRedelegateParametersDto {
source: Value::v(self.source.to_string()),
src_validator: Value::r(self.source_validator, "validator-address".to_string()),
src_validator: Value::r(self.source_validator, "validator-0-address".to_string()),
dest_validator: Value::f(Some(step_index - 1)),
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
6 changes: 3 additions & 3 deletions src/tasks/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub enum TxBondStorageKeys {
impl ToString for TxBondStorageKeys {
fn to_string(&self) -> String {
match self {
TxBondStorageKeys::SourceAddress => "source-address".to_string(),
TxBondStorageKeys::ValidatorAddress => "validator-address".to_string(),
TxBondStorageKeys::Amount => "amount".to_string(),
TxBondStorageKeys::SourceAddress => "source-0-address".to_string(),
TxBondStorageKeys::ValidatorAddress => "validator-0-address".to_string(),
TxBondStorageKeys::Amount => "amount-0".to_string(),
}
}
}
Expand Down
Loading

0 comments on commit fc3ed8a

Please sign in to comment.