Skip to content

Commit

Permalink
add tests for queries, but need to add staking
Browse files Browse the repository at this point in the history
  • Loading branch information
NYBACHOK committed Oct 29, 2024
1 parent a34c19a commit 335c7a7
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 11 deletions.
4 changes: 4 additions & 0 deletions gears/src/utils/node/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,8 @@ impl<G: Clone, App: ABCIApplication<G>> MockNode<App, G> {
let _ = self.step(vec![], Timestamp::UNIX_EPOCH);
}
}

pub fn height(&self) -> u32 {
self.height
}
}
1 change: 1 addition & 0 deletions x/mint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ ibc-proto = { workspace = true }
clap = { workspace = true }

[dev-dependencies]
gears = { path = "../../gears", features = ["cli", "xmods", "governance", "utils" ] }
data-encoding = { workspace = true }
strum = { workspace = true }
14 changes: 11 additions & 3 deletions x/mint/src/abci_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ impl<
let total_staking_supply = self
.keeper
.staking_token_supply(ctx)
.expect("overflow")
.amount;
.map(|this| this.amount)
.unwrap_or_default();
let bonded_ration = self.keeper.bonded_ratio(ctx);

//
Expand All @@ -210,7 +210,15 @@ impl<
let minted_coin = minter.block_provision(&params).expect("overflow");
let minted_attribute =
EventAttribute::new("amount".into(), minted_coin.amount.to_string().into(), true);
let minted_coins = UnsignedCoins::new(vec![minted_coin]).expect("invalid coin for minting");

let minted_coins = match UnsignedCoins::new([minted_coin]) {
Ok(minted_coins) => minted_coins,
Err(_) => {
tracing::info!("No suitable coin for minting found");

return;
}
};

if let Err(err) = self.keeper.mint_coins(ctx, minted_coins.clone()) {
panic!(
Expand Down
10 changes: 8 additions & 2 deletions x/mint/tests/abci_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ fn test_init_and_few_blocks() {

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;

assert_eq!(data_encoding::HEXLOWER.encode(app_hash), "");
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"dca0afec3e333f4eb5d48a074ec6a861dbf945f6cc04e8e880e76b4136857180"
);

node.skip_steps(100);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(data_encoding::HEXLOWER.encode(app_hash), "");
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"548e264ab9174c4c366abd27c0d0c888fa591977145d314f9c548e60160f01d4"
);
}
70 changes: 70 additions & 0 deletions x/mint/tests/query_inflation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use gears::{
core::Protobuf,
extensions::testing::UnwrapTesting,
tendermint::types::{
request::query::RequestQuery,
response::ResponseQuery,
time::{
duration::Duration,
timestamp::{Timestamp, TimestampSeconds},
},
},
types::decimal256::Decimal256,
};
use mint::types::query::{request::QueryInflationRequest, response::QueryInflationResponse};
use utils::set_node;

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

#[test]
fn query_inflation_after_init() {
let mut node = set_node();

let _ = node.step(vec![], Timestamp::UNIX_EPOCH);

let q = QueryInflationRequest {};
let ResponseQuery { value, .. } = node.query(RequestQuery {
data: q.encode_vec().into(),
path: QueryInflationRequest::QUERY_URL.to_owned(),
height: 1,
prove: false,
});

let QueryInflationResponse { inflation } =
QueryInflationResponse::decode_vec(&value).unwrap_test();

let expected_inflation = Decimal256::from_atomics(2_u8, 1).unwrap_test();

assert_eq!(expected_inflation, inflation);
}

#[test]
fn query_inflation_after_month_without_staking() {
let mut node = set_node();

// Well. I simulate chain which runs for year and each block takes 5 seconds
let mut timestamp = Timestamp::UNIX_EPOCH;
while timestamp.timestamp_seconds() <= TimestampSeconds::try_from(2_628_000).unwrap_test() {
let _ = node.step(vec![], Timestamp::UNIX_EPOCH);

timestamp = timestamp
.checked_add(Duration::new_from_secs(5))
.unwrap_test();
}

let q = QueryInflationRequest {};
let ResponseQuery { value, .. } = node.query(RequestQuery {
data: q.encode_vec().into(),
path: QueryInflationRequest::QUERY_URL.to_owned(),
height: node.height() as i64,
prove: false,
});

let QueryInflationResponse { inflation } =
QueryInflationResponse::decode_vec(&value).unwrap_test();

let expected_inflation = Decimal256::from_atomics(2_u8, 1).unwrap_test();

assert_eq!(expected_inflation, inflation);
}
71 changes: 71 additions & 0 deletions x/mint/tests/query_provision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use gears::{
core::Protobuf,
extensions::testing::UnwrapTesting,
tendermint::types::{
request::query::RequestQuery,
response::ResponseQuery,
time::{
duration::Duration,
timestamp::{Timestamp, TimestampSeconds},
},
},
types::decimal256::Decimal256,
};
use mint::types::query::{
request::QueryAnnualProvisionsRequest, response::QueryAnnualProvisionsResponse,
};
use utils::set_node;

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

