Skip to content

Commit 5c00f10

Browse files
committed
tests(staking): increase test coverage for stake querying methods
1 parent 5ec6673 commit 5c00f10

File tree

5 files changed

+85
-30
lines changed

5 files changed

+85
-30
lines changed

data_structures/src/staking/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::staking::aux::StakeKey;
1+
use crate::staking::helpers::StakeKey;
22
use failure::Fail;
33
use std::{
44
convert::From,

data_structures/src/staking/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![deny(missing_docs)]
22

3-
/// Auxiliary convenience types and data structures.
4-
pub mod helpers;
53
/// Constants related to the staking functionality.
64
pub mod constants;
75
/// Errors related to the staking functionality.
86
pub mod errors;
7+
/// Auxiliary convenience types and data structures.
8+
pub mod helpers;
99
/// The data structure and related logic for stake entries.
1010
pub mod stake;
1111
/// The data structure and related logic for keeping track of multiple stake entries.
@@ -16,9 +16,9 @@ pub mod stakes;
1616
pub mod prelude {
1717
pub use crate::capabilities::*;
1818

19-
pub use super::helpers::*;
2019
pub use super::constants::*;
2120
pub use super::errors::*;
21+
pub use super::helpers::*;
2222
pub use super::stake::*;
2323
pub use super::stakes::*;
2424
}

data_structures/src/staking/stakes.rs

+79-24
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ where
6363
Coins: Ord,
6464
Epoch: Default,
6565
{
66-
/// A listing of all the stakers, indexed by their address.
66+
/// A listing of all the stake entries, indexed by their stake key.
6767
by_key: BTreeMap<StakeKey<Address>, SyncStake<Address, Coins, Epoch, Power>>,
68-
/// A listing of all the stakers, indexed by validator.
69-
by_validator: BTreeMap<Address, SyncStake<Address, Coins, Epoch, Power>>,
70-
/// A listing of all the stakers, indexed by withdrawer.
71-
by_withdrawer: BTreeMap<Address, SyncStake<Address, Coins, Epoch, Power>>,
72-
/// A listing of all the stakers, indexed by their coins and address.
68+
/// A listing of all the stake entries, indexed by validator.
69+
by_validator: BTreeMap<Address, Vec<SyncStake<Address, Coins, Epoch, Power>>>,
70+
/// A listing of all the stake entries, indexed by withdrawer.
71+
by_withdrawer: BTreeMap<Address, Vec<SyncStake<Address, Coins, Epoch, Power>>>,
72+
/// A listing of all the stake entries, indexed by their coins and address.
7373
///
7474
/// Because this uses a compound key to prevent duplicates, if we want to know which addresses
7575
/// have staked a particular amount, we just need to run a range lookup on the tree.
@@ -146,11 +146,12 @@ where
146146

147147
let validator_key = coins_and_addresses.clone().addresses.validator;
148148
self.by_validator.remove(&validator_key);
149-
self.by_validator.insert(validator_key, stake.clone());
149+
self.by_validator.insert(validator_key, vec![stake.clone()]);
150150

151151
let withdrawer_key = coins_and_addresses.addresses.withdrawer;
152152
self.by_withdrawer.remove(&withdrawer_key);
153-
self.by_withdrawer.insert(withdrawer_key, stake.clone());
153+
self.by_withdrawer
154+
.insert(withdrawer_key, vec![stake.clone()]);
154155

155156
Ok(stake.value.read()?.clone())
156157
}
@@ -308,12 +309,12 @@ where
308309
pub fn query_stakes<TIQSK>(
309310
&mut self,
310311
query: TIQSK,
311-
) -> StakesResult<Coins, Address, Coins, Epoch>
312+
) -> StakesResult<Vec<Stake<Address, Coins, Epoch, Power>>, Address, Coins, Epoch>
312313
where
313314
TIQSK: TryInto<QueryStakesKey<Address>>,
314315
{
315316
match query.try_into() {
316-
Ok(QueryStakesKey::Key(key)) => self.query_by_key(key),
317+
Ok(QueryStakesKey::Key(key)) => self.query_by_key(key).map(|stake| vec![stake]),
317318
Ok(QueryStakesKey::Validator(validator)) => self.query_by_validator(validator),
318319
Ok(QueryStakesKey::Withdrawer(withdrawer)) => self.query_by_withdrawer(withdrawer),
319320
Err(_) => Err(StakesError::EmptyQuery),
@@ -322,41 +323,47 @@ where
322323

323324
/// Query stakes by stake key.
324325
#[inline(always)]
325-
fn query_by_key(&self, key: StakeKey<Address>) -> StakesResult<Coins, Address, Coins, Epoch> {
326+
fn query_by_key(
327+
&self,
328+
key: StakeKey<Address>,
329+
) -> StakesResult<Stake<Address, Coins, Epoch, Power>, Address, Coins, Epoch> {
326330
Ok(self
327331
.by_key
328332
.get(&key)
329333
.ok_or(StakesError::EntryNotFound { key })?
330334
.value
331335
.read()?
332-
.coins)
336+
.clone())
333337
}
334338

335339
/// Query stakes by validator address.
336340
#[inline(always)]
337-
fn query_by_validator(&self, validator: Address) -> StakesResult<Coins, Address, Coins, Epoch> {
341+
fn query_by_validator(
342+
&self,
343+
validator: Address,
344+
) -> StakesResult<Vec<Stake<Address, Coins, Epoch, Power>>, Address, Coins, Epoch> {
338345
Ok(self
339346
.by_validator
340347
.get(&validator)
341348
.ok_or(StakesError::ValidatorNotFound { validator })?
342-
.value
343-
.read()?
344-
.coins)
349+
.iter()
350+
.map(|stake| stake.value.read().unwrap().clone())
351+
.collect())
345352
}
346353

347354
/// Query stakes by withdrawer address.
348355
#[inline(always)]
349356
fn query_by_withdrawer(
350357
&self,
351358
withdrawer: Address,
352-
) -> StakesResult<Coins, Address, Coins, Epoch> {
359+
) -> StakesResult<Vec<Stake<Address, Coins, Epoch, Power>>, Address, Coins, Epoch> {
353360
Ok(self
354361
.by_withdrawer
355362
.get(&withdrawer)
356363
.ok_or(StakesError::WithdrawerNotFound { withdrawer })?
357-
.value
358-
.read()?
359-
.coins)
364+
.iter()
365+
.map(|stake| stake.value.read().unwrap().clone())
366+
.collect())
360367
}
361368
}
362369

