Checking for valid dealer + blacklisting

This commit is contained in:
Jędrzej Stuczyński
2022-04-13 11:57:23 +01:00
parent 1a8058aff5
commit 9d2b7d6940
9 changed files with 95 additions and 3 deletions
@@ -2,3 +2,4 @@
// SPDX-License-Identifier: Apache-2.0
pub mod msg;
pub mod types;
@@ -5,6 +5,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct InstantiateMsg {}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
@@ -0,0 +1,38 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct Blacklisting {
reason: BlacklistingReason,
height: u64,
}
impl Display for Blacklisting {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"blacklisted at block height {}. reason given: {}",
self.height, self.height
)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum BlacklistingReason {
InactiveForConsecutiveEpochs,
}
impl Display for BlacklistingReason {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
BlacklistingReason::InactiveForConsecutiveEpochs => {
write!(f, "has been inactive for multiple consecutive epochs")
}
}
}
}
+3 -2
View File
@@ -221,6 +221,7 @@ dependencies = [
"config",
"cosmwasm-std",
"cosmwasm-storage",
"cw-storage-plus",
"serde",
"thiserror",
]
@@ -403,9 +404,9 @@ dependencies = [
[[package]]
name = "cw-storage-plus"
version = "0.13.1"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e8b7f9a758c030d375520df947323c052704f784561fc28dcaab4f988c50a30"
checksum = "9336ecef1e19d56cf6e3e932475fc6a3dee35eec5a386e07917a1d1ba6bb0e35"
dependencies = [
"cosmwasm-std",
"schemars",
+2 -1
View File
@@ -12,7 +12,8 @@ crate-type = ["cdylib", "rlib"]
coconut-dkg-common = { path = "../../common/cosmwasm-smart-contracts/coconut-dkg-contract" }
config = { path = "../../common/config"}
cosmwasm-std = "1.0.0-beta8"
cosmwasm-std = { version = "1.0.0-beta8", features = ["staking"] }
cosmwasm-storage = "1.0.0-beta8"
cw-storage-plus = "0.13.2"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = "1.0.23"
+7
View File
@@ -1,6 +1,7 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use coconut_dkg_common::types::Blacklisting;
use cosmwasm_std::StdError;
use thiserror::Error;
@@ -17,4 +18,10 @@ pub enum ContractError {
#[error("Wrong coin denomination, you must send {}", DENOM)]
WrongDenom,
#[error("This dealer has been blacklisted - {reason}")]
BlacklistedDealer { reason: Blacklisting },
#[error("This potential dealer is not a validator")]
NotAValidator,
}
+1
View File
@@ -7,6 +7,7 @@ use cosmwasm_std::{entry_point, Deps, DepsMut, Env, MessageInfo, QueryResponse,
mod error;
mod queries;
mod storage;
mod support;
mod transactions;
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use coconut_dkg_common::types::Blacklisting;
use cosmwasm_std::Addr;
use cw_storage_plus::Map;
pub(crate) const BLACKLISTED_DEALERS: Map<'_, &'_ Addr, Blacklisting> = Map::new("bld");
+34
View File
@@ -1,2 +1,36 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::storage::BLACKLISTED_DEALERS;
use crate::ContractError;
use cosmwasm_std::{ensure, Addr, Deps, DepsMut, Env, MessageInfo, Response, Storage};
// currently we only require that
// a) it's a validator
// b) it wasn't blacklisted
fn verify_dealer(deps: Deps<'_>, dealer: &Addr) -> Result<(), ContractError> {
if let Some(blacklisting) = BLACKLISTED_DEALERS.may_load(deps.storage, dealer)? {
return Err(ContractError::BlacklistedDealer {
reason: blacklisting,
});
}
let all_validators = deps.querier.query_all_validators()?;
if !all_validators
.iter()
.any(|validator| validator.address == dealer.as_ref())
{
return Err(ContractError::NotAValidator);
}
Ok(())
}
pub fn try_add_dealer(
deps: DepsMut<'_>,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
verify_dealer(deps.as_ref(), &info.sender)?;
todo!()
}