Browse Source

substrate v2: fixed mocks

Mokhtar Naamani 5 years ago
parent
commit
3ff157c719

+ 50 - 25
src/governance/mock.rs

@@ -8,11 +8,12 @@ pub use system;
 pub use primitives::{Blake2Hasher, H256};
 pub use runtime_primitives::{
     testing::{Digest, DigestItem, Header, UintAuthorityId},
-    traits::{BlakeTwo256, IdentityLookup, OnFinalize},
-    BuildStorage,
+    traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize},
+    weights::Weight,
+    BuildStorage, Perbill,
 };
 
-use srml_support::impl_outer_origin;
+use srml_support::{impl_outer_origin, parameter_types};
 
 impl_outer_origin! {
     pub enum Origin for Test {}
@@ -35,32 +36,40 @@ impl<T: system::Trait> Members<T> for MockMembership {
     }
 }
 
-// For testing the module, we construct most of a mock runtime. This means
-// first constructing a configuration type (`Test`) which `impl`s each of the
-// configuration traits of modules we want to use.
-#[derive(Clone, Eq, PartialEq)]
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
 pub struct Test;
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+    pub const MinimumPeriod: u64 = 5;
+}
+
 impl system::Trait for Test {
     type Origin = Origin;
     type Index = u64;
     type BlockNumber = u64;
+    type Call = ();
     type Hash = H256;
     type Hashing = BlakeTwo256;
-    type Digest = Digest;
     type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
     type Header = Header;
+    type WeightMultiplierUpdate = ();
     type Event = ();
-    type Log = DigestItem;
-    type Lookup = IdentityLookup<u64>;
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
 }
