Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyyrroonn committed Apr 26, 2020
1 parent eb15169 commit cf3afae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
15 changes: 8 additions & 7 deletions cstrml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub struct StakeVoteEdge<Balance: HasCompact> {
/// The total of valid votes.
pub total: Balance,
/// The details of valid votes.
/// index is the relative index in Valiation.guarantors for each guarantor
pub records: BTreeMap<u32, Balance>,
}

Expand Down Expand Up @@ -1168,7 +1169,7 @@ impl<T: Trait> Module<T> {
let mut validations = Self::validators(&target);
let mut new_guarantors = vec![];
for value in validations.guarantors {
if &value != g_stash {
if &value != g_stash { // chill all guarantors in Validations.guarantors
new_guarantors.push(value.clone());
}
}
Expand Down Expand Up @@ -1217,17 +1218,17 @@ impl<T: Trait> Module<T> {
};
new_edge.total = g_votes.clone();
let new_total = g_votes.clone();
let mut g_index: usize = 0;
let mut g_index: usize = 0; // used for removing guarantors from new_guarantors since g_stash doesn't vote anymore.
for (index, votes) in g_old_edge.records.iter() {
while &new_guarantors[g_index] != g_stash {
while &new_guarantors[g_index] != g_stash { // found matching guarantor
g_index += 1;
}
if g_votes == Zero::zero() {
new_guarantors.remove(g_index.clone());
new_guarantors.remove(g_index.clone()); // remove one item -> don't add g_index by one
} else {
let real_votes = g_votes.min(votes.clone());
g_votes -= real_votes;
new_edge.records.insert(index.clone(), real_votes);
new_edge.records.insert(index.clone(), real_votes); // upsert real votes
g_index += 1;
}
}
Expand Down Expand Up @@ -1269,7 +1270,7 @@ impl<T: Trait> Module<T> {

// c. New edge
let mut edge = Self::guarantee_rel(&g_stash, &v_stash).unwrap_or_default();
let rel_index = edge.records.len() as u32;
let rel_index = edge.records.len() as u32; // default index is 0
let g_extra_votes = g_votes - edge.total;
let real_extra_votes = (v_limit - v_total_stakes).min(g_extra_votes);
edge.total += real_extra_votes;
Expand Down Expand Up @@ -1598,7 +1599,7 @@ impl<T: Trait> Module<T> {
let mut remains = to_votes(v_limit_stakes - v_own_stakes);
let guarantors = &validations.guarantors;
// 1. Update GuaranteeRel
let mut rel_indexes: BTreeMap<T::AccountId, u32> = BTreeMap::new();
let mut rel_indexes: BTreeMap<T::AccountId, u32> = BTreeMap::new(); // used to calculate index in edge.records
for guarantor in guarantors {
let mut edge = Self::guarantee_rel(&guarantor, &v_stash).unwrap_or_default();
if !rel_indexes.contains_key(&guarantor) {
Expand Down
17 changes: 17 additions & 0 deletions cstrml/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3389,6 +3389,23 @@ fn guarantee_order_should_work() {
assert_eq!(Staking::guarantee_rel(1, 5).unwrap().records.get(&(4 as u32)), None);
assert_eq!(Staking::guarantee_rel(3, 5).unwrap().records.get(&(0 as u32)), Some(&(1000 as Balance)));
assert_eq!(Staking::guarantee_rel(7, 5).unwrap().records.get(&(0 as u32)), Some(&(1000 as Balance)));


assert_ok!(Staking::guarantee(Origin::signed(2), vec![(5, 1000)]));
assert_eq!(
Staking::validators(&5),
Validations{
guarantee_fee: Default::default(),
guarantors: vec![1, 7, 1, 3, 1]
}
);
assert_eq!(Staking::guarantee_rel(1, 5).unwrap().records.get(&(0 as u32)), Some(&(250 as Balance)));
assert_eq!(Staking::guarantee_rel(1, 5).unwrap().records.get(&(1 as u32)), Some(&(250 as Balance)));
assert_eq!(Staking::guarantee_rel(1, 5).unwrap().records.get(&(2 as u32)), Some(&(500 as Balance)));
assert_eq!(Staking::guarantee_rel(1, 5).unwrap().records.get(&(3 as u32)), None);
assert_eq!(Staking::guarantee_rel(1, 5).unwrap().records.get(&(4 as u32)), None);
assert_eq!(Staking::guarantee_rel(3, 5).unwrap().records.get(&(0 as u32)), Some(&(1000 as Balance)));
assert_eq!(Staking::guarantee_rel(7, 5).unwrap().records.get(&(0 as u32)), Some(&(1000 as Balance)));
});
}

Expand Down

0 comments on commit cf3afae

Please sign in to comment.