Skip to content

Commit

Permalink
additional rewarding-related unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Oct 10, 2024
1 parent d53f841 commit 07c9501
Show file tree
Hide file tree
Showing 15 changed files with 565 additions and 207 deletions.
27 changes: 27 additions & 0 deletions common/cosmwasm-smart-contracts/contracts-common/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use cosmwasm_std::{BankMsg, Coin, CosmosMsg, Response};

pub trait ResponseExt<T> {
fn add_optional_message(self, msg: Option<impl Into<CosmosMsg<T>>>) -> Self;

fn send_tokens(self, to: impl AsRef<str>, amount: Coin) -> Self;
}

impl<T> ResponseExt<T> for Response<T> {
fn add_optional_message(self, msg: Option<impl Into<CosmosMsg<T>>>) -> Self {
if let Some(msg) = msg {
self.add_message(msg)
} else {
self
}
}

fn send_tokens(self, to: impl AsRef<str>, amount: Coin) -> Self {
self.add_message(BankMsg::Send {
to_address: to.as_ref().to_string(),
amount: vec![amount],
})
}
}
2 changes: 2 additions & 0 deletions common/cosmwasm-smart-contracts/contracts-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pub mod events;
pub mod signing;
pub mod types;

pub mod helpers;

pub use types::*;
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ impl RewardedSetMetadata {
}
}

