fixtures.rs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. use frame_support::dispatch::DispatchResult;
  2. use frame_support::storage::StorageMap;
  3. use frame_support::traits::{Currency, OnFinalize, OnInitialize};
  4. use frame_system::{EventRecord, Phase, RawOrigin};
  5. use sp_std::collections::btree_map::BTreeMap;
  6. use sp_std::collections::btree_set::BTreeSet;
  7. use super::mocks::{
  8. Balances, CollectiveFlip, Storage, System, Test, TestEvent, DEFAULT_MEMBER_ACCOUNT_ID,
  9. DEFAULT_STORAGE_PROVIDER_ACCOUNT_ID, STORAGE_WG_LEADER_ACCOUNT_ID,
  10. };
  11. use crate::tests::mocks::{
  12. DEFAULT_DISTRIBUTION_PROVIDER_ACCOUNT_ID, DISTRIBUTION_WG_LEADER_ACCOUNT_ID,
  13. };
  14. use crate::{
  15. BagId, Cid, DataObjectCreationParameters, DataObjectStorage, DistributionBucket,
  16. DistributionBucketId, DynamicBagDeletionPrize, DynamicBagId, DynamicBagType, RawEvent,
  17. StaticBagId, StorageBucketOperatorStatus, UploadParameters,
  18. };
  19. // Recommendation from Parity on testing on_finalize
  20. // https://substrate.dev/docs/en/next/development/module/tests
  21. pub fn run_to_block(n: u64) {
  22. while System::block_number() < n {
  23. <System as OnFinalize<u64>>::on_finalize(System::block_number());
  24. <CollectiveFlip as OnFinalize<u64>>::on_finalize(System::block_number());
  25. <Storage as OnFinalize<u64>>::on_finalize(System::block_number());
  26. System::set_block_number(System::block_number() + 1);
  27. <System as OnInitialize<u64>>::on_initialize(System::block_number());
  28. <CollectiveFlip as OnInitialize<u64>>::on_initialize(System::block_number());
  29. <Storage as OnInitialize<u64>>::on_initialize(System::block_number());
  30. }
  31. }
  32. pub fn increase_account_balance(account_id: &u64, balance: u64) {
  33. let _ = Balances::deposit_creating(&account_id, balance);
  34. }
  35. pub struct EventFixture;
  36. impl EventFixture {
  37. pub fn assert_last_crate_event(
  38. expected_raw_event: RawEvent<
  39. u64,
  40. u64,
  41. u64,
  42. UploadParameters<Test>,
  43. BagId<Test>,
  44. DynamicBagId<Test>,
  45. u64,
  46. u64,
  47. u64,
  48. DistributionBucketId<Test>,
  49. u64,
  50. >,
  51. ) {
  52. let converted_event = TestEvent::storage(expected_raw_event);
  53. Self::assert_last_global_event(converted_event)
  54. }
  55. pub fn contains_crate_event(
  56. expected_raw_event: RawEvent<
  57. u64,
  58. u64,
  59. u64,
  60. UploadParameters<Test>,
  61. BagId<Test>,
  62. DynamicBagId<Test>,
  63. u64,
  64. u64,
  65. u64,
  66. DistributionBucketId<Test>,
  67. u64,
  68. >,
  69. ) {
  70. let converted_event = TestEvent::storage(expected_raw_event);
  71. Self::contains_global_event(converted_event)
  72. }
  73. pub fn assert_last_global_event(expected_event: TestEvent) {
  74. let expected_event = EventRecord {
  75. phase: Phase::Initialization,
  76. event: expected_event,
  77. topics: vec![],
  78. };
  79. assert_eq!(System::events().pop().unwrap(), expected_event);
  80. }
  81. fn contains_global_event(expected_event: TestEvent) {
  82. let expected_event = EventRecord {
  83. phase: Phase::Initialization,
  84. event: expected_event,
  85. topics: vec![],
  86. };
  87. assert!(System::events().iter().any(|ev| *ev == expected_event));
  88. }
  89. }
  90. const DEFAULT_ACCOUNT_ID: u64 = 1;
  91. const DEFAULT_WORKER_ID: u64 = 1;
  92. pub struct CreateStorageBucketFixture {
  93. origin: RawOrigin<u64>,
  94. invite_worker: Option<u64>,
  95. accepting_new_bags: bool,
  96. size_limit: u64,
  97. objects_limit: u64,
  98. }
  99. impl CreateStorageBucketFixture {
  100. pub fn default() -> Self {
  101. Self {
  102. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  103. invite_worker: None,
  104. accepting_new_bags: true,
  105. size_limit: 0,
  106. objects_limit: 0,
  107. }
  108. }
  109. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  110. Self { origin, ..self }
  111. }
  112. pub fn with_invite_worker(self, invite_worker: Option<u64>) -> Self {
  113. Self {
  114. invite_worker,
  115. ..self
  116. }
  117. }
  118. pub fn with_accepting_new_bags(self, accepting_new_bags: bool) -> Self {
  119. Self {
  120. accepting_new_bags,
  121. ..self
  122. }
  123. }
  124. pub fn with_size_limit(self, size_limit: u64) -> Self {
  125. Self { size_limit, ..self }
  126. }
  127. pub fn with_objects_limit(self, objects_limit: u64) -> Self {
  128. Self {
  129. objects_limit,
  130. ..self
  131. }
  132. }
  133. pub fn call_and_assert(&self, expected_result: DispatchResult) -> Option<u64> {
  134. let next_storage_bucket_id = Storage::next_storage_bucket_id();
  135. let actual_result = Storage::create_storage_bucket(
  136. self.origin.clone().into(),
  137. self.invite_worker,
  138. self.accepting_new_bags,
  139. self.size_limit,
  140. self.objects_limit,
  141. );
  142. assert_eq!(actual_result, expected_result);
  143. if actual_result.is_ok() {
  144. assert_eq!(
  145. next_storage_bucket_id + 1,
  146. Storage::next_storage_bucket_id()
  147. );
  148. assert!(<crate::StorageBucketById<Test>>::contains_key(
  149. next_storage_bucket_id
  150. ));
  151. Some(next_storage_bucket_id)
  152. } else {
  153. assert_eq!(next_storage_bucket_id, Storage::next_storage_bucket_id());
  154. assert!(!<crate::StorageBucketById<Test>>::contains_key(
  155. next_storage_bucket_id
  156. ));
  157. None
  158. }
  159. }
  160. }
  161. pub struct AcceptStorageBucketInvitationFixture {
  162. origin: RawOrigin<u64>,
  163. worker_id: u64,
  164. storage_bucket_id: u64,
  165. transactor_account_id: u64,
  166. }
  167. impl AcceptStorageBucketInvitationFixture {
  168. pub fn default() -> Self {
  169. Self {
  170. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  171. worker_id: DEFAULT_WORKER_ID,
  172. storage_bucket_id: Default::default(),
  173. transactor_account_id: DEFAULT_ACCOUNT_ID,
  174. }
  175. }
  176. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  177. Self { origin, ..self }
  178. }
  179. pub fn with_worker_id(self, worker_id: u64) -> Self {
  180. Self { worker_id, ..self }
  181. }
  182. pub fn with_transactor_account_id(self, transactor_account_id: u64) -> Self {
  183. Self {
  184. transactor_account_id,
  185. ..self
  186. }
  187. }
  188. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  189. Self {
  190. storage_bucket_id,
  191. ..self
  192. }
  193. }
  194. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  195. let old_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  196. let actual_result = Storage::accept_storage_bucket_invitation(
  197. self.origin.clone().into(),
  198. self.worker_id,
  199. self.storage_bucket_id,
  200. self.transactor_account_id,
  201. );
  202. assert_eq!(actual_result, expected_result);
  203. let new_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  204. if actual_result.is_ok() {
  205. assert_eq!(
  206. new_bucket.operator_status,
  207. StorageBucketOperatorStatus::StorageWorker(
  208. self.worker_id,
  209. self.transactor_account_id
  210. )
  211. );
  212. } else {
  213. assert_eq!(old_bucket, new_bucket);
  214. }
  215. }
  216. }
  217. pub struct UpdateStorageBucketForBagsFixture {
  218. origin: RawOrigin<u64>,
  219. bag_id: BagId<Test>,
  220. add_bucket_ids: BTreeSet<u64>,
  221. remove_bucket_ids: BTreeSet<u64>,
  222. }
  223. impl UpdateStorageBucketForBagsFixture {
  224. pub fn default() -> Self {
  225. Self {
  226. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  227. bag_id: Default::default(),
  228. add_bucket_ids: Default::default(),
  229. remove_bucket_ids: Default::default(),
  230. }
  231. }
  232. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  233. Self { origin, ..self }
  234. }
  235. pub fn with_add_bucket_ids(self, add_bucket_ids: BTreeSet<u64>) -> Self {
  236. Self {
  237. add_bucket_ids,
  238. ..self
  239. }
  240. }
  241. pub fn with_remove_bucket_ids(self, remove_bucket_ids: BTreeSet<u64>) -> Self {
  242. Self {
  243. remove_bucket_ids,
  244. ..self
  245. }
  246. }
  247. pub fn with_bag_id(self, bag_id: BagId<Test>) -> Self {
  248. Self { bag_id, ..self }
  249. }
  250. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  251. let actual_result = Storage::update_storage_buckets_for_bag(
  252. self.origin.clone().into(),
  253. self.bag_id.clone(),
  254. self.add_bucket_ids.clone(),
  255. self.remove_bucket_ids.clone(),
  256. );
  257. assert_eq!(actual_result, expected_result);
  258. }
  259. }
  260. pub struct UploadFixture {
  261. params: UploadParameters<Test>,
  262. }
  263. impl UploadFixture {
  264. pub fn default() -> Self {
  265. Self {
  266. params: Default::default(),
  267. }
  268. }
  269. pub fn with_params(self, params: UploadParameters<Test>) -> Self {
  270. Self { params, ..self }
  271. }
  272. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  273. let old_next_data_object_id = Storage::next_data_object_id();
  274. let actual_result = Storage::upload_data_objects(self.params.clone());
  275. assert_eq!(actual_result, expected_result);
  276. if actual_result.is_ok() {
  277. // check next data object ID
  278. assert_eq!(
  279. Storage::next_data_object_id(),
  280. old_next_data_object_id + self.params.object_creation_list.len() as u64
  281. );
  282. } else {
  283. assert_eq!(Storage::next_data_object_id(), old_next_data_object_id);
  284. }
  285. }
  286. }
  287. pub fn create_data_object_candidates(
  288. starting_index: u8,
  289. number: u8,
  290. ) -> Vec<DataObjectCreationParameters> {
  291. let range = starting_index..(starting_index + number);
  292. range
  293. .into_iter()
  294. .map(|idx| DataObjectCreationParameters {
  295. size: 10 * idx as u64,
  296. ipfs_content_id: vec![idx],
  297. })
  298. .collect()
  299. }
  300. pub fn create_single_data_object() -> Vec<DataObjectCreationParameters> {
  301. create_data_object_candidates(1, 1)
  302. }
  303. pub struct SetStorageOperatorMetadataFixture {
  304. origin: RawOrigin<u64>,
  305. worker_id: u64,
  306. storage_bucket_id: u64,
  307. metadata: Vec<u8>,
  308. }
  309. impl SetStorageOperatorMetadataFixture {
  310. pub fn default() -> Self {
  311. Self {
  312. origin: RawOrigin::Signed(DEFAULT_MEMBER_ACCOUNT_ID),
  313. worker_id: DEFAULT_WORKER_ID,
  314. storage_bucket_id: Default::default(),
  315. metadata: Vec::new(),
  316. }
  317. }
  318. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  319. Self { origin, ..self }
  320. }
  321. pub fn with_worker_id(self, worker_id: u64) -> Self {
  322. Self { worker_id, ..self }
  323. }
  324. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  325. Self {
  326. storage_bucket_id,
  327. ..self
  328. }
  329. }
  330. pub fn with_metadata(self, metadata: Vec<u8>) -> Self {
  331. Self { metadata, ..self }
  332. }
  333. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  334. let actual_result = Storage::set_storage_operator_metadata(
  335. self.origin.clone().into(),
  336. self.worker_id,
  337. self.storage_bucket_id,
  338. self.metadata.clone(),
  339. );
  340. assert_eq!(actual_result, expected_result);
  341. }
  342. }
  343. pub struct AcceptPendingDataObjectsFixture {
  344. origin: RawOrigin<u64>,
  345. worker_id: u64,
  346. storage_bucket_id: u64,
  347. bag_id: BagId<Test>,
  348. data_object_ids: BTreeSet<u64>,
  349. }
  350. impl AcceptPendingDataObjectsFixture {
  351. pub fn default() -> Self {
  352. Self {
  353. origin: RawOrigin::Signed(DEFAULT_STORAGE_PROVIDER_ACCOUNT_ID),
  354. worker_id: DEFAULT_WORKER_ID,
  355. storage_bucket_id: Default::default(),
  356. data_object_ids: Default::default(),
  357. bag_id: Default::default(),
  358. }
  359. }
  360. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  361. Self { origin, ..self }
  362. }
  363. pub fn with_worker_id(self, worker_id: u64) -> Self {
  364. Self { worker_id, ..self }
  365. }
  366. pub fn with_bag_id(self, bag_id: BagId<Test>) -> Self {
  367. Self { bag_id, ..self }
  368. }
  369. pub fn with_data_object_ids(self, data_object_ids: BTreeSet<u64>) -> Self {
  370. Self {
  371. data_object_ids,
  372. ..self
  373. }
  374. }
  375. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  376. Self {
  377. storage_bucket_id,
  378. ..self
  379. }
  380. }
  381. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  382. let actual_result = Storage::accept_pending_data_objects(
  383. self.origin.clone().into(),
  384. self.worker_id,
  385. self.storage_bucket_id,
  386. self.bag_id.clone(),
  387. self.data_object_ids.clone(),
  388. );
  389. assert_eq!(actual_result, expected_result);
  390. }
  391. }
  392. pub struct CancelStorageBucketInvitationFixture {
  393. origin: RawOrigin<u64>,
  394. storage_bucket_id: u64,
  395. }
  396. impl CancelStorageBucketInvitationFixture {
  397. pub fn default() -> Self {
  398. Self {
  399. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  400. storage_bucket_id: Default::default(),
  401. }
  402. }
  403. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  404. Self { origin, ..self }
  405. }
  406. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  407. Self {
  408. storage_bucket_id,
  409. ..self
  410. }
  411. }
  412. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  413. let old_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  414. let actual_result = Storage::cancel_storage_bucket_operator_invite(
  415. self.origin.clone().into(),
  416. self.storage_bucket_id,
  417. );
  418. assert_eq!(actual_result, expected_result);
  419. let new_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  420. if actual_result.is_ok() {
  421. assert_eq!(
  422. new_bucket.operator_status,
  423. StorageBucketOperatorStatus::Missing
  424. );
  425. } else {
  426. assert_eq!(old_bucket, new_bucket);
  427. }
  428. }
  429. }
  430. pub struct InviteStorageBucketOperatorFixture {
  431. origin: RawOrigin<u64>,
  432. operator_worker_id: u64,
  433. storage_bucket_id: u64,
  434. }
  435. impl InviteStorageBucketOperatorFixture {
  436. pub fn default() -> Self {
  437. Self {
  438. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  439. operator_worker_id: DEFAULT_WORKER_ID,
  440. storage_bucket_id: Default::default(),
  441. }
  442. }
  443. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  444. Self { origin, ..self }
  445. }
  446. pub fn with_operator_worker_id(self, operator_worker_id: u64) -> Self {
  447. Self {
  448. operator_worker_id,
  449. ..self
  450. }
  451. }
  452. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  453. Self {
  454. storage_bucket_id,
  455. ..self
  456. }
  457. }
  458. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  459. let old_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  460. let actual_result = Storage::invite_storage_bucket_operator(
  461. self.origin.clone().into(),
  462. self.storage_bucket_id,
  463. self.operator_worker_id,
  464. );
  465. assert_eq!(actual_result, expected_result);
  466. let new_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  467. if actual_result.is_ok() {
  468. assert_eq!(
  469. new_bucket.operator_status,
  470. StorageBucketOperatorStatus::InvitedStorageWorker(self.operator_worker_id)
  471. );
  472. } else {
  473. assert_eq!(old_bucket, new_bucket);
  474. }
  475. }
  476. }
  477. pub struct UpdateUploadingBlockedStatusFixture {
  478. origin: RawOrigin<u64>,
  479. new_status: bool,
  480. }
  481. impl UpdateUploadingBlockedStatusFixture {
  482. pub fn default() -> Self {
  483. Self {
  484. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  485. new_status: false,
  486. }
  487. }
  488. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  489. Self { origin, ..self }
  490. }
  491. pub fn with_new_status(self, new_status: bool) -> Self {
  492. Self { new_status, ..self }
  493. }
  494. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  495. let old_status = Storage::uploading_blocked();
  496. let actual_result =
  497. Storage::update_uploading_blocked_status(self.origin.clone().into(), self.new_status);
  498. assert_eq!(actual_result, expected_result);
  499. if actual_result.is_ok() {
  500. assert_eq!(Storage::uploading_blocked(), self.new_status);
  501. } else {
  502. assert_eq!(old_status, Storage::uploading_blocked());
  503. }
  504. }
  505. }
  506. pub struct MoveDataObjectsFixture {
  507. src_bag_id: BagId<Test>,
  508. dest_bag_id: BagId<Test>,
  509. data_object_ids: BTreeSet<u64>,
  510. }
  511. impl MoveDataObjectsFixture {
  512. pub fn default() -> Self {
  513. Self {
  514. src_bag_id: BagId::<Test>::Static(StaticBagId::Council),
  515. dest_bag_id: BagId::<Test>::Static(StaticBagId::Council),
  516. data_object_ids: BTreeSet::new(),
  517. }
  518. }
  519. pub fn with_src_bag_id(self, src_bag_id: BagId<Test>) -> Self {
  520. Self { src_bag_id, ..self }
  521. }
  522. pub fn with_dest_bag_id(self, dest_bag_id: BagId<Test>) -> Self {
  523. Self {
  524. dest_bag_id,
  525. ..self
  526. }
  527. }
  528. pub fn with_data_object_ids(self, data_object_ids: BTreeSet<u64>) -> Self {
  529. Self {
  530. data_object_ids,
  531. ..self
  532. }
  533. }
  534. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  535. let actual_result = Storage::move_data_objects(
  536. self.src_bag_id.clone(),
  537. self.dest_bag_id.clone(),
  538. self.data_object_ids.clone(),
  539. );
  540. assert_eq!(actual_result, expected_result);
  541. }
  542. }
  543. pub struct DeleteDataObjectsFixture {
  544. deletion_prize_account_id: u64,
  545. bag_id: BagId<Test>,
  546. data_object_ids: BTreeSet<u64>,
  547. }
  548. impl DeleteDataObjectsFixture {
  549. pub fn default() -> Self {
  550. Self {
  551. bag_id: Default::default(),
  552. data_object_ids: Default::default(),
  553. deletion_prize_account_id: DEFAULT_ACCOUNT_ID,
  554. }
  555. }
  556. pub fn with_bag_id(self, bag_id: BagId<Test>) -> Self {
  557. Self { bag_id, ..self }
  558. }
  559. pub fn with_data_object_ids(self, data_object_ids: BTreeSet<u64>) -> Self {
  560. Self {
  561. data_object_ids,
  562. ..self
  563. }
  564. }
  565. pub fn with_deletion_account_id(self, deletion_prize_account_id: u64) -> Self {
  566. Self {
  567. deletion_prize_account_id,
  568. ..self
  569. }
  570. }
  571. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  572. let actual_result = Storage::delete_data_objects(
  573. self.deletion_prize_account_id,
  574. self.bag_id.clone(),
  575. self.data_object_ids.clone(),
  576. );
  577. assert_eq!(actual_result, expected_result);
  578. }
  579. }
  580. pub struct UpdateStorageBucketStatusFixture {
  581. origin: RawOrigin<u64>,
  582. storage_bucket_id: u64,
  583. new_status: bool,
  584. }
  585. impl UpdateStorageBucketStatusFixture {
  586. pub fn default() -> Self {
  587. Self {
  588. origin: RawOrigin::Signed(DEFAULT_MEMBER_ACCOUNT_ID),
  589. storage_bucket_id: Default::default(),
  590. new_status: false,
  591. }
  592. }
  593. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  594. Self { origin, ..self }
  595. }
  596. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  597. Self {
  598. storage_bucket_id,
  599. ..self
  600. }
  601. }
  602. pub fn with_new_status(self, new_status: bool) -> Self {
  603. Self { new_status, ..self }
  604. }
  605. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  606. let actual_result = Storage::update_storage_bucket_status(
  607. self.origin.clone().into(),
  608. self.storage_bucket_id,
  609. self.new_status,
  610. );
  611. assert_eq!(actual_result, expected_result);
  612. if actual_result.is_ok() {
  613. let bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  614. assert_eq!(bucket.accepting_new_bags, self.new_status);
  615. }
  616. }
  617. }
  618. pub struct UpdateBlacklistFixture {
  619. origin: RawOrigin<u64>,
  620. remove_hashes: BTreeSet<Cid>,
  621. add_hashes: BTreeSet<Cid>,
  622. }
  623. impl UpdateBlacklistFixture {
  624. pub fn default() -> Self {
  625. Self {
  626. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  627. remove_hashes: BTreeSet::new(),
  628. add_hashes: BTreeSet::new(),
  629. }
  630. }
  631. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  632. Self { origin, ..self }
  633. }
  634. pub fn with_add_hashes(self, add_hashes: BTreeSet<Cid>) -> Self {
  635. Self { add_hashes, ..self }
  636. }
  637. pub fn with_remove_hashes(self, remove_hashes: BTreeSet<Cid>) -> Self {
  638. Self {
  639. remove_hashes,
  640. ..self
  641. }
  642. }
  643. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  644. let actual_result = Storage::update_blacklist(
  645. self.origin.clone().into(),
  646. self.remove_hashes.clone(),
  647. self.add_hashes.clone(),
  648. );
  649. assert_eq!(actual_result, expected_result);
  650. }
  651. }
  652. pub struct DeleteDynamicBagFixture {
  653. bag_id: DynamicBagId<Test>,
  654. deletion_account_id: u64,
  655. }
  656. impl DeleteDynamicBagFixture {
  657. pub fn default() -> Self {
  658. Self {
  659. bag_id: Default::default(),
  660. deletion_account_id: DEFAULT_ACCOUNT_ID,
  661. }
  662. }
  663. pub fn with_deletion_account_id(self, deletion_account_id: u64) -> Self {
  664. Self {
  665. deletion_account_id,
  666. ..self
  667. }
  668. }
  669. pub fn with_bag_id(self, bag_id: DynamicBagId<Test>) -> Self {
  670. Self { bag_id, ..self }
  671. }
  672. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  673. let actual_result =
  674. Storage::delete_dynamic_bag(self.deletion_account_id, self.bag_id.clone());
  675. assert_eq!(actual_result, expected_result);
  676. }
  677. }
  678. pub struct DeleteStorageBucketFixture {
  679. origin: RawOrigin<u64>,
  680. storage_bucket_id: u64,
  681. }
  682. impl DeleteStorageBucketFixture {
  683. pub fn default() -> Self {
  684. Self {
  685. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  686. storage_bucket_id: Default::default(),
  687. }
  688. }
  689. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  690. Self { origin, ..self }
  691. }
  692. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  693. Self {
  694. storage_bucket_id,
  695. ..self
  696. }
  697. }
  698. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  699. let actual_result =
  700. Storage::delete_storage_bucket(self.origin.clone().into(), self.storage_bucket_id);
  701. assert_eq!(actual_result, expected_result);
  702. if actual_result.is_ok() {
  703. assert!(!<crate::StorageBucketById<Test>>::contains_key(
  704. self.storage_bucket_id
  705. ));
  706. }
  707. }
  708. }
  709. pub struct RemoveStorageBucketOperatorFixture {
  710. origin: RawOrigin<u64>,
  711. storage_bucket_id: u64,
  712. }
  713. impl RemoveStorageBucketOperatorFixture {
  714. pub fn default() -> Self {
  715. Self {
  716. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  717. storage_bucket_id: Default::default(),
  718. }
  719. }
  720. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  721. Self { origin, ..self }
  722. }
  723. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  724. Self {
  725. storage_bucket_id,
  726. ..self
  727. }
  728. }
  729. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  730. let old_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  731. let actual_result = Storage::remove_storage_bucket_operator(
  732. self.origin.clone().into(),
  733. self.storage_bucket_id,
  734. );
  735. assert_eq!(actual_result, expected_result);
  736. let new_bucket = Storage::storage_bucket_by_id(self.storage_bucket_id);
  737. if actual_result.is_ok() {
  738. assert_eq!(
  739. new_bucket.operator_status,
  740. StorageBucketOperatorStatus::Missing
  741. );
  742. } else {
  743. assert_eq!(old_bucket, new_bucket);
  744. }
  745. }
  746. }
  747. pub struct UpdateDataObjectPerMegabyteFeeFixture {
  748. origin: RawOrigin<u64>,
  749. new_fee: u64,
  750. }
  751. impl UpdateDataObjectPerMegabyteFeeFixture {
  752. pub fn default() -> Self {
  753. Self {
  754. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  755. new_fee: 0,
  756. }
  757. }
  758. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  759. Self { origin, ..self }
  760. }
  761. pub fn with_new_fee(self, new_fee: u64) -> Self {
  762. Self { new_fee, ..self }
  763. }
  764. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  765. let old_fee = Storage::data_object_per_mega_byte_fee();
  766. let actual_result = Storage::update_data_size_fee(self.origin.clone().into(), self.new_fee);
  767. assert_eq!(actual_result, expected_result);
  768. if actual_result.is_ok() {
  769. assert_eq!(Storage::data_object_per_mega_byte_fee(), self.new_fee);
  770. } else {
  771. assert_eq!(old_fee, Storage::data_object_per_mega_byte_fee());
  772. }
  773. }
  774. }
  775. pub struct UpdateStorageBucketsPerBagLimitFixture {
  776. origin: RawOrigin<u64>,
  777. new_limit: u64,
  778. }
  779. impl UpdateStorageBucketsPerBagLimitFixture {
  780. pub fn default() -> Self {
  781. Self {
  782. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  783. new_limit: 0,
  784. }
  785. }
  786. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  787. Self { origin, ..self }
  788. }
  789. pub fn with_new_limit(self, new_limit: u64) -> Self {
  790. Self { new_limit, ..self }
  791. }
  792. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  793. let old_fee = Storage::storage_buckets_per_bag_limit();
  794. let actual_result = Storage::update_storage_buckets_per_bag_limit(
  795. self.origin.clone().into(),
  796. self.new_limit,
  797. );
  798. assert_eq!(actual_result, expected_result);
  799. if actual_result.is_ok() {
  800. assert_eq!(Storage::storage_buckets_per_bag_limit(), self.new_limit);
  801. } else {
  802. assert_eq!(old_fee, Storage::storage_buckets_per_bag_limit());
  803. }
  804. }
  805. }
  806. pub struct SetStorageBucketVoucherLimitsFixture {
  807. origin: RawOrigin<u64>,
  808. storage_bucket_id: u64,
  809. new_objects_size_limit: u64,
  810. new_objects_number_limit: u64,
  811. }
  812. impl SetStorageBucketVoucherLimitsFixture {
  813. pub fn default() -> Self {
  814. Self {
  815. origin: RawOrigin::Signed(DEFAULT_MEMBER_ACCOUNT_ID),
  816. storage_bucket_id: Default::default(),
  817. new_objects_size_limit: 0,
  818. new_objects_number_limit: 0,
  819. }
  820. }
  821. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  822. Self { origin, ..self }
  823. }
  824. pub fn with_storage_bucket_id(self, storage_bucket_id: u64) -> Self {
  825. Self {
  826. storage_bucket_id,
  827. ..self
  828. }
  829. }
  830. pub fn with_new_objects_size_limit(self, new_objects_size_limit: u64) -> Self {
  831. Self {
  832. new_objects_size_limit,
  833. ..self
  834. }
  835. }
  836. pub fn with_new_objects_number_limit(self, new_objects_number_limit: u64) -> Self {
  837. Self {
  838. new_objects_number_limit,
  839. ..self
  840. }
  841. }
  842. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  843. let old_voucher = Storage::storage_bucket_by_id(self.storage_bucket_id).voucher;
  844. let actual_result = Storage::set_storage_bucket_voucher_limits(
  845. self.origin.clone().into(),
  846. self.storage_bucket_id,
  847. self.new_objects_size_limit.clone(),
  848. self.new_objects_number_limit.clone(),
  849. );
  850. assert_eq!(actual_result, expected_result);
  851. let new_voucher = Storage::storage_bucket_by_id(self.storage_bucket_id).voucher;
  852. if actual_result.is_ok() {
  853. assert_eq!(self.new_objects_size_limit, new_voucher.size_limit);
  854. assert_eq!(self.new_objects_number_limit, new_voucher.objects_limit);
  855. } else {
  856. assert_eq!(old_voucher.size_limit, new_voucher.size_limit);
  857. assert_eq!(old_voucher.objects_limit, new_voucher.objects_limit);
  858. }
  859. }
  860. }
  861. pub struct UpdateStorageBucketsVoucherMaxLimitsFixture {
  862. origin: RawOrigin<u64>,
  863. new_objects_size_limit: u64,
  864. new_objects_number_limit: u64,
  865. }
  866. impl UpdateStorageBucketsVoucherMaxLimitsFixture {
  867. pub fn default() -> Self {
  868. Self {
  869. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  870. new_objects_size_limit: 0,
  871. new_objects_number_limit: 0,
  872. }
  873. }
  874. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  875. Self { origin, ..self }
  876. }
  877. pub fn with_new_objects_size_limit(self, new_objects_size_limit: u64) -> Self {
  878. Self {
  879. new_objects_size_limit,
  880. ..self
  881. }
  882. }
  883. pub fn with_new_objects_number_limit(self, new_objects_number_limit: u64) -> Self {
  884. Self {
  885. new_objects_number_limit,
  886. ..self
  887. }
  888. }
  889. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  890. let old_size_limit = Storage::voucher_max_objects_size_limit();
  891. let old_number_limit = Storage::voucher_max_objects_number_limit();
  892. let actual_result = Storage::update_storage_buckets_voucher_max_limits(
  893. self.origin.clone().into(),
  894. self.new_objects_size_limit.clone(),
  895. self.new_objects_number_limit.clone(),
  896. );
  897. assert_eq!(actual_result, expected_result);
  898. if actual_result.is_ok() {
  899. assert_eq!(
  900. self.new_objects_size_limit,
  901. Storage::voucher_max_objects_size_limit()
  902. );
  903. assert_eq!(
  904. self.new_objects_number_limit,
  905. Storage::voucher_max_objects_number_limit()
  906. );
  907. } else {
  908. assert_eq!(old_size_limit, Storage::voucher_max_objects_size_limit());
  909. assert_eq!(
  910. old_number_limit,
  911. Storage::voucher_max_objects_number_limit()
  912. );
  913. }
  914. }
  915. }
  916. pub struct CreateDynamicBagFixture {
  917. bag_id: DynamicBagId<Test>,
  918. deletion_prize: Option<DynamicBagDeletionPrize<Test>>,
  919. }
  920. impl CreateDynamicBagFixture {
  921. pub fn default() -> Self {
  922. Self {
  923. bag_id: Default::default(),
  924. deletion_prize: Default::default(),
  925. }
  926. }
  927. pub fn with_bag_id(self, bag_id: DynamicBagId<Test>) -> Self {
  928. Self { bag_id, ..self }
  929. }
  930. pub fn with_deletion_prize(self, deletion_prize: DynamicBagDeletionPrize<Test>) -> Self {
  931. Self {
  932. deletion_prize: Some(deletion_prize),
  933. ..self
  934. }
  935. }
  936. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  937. let actual_result =
  938. Storage::create_dynamic_bag(self.bag_id.clone(), self.deletion_prize.clone());
  939. assert_eq!(actual_result, expected_result);
  940. if actual_result.is_ok() {
  941. let bag_id: BagId<Test> = self.bag_id.clone().into();
  942. assert!(<crate::Bags<Test>>::contains_key(&bag_id));
  943. }
  944. }
  945. }
  946. pub struct UpdateNumberOfStorageBucketsInDynamicBagCreationPolicyFixture {
  947. origin: RawOrigin<u64>,
  948. new_storage_buckets_number: u64,
  949. dynamic_bag_type: DynamicBagType,
  950. }
  951. impl UpdateNumberOfStorageBucketsInDynamicBagCreationPolicyFixture {
  952. pub fn default() -> Self {
  953. Self {
  954. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  955. new_storage_buckets_number: 0,
  956. dynamic_bag_type: Default::default(),
  957. }
  958. }
  959. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  960. Self { origin, ..self }
  961. }
  962. pub fn with_new_storage_buckets_number(self, new_storage_buckets_number: u64) -> Self {
  963. Self {
  964. new_storage_buckets_number,
  965. ..self
  966. }
  967. }
  968. pub fn with_dynamic_bag_type(self, dynamic_bag_type: DynamicBagType) -> Self {
  969. Self {
  970. dynamic_bag_type,
  971. ..self
  972. }
  973. }
  974. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  975. let old_policy = Storage::get_dynamic_bag_creation_policy(self.dynamic_bag_type);
  976. let actual_result =
  977. Storage::update_number_of_storage_buckets_in_dynamic_bag_creation_policy(
  978. self.origin.clone().into(),
  979. self.dynamic_bag_type,
  980. self.new_storage_buckets_number,
  981. );
  982. assert_eq!(actual_result, expected_result);
  983. let new_policy = Storage::get_dynamic_bag_creation_policy(self.dynamic_bag_type);
  984. if actual_result.is_ok() {
  985. assert_eq!(
  986. new_policy.number_of_storage_buckets,
  987. self.new_storage_buckets_number
  988. );
  989. } else {
  990. assert_eq!(old_policy, new_policy);
  991. }
  992. }
  993. }
  994. pub struct CreateDistributionBucketFamilyFixture {
  995. origin: RawOrigin<u64>,
  996. }
  997. impl CreateDistributionBucketFamilyFixture {
  998. pub fn default() -> Self {
  999. Self {
  1000. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  1001. }
  1002. }
  1003. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1004. Self { origin, ..self }
  1005. }
  1006. pub fn call_and_assert(&self, expected_result: DispatchResult) -> Option<u64> {
  1007. let next_family_id = Storage::next_distribution_bucket_family_id();
  1008. let family_number = Storage::distribution_bucket_family_number();
  1009. let actual_result = Storage::create_distribution_bucket_family(self.origin.clone().into());
  1010. assert_eq!(actual_result, expected_result);
  1011. if actual_result.is_ok() {
  1012. assert_eq!(
  1013. next_family_id + 1,
  1014. Storage::next_distribution_bucket_family_id()
  1015. );
  1016. assert_eq!(
  1017. family_number + 1,
  1018. Storage::distribution_bucket_family_number()
  1019. );
  1020. assert!(<crate::DistributionBucketFamilyById<Test>>::contains_key(
  1021. next_family_id
  1022. ));
  1023. Some(next_family_id)
  1024. } else {
  1025. assert_eq!(
  1026. next_family_id,
  1027. Storage::next_distribution_bucket_family_id()
  1028. );
  1029. assert_eq!(family_number, Storage::distribution_bucket_family_number());
  1030. assert!(!<crate::DistributionBucketFamilyById<Test>>::contains_key(
  1031. next_family_id
  1032. ));
  1033. None
  1034. }
  1035. }
  1036. }
  1037. pub struct DeleteDistributionBucketFamilyFixture {
  1038. origin: RawOrigin<u64>,
  1039. family_id: u64,
  1040. }
  1041. impl DeleteDistributionBucketFamilyFixture {
  1042. pub fn default() -> Self {
  1043. Self {
  1044. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  1045. family_id: Default::default(),
  1046. }
  1047. }
  1048. pub fn with_family_id(self, family_id: u64) -> Self {
  1049. Self { family_id, ..self }
  1050. }
  1051. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1052. Self { origin, ..self }
  1053. }
  1054. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1055. let family_number = Storage::distribution_bucket_family_number();
  1056. let actual_result =
  1057. Storage::delete_distribution_bucket_family(self.origin.clone().into(), self.family_id);
  1058. assert_eq!(actual_result, expected_result);
  1059. if actual_result.is_ok() {
  1060. assert_eq!(
  1061. family_number - 1,
  1062. Storage::distribution_bucket_family_number()
  1063. );
  1064. assert!(!<crate::DistributionBucketFamilyById<Test>>::contains_key(
  1065. self.family_id
  1066. ));
  1067. } else {
  1068. assert_eq!(family_number, Storage::distribution_bucket_family_number());
  1069. }
  1070. }
  1071. }
  1072. pub struct CreateDistributionBucketFixture {
  1073. origin: RawOrigin<u64>,
  1074. family_id: u64,
  1075. accept_new_bags: bool,
  1076. }
  1077. impl CreateDistributionBucketFixture {
  1078. pub fn default() -> Self {
  1079. Self {
  1080. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  1081. family_id: Default::default(),
  1082. accept_new_bags: false,
  1083. }
  1084. }
  1085. pub fn with_family_id(self, family_id: u64) -> Self {
  1086. Self { family_id, ..self }
  1087. }
  1088. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1089. Self { origin, ..self }
  1090. }
  1091. pub fn with_accept_new_bags(self, accept_new_bags: bool) -> Self {
  1092. Self {
  1093. accept_new_bags,
  1094. ..self
  1095. }
  1096. }
  1097. pub fn call_and_assert(&self, expected_result: DispatchResult) -> Option<u64> {
  1098. let next_bucket_index = Storage::distribution_bucket_family_by_id(self.family_id)
  1099. .next_distribution_bucket_index;
  1100. let actual_result = Storage::create_distribution_bucket(
  1101. self.origin.clone().into(),
  1102. self.family_id,
  1103. self.accept_new_bags,
  1104. );
  1105. assert_eq!(actual_result, expected_result);
  1106. if actual_result.is_ok() {
  1107. assert_eq!(
  1108. next_bucket_index + 1,
  1109. Storage::distribution_bucket_family_by_id(self.family_id)
  1110. .next_distribution_bucket_index
  1111. );
  1112. let bucket: DistributionBucket<Test> =
  1113. Storage::distribution_bucket_by_family_id_by_index(
  1114. self.family_id,
  1115. next_bucket_index,
  1116. );
  1117. assert_eq!(bucket.accepting_new_bags, self.accept_new_bags);
  1118. Some(next_bucket_index)
  1119. } else {
  1120. assert_eq!(
  1121. next_bucket_index,
  1122. Storage::distribution_bucket_family_by_id(self.family_id)
  1123. .next_distribution_bucket_index
  1124. );
  1125. None
  1126. }
  1127. }
  1128. }
  1129. pub struct UpdateDistributionBucketStatusFixture {
  1130. origin: RawOrigin<u64>,
  1131. family_id: u64,
  1132. distribution_bucket_index: u64,
  1133. new_status: bool,
  1134. }
  1135. impl UpdateDistributionBucketStatusFixture {
  1136. pub fn default() -> Self {
  1137. Self {
  1138. origin: RawOrigin::Signed(DEFAULT_MEMBER_ACCOUNT_ID),
  1139. family_id: Default::default(),
  1140. distribution_bucket_index: Default::default(),
  1141. new_status: false,
  1142. }
  1143. }
  1144. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1145. Self {
  1146. distribution_bucket_index: bucket_index,
  1147. ..self
  1148. }
  1149. }
  1150. pub fn with_family_id(self, family_id: u64) -> Self {
  1151. Self { family_id, ..self }
  1152. }
  1153. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1154. Self { origin, ..self }
  1155. }
  1156. pub fn with_new_status(self, new_status: bool) -> Self {
  1157. Self { new_status, ..self }
  1158. }
  1159. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1160. let actual_result = Storage::update_distribution_bucket_status(
  1161. self.origin.clone().into(),
  1162. Storage::create_distribution_bucket_id(self.family_id, self.distribution_bucket_index),
  1163. self.new_status,
  1164. );
  1165. assert_eq!(actual_result, expected_result);
  1166. }
  1167. }
  1168. pub struct DeleteDistributionBucketFixture {
  1169. origin: RawOrigin<u64>,
  1170. family_id: u64,
  1171. distribution_bucket_index: u64,
  1172. }
  1173. impl DeleteDistributionBucketFixture {
  1174. pub fn default() -> Self {
  1175. Self {
  1176. origin: RawOrigin::Signed(DEFAULT_MEMBER_ACCOUNT_ID),
  1177. family_id: Default::default(),
  1178. distribution_bucket_index: Default::default(),
  1179. }
  1180. }
  1181. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1182. Self {
  1183. distribution_bucket_index: bucket_index,
  1184. ..self
  1185. }
  1186. }
  1187. pub fn with_family_id(self, family_id: u64) -> Self {
  1188. Self { family_id, ..self }
  1189. }
  1190. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1191. Self { origin, ..self }
  1192. }
  1193. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1194. let actual_result = Storage::delete_distribution_bucket(
  1195. self.origin.clone().into(),
  1196. Storage::create_distribution_bucket_id(self.family_id, self.distribution_bucket_index),
  1197. );
  1198. assert_eq!(actual_result, expected_result);
  1199. }
  1200. }
  1201. pub struct UpdateDistributionBucketForBagsFixture {
  1202. origin: RawOrigin<u64>,
  1203. bag_id: BagId<Test>,
  1204. family_id: u64,
  1205. add_bucket_indices: BTreeSet<u64>,
  1206. remove_bucket_indices: BTreeSet<u64>,
  1207. }
  1208. impl UpdateDistributionBucketForBagsFixture {
  1209. pub fn default() -> Self {
  1210. Self {
  1211. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  1212. bag_id: Default::default(),
  1213. family_id: Default::default(),
  1214. add_bucket_indices: Default::default(),
  1215. remove_bucket_indices: Default::default(),
  1216. }
  1217. }
  1218. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1219. Self { origin, ..self }
  1220. }
  1221. pub fn with_add_bucket_indices(self, add_bucket_indices: BTreeSet<u64>) -> Self {
  1222. Self {
  1223. add_bucket_indices,
  1224. ..self
  1225. }
  1226. }
  1227. pub fn with_remove_bucket_indices(self, remove_bucket_indices: BTreeSet<u64>) -> Self {
  1228. Self {
  1229. remove_bucket_indices,
  1230. ..self
  1231. }
  1232. }
  1233. pub fn with_bag_id(self, bag_id: BagId<Test>) -> Self {
  1234. Self { bag_id, ..self }
  1235. }
  1236. pub fn with_family_id(self, family_id: u64) -> Self {
  1237. Self { family_id, ..self }
  1238. }
  1239. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1240. let actual_result = Storage::update_distribution_buckets_for_bag(
  1241. self.origin.clone().into(),
  1242. self.bag_id.clone(),
  1243. self.family_id,
  1244. self.add_bucket_indices.clone(),
  1245. self.remove_bucket_indices.clone(),
  1246. );
  1247. assert_eq!(actual_result, expected_result);
  1248. }
  1249. }
  1250. pub struct UpdateDistributionBucketsPerBagLimitFixture {
  1251. origin: RawOrigin<u64>,
  1252. new_limit: u64,
  1253. }
  1254. impl UpdateDistributionBucketsPerBagLimitFixture {
  1255. pub fn default() -> Self {
  1256. Self {
  1257. origin: RawOrigin::Signed(DISTRIBUTION_WG_LEADER_ACCOUNT_ID),
  1258. new_limit: 0,
  1259. }
  1260. }
  1261. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1262. Self { origin, ..self }
  1263. }
  1264. pub fn with_new_limit(self, new_limit: u64) -> Self {
  1265. Self { new_limit, ..self }
  1266. }
  1267. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1268. let old_limit = Storage::distribution_buckets_per_bag_limit();
  1269. let actual_result = Storage::update_distribution_buckets_per_bag_limit(
  1270. self.origin.clone().into(),
  1271. self.new_limit,
  1272. );
  1273. assert_eq!(actual_result, expected_result);
  1274. if actual_result.is_ok() {
  1275. assert_eq!(
  1276. Storage::distribution_buckets_per_bag_limit(),
  1277. self.new_limit
  1278. );
  1279. } else {
  1280. assert_eq!(old_limit, Storage::distribution_buckets_per_bag_limit());
  1281. }
  1282. }
  1283. }
  1284. pub struct UpdateDistributionBucketModeFixture {
  1285. origin: RawOrigin<u64>,
  1286. family_id: u64,
  1287. distribution_bucket_index: u64,
  1288. distributing: bool,
  1289. }
  1290. impl UpdateDistributionBucketModeFixture {
  1291. pub fn default() -> Self {
  1292. Self {
  1293. origin: RawOrigin::Signed(DEFAULT_MEMBER_ACCOUNT_ID),
  1294. family_id: Default::default(),
  1295. distribution_bucket_index: Default::default(),
  1296. distributing: true,
  1297. }
  1298. }
  1299. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1300. Self {
  1301. distribution_bucket_index: bucket_index,
  1302. ..self
  1303. }
  1304. }
  1305. pub fn with_family_id(self, family_id: u64) -> Self {
  1306. Self { family_id, ..self }
  1307. }
  1308. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1309. Self { origin, ..self }
  1310. }
  1311. pub fn with_distributing(self, distributing: bool) -> Self {
  1312. Self {
  1313. distributing,
  1314. ..self
  1315. }
  1316. }
  1317. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1318. let actual_result = Storage::update_distribution_bucket_mode(
  1319. self.origin.clone().into(),
  1320. Storage::create_distribution_bucket_id(self.family_id, self.distribution_bucket_index),
  1321. self.distributing,
  1322. );
  1323. assert_eq!(actual_result, expected_result);
  1324. }
  1325. }
  1326. pub struct UpdateFamiliesInDynamicBagCreationPolicyFixture {
  1327. origin: RawOrigin<u64>,
  1328. dynamic_bag_type: DynamicBagType,
  1329. families: BTreeMap<u64, u32>,
  1330. }
  1331. impl UpdateFamiliesInDynamicBagCreationPolicyFixture {
  1332. pub fn default() -> Self {
  1333. Self {
  1334. origin: RawOrigin::Signed(STORAGE_WG_LEADER_ACCOUNT_ID),
  1335. dynamic_bag_type: Default::default(),
  1336. families: Default::default(),
  1337. }
  1338. }
  1339. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1340. Self { origin, ..self }
  1341. }
  1342. pub fn with_families(self, families: BTreeMap<u64, u32>) -> Self {
  1343. Self { families, ..self }
  1344. }
  1345. pub fn with_dynamic_bag_type(self, dynamic_bag_type: DynamicBagType) -> Self {
  1346. Self {
  1347. dynamic_bag_type,
  1348. ..self
  1349. }
  1350. }
  1351. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1352. let old_policy = Storage::get_dynamic_bag_creation_policy(self.dynamic_bag_type);
  1353. let actual_result = Storage::update_families_in_dynamic_bag_creation_policy(
  1354. self.origin.clone().into(),
  1355. self.dynamic_bag_type,
  1356. self.families.clone(),
  1357. );
  1358. assert_eq!(actual_result, expected_result);
  1359. let new_policy = Storage::get_dynamic_bag_creation_policy(self.dynamic_bag_type);
  1360. assert_eq!(
  1361. old_policy.number_of_storage_buckets,
  1362. new_policy.number_of_storage_buckets
  1363. );
  1364. if actual_result.is_ok() {
  1365. assert_eq!(new_policy.families, self.families);
  1366. } else {
  1367. assert_eq!(old_policy, new_policy);
  1368. }
  1369. }
  1370. }
  1371. pub struct InviteDistributionBucketOperatorFixture {
  1372. origin: RawOrigin<u64>,
  1373. operator_worker_id: u64,
  1374. family_id: u64,
  1375. bucket_index: u64,
  1376. }
  1377. impl InviteDistributionBucketOperatorFixture {
  1378. pub fn default() -> Self {
  1379. Self {
  1380. origin: RawOrigin::Signed(DEFAULT_ACCOUNT_ID),
  1381. operator_worker_id: DEFAULT_WORKER_ID,
  1382. bucket_index: Default::default(),
  1383. family_id: Default::default(),
  1384. }
  1385. }
  1386. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1387. Self { origin, ..self }
  1388. }
  1389. pub fn with_operator_worker_id(self, operator_worker_id: u64) -> Self {
  1390. Self {
  1391. operator_worker_id,
  1392. ..self
  1393. }
  1394. }
  1395. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1396. Self {
  1397. bucket_index,
  1398. ..self
  1399. }
  1400. }
  1401. pub fn with_family_id(self, family_id: u64) -> Self {
  1402. Self { family_id, ..self }
  1403. }
  1404. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1405. let actual_result = Storage::invite_distribution_bucket_operator(
  1406. self.origin.clone().into(),
  1407. Storage::create_distribution_bucket_id(self.family_id, self.bucket_index),
  1408. self.operator_worker_id,
  1409. );
  1410. assert_eq!(actual_result, expected_result);
  1411. if actual_result.is_ok() {
  1412. let new_bucket: DistributionBucket<Test> =
  1413. Storage::distribution_bucket_by_family_id_by_index(
  1414. self.family_id,
  1415. self.bucket_index,
  1416. );
  1417. assert!(new_bucket
  1418. .pending_invitations
  1419. .contains(&self.operator_worker_id),);
  1420. }
  1421. }
  1422. }
  1423. pub struct CancelDistributionBucketInvitationFixture {
  1424. origin: RawOrigin<u64>,
  1425. bucket_index: u64,
  1426. family_id: u64,
  1427. operator_worker_id: u64,
  1428. }
  1429. impl CancelDistributionBucketInvitationFixture {
  1430. pub fn default() -> Self {
  1431. Self {
  1432. origin: RawOrigin::Signed(DEFAULT_DISTRIBUTION_PROVIDER_ACCOUNT_ID),
  1433. bucket_index: Default::default(),
  1434. family_id: Default::default(),
  1435. operator_worker_id: Default::default(),
  1436. }
  1437. }
  1438. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1439. Self { origin, ..self }
  1440. }
  1441. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1442. Self {
  1443. bucket_index,
  1444. ..self
  1445. }
  1446. }
  1447. pub fn with_family_id(self, family_id: u64) -> Self {
  1448. Self { family_id, ..self }
  1449. }
  1450. pub fn with_operator_worker_id(self, operator_worker_id: u64) -> Self {
  1451. Self {
  1452. operator_worker_id,
  1453. ..self
  1454. }
  1455. }
  1456. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1457. let actual_result = Storage::cancel_distribution_bucket_operator_invite(
  1458. self.origin.clone().into(),
  1459. Storage::create_distribution_bucket_id(self.family_id, self.bucket_index),
  1460. self.operator_worker_id,
  1461. );
  1462. assert_eq!(actual_result, expected_result);
  1463. if actual_result.is_ok() {
  1464. let new_bucket: DistributionBucket<Test> =
  1465. Storage::distribution_bucket_by_family_id_by_index(
  1466. self.family_id,
  1467. self.bucket_index,
  1468. );
  1469. assert!(!new_bucket
  1470. .pending_invitations
  1471. .contains(&self.operator_worker_id));
  1472. }
  1473. }
  1474. }
  1475. pub struct AcceptDistributionBucketInvitationFixture {
  1476. origin: RawOrigin<u64>,
  1477. bucket_index: u64,
  1478. family_id: u64,
  1479. worker_id: u64,
  1480. }
  1481. impl AcceptDistributionBucketInvitationFixture {
  1482. pub fn default() -> Self {
  1483. Self {
  1484. origin: RawOrigin::Signed(DEFAULT_DISTRIBUTION_PROVIDER_ACCOUNT_ID),
  1485. bucket_index: Default::default(),
  1486. family_id: Default::default(),
  1487. worker_id: Default::default(),
  1488. }
  1489. }
  1490. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1491. Self { origin, ..self }
  1492. }
  1493. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1494. Self {
  1495. bucket_index,
  1496. ..self
  1497. }
  1498. }
  1499. pub fn with_family_id(self, family_id: u64) -> Self {
  1500. Self { family_id, ..self }
  1501. }
  1502. pub fn with_worker_id(self, worker_id: u64) -> Self {
  1503. Self { worker_id, ..self }
  1504. }
  1505. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1506. let actual_result = Storage::accept_distribution_bucket_invitation(
  1507. self.origin.clone().into(),
  1508. self.worker_id,
  1509. Storage::create_distribution_bucket_id(self.family_id, self.bucket_index),
  1510. );
  1511. assert_eq!(actual_result, expected_result);
  1512. if actual_result.is_ok() {
  1513. let new_bucket: DistributionBucket<Test> =
  1514. Storage::distribution_bucket_by_family_id_by_index(
  1515. self.family_id,
  1516. self.bucket_index,
  1517. );
  1518. assert!(!new_bucket.pending_invitations.contains(&self.worker_id));
  1519. assert!(new_bucket.operators.contains(&self.worker_id));
  1520. }
  1521. }
  1522. }
  1523. pub struct SetDistributionBucketMetadataFixture {
  1524. origin: RawOrigin<u64>,
  1525. bucket_index: u64,
  1526. family_id: u64,
  1527. worker_id: u64,
  1528. metadata: Vec<u8>,
  1529. }
  1530. impl SetDistributionBucketMetadataFixture {
  1531. pub fn default() -> Self {
  1532. Self {
  1533. origin: RawOrigin::Signed(DEFAULT_DISTRIBUTION_PROVIDER_ACCOUNT_ID),
  1534. bucket_index: Default::default(),
  1535. family_id: Default::default(),
  1536. worker_id: Default::default(),
  1537. metadata: Default::default(),
  1538. }
  1539. }
  1540. pub fn with_metadata(self, metadata: Vec<u8>) -> Self {
  1541. Self { metadata, ..self }
  1542. }
  1543. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1544. Self { origin, ..self }
  1545. }
  1546. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1547. Self {
  1548. bucket_index,
  1549. ..self
  1550. }
  1551. }
  1552. pub fn with_family_id(self, family_id: u64) -> Self {
  1553. Self { family_id, ..self }
  1554. }
  1555. pub fn with_worker_id(self, worker_id: u64) -> Self {
  1556. Self { worker_id, ..self }
  1557. }
  1558. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1559. let actual_result = Storage::set_distribution_operator_metadata(
  1560. self.origin.clone().into(),
  1561. self.worker_id,
  1562. Storage::create_distribution_bucket_id(self.family_id, self.bucket_index),
  1563. self.metadata.clone(),
  1564. );
  1565. assert_eq!(actual_result, expected_result);
  1566. }
  1567. }
  1568. pub struct RemoveDistributionBucketOperatorFixture {
  1569. origin: RawOrigin<u64>,
  1570. bucket_index: u64,
  1571. family_id: u64,
  1572. operator_worker_id: u64,
  1573. }
  1574. impl RemoveDistributionBucketOperatorFixture {
  1575. pub fn default() -> Self {
  1576. Self {
  1577. origin: RawOrigin::Signed(DISTRIBUTION_WG_LEADER_ACCOUNT_ID),
  1578. bucket_index: Default::default(),
  1579. family_id: Default::default(),
  1580. operator_worker_id: Default::default(),
  1581. }
  1582. }
  1583. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1584. Self { origin, ..self }
  1585. }
  1586. pub fn with_bucket_index(self, bucket_index: u64) -> Self {
  1587. Self {
  1588. bucket_index,
  1589. ..self
  1590. }
  1591. }
  1592. pub fn with_family_id(self, family_id: u64) -> Self {
  1593. Self { family_id, ..self }
  1594. }
  1595. pub fn with_operator_worker_id(self, operator_worker_id: u64) -> Self {
  1596. Self {
  1597. operator_worker_id,
  1598. ..self
  1599. }
  1600. }
  1601. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1602. let actual_result = Storage::remove_distribution_bucket_operator(
  1603. self.origin.clone().into(),
  1604. Storage::create_distribution_bucket_id(self.family_id, self.bucket_index),
  1605. self.operator_worker_id,
  1606. );
  1607. assert_eq!(actual_result, expected_result);
  1608. if actual_result.is_ok() {
  1609. let new_bucket: DistributionBucket<Test> =
  1610. Storage::distribution_bucket_by_family_id_by_index(
  1611. self.family_id,
  1612. self.bucket_index,
  1613. );
  1614. assert!(!new_bucket.operators.contains(&self.operator_worker_id));
  1615. }
  1616. }
  1617. }
  1618. pub struct SetDistributionBucketFamilyMetadataFixture {
  1619. origin: RawOrigin<u64>,
  1620. family_id: u64,
  1621. metadata: Vec<u8>,
  1622. }
  1623. impl SetDistributionBucketFamilyMetadataFixture {
  1624. pub fn default() -> Self {
  1625. Self {
  1626. origin: RawOrigin::Signed(DEFAULT_DISTRIBUTION_PROVIDER_ACCOUNT_ID),
  1627. family_id: Default::default(),
  1628. metadata: Default::default(),
  1629. }
  1630. }
  1631. pub fn with_metadata(self, metadata: Vec<u8>) -> Self {
  1632. Self { metadata, ..self }
  1633. }
  1634. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  1635. Self { origin, ..self }
  1636. }
  1637. pub fn with_family_id(self, family_id: u64) -> Self {
  1638. Self { family_id, ..self }
  1639. }
  1640. pub fn call_and_assert(&self, expected_result: DispatchResult) {
  1641. let actual_result = Storage::set_distribution_bucket_family_metadata(
  1642. self.origin.clone().into(),
  1643. self.family_id,
  1644. self.metadata.clone(),
  1645. );
  1646. assert_eq!(actual_result, expected_result);
  1647. }
  1648. }