Skip to content

Commit a4d74f9

Browse files
committed
fix(msp): respect the maximum storage capacity limit set locally even if the available capacity is sufficient on chain
1 parent 3294a3b commit a4d74f9

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

client/src/tasks/msp_move_bucket.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{sync::Mutex, time::Duration};
44

55
use sc_tracing::tracing::*;
66
use sp_core::H256;
7-
use sp_runtime::traits::{CheckedAdd, SaturatedConversion, Zero};
7+
use sp_runtime::traits::{CheckedAdd, CheckedSub, SaturatedConversion, Zero};
88

99
use pallet_file_system::types::BucketMoveRequestResponse;
1010
use shc_actors_framework::event_bus::EventHandler;
@@ -544,6 +544,22 @@ where
544544
required_size: StorageDataUnit<Runtime>,
545545
own_msp_id: ProviderId<Runtime>,
546546
) -> anyhow::Result<()> {
547+
let max_storage_capacity = self
548+
.storage_hub_handler
549+
.provider_config
550+
.capacity_config
551+
.max_capacity();
552+
553+
let current_capacity = self
554+
.storage_hub_handler
555+
.blockchain
556+
.query_storage_provider_capacity(own_msp_id)
557+
.await
558+
.map_err(|e| {
559+
error!(target: LOG_TARGET, "Failed to query storage provider capacity: {:?}", e);
560+
anyhow::anyhow!("Failed to query storage provider capacity: {:?}", e)
561+
})?;
562+
547563
let available_capacity = self
548564
.storage_hub_handler
549565
.blockchain
@@ -554,6 +570,32 @@ where
554570
anyhow::anyhow!("Failed to query available storage capacity: {:?}", e)
555571
})?;
556572

573+
// Calculate currently used storage
574+
let used_capacity = current_capacity
575+
.checked_sub(&available_capacity)
576+
.ok_or_else(|| {
577+
anyhow::anyhow!(
578+
"Available capacity ({}) exceeds current capacity ({})",
579+
available_capacity,
580+
current_capacity
581+
)
582+
})?;
583+
584+
// Check if accepting this bucket would exceed our local max storage capacity limit
585+
let projected_usage = used_capacity
586+
.checked_add(&required_size)
587+
.ok_or_else(|| anyhow::anyhow!("Overflow calculating projected storage usage"))?;
588+
589+
// Respect the maximum storage capacity limit set locally even if the available capacity is sufficient on chain.
590+
if projected_usage > max_storage_capacity {
591+
let err_msg = format!(
592+
"Accepting bucket would exceed maximum storage capacity limit. Used: {}, Required: {}, Max: {}",
593+
used_capacity, required_size, max_storage_capacity
594+
);
595+
warn!(target: LOG_TARGET, "{}", err_msg);
596+
return Err(anyhow::anyhow!(err_msg));
597+
}
598+
557599
// Increase storage capacity if the available capacity is less than the required size
558600
if available_capacity < required_size {
559601
warn!(
@@ -563,22 +605,6 @@ where
563605
required_size
564606
);
565607

566-
let current_capacity = self
567-
.storage_hub_handler
568-
.blockchain
569-
.query_storage_provider_capacity(own_msp_id)
570-
.await
571-
.map_err(|e| {
572-
error!(target: LOG_TARGET, "Failed to query storage provider capacity: {:?}", e);
573-
anyhow::anyhow!("Failed to query storage provider capacity: {:?}", e)
574-
})?;
575-
576-
let max_storage_capacity = self
577-
.storage_hub_handler
578-
.provider_config
579-
.capacity_config
580-
.max_capacity();
581-
582608
if max_storage_capacity <= current_capacity {
583609
let err_msg =
584610
"Reached maximum storage capacity limit. Unable to add more storage capacity.";

0 commit comments

Comments
 (0)