Skip to content

Commit

Permalink
tests to query inflation with tokens changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NYBACHOK committed Oct 29, 2024
1 parent 335c7a7 commit 57a1b13
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 29 deletions.
40 changes: 37 additions & 3 deletions x/mint/tests/abci_init.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use gears::tendermint::types::time::timestamp::Timestamp;
use utils::set_node;
use std::str::FromStr;

use gears::{
extensions::testing::UnwrapTesting,
tendermint::types::time::timestamp::Timestamp,
types::{base::coin::UnsignedCoin, decimal256::Decimal256, uint::Uint256},
};
use utils::{set_node, MockBankKeeper, MockStakingKeeper};

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

#[test]
fn test_init_and_few_blocks() {
let mut node = set_node();
let mut node = set_node(None, None);

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

Expand All @@ -23,3 +29,31 @@ fn test_init_and_few_blocks() {
"548e264ab9174c4c366abd27c0d0c888fa591977145d314f9c548e60160f01d4"
);
}

#[test]
fn test_init_and_few_blocks_with_tokens() {
let mut node = set_node(
Some(MockBankKeeper::new(
UnsignedCoin::from_str("1000000000000uatom").unwrap_test(),
None,
)),
Some(MockStakingKeeper::new(Decimal256::new(Uint256::from(
1000000000_u64,
)))),
);

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

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

node.skip_steps(100);

let app_hash = &node.step(vec![], Timestamp::UNIX_EPOCH).app_hash;
assert_eq!(
data_encoding::HEXLOWER.encode(app_hash),
"15bd8b0fd25f63bb734e67bb355c189c75836a9331066975de8c095822529c09"
);
}
110 changes: 102 additions & 8 deletions x/mint/tests/query_inflation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::{
str::FromStr,
sync::{Arc, RwLock},
};

use gears::{
core::Protobuf,
extensions::testing::UnwrapTesting,
extensions::{lock::AcquireRwLock, testing::UnwrapTesting},
tendermint::types::{
request::query::RequestQuery,
response::ResponseQuery,
Expand All @@ -9,17 +14,47 @@ use gears::{
timestamp::{Timestamp, TimestampSeconds},
},
},
types::decimal256::Decimal256,
types::{base::coin::UnsignedCoin, decimal256::Decimal256, uint::Uint256},
};
use mint::types::query::{request::QueryInflationRequest, response::QueryInflationResponse};
use utils::set_node;
use utils::{set_node, MockBankKeeper, MockStakingKeeper};

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

