Querying contract for the current(and past) set of dkg dealers

This commit is contained in:
Jędrzej Stuczyński
2022-05-10 15:05:21 +01:00
parent 7271d19afa
commit bd216453cc
14 changed files with 306 additions and 85 deletions
Generated
+1
View File
@@ -626,6 +626,7 @@ dependencies = [
name = "coconut-dkg-common"
version = "0.1.0"
dependencies = [
"cosmwasm-std",
"schemars",
"serde",
]
@@ -21,6 +21,9 @@ use crate::nymd::{
error::NymdError, CosmWasmClient, NymdClient, QueryNymdClient, SigningNymdClient,
};
use crate::nymd::traits::DkgClient;
use crate::nymd::SigningCosmWasmClient;
use coconut_dkg_common::types::DealerDetails;
#[cfg(feature = "nymd-client")]
use mixnet_contract_common::{
mixnode::DelegationEvent, ContractStateParams, Delegation, IdentityKey, Interval,
@@ -49,6 +52,8 @@ pub struct Config {
gateway_page_limit: Option<u32>,
mixnode_delegations_page_limit: Option<u32>,
rewarded_set_page_limit: Option<u32>,
#[cfg(feature = "dkg")]
dealers_page_limit: Option<u32>,
}
#[cfg(feature = "nymd-client")]
@@ -74,6 +79,8 @@ impl Config {
gateway_page_limit: None,
mixnode_delegations_page_limit: None,
rewarded_set_page_limit: None,
#[cfg(feature = "dkg")]
dealers_page_limit: None,
}
}
@@ -121,6 +128,8 @@ pub struct Client<C> {
gateway_page_limit: Option<u32>,
mixnode_delegations_page_limit: Option<u32>,
rewarded_set_page_limit: Option<u32>,
#[cfg(feature = "dkg")]
dealers_page_limit: Option<u32>,
// ideally they would have been read-only, but unfortunately rust doesn't have such features
pub validator_api: validator_api::Client,
@@ -155,7 +164,8 @@ impl Client<SigningNymdClient> {
mixnode_page_limit: config.mixnode_page_limit,
gateway_page_limit: config.gateway_page_limit,
mixnode_delegations_page_limit: config.mixnode_delegations_page_limit,
rewarded_set_page_limit: None,
rewarded_set_page_limit: config.rewarded_set_page_limit,
dealers_page_limit: config.dealers_page_limit,
validator_api: validator_api_client,
nymd: nymd_client,
})
@@ -216,6 +226,7 @@ impl Client<QueryNymdClient> {
gateway_page_limit: config.gateway_page_limit,
mixnode_delegations_page_limit: config.mixnode_delegations_page_limit,
rewarded_set_page_limit: config.rewarded_set_page_limit,
dealers_page_limit: config.dealers_page_limit,
validator_api: validator_api_client,
nymd: nymd_client,
})
@@ -745,3 +756,60 @@ impl ApiClient {
Ok(self.validator_api.get_coconut_verification_key().await?)
}
}
// dkg-related impl block
#[cfg(feature = "dkg")]
impl<C> Client<C>
where
C: SigningCosmWasmClient + Send + Sync,
{
pub async fn get_all_nymd_current_dealers(
&self,
) -> Result<Vec<DealerDetails>, ValidatorClientError>
where
C: CosmWasmClient + Sync,
{
let mut dealers = Vec::new();
let mut start_after = None;
loop {
let mut paged_response = self
.nymd
.get_current_dealers_paged(start_after.take(), self.mixnode_page_limit)
.await?;
dealers.append(&mut paged_response.dealers);
if let Some(start_after_res) = paged_response.start_next_after {
start_after = Some(start_after_res.into_string())
} else {
break;
}
}
Ok(dealers)
}
pub async fn get_all_nymd_past_dealers(
&self,
) -> Result<Vec<DealerDetails>, ValidatorClientError>
where
C: CosmWasmClient + Sync,
{
let mut dealers = Vec::new();
let mut start_after = None;
loop {
let mut paged_response = self
.nymd
.get_past_dealers_paged(start_after.take(), self.mixnode_page_limit)
.await?;
dealers.append(&mut paged_response.dealers);
if let Some(start_after_res) = paged_response.start_next_after {
start_after = Some(start_after_res.into_string())
} else {
break;
}
}
Ok(dealers)
}
}
@@ -5,6 +5,7 @@ use crate::nymd::cosmwasm_client::types::ExecuteResult;
use crate::nymd::error::NymdError;
use crate::nymd::{Fee, NymdClient, SigningCosmWasmClient};
use async_trait::async_trait;
use coconut_dkg_common::dealer::PagedDealerResponse;
use coconut_dkg_common::msg::ExecuteMsg as DkgExecuteMsg;
use coconut_dkg_common::msg::QueryMsg as DkgQueryMsg;
use coconut_dkg_common::types::Epoch;
@@ -12,6 +13,17 @@ use coconut_dkg_common::types::Epoch;
#[async_trait]
pub trait DkgClient {
async fn get_current_dkg_epoch(&self) -> Result<Epoch, NymdError>;
async fn get_current_dealers_paged(
&self,
start_after: Option<String>,
page_limit: Option<u32>,
) -> Result<PagedDealerResponse, NymdError>;
async fn get_past_dealers_paged(
&self,
start_after: Option<String>,
page_limit: Option<u32>,
) -> Result<PagedDealerResponse, NymdError>;
async fn submit_dealing_commitment(
&self,
epoch_id: u32,
@@ -32,6 +44,32 @@ where
.query_contract_smart(self.coconut_dkg_contract_address()?, &request)
.await
}
async fn get_current_dealers_paged(
&self,
start_after: Option<String>,
page_limit: Option<u32>,
) -> Result<PagedDealerResponse, NymdError> {
let request = DkgQueryMsg::GetCurrentDealers {
start_after,
limit: page_limit,
};
self.client
.query_contract_smart(self.coconut_dkg_contract_address()?, &request)
.await
}
async fn get_past_dealers_paged(
&self,
start_after: Option<String>,
page_limit: Option<u32>,
) -> Result<PagedDealerResponse, NymdError> {
let request = DkgQueryMsg::GetPastDealers {
start_after,
limit: page_limit,
};
self.client
.query_contract_smart(self.coconut_dkg_contract_address()?, &request)
.await
}
async fn submit_dealing_commitment(
&self,
@@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cosmwasm-std = "1.0.0-beta8"
schemars = "0.8"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
@@ -0,0 +1,107 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::types::{BlockHeight, EncodedBTEPublicKeyWithProof, EncodedEd25519PublicKey, NodeIndex};
use cosmwasm_std::Addr;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct DealerDetails {
pub address: Addr,
pub joined_at: BlockHeight,
pub left_at: Option<BlockHeight>,
pub blacklisting: Option<Blacklisting>,
pub ed25519_public_key: EncodedEd25519PublicKey,
pub bte_public_key_with_proof: EncodedBTEPublicKeyWithProof,
pub assigned_index: NodeIndex,
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct Blacklisting {
pub reason: BlacklistingReason,
pub height: BlockHeight,
pub expiration: Option<BlockHeight>,
}
impl Blacklisting {
pub fn has_expired(&self, current_block: BlockHeight) -> bool {
self.expiration
.map(|expiration| expiration <= current_block)
.unwrap_or_default()
}
}
impl Display for Blacklisting {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(expiration) = self.expiration {
write!(
f,
"blacklisted at block height {}. reason given: {}. Expires at: {}",
self.height, self.height, expiration
)
} else {
write!(
f,
"blacklisted at block height {}. reason given: {}",
self.height, self.height
)
}
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum BlacklistingReason {
InactiveForConsecutiveEpochs,
MalformedBTEPublicKey,
InvalidBTEPublicKey,
MalformedEd25519PublicKey,
Ed25519PossessionVerificationFailure,
}
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")
}
BlacklistingReason::MalformedBTEPublicKey => {
write!(f, "provided malformed BTE Public Key")
}
BlacklistingReason::InvalidBTEPublicKey => write!(f, "provided invalid BTE Public Key"),
BlacklistingReason::MalformedEd25519PublicKey => {
write!(f, "provided malformed ed25519 Public Key")
}
BlacklistingReason::Ed25519PossessionVerificationFailure => {
write!(
f,
"failed to verify possession of provided ed25519 Public Key"
)
}
}
}
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct PagedDealerResponse {
pub dealers: Vec<DealerDetails>,
pub per_page: usize,
pub start_next_after: Option<Addr>,
}
impl PagedDealerResponse {
pub fn new(
dealers: Vec<DealerDetails>,
per_page: usize,
start_next_after: Option<Addr>,
) -> Self {
PagedDealerResponse {
dealers,
per_page,
start_next_after,
}
}
}
@@ -1,5 +1,6 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
pub mod dealer;
pub mod msg;
pub mod types;
@@ -32,6 +32,14 @@ pub enum ExecuteMsg {
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetCurrentEpoch {},
GetCurrentDealers {
limit: Option<u32>,
start_after: Option<String>,
},
GetPastDealers {
limit: Option<u32>,
start_after: Option<String>,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
@@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
pub use crate::dealer::{Blacklisting, BlacklistingReason, DealerDetails};
pub type BlockHeight = u64;
pub type EncodedEd25519PublicKey = String;
pub type EncodedEd25519PublicKeyRef<'a> = &'a str;
@@ -11,84 +11,6 @@ pub type EncodedBTEPublicKeyWithProof = String;
pub type NodeIndex = u64;
pub type EpochId = u32;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct DealerDetails {
pub joined_at: BlockHeight,
pub left_at: Option<BlockHeight>,
pub blacklisting: Option<Blacklisting>,
pub ed25519_public_key: EncodedEd25519PublicKey,
pub bte_public_key_with_proof: EncodedBTEPublicKeyWithProof,
pub assigned_index: NodeIndex,
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct Blacklisting {
pub reason: BlacklistingReason,
pub height: BlockHeight,
pub expiration: Option<BlockHeight>,
}
impl Blacklisting {
pub fn has_expired(&self, current_block: BlockHeight) -> bool {
self.expiration
.map(|expiration| expiration <= current_block)
.unwrap_or_default()
}
}
impl Display for Blacklisting {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(expiration) = self.expiration {
write!(
f,
"blacklisted at block height {}. reason given: {}. Expires at: {}",
self.height, self.height, expiration
)
} else {
write!(
f,
"blacklisted at block height {}. reason given: {}",
self.height, self.height
)
}
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum BlacklistingReason {
InactiveForConsecutiveEpochs,
MalformedBTEPublicKey,
InvalidBTEPublicKey,
MalformedEd25519PublicKey,
Ed25519PossessionVerificationFailure,
}
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")
}
BlacklistingReason::MalformedBTEPublicKey => {
write!(f, "provided malformed BTE Public Key")
}
BlacklistingReason::InvalidBTEPublicKey => write!(f, "provided invalid BTE Public Key"),
BlacklistingReason::MalformedEd25519PublicKey => {
write!(f, "provided malformed ed25519 Public Key")
}
BlacklistingReason::Ed25519PossessionVerificationFailure => {
write!(
f,
"failed to verify possession of provided ed25519 Public Key"
)
}
}
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct Epoch {
+1
View File
@@ -241,6 +241,7 @@ dependencies = [
name = "coconut-dkg-common"
version = "0.1.0"
dependencies = [
"cosmwasm-std",
"schemars",
"serde",
]
@@ -1,2 +1,51 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::dealers::storage;
use crate::dealers::storage::DealersMap;
use coconut_dkg_common::dealer::PagedDealerResponse;
use cosmwasm_std::{Deps, Order, StdResult};
use cw_storage_plus::Bound;
fn query_dealers(
deps: Deps<'_>,
start_after: Option<String>,
limit: Option<u32>,
underlying_map: DealersMap<'_>,
) -> StdResult<PagedDealerResponse> {
let limit = limit
.unwrap_or(storage::DEALERS_PAGE_DEFAULT_LIMIT)
.min(storage::DEALERS_PAGE_MAX_LIMIT) as usize;
let addr = start_after
.map(|addr| deps.api.addr_validate(&addr))
.transpose()?;
let start = addr.as_ref().map(Bound::exclusive);
let dealers = underlying_map
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|res| res.map(|item| item.1))
.collect::<StdResult<Vec<_>>>()?;
let start_next_after = dealers.last().map(|dealer| dealer.address.clone());
Ok(PagedDealerResponse::new(dealers, limit, start_next_after))
}
pub fn query_current_dealers_paged(
deps: Deps<'_>,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<PagedDealerResponse> {
query_dealers(deps, start_after, limit, storage::current_dealers())
}
pub fn query_past_dealers_paged(
deps: Deps<'_>,
start_after: Option<String>,
limit: Option<u32>,
) -> StdResult<PagedDealerResponse> {
query_dealers(deps, start_after, limit, storage::past_dealers())
}
+7 -2
View File
@@ -11,9 +11,14 @@ const CURRENT_DEALERS_PK: &str = "crd";
const PAST_DEALERS_PK: &str = "ptd";
const DEALERS_NODE_INDEX_IDX_NAMESPACE: &str = "dni";
pub(crate) const DEALERS_PAGE_MAX_LIMIT: u32 = 75;
pub(crate) const DEALERS_PAGE_DEFAULT_LIMIT: u32 = 50;
pub(crate) const BLACKLISTED_DEALERS: Map<'_, &'_ Addr, Blacklisting> = Map::new("bld");
pub(crate) const NODE_INDEX_COUNTER: Item<NodeIndex> = Item::new("node_index_counter");
pub(crate) type DealersMap<'a> = IndexedMap<'a, &'a Addr, DealerDetails, DealersIndex<'a>>;
pub(crate) struct DealersIndex<'a> {
pub(crate) node_index: UniqueIndex<'a, NodeIndex, DealerDetails>,
}
@@ -25,14 +30,14 @@ impl<'a> IndexList<DealerDetails> for DealersIndex<'a> {
}
}
pub(crate) fn current_dealers<'a>() -> IndexedMap<'a, &'a Addr, DealerDetails, DealersIndex<'a>> {
pub(crate) fn current_dealers<'a>() -> DealersMap<'a> {
let indexes = DealersIndex {
node_index: UniqueIndex::new(|d| d.assigned_index, DEALERS_NODE_INDEX_IDX_NAMESPACE),
};
IndexedMap::new(CURRENT_DEALERS_PK, indexes)
}
pub(crate) fn past_dealers<'a>() -> IndexedMap<'a, &'a Addr, DealerDetails, DealersIndex<'a>> {
pub(crate) fn past_dealers<'a>() -> DealersMap<'a> {
let indexes = DealersIndex {
node_index: UniqueIndex::new(|d| d.assigned_index, DEALERS_NODE_INDEX_IDX_NAMESPACE),
};
@@ -154,6 +154,7 @@ pub fn try_add_dealer(
// save the dealer into the storage
let dealer_details = DealerDetails {
address: info.sender.clone(),
joined_at: env.block.height,
left_at: None,
blacklisting: None,
+7
View File
@@ -1,6 +1,7 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::dealers::queries::{query_current_dealers_paged, query_past_dealers_paged};
use crate::epoch::queries::query_current_epoch;
use crate::error::ContractError;
use coconut_dkg_common::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
@@ -81,6 +82,12 @@ pub fn execute(
pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> Result<QueryResponse, ContractError> {
let response = match msg {
QueryMsg::GetCurrentEpoch {} => to_binary(&query_current_epoch(deps.storage)?)?,
QueryMsg::GetCurrentDealers { limit, start_after } => {
to_binary(&query_current_dealers_paged(deps, start_after, limit)?)?
}
QueryMsg::GetPastDealers { limit, start_after } => {
to_binary(&query_past_dealers_paged(deps, start_after, limit)?)?
}
};
Ok(response)
+15 -3
View File
@@ -5,7 +5,7 @@ use crate::config::Config;
use crate::rewarded_set_updater::error::RewardingError;
#[cfg(feature = "coconut")]
use async_trait::async_trait;
use coconut_dkg_common::types::Epoch as DkgEpoch;
use coconut_dkg_common::types::{DealerDetails, Epoch as DkgEpoch};
use config::defaults::{DEFAULT_NETWORK, DEFAULT_VALIDATOR_API_PORT};
use mixnet_contract_common::Interval;
use mixnet_contract_common::{
@@ -432,11 +432,23 @@ impl<C> Client<C>
where
C: SigningCosmWasmClient + Sync + Send,
{
async fn get_dkg_epoch(&self) -> Result<DkgEpoch, NymdError> {
pub(crate) async fn get_dkg_epoch(&self) -> Result<DkgEpoch, NymdError> {
self.0.read().await.nymd.get_current_dkg_epoch().await
}
async fn submit_dealing_commitment(
pub(crate) async fn get_current_dealers(
&self,
) -> Result<Vec<DealerDetails>, ValidatorClientError> {
self.0.read().await.get_all_nymd_current_dealers().await
}
pub(crate) async fn get_past_dealers(
&self,
) -> Result<Vec<DealerDetails>, ValidatorClientError> {
self.0.read().await.get_all_nymd_past_dealers().await
}
pub(crate) async fn submit_dealing_commitment(
&self,
epoch_id: u32,
dealing_digest: [u8; 32],