Skip to content
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

Fix task distribution #1013

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions pallets/tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ pub mod pallet {

/// Sets the reward for write tasks.
///
// # Flow
/// # Flow
/// 1. Ensure the origin of the transaction is a root user.
/// 2. Insert the new reward amount for the specified network into the [`NetworkWriteReward`] storage.
/// 3. Emit an event [`Event::WriteTaskRewardSet`] indicating the write task reward has been set.
Expand Down Expand Up @@ -1275,7 +1275,7 @@ pub mod pallet {
/// - Retrieve all shards associated with the `network`.
/// - Collect these shards into a list.
/// - Sort the list based on the number of tasks each shard currently has.
/// - Iterate through the sorted list in reverse order.
/// - Iterate through the sorted list.
/// - For each shard in the list, call [`Self::schedule_tasks_shard`] to schedule tasks.
fn schedule_tasks(network: NetworkId, shard_id: Option<ShardId>) {
if let Some(shard_id) = shard_id {
Expand All @@ -1287,7 +1287,7 @@ pub mod pallet {
.count()
.cmp(&ShardTasks::<T>::iter_prefix(*b).count())
});
shards.into_iter().rev().for_each(|(shard, _)| {
shards.into_iter().for_each(|(shard, _)| {
Self::schedule_tasks_shard(network, shard);
});
}
Expand Down
58 changes: 58 additions & 0 deletions pallets/tasks/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,64 @@ fn test_task_execution_order() {
});
}

#[test]
fn test_multi_shard_distribution() {
new_test_ext().execute_with(|| {
// Shard creation
for i in 0..3 {
Shards::create_shard(
ETHEREUM,
[[0u8; 32].into(), [1u8; 32].into(), [2u8; 32].into()].to_vec(),
1,
);
ShardState::<Test>::insert(i, ShardStatus::Online);
Tasks::shard_online(i, ETHEREUM);
}

// Tasks creation and assingment
for _ in 0..9 {
assert_ok!(Tasks::create_task(
RawOrigin::Signed([0; 32].into()).into(),
mock_task(ETHEREUM, 3)
));
}

assert_eq!(ShardTasks::<Test>::iter_prefix(0).count(), 3);
assert_eq!(ShardTasks::<Test>::iter_prefix(1).count(), 3);
assert_eq!(ShardTasks::<Test>::iter_prefix(2).count(), 3);
});
}

#[test]
fn test_multi_shard_distribution_task_more_than_limit() {
new_test_ext().execute_with(|| {
// Shard creation
for i in 0..3 {
Shards::create_shard(
ETHEREUM,
[[0u8; 32].into(), [1u8; 32].into(), [2u8; 32].into()].to_vec(),
1,
);
ShardState::<Test>::insert(i, ShardStatus::Online);
Tasks::shard_online(i, ETHEREUM);
}

assert_ok!(Tasks::set_shard_task_limit(RawOrigin::Root.into(), ETHEREUM, 5));

// Tasks creation and assingment
for _ in 0..30 {
assert_ok!(Tasks::create_task(
RawOrigin::Signed([0; 32].into()).into(),
mock_task(ETHEREUM, 3)
));
}

assert_eq!(ShardTasks::<Test>::iter_prefix(0).count(), 5);
assert_eq!(ShardTasks::<Test>::iter_prefix(1).count(), 5);
assert_eq!(ShardTasks::<Test>::iter_prefix(2).count(), 5);
});
}

#[test]
fn test_assingment_with_diff_shard_size() {
new_test_ext().execute_with(|| {
Expand Down
Loading