Skip to content

Fix not to include zero-valued entry in delegation #1448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions core/src/consensus/tendermint/stake/action_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ impl<'a> Delegation<'a> {
}

pub fn add_quantity(&mut self, delegatee: Address, quantity: StakeQuantity) -> StateResult<()> {
if quantity == 0 {
return Ok(())
}
*self.delegatees.entry(delegatee).or_insert(0) += quantity;
Ok(())
}
Expand Down Expand Up @@ -461,4 +464,41 @@ mod tests {
assert_eq!(delegation.get_quantity(delegatee), delegation_amount[delegatee]);
}
}

#[test]
fn delegation_zero_add_should_not_be_included() {
let mut state = helpers::get_temp_state();

// Prepare
let delegator = Address::random();
let delegatee1 = Address::random();
let delegatee2 = Address::random();

// Do delegate
let mut delegation = Delegation::load_from_state(&state, &delegator).unwrap();
delegation.add_quantity(delegatee1, 100).unwrap();
delegation.add_quantity(delegatee2, 0).unwrap();
delegation.save_to_state(&mut state).unwrap();

let delegation = Delegation::load_from_state(&state, &delegator).unwrap();
let delegated = delegation.iter().collect::<Vec<_>>();
assert_eq!(&delegated, &[(&delegatee1, &100)]);
}

#[test]
fn delegation_empty_removed_from_state() {
let mut state = helpers::get_temp_state();

// Prepare
let delegator = Address::random();
let delegatee = Address::random();

// Do delegate
let mut delegation = Delegation::load_from_state(&state, &delegator).unwrap();
delegation.add_quantity(delegatee, 0).unwrap();
delegation.save_to_state(&mut state).unwrap();

let result = state.action_data(&get_delegation_key(&delegator)).unwrap();
assert_eq!(result, None);
}
}