Skip to content

Commit

Permalink
Feature/field renaming (nymtech#1654)
Browse files Browse the repository at this point in the history
* Replaced serde renames to aliases

Ideally I would have removed all serde macros, but then it would have broken existing QA deployments - perhaps we should do it later

* Renamed 'node_id' in Delegation to 'mix_id'

* Further renamings of 'node_id' to 'mix_id' in various places
  • Loading branch information
jstuczyn authored Sep 23, 2022
1 parent 996f0bf commit 879ce3f
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn print_delegations(delegations: Vec<Delegation>, client: &SigningClientW
for delegation in delegations {
table.add_row(vec![
to_iso_timestamp(delegation.height as u32, client).await,
delegation.node_id.to_string(),
delegation.mix_id.to_string(),
pretty_cosmwasm_coin(&delegation.amount),
delegation
.proxy
Expand Down
19 changes: 10 additions & 9 deletions common/cosmwasm-smart-contracts/mixnet-contract/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ pub struct Delegation {
pub owner: Addr,

/// Id of the MixNode that this delegation was performed against.
pub node_id: NodeId,
#[serde(alias = "node_id")]
pub mix_id: NodeId,

// Note to UI/UX devs: there's absolutely no point in displaying this value to the users,
// it would serve them no purpose. It's only used for calculating rewards
/// Value of the "unit delegation" associated with the mixnode at the time of delegation.
#[serde(rename = "crr")]
#[serde(alias = "crr")]
pub cumulative_reward_ratio: Decimal,

/// Original delegation amount. Note that it is never mutated as delegation accumulates rewards.
Expand All @@ -55,15 +56,15 @@ pub struct Delegation {
impl Delegation {
pub fn new(
owner: Addr,
node_id: NodeId,
mix_id: NodeId,
cumulative_reward_ratio: Decimal,
amount: Coin,
height: u64,
proxy: Option<Addr>,
) -> Self {
Delegation {
owner,
node_id,
mix_id,
cumulative_reward_ratio,
amount,
height,
Expand All @@ -72,20 +73,20 @@ impl Delegation {
}

pub fn generate_storage_key(
node_id: NodeId,
mix_id: NodeId,
owner_address: &Addr,
proxy: Option<&Addr>,
) -> StorageKey {
(node_id, generate_owner_storage_subkey(owner_address, proxy))
(mix_id, generate_owner_storage_subkey(owner_address, proxy))
}

// this function might seem a bit redundant, but I'd rather explicitly keep it around in case
// some types change in the future
pub fn generate_storage_key_with_subkey(
node_id: NodeId,
mix_id: NodeId,
owner_proxy_subkey: OwnerProxySubKey,
) -> StorageKey {
(node_id, owner_proxy_subkey)
(mix_id, owner_proxy_subkey)
}

pub fn dec_amount(&self) -> Decimal {
Expand All @@ -99,7 +100,7 @@ impl Delegation {
}

pub fn storage_key(&self) -> StorageKey {
Self::generate_storage_key(self.node_id, &self.owner, self.proxy.as_ref())
Self::generate_storage_key(self.mix_id, &self.owner, self.proxy.as_ref())
}
}

Expand Down
24 changes: 12 additions & 12 deletions common/cosmwasm-smart-contracts/mixnet-contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub enum MixnetContractError {
#[error("Not enough funds sent for node delegation. (received {received}, minimum {minimum})")]
InsufficientDelegation { received: Coin, minimum: Coin },

#[error("Mixnode ({id}) does not exist")]
MixNodeBondNotFound { id: NodeId },
#[error("Mixnode ({mix_id}) does not exist")]
MixNodeBondNotFound { mix_id: NodeId },

#[error("{owner} does not seem to own any mixnodes")]
NoAssociatedMixNodeBond { owner: Addr },
Expand Down Expand Up @@ -83,23 +83,23 @@ pub enum MixnetContractError {
epoch_end: i64,
},

#[error("Mixnode {node_id} has already been rewarded during the current rewarding epoch ({absolute_epoch_id})")]
#[error("Mixnode {mix_id} has already been rewarded during the current rewarding epoch ({absolute_epoch_id})")]
MixnodeAlreadyRewarded {
node_id: NodeId,
mix_id: NodeId,
absolute_epoch_id: u32,
},

#[error("Mixnode {node_id} hasn't been selected to the rewarding set in this epoch ({absolute_epoch_id})")]
#[error("Mixnode {mix_id} hasn't been selected to the rewarding set in this epoch ({absolute_epoch_id})")]
MixnodeNotInRewardedSet {
node_id: NodeId,
mix_id: NodeId,
absolute_epoch_id: u32,
},

#[error("Mixnode {node_id} is currently in the process of unbonding")]
MixnodeIsUnbonding { node_id: NodeId },
#[error("Mixnode {mix_id} is currently in the process of unbonding")]
MixnodeIsUnbonding { mix_id: NodeId },

#[error("Mixnode {node_id} has already unbonded")]
MixnodeHasUnbonded { node_id: NodeId },
#[error("Mixnode {mix_id} has already unbonded")]
MixnodeHasUnbonded { mix_id: NodeId },

#[error("The contract has ended up in a state that was deemed impossible: {comment}")]
InconsistentState { comment: String },
Expand Down Expand Up @@ -134,6 +134,6 @@ pub enum MixnetContractError {
#[error("Received unexpected value for the rewarded set. Got: {received}, expected at most: {expected}")]
UnexpectedRewardedSetSize { received: u32, expected: u32 },

#[error("Mixnode {node_id} appears multiple times in the provided rewarded set update!")]
DuplicateRewardedSetNode { node_id: NodeId },
#[error("Mixnode {mix_id} appears multiple times in the provided rewarded set update!")]
DuplicateRewardedSetNode { mix_id: NodeId },
}
44 changes: 22 additions & 22 deletions common/cosmwasm-smart-contracts/mixnet-contract/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub const DELEGATION_TARGET_KEY: &str = "delegation_target";
pub const DELEGATION_HEIGHT_KEY: &str = "delegation_latest_block_height";

// bonding/unbonding
pub const NODE_ID_KEY: &str = "node_id";
pub const MIX_ID_KEY: &str = "mix_id";
pub const NODE_IDENTITY_KEY: &str = "identity";
pub const ASSIGNED_LAYER_KEY: &str = "assigned_layer";

Expand Down Expand Up @@ -202,7 +202,7 @@ pub fn new_withdraw_operator_reward_event(
.add_attribute(OWNER_KEY, owner.as_str())
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
.add_attribute(AMOUNT_KEY, amount.to_string())
.add_attribute(NODE_ID_KEY, mix_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
}

pub fn new_withdraw_delegator_reward_event(
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn new_undelegation_event(delegator: &Addr, proxy: &Option<Addr>, mix_id: No
Event::new(MixnetEventType::Undelegation)
.add_attribute(DELEGATOR_KEY, delegator)
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
.add_attribute(NODE_ID_KEY, mix_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
}

pub fn new_pending_undelegation_event(
Expand All @@ -280,7 +280,7 @@ pub fn new_pending_undelegation_event(
Event::new(MixnetEventType::PendingUndelegation)
.add_attribute(DELEGATOR_KEY, delegator)
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
.add_attribute(NODE_ID_KEY, mix_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
}

pub fn new_gateway_bonding_event(
Expand Down Expand Up @@ -314,68 +314,68 @@ pub fn new_mixnode_bonding_event(
proxy: &Option<Addr>,
amount: &Coin,
identity: IdentityKeyRef<'_>,
node_id: NodeId,
mix_id: NodeId,
assigned_layer: Layer,
) -> Event {
// coin implements Display trait and we use that implementation here
Event::new(MixnetEventType::MixnodeBonding)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(NODE_IDENTITY_KEY, identity)
.add_attribute(OWNER_KEY, owner)
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
.add_attribute(ASSIGNED_LAYER_KEY, assigned_layer)
.add_attribute(AMOUNT_KEY, amount.to_string())
}

pub fn new_mixnode_unbonding_event(node_id: NodeId) -> Event {
Event::new(MixnetEventType::MixnodeUnbonding).add_attribute(NODE_ID_KEY, node_id.to_string())
pub fn new_mixnode_unbonding_event(mix_id: NodeId) -> Event {
Event::new(MixnetEventType::MixnodeUnbonding).add_attribute(MIX_ID_KEY, mix_id.to_string())
}

pub fn new_pending_mixnode_unbonding_event(
owner: &Addr,
proxy: &Option<Addr>,
identity: IdentityKeyRef<'_>,
node_id: NodeId,
mix_id: NodeId,
) -> Event {
Event::new(MixnetEventType::PendingMixnodeUnbonding)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(NODE_IDENTITY_KEY, identity)
.add_attribute(OWNER_KEY, owner)
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
}

pub fn new_mixnode_config_update_event(
node_id: NodeId,
mix_id: NodeId,
owner: &Addr,
proxy: &Option<Addr>,
update: &MixNodeConfigUpdate,
) -> Event {
Event::new(MixnetEventType::MixnodeConfigUpdate)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(OWNER_KEY, owner)
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
.add_attribute(UPDATED_MIXNODE_CONFIG_KEY, update.to_inline_json())
}

pub fn new_mixnode_pending_cost_params_update_event(
node_id: NodeId,
mix_id: NodeId,
owner: &Addr,
proxy: &Option<Addr>,
new_costs: &MixNodeCostParams,
) -> Event {
Event::new(MixnetEventType::PendingMixnodeCostParamsUpdate)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(OWNER_KEY, owner)
.add_optional_attribute(PROXY_KEY, proxy.as_ref())
.add_attribute(UPDATED_MIXNODE_COST_PARAMS_KEY, new_costs.to_inline_json())
}

pub fn new_mixnode_cost_params_update_event(
node_id: NodeId,
mix_id: NodeId,
new_costs: &MixNodeCostParams,
) -> Event {
Event::new(MixnetEventType::MixnodeCostParamsUpdate)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(UPDATED_MIXNODE_COST_PARAMS_KEY, new_costs.to_inline_json())
}

Expand Down Expand Up @@ -431,29 +431,29 @@ pub fn new_settings_update_event(
event
}

pub fn new_not_found_mix_operator_rewarding_event(interval: Interval, node_id: NodeId) -> Event {
pub fn new_not_found_mix_operator_rewarding_event(interval: Interval, mix_id: NodeId) -> Event {
Event::new(MixnetEventType::MixnodeRewarding)
.add_attribute(
INTERVAL_KEY,
interval.current_epoch_absolute_id().to_string(),
)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(NO_REWARD_REASON_KEY, BOND_NOT_FOUND_VALUE)
}

pub fn new_zero_uptime_mix_operator_rewarding_event(interval: Interval, node_id: NodeId) -> Event {
pub fn new_zero_uptime_mix_operator_rewarding_event(interval: Interval, mix_id: NodeId) -> Event {
Event::new(MixnetEventType::MixnodeRewarding)
.add_attribute(
INTERVAL_KEY,
interval.current_epoch_absolute_id().to_string(),
)
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(NO_REWARD_REASON_KEY, ZERO_PERFORMANCE_VALUE)
}

pub fn new_mix_rewarding_event(
interval: Interval,
node_id: NodeId,
mix_id: NodeId,
reward_distribution: RewardDistribution,
prior_delegates: Decimal,
prior_unit_delegation: Decimal,
Expand All @@ -465,7 +465,7 @@ pub fn new_mix_rewarding_event(
)
.add_attribute(PRIOR_DELEGATES_KEY, prior_delegates.to_string())
.add_attribute(PRIOR_UNIT_DELEGATION_KEY, prior_unit_delegation.to_string())
.add_attribute(NODE_ID_KEY, node_id.to_string())
.add_attribute(MIX_ID_KEY, mix_id.to_string())
.add_attribute(
OPERATOR_REWARD_KEY,
reward_distribution.operator.to_string(),
Expand Down
14 changes: 7 additions & 7 deletions common/cosmwasm-smart-contracts/mixnet-contract/src/mixnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,34 @@ impl MixNodeDetails {
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
pub struct MixNodeRewarding {
/// Information provided by the operator that influence the cost function.
#[serde(rename = "cp")]
#[serde(alias = "cp")]
pub cost_params: MixNodeCostParams,

/// Total pledge and compounded reward earned by the node operator.
#[serde(rename = "op")]
#[serde(alias = "op")]
pub operator: Decimal,

/// Total delegation and compounded reward earned by all node delegators.
#[serde(rename = "dg")]
#[serde(alias = "dg")]
pub delegates: Decimal,

/// Cumulative reward earned by the "unit delegation" since the block 0.
#[serde(rename = "tur")]
#[serde(alias = "tur")]
pub total_unit_reward: Decimal,

/// Value of the theoretical "unit delegation" that has delegated to this mixnode at block 0.
#[serde(rename = "ud")]
#[serde(alias = "ud")]
pub unit_delegation: Decimal,

/// Marks the epoch when this node was last rewarded so that we wouldn't accidentally attempt
/// to reward it multiple times in the same epoch.
#[serde(rename = "le")]
#[serde(alias = "le")]
pub last_rewarded_epoch: EpochId,

// technically we don't need that field to determine reward magnitude or anything
// but it saves on extra queries to determine if we're removing the final delegation
// (so that we could zero the field correctly)
#[serde(rename = "uqd")]
#[serde(alias = "uqd")]
pub unique_delegations: u32,
}

Expand Down
2 changes: 1 addition & 1 deletion common/types/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Delegation {
) -> Result<Self, TypesError> {
Ok(Delegation {
owner: delegation.owner.to_string(),
mix_id: delegation.node_id,
mix_id: delegation.mix_id,
amount: reg.attempt_convert_to_display_dec_coin(delegation.amount.into())?,
height: delegation.height,
proxy: delegation.proxy.map(|d| d.to_string()),
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 @@ -15,7 +15,7 @@ pub(crate) fn undelegate(
) -> Result<Coin, MixnetContractError> {
let tokens = mix_rewarding.undelegate(&delegation)?;

rewards_storage::MIXNODE_REWARDING.save(store, delegation.node_id, &mix_rewarding)?;
rewards_storage::MIXNODE_REWARDING.save(store, delegation.mix_id, &mix_rewarding)?;
storage::delegations().replace(store, delegation.storage_key(), None, Some(&delegation))?;

Ok(tokens)
Expand Down
Loading

0 comments on commit 879ce3f

Please sign in to comment.