Introduced type aliases for mixnode and gateway keys (#650)

* Introduced type aliases for mixnode and gateway keys

* Ibid for error types

* Ibid for tests
This commit is contained in:
Jędrzej Stuczyński
2021-06-17 09:01:41 +01:00
committed by GitHub
parent e470ac4b0f
commit d03798629c
9 changed files with 72 additions and 64 deletions
+7 -6
View File
@@ -1,6 +1,7 @@
// due to code generated by JsonSchema
#![allow(clippy::field_reassign_with_default)]
use crate::IdentityKey;
use cosmwasm_std::Coin;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@@ -8,12 +9,12 @@ use std::fmt::Display;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
pub struct Delegation {
node_identity: String,
node_identity: IdentityKey,
amount: Coin,
}
impl Delegation {
pub fn new(node_identity: String, amount: Coin) -> Self {
pub fn new(node_identity: IdentityKey, amount: Coin) -> Self {
Delegation {
node_identity,
amount,
@@ -41,14 +42,14 @@ impl Display for Delegation {
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
pub struct PagedMixDelegationsResponse {
pub node_identity: String,
pub node_identity: IdentityKey,
pub delegations: Vec<Delegation>,
pub start_next_after: Option<String>,
}
impl PagedMixDelegationsResponse {
pub fn new(
node_identity: String,
node_identity: IdentityKey,
delegations: Vec<Delegation>,
start_next_after: Option<String>,
) -> Self {
@@ -62,14 +63,14 @@ impl PagedMixDelegationsResponse {
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
pub struct PagedGatewayDelegationsResponse {
pub node_identity: String,
pub node_identity: IdentityKey,
pub delegations: Vec<Delegation>,
pub start_next_after: Option<String>,
}
impl PagedGatewayDelegationsResponse {
pub fn new(
node_identity: String,
node_identity: IdentityKey,
delegations: Vec<Delegation>,
start_next_after: Option<String>,
) -> Self {
+5 -4
View File
@@ -1,6 +1,7 @@
// due to code generated by JsonSchema
#![allow(clippy::field_reassign_with_default)]
use crate::{IdentityKey, SphinxKey};
use cosmwasm_std::{Coin, HumanAddr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@@ -13,9 +14,9 @@ pub struct Gateway {
pub mix_host: String,
pub clients_host: String,
pub location: String,
pub sphinx_key: String,
pub sphinx_key: SphinxKey,
/// Base58 encoded ed25519 EdDSA public key of the gateway used to derive shared keys with clients
pub identity_key: String,
pub identity_key: IdentityKey,
pub version: String,
}
@@ -24,8 +25,8 @@ impl Gateway {
mix_host: String,
clients_host: String,
location: String,
sphinx_key: String,
identity_key: String,
sphinx_key: SphinxKey,
identity_key: IdentityKey,
version: String,
) -> Self {
Gateway {
+4
View File
@@ -17,3 +17,7 @@ pub struct LayerDistribution {
pub layer3: u64,
pub invalid: u64,
}
// type aliases for better reasoning about available data
pub type IdentityKey = String;
pub type SphinxKey = String;
+5 -4
View File
@@ -1,6 +1,7 @@
// due to code generated by JsonSchema
#![allow(clippy::field_reassign_with_default)]
use crate::{IdentityKey, SphinxKey};
use cosmwasm_std::{Coin, HumanAddr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@@ -13,9 +14,9 @@ pub struct MixNode {
pub host: String,
pub layer: u64,
pub location: String,
pub sphinx_key: String,
pub sphinx_key: SphinxKey,
/// Base58 encoded ed25519 EdDSA public key.
pub identity_key: String,
pub identity_key: IdentityKey,
pub version: String,
}
@@ -24,8 +25,8 @@ impl MixNode {
host: String,
layer: u64,
location: String,
sphinx_key: String,
identity_key: String,
sphinx_key: SphinxKey,
identity_key: IdentityKey,
version: String,
) -> Self {
MixNode {
+5 -4
View File
@@ -1,5 +1,6 @@
use crate::contract::DENOM;
use cosmwasm_std::{HumanAddr, StdError};
use mixnet_contract::IdentityKey;
use thiserror::Error;
/// Custom errors for contract failure conditions.
@@ -17,7 +18,7 @@ pub enum ContractError {
InsufficientMixNodeBond { received: u128, minimum: u128 },
#[error("Mixnode ({identity:?}) does not exist")]
MixNodeBondNotFound { identity: String },
MixNodeBondNotFound { identity: IdentityKey },
#[error(
"Not enough funds sent for gateway bond. (received {received:?}, minimum {minimum:?})"
@@ -25,7 +26,7 @@ pub enum ContractError {
InsufficientGatewayBond { received: u128, minimum: u128 },
#[error("Gateway ({identity:?}) does not exist")]
GatewayBondNotFound { identity: String },
GatewayBondNotFound { identity: IdentityKey },
#[error("{owner:?} does not seem to own any mixnodes")]
NoAssociatedMixNodeBond { owner: HumanAddr },
@@ -73,8 +74,8 @@ pub enum ContractError {
InvalidSender { owner: HumanAddr },
#[error("Could not find any delegation information associated with mixnode {identity:?}")]
NoMixnodeDelegationFound { identity: String },
NoMixnodeDelegationFound { identity: IdentityKey },
#[error("Could not find any delegation information associated with gateway {identity:?}")]
NoGatewayDelegationFound { identity: String },
NoGatewayDelegationFound { identity: IdentityKey },
}
+13 -13
View File
@@ -1,6 +1,6 @@
use crate::state::StateParams;
use cosmwasm_std::HumanAddr;
use mixnet_contract::{Gateway, MixNode};
use mixnet_contract::{Gateway, IdentityKey, MixNode};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@@ -21,29 +21,29 @@ pub enum HandleMsg {
UpdateStateParams(StateParams),
DelegateToMixnode {
mix_identity: String,
mix_identity: IdentityKey,
},
UndelegateFromMixnode {
mix_identity: String,
mix_identity: IdentityKey,
},
DelegateToGateway {
gateway_identity: String,
gateway_identity: IdentityKey,
},
UndelegateFromGateway {
gateway_identity: String,
gateway_identity: IdentityKey,
},
RewardMixnode {
identity: String,
identity: IdentityKey,
// percentage value in range 0-100
uptime: u32,
},
RewardGateway {
identity: String,
identity: IdentityKey,
// percentage value in range 0-100
uptime: u32,
},
@@ -54,10 +54,10 @@ pub enum HandleMsg {
pub enum QueryMsg {
GetMixNodes {
limit: Option<u32>,
start_after: Option<String>,
start_after: Option<IdentityKey>,
},
GetGateways {
start_after: Option<String>,
start_after: Option<IdentityKey>,
limit: Option<u32>,
},
OwnsMixnode {
@@ -68,21 +68,21 @@ pub enum QueryMsg {
},
StateParams {},
GetMixDelegations {
mix_identity: String,
mix_identity: IdentityKey,
start_after: Option<String>,
limit: Option<u32>,
},
GetMixDelegation {
mix_identity: String,
mix_identity: IdentityKey,
address: HumanAddr,
},
GetGatewayDelegations {
gateway_identity: String,
gateway_identity: IdentityKey,
start_after: Option<String>,
limit: Option<u32>,
},
GetGatewayDelegation {
gateway_identity: String,
gateway_identity: IdentityKey,
address: HumanAddr,
},
LayerDistribution {},
+19 -19
View File
@@ -10,7 +10,7 @@ use cosmwasm_std::Order;
use cosmwasm_std::StdResult;
use cosmwasm_std::{coin, HumanAddr};
use mixnet_contract::{
Delegation, GatewayBond, GatewayOwnershipResponse, LayerDistribution, MixNodeBond,
Delegation, GatewayBond, GatewayOwnershipResponse, IdentityKey, LayerDistribution, MixNodeBond,
MixOwnershipResponse, PagedGatewayDelegationsResponse, PagedGatewayResponse,
PagedMixDelegationsResponse, PagedResponse,
};
@@ -24,7 +24,7 @@ const DELEGATION_PAGE_DEFAULT_LIMIT: u32 = 500;
pub fn query_mixnodes_paged(
deps: Deps,
start_after: Option<String>,
start_after: Option<IdentityKey>,
limit: Option<u32>,
) -> StdResult<PagedResponse> {
let limit = limit
@@ -45,7 +45,7 @@ pub fn query_mixnodes_paged(
pub(crate) fn query_gateways_paged(
deps: Deps,
start_after: Option<String>,
start_after: Option<IdentityKey>,
limit: Option<u32>,
) -> StdResult<PagedGatewayResponse> {
let limit = limit
@@ -110,7 +110,7 @@ fn calculate_start_value(start_after: Option<String>) -> Option<Vec<u8>> {
pub(crate) fn query_mixnode_delegations_paged(
deps: Deps,
mix_identity: String,
mix_identity: IdentityKey,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<PagedMixDelegationsResponse> {
@@ -146,7 +146,7 @@ pub(crate) fn query_mixnode_delegations_paged(
// queries for delegation value of given address for particular node
pub(crate) fn query_mixnode_delegation(
deps: Deps,
mix_identity: String,
mix_identity: IdentityKey,
address: HumanAddr,
) -> Result<Delegation, ContractError> {
match mix_delegations_read(deps.storage, &mix_identity).may_load(address.as_bytes())? {
@@ -162,7 +162,7 @@ pub(crate) fn query_mixnode_delegation(
pub(crate) fn query_gateway_delegations_paged(
deps: Deps,
gateway_identity: String,
gateway_identity: IdentityKey,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<PagedGatewayDelegationsResponse> {
@@ -198,7 +198,7 @@ pub(crate) fn query_gateway_delegations_paged(
// queries for delegation value of given address for particular node
pub(crate) fn query_gateway_delegation(
deps: Deps,
gateway_identity: String,
gateway_identity: IdentityKey,
address: HumanAddr,
) -> Result<Delegation, ContractError> {
match gateway_delegations_read(deps.storage, &gateway_identity).may_load(address.as_bytes())? {
@@ -572,7 +572,7 @@ mod tests {
fn retrieval_obeys_limits() {
let mut deps = helpers::init_contract();
let limit = 2;
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
store_n_delegations(100, &mut deps.storage, &node_identity);
let page1 = query_mixnode_delegations_paged(
@@ -588,7 +588,7 @@ mod tests {
#[test]
fn retrieval_has_default_limit() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
store_n_delegations(
DELEGATION_PAGE_DEFAULT_LIMIT * 10,
&mut deps.storage,
@@ -607,7 +607,7 @@ mod tests {
#[test]
fn retrieval_has_max_limit() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
store_n_delegations(
DELEGATION_PAGE_DEFAULT_LIMIT * 10,
&mut deps.storage,
@@ -632,7 +632,7 @@ mod tests {
#[test]
fn pagination_works() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
mix_delegations(&mut deps.storage, &node_identity)
.save("1".as_bytes(), &Uint128(42))
@@ -713,7 +713,7 @@ mod tests {
#[test]
fn mix_deletion_query_returns_current_delegation_value() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
mix_delegations(&mut deps.storage, &node_identity)
.save("foo".as_bytes(), &Uint128(42))
@@ -728,7 +728,7 @@ mod tests {
#[test]
fn mix_deletion_query_returns_error_if_delegation_doesnt_exist() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
assert_eq!(
Err(ContractError::NoMixnodeDelegationFound {
@@ -781,7 +781,7 @@ mod tests {
fn retrieval_obeys_limits() {
let mut deps = helpers::init_contract();
let limit = 2;
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
store_n_delegations(100, &mut deps.storage, &node_identity);
let page1 = query_gateway_delegations_paged(
@@ -797,7 +797,7 @@ mod tests {
#[test]
fn retrieval_has_default_limit() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
store_n_delegations(
DELEGATION_PAGE_DEFAULT_LIMIT * 10,
&mut deps.storage,
@@ -816,7 +816,7 @@ mod tests {
#[test]
fn retrieval_has_max_limit() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
store_n_delegations(
DELEGATION_PAGE_DEFAULT_LIMIT * 10,
&mut deps.storage,
@@ -841,7 +841,7 @@ mod tests {
#[test]
fn pagination_works() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
gateway_delegations(&mut deps.storage, &node_identity)
.save("1".as_bytes(), &Uint128(42))
@@ -922,7 +922,7 @@ mod tests {
#[test]
fn gateway_deletion_query_returns_current_delegation_value() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
gateway_delegations(&mut deps.storage, &node_identity)
.save("foo".as_bytes(), &Uint128(42))
@@ -937,7 +937,7 @@ mod tests {
#[test]
fn gateway_deletion_query_returns_error_if_delegation_doesnt_exist() {
let mut deps = helpers::init_contract();
let node_identity: String = "foo".into();
let node_identity: IdentityKey = "foo".into();
assert_eq!(
Err(ContractError::NoGatewayDelegationFound {
+5 -5
View File
@@ -5,7 +5,7 @@ use cosmwasm_storage::{
bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket, ReadonlySingleton,
Singleton,
};
use mixnet_contract::{GatewayBond, LayerDistribution, MixNodeBond};
use mixnet_contract::{GatewayBond, IdentityKey, LayerDistribution, MixNodeBond};
// storage prefixes
// all of them must be unique and presumably not be a prefix of a different one
@@ -153,11 +153,11 @@ pub fn mixnodes_read(storage: &dyn Storage) -> ReadonlyBucket<MixNodeBond> {
}
// owner address -> node identity
pub fn mixnodes_owners(storage: &mut dyn Storage) -> Bucket<String> {
pub fn mixnodes_owners(storage: &mut dyn Storage) -> Bucket<IdentityKey> {
bucket(storage, PREFIX_MIXNODES_OWNERS)
}
pub fn mixnodes_owners_read(storage: &dyn Storage) -> ReadonlyBucket<String> {
pub fn mixnodes_owners_read(storage: &dyn Storage) -> ReadonlyBucket<IdentityKey> {
bucket_read(storage, PREFIX_MIXNODES_OWNERS)
}
@@ -289,11 +289,11 @@ pub fn gateways_read(storage: &dyn Storage) -> ReadonlyBucket<GatewayBond> {
}
// owner address -> node identity
pub fn gateways_owners(storage: &mut dyn Storage) -> Bucket<String> {
pub fn gateways_owners(storage: &mut dyn Storage) -> Bucket<IdentityKey> {
bucket(storage, PREFIX_GATEWAYS_OWNERS)
}
pub fn gateways_owners_read(storage: &dyn Storage) -> ReadonlyBucket<String> {
pub fn gateways_owners_read(storage: &dyn Storage) -> ReadonlyBucket<IdentityKey> {
bucket_read(storage, PREFIX_GATEWAYS_OWNERS)
}
+9 -9
View File
@@ -12,7 +12,7 @@ use crate::storage::{
use cosmwasm_std::{
attr, coins, BankMsg, Coin, Decimal, DepsMut, Env, HandleResponse, MessageInfo, Uint128,
};
use mixnet_contract::{Gateway, GatewayBond, MixNode, MixNodeBond};
use mixnet_contract::{Gateway, GatewayBond, IdentityKey, MixNode, MixNodeBond};
fn validate_mixnode_bond(bond: &[Coin], minimum_bond: Uint128) -> Result<(), ContractError> {
// check if anything was put as bond
@@ -311,7 +311,7 @@ pub(crate) fn try_update_state_params(
pub(crate) fn try_reward_mixnode(
deps: DepsMut,
info: MessageInfo,
mix_identity: String,
mix_identity: IdentityKey,
uptime: u32,
) -> Result<HandleResponse, ContractError> {
let state = config_read(deps.storage).load().unwrap();
@@ -349,7 +349,7 @@ pub(crate) fn try_reward_mixnode(
pub(crate) fn try_reward_gateway(
deps: DepsMut,
info: MessageInfo,
gateway_identity: String,
gateway_identity: IdentityKey,
uptime: u32,
) -> Result<HandleResponse, ContractError> {
let state = config_read(deps.storage).load().unwrap();
@@ -405,7 +405,7 @@ fn validate_delegation_stake(delegation: &[Coin]) -> Result<(), ContractError> {
pub(crate) fn try_delegate_to_mixnode(
deps: DepsMut,
info: MessageInfo,
mix_identity: String,
mix_identity: IdentityKey,
) -> Result<HandleResponse, ContractError> {
// check if the delegation contains any funds of the appropriate denomination
validate_delegation_stake(&info.sent_funds)?;
@@ -437,7 +437,7 @@ pub(crate) fn try_remove_delegation_from_mixnode(
deps: DepsMut,
info: MessageInfo,
env: Env,
mix_identity: String,
mix_identity: IdentityKey,
) -> Result<HandleResponse, ContractError> {
let mut delegation_bucket = mix_delegations(deps.storage, &mix_identity);
let sender_bytes = info.sender.as_bytes();
@@ -469,7 +469,7 @@ pub(crate) fn try_remove_delegation_from_mixnode(
pub(crate) fn try_delegate_to_gateway(
deps: DepsMut,
info: MessageInfo,
gateway_identity: String,
gateway_identity: IdentityKey,
) -> Result<HandleResponse, ContractError> {
// check if the delegation contains any funds of the appropriate denomination
validate_delegation_stake(&info.sent_funds)?;
@@ -501,7 +501,7 @@ pub(crate) fn try_remove_delegation_from_gateway(
deps: DepsMut,
info: MessageInfo,
env: Env,
gateway_identity: String,
gateway_identity: IdentityKey,
) -> Result<HandleResponse, ContractError> {
let mut delegation_bucket = gateway_delegations(deps.storage, &gateway_identity);
let sender_bytes = info.sender.as_bytes();
@@ -1503,7 +1503,7 @@ pub mod tests {
let network_monitor_address = current_state.network_monitor_address;
let node_owner: HumanAddr = "node-owner".into();
let node_identity: String = "nodeidentity".into();
let node_identity: IdentityKey = "nodeidentity".into();
// errors out if executed by somebody else than network monitor
let info = mock_info("not-the-monitor", &[]);
@@ -1563,7 +1563,7 @@ pub mod tests {
let network_monitor_address = current_state.network_monitor_address;
let node_owner: HumanAddr = "node-owner".into();
let node_identity: String = "nodeidentity".into();
let node_identity: IdentityKey = "nodeidentity".into();
// errors out if executed by somebody else than network monitor
let info = mock_info("not-the-monitor", &[]);