Skip to content

Commit

Permalink
fix add_txn
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse committed Jun 21, 2024
1 parent 97ea0eb commit aacf808
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 23 deletions.
69 changes: 49 additions & 20 deletions consensus/src/transaction_shuffler/use_case_aware/delayed_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ where
use_case_entry.remove();
self.use_case_key_by_priority
.insert(use_case.priority_key(), use_case_key);
} else {
break
}
}
}
Expand Down Expand Up @@ -313,7 +315,7 @@ where
}
}

// A previously delayed txn is ready. Pop it and update priorities.
// A previously delayed txn might be ready. Pop it and update priorities.

// First, both the use case and account need to be removed from the priority queues.
let use_case_prio_key = *use_case_prio_key;
Expand Down Expand Up @@ -471,22 +473,46 @@ where
match self.accounts.get_mut(&address) {
Some(account) => {
if account.is_empty() {
// account was empty (placeholder), it's not in any use case's priority queue.
account.queue_txn(input_idx, txn);
match self.use_cases.get_mut(&use_case_key) {
Some(use_case) => {
self.use_case_key_by_priority
.strict_remove(&use_case.priority_key());
use_case.add_account(address, account);
self.use_case_key_by_priority
.strict_insert(use_case.priority_key(), use_case_key.clone());
},
None => {
let use_case =
UseCase::new_with_account(self.output_idx + 1, address, account);
self.use_case_key_by_priority
.strict_insert(use_case.priority_key(), use_case_key.clone());
},
// account was empty (placeholder)
let old_use_case_key = account.use_case_key().clone();

if old_use_case_key != use_case_key {
let old_use_case = self.use_cases.expect_mut(&old_use_case_key);
self.use_case_key_by_priority
.strict_remove(&old_use_case.priority_key());
old_use_case
.account_by_priority
.strict_remove(&account.priority_key());
self.use_case_key_by_priority
.strict_insert(old_use_case.priority_key(), old_use_case_key.clone());

account.queue_txn(input_idx, txn);

match self.use_cases.get_mut(&use_case_key) {
Some(use_case) => {
self.use_case_key_by_priority
.strict_remove(&use_case.priority_key());
use_case.add_account(address, account);
self.use_case_key_by_priority
.strict_insert(use_case.priority_key(), use_case_key.clone());
},
None => {
let use_case =
UseCase::new_with_account(self.output_idx + 1, address, account);
self.use_case_key_by_priority
.strict_insert(use_case.priority_key(), use_case_key.clone());
self.use_cases.strict_insert(use_case_key, use_case);
},
}
} else {
let use_case = self.use_cases.expect_mut(&use_case_key);
self.use_case_key_by_priority.strict_remove(&use_case.priority_key());
use_case.account_by_priority.strict_remove(&account.priority_key());

account.queue_txn(input_idx, txn);

use_case.add_account(address, account);
self.use_case_key_by_priority.strict_insert(use_case.priority_key(), use_case_key.clone());
}
} else {
// Account is not empty, appending a txn won't affect its position in the
Expand All @@ -495,8 +521,10 @@ where
}
},
None => {
// Use case must exist but not ready, otherwise the txn wouldn't need to be delayed.
let use_case = self.use_cases.expect_mut(&use_case_key);
self.use_case_key_by_priority.strict_remove(&use_case.priority_key());

// Use case must exist but not ready, otherwise the txn wouldn't need to be delayed.
// ... Use case must be subject to a greater delay than our newly tracked account,
// so no need to more it within the priority queue.
assert!(use_case.try_delay_till > self.output_idx);
Expand All @@ -505,8 +533,9 @@ where
let account = Account::new_with_txn(self.output_idx + 1, input_idx, txn);
use_case.add_account(address, &account);
self.accounts.insert(address, account);
},
};
self.use_case_key_by_priority.strict_insert(use_case.priority_key(), use_case_key.clone());
}
}
}

/// Return the txn back if relevant use case and sender are not subject to delaying. Otherwise,
Expand Down
35 changes: 32 additions & 3 deletions consensus/src/transaction_shuffler/use_case_aware/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,38 @@ fn assert_shuffle_result(
}

#[test]
fn test_spread_by_sender() {
fn test_no_spreading() {
let config = Config {
sender_spread_factor: 2,
sender_spread_factor: 0,
// ignore use case conflicts
platform_use_case_spread_factor: 0,
// ignore use case conflicts
user_use_case_spread_factor: 0,
};

let txns = [
// 5 txns from A1
(PP, A1),
(OO, A1),
(C1, A1),
(C2, A1),
(C3, A1),
// 3 txns from A2
(PP, A2),
(PP, A2),
(PP, A2),
// 2 txns from A3
(C1, A3),
(C1, A3),
];

assert_shuffle_result(config, txns, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}

#[test]
fn test_spread_by_sender_1() {
let config = Config {
sender_spread_factor: 1,
// ignore use case conflicts
platform_use_case_spread_factor: 0,
// ignore use case conflicts
Expand All @@ -128,7 +157,7 @@ fn test_spread_by_sender() {
(C1, A3),
];

assert_shuffle_result(config, txns, [0, 5, 8, 1, 6, 9, 2, 7, 3, 4]);
assert_shuffle_result(config, txns, [0, 5, 1, 6, 2, 7, 3, 8, 4, 9]);
}

#[test]
Expand Down

0 comments on commit aacf808

Please sign in to comment.