fixed clippy warnings on existing code
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
|
||||
// Copyright 2022-2024 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use schemars::JsonSchema;
|
||||
@@ -35,6 +35,12 @@ impl From<Vec<u8>> for ContractSafeBytes {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a [u8]> for ContractSafeBytes {
|
||||
fn from(value: &'a [u8]) -> Self {
|
||||
value.to_vec().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ContractSafeBytes {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
if !self.0.is_empty() {
|
||||
|
||||
@@ -121,233 +121,108 @@ pub fn query_dealing_chunk(
|
||||
})
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// pub(crate) mod tests {
|
||||
// use super::*;
|
||||
// use crate::dealings::storage::{DEALINGS_PAGE_DEFAULT_LIMIT, DEALINGS_PAGE_MAX_LIMIT};
|
||||
// use crate::support::tests::fixtures::{dealing_bytes_fixture, partial_dealing_fixture};
|
||||
// use crate::support::tests::helpers::init_contract;
|
||||
// use cosmwasm_std::{Addr, DepsMut};
|
||||
// use nym_coconut_dkg_common::types::PartialContractDealing;
|
||||
//
|
||||
// fn fill_dealings(deps: DepsMut<'_>, epoch: EpochId, dealers: usize, key_size: u32) {
|
||||
// for i in 0..dealers {
|
||||
// let dealer = Addr::unchecked(format!("dealer{i}"));
|
||||
// for dealing_index in 0..key_size {
|
||||
// StoredDealing::save(
|
||||
// deps.storage,
|
||||
// epoch,
|
||||
// &dealer,
|
||||
// PartialContractDealing {
|
||||
// dealing_index: dealing_index,
|
||||
// data: dealing_bytes_fixture(),
|
||||
// },
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn test_query_dealing() {
|
||||
// let mut deps = init_contract();
|
||||
//
|
||||
// let bad_address = "FOOMP".to_string();
|
||||
// assert!(query_dealing(deps.as_ref(), 0, bad_address, 0).is_err());
|
||||
//
|
||||
// let empty = query_dealing(deps.as_ref(), 0, "foo".to_string(), 0).unwrap();
|
||||
// assert_eq!(empty.epoch_id, 0);
|
||||
// assert_eq!(empty.dealing_index, 0);
|
||||
// assert_eq!(empty.dealer, Addr::unchecked("foo"));
|
||||
// assert!(empty.dealing.is_none());
|
||||
//
|
||||
// // insert the dealing
|
||||
// let dealing = partial_dealing_fixture();
|
||||
// StoredDealing::save(
|
||||
// deps.as_mut().storage,
|
||||
// 0,
|
||||
// &Addr::unchecked("foo"),
|
||||
// dealing.clone(),
|
||||
// );
|
||||
//
|
||||
// let retrieved = query_dealing(deps.as_ref(), 0, "foo".to_string(), 0).unwrap();
|
||||
// assert_eq!(retrieved.epoch_id, 0);
|
||||
// assert_eq!(retrieved.dealing_index, dealing.dealing_index);
|
||||
// assert_eq!(retrieved.dealer, Addr::unchecked("foo"));
|
||||
// assert_eq!(retrieved.dealing.unwrap(), dealing.data);
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn test_query_dealing_status() {
|
||||
// let mut deps = init_contract();
|
||||
//
|
||||
// let bad_address = "FOOMP".to_string();
|
||||
// assert!(query_dealing_status(deps.as_ref(), 0, bad_address, 0).is_err());
|
||||
//
|
||||
// let empty = query_dealing_status(deps.as_ref(), 0, "foo".to_string(), 0).unwrap();
|
||||
// assert_eq!(empty.epoch_id, 0);
|
||||
// assert_eq!(empty.dealing_index, 0);
|
||||
// assert_eq!(empty.dealer, Addr::unchecked("foo"));
|
||||
// assert!(!empty.dealing_submitted);
|
||||
//
|
||||
// // insert the dealing
|
||||
// let dealing = partial_dealing_fixture();
|
||||
// StoredDealing::save(
|
||||
// deps.as_mut().storage,
|
||||
// 0,
|
||||
// &Addr::unchecked("foo"),
|
||||
// dealing.clone(),
|
||||
// );
|
||||
//
|
||||
// let retrieved = query_dealing_status(deps.as_ref(), 0, "foo".to_string(), 0).unwrap();
|
||||
// assert_eq!(retrieved.epoch_id, 0);
|
||||
// assert_eq!(retrieved.dealing_index, dealing.dealing_index);
|
||||
// assert_eq!(retrieved.dealer, Addr::unchecked("foo"));
|
||||
// assert!(retrieved.dealing_submitted)
|
||||
// }
|
||||
//
|
||||
// #[cfg(test)]
|
||||
// mod query_dealings {
|
||||
// use super::*;
|
||||
// use nym_coconut_dkg_common::types::DEFAULT_DEALINGS;
|
||||
//
|
||||
// #[test]
|
||||
// fn dealings_empty_on_init() {
|
||||
// let deps = init_contract();
|
||||
// let all_dealings = StoredDealing::unchecked_all_entries(&deps.storage);
|
||||
// assert!(all_dealings.is_empty())
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn dealings_paged_retrieval_obeys_limits() {
|
||||
// let mut deps = init_contract();
|
||||
// let limit = 2;
|
||||
// fill_dealings(deps.as_mut(), 0, 10, DEFAULT_DEALINGS as u32);
|
||||
//
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// let page1 =
|
||||
// query_dealings_paged(deps.as_ref(), 0, dealer, None, Option::from(limit))
|
||||
// .unwrap();
|
||||
// assert_eq!(limit, page1.dealings.len() as u32);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn dealings_paged_retrieval_has_default_limit() {
|
||||
// let mut deps = init_contract();
|
||||
// fill_dealings(deps.as_mut(), 0, 10, DEFAULT_DEALINGS as u32);
|
||||
//
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// // query without explicitly setting a limit
|
||||
// let page1 = query_dealings_paged(deps.as_ref(), 0, dealer, None, None).unwrap();
|
||||
//
|
||||
// assert_eq!(DEALINGS_PAGE_DEFAULT_LIMIT, page1.dealings.len() as u32);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn dealings_paged_retrieval_has_max_limit() {
|
||||
// let mut deps = init_contract();
|
||||
// fill_dealings(deps.as_mut(), 0, 10, DEFAULT_DEALINGS as u32);
|
||||
//
|
||||
// // query with a crazily high limit in an attempt to use too many resources
|
||||
// let crazy_limit = 1000 * DEALINGS_PAGE_MAX_LIMIT;
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// let page1 =
|
||||
// query_dealings_paged(deps.as_ref(), 0, dealer, None, Option::from(crazy_limit))
|
||||
// .unwrap();
|
||||
//
|
||||
// // we default to a decent sized upper bound instead
|
||||
// let expected_limit = DEALINGS_PAGE_MAX_LIMIT;
|
||||
// assert_eq!(expected_limit, page1.dealings.len() as u32);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn dealings_pagination_works() {
|
||||
// let mut deps = init_contract();
|
||||
//
|
||||
// fill_dealings(deps.as_mut(), 0, 10, 1);
|
||||
// let per_page = 2;
|
||||
//
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// let page1 =
|
||||
// query_dealings_paged(deps.as_ref(), 0, dealer, None, Option::from(per_page))
|
||||
// .unwrap();
|
||||
//
|
||||
// // page should have 1 result on it
|
||||
// assert_eq!(1, page1.dealings.len());
|
||||
// }
|
||||
//
|
||||
// // save another
|
||||
// fill_dealings(deps.as_mut(), 1, 10, 2);
|
||||
//
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// // page1 should have 2 results on it
|
||||
// let page1 =
|
||||
// query_dealings_paged(deps.as_ref(), 1, dealer, None, Option::from(per_page))
|
||||
// .unwrap();
|
||||
// assert_eq!(2, page1.dealings.len());
|
||||
// }
|
||||
//
|
||||
// fill_dealings(deps.as_mut(), 3, 10, 3);
|
||||
//
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// // page1 still has 2 results
|
||||
// let page1 = query_dealings_paged(
|
||||
// deps.as_ref(),
|
||||
// 3,
|
||||
// dealer.clone(),
|
||||
// None,
|
||||
// Option::from(per_page),
|
||||
// )
|
||||
// .unwrap();
|
||||
// assert_eq!(2, page1.dealings.len());
|
||||
//
|
||||
// // retrieving the next page should start after the last key on this page
|
||||
// let start_after = page1.start_next_after.unwrap();
|
||||
// let page2 = query_dealings_paged(
|
||||
// deps.as_ref(),
|
||||
// 3,
|
||||
// dealer,
|
||||
// Option::from(start_after),
|
||||
// Option::from(per_page),
|
||||
// )
|
||||
// .unwrap();
|
||||
//
|
||||
// assert_eq!(1, page2.dealings.len());
|
||||
// }
|
||||
//
|
||||
// fill_dealings(deps.as_mut(), 4, 10, 4);
|
||||
//
|
||||
// for dealer in 0..10 {
|
||||
// let dealer = format!("dealer{dealer}");
|
||||
// let page1 = query_dealings_paged(
|
||||
// deps.as_ref(),
|
||||
// 4,
|
||||
// dealer.clone(),
|
||||
// None,
|
||||
// Option::from(per_page),
|
||||
// )
|
||||
// .unwrap();
|
||||
// let start_after = page1.start_next_after.unwrap();
|
||||
// let page2 = query_dealings_paged(
|
||||
// deps.as_ref(),
|
||||
// 4,
|
||||
// dealer,
|
||||
// Option::from(start_after),
|
||||
// Option::from(per_page),
|
||||
// )
|
||||
// .unwrap();
|
||||
//
|
||||
// // now we have 2 pages, with 2 results on the second page
|
||||
// assert_eq!(2, page2.dealings.len());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::support::tests::fixtures::{dealing_bytes_fixture, partial_dealing_fixture};
|
||||
use crate::support::tests::helpers::init_contract;
|
||||
use cosmwasm_std::{Addr, DepsMut};
|
||||
use nym_coconut_dkg_common::dealing::{DealingChunkInfo, PartialContractDealing};
|
||||
|
||||
#[allow(unused)]
|
||||
fn fill_dealings(
|
||||
deps: DepsMut<'_>,
|
||||
epoch: EpochId,
|
||||
dealers: usize,
|
||||
key_size: u32,
|
||||
chunks: u16,
|
||||
) {
|
||||
for i in 0..dealers {
|
||||
let dealer = Addr::unchecked(format!("dealer{i}"));
|
||||
for dealing_index in 0..key_size {
|
||||
let data = dealing_bytes_fixture();
|
||||
let chunks = data.0.chunks(data.len() / chunks as usize);
|
||||
|
||||
let mut chunk_infos = Vec::new();
|
||||
for (chunk_index, chunk) in chunks.enumerate() {
|
||||
chunk_infos.push(DealingChunkInfo { size: chunk.len() });
|
||||
StoredDealing::save(
|
||||
deps.storage,
|
||||
epoch,
|
||||
&dealer,
|
||||
PartialContractDealing {
|
||||
dealing_index,
|
||||
chunk_index: chunk_index as ChunkIndex,
|
||||
data: chunk.into(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_dealing_chunk() {
|
||||
let mut deps = init_contract();
|
||||
|
||||
let bad_address = "FOOMP".to_string();
|
||||
assert!(query_dealing_chunk(deps.as_ref(), 0, bad_address, 0, 0).is_err());
|
||||
|
||||
let empty = query_dealing_chunk(deps.as_ref(), 0, "foo".to_string(), 0, 0).unwrap();
|
||||
assert_eq!(empty.epoch_id, 0);
|
||||
assert_eq!(empty.dealing_index, 0);
|
||||
assert_eq!(empty.chunk_index, 0);
|
||||
assert_eq!(empty.dealer, Addr::unchecked("foo"));
|
||||
assert!(empty.chunk.is_none());
|
||||
|
||||
// insert the dealing chunk
|
||||
let dealing = partial_dealing_fixture();
|
||||
StoredDealing::save(
|
||||
deps.as_mut().storage,
|
||||
0,
|
||||
&Addr::unchecked("foo"),
|
||||
dealing.clone(),
|
||||
);
|
||||
|
||||
let retrieved = query_dealing_chunk(deps.as_ref(), 0, "foo".to_string(), 0, 0).unwrap();
|
||||
assert_eq!(retrieved.epoch_id, 0);
|
||||
assert_eq!(retrieved.dealing_index, dealing.dealing_index);
|
||||
assert_eq!(retrieved.chunk_index, dealing.chunk_index);
|
||||
assert_eq!(retrieved.dealer, Addr::unchecked("foo"));
|
||||
assert_eq!(retrieved.chunk.unwrap(), dealing.data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_dealing_status() {
|
||||
let deps = init_contract();
|
||||
|
||||
let bad_address = "FOOMP".to_string();
|
||||
assert!(query_dealing_status(deps.as_ref(), 0, bad_address, 0).is_err());
|
||||
|
||||
let empty = query_dealing_status(deps.as_ref(), 0, "foo".to_string(), 0).unwrap();
|
||||
assert_eq!(empty.epoch_id, 0);
|
||||
assert_eq!(empty.dealing_index, 0);
|
||||
assert_eq!(empty.dealer, Addr::unchecked("foo"));
|
||||
assert!(!empty.status.fully_submitted);
|
||||
assert!(!empty.status.has_metadata);
|
||||
assert!(empty.status.chunk_submission_status.is_empty());
|
||||
|
||||
// insert the metadata
|
||||
//
|
||||
|
||||
// // insert the dealing
|
||||
// let dealing = partial_dealing_fixture();
|
||||
// StoredDealing::save(
|
||||
// deps.as_mut().storage,
|
||||
// 0,
|
||||
// &Addr::unchecked("foo"),
|
||||
// dealing.clone(),
|
||||
// );
|
||||
//
|
||||
// let retrieved = query_dealing_status(deps.as_ref(), 0, "foo".to_string(), 0).unwrap();
|
||||
// assert_eq!(retrieved.epoch_id, 0);
|
||||
// assert_eq!(retrieved.dealing_index, dealing.dealing_index);
|
||||
// assert_eq!(retrieved.dealer, Addr::unchecked("foo"));
|
||||
// assert!(retrieved.dealing_submitted)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::error::ContractError;
|
||||
use cosmwasm_std::{Addr, Order, Record, StdResult, Storage};
|
||||
use cw_storage_plus::{Bound, Key, KeyDeserialize, Map, Path, Prefix, Prefixer, PrimaryKey};
|
||||
use cosmwasm_std::{Addr, Storage};
|
||||
use cw_storage_plus::{Key, Map, Path, PrimaryKey};
|
||||
use nym_coconut_dkg_common::dealing::{DealingMetadata, PartialContractDealing};
|
||||
use nym_coconut_dkg_common::types::{
|
||||
ChunkIndex, ContractSafeBytes, DealingIndex, EpochId, PartialContractDealingData,
|
||||
@@ -24,15 +24,6 @@ pub(crate) fn metadata_exists(
|
||||
DEALINGS_METADATA.has(storage, (epoch_id, dealer, dealing_index))
|
||||
}
|
||||
|
||||
pub(crate) fn may_read_metadata(
|
||||
storage: &dyn Storage,
|
||||
epoch_id: EpochId,
|
||||
dealer: Dealer,
|
||||
dealing_index: DealingIndex,
|
||||
) -> Result<Option<DealingMetadata>, ContractError> {
|
||||
Ok(DEALINGS_METADATA.may_load(storage, (epoch_id, dealer, dealing_index))?)
|
||||
}
|
||||
|
||||
pub(crate) fn must_read_metadata(
|
||||
storage: &dyn Storage,
|
||||
epoch_id: EpochId,
|
||||
@@ -71,16 +62,54 @@ pub(crate) struct StoredDealing;
|
||||
impl StoredDealing {
|
||||
const NAMESPACE: &'static [u8] = b"dealing";
|
||||
|
||||
// prefix-range related should we need it
|
||||
#[cfg(test)]
|
||||
fn deserialize_dealing_record(
|
||||
kv: Record,
|
||||
) -> StdResult<(ChunkIndex, PartialContractDealingData)> {
|
||||
kv: cosmwasm_std::Record,
|
||||
) -> cosmwasm_std::StdResult<(ChunkIndex, PartialContractDealingData)> {
|
||||
let (k, v) = kv;
|
||||
let index = <ChunkIndex as KeyDeserialize>::from_vec(k)?;
|
||||
let index = <ChunkIndex as cw_storage_plus::KeyDeserialize>::from_vec(k)?;
|
||||
let data = ContractSafeBytes(v);
|
||||
|
||||
Ok((index, data))
|
||||
}
|
||||
|
||||
// prefix-range related should we need it
|
||||
#[cfg(test)]
|
||||
fn prefix(
|
||||
prefix: (EpochId, Dealer, DealingIndex),
|
||||
) -> cw_storage_plus::Prefix<ChunkIndex, PartialContractDealingData, ChunkIndex> {
|
||||
use cw_storage_plus::Prefixer;
|
||||
|
||||
cw_storage_plus::Prefix::with_deserialization_functions(
|
||||
Self::NAMESPACE,
|
||||
&prefix.prefix(),
|
||||
&[],
|
||||
// explicitly panic to make sure we're never attempting to call an unexpected deserializer on our data
|
||||
|_, _, kv| Self::deserialize_dealing_record(kv),
|
||||
|_, _, _| panic!("attempted to call custom de_fn_v"),
|
||||
)
|
||||
}
|
||||
|
||||
// prefix-range related should we need it
|
||||
#[cfg(test)]
|
||||
pub(crate) fn prefix_range<'a>(
|
||||
storage: &'a dyn Storage,
|
||||
prefix: (EpochId, Dealer, DealingIndex),
|
||||
start: Option<cw_storage_plus::Bound<ChunkIndex>>,
|
||||
) -> impl Iterator<Item = cosmwasm_std::StdResult<PartialContractDealing>> + 'a {
|
||||
let dealing_index = prefix.2;
|
||||
Self::prefix(prefix)
|
||||
.range(storage, start, None, cosmwasm_std::Order::Ascending)
|
||||
.map(move |maybe_record| {
|
||||
maybe_record.map(|(chunk_index, data)| PartialContractDealing {
|
||||
dealing_index,
|
||||
chunk_index,
|
||||
data,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn storage_key(
|
||||
epoch_id: EpochId,
|
||||
dealer: Dealer,
|
||||
@@ -97,39 +126,26 @@ impl StoredDealing {
|
||||
)
|
||||
}
|
||||
|
||||
fn prefix(
|
||||
prefix: (EpochId, Dealer, DealingIndex),
|
||||
) -> Prefix<ChunkIndex, PartialContractDealingData, ChunkIndex> {
|
||||
Prefix::with_deserialization_functions(
|
||||
Self::NAMESPACE,
|
||||
&prefix.prefix(),
|
||||
&[],
|
||||
// explicitly panic to make sure we're never attempting to call an unexpected deserializer on our data
|
||||
|_, _, kv| Self::deserialize_dealing_record(kv),
|
||||
|_, _, _| panic!("attempted to call custom de_fn_v"),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn exists(
|
||||
storage: &dyn Storage,
|
||||
epoch_id: EpochId,
|
||||
dealer: &Addr,
|
||||
dealing_index: DealingIndex,
|
||||
chunk_index: ChunkIndex,
|
||||
) -> StdResult<bool> {
|
||||
// whenever the dealing is saved, the metadata is appropriately updated
|
||||
// reading metadata is way cheaper than the dealing chunk itself
|
||||
let Some(metadata) =
|
||||
DEALINGS_METADATA.may_load(storage, (epoch_id, dealer, dealing_index))?
|
||||
else {
|
||||
return Ok(false);
|
||||
};
|
||||
let Some(chunk_info) = metadata.submitted_chunks.get(&chunk_index) else {
|
||||
return Ok(false);
|
||||
};
|
||||
Ok(chunk_info.status.submitted())
|
||||
// StoredDealing::storage_key(epoch_id, dealer, dealing_index).has(storage)
|
||||
}
|
||||
// pub(crate) fn exists(
|
||||
// storage: &dyn Storage,
|
||||
// epoch_id: EpochId,
|
||||
// dealer: &Addr,
|
||||
// dealing_index: DealingIndex,
|
||||
// chunk_index: ChunkIndex,
|
||||
// ) -> StdResult<bool> {
|
||||
// // whenever the dealing is saved, the metadata is appropriately updated
|
||||
// // reading metadata is way cheaper than the dealing chunk itself
|
||||
// let Some(metadata) =
|
||||
// DEALINGS_METADATA.may_load(storage, (epoch_id, dealer, dealing_index))?
|
||||
// else {
|
||||
// return Ok(false);
|
||||
// };
|
||||
// let Some(chunk_info) = metadata.submitted_chunks.get(&chunk_index) else {
|
||||
// return Ok(false);
|
||||
// };
|
||||
// Ok(chunk_info.status.submitted())
|
||||
// // StoredDealing::storage_key(epoch_id, dealer, dealing_index).has(storage)
|
||||
// }
|
||||
|
||||
pub(crate) fn save(
|
||||
storage: &mut dyn Storage,
|
||||
@@ -158,23 +174,6 @@ impl StoredDealing {
|
||||
storage.get(&storage_key).map(ContractSafeBytes)
|
||||
}
|
||||
|
||||
pub(crate) fn prefix_range<'a>(
|
||||
storage: &'a dyn Storage,
|
||||
prefix: (EpochId, Dealer, DealingIndex),
|
||||
start: Option<Bound<ChunkIndex>>,
|
||||
) -> impl Iterator<Item = StdResult<PartialContractDealing>> + 'a {
|
||||
let dealing_index = prefix.2;
|
||||
Self::prefix(prefix)
|
||||
.range(storage, start, None, Order::Ascending)
|
||||
.map(move |maybe_record| {
|
||||
maybe_record.map(|(chunk_index, data)| PartialContractDealing {
|
||||
dealing_index,
|
||||
chunk_index,
|
||||
data,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// iterate over all values, only to be used in tests due to the amount of data being returned
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::type_complexity)]
|
||||
@@ -184,20 +183,25 @@ impl StoredDealing {
|
||||
(EpochId, Addr, (DealingIndex, ChunkIndex)),
|
||||
PartialContractDealingData,
|
||||
)> {
|
||||
use cw_storage_plus::KeyDeserialize;
|
||||
|
||||
type StorageKey<'a> = (EpochId, Dealer<'a>, (DealingIndex, ChunkIndex));
|
||||
|
||||
let empty_prefix: Prefix<StorageKey, PartialContractDealingData, StorageKey> =
|
||||
Prefix::with_deserialization_functions(
|
||||
Self::NAMESPACE,
|
||||
&[],
|
||||
&[],
|
||||
|_, _, kv| StorageKey::from_vec(kv.0).map(|kt| (kt, ContractSafeBytes(kv.1))),
|
||||
|_, _, _| unimplemented!(),
|
||||
);
|
||||
let empty_prefix: cw_storage_plus::Prefix<
|
||||
StorageKey,
|
||||
PartialContractDealingData,
|
||||
StorageKey,
|
||||
> = cw_storage_plus::Prefix::with_deserialization_functions(
|
||||
Self::NAMESPACE,
|
||||
&[],
|
||||
&[],
|
||||
|_, _, kv| StorageKey::from_vec(kv.0).map(|kt| (kt, ContractSafeBytes(kv.1))),
|
||||
|_, _, _| unimplemented!(),
|
||||
);
|
||||
|
||||
empty_prefix
|
||||
.range(storage, None, None, Order::Ascending)
|
||||
.collect::<StdResult<_>>()
|
||||
.range(storage, None, None, cosmwasm_std::Order::Ascending)
|
||||
.collect::<cosmwasm_std::StdResult<_>>()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
@@ -206,6 +210,7 @@ impl StoredDealing {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::support::tests::helpers::init_contract;
|
||||
use cw_storage_plus::Bound;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn dealing_data(
|
||||
|
||||
@@ -207,7 +207,9 @@ pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::epoch_state::storage::CURRENT_EPOCH;
|
||||
use crate::epoch_state::transactions::{advance_epoch_state, try_initiate_dkg};
|
||||
use crate::support::tests::fixtures::{dealer_details_fixture, partial_dealing_fixture};
|
||||
use crate::support::tests::fixtures::{
|
||||
dealer_details_fixture, dealing_metadata_fixture, partial_dealing_fixture,
|
||||
};
|
||||
use crate::support::tests::helpers;
|
||||
use crate::support::tests::helpers::{add_fixture_dealer, ADMIN_ADDRESS};
|
||||
use cosmwasm_std::testing::{mock_env, mock_info};
|
||||
@@ -218,129 +220,185 @@ pub(crate) mod tests {
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn invalid_commit_dealing() {
|
||||
// todo!()
|
||||
// let mut deps = helpers::init_contract();
|
||||
// let mut env = mock_env();
|
||||
// try_initiate_dkg(deps.as_mut(), env.clone(), mock_info(ADMIN_ADDRESS, &[])).unwrap();
|
||||
fn invalid_commit_dealing_chunk() {
|
||||
let mut deps = helpers::init_contract();
|
||||
let mut env = mock_env();
|
||||
try_initiate_dkg(deps.as_mut(), env.clone(), mock_info(ADMIN_ADDRESS, &[])).unwrap();
|
||||
|
||||
let owner = Addr::unchecked("owner1");
|
||||
let info = mock_info(owner.as_str(), &[]);
|
||||
let dealing = partial_dealing_fixture();
|
||||
|
||||
let ret = try_commit_dealings_chunk(
|
||||
deps.as_mut(),
|
||||
env.clone(),
|
||||
info.clone(),
|
||||
dealing.clone(),
|
||||
false,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(
|
||||
ret,
|
||||
ContractError::IncorrectEpochState {
|
||||
current_state: EpochState::PublicKeySubmission { resharing: false }.to_string(),
|
||||
expected_state: EpochState::DealingExchange { resharing: false }.to_string()
|
||||
}
|
||||
);
|
||||
|
||||
env.block.time = env
|
||||
.block
|
||||
.time
|
||||
.plus_seconds(TimeConfiguration::default().public_key_submission_time_secs);
|
||||
add_fixture_dealer(deps.as_mut());
|
||||
advance_epoch_state(deps.as_mut(), env.clone()).unwrap();
|
||||
|
||||
let ret = try_commit_dealings_chunk(
|
||||
deps.as_mut(),
|
||||
env.clone(),
|
||||
info.clone(),
|
||||
dealing.clone(),
|
||||
false,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(ret, ContractError::NotADealer);
|
||||
|
||||
let dealer_details = DealerDetails {
|
||||
address: owner.clone(),
|
||||
bte_public_key_with_proof: String::new(),
|
||||
ed25519_identity: String::new(),
|
||||
announce_address: String::new(),
|
||||
assigned_index: 1,
|
||||
};
|
||||
dealers_storage::current_dealers()
|
||||
.save(deps.as_mut().storage, &owner, &dealer_details)
|
||||
.unwrap();
|
||||
|
||||
// assume we're in resharing mode
|
||||
CURRENT_EPOCH
|
||||
.update::<_, ContractError>(deps.as_mut().storage, |mut epoch| {
|
||||
epoch.state = EpochState::DealingExchange { resharing: true };
|
||||
Ok(epoch)
|
||||
})
|
||||
.unwrap();
|
||||
INITIAL_REPLACEMENT_DATA
|
||||
.save(
|
||||
deps.as_mut().storage,
|
||||
&InitialReplacementData {
|
||||
initial_dealers: vec![],
|
||||
initial_height: 1,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let ret = try_commit_dealings_chunk(
|
||||
deps.as_mut(),
|
||||
env.clone(),
|
||||
info.clone(),
|
||||
dealing.clone(),
|
||||
true,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(ret, ContractError::NotAnInitialDealer);
|
||||
|
||||
INITIAL_REPLACEMENT_DATA
|
||||
.update::<_, ContractError>(deps.as_mut().storage, |mut data| {
|
||||
data.initial_dealers = vec![dealer_details_fixture(1).address];
|
||||
Ok(data)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
// back to 'normal' mode
|
||||
CURRENT_EPOCH
|
||||
.update::<_, ContractError>(deps.as_mut().storage, |mut epoch| {
|
||||
epoch.state = EpochState::DealingExchange { resharing: false };
|
||||
Ok(epoch)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
// TODO: test case: no metadata
|
||||
//
|
||||
// let owner = Addr::unchecked("owner1");
|
||||
// let info = mock_info(owner.as_str(), &[]);
|
||||
// let dealing = partial_dealing_fixture();
|
||||
//
|
||||
// let ret =
|
||||
// try_commit_dealings(deps.as_mut(), info.clone(), dealing.clone(), false).unwrap_err();
|
||||
// assert_eq!(
|
||||
// ret,
|
||||
// ContractError::IncorrectEpochState {
|
||||
// current_state: EpochState::PublicKeySubmission { resharing: false }.to_string(),
|
||||
// expected_state: EpochState::DealingExchange { resharing: false }.to_string()
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// env.block.time = env
|
||||
// .block
|
||||
// .time
|
||||
// .plus_seconds(TimeConfiguration::default().public_key_submission_time_secs);
|
||||
// add_fixture_dealer(deps.as_mut());
|
||||
// advance_epoch_state(deps.as_mut(), env).unwrap();
|
||||
//
|
||||
// let ret =
|
||||
// try_commit_dealings(deps.as_mut(), info.clone(), dealing.clone(), false).unwrap_err();
|
||||
// assert_eq!(ret, ContractError::NotADealer);
|
||||
//
|
||||
// let dealer_details = DealerDetails {
|
||||
// address: owner.clone(),
|
||||
// bte_public_key_with_proof: String::new(),
|
||||
// ed25519_identity: String::new(),
|
||||
// announce_address: String::new(),
|
||||
// assigned_index: 1,
|
||||
// };
|
||||
// dealers_storage::current_dealers()
|
||||
// .save(deps.as_mut().storage, &owner, &dealer_details)
|
||||
// .unwrap();
|
||||
//
|
||||
// // assume we're in resharing mode
|
||||
// CURRENT_EPOCH
|
||||
// .update::<_, ContractError>(deps.as_mut().storage, |mut epoch| {
|
||||
// epoch.state = EpochState::DealingExchange { resharing: true };
|
||||
// Ok(epoch)
|
||||
// })
|
||||
// .unwrap();
|
||||
// INITIAL_REPLACEMENT_DATA
|
||||
// .save(
|
||||
// deps.as_mut().storage,
|
||||
// &InitialReplacementData {
|
||||
// initial_dealers: vec![],
|
||||
// initial_height: 1,
|
||||
// },
|
||||
// )
|
||||
// .unwrap();
|
||||
// let ret =
|
||||
// try_commit_dealings(deps.as_mut(), info.clone(), dealing.clone(), true).unwrap_err();
|
||||
// assert_eq!(ret, ContractError::NotAnInitialDealer);
|
||||
//
|
||||
// INITIAL_REPLACEMENT_DATA
|
||||
// .update::<_, ContractError>(deps.as_mut().storage, |mut data| {
|
||||
// data.initial_dealers = vec![dealer_details_fixture(1).address];
|
||||
// Ok(data)
|
||||
// })
|
||||
// .unwrap();
|
||||
//
|
||||
// // back to 'normal' mode
|
||||
// CURRENT_EPOCH
|
||||
// .update::<_, ContractError>(deps.as_mut().storage, |mut epoch| {
|
||||
// epoch.state = EpochState::DealingExchange { resharing: false };
|
||||
// Ok(epoch)
|
||||
// })
|
||||
// .unwrap();
|
||||
//
|
||||
// // dealing out of range
|
||||
// let ret = try_commit_dealings(
|
||||
// deps.as_mut(),
|
||||
// info.clone(),
|
||||
// PartialContractDealing {
|
||||
// dealing_index: 42,
|
||||
// data: ContractSafeBytes(vec![1, 2, 3]),
|
||||
// },
|
||||
// false,
|
||||
// )
|
||||
// .unwrap_err();
|
||||
// assert_eq!(
|
||||
// ret,
|
||||
// ContractError::DealingOutOfRange {
|
||||
// epoch_id: 0,
|
||||
// dealer: info.sender.clone(),
|
||||
// index: 42,
|
||||
// key_size: DEFAULT_DEALINGS as u32,
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// // 'good' dealing
|
||||
// let ret = try_commit_dealings(deps.as_mut(), info.clone(), dealing.clone(), false);
|
||||
// assert!(ret.is_ok());
|
||||
//
|
||||
// // duplicate dealing
|
||||
// let ret =
|
||||
// try_commit_dealings(deps.as_mut(), info.clone(), dealing.clone(), false).unwrap_err();
|
||||
// assert_eq!(
|
||||
// ret,
|
||||
// ContractError::DealingAlreadyCommitted {
|
||||
// epoch_id: 0,
|
||||
// dealer: info.sender.clone(),
|
||||
// index: 0,
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// // same index, but next epoch
|
||||
// CURRENT_EPOCH
|
||||
// .update::<_, ContractError>(deps.as_mut().storage, |mut epoch| {
|
||||
// epoch.epoch_id += 1;
|
||||
// Ok(epoch)
|
||||
// })
|
||||
// .unwrap();
|
||||
//
|
||||
// let ret = try_commit_dealings(deps.as_mut(), info.clone(), dealing.clone(), false);
|
||||
// assert!(ret.is_ok());
|
||||
|
||||
// add dealing metadata
|
||||
try_submit_dealings_metadata(
|
||||
deps.as_mut(),
|
||||
info.clone(),
|
||||
0,
|
||||
dealing_metadata_fixture(),
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// dealing chunk out of range
|
||||
let ret = try_commit_dealings_chunk(
|
||||
deps.as_mut(),
|
||||
env.clone(),
|
||||
info.clone(),
|
||||
PartialContractDealing {
|
||||
dealing_index: 0,
|
||||
chunk_index: 42,
|
||||
data: ContractSafeBytes(vec![1, 2, 3]),
|
||||
},
|
||||
false,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(
|
||||
ret,
|
||||
ContractError::DealingChunkNotInMetadata {
|
||||
epoch_id: 0,
|
||||
dealer: info.sender.clone(),
|
||||
dealing_index: 0,
|
||||
chunk_index: 42,
|
||||
}
|
||||
);
|
||||
|
||||
// 'good' dealing
|
||||
let ret = try_commit_dealings_chunk(
|
||||
deps.as_mut(),
|
||||
env.clone(),
|
||||
info.clone(),
|
||||
dealing.clone(),
|
||||
false,
|
||||
);
|
||||
assert!(ret.is_ok());
|
||||
|
||||
// duplicate dealing
|
||||
let ret = try_commit_dealings_chunk(
|
||||
deps.as_mut(),
|
||||
env.clone(),
|
||||
info.clone(),
|
||||
dealing.clone(),
|
||||
false,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(
|
||||
ret,
|
||||
ContractError::DealingChunkAlreadyCommitted {
|
||||
epoch_id: 0,
|
||||
dealer: info.sender.clone(),
|
||||
dealing_index: 0,
|
||||
chunk_index: 0,
|
||||
block_height: env.block.height,
|
||||
}
|
||||
);
|
||||
|
||||
// same index, but next epoch
|
||||
CURRENT_EPOCH
|
||||
.update::<_, ContractError>(deps.as_mut().storage, |mut epoch| {
|
||||
epoch.epoch_id += 1;
|
||||
Ok(epoch)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
try_submit_dealings_metadata(
|
||||
deps.as_mut(),
|
||||
info.clone(),
|
||||
0,
|
||||
dealing_metadata_fixture(),
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let ret = try_commit_dealings_chunk(deps.as_mut(), env, info, dealing.clone(), false);
|
||||
assert!(ret.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use cosmwasm_std::Addr;
|
||||
use nym_coconut_dkg_common::dealer::DealerDetails;
|
||||
use nym_coconut_dkg_common::dealing::PartialContractDealing;
|
||||
use nym_coconut_dkg_common::dealing::{DealingChunkInfo, PartialContractDealing};
|
||||
use nym_coconut_dkg_common::types::ContractSafeBytes;
|
||||
use nym_coconut_dkg_common::verification_key::ContractVKShare;
|
||||
|
||||
@@ -20,18 +20,26 @@ pub fn vk_share_fixture(owner: &str, index: u64) -> ContractVKShare {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn dealing_bytes_fixture() -> ContractSafeBytes {
|
||||
ContractSafeBytes(vec![1, 2, 3])
|
||||
ContractSafeBytes(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
||||
}
|
||||
|
||||
pub fn partial_dealing_fixture() -> PartialContractDealing {
|
||||
PartialContractDealing {
|
||||
chunk_index: 0,
|
||||
dealing_index: 0,
|
||||
data: ContractSafeBytes(vec![1, 2, 3]),
|
||||
data: ContractSafeBytes(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dealing_metadata_fixture() -> Vec<DealingChunkInfo> {
|
||||
let chunk_fixture = partial_dealing_fixture();
|
||||
vec![DealingChunkInfo {
|
||||
size: chunk_fixture.data.len(),
|
||||
}]
|
||||
}
|
||||
|
||||
pub fn dealer_details_fixture(assigned_index: u64) -> DealerDetails {
|
||||
DealerDetails {
|
||||
address: Addr::unchecked(format!("owner{}", assigned_index)),
|
||||
|
||||
@@ -210,7 +210,7 @@ impl<R: RngCore + CryptoRng> DkgController<R> {
|
||||
|
||||
// we might be in resharing mode and this dealer was not in "initial" set.
|
||||
// in that case we don't expect any dealings
|
||||
if resharing && !initial_dealers.contains(&dealer) {
|
||||
if resharing && !initial_dealers.contains(dealer) {
|
||||
return Ok(Ok(HashMap::new()));
|
||||
}
|
||||
return Ok(Err(DealerRejectionReason::NoDealingsProvided));
|
||||
|
||||
Reference in New Issue
Block a user