From 3658f0cf769df3601a38a4cf6ac16e4c171a0771 Mon Sep 17 00:00:00 2001 From: Gianmarco Fraccaroli Date: Tue, 19 Mar 2024 15:06:22 +0100 Subject: [PATCH] added bond + check bond --- src/checks/bonds.rs | 6 +++--- src/gen/hooks/mod.rs | 1 + src/gen/hooks/query_validators.rs | 6 ++++-- src/gen/main.rs | 17 ++++++++++------- src/gen/scenario_builder.rs | 2 +- src/gen/step.rs | 2 +- src/gen/steps/bonds.rs | 25 ++++++++++++++++++++----- src/gen/steps/faucet_transfer.rs | 2 +- src/gen/steps/init_account.rs | 17 ++++++++++++++--- src/gen/steps/new_wallet_key.rs | 2 +- src/gen/steps/transparent_transfer.rs | 2 +- src/queries/validators.rs | 2 +- src/tasks/bond.rs | 6 +++--- src/tasks/init_account.rs | 4 ++-- 14 files changed, 63 insertions(+), 31 deletions(-) diff --git a/src/checks/bonds.rs b/src/checks/bonds.rs index cdff883..82ecfec 100644 --- a/src/checks/bonds.rs +++ b/src/checks/bonds.rs @@ -49,9 +49,9 @@ impl Check for BondsCheck { #[derive(Clone, Debug, Deserialize)] pub struct BondsCheckParametersDto { - amount: Value, - delegate: Value, - delegator: Value, + pub amount: Value, + pub delegate: Value, + pub delegator: Value, } #[derive(Clone, Debug)] diff --git a/src/gen/hooks/mod.rs b/src/gen/hooks/mod.rs index d1c4649..cf26310 100644 --- a/src/gen/hooks/mod.rs +++ b/src/gen/hooks/mod.rs @@ -1,4 +1,5 @@ pub mod check_balance; +pub mod check_bond; pub mod check_step; pub mod query_validators; pub mod storage_check; diff --git a/src/gen/hooks/query_validators.rs b/src/gen/hooks/query_validators.rs index b0cfe08..65dab9a 100644 --- a/src/gen/hooks/query_validators.rs +++ b/src/gen/hooks/query_validators.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use derive_builder::Builder; use namada_scenario_tester::{ - scenario::StepType, + queries::validators::ValidatorsQueryParametersDto, scenario::StepType, }; use crate::step::Hook; @@ -24,7 +24,9 @@ impl QueryValidatorSet { impl Hook for QueryValidatorSet { fn to_json(&self) -> StepType { - todo!() + StepType::QueryValidators { + parameters: ValidatorsQueryParametersDto { epoch: None }, + } } } diff --git a/src/gen/main.rs b/src/gen/main.rs index 976b2c5..d84cdf8 100644 --- a/src/gen/main.rs +++ b/src/gen/main.rs @@ -15,26 +15,29 @@ fn main() { TaskType::NewWalletKey, TaskType::FaucetTransafer, TaskType::TransparentTransfer, - // TaskType::Bond, - // TaskType::InitAccount, + TaskType::Bond, + TaskType::InitAccount, ]; - let mut scenario_builder = ScenarioBuilder::new(tasks, vec![1.into(), 1.into(), 2.into()]); + let mut scenario_builder = ScenarioBuilder::new( + tasks, + vec![1.into(), 1.into(), 2.into(), 2.into(), 1.into()], + ); - for _step_index in 0..=10 { + for _step_index in 0..=20 { let next_task = loop { let task_type = scenario_builder.choose_next_task(); if scenario_builder.is_valid_task(task_type) { break task_type; } }; - let step = scenario_builder.build_step(next_task); // bond + let step = scenario_builder.build_step(next_task); scenario_builder.update_state(step.clone()); scenario_builder.update_scenario(step.clone()); } - for step in scenario_builder.scenario { - println!("{:?}", step); + for (index, step) in scenario_builder.scenario.iter().enumerate() { + println!("{}: {:?}", index, step); } } diff --git a/src/gen/scenario_builder.rs b/src/gen/scenario_builder.rs index df67e89..bfdf5a9 100644 --- a/src/gen/scenario_builder.rs +++ b/src/gen/scenario_builder.rs @@ -83,7 +83,7 @@ impl ScenarioBuilder { .into_iter() .map(|step| step.to_json()) .collect::>(); - let step_json = step.to_json(); + let step_json = step.to_json(step_index); self.scenario.extend(pre_hooks_json); self.scenario.push(step_json); diff --git a/src/gen/step.rs b/src/gen/step.rs index f4287ce..93cd49d 100644 --- a/src/gen/step.rs +++ b/src/gen/step.rs @@ -123,7 +123,7 @@ impl TaskType { } pub trait Step: DynClone + Debug + Display { - fn to_json(&self) -> StepType; + fn to_json(&self, step_index: u64) -> StepType; fn update_state(&self, state: &mut State); fn post_hooks(&self, step_index: u64, state: &State) -> Vec>; fn pre_hooks(&self, state: &State) -> Vec>; diff --git a/src/gen/steps/bonds.rs b/src/gen/steps/bonds.rs index 9c6a831..3f779c4 100644 --- a/src/gen/steps/bonds.rs +++ b/src/gen/steps/bonds.rs @@ -1,11 +1,13 @@ use std::fmt::Display; use derive_builder::Builder; -use namada_scenario_tester::scenario::StepType; +use namada_scenario_tester::{ + scenario::StepType, tasks::bond::TxBondParametersDto, utils::value::Value, +}; use crate::{ entity::Alias, - hooks::{check_step::CheckStep, query_validators::QueryValidatorSet}, + hooks::{check_bond::CheckBond, check_step::CheckStep, query_validators::QueryValidatorSet}, state::State, step::Step, }; @@ -17,8 +19,14 @@ pub struct Bond { } impl Step for Bond { - fn to_json(&self) -> StepType { - todo!() + fn to_json(&self, step_index: u64) -> StepType { + StepType::Bond { + parameters: TxBondParametersDto { + source: Value::v(self.source.to_string()), + validator: Value::r(step_index - 1, "validator-1-address".to_string()), + amount: Value::v(self.amount.to_string()), + }, + } } fn update_state(&self, state: &mut crate::state::State) { @@ -27,7 +35,14 @@ impl Step for Bond { } fn post_hooks(&self, step_index: u64, _state: &State) -> Vec> { - vec![Box::new(CheckStep::new(step_index))] + vec![ + Box::new(CheckStep::new(step_index)), + Box::new(CheckBond::new( + self.source.clone(), + step_index - 1, + self.amount, + )), + ] } fn pre_hooks(&self, _state: &State) -> Vec> { diff --git a/src/gen/steps/faucet_transfer.rs b/src/gen/steps/faucet_transfer.rs index c7b98e3..410d813 100644 --- a/src/gen/steps/faucet_transfer.rs +++ b/src/gen/steps/faucet_transfer.rs @@ -21,7 +21,7 @@ pub struct FaucetTransfer { } impl Step for FaucetTransfer { - fn to_json(&self) -> StepType { + fn to_json(&self, _step_index: u64) -> StepType { StepType::TransparentTransfer { parameters: TxTransparentTransferParametersDto { source: Value::v("faucet".to_string()), diff --git a/src/gen/steps/init_account.rs b/src/gen/steps/init_account.rs index e2eb617..6d013b5 100644 --- a/src/gen/steps/init_account.rs +++ b/src/gen/steps/init_account.rs @@ -1,7 +1,9 @@ use std::fmt::Display; use derive_builder::Builder; -use namada_scenario_tester::scenario::StepType; +use namada_scenario_tester::{ + scenario::StepType, tasks::init_account::TxInitAccountParametersDto, utils::value::Value, +}; use crate::{entity::Alias, hooks::check_step::CheckStep, state::State, step::Step}; @@ -13,8 +15,17 @@ pub struct InitAccount { } impl Step for InitAccount { - fn to_json(&self) -> StepType { - todo!() + fn to_json(&self, _step_index: u64) -> StepType { + StepType::InitAccount { + parameters: TxInitAccountParametersDto { + sources: self + .pks + .iter() + .map(|alias| Value::v(alias.to_string())) + .collect(), + threshold: Some(Value::v(self.threshold.to_string())), + }, + } } fn update_state(&self, state: &mut crate::state::State) { diff --git a/src/gen/steps/new_wallet_key.rs b/src/gen/steps/new_wallet_key.rs index e45c8a5..571ba56 100644 --- a/src/gen/steps/new_wallet_key.rs +++ b/src/gen/steps/new_wallet_key.rs @@ -12,7 +12,7 @@ pub struct NewWalletStep { } impl Step for NewWalletStep { - fn to_json(&self) -> StepType { + fn to_json(&self, _step_index: u64) -> StepType { StepType::WalletNewKey { parameters: WalletNewKeyParametersDto {}, } diff --git a/src/gen/steps/transparent_transfer.rs b/src/gen/steps/transparent_transfer.rs index 6d0a24c..ae3dbbf 100644 --- a/src/gen/steps/transparent_transfer.rs +++ b/src/gen/steps/transparent_transfer.rs @@ -22,7 +22,7 @@ pub struct TransparentTransfer { } impl Step for TransparentTransfer { - fn to_json(&self) -> StepType { + fn to_json(&self, _step_index: u64) -> StepType { StepType::TransparentTransfer { parameters: TxTransparentTransferParametersDto { source: Value::v(self.source.to_string()), diff --git a/src/queries/validators.rs b/src/queries/validators.rs index abf5b7f..b81a8fb 100644 --- a/src/queries/validators.rs +++ b/src/queries/validators.rs @@ -68,7 +68,7 @@ impl Query for ValidatorsQuery { #[derive(Clone, Debug, Deserialize)] pub struct ValidatorsQueryParametersDto { - epoch: Option, + pub epoch: Option, } #[derive(Clone, Debug)] diff --git a/src/tasks/bond.rs b/src/tasks/bond.rs index eb9957f..a0a0f41 100644 --- a/src/tasks/bond.rs +++ b/src/tasks/bond.rs @@ -99,9 +99,9 @@ impl Task for TxBond { #[derive(Clone, Debug, Deserialize)] pub struct TxBondParametersDto { - source: Value, - validator: Value, - amount: Value, + pub source: Value, + pub validator: Value, + pub amount: Value, } #[derive(Clone, Debug)] diff --git a/src/tasks/init_account.rs b/src/tasks/init_account.rs index 89fc57b..e5c5c0b 100644 --- a/src/tasks/init_account.rs +++ b/src/tasks/init_account.rs @@ -167,8 +167,8 @@ impl Task for TxInitAccount { #[derive(Clone, Debug, Deserialize)] pub struct TxInitAccountParametersDto { - sources: Vec, - threshold: Option, + pub sources: Vec, + pub threshold: Option, } impl TxInitAccountParametersDto {