#[test]
fn query_provision_after_init() {
let mut node = set_node();

let _ = node.step(vec![], Timestamp::UNIX_EPOCH);

let q = QueryAnnualProvisionsRequest {};
let ResponseQuery { value, .. } = node.query(RequestQuery {
data: q.encode_vec().into(),
path: QueryAnnualProvisionsRequest::QUERY_URL.to_owned(),
height: 1,
prove: false,
});

let QueryAnnualProvisionsResponse { annual_provisions } =
QueryAnnualProvisionsResponse::decode_vec(&value).unwrap_test();

let expected_provisions = Decimal256::zero();

assert_eq!(expected_provisions, annual_provisions);
}

#[test]
fn query_provision_after_month_without_staking() {
let mut node = set_node();

let mut timestamp = Timestamp::UNIX_EPOCH;
while timestamp.timestamp_seconds() <= TimestampSeconds::try_from(2_628_000).unwrap_test() {
let _ = node.step(vec![], Timestamp::UNIX_EPOCH);

timestamp = timestamp
.checked_add(Duration::new_from_secs(5))
.unwrap_test();
}

let q = QueryAnnualProvisionsRequest {};
let ResponseQuery { value, .. } = node.query(RequestQuery {
data: q.encode_vec().into(),
path: QueryAnnualProvisionsRequest::QUERY_URL.to_owned(),
height: 100, // todo
prove: false,
});

let QueryAnnualProvisionsResponse { annual_provisions } =
QueryAnnualProvisionsResponse::decode_vec(&value).unwrap_test();

let expected_provisions = Decimal256::zero();

assert_eq!(expected_provisions, annual_provisions);
}
17 changes: 11 additions & 6 deletions x/mint/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::str::FromStr;

use gears::{
application::handlers::node::ModuleInfo,
baseapp::BaseApp,
derive::{ParamsKeys, StoreKeys},
extensions::testing::UnwrapTesting,
store::database::MemDB,
utils::node::{GenesisSource, MockApplication, MockNode, MockOptions},
types::denom::Denom,
utils::node::{init_node, GenesisSource, MockApplication, MockNode, MockOptions},
x::{
keepers::{
bank::{BalancesKeeper, BankKeeper},
Expand Down Expand Up @@ -88,7 +92,7 @@ pub fn set_node() -> MockNode<
SubspaceKey::Mint,
);

let _opt = MockOptions::<
let opt = MockOptions::<
SubspaceKey,
MintAbciHandler<_, _, _, _, _, MintModuleInfo>,
MintGenesis,
Expand All @@ -97,10 +101,11 @@ pub fn set_node() -> MockNode<
.baseapp_sbs_key(SubspaceKey::BaseApp)
.genesis(GenesisSource::Default);

// init_node(opt)
todo!()
init_node(opt).0
}



#[derive(Debug, Clone)]
pub struct MockStakingKeeper;

Expand All @@ -112,7 +117,7 @@ impl MintingStakingKeeper<SpaceKey, Modules> for MockStakingKeeper {
&self,
_ctx: &CTX,
) -> Result<gears::types::denom::Denom, gears::types::store::gas::errors::GasStoreErrors> {
todo!()
Ok(Denom::from_str("uatom").unwrap_test())
}

fn total_bonded_tokens<
Expand Down Expand Up @@ -243,6 +248,6 @@ impl BalancesKeeper<SpaceKey, Modules> for MockBankKeeper {
Option<gears::types::base::coin::UnsignedCoin>,
gears::types::store::gas::errors::GasStoreErrors,
> {
todo!()
Ok(None)
}
}

0 comments on commit 335c7a7

Please sign in to comment.