// important note: this currently does **NOT** include gateway role as they're not being rewarded
// and the metadata is primarily used for data lookup during epoch transition
pub fn highest_rewarded_id(&self) -> NodeId {
let mut highest = 0;
if self.layer1_metadata.highest_id > highest {
Expand All @@ -206,6 +204,12 @@ impl RewardedSetMetadata {
if self.layer3_metadata.highest_id > highest {
highest = self.layer3_metadata.highest_id;
}
if self.entry_gateway_metadata.highest_id > highest {
highest = self.entry_gateway_metadata.highest_id;
}
if self.exit_gateway_metadata.highest_id > highest {
highest = self.exit_gateway_metadata.highest_id;
}
if self.standby_metadata.highest_id > highest {
highest = self.standby_metadata.highest_id;
}
Expand Down
4 changes: 4 additions & 0 deletions common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub struct RoleAssignment {
}

impl RoleAssignment {
pub fn new(role: Role, nodes: Vec<NodeId>) -> RoleAssignment {
RoleAssignment { role, nodes }
}

pub fn is_final_assignment(&self) -> bool {
self.role.is_standby()
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/mixnet/src/compat/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ mod tests {
let node_id = test.add_legacy_mixnode("owner2", None);
let sender = mock_info("owner2", &[]);
test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![node_id]);
test.force_change_mix_rewarded_set(vec![node_id]);
test.start_epoch_transition();
test.reward_with_distribution(node_id, active_params);

Expand Down Expand Up @@ -644,7 +644,7 @@ mod tests {
let node_id = test.add_dummy_nymnode("owner2", None);
let sender = mock_info("owner2", &[]);
test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![node_id]);
test.force_change_mix_rewarded_set(vec![node_id]);
test.start_epoch_transition();
test.reward_with_distribution(node_id, active_params);

Expand Down
2 changes: 1 addition & 1 deletion contracts/mixnet/src/delegations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod tests {
test.add_immediate_delegation(delegator, og_amount, mix_id);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
let dist1 = test.reward_with_distribution_ignore_state(mix_id, active_params);
test.skip_to_next_epoch_end();
let dist2 = test.reward_with_distribution_ignore_state(mix_id, active_params);
Expand Down
4 changes: 2 additions & 2 deletions contracts/mixnet/src/gateways/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use crate::mixnet_contract_settings::storage as mixnet_params_storage;
use crate::nodes::helpers::save_new_nymnode_with_id;
use crate::nodes::transactions::add_nym_node_inner;
use crate::support::helpers::ensure_epoch_in_progress_state;
use crate::support::helpers::AttachSendTokens;
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use mixnet_contract_common::error::MixnetContractError;
use mixnet_contract_common::events::{
new_gateway_config_update_event, new_gateway_unbonding_event, new_migrated_gateway_event,
};
use mixnet_contract_common::gateway::GatewayConfigUpdate;
use mixnet_contract_common::{Gateway, GatewayBondingPayload, NodeCostParams};
use nym_contracts_common::helpers::ResponseExt;
use nym_contracts_common::signing::MessageSignature;

pub(crate) fn try_add_gateway(
Expand Down Expand Up @@ -351,7 +351,7 @@ pub mod tests {
assert_eq!(&Addr::unchecked("bob"), first_node.owner());

// add a node owned by fred
let fred_identity = test.add_legacy_gateway("fred", None);
let (fred_identity, _) = test.add_legacy_gateway("fred", None);

// let's make sure we now have 2 nodes:
let nodes = queries::query_gateways_paged(test.deps(), None, None)
Expand Down
31 changes: 16 additions & 15 deletions contracts/mixnet/src/interval/pending_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use mixnet_contract_common::pending_events::{
};
use mixnet_contract_common::reward_params::{ActiveSetUpdate, IntervalRewardingParamsUpdate};
use mixnet_contract_common::{BlockHeight, Delegation, NodeId};
use nym_contracts_common::helpers::ResponseExt;

use crate::delegations;
use crate::delegations::storage as delegations_storage;
Expand All @@ -28,7 +29,7 @@ use crate::nodes::helpers::{cleanup_post_unbond_nym_node_storage, get_node_detai
use crate::nodes::storage as nymnodes_storage;
use crate::rewards::storage as rewards_storage;
use crate::rewards::storage::RewardingStorage;
use crate::support::helpers::{ensure_any_node_bonded, AttachSendTokens};
use crate::support::helpers::ensure_any_node_bonded;

pub(crate) trait ContractExecutableEvent {
// note: the error only means a HARD error like we failed to read from storage.
Expand Down Expand Up @@ -837,7 +838,7 @@ mod tests {
let active_params = test.active_node_params(100.0);

// perform some rewarding here to advance the unit delegation beyond the initial value
test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
test.skip_to_next_epoch_end();
test.reward_with_distribution_ignore_state(mix_id, active_params);
test.skip_to_next_epoch_end();
Expand Down Expand Up @@ -911,7 +912,7 @@ mod tests {
let active_params = test.active_node_params(100.0);

// perform some rewarding here to advance the unit delegation beyond the initial value
test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
test.skip_to_next_epoch_end();
test.reward_with_distribution_ignore_state(mix_id, active_params);
test.skip_to_next_epoch_end();
Expand Down Expand Up @@ -1001,7 +1002,7 @@ mod tests {
let active_params = test.active_node_params(100.0);

// perform some rewarding here to advance the unit delegation beyond the initial value
test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
test.skip_to_next_epoch_end();
test.reward_with_distribution_ignore_state(mix_id, active_params);
test.skip_to_next_epoch_end();
Expand Down Expand Up @@ -1135,7 +1136,7 @@ mod tests {
.unwrap();
let active_params = test.active_node_params(100.0);

test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
test.skip_to_next_epoch_end();
let dist1 = test.reward_with_distribution_ignore_state(mix_id, active_params);
test.skip_to_next_epoch_end();
Expand Down Expand Up @@ -1265,7 +1266,7 @@ mod tests {
test.add_immediate_delegation("carol", 111_111_111u128, mix_id_full_pledge);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);

let dist1 = test.reward_with_distribution_ignore_state(mix_id_repledge, active_params);
let dist2 =
Expand All @@ -1289,7 +1290,7 @@ mod tests {
test.add_immediate_delegation("carol", 111_111_111_000u128, mix_id_repledge);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge]);

let dist = test.reward_with_distribution_ignore_state(mix_id_repledge, active_params);

Expand Down Expand Up @@ -1327,7 +1328,7 @@ mod tests {
);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);

// go through few epochs of rewarding
for _ in 0..500 {
Expand Down Expand Up @@ -1356,7 +1357,7 @@ mod tests {
test.add_immediate_delegation("carol", 111_111_111_000u128, mix_id_repledge);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge]);

let mut cumulative_op_reward = Decimal::zero();
let mut cumulative_del_reward = Decimal::zero();
Expand Down Expand Up @@ -1405,7 +1406,7 @@ mod tests {
);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);

// go through few more epochs of rewarding
for _ in 0..500 {
Expand Down Expand Up @@ -1544,7 +1545,7 @@ mod tests {
test.add_immediate_delegation("carol", 111_111_111u128, mix_id_full_pledge);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);

let dist1 = test.reward_with_distribution_ignore_state(mix_id_repledge, active_params);
let dist2 =
Expand All @@ -1568,7 +1569,7 @@ mod tests {
test.add_immediate_delegation("carol", 111_111_111_000u128, mix_id_repledge);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge]);

let dist = test.reward_with_distribution_ignore_state(mix_id_repledge, active_params);

Expand Down Expand Up @@ -1606,7 +1607,7 @@ mod tests {
);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);

// go through few epochs of rewarding
for _ in 0..500 {
Expand Down Expand Up @@ -1635,7 +1636,7 @@ mod tests {
test.add_immediate_delegation("carol", 111_111_111_000u128, mix_id_repledge);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge]);

let mut cumulative_op_reward = Decimal::zero();
let mut cumulative_del_reward = Decimal::zero();
Expand Down Expand Up @@ -1684,7 +1685,7 @@ mod tests {
);

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);
test.force_change_mix_rewarded_set(vec![mix_id_repledge, mix_id_full_pledge]);

// go through few more epochs of rewarding
for _ in 0..500 {
Expand Down
2 changes: 1 addition & 1 deletion contracts/mixnet/src/interval/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ mod tests {
let mut test = TestSetup::new();
let rewarding_validator = test.rewarding_validator();

test.force_change_rewarded_set(vec![1, 2, 3, 4, 5]);
test.force_change_mix_rewarded_set(vec![1, 2, 3, 4, 5]);
test.skip_to_current_epoch_end();
let env = test.env();

Expand Down
4 changes: 2 additions & 2 deletions contracts/mixnet/src/rewards/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod tests {
assert_eq!(res.amount, Uint128::zero());

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
let dist1 = test.reward_with_distribution_ignore_state(mix_id, active_params);

test.skip_to_next_epoch_end();
Expand Down Expand Up @@ -309,7 +309,7 @@ mod tests {
assert_eq!(res.amount, Uint128::zero());

test.skip_to_next_epoch_end();
test.force_change_rewarded_set(vec![mix_id]);
test.force_change_mix_rewarded_set(vec![mix_id]);
let dist1 = test.reward_with_distribution_ignore_state(mix_id, active_params);

test.skip_to_next_epoch_end();
Expand Down
Loading

0 comments on commit 07c9501

Please sign in to comment.