Browse Source

runtime: Upgrade ‘service-discovery’ pallet.

Shamil Gadelshin 4 năm trước cách đây
mục cha
commit
60d672d296

+ 22 - 0
Cargo.lock

@@ -1126,6 +1126,28 @@ dependencies = [
  "sp-runtime",
 ]
 
+[[package]]
+name = "pallet-service-discovery"
+version = "3.0.0"
+dependencies = [
+ "frame-support",
+ "frame-system",
+ "pallet-balances",
+ "pallet-common",
+ "pallet-hiring",
+ "pallet-membership",
+ "pallet-recurring-reward",
+ "pallet-stake",
+ "pallet-timestamp",
+ "pallet-token-mint",
+ "pallet-working-group",
+ "parity-scale-codec",
+ "serde",
+ "sp-core",
+ "sp-io",
+ "sp-runtime",
+]
+
 [[package]]
 name = "pallet-stake"
 version = "3.0.0"

+ 1 - 1
Cargo.toml

@@ -12,7 +12,7 @@ members = [
 	"runtime-modules/membership",
 	"runtime-modules/memo",
 	"runtime-modules/recurring-reward",
-#	"runtime-modules/service-discovery",
+	"runtime-modules/service-discovery",
 	"runtime-modules/stake",
 	"runtime-modules/storage",
 	"runtime-modules/token-minting",

+ 28 - 104
runtime-modules/service-discovery/Cargo.toml

@@ -1,112 +1,36 @@
 [package]
-name = 'substrate-service-discovery-module'
-version = '2.0.0'
+name = 'pallet-service-discovery'
+version = '3.0.0'
 authors = ['Joystream contributors']
 edition = '2018'
 
+[dependencies]
+serde = { version = "1.0.101", optional = true, features = ["derive"] }
+codec = { package = 'parity-scale-codec', version = '1.3.1', default-features = false, features = ['derive'] }
+frame-support = { package = 'frame-support', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+system = { package = 'frame-system', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+sp-runtime = { package = 'sp-runtime', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+working-group = { package = 'pallet-working-group', default-features = false, path = '../working-group'}
+
+[dev-dependencies]
+sp-io = { package = 'sp-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+sp-core = { package = 'sp-core', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+timestamp = { package = 'pallet-timestamp', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+balances = { package = 'pallet-balances', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '00768a1f21a579c478fe5d4f51e1fa71f7db9fd4'}
+membership = { package = 'pallet-membership', default-features = false, path = '../membership'}
+stake = { package = 'pallet-stake', default-features = false, path = '../stake'}
+hiring = { package = 'pallet-hiring', default-features = false, path = '../hiring'}
+minting = { package = 'pallet-token-mint', default-features = false, path = '../token-minting'}
+recurringrewards = { package = 'pallet-recurring-reward', default-features = false, path = '../recurring-reward'}
+common = { package = 'pallet-common', default-features = false, path = '../common'}
+
 [features]
 default = ['std']
 std = [
-	'sr-primitives/std',
-	'srml-support/std',
-	'system/std',
-	'rstd/std',
 	'serde',
-    'codec/std',
-    'primitives/std',
-    'working-group/std',
-]
-
-[dependencies.sr-primitives]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'sr-primitives'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dependencies.srml-support]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'srml-support'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dependencies.common]
-default_features = false
-package = 'substrate-common-module'
-path = '../common'
-
-[dependencies.system]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'srml-system'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dependencies.rstd]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'sr-std'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dependencies.serde]
-features = ['derive']
-optional = true
-version = '1.0.101'
-
-[dependencies.primitives]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'substrate-primitives'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dependencies.codec]
-default-features = false
-features = ['derive']
-package = 'parity-scale-codec'
-version = '1.0.0'
-
-[dev-dependencies.runtime-io]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'sr-io'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dependencies.working-group]
-default_features = false
-package = 'substrate-working-group-module'
-path = '../working-group'
-
-[dev-dependencies.balances]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'srml-balances'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
-
-[dev-dependencies.recurringrewards]
-default_features = false
-package = 'substrate-recurring-reward-module'
-path = '../recurring-reward'
-
-[dev-dependencies.hiring]
-default_features = false
-package = 'substrate-hiring-module'
-path = '../hiring'
-
-[dev-dependencies.stake]
-default_features = false
-package = 'substrate-stake-module'
-path = '../stake'
-
-[dev-dependencies.minting]
-default_features = false
-package = 'substrate-token-mint-module'
-path = '../token-minting'
-
-[dev-dependencies.membership]
-default_features = false
-package = 'substrate-membership-module'
-path = '../membership'
-
-[dev-dependencies.timestamp]
-default_features = false
-git = 'https://github.com/paritytech/substrate.git'
-package = 'srml-timestamp'
-rev = 'c37bb08535c49a12320af7facfd555ce05cce2e8'
+	'codec/std',
+	'frame-support/std',
+	'system/std',
+	'sp-runtime/std',
+	'working-group/std',
+]

