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

More abci tests for staking #322

Merged
merged 13 commits into from
Nov 6, 2024
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions gears/src/x/keepers/mocks/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl AuthParams for MockAuthParams {
pub struct MockAuthKeeper {
pub get_auth_params: MockAuthParams,
pub has_account: bool,
pub get_account: Option<Account>,
pub get_account: Vec<Account>,
}

impl<SK: StoreKey, M: Module> AuthKeeper<SK, M> for MockAuthKeeper {
Expand All @@ -70,9 +70,15 @@ impl<SK: StoreKey, M: Module> AuthKeeper<SK, M> for MockAuthKeeper {
fn get_account<DB: database::Database, CTX: QueryableContext<DB, SK>>(
&self,
_: &CTX,
_: &address::AccAddress,
addr: &address::AccAddress,
) -> Result<Option<Account>, GasStoreErrors> {
Ok(self.get_account.clone())
let account = self
.get_account
.iter()
.find(|this| this.get_address() == addr)
.cloned();

Ok(account)
}

fn set_account<DB: database::Database, CTX: TransactionalContext<DB, SK>>(
Expand Down
3 changes: 2 additions & 1 deletion gears/src/x/keepers/mocks/bank.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use address::AccAddress;
use kv_store::StoreKey;

use crate::{
Expand All @@ -21,7 +22,7 @@ use crate::{
pub struct MockBankKeeper {
pub get_denom_metadata: Option<Metadata>,
pub balance_all: Vec<UnsignedCoin>,
pub balance: Option<UnsignedCoin>,
pub balance: HashMap<AccAddress, UnsignedCoin>,
pub supply: HashMap<Denom, UnsignedCoin>,
}

Expand Down
14 changes: 11 additions & 3 deletions gears/src/x/keepers/mocks/gov.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmwasm_std::Uint256;
use kv_store::StoreKey;

use crate::{
Expand All @@ -11,9 +12,16 @@ impl<SK: StoreKey, M: Module> GovernanceBankKeeper<SK, M> for MockBankKeeper {
fn balance<DB: database::Database, CTX: crate::context::QueryableContext<DB, SK>>(
&self,
_: &CTX,
_: &address::AccAddress,
_: &crate::types::denom::Denom,
addr: &address::AccAddress,
denom: &crate::types::denom::Denom,
) -> Result<UnsignedCoin, GasStoreErrors> {
Ok(self.balance.clone().expect("balances field in mock is not set"))
Ok(self
.balance
.get(addr)
.cloned()
.unwrap_or_else(|| UnsignedCoin {
denom: denom.clone(),
amount: Uint256::zero(),
}))
}
}
1 change: 1 addition & 0 deletions tendermint/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod types {
}

pub mod response {
pub use crate::types::response::deliver_tx::ResponseDeliverTx;
pub use crate::types::response::query::ResponseQuery;
}

Expand Down
13 changes: 12 additions & 1 deletion x/mint/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn set_node(
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Default);

init_node(opt).0
init_node(opt)
}

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -251,6 +251,17 @@ impl BankKeeper<SpaceKey, Modules> for MockBankKeeper {
) -> Result<(), gears::x::errors::BankKeeperError> {
todo!()
}

fn send_coins_from_account_to_account<
DB: gears::store::database::Database,
CTX: gears::context::TransactionalContext<DB, SpaceKey>,
>(
&self,
_ctx: &mut CTX,
_msg: &gears::types::msg::send::MsgSend,
) -> Result<(), gears::x::errors::BankKeeperError> {
todo!()
}
}

impl BalancesKeeper<SpaceKey, Modules> for MockBankKeeper {
Expand Down
4 changes: 4 additions & 0 deletions x/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ tracing = { workspace = true }
tonic = { workspace = true }

[dev-dependencies]
bank = { path = "../bank" }
auth ={ path = "../auth" }
data-encoding = { workspace = true }
strum = { workspace = true }
gears = { path = "../../gears", features = ["cli", "xmods", "governance", "utils", "mocks"] }
vec1 = { workspace = true }
pretty_assertions = "1.4.1"
5 changes: 4 additions & 1 deletion x/staking/src/keeper/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ impl<
// validator must already be registered
let mut validator = self
.validator(ctx, &msg.validator_address)?
.ok_or(anyhow::anyhow!("Account {} exists", msg.validator_address))?;
.ok_or(anyhow::anyhow!(
"Account not {} exists",
msg.validator_address
))?;

// replace all editable fields (clients should autofill existing values)
let description = validator
Expand Down
23 changes: 21 additions & 2 deletions x/staking/src/types/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Commission {
}

/// Description defines a validator description.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct EditDescription {
/// moniker defines a human-readable name for the validator.
pub moniker: Option<String>,
Expand Down Expand Up @@ -272,6 +272,26 @@ pub struct Description {
impl Protobuf<Description> for Description {}

impl Description {
pub fn try_new<T: Into<String>>(
moniker: T,
identity: T,
website: T,
security_contact: T,
details: T,
) -> Result<Self, anyhow::Error> {
let desc = Self {
moniker: moniker.into(),
identity: identity.into(),
website: website.into(),
security_contact: security_contact.into(),
details: details.into(),
};

desc.ensure_length()?;

Ok(desc)
}

/// create_updated_description creates a description with the base of current description
/// supplemented by values from a given description. An error is
/// returned if the resulting description contains an invalid length.
Expand Down Expand Up @@ -458,7 +478,6 @@ impl ValueRenderer for CreateValidator {
}
}

/// CreateValidator defines a SDK message for creating a new validator.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, AppMessage)]
#[msg(url = "/cosmos.staking.v1beta1.MsgEditValidator")]
pub struct EditValidator {
Expand Down
133 changes: 0 additions & 133 deletions x/staking/tests/abci.rs
Original file line number Diff line number Diff line change
@@ -1,133 +0,0 @@
use std::str::FromStr;

use gears::{
application::handlers::node::ModuleInfo,
derive::{ParamsKeys, StoreKeys},
tendermint::types::time::timestamp::Timestamp,
types::{address::AccAddress, base::coin::UnsignedCoin},
utils::node::{init_node, GenesisSource, MockOptionsFormer},
x::{
keepers::mocks::{auth::MockAuthKeeper, bank::MockBankKeeper},
module::Module,
},
};
use staking::{GenesisState, Keeper, MockHookKeeper, StakingABCIHandler};

#[test]
/// In this scenario, we test the initialization of the application and execute a few blocks
fn test_init_and_few_blocks() {
let opt: MockOptionsFormer<
SubspaceKey,
StakingABCIHandler<
SpaceKey,
SubspaceKey,
MockAuthKeeper,
MockBankKeeper,
MockHookKeeper<SpaceKey, MockAuthKeeper, StakingModules>,
StakingModules,
BankModuleInfo,
>,
GenesisState,
> = MockOptionsFormer::new()
.abci_handler(StakingABCIHandler::new(Keeper::new(
SpaceKey::Auth,
SubspaceKey::Auth,
MockAuthKeeper::former().form(),
MockBankKeeper::former()
.balance(UnsignedCoin::from_str("34uaton").expect("valid default"))
.form(),
None,
StakingModules::BondedPool,
StakingModules::NotBondedPool,
)))
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Default);

let mut node = init_node(opt);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"67647df38f8fe610ef4c15581f73ac76d4c8598f02db3fb3cf23052a9de7da22"
);

node.skip_steps(100);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"0b82cabbbe14529b18ce923a4599cfa8ce5b9557fc8bf3d9a430af4858de3632"
);
}

#[derive(Debug, Clone)]
struct BankModuleInfo;

impl ModuleInfo for BankModuleInfo {
const NAME: &'static str = "bank";
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StakingModules {
FeeCollector,
BondedPool,
NotBondedPool,
}

impl Module for StakingModules {
fn name(&self) -> String {
match self {
StakingModules::FeeCollector => "fee_collector".into(),
StakingModules::BondedPool => staking::BONDED_POOL_NAME.into(),
StakingModules::NotBondedPool => staking::NOT_BONDED_POOL_NAME.into(),
}
}

fn address(&self) -> AccAddress {
match self {
StakingModules::FeeCollector => {
AccAddress::from_bech32("cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta")
.expect("hard coded address is valid")
}
StakingModules::BondedPool => {
AccAddress::from_bech32("cosmos10q6njatvx5u9jwz2desnzdj6232yx4te2e2h5sekvdj4jwzxx3tkvdntv439wnp32ae5gstzfdkxsdrdwgehyknvx4xhxs6jxqe9g5ttg345wv25fy6rz46gxes56urw232kvs2fwpt5j33k09ekvdntwpg9zu662kyl4s")
.expect("hard coded address is valid")
}
StakingModules::NotBondedPool => {
AccAddress::from_bech32("cosmos1w9f8xsn3x48kcatsdpaxg7texqc4zc6vddx5wvt5d3n8vu6gx3c9g668fgc5xsjj29pnvkn5va4k5en6da24zvtk2q69y7tfdq6k7kf4w3p56m6ng3a8w4pexe39wu2fdfz5y4rpv564z6ektpz4gjtdd4fy7snn03rq66")
.expect("hard coded address is valid")
}
}
}

fn permissions(&self) -> Vec<String> {
match self {
StakingModules::FeeCollector => Vec::new(),
StakingModules::BondedPool => vec!["burner".into(), "staking".into()],
StakingModules::NotBondedPool => {
vec!["burner".into(), "staking".into()]
}
}
}
}

#[derive(strum::EnumIter, Debug, PartialEq, Eq, Hash, Clone, StoreKeys)]
#[skey(params = Params)]
pub enum SpaceKey {
#[skey(to_string = "acc")]
Auth,
#[skey(to_string = "bank")]
Bank,
#[skey(to_string = "params")]
Params,
}

#[derive(strum::EnumIter, Debug, PartialEq, Eq, Hash, Clone, ParamsKeys)]
pub enum SubspaceKey {
#[pkey(to_string = "auth/")]
Auth,
#[pkey(to_string = "bank/")]
Bank,
#[pkey(to_string = "baseapp/")]
BaseApp,
}
26 changes: 26 additions & 0 deletions x/staking/tests/abci_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use gears::{tendermint::types::time::timestamp::Timestamp, utils::node::GenesisSource};

use utils::set_node;

#[path = "./utils.rs"]
mod utils;

#[test]
/// In this scenario, we test the initialization of the application and execute a few blocks
fn test_init_and_few_blocks() {
let mut node = set_node(GenesisSource::Default);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"9a6bf6c50ecff19e4ea4838630b5adb8d399a05b62ff742dc26d316f019e54ca"
);

node.skip_steps(100);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"a31893c018dc6bb7cf756c57a2e0e252fdbbf83b33e3039307ade8476dbef999"
);
}
Loading
Loading