@@ -753,11 +760,59 @@ mod tests {
753760
let charlie_erin = (charlie, erin);
754761

755762
stakes.add_stake(alice_charlie, 10, 0).unwrap();
756-
stakes.add_stake(bob_david, 20, 20).unwrap();
757-
stakes.add_stake(charlie_erin, 30, 30).unwrap();
763+
stakes.add_stake(bob_david, 20, 30).unwrap();
764+
stakes.add_stake(charlie_erin, 40, 50).unwrap();
765+
766+
let result = stakes.query_stakes(QueryStakesKey::Key(bob_david.into()));
767+
assert_eq!(
768+
result,
769+
Ok(vec![Stake::from_parts(
770+
20,
771+
CapabilityMap {
772+
mining: 30,
773+
witnessing: 30
774+
}
775+
)])
776+
);
758777

759-
let result = stakes.query_stakes(QueryStakesKey::Key(alice_charlie.into()));
778+
let result = stakes.query_by_validator(bob.into());
779+
assert_eq!(
780+
result,
781+
Ok(vec![Stake::from_parts(
782+
20,
783+
CapabilityMap {
784+
mining: 30,
785+
witnessing: 30
786+
}
787+
)])
788+
);
789+
790+
let result = stakes.query_by_validator(david.into());
791+
assert_eq!(
792+
result,
793+
Err(StakesError::ValidatorNotFound {
794+
validator: david.into()
795+
})
796+
);
760797

761-
assert_eq!(result, Ok(10))
798+
let result = stakes.query_by_withdrawer(david.into());
799+
assert_eq!(
800+
result,
801+
Ok(vec![Stake::from_parts(
802+
20,
803+
CapabilityMap {
804+
mining: 30,
805+
witnessing: 30
806+
}
807+
)])
808+
);
809+
810+
let result = stakes.query_by_withdrawer(bob.into());
811+
assert_eq!(
812+
result,
813+
Err(StakesError::WithdrawerNotFound {
814+
withdrawer: bob.into()
815+
})
816+
);
762817
}
763818
}

data_structures/src/wit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{fmt, ops::*};
22

33
use serde::{Deserialize, Serialize};
44

5-
use crate::{chain::Epoch, staking::aux::Power};
5+
use crate::{chain::Epoch, staking::helpers::Power};
66

77
/// 1 nanowit is the minimal unit of value
88
/// 1 wit = 10^9 nanowits

partial_struct/tests/partial_struct_derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use partial_struct::PartialStruct;
22

33
#[test]
4-
#[allow(clippy::redundant_clone)]
4+
#[allow(clippy::redundant_clone, dead_code)]
55
fn test_partial_derive() {
66
#[derive(PartialStruct, Debug, Clone, PartialEq)]
77
#[partial_struct(derive(Debug, Clone, PartialEq))]

0 commit comments

Comments
 (0)