+ 12 - 9
runtime-modules/service-discovery/src/lib.rs

@@ -23,12 +23,11 @@ mod mock;
 mod tests;
 
 use codec::{Decode, Encode};
-use rstd::prelude::*;
 #[cfg(feature = "std")]
 use serde::{Deserialize, Serialize};
 
-use srml_support::{decl_event, decl_module, decl_storage, ensure};
-use system::{self, ensure_root};
+use frame_support::{decl_event, decl_module, decl_storage, ensure};
+use system::ensure_root;
 /*
   Although there is support for ed25519 keys as the IPNS identity key and we could potentially
   reuse the same key for the role account and ipns (and make this discovery module obselete)
@@ -79,14 +78,14 @@ pub trait Trait: system::Trait + working_group::Trait<StorageWorkingGroupInstanc
 decl_storage! {
     trait Store for Module<T: Trait> as Discovery {
         /// Bootstrap endpoints maintained by root
-        pub BootstrapEndpoints get(bootstrap_endpoints): Vec<Url>;
+        pub BootstrapEndpoints get(fn bootstrap_endpoints): Vec<Url>;
 
         /// Mapping of service providers' storage provider id to their AccountInfo
-        pub AccountInfoByStorageProviderId get(account_info_by_storage_provider_id):
-            map StorageProviderId<T> => AccountInfo<T::BlockNumber>;
+        pub AccountInfoByStorageProviderId get(fn account_info_by_storage_provider_id):
+            map hasher(blake2_128_concat) StorageProviderId<T> => AccountInfo<T::BlockNumber>;
 
         /// Lifetime of an AccountInfo record in AccountInfoByAccountId map
-        pub DefaultLifetime get(default_lifetime) config():
+        pub DefaultLifetime get(fn default_lifetime) config():
             T::BlockNumber = T::BlockNumber::from(DEFAULT_LIFETIME);
     }
 }
@@ -117,6 +116,7 @@ decl_module! {
 
         /// Creates the AccountInfo to save an IPNS identity for the storage provider.
         /// Requires signed storage provider credentials.
+        #[weight = 10_000_000] // TODO: adjust weight
         pub fn set_ipns_id(
             origin,
             storage_provider_id: StorageProviderId<T>,
@@ -140,12 +140,13 @@ decl_module! {
 
         /// Deletes the AccountInfo with the IPNS identity for the storage provider.
         /// Requires signed storage provider credentials.
+        #[weight = 10_000_000] // TODO: adjust weight
         pub fn unset_ipns_id(origin, storage_provider_id: StorageProviderId<T>) {
             <StorageWorkingGroup<T>>::ensure_worker_signed(origin, &storage_provider_id)?;
 
             // == MUTATION SAFE ==
 
-            if <AccountInfoByStorageProviderId<T>>::exists(storage_provider_id) {
+            if <AccountInfoByStorageProviderId<T>>::contains_key(storage_provider_id) {
                 <AccountInfoByStorageProviderId<T>>::remove(storage_provider_id);
                 Self::deposit_event(RawEvent::AccountInfoRemoved(storage_provider_id));
             }
@@ -154,6 +155,7 @@ decl_module! {
         // Privileged methods
 
         /// Sets default lifetime for storage providers accounts info. Requires root privileges.
+        #[weight = 10_000_000] // TODO: adjust weight
         pub fn set_default_lifetime(origin, lifetime: T::BlockNumber) {
             ensure_root(origin)?;
             ensure!(lifetime >= T::BlockNumber::from(MINIMUM_LIFETIME),
@@ -165,6 +167,7 @@ decl_module! {
         }
 
         /// Sets bootstrap endpoints for the Colossus. Requires root privileges.
+        #[weight = 10_000_000] // TODO: adjust weight
         pub fn set_bootstrap_endpoints(origin, endpoints: Vec<Url>) {
             ensure_root(origin)?;
 
@@ -178,7 +181,7 @@ decl_module! {
 impl<T: Trait> Module<T> {
     /// Verifies that account info for the storage provider is still valid.
     pub fn is_account_info_expired(storage_provider_id: &StorageProviderId<T>) -> bool {
-        !<AccountInfoByStorageProviderId<T>>::exists(storage_provider_id)
+        !<AccountInfoByStorageProviderId<T>>::contains_key(storage_provider_id)
             || <system::Module<T>>::block_number()
                 > <AccountInfoByStorageProviderId<T>>::get(storage_provider_id).expires_at
     }

+ 20 - 17
runtime-modules/service-discovery/src/mock.rs

@@ -2,15 +2,14 @@
 
 pub use crate::*;
 
-pub use primitives::{Blake2Hasher, H256};
-pub use sr_primitives::{
-    testing::{Digest, DigestItem, Header, UintAuthorityId},
-    traits::{BlakeTwo256, IdentityLookup, OnFinalize},
-    BuildStorage, Perbill,
+use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
+use sp_core::H256;
+use sp_runtime::{
+    testing::Header,
+    traits::{BlakeTwo256, IdentityLookup},
+    Perbill,
 };
 
-use srml_support::{impl_outer_event, impl_outer_origin, parameter_types};
-
 // The storage working group instance alias.
 pub type StorageWorkingGroupInstance = working_group::Instance2;
 
@@ -37,7 +36,8 @@ impl_outer_event! {
         discovery<T>,
         balances<T>,
         membership_mod<T>,
-         working_group_mod StorageWorkingGroupInstance <T>,
+        working_group_mod StorageWorkingGroupInstance <T>,
+        system<T>,
     }
 }
 
@@ -52,15 +52,14 @@ parameter_types! {
     pub const MinimumPeriod: u64 = 5;
     pub const StakePoolId: [u8; 8] = *b"joystake";
     pub const ExistentialDeposit: u32 = 0;
-    pub const TransferFee: u32 = 0;
-    pub const CreationFee: u32 = 0;
 }
 
 impl system::Trait for Test {
+    type BaseCallFilter = ();
     type Origin = Origin;
+    type Call = ();
     type Index = u64;
     type BlockNumber = u64;
-    type Call = ();
     type Hash = H256;
     type Hashing = BlakeTwo256;
     type AccountId = u64;
@@ -69,9 +68,17 @@ impl system::Trait for Test {
     type Event = MetaEvent;
     type BlockHashCount = BlockHashCount;
     type MaximumBlockWeight = MaximumBlockWeight;
+    type DbWeight = ();
+    type BlockExecutionWeight = ();
+    type ExtrinsicBaseWeight = ();
+    type MaximumExtrinsicWeight = ();
     type MaximumBlockLength = MaximumBlockLength;
     type AvailableBlockRatio = AvailableBlockRatio;
     type Version = ();
+    type ModuleToIndex = ();
+    type AccountData = balances::AccountData<u64>;
+    type OnNewAccount = ();
+    type OnKilledAccount = ();
 }
 
 impl Trait for Test {
@@ -112,14 +119,10 @@ impl common::currency::GovernanceCurrency for Test {
 
 impl balances::Trait for Test {
     type Balance = u64;
-    type OnFreeBalanceZero = ();
-    type OnNewAccount = ();
-    type TransferPayment = ();
     type DustRemoval = ();
     type Event = MetaEvent;
     type ExistentialDeposit = ExistentialDeposit;
-    type TransferFee = TransferFee;
-    type CreationFee = CreationFee;
+    type AccountStore = System;
 }
 
 impl recurringrewards::Trait for Test {
@@ -143,7 +146,7 @@ impl timestamp::Trait for Test {
     type MinimumPeriod = MinimumPeriod;
 }
 
-pub fn initial_test_ext() -> runtime_io::TestExternalities {
+pub fn initial_test_ext() -> sp_io::TestExternalities {
     let t = system::GenesisConfig::default()
         .build_storage::<Test>()
         .unwrap();

+ 15 - 12
runtime-modules/service-discovery/src/tests.rs

@@ -2,8 +2,7 @@
 
 use super::mock::*;
 
-use srml_support::*;
-use system::{self, EventRecord, Phase};
+use system::{EventRecord, Phase, RawOrigin};
 
 #[test]
 fn set_ipns_id() {
@@ -22,7 +21,7 @@ fn set_ipns_id() {
         )
         .is_ok());
 
-        assert!(<AccountInfoByStorageProviderId<Test>>::exists(
+        assert!(<AccountInfoByStorageProviderId<Test>>::contains_key(
             &storage_provider_id
         ));
         let account_info = Discovery::account_info_by_storage_provider_id(&storage_provider_id);
@@ -37,7 +36,7 @@ fn set_ipns_id() {
         assert_eq!(
             *System::events().last().unwrap(),
             EventRecord {
-                phase: Phase::ApplyExtrinsic(0),
+                phase: Phase::Initialization,
                 event: MetaEvent::discovery(RawEvent::AccountInfoUpdated(
                     storage_provider_id,
                     identity.clone()
@@ -55,7 +54,7 @@ fn set_ipns_id() {
             identity.clone(),
         )
         .is_err());
-        assert!(!<AccountInfoByStorageProviderId<Test>>::exists(
+        assert!(!<AccountInfoByStorageProviderId<Test>>::contains_key(
             &invalid_storage_provider_id
         ));
     });
@@ -64,6 +63,9 @@ fn set_ipns_id() {
 #[test]
 fn unset_ipns_id() {
     initial_test_ext().execute_with(|| {
+        let current_block_number = 1000;
+        System::set_block_number(current_block_number);
+
         let (storage_provider_account_id, storage_provider_id) = hire_storage_provider();
 
         <AccountInfoByStorageProviderId<Test>>::insert(
@@ -74,7 +76,7 @@ fn unset_ipns_id() {
             },
         );
 
-        assert!(<AccountInfoByStorageProviderId<Test>>::exists(
+        assert!(<AccountInfoByStorageProviderId<Test>>::contains_key(
             &storage_provider_account_id
         ));
 
@@ -83,14 +85,14 @@ fn unset_ipns_id() {
             storage_provider_id
         )
         .is_ok());
-        assert!(!<AccountInfoByStorageProviderId<Test>>::exists(
+        assert!(!<AccountInfoByStorageProviderId<Test>>::contains_key(
             &storage_provider_account_id
         ));
 
         assert_eq!(
             *System::events().last().unwrap(),
             EventRecord {
-                phase: Phase::ApplyExtrinsic(0),
+                phase: Phase::Initialization,
                 event: MetaEvent::discovery(RawEvent::AccountInfoRemoved(storage_provider_id)),
                 topics: vec![]
             }
@@ -104,7 +106,7 @@ fn unset_ipns_id() {
             invalid_storage_provider_account_id,
         )
         .is_err());
-        assert!(!<AccountInfoByStorageProviderId<Test>>::exists(
+        assert!(!<AccountInfoByStorageProviderId<Test>>::contains_key(
             &invalid_storage_provider_id
         ));
     });
@@ -142,7 +144,7 @@ fn set_default_lifetime() {
             ""
         );
         assert!(
-            Discovery::set_default_lifetime(Origin::ROOT, lifetime).is_ok(),
+            Discovery::set_default_lifetime(RawOrigin::Root.into(), lifetime).is_ok(),
             ""
         );
         assert_eq!(Discovery::default_lifetime(), lifetime, "");
@@ -151,7 +153,8 @@ fn set_default_lifetime() {
         let less_than_min_lifetime =
             <Test as system::Trait>::BlockNumber::from(MINIMUM_LIFETIME - 1);
         assert!(
-            Discovery::set_default_lifetime(Origin::ROOT, less_than_min_lifetime).is_err(),
+            Discovery::set_default_lifetime(RawOrigin::Root.into(), less_than_min_lifetime)
+                .is_err(),
             ""
         );
     });
@@ -167,7 +170,7 @@ fn set_bootstrap_endpoints() {
             ""
         );
         assert!(
-            Discovery::set_bootstrap_endpoints(Origin::ROOT, endpoints.clone()).is_ok(),
+            Discovery::set_bootstrap_endpoints(RawOrigin::Root.into(), endpoints.clone()).is_ok(),
             ""
         );
         assert_eq!(Discovery::bootstrap_endpoints(), endpoints, "");