Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions runtime-modules/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ pub trait DataObjectStorage<T: Trait> {
/// - balance of data size fee + total deletion prize is transferred from caller to treasury account
fn upload_data_objects(params: UploadParameters<T>) -> DispatchResult;

/// Returns the funds needed to upload a num of objects
/// This is dependent on (data_obj_deletion_prize * num_of_objs_to_upload) plus a storage fee that depends on the
/// objs_total_size_in_bytes
fn funds_needed_for_upload(
num_of_objs_to_upload: usize,
objs_total_size_in_bytes: u64,
) -> BalanceOf<T>;

/// Validates moving objects parameters.
/// Validates voucher usage for affected buckets.
///
Expand Down Expand Up @@ -3493,6 +3501,19 @@ impl<T: Trait> DataObjectStorage<T> for Module<T> {
.map(|x| x.0)
.collect()
}

fn funds_needed_for_upload(
num_of_objs_to_upload: usize,
objs_total_size_in_bytes: u64,
) -> BalanceOf<T> {
let num_of_objs_to_upload = num_of_objs_to_upload.saturated_into();
let deletion_fee =
Self::data_object_deletion_prize_value().saturating_mul(num_of_objs_to_upload);

let storage_fee = Self::calculate_data_storage_fee(objs_total_size_in_bytes);

deletion_fee.saturating_add(storage_fee)
}
}

impl<T: Trait> Module<T> {
Expand Down
100 changes: 94 additions & 6 deletions runtime-modules/storage/src/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ use frame_support::traits::{Currency, OnFinalize, OnInitialize};
use frame_system::{EventRecord, Phase, RawOrigin};
use sp_std::collections::btree_map::BTreeMap;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::iter::FromIterator;

use crate::sp_api_hidden_includes_decl_storage::hidden_include::IterableStorageDoubleMap;
use crate::sp_api_hidden_includes_decl_storage::hidden_include::StorageDoubleMap;
use crate::sp_api_hidden_includes_decl_storage::hidden_include::{
IterableStorageDoubleMap, StorageDoubleMap, StorageValue,
};

use super::mocks::{
create_cid, Balances, CollectiveFlip, Storage, System, Test, TestEvent,
DEFAULT_DISTRIBUTION_PROVIDER_ACCOUNT_ID, DEFAULT_MEMBER_ACCOUNT_ID, DEFAULT_MEMBER_ID,
DEFAULT_STORAGE_BUCKET_OBJECTS_LIMIT, DEFAULT_STORAGE_BUCKET_SIZE_LIMIT,
DEFAULT_STORAGE_PROVIDER_ACCOUNT_ID, DISTRIBUTION_WG_LEADER_ACCOUNT_ID,
STORAGE_WG_LEADER_ACCOUNT_ID,
STORAGE_WG_LEADER_ACCOUNT_ID, VOUCHER_OBJECTS_LIMIT, VOUCHER_SIZE_LIMIT,
};

use crate::{
BagId, Cid, DataObjectCreationParameters, DataObjectStorage, DistributionBucket,
DistributionBucketId, DynBagCreationParameters, DynamicBagId, DynamicBagType, RawEvent,
StaticBagId, StorageBucketOperatorStatus, UploadParameters,
BagId, Cid, DataObjectCreationParameters, DataObjectDeletionPrizeValue,
DataObjectPerMegabyteFee, DataObjectStorage, DistributionBucket, DistributionBucketId,
DynBagCreationParameters, DynamicBagId, DynamicBagType, RawEvent, StaticBagId,
StorageBucketOperatorStatus, UploadParameters,
};

// Recommendation from Parity on testing on_finalize
Expand All @@ -40,6 +43,91 @@ pub fn increase_account_balance(account_id: &u64, balance: u64) {
let _ = Balances::deposit_creating(&account_id, balance);
}

pub fn set_data_object_per_mega_byte_fee(mb_fee: u64) {
DataObjectPerMegabyteFee::<Test>::put(mb_fee);
}

pub fn set_data_object_deletion_prize_value(deletion_prize: u64) {
DataObjectDeletionPrizeValue::<Test>::put(deletion_prize);
}

pub fn set_max_voucher_limits() {
set_max_voucher_limits_with_params(VOUCHER_SIZE_LIMIT, VOUCHER_OBJECTS_LIMIT);
}

pub fn set_max_voucher_limits_with_params(size_limit: u64, objects_limit: u64) {
UpdateStorageBucketsVoucherMaxLimitsFixture::default()
.with_new_objects_size_limit(size_limit)
.with_new_objects_number_limit(objects_limit)
.call_and_assert(Ok(()));
}

pub fn create_storage_buckets(buckets_number: u64) -> BTreeSet<u64> {
set_max_voucher_limits();
CreateStorageBucketFixture::default()
.with_origin(RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID))
.with_objects_limit(DEFAULT_STORAGE_BUCKET_OBJECTS_LIMIT)
.with_size_limit(DEFAULT_STORAGE_BUCKET_SIZE_LIMIT)
.create_several(buckets_number)
}

