Add proxy: Addr to MixNodeBond

This commit is contained in:
Drazen Urch
2021-11-20 22:15:55 +01:00
parent a44d5f98b2
commit 4352dddde5
9 changed files with 128 additions and 26 deletions
+8
View File
@@ -148,6 +148,7 @@ pub struct MixNodeBond {
pub block_height: u64,
pub mix_node: MixNode,
pub profit_margin_percent: Option<u8>,
pub proxy: Option<Addr>
}
impl MixNodeBond {
@@ -158,6 +159,7 @@ impl MixNodeBond {
block_height: u64,
mix_node: MixNode,
profit_margin_percent: Option<u8>,
proxy: Option<Addr>,
) -> Self {
MixNodeBond {
total_delegation: coin(0, &bond_amount.denom),
@@ -167,6 +169,7 @@ impl MixNodeBond {
block_height,
mix_node,
profit_margin_percent,
proxy
}
}
@@ -440,6 +443,7 @@ mod tests {
block_height: 100,
mix_node: mixnode_fixture(),
profit_margin_percent: Some(10),
proxy: None
};
let mix2 = MixNodeBond {
@@ -450,6 +454,7 @@ mod tests {
block_height: 120,
mix_node: mixnode_fixture(),
profit_margin_percent: Some(10),
proxy: None
};
let mix3 = MixNodeBond {
@@ -460,6 +465,7 @@ mod tests {
block_height: 120,
mix_node: mixnode_fixture(),
profit_margin_percent: Some(10),
proxy: None
};
let mix4 = MixNodeBond {
@@ -470,6 +476,7 @@ mod tests {
block_height: 120,
mix_node: mixnode_fixture(),
profit_margin_percent: Some(10),
proxy: None
};
let mix5 = MixNodeBond {
@@ -480,6 +487,7 @@ mod tests {
block_height: 120,
mix_node: mixnode_fixture(),
profit_margin_percent: Some(10),
proxy: None
};
// summary:
+10 -3
View File
@@ -60,11 +60,18 @@ pub enum ExecuteMsg {
},
DelegateToMixnodeOnBehalf {
mix_identity: IdentityKey,
delegate_addr: Addr,
delegate: Addr,
},
UnDelegateFromMixnodeOnBehalf {
UndelegateFromMixnodeOnBehalf {
mix_identity: IdentityKey,
delegate_addr: Addr,
delegate: Addr,
},
BondMixnodeOnBehalf {
mix_node: MixNode,
owner: Addr,
},
UnbondMixnodeOnBehalf {
owner: Addr,
},
}
+11 -5
View File
@@ -145,23 +145,29 @@ pub fn execute(
} => transactions::try_finish_mixnode_rewarding(deps, info, rewarding_interval_nonce),
ExecuteMsg::DelegateToMixnodeOnBehalf {
mix_identity,
delegate_addr,
delegate,
} => transactions::try_delegate_to_mixnode_on_behalf(
deps,
env,
info,
mix_identity,
delegate_addr,
delegate,
),
ExecuteMsg::UnDelegateFromMixnodeOnBehalf {
ExecuteMsg::UndelegateFromMixnodeOnBehalf {
mix_identity,
delegate_addr,
delegate,
} => transactions::try_remove_delegation_from_mixnode_on_behalf(
deps,
info,
mix_identity,
delegate_addr,
delegate,
),
ExecuteMsg::BondMixnodeOnBehalf { mix_node, owner } => {
transactions::try_add_mixnode_on_behalf(deps, env, info, mix_node, owner)
}
ExecuteMsg::UnbondMixnodeOnBehalf { owner } => {
transactions::try_remove_mixnode_on_behalf(deps, env, info, owner)
}
}
}
+1
View File
@@ -460,6 +460,7 @@ mod tests {
..mix_node_fixture()
},
profit_margin_percent: Some(10),
proxy: None
};
mixnodes(&mut storage)
+1
View File
@@ -134,6 +134,7 @@ pub mod helpers {
12_345,
mix_node,
None,
None
)
}
+51 -10
View File
@@ -59,13 +59,42 @@ fn validate_mixnode_bond(bond: &[Coin], minimum_bond: Uint128) -> Result<(), Con
Ok(())
}
pub(crate) fn try_add_mixnode(
pub fn try_add_mixnode_on_behalf(
deps: DepsMut,
env: Env,
info: MessageInfo,
mix_node: MixNode,
owner: Addr,
) -> Result<Response, ContractError> {
let proxy = info.sender.to_owned();
if proxy != VESTING_CONTRACT_ADDR {
return Err(ContractError::Unauthorized);
}
_try_add_mixnode(deps, env, info, mix_node, owner, None)
}
pub fn try_add_mixnode(
deps: DepsMut,
env: Env,
info: MessageInfo,
mix_node: MixNode,
) -> Result<Response, ContractError> {
let sender_bytes = info.sender.as_bytes();
let owner = info.sender.to_owned();
_try_add_mixnode(deps, env, info, mix_node, owner, None)
}
fn _try_add_mixnode(
deps: DepsMut,
env: Env,
info: MessageInfo,
mix_node: MixNode,
owner: Addr,
proxy: Option<Addr>,
) -> Result<Response, ContractError> {
let sender_bytes = owner.as_bytes();
// if the client has an active bonded gateway, don't allow mixnode bonding
if gateways_owners_read(deps.storage)
@@ -88,7 +117,7 @@ pub(crate) fn try_add_mixnode(
if let Some(existing_bond) =
mixnodes_read(deps.storage).may_load(mix_node.identity_key.as_bytes())?
{
if existing_bond.owner != info.sender {
if existing_bond.owner != owner {
return Err(ContractError::DuplicateMixnode {
owner: existing_bond.owner,
});
@@ -103,11 +132,12 @@ pub(crate) fn try_add_mixnode(
let mut bond = MixNodeBond::new(
info.funds[0].clone(),
info.sender.clone(),
owner.clone(),
layer,
env.block.height,
mix_node,
None,
proxy,
);
// this might potentially require more gas if a significant number of delegations was there
@@ -130,6 +160,15 @@ pub(crate) fn try_add_mixnode(
})
}
pub fn try_remove_mixnode_on_behalf(
deps: DepsMut,
env: Env,
info: MessageInfo,
owner: Addr,
) -> Result<Response, ContractError> {
unimplemented!()
}
pub(crate) fn try_remove_mixnode(
deps: DepsMut,
info: MessageInfo,
@@ -670,7 +709,7 @@ pub(crate) fn try_delegate_to_mixnode_on_behalf(
env: Env,
info: MessageInfo,
mix_identity: IdentityKey,
delegate_addr: Addr,
delegate: Addr,
) -> Result<Response, ContractError> {
// check if the delegation contains any funds of the appropriate denomination
validate_delegation_stake(&info.funds)?;
@@ -686,7 +725,7 @@ pub(crate) fn try_delegate_to_mixnode_on_behalf(
deps,
env,
mix_identity,
&delegate_addr,
&delegate,
amount,
Some(proxy_address),
)
@@ -696,7 +735,7 @@ fn _try_delegate_to_mixnode(
deps: DepsMut,
env: Env,
mix_identity: IdentityKey,
delegate_addr: &Addr,
delegate: &Addr,
amount: Uint128,
proxy_address: Option<Addr>,
) -> Result<Response, ContractError> {
@@ -717,14 +756,14 @@ fn _try_delegate_to_mixnode(
let mut delegation_bucket = mix_delegations(deps.storage, &mix_identity);
// We need to differentiate proxy delefations from direct delegations with the same owner
let sender_bytes = if let Some(proxy_address) = &proxy_address {
delegate_addr
delegate
.as_bytes()
.iter()
.zip(proxy_address.as_bytes())
.map(|(x, y)| x ^ y)
.collect()
} else {
delegate_addr.as_bytes().to_vec()
delegate.as_bytes().to_vec()
};
// write the delegation
@@ -736,7 +775,7 @@ fn _try_delegate_to_mixnode(
let new_delegation = RawDelegationData::new(new_amount, env.block.height, proxy_address);
delegation_bucket.save(&sender_bytes, &new_delegation)?;
reverse_mix_delegations(deps.storage, delegate_addr).save(mix_identity.as_bytes(), &())?;
reverse_mix_delegations(deps.storage, delegate).save(mix_identity.as_bytes(), &())?;
Ok(Response::default())
}
@@ -2169,6 +2208,7 @@ pub mod tests {
..mix_node_fixture()
},
profit_margin_percent: Some(10),
proxy: None,
};
mixnodes(deps.as_mut().storage)
@@ -2458,6 +2498,7 @@ pub mod tests {
..mix_node_fixture()
},
profit_margin_percent: Some(10),
proxy: None,
};
mixnodes(deps.as_mut().storage)
+33 -3
View File
@@ -52,12 +52,42 @@ pub fn execute(
ExecuteMsg::WithdrawVestedCoins { amount } => {
try_withdraw_vested_coins(amount, env, info, deps)
}
ExecuteMsg::TrackUndelegation { address, mix_identity, amount } => {
try_track_undelegation(address, mix_identity, amount, deps)
}
ExecuteMsg::TrackUndelegation {
address,
mix_identity,
amount,
} => try_track_undelegation(address, mix_identity, amount, deps),
ExecuteMsg::BondMixnode {
mix_identity,
amount,
} => try_bond_mixnode(mix_identity, amount, info, env, deps),
ExecuteMsg::UnbondMixnode {
mix_identity,
amount,
} => try_unbond_mixnode(mix_identity, amount, info, env, deps),
}
}
fn try_bond_mixnode(
mix_identity: IdentityKey,
amount: Coin,
info: MessageInfo,
env: Env,
deps: DepsMut,
) -> Result<Response, ContractError> {
unimplemented!()
}
fn try_unbond_mixnode(
mix_identity: IdentityKey,
amount: Coin,
info: MessageInfo,
env: Env,
deps: DepsMut,
) -> Result<Response, ContractError> {
unimplemented!()
}
pub fn try_withdraw_vested_coins(
amount: Coin,
env: Env,
+10 -2
View File
@@ -1,4 +1,4 @@
use cosmwasm_std::{Coin, Timestamp, Addr};
use cosmwasm_std::{Addr, Coin, Timestamp};
use mixnet_contract::IdentityKey;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@@ -27,7 +27,15 @@ pub enum ExecuteMsg {
address: Addr,
mix_identity: IdentityKey,
amount: Coin,
}
},
BondMixnode {
mix_identity: IdentityKey,
amount: Coin,
},
UnbondMixnode {
mix_identity: IdentityKey,
amount: Coin,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
+3 -3
View File
@@ -238,7 +238,7 @@ impl DelegationAccount for PeriodicVestingAccount {
if coin.amount < self.get_balance(storage) {
let msg = MixnetExecuteMsg::DelegateToMixnodeOnBehalf {
mix_identity: mix_identity.clone(),
delegate_addr: self.address.clone(),
delegate: self.address.clone(),
};
let messages =
vec![
@@ -265,9 +265,9 @@ impl DelegationAccount for PeriodicVestingAccount {
&self,
mix_identity: IdentityKey,
) -> Result<Response, ContractError> {
let msg = MixnetExecuteMsg::UnDelegateFromMixnodeOnBehalf {
let msg = MixnetExecuteMsg::UndelegateFromMixnodeOnBehalf {
mix_identity,
delegate_addr: self.address.clone(),
delegate: self.address.clone(),
};
let messages = vec![wasm_execute(
DEFAULT_MIXNET_CONTRACT_ADDRESS,