+
 impl timestamp::Trait for Test {
     type Moment = u64;
     type OnTimestampSet = ();
-}
-impl consensus::Trait for Test {
-    type SessionKey = UintAuthorityId;
-    type InherentOfflineReport = ();
-    type Log = DigestItem;
+    type MinimumPeriod = MinimumPeriod;
 }
 impl council::Trait for Test {
     type Event = ();
@@ -75,24 +84,40 @@ impl election::Trait for Test {
     type Members = MockMembership;
 }
 
-impl balances::Trait for Test {
-    type Event = ();
+pub struct WeightToFee(u32);
+impl Convert<Weight, u64> for WeightToFee {
+    fn convert(t: Weight) -> u64 {
+        t as u64
+    }
+}
 
-    /// The balance of an account.
-    type Balance = u32;
+parameter_types! {
+    pub const ExistentialDeposit: u32 = 0;
+    pub const TransferFee: u32 = 0;
+    pub const CreationFee: u32 = 0;
+    pub const TransactionBaseFee: u32 = 1;
+    pub const TransactionByteFee: u32 = 0;
+}
 
-    /// A function which is invoked when the free-balance has fallen below the existential deposit and
-    /// has been reduced to zero.
-    ///
-    /// Gives a chance to clean up resources associated with the given account.
+impl balances::Trait for Test {
+    /// The type for recording an account's balance.
+    type Balance = u64;
+    /// What to do if an account's free balance gets zeroed.
     type OnFreeBalanceZero = ();
-
-    /// Handler for when a new account is created.
+    /// What to do if a new account is created.
     type OnNewAccount = ();
+    /// The ubiquitous event type.
+    type Event = ();
 
     type TransactionPayment = ();
     type DustRemoval = ();
     type TransferPayment = ();
+    type ExistentialDeposit = ExistentialDeposit;
+    type TransferFee = TransferFee;
+    type CreationFee = CreationFee;
+    type TransactionBaseFee = TransactionBaseFee;
+    type TransactionByteFee = TransactionByteFee;
+    type WeightToFee = WeightToFee;
 }
 
 impl GovernanceCurrency for Test {

+ 54 - 15
src/governance/proposals.rs

@@ -560,9 +560,10 @@ mod tests {
     // The testing primitives are very useful for avoiding having to work with signatures
     // or public keys. `u64` is used as the `AccountId` and no `Signature`s are requried.
     use runtime_primitives::{
-        testing::{Digest, DigestItem, Header, UintAuthorityId},
-        traits::{BlakeTwo256, IdentityLookup},
-        BuildStorage,
+        testing::Header,
+        traits::{BlakeTwo256, Convert, IdentityLookup},
+        weights::Weight,
+        BuildStorage, Perbill,
     };
     use srml_support::*;
 
@@ -570,39 +571,77 @@ mod tests {
         pub enum Origin for Test {}
     }
 
-    // For testing the module, we construct most of a mock runtime. This means
-    // first constructing a configuration type (`Test`) which `impl`s each of the
-    // configuration traits of modules we want to use.
-    #[derive(Clone, Eq, PartialEq)]
+    // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+    #[derive(Clone, PartialEq, Eq, Debug)]
     pub struct Test;
 
+    parameter_types! {
+        pub const BlockHashCount: u64 = 250;
+        pub const MaximumBlockWeight: u32 = 1024;
+        pub const MaximumBlockLength: u32 = 2 * 1024;
+        pub const AvailableBlockRatio: Perbill = Perbill::one();
+        pub const MinimumPeriod: u64 = 5;
+    }
+
     impl system::Trait for Test {
         type Origin = Origin;
         type Index = u64;
         type BlockNumber = u64;
+        type Call = ();
         type Hash = H256;
         type Hashing = BlakeTwo256;
-        type Digest = Digest;
         type AccountId = u64;
-        type Lookup = IdentityLookup<u64>;
+        type Lookup = IdentityLookup<Self::AccountId>;
         type Header = Header;
+        type WeightMultiplierUpdate = ();
         type Event = ();
-        type Log = DigestItem;
+        type BlockHashCount = BlockHashCount;
+        type MaximumBlockWeight = MaximumBlockWeight;
+        type MaximumBlockLength = MaximumBlockLength;
+        type AvailableBlockRatio = AvailableBlockRatio;
+        type Version = ();
+    }
+
+    impl timestamp::Trait for Test {
+        type Moment = u64;
+        type OnTimestampSet = ();
+        type MinimumPeriod = MinimumPeriod;
+    }
+
+    pub struct WeightToFee(u32);
+    impl Convert<Weight, u64> for WeightToFee {
+        fn convert(t: Weight) -> u64 {
+            t as u64
+        }
+    }
+
+    parameter_types! {
+        pub const ExistentialDeposit: u32 = 0;
+        pub const TransferFee: u32 = 0;
+        pub const CreationFee: u32 = 0;
+        pub const TransactionBaseFee: u32 = 1;
+        pub const TransactionByteFee: u32 = 0;
     }
 
     impl balances::Trait for Test {
+        /// The type for recording an account's balance.
         type Balance = u64;
+        /// What to do if an account's free balance gets zeroed.
         type OnFreeBalanceZero = ();
+        /// What to do if a new account is created.
         type OnNewAccount = ();
+        /// The ubiquitous event type.
         type Event = ();
+
         type TransactionPayment = ();
         type DustRemoval = ();
         type TransferPayment = ();
-    }
-
-    impl timestamp::Trait for Test {
-        type Moment = u64;
-        type OnTimestampSet = ();
+        type ExistentialDeposit = ExistentialDeposit;
+        type TransferFee = TransferFee;
+        type CreationFee = CreationFee;
+        type TransactionBaseFee = TransactionBaseFee;
+        type TransactionByteFee = TransactionByteFee;
+        type WeightToFee = WeightToFee;
     }
 
     impl council::Trait for Test {

+ 50 - 25
src/membership/mock.rs

@@ -8,62 +8,87 @@ pub use system;
 pub use primitives::{Blake2Hasher, H256};
 pub use runtime_primitives::{
     testing::{Digest, DigestItem, Header, UintAuthorityId},
-    traits::{BlakeTwo256, IdentityLookup, OnFinalize},
-    BuildStorage,
+    traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize},
+    weights::Weight,
+    BuildStorage, Perbill,
 };
 
-use srml_support::impl_outer_origin;
+use srml_support::{impl_outer_origin, parameter_types};
 
 impl_outer_origin! {
     pub enum Origin for Test {}
 }
 
-// For testing the module, we construct most of a mock runtime. This means
-// first constructing a configuration type (`Test`) which `impl`s each of the
-// configuration traits of modules we want to use.
-#[derive(Clone, Eq, PartialEq, Debug)]
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
 pub struct Test;
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+    pub const MinimumPeriod: u64 = 5;
+}
+
 impl system::Trait for Test {
     type Origin = Origin;
     type Index = u64;
     type BlockNumber = u64;
+    type Call = ();
     type Hash = H256;
     type Hashing = BlakeTwo256;
-    type Digest = Digest;
     type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
     type Header = Header;
+    type WeightMultiplierUpdate = ();
     type Event = ();
-    type Log = DigestItem;
-    type Lookup = IdentityLookup<u64>;
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
 }
+
 impl timestamp::Trait for Test {
     type Moment = u64;
     type OnTimestampSet = ();
-}
-impl consensus::Trait for Test {
-    type SessionKey = UintAuthorityId;
-    type InherentOfflineReport = ();
-    type Log = DigestItem;
+    type MinimumPeriod = MinimumPeriod;
 }
 
-impl balances::Trait for Test {
-    type Event = ();
+pub struct WeightToFee(u32);
+impl Convert<Weight, u64> for WeightToFee {
+    fn convert(t: Weight) -> u64 {
+        t as u64
+    }
+}
 
-    /// The balance of an account.
-    type Balance = u32;
+parameter_types! {
+    pub const ExistentialDeposit: u32 = 0;
+    pub const TransferFee: u32 = 0;
+    pub const CreationFee: u32 = 0;
+    pub const TransactionBaseFee: u32 = 1;
+    pub const TransactionByteFee: u32 = 0;
+}
 
-    /// A function which is invoked when the free-balance has fallen below the existential deposit and
-    /// has been reduced to zero.
-    ///
-    /// Gives a chance to clean up resources associated with the given account.
+impl balances::Trait for Test {
+    /// The type for recording an account's balance.
+    type Balance = u64;
+    /// What to do if an account's free balance gets zeroed.
     type OnFreeBalanceZero = ();
-
-    /// Handler for when a new account is created.
+    /// What to do if a new account is created.
     type OnNewAccount = ();
+    /// The ubiquitous event type.
+    type Event = ();
 
     type TransactionPayment = ();
     type DustRemoval = ();
     type TransferPayment = ();
+    type ExistentialDeposit = ExistentialDeposit;
+    type TransferFee = TransferFee;
+    type CreationFee = CreationFee;
+    type TransactionBaseFee = TransactionBaseFee;
+    type TransactionByteFee = TransactionByteFee;
+    type WeightToFee = WeightToFee;
 }
 
 impl GovernanceCurrency for Test {

+ 46 - 28
src/roles/mock.rs

@@ -9,62 +9,80 @@ pub use system;
 pub use primitives::{Blake2Hasher, H256};
 pub use runtime_primitives::{
     testing::{Digest, DigestItem, Header, UintAuthorityId},
-    traits::{BlakeTwo256, IdentityLookup, OnFinalize},
-    BuildStorage,
+    traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize},
+    weights::Weight,
+    BuildStorage, Perbill,
 };
 
-use srml_support::impl_outer_origin;
+use srml_support::{impl_outer_origin, parameter_types};
 
 impl_outer_origin! {
     pub enum Origin for Test {}
 }
 
-// For testing the module, we construct most of a mock runtime. This means
-// first constructing a configuration type (`Test`) which `impl`s each of the
-// configuration traits of modules we want to use.
-#[derive(Clone, Eq, PartialEq, Debug)]
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
 pub struct Test;
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+}
+
 impl system::Trait for Test {
     type Origin = Origin;
     type Index = u64;
     type BlockNumber = u64;
+    type Call = ();
     type Hash = H256;
     type Hashing = BlakeTwo256;
-    type Digest = Digest;
     type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
     type Header = Header;
+    type WeightMultiplierUpdate = ();
     type Event = ();
-    type Log = DigestItem;
-    type Lookup = IdentityLookup<u64>;
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
 }
-impl timestamp::Trait for Test {
-    type Moment = u64;
-    type OnTimestampSet = ();
+
+pub struct WeightToFee(u32);
+impl Convert<Weight, u64> for WeightToFee {
+    fn convert(t: Weight) -> u64 {
+        t as u64
+    }
 }
-impl consensus::Trait for Test {
-    type SessionKey = UintAuthorityId;
-    type InherentOfflineReport = ();
-    type Log = DigestItem;
+
+parameter_types! {
+    pub const ExistentialDeposit: u32 = 0;
+    pub const TransferFee: u32 = 0;
+    pub const CreationFee: u32 = 0;
+    pub const TransactionBaseFee: u32 = 1;
+    pub const TransactionByteFee: u32 = 0;
 }
 
 impl balances::Trait for Test {
-    type Event = ();
-
-    /// The balance of an account.
-    type Balance = u32;
-
-    /// A function which is invoked when the free-balance has fallen below the existential deposit and
-    /// has been reduced to zero.
-    ///
-    /// Gives a chance to clean up resources associated with the given account.
+    /// The type for recording an account's balance.
+    type Balance = u64;
+    /// What to do if an account's free balance gets zeroed.
     type OnFreeBalanceZero = ();
-
-    /// Handler for when a new account is created.
+    /// What to do if a new account is created.
     type OnNewAccount = ();
+    /// The ubiquitous event type.
+    type Event = ();
 
     type TransactionPayment = ();
     type DustRemoval = ();
     type TransferPayment = ();
+    type ExistentialDeposit = ExistentialDeposit;
+    type TransferFee = TransferFee;
+    type CreationFee = CreationFee;
+    type TransactionBaseFee = TransactionBaseFee;
+    type TransactionByteFee = TransactionByteFee;
+    type WeightToFee = WeightToFee;
 }
 
 impl GovernanceCurrency for Test {

+ 19 - 9
src/service_discovery/mock.rs

@@ -8,10 +8,10 @@ pub use primitives::{Blake2Hasher, H256};
 pub use runtime_primitives::{
     testing::{Digest, DigestItem, Header, UintAuthorityId},
     traits::{BlakeTwo256, IdentityLookup, OnFinalize},
-    BuildStorage,
+    BuildStorage, Perbill,
 };
 
-use srml_support::{impl_outer_event, impl_outer_origin};
+use srml_support::{impl_outer_event, impl_outer_origin, parameter_types};
 
 impl_outer_origin! {
     pub enum Origin for Test {}
@@ -23,23 +23,33 @@ impl_outer_event! {
     }
 }
 
-// For testing the module, we construct most of a mock runtime. This means
-// first constructing a configuration type (`Test`) which `impl`s each of the
-// configuration traits of modules we want to use.
-#[derive(Clone, Eq, PartialEq, Debug)]
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
 pub struct Test;
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+}
+
 impl system::Trait for Test {
     type Origin = Origin;
     type Index = u64;
     type BlockNumber = u64;
+    type Call = ();
     type Hash = H256;
     type Hashing = BlakeTwo256;
-    type Digest = Digest;
     type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
     type Header = Header;
+    type WeightMultiplierUpdate = ();
     type Event = MetaEvent;
-    type Log = DigestItem;
-    type Lookup = IdentityLookup<u64>;
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
 }
 
 pub fn alice_account() -> u64 {

+ 1 - 1
src/storage/data_object_type_registry.rs

@@ -157,7 +157,7 @@ impl<T: Trait> Module<T> {
 
 #[cfg(test)]
 mod tests {
-    use super::*;
+    //use super::*;
     use crate::storage::mock::*;
 
     use system::{self, EventRecord, Phase};

+ 69 - 46
src/storage/mock.rs

@@ -1,7 +1,7 @@
 #![cfg(test)]
 
 pub use super::{data_directory, data_object_storage_registry, data_object_type_registry};
-use crate::currency::GovernanceCurrency;
+pub use crate::currency::GovernanceCurrency;
 use crate::roles::actors;
 use crate::traits;
 use runtime_io::with_externalities;
@@ -10,11 +10,12 @@ pub use system;
 pub use primitives::{Blake2Hasher, H256};
 pub use runtime_primitives::{
     testing::{Digest, DigestItem, Header, UintAuthorityId},
-    traits::{BlakeTwo256, IdentityLookup, OnFinalize},
-    BuildStorage,
+    traits::{BlakeTwo256, Convert, IdentityLookup, OnFinalize},
+    weights::Weight,
+    BuildStorage, Perbill,
 };
 
-use srml_support::{impl_outer_event, impl_outer_origin};
+use srml_support::{impl_outer_event, impl_outer_origin, parameter_types};
 
 impl_outer_origin! {
     pub enum Origin for Test {}
@@ -112,23 +113,80 @@ impl traits::ContentIdExists<Test> for MockContent {
     }
 }
 
-// For testing the module, we construct most of a mock runtime. This means
-// first constructing a configuration type (`Test`) which `impl`s each of the
-// configuration traits of modules we want to use.
-#[derive(Clone, Eq, PartialEq, Debug)]
+// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
+#[derive(Clone, PartialEq, Eq, Debug)]
 pub struct Test;
+parameter_types! {
+    pub const BlockHashCount: u64 = 250;
+    pub const MaximumBlockWeight: u32 = 1024;
+    pub const MaximumBlockLength: u32 = 2 * 1024;
+    pub const AvailableBlockRatio: Perbill = Perbill::one();
+    pub const MinimumPeriod: u64 = 5;
+}
+
 impl system::Trait for Test {
     type Origin = Origin;
     type Index = u64;
     type BlockNumber = u64;
+    type Call = ();
     type Hash = H256;
     type Hashing = BlakeTwo256;
-    type Digest = Digest;
     type AccountId = u64;
+    type Lookup = IdentityLookup<Self::AccountId>;
     type Header = Header;
+    type WeightMultiplierUpdate = ();
     type Event = MetaEvent;
-    type Log = DigestItem;
-    type Lookup = IdentityLookup<u64>;
+    type BlockHashCount = BlockHashCount;
+    type MaximumBlockWeight = MaximumBlockWeight;
+    type MaximumBlockLength = MaximumBlockLength;
+    type AvailableBlockRatio = AvailableBlockRatio;
+    type Version = ();
+}
+
+impl timestamp::Trait for Test {
+    type Moment = u64;
+    type OnTimestampSet = ();
+    type MinimumPeriod = MinimumPeriod;
+}
+
+pub struct WeightToFee(u32);
+impl Convert<Weight, u64> for WeightToFee {
+    fn convert(t: Weight) -> u64 {
+        t as u64
+    }
+}
+
+parameter_types! {
+    pub const ExistentialDeposit: u32 = 0;
+    pub const TransferFee: u32 = 0;
+    pub const CreationFee: u32 = 0;
+    pub const TransactionBaseFee: u32 = 1;
+    pub const TransactionByteFee: u32 = 0;
+}
+
+impl balances::Trait for Test {
+    /// The type for recording an account's balance.
+    type Balance = u64;
+    /// What to do if an account's free balance gets zeroed.
+    type OnFreeBalanceZero = ();
+    /// What to do if a new account is created.
+    type OnNewAccount = ();
+    /// The ubiquitous event type.
+    type Event = MetaEvent;
+
+    type TransactionPayment = ();
+    type DustRemoval = ();
+    type TransferPayment = ();
+    type ExistentialDeposit = ExistentialDeposit;
+    type TransferFee = TransferFee;
+    type CreationFee = CreationFee;
+    type TransactionBaseFee = TransactionBaseFee;
+    type TransactionByteFee = TransactionByteFee;
+    type WeightToFee = WeightToFee;
+}
+
+impl GovernanceCurrency for Test {
+    type Currency = balances::Module<Self>;
 }
 
 impl data_object_type_registry::Trait for Test {
@@ -163,41 +221,6 @@ impl actors::ActorRemoved<Test> for () {
     fn actor_removed(_: &u64) {}
 }
 
-impl timestamp::Trait for Test {
-    type Moment = u64;
-    type OnTimestampSet = ();
-}
-
-impl consensus::Trait for Test {
-    type SessionKey = UintAuthorityId;
-    type InherentOfflineReport = ();
-    type Log = DigestItem;
-}
-
-impl balances::Trait for Test {
-    type Event = MetaEvent;
-
-    /// The balance of an account.
-    type Balance = u32;
-
-    /// A function which is invoked when the free-balance has fallen below the existential deposit and
-    /// has been reduced to zero.
-    ///
-    /// Gives a chance to clean up resources associated with the given account.
-    type OnFreeBalanceZero = ();
-
-    /// Handler for when a new account is created.
-    type OnNewAccount = ();
-
-    type TransactionPayment = ();
-    type DustRemoval = ();
-    type TransferPayment = ();
-}
-
-impl GovernanceCurrency for Test {
-    type Currency = balances::Module<Self>;
-}
-
 pub struct ExtBuilder {
     first_data_object_type_id: u64,
     first_content_id: u64,