pub fn create_default_storage_bucket_and_assign_to_bag(bag_id: BagId<Test>) -> u64 {
let objects_limit = 1;
let size_limit = 100;

create_storage_bucket_and_assign_to_bag(bag_id, None, objects_limit, size_limit)
}

pub fn create_storage_bucket_and_assign_to_bag(
bag_id: BagId<Test>,
storage_provider_id: Option<u64>,
objects_limit: u64,
size_limit: u64,
) -> u64 {
set_max_voucher_limits();
set_default_update_storage_buckets_per_bag_limit();

let bucket_id = CreateStorageBucketFixture::default()
.with_origin(RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID))
.with_invite_worker(storage_provider_id)
.with_objects_limit(objects_limit)
.with_size_limit(size_limit)
.call_and_assert(Ok(()))
.unwrap();

let buckets = BTreeSet::from_iter(vec![bucket_id]);

UpdateStorageBucketForBagsFixture::default()
.with_origin(RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID))
.with_bag_id(bag_id.clone())
.with_add_bucket_ids(buckets.clone())
.call_and_assert(Ok(()));

if let Some(storage_provider_id) = storage_provider_id {
AcceptStorageBucketInvitationFixture::default()
.with_origin(RawOrigin::Signed(DEFAULT_STORAGE_PROVIDER_ACCOUNT_ID))
.with_transactor_account_id(DEFAULT_STORAGE_PROVIDER_ACCOUNT_ID)
.with_storage_bucket_id(bucket_id)
.with_worker_id(storage_provider_id)
.call_and_assert(Ok(()));
}

bucket_id
}

pub fn set_update_storage_buckets_per_bag_limit(new_limit: u64) {
UpdateStorageBucketsPerBagLimitFixture::default()
.with_origin(RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID))
.with_new_limit(new_limit)
.call_and_assert(Ok(()))
}

pub fn set_default_update_storage_buckets_per_bag_limit() {
let new_limit = 7;

set_update_storage_buckets_per_bag_limit(new_limit);
}

pub struct EventFixture;
impl EventFixture {
pub fn assert_last_crate_event(
Expand Down
2 changes: 1 addition & 1 deletion runtime-modules/storage/src/tests/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub const VOUCHER_OBJECTS_LIMIT: u64 = 20;
pub const DEFAULT_STORAGE_BUCKET_SIZE_LIMIT: u64 = 100;
pub const DEFAULT_STORAGE_BUCKET_OBJECTS_LIMIT: u64 = 10;
pub const DEFAULT_STORAGE_BUCKETS_NUMBER: u64 = 3;

pub const ONE_MB: u64 = 1_048_576;
impl crate::Trait for Test {
type Event = TestEvent;
type DataObjectId = u64;
Expand Down
Loading