Skip to content

Commit

Permalink
feat: validate storage capacity resources
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter committed May 22, 2024
1 parent eb36aa9 commit 62df060
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
12 changes: 9 additions & 3 deletions substrate-node/support/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Resources {

pub const ONE_THOUSAND: u128 = 1_000;
pub const GIGABYTE: u128 = 1024 * 1024 * 1024;
pub const MINIMUM_STORAGE_CAPACITY: u128 = 100 * GIGABYTE;

impl Resources {
pub fn empty() -> Resources {
Expand All @@ -35,13 +36,18 @@ impl Resources {
}

pub fn validate_hru(&self) -> bool {
// No HRU minimun requirement
true
// HRU minimum of 100 GB
self.hru as u128 >= MINIMUM_STORAGE_CAPACITY
}

pub fn validate_sru(&self) -> bool {
// SRU minimum of 100 GB
self.sru as u128 >= 100 * GIGABYTE
self.sru as u128 >= MINIMUM_STORAGE_CAPACITY
}

pub fn validate_storage(&self) -> bool {
// At least one type of storage (HDD or SSD) must have minimum capacity size
self.validate_hru() || self.validate_sru()
}

pub fn validate_cru(&self) -> bool {
Expand Down
32 changes: 31 additions & 1 deletion substrate-node/support/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::resources::{Resources, GIGABYTE};
use crate::resources::{Resources, GIGABYTE, MINIMUM_STORAGE_CAPACITY};
use crate::types::{PublicIpError, IP4, IP6};
use frame_support::storage::bounded_vec::BoundedVec;
use frame_support::{assert_err, assert_ok};
Expand Down Expand Up @@ -94,6 +94,36 @@ fn test_calc_su_4() {
assert_eq!(su, 3);
}

#[test]
fn test_validate_storage() {
// SSD only node
let resources = Resources {
hru: 0,
cru: 0,
mru: 0,
sru: MINIMUM_STORAGE_CAPACITY as u64,
};
assert_eq!(resources.validate_storage(), true);

// HDD only node
let resources = Resources {
hru: MINIMUM_STORAGE_CAPACITY as u64,
cru: 0,
mru: 0,
sru: 0,
};
assert_eq!(resources.validate_storage(), true);

// Storage capacities are both under minimum requirement
let resources = Resources {
hru: MINIMUM_STORAGE_CAPACITY as u64 - 1,
cru: 0,
mru: 0,
sru: MINIMUM_STORAGE_CAPACITY as u64 - 1,
};
assert_eq!(resources.validate_storage(), false);
}

#[test]
fn test_resources_diff() {
let resources = Resources {
Expand Down

0 comments on commit 62df060

Please sign in to comment.