mock.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #![cfg(test)]
  2. use frame_support::storage::StorageMap;
  3. use frame_support::traits::{OnFinalize, OnInitialize};
  4. use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
  5. use sp_core::H256;
  6. use sp_runtime::{
  7. testing::Header,
  8. traits::{BlakeTwo256, IdentityLookup},
  9. Perbill,
  10. };
  11. use crate::data_directory::ContentIdExists;
  12. use crate::data_directory::Quota;
  13. pub use crate::data_directory::{ContentParameters, StorageObjectOwner};
  14. use crate::data_object_type_registry::IsActiveDataObjectType;
  15. use crate::ContentId;
  16. pub use crate::StorageWorkingGroupInstance;
  17. pub use crate::{data_directory, data_object_storage_registry, data_object_type_registry};
  18. use common::currency::GovernanceCurrency;
  19. use membership;
  20. mod working_group_mod {
  21. pub use super::StorageWorkingGroupInstance;
  22. pub use working_group::Event;
  23. }
  24. mod members {
  25. pub use membership::Event;
  26. }
  27. impl_outer_origin! {
  28. pub enum Origin for Test {}
  29. }
  30. impl_outer_event! {
  31. pub enum MetaEvent for Test {
  32. data_object_type_registry<T>,
  33. data_directory<T>,
  34. data_object_storage_registry<T>,
  35. balances<T>,
  36. members<T>,
  37. working_group_mod StorageWorkingGroupInstance <T>,
  38. system<T>,
  39. }
  40. }
  41. pub const TEST_FIRST_DATA_OBJECT_TYPE_ID: u64 = 1000;
  42. pub const TEST_FIRST_CONTENT_ID: u64 = 2000;
  43. pub const TEST_FIRST_RELATIONSHIP_ID: u64 = 3000;
  44. pub const TEST_FIRST_METADATA_ID: u64 = 4000;
  45. pub const TEST_MOCK_LIAISON_STORAGE_PROVIDER_ID: u32 = 1;
  46. pub const TEST_MOCK_EXISTING_CID: u64 = 42;
  47. pub struct AnyDataObjectTypeIsActive {}
  48. impl<T: data_object_type_registry::Trait> IsActiveDataObjectType<T> for AnyDataObjectTypeIsActive {
  49. fn is_active_data_object_type(_which: &T::DataObjectTypeId) -> bool {
  50. true
  51. }
  52. }
  53. pub struct MockContent {}
  54. impl ContentIdExists<Test> for MockContent {
  55. fn has_content(which: &ContentId<Test>) -> bool {
  56. *which == TEST_MOCK_EXISTING_CID
  57. }
  58. fn get_data_object(
  59. which: &ContentId<Test>,
  60. ) -> Result<data_directory::DataObject<Test>, &'static str> {
  61. match *which {
  62. TEST_MOCK_EXISTING_CID => Ok(data_directory::DataObjectInternal {
  63. type_id: 1,
  64. size: 1234,
  65. added_at: data_directory::BlockAndTime {
  66. block: 10,
  67. time: 1024,
  68. },
  69. owner: StorageObjectOwner::Member(1),
  70. liaison: TEST_MOCK_LIAISON_STORAGE_PROVIDER_ID,
  71. liaison_judgement: data_directory::LiaisonJudgement::Pending,
  72. ipfs_content_id: vec![],
  73. }),
  74. _ => Err("nope, missing"),
  75. }
  76. }
  77. }
  78. // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
  79. #[derive(Clone, PartialEq, Eq, Debug)]
  80. pub struct Test;
  81. parameter_types! {
  82. pub const BlockHashCount: u64 = 250;
  83. pub const MaximumBlockWeight: u32 = 1024;
  84. pub const MaximumBlockLength: u32 = 2 * 1024;
  85. pub const AvailableBlockRatio: Perbill = Perbill::one();
  86. pub const MinimumPeriod: u64 = 5;
  87. pub const DefaultQuota: Quota = Quota::new(5000, 50);
  88. }
  89. impl system::Trait for Test {
  90. type BaseCallFilter = ();
  91. type Origin = Origin;
  92. type Call = ();
  93. type Index = u64;
  94. type BlockNumber = u64;
  95. type Hash = H256;
  96. type Hashing = BlakeTwo256;
  97. type AccountId = u64;
  98. type Lookup = IdentityLookup<Self::AccountId>;
  99. type Header = Header;
  100. type Event = MetaEvent;
  101. type BlockHashCount = BlockHashCount;
  102. type MaximumBlockWeight = MaximumBlockWeight;
  103. type DbWeight = ();
  104. type BlockExecutionWeight = ();
  105. type ExtrinsicBaseWeight = ();
  106. type MaximumExtrinsicWeight = ();
  107. type MaximumBlockLength = MaximumBlockLength;
  108. type AvailableBlockRatio = AvailableBlockRatio;
  109. type Version = ();
  110. type ModuleToIndex = ();
  111. type AccountData = balances::AccountData<u64>;
  112. type OnNewAccount = ();
  113. type OnKilledAccount = ();
  114. }
  115. impl pallet_timestamp::Trait for Test {
  116. type Moment = u64;
  117. type OnTimestampSet = ();
  118. type MinimumPeriod = MinimumPeriod;
  119. }
  120. impl common::MembershipTypes for Test {
  121. type MemberId = u64;
  122. type ActorId = u64;
  123. }
  124. impl common::StorageOwnership for Test {
  125. type ChannelId = u64;
  126. type DAOId = u64;
  127. type ContentId = u64;
  128. type DataObjectTypeId = u64;
  129. }
  130. parameter_types! {
  131. pub const ExistentialDeposit: u32 = 0;
  132. pub const StakePoolId: [u8; 8] = *b"joystake";
  133. }
  134. impl balances::Trait for Test {
  135. type Balance = u64;
  136. type DustRemoval = ();
  137. type Event = MetaEvent;
  138. type ExistentialDeposit = ExistentialDeposit;
  139. type AccountStore = System;
  140. }
  141. impl GovernanceCurrency for Test {
  142. type Currency = balances::Module<Self>;
  143. }
  144. parameter_types! {
  145. pub const MaxWorkerNumberLimit: u32 = 3;
  146. }
  147. impl working_group::Trait<StorageWorkingGroupInstance> for Test {
  148. type Event = MetaEvent;
  149. type MaxWorkerNumberLimit = MaxWorkerNumberLimit;
  150. }
  151. impl data_object_type_registry::Trait for Test {
  152. type Event = MetaEvent;
  153. }
  154. impl data_directory::Trait for Test {
  155. type Event = MetaEvent;
  156. type StorageProviderHelper = ();
  157. type IsActiveDataObjectType = AnyDataObjectTypeIsActive;
  158. type MemberOriginValidator = ();
  159. type DefaultQuota = DefaultQuota;
  160. }
  161. impl crate::data_directory::StorageProviderHelper<Test> for () {
  162. fn get_random_storage_provider() -> Result<u32, &'static str> {
  163. Ok(1)
  164. }
  165. }
  166. impl common::origin::ActorOriginValidator<Origin, u64, u64> for () {
  167. fn ensure_actor_origin(origin: Origin, _account_id: u64) -> Result<u64, &'static str> {
  168. let signed_account_id = system::ensure_signed(origin)?;
  169. Ok(signed_account_id)
  170. }
  171. }
  172. impl data_object_storage_registry::Trait for Test {
  173. type Event = MetaEvent;
  174. type DataObjectStorageRelationshipId = u64;
  175. type ContentIdExists = MockContent;
  176. }
  177. impl membership::Trait for Test {
  178. type Event = MetaEvent;
  179. type MemberId = u64;
  180. type SubscriptionId = u32;
  181. type PaidTermId = u32;
  182. type ActorId = u32;
  183. }
  184. impl stake::Trait for Test {
  185. type Currency = Balances;
  186. type StakePoolId = StakePoolId;
  187. type StakingEventsHandler = ();
  188. type StakeId = u64;
  189. type SlashId = u64;
  190. }
  191. impl minting::Trait for Test {
  192. type Currency = Balances;
  193. type MintId = u64;
  194. }
  195. impl recurringrewards::Trait for Test {
  196. type PayoutStatusHandler = ();
  197. type RecipientId = u64;
  198. type RewardRelationshipId = u64;
  199. }
  200. impl hiring::Trait for Test {
  201. type OpeningId = u64;
  202. type ApplicationId = u64;
  203. type ApplicationDeactivatedHandler = ();
  204. type StakeHandlerProvider = hiring::Module<Self>;
  205. }
  206. pub struct ExtBuilder {
  207. quota_objects_limit_upper_bound: u64,
  208. quota_size_limit_upper_bound: u64,
  209. global_quota: Quota,
  210. first_data_object_type_id: u64,
  211. first_content_id: u64,
  212. first_relationship_id: u64,
  213. first_metadata_id: u64,
  214. }
  215. impl Default for ExtBuilder {
  216. fn default() -> Self {
  217. Self {
  218. quota_objects_limit_upper_bound: 200,
  219. quota_size_limit_upper_bound: 20000,
  220. global_quota: Quota::new(2000000, 2000),
  221. first_data_object_type_id: 1,
  222. first_content_id: 2,
  223. first_relationship_id: 3,
  224. first_metadata_id: 4,
  225. }
  226. }
  227. }
  228. impl ExtBuilder {
  229. pub fn first_data_object_type_id(mut self, first_data_object_type_id: u64) -> Self {
  230. self.first_data_object_type_id = first_data_object_type_id;
  231. self
  232. }
  233. pub fn first_content_id(mut self, first_content_id: u64) -> Self {
  234. self.first_content_id = first_content_id;
  235. self
  236. }
  237. pub fn first_relationship_id(mut self, first_relationship_id: u64) -> Self {
  238. self.first_relationship_id = first_relationship_id;
  239. self
  240. }
  241. pub fn first_metadata_id(mut self, first_metadata_id: u64) -> Self {
  242. self.first_metadata_id = first_metadata_id;
  243. self
  244. }
  245. pub fn build(self) -> sp_io::TestExternalities {
  246. let mut t = system::GenesisConfig::default()
  247. .build_storage::<Test>()
  248. .unwrap();
  249. data_directory::GenesisConfig::<Test> {
  250. quota_size_limit_upper_bound: self.quota_size_limit_upper_bound,
  251. quota_objects_limit_upper_bound: self.quota_objects_limit_upper_bound,
  252. global_quota: self.global_quota,
  253. data_object_by_content_id: vec![],
  254. quotas: vec![],
  255. uploading_blocked: false,
  256. }
  257. .assimilate_storage(&mut t)
  258. .unwrap();
  259. data_object_type_registry::GenesisConfig::<Test> {
  260. first_data_object_type_id: self.first_data_object_type_id,
  261. }
  262. .assimilate_storage(&mut t)
  263. .unwrap();
  264. data_object_storage_registry::GenesisConfig::<Test> {
  265. first_relationship_id: self.first_relationship_id,
  266. }
  267. .assimilate_storage(&mut t)
  268. .unwrap();
  269. membership::GenesisConfig::<Test> {
  270. default_paid_membership_fee: 0,
  271. members: vec![membership::genesis::Member {
  272. member_id: 0,
  273. root_account: 1,
  274. controller_account: 1,
  275. handle: "alice".into(),
  276. avatar_uri: "".into(),
  277. about: "".into(),
  278. registered_at_time: 0,
  279. }],
  280. }
  281. .assimilate_storage(&mut t)
  282. .unwrap();
  283. t.into()
  284. }
  285. }
  286. pub type TestDataObjectType = data_object_type_registry::DataObjectType;
  287. pub type Balances = balances::Module<Test>;
  288. pub type System = system::Module<Test>;
  289. pub type TestDataObjectTypeRegistry = data_object_type_registry::Module<Test>;
  290. pub type TestDataDirectory = data_directory::Module<Test>;
  291. pub type TestDataObjectStorageRegistry = data_object_storage_registry::Module<Test>;
  292. pub fn with_default_mock_builder<R, F: FnOnce() -> R>(f: F) -> R {
  293. ExtBuilder::default()
  294. .first_data_object_type_id(TEST_FIRST_DATA_OBJECT_TYPE_ID)
  295. .first_content_id(TEST_FIRST_CONTENT_ID)
  296. .first_relationship_id(TEST_FIRST_RELATIONSHIP_ID)
  297. .first_metadata_id(TEST_FIRST_METADATA_ID)
  298. .build()
  299. .execute_with(|| f())
  300. }
  301. pub(crate) fn hire_storage_provider() -> (u64, u32) {
  302. let storage_provider_id = 1;
  303. let role_account_id = 1;
  304. let storage_provider = working_group::Worker {
  305. member_id: 1,
  306. role_account_id,
  307. reward_relationship: None,
  308. role_stake_profile: None,
  309. };
  310. <working_group::WorkerById<Test, StorageWorkingGroupInstance>>::insert(
  311. storage_provider_id,
  312. storage_provider,
  313. );
  314. (role_account_id, storage_provider_id)
  315. }
  316. // Recommendation from Parity on testing on_finalize
  317. // https://substrate.dev/docs/en/next/development/module/tests
  318. pub fn run_to_block(n: u64) {
  319. while System::block_number() < n {
  320. <System as OnFinalize<u64>>::on_finalize(System::block_number());
  321. <TestDataObjectTypeRegistry as OnFinalize<u64>>::on_finalize(System::block_number());
  322. <TestDataDirectory as OnFinalize<u64>>::on_finalize(System::block_number());
  323. <TestDataObjectStorageRegistry as OnFinalize<u64>>::on_finalize(System::block_number());
  324. System::set_block_number(System::block_number() + 1);
  325. <System as OnInitialize<u64>>::on_initialize(System::block_number());
  326. <TestDataObjectTypeRegistry as OnInitialize<u64>>::on_initialize(System::block_number());
  327. <TestDataDirectory as OnInitialize<u64>>::on_initialize(System::block_number());
  328. <TestDataObjectStorageRegistry as OnInitialize<u64>>::on_initialize(System::block_number());
  329. }
  330. }