#[test]
fn query_inflation_after_init_without_staking_supply() {
let mut node = set_node(None, None);

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_init() {
let mut node = set_node();
let mut node = set_node(
Some(MockBankKeeper::new(
UnsignedCoin::from_str("1000000000000uatom").unwrap_test(),
None,
)),
Some(MockStakingKeeper::new(Decimal256::new(Uint256::from(
1000000000_u64,
)))),
);

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

Expand All @@ -40,13 +75,13 @@ fn query_inflation_after_init() {
}

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

// Well. I simulate chain which runs for year and each block takes 5 seconds
// Well. I simulate chain which runs for month 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);
let _ = node.step(vec![], timestamp);

timestamp = timestamp
.checked_add(Duration::new_from_secs(5))
Expand All @@ -68,3 +103,62 @@ fn query_inflation_after_month_without_staking() {

assert_eq!(expected_inflation, inflation);
}

#[test]
fn query_inflation_after_month() {
let total_supply = Arc::new(RwLock::new(Some(
UnsignedCoin::from_str("1000000000000uatom").unwrap_test(),
)));

let mut node = set_node(
Some(MockBankKeeper {
expected_mint_amount: None,
supply: total_supply.clone(),
}),
Some(MockStakingKeeper::new(Decimal256::new(Uint256::from(
1000000000_u64,
)))),
);

// Well. I simulate chain which runs for month 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);

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

match &mut *total_supply.acquire_write() {
Some(supply) => {
supply.amount = supply
.amount
.checked_add(Uint256::from(10000000000000000_u64))
.unwrap_test();
}
None => (),
};

let _ = node.step(
vec![],
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);
}
4 changes: 2 additions & 2 deletions x/mint/tests/query_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod utils;

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

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

Expand All @@ -43,7 +43,7 @@ fn query_provision_after_init() {

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

let mut timestamp = Timestamp::UNIX_EPOCH;
while timestamp.timestamp_seconds() <= TimestampSeconds::try_from(2_628_000).unwrap_test() {
Expand Down
67 changes: 51 additions & 16 deletions x/mint/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::str::FromStr;
use std::{
str::FromStr,
sync::{Arc, RwLock},
};

use gears::{
application::handlers::node::ModuleInfo,
baseapp::BaseApp,
derive::{ParamsKeys, StoreKeys},
extensions::testing::UnwrapTesting,
extensions::{lock::AcquireRwLock, testing::UnwrapTesting},
store::database::MemDB,
types::denom::Denom,
types::{
base::{coin::UnsignedCoin, coins::UnsignedCoins},
decimal256::Decimal256,
denom::Denom,
},
utils::node::{init_node, GenesisSource, MockApplication, MockNode, MockOptions},
x::{
keepers::{
Expand Down Expand Up @@ -65,7 +72,10 @@ impl ModuleInfo for MintModuleInfo {
const NAME: &'static str = "mint";
}

pub fn set_node() -> MockNode<
pub fn set_node(
bank_mock: Option<MockBankKeeper>,
staking_mock: Option<MockStakingKeeper>,
) -> MockNode<
BaseApp<
MemDB,
SubspaceKey,
Expand All @@ -84,8 +94,8 @@ pub fn set_node() -> MockNode<
let handler = MintAbciHandler::new(
MintKeeper::new(
SpaceKey::Mint,
MockStakingKeeper,
MockBankKeeper,
staking_mock.unwrap_or_default(),
bank_mock.unwrap_or_default(),
Modules::Mint,
Modules::FeeCollector,
),
Expand All @@ -104,10 +114,18 @@ pub fn set_node() -> MockNode<
init_node(opt).0
}

#[derive(Debug, Clone, Default)]
pub struct MockStakingKeeper {
pub total_bonded_tokens: Arc<RwLock<Decimal256>>,
}


#[derive(Debug, Clone)]
pub struct MockStakingKeeper;
impl MockStakingKeeper {
pub fn new(total_bonded_tokens: Decimal256) -> Self {
Self {
total_bonded_tokens: Arc::new(RwLock::new(total_bonded_tokens)),
}
}
}

impl MintingStakingKeeper<SpaceKey, Modules> for MockStakingKeeper {
fn staking_denom<
Expand All @@ -130,12 +148,24 @@ impl MintingStakingKeeper<SpaceKey, Modules> for MockStakingKeeper {
gears::types::decimal256::Decimal256,
gears::types::store::gas::errors::GasStoreErrors,
> {
todo!()
Ok(self.total_bonded_tokens.acquire_read().clone())
}
}

#[derive(Debug, Clone)]
pub struct MockBankKeeper;
#[derive(Debug, Clone, Default)]
pub struct MockBankKeeper {
pub expected_mint_amount: Option<Arc<RwLock<UnsignedCoins>>>,
pub supply: Arc<RwLock<Option<UnsignedCoin>>>,
}

impl MockBankKeeper {
pub fn new(supply: UnsignedCoin, expected_mint: Option<UnsignedCoins>) -> Self {
Self {
supply: Arc::new(RwLock::new(Some(supply))),
expected_mint_amount: expected_mint.map(|this| Arc::new(RwLock::new(this))),
}
}
}

impl MintingBankKeeper<SpaceKey, Modules> for MockBankKeeper {
fn mint_coins<
Expand All @@ -145,9 +175,14 @@ impl MintingBankKeeper<SpaceKey, Modules> for MockBankKeeper {
&self,
_ctx: &mut CTX,
_module: &Modules,
_amount: gears::types::base::coins::UnsignedCoins,
amount: gears::types::base::coins::UnsignedCoins,
) -> Result<(), gears::x::errors::BankKeeperError> {
todo!()
match &self.expected_mint_amount {
Some(exp_amount) => assert_eq!(exp_amount.acquire_read().clone(), amount),
None => (),
}

Ok(())
}
}

Expand Down Expand Up @@ -188,7 +223,7 @@ impl BankKeeper<SpaceKey, Modules> for MockBankKeeper {
_recepient_pool: &Modules,
_amount: gears::types::base::coins::UnsignedCoins,
) -> Result<(), gears::x::errors::BankKeeperError> {
todo!()
Ok(())
}

fn denom_metadata<
Expand Down Expand Up @@ -248,6 +283,6 @@ impl BalancesKeeper<SpaceKey, Modules> for MockBankKeeper {
Option<gears::types::base::coin::UnsignedCoin>,
gears::types::store::gas::errors::GasStoreErrors,
> {
Ok(None)
Ok(self.supply.acquire_read().clone())
}
}

0 comments on commit 57a1b13

Please sign in to comment.