Skip to content

Commit

Permalink
Optimised delete jar method
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Oct 18, 2023
1 parent 2e0a0eb commit a4c608d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
2 changes: 1 addition & 1 deletion contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Contract {

if let Some(ref cache) = jar.cache {
if cache.interest == 0 && jar.principal == 0 {
self.delete_jar(jar_before_transfer);
self.delete_jar(&jar_before_transfer.account_id, jar_before_transfer.id);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl JarApi for Contract {
let (should_be_closed, withdraw_jar) = jar.withdrawn(product, principal, now);

if should_be_closed {
self.delete_jar(withdraw_jar);
self.delete_jar(&withdraw_jar.account_id, withdraw_jar.id);
} else {
let jar_id = withdraw_jar.id;
*self.get_jar_mut_internal(&account_id, jar_id) = withdraw_jar;
Expand Down
33 changes: 11 additions & 22 deletions contract/src/jar/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,40 +278,29 @@ impl Contract {
U128(principal)
}

pub(crate) fn delete_jar(&mut self, jar: Jar) {
let account = &jar.account_id;

pub(crate) fn delete_jar(&mut self, account_id: &AccountId, jar_id: JarId) {
let jars = self
.account_jars
.get_mut(account)
.unwrap_or_else(|| env::panic_str(&format!("Account '{account}' doesn't exist")));

require!(!jars.is_empty(), "Trying to delete jar from empty account");

if jars.len() == 1 {
jars.clear();
return;
}
.get_mut(account_id)
.unwrap_or_else(|| panic_str(&format!("Account '{account_id}' doesn't exist")));

// On jar deletion, we move the last jar in the vector in the deleted jar's place.
// This way we don't need to shift all jars to fill empty space in the vector.
require!(
!jars.is_empty(),
"Trying to delete a jar from account without any jars."
);

let jar_position = jars
.iter()
.position(|j| j.id == jar.id)
.unwrap_or_else(|| env::panic_str(&format!("Jar with id {} doesn't exist", jar.id)));

let last_jar = jars.pop().unwrap();
.position(|j| j.id == jar_id)
.unwrap_or_else(|| panic_str(&format!("Jar with id {jar_id} doesn't exist")));

if jar_position != jars.len() {
jars[jar_position] = last_jar;
}
jars.swap_remove(jar_position);
}

pub(crate) fn get_jar_mut_internal(&mut self, account: &AccountId, id: JarId) -> &mut Jar {
self.account_jars
.get_mut(account)
.unwrap_or_else(|| env::panic_str(&format!("Account '{account}' doesn't exist")))
.unwrap_or_else(|| panic_str(&format!("Account '{account}' doesn't exist")))
.get_jar_mut(id)
}

Expand Down
2 changes: 1 addition & 1 deletion contract/src/withdraw/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Contract {
) -> WithdrawView {
if is_promise_success {
if close_jar {
self.delete_jar(original_jar.clone());
self.delete_jar(&original_jar.account_id, original_jar.id);
} else {
self.get_jar_mut_internal(&original_jar.account_id, original_jar.id)
.unlock();
Expand Down

0 comments on commit a4c608d

Please sign in to comment.