Skip to content

Commit

Permalink
test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Aug 2, 2022
1 parent 39d62b7 commit 1a562e5
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ jobs:
- name: Check Build
run: |
SKIP_WASM_BUILD=1 cargo check --release
- name: Test the pallet
run: |
cargo test -p pallet-slashing-voting
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The additional feature is the slashing mechanism which provides an incentive for

Navigate to [`pallet/slashing-voting`](pallets/slashing-voting/README.md)

**NOTE**: *This is an experimental pallet, no research has not conducted to actually prove economic costs of this governance system.*
**NOTE**: *This is an experimental pallet, no research has been conducted to actually prove economic costs of this governance system.*

## Idea

Expand Down
3 changes: 2 additions & 1 deletion pallets/slashing-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ branch = 'polkadot-v0.9.26' # Must *match* the rest of your Substrate deps!
[dev-dependencies]
sp-io = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26" }
sp-core = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26" }
pallet-identity = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26" }
pallet-balances = { version = "4.0.0-dev",git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26" }

[features]
default = ["std"]
Expand All @@ -39,5 +41,4 @@ std = [
"sp-std/std"
]

runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
6 changes: 6 additions & 0 deletions pallets/slashing-voting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

use frame_support::traits::Get;
use frame_support::traits::ReservableCurrency;
use frame_support::BoundedVec;
Expand Down
150 changes: 150 additions & 0 deletions pallets/slashing-voting/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use crate as pallet_voting;
use frame_system::EnsureRoot;
use frame_support::pallet_prelude::ConstU32;
use frame_support::traits::ConstU128;
use frame_support::traits::{ConstU16, ConstU64};
use frame_system as system;
use frame_support::parameter_types;
use frame_support::PalletId;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup, IdentifyAccount, Verify},
MultiSignature,
};

pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
pub type Block = frame_system::mocking::MockBlock<Test>;

pub const UNIT: u128 = 1000000000000;
/// An index to a block.
pub type BlockNumber = u64;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

/// Balance of an account.
pub type Balance = u128;

/// Index of a transaction in the chain.
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
QuadraticVoting: pallet_voting::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Identity: pallet_identity::{Pallet, Call, Storage, Event<T>},
}
);

impl system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = Call;
type Index = Index;
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u128>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

//let's make identity operations free-of-charge for testing purposes
parameter_types! {
pub const BasicDeposit: Balance = 0;
pub const FieldDeposit: Balance = 0;
pub const SubAccountDeposit: Balance = 0;
pub const MaxSubAccounts: u32 = 100;
pub const MaxAdditionalFields: u32 = 100;
pub const MaxRegistrars: u32 = 20;
}

impl pallet_identity::Config for Test {
type Event = Event;
type Currency = Balances;
type BasicDeposit = BasicDeposit;
type FieldDeposit = FieldDeposit;
type SubAccountDeposit = SubAccountDeposit;
type MaxSubAccounts = MaxSubAccounts;
type MaxAdditionalFields = MaxAdditionalFields;
type MaxRegistrars = MaxRegistrars;
type Slashed = ();
type ForceOrigin = EnsureRoot<AccountId>;
type RegistrarOrigin = EnsureRoot<AccountId>;
type WeightInfo = pallet_identity::weights::SubstrateWeight<Test>;
}

impl pallet_balances::Config for Test {
type MaxLocks = ConstU32<50>;
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
/// The type for recording an account's balance.
type Balance = Balance;
/// The ubiquitous event type.
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ConstU128<500>;
type AccountStore = System;
type WeightInfo = pallet_balances::weights::SubstrateWeight<Test>;
}

parameter_types! {
pub const EntryFee: Balance = 30_000 * UNIT;
pub const MaxProposals: u32 = 10u32;
pub const RevealLength: BlockNumber = 50u64;
pub const MinLength: BlockNumber = 100u64;
pub const MaxTokens: u8 = 100u8;
pub const VotingPalletId: PalletId = PalletId(*b"p/v8t1ng");
}

pub struct VotingIdentityProvider;
impl pallet_voting::IdentityProvider<AccountId> for VotingIdentityProvider {
fn check_existence(account: &AccountId) -> bool {
Identity::identity(account).is_some()
}
}

impl pallet_voting::Config for Test {
type Event = Event;
type IdentityProvider = VotingIdentityProvider;
type Currency = Balances;
type BasicDeposit = EntryFee;
type MaxProposals = MaxProposals;
type Public = <Signature as Verify>::Signer;
type Signature = MultiSignature;
type RevealLength = RevealLength;
type MinLength = MinLength;
type MaxVotingTokens = MaxTokens;
type PalletId = VotingPalletId;
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
}
11 changes: 11 additions & 0 deletions pallets/slashing-voting/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::{mock::*, Error};
use frame_support::{assert_noop, assert_ok};


#[test]
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
// Dispatch a signed extrinsic.
assert!(true);
});
}
10 changes: 0 additions & 10 deletions pallets/slashing-voting/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ pub enum Vote {
No,
}

/// To generate signature
/// ```
/// fn generate() -> String {
/// let pair: sp_core::sr25519::Pair = Pair::from_string("//Alice", None).unwrap();
/// let payload = (Vote::No, 10u32).encode();
/// let payload: [u8; 6] = payload.try_into().unwrap();
/// let signed = pair.sign(&payload).0;
/// format!("{:02x?}", signed)
/// }
/// ```

#[derive(Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, TypeInfo)]
pub struct Commit<Signature> {
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ try-runtime = [
"pallet-grandpa/try-runtime",
"pallet-randomness-collective-flip/try-runtime",
"pallet-sudo/try-runtime",
"pallet-slashing-voting/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
]
1 change: 0 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ mod benches {
[frame_system, SystemBench::<Runtime>]
[pallet_balances, Balances]
[pallet_timestamp, Timestamp]
[pallet_template, TemplateModule]
);
}

Expand Down

0 comments on commit 1a562e5

Please sign in to comment.