Skip to content

Commit

Permalink
v1 governance integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jun 11, 2024
1 parent a64276f commit c218080
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 23 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ pallet-scheduler = { git = "https://github.com/paritytech/polkadot-sdk", branch
pallet-grandpa = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
pallet-message-queue = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
pallet-membership = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
pallet-collective = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }
pallet-democracy = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.3.0", default-features = false }

# EVM & Ethereum
# (wasm)
Expand Down
36 changes: 33 additions & 3 deletions bin/collator/src/local/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
//! Chain specifications.

use local_runtime::{
wasm_binary_unwrap, AccountId, AuraConfig, AuraId, BalancesConfig, DappStakingConfig,
EVMConfig, GrandpaConfig, GrandpaId, InflationConfig, InflationParameters, Precompiles,
RuntimeGenesisConfig, Signature, SudoConfig, SystemConfig, TierThreshold, VestingConfig, AST,
wasm_binary_unwrap, AccountId, AuraConfig, AuraId, BalancesConfig, CouncilMembershipConfig,
DappStakingCommitteeMembershipConfig, DappStakingConfig, EVMConfig, GrandpaConfig, GrandpaId,
InflationConfig, InflationParameters, Precompiles, RuntimeGenesisConfig, Signature, SudoConfig,
SystemConfig, TechnicalCommitteeMembershipConfig, TierThreshold, VestingConfig, AST,
};
use sc_service::ChainType;
use sp_core::{crypto::Ss58Codec, sr25519, Pair, Public};
Expand Down Expand Up @@ -98,6 +99,11 @@ fn testnet_genesis(
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
) -> RuntimeGenesisConfig {
let accounts: Vec<AccountId> = vec!["Alice", "Bob", "Charlie", "Dave", "Eve"]
.iter()
.map(|s| get_account_id_from_seed::<sr25519::Public>(s))
.collect();

// This is supposed the be the simplest bytecode to revert without returning any data.
// We will pre-deploy it under all of our precompiles to ensure they can be called from
// within contracts.
Expand Down Expand Up @@ -186,6 +192,30 @@ fn testnet_genesis(
params: InflationParameters::default(),
..Default::default()
},
council_membership: CouncilMembershipConfig {
members: accounts
.clone()
.try_into()
.expect("Should support at least 5 members."),
phantom: Default::default(),
},
technical_committee_membership: TechnicalCommitteeMembershipConfig {
members: accounts[..3]
.to_vec()
.try_into()
.expect("Should support at least 3 members."),
phantom: Default::default(),
},
dapp_staking_committee_membership: DappStakingCommitteeMembershipConfig {
members: accounts
.try_into()
.expect("Should support at least 5 members."),
phantom: Default::default(),
},
council: Default::default(),
technical_committee: Default::default(),
dapp_staking_committee: Default::default(),
democracy: Default::default(),
}
}

Expand Down
19 changes: 11 additions & 8 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub mod pallet {
+ MaxEncodedLen
+ SmartContractHandle<Self::AccountId>;

/// Privileged origin that is allowed to register & unregister smart contracts to & from the protocol.
type ContractRegistryOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;

/// Privileged origin for managing dApp staking pallet.
type ManagerOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;

Expand Down Expand Up @@ -656,7 +659,7 @@ pub mod pallet {
smart_contract: T::SmartContract,
) -> DispatchResult {
Self::ensure_pallet_enabled()?;
T::ManagerOrigin::ensure_origin(origin)?;
T::ContractRegistryOrigin::ensure_origin(origin)?;

ensure!(
!IntegratedDApps::<T>::contains_key(&smart_contract),
Expand Down Expand Up @@ -744,7 +747,7 @@ pub mod pallet {
new_owner: T::AccountId,
) -> DispatchResult {
Self::ensure_pallet_enabled()?;
let origin = Self::ensure_signed_or_manager(origin)?;
let origin = Self::ensure_signed_or_root(origin)?;

IntegratedDApps::<T>::try_mutate(
&smart_contract,
Expand Down Expand Up @@ -783,7 +786,7 @@ pub mod pallet {
smart_contract: T::SmartContract,
) -> DispatchResult {
Self::ensure_pallet_enabled()?;
T::ManagerOrigin::ensure_origin(origin)?;
T::ContractRegistryOrigin::ensure_origin(origin)?;

let dapp_info =
IntegratedDApps::<T>::get(&smart_contract).ok_or(Error::<T>::ContractNotFound)?;
Expand Down Expand Up @@ -1565,7 +1568,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::force())]
pub fn force(origin: OriginFor<T>, forcing_type: ForcingType) -> DispatchResult {
Self::ensure_pallet_enabled()?;
T::ManagerOrigin::ensure_origin(origin)?;
ensure_root(origin)?;

ensure!(!Safeguard::<T>::get(), Error::<T>::ForceNotAllowed);

Expand Down Expand Up @@ -1609,13 +1612,13 @@ pub mod pallet {
}
}

/// Ensure that the origin is either the `ManagerOrigin` or a signed origin.
/// Ensure that the origin is either the root or a signed origin.
///
/// In case of manager, `Ok(None)` is returned, and if signed origin `Ok(Some(AccountId))` is returned.
pub(crate) fn ensure_signed_or_manager(
/// In case of root, `Ok(None)` is returned, and if signed origin `Ok(Some(AccountId))` is returned.
fn ensure_signed_or_root(
origin: T::RuntimeOrigin,
) -> Result<Option<T::AccountId>, BadOrigin> {
if T::ManagerOrigin::ensure_origin(origin.clone()).is_ok() {
if ensure_root(origin.clone()).is_ok() {
return Ok(None);
}
let who = ensure_signed(origin)?;
Expand Down
14 changes: 11 additions & 3 deletions pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{
};

use frame_support::{
construct_runtime, parameter_types,
traits::{fungible::Mutate as FunMutate, ConstU128, ConstU32},
construct_runtime, ord_parameter_types, parameter_types,
traits::{fungible::Mutate as FunMutate, ConstU128, ConstU32, EitherOfDiverse},
weights::Weight,
};
use sp_arithmetic::fixed_point::FixedU128;
Expand All @@ -40,6 +40,7 @@ use astar_primitives::{
dapp_staking::{Observer as DappStakingObserver, SmartContract, StandardTierSlots},
Balance, BlockNumber,
};
use frame_system::{EnsureRoot, EnsureSignedBy};

pub(crate) type AccountId = u64;

Expand Down Expand Up @@ -198,13 +199,20 @@ impl AccountCheck<AccountId> for DummyAccountCheck {
parameter_types! {
pub const BaseNativeCurrencyPrice: FixedU128 = FixedU128::from_rational(5, 100);
}
ord_parameter_types! {
pub const ContractRegistryAccount: AccountId = 1337;
pub const ManagerAccount: AccountId = 25711;
}

impl pallet_dapp_staking::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type SmartContract = MockSmartContract;
type ManagerOrigin = frame_system::EnsureRoot<AccountId>;
type ContractRegistryOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, EnsureSignedBy<ContractRegistryAccount, AccountId>>;
type ManagerOrigin =
EitherOfDiverse<EnsureRoot<AccountId>, EnsureSignedBy<ManagerAccount, AccountId>>;
type NativePriceProvider = DummyPriceProvider;
type StakingRewardHandler = DummyStakingRewardHandler;
type CycleConfiguration = DummyCycleConfiguration;
Expand Down
35 changes: 34 additions & 1 deletion pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,34 @@ fn register_is_ok() {
// Register two contracts using the same owner
assert_register(7, &MockSmartContract::Wasm(2));
assert_register(7, &MockSmartContract::Wasm(3));

// Register a contract using non-root origin
let smart_contract = MockSmartContract::Wasm(4);
let owner = 11;
let dapp_id = NextDAppId::<Test>::get();
assert_ok!(DappStaking::register(
RuntimeOrigin::signed(ContractRegistryAccount::get()),
owner,
smart_contract.clone()
));
System::assert_last_event(RuntimeEvent::DappStaking(Event::DAppRegistered {
owner,
smart_contract,
dapp_id,
}));
})
}

#[test]
fn register_with_incorrect_origin_fails() {
ExtBuilder::build().execute_with(|| {
// Test assumes that Contract registry & Manager origins are different.
assert_noop!(
DappStaking::register(RuntimeOrigin::signed(1), 3, MockSmartContract::Wasm(2)),
DappStaking::register(
RuntimeOrigin::signed(ManagerAccount::get()),
3,
MockSmartContract::Wasm(2)
),
BadOrigin
);
})
Expand Down Expand Up @@ -427,6 +447,19 @@ fn unregister_no_stake_is_ok() {

// Nothing staked on contract, just unregister it.
assert_unregister(&smart_contract);

// Prepare another dApp, unregister it using non-root origin
let smart_contract = MockSmartContract::Wasm(5);
assert_register(owner, &smart_contract);

assert_ok!(DappStaking::unregister(
RuntimeOrigin::signed(ContractRegistryAccount::get()),
smart_contract.clone(),
));
System::assert_last_event(RuntimeEvent::DappStaking(Event::DAppUnregistered {
smart_contract: smart_contract.clone(),
era: ActiveProtocolState::<Test>::get().era,
}));
})
}

Expand Down
1 change: 1 addition & 0 deletions precompiles/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl pallet_dapp_staking_v3::Config for Test {
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type SmartContract = MockSmartContract;
type ContractRegistryOrigin = frame_system::EnsureRoot<AccountId>;
type ManagerOrigin = frame_system::EnsureRoot<AccountId>;
type NativePriceProvider = DummyPriceProvider;
type StakingRewardHandler = DummyStakingRewardHandler;
Expand Down
6 changes: 5 additions & 1 deletion primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fp-evm = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-assets = { workspace = true }
pallet-collective = { workspace = true }
pallet-membership = { workspace = true }
sp-arithmetic = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
Expand Down Expand Up @@ -58,6 +60,8 @@ std = [
"fp-evm/std",
"frame-support/std",
"frame-system/std",
"pallet-membership/std",
"pallet-collective/std",
"sp-std/std",
"sp-runtime/std",
"sp-core/std",
Expand All @@ -77,4 +81,4 @@ std = [
"sp-arithmetic/std",
]
runtime-benchmarks = ["xcm-builder/runtime-benchmarks", "pallet-assets/runtime-benchmarks"]
try-runtime = ["pallet-contracts/try-runtime"]
try-runtime = ["pallet-contracts/try-runtime", "pallet-membership/try-runtime", "pallet-collective/try-runtime"]
64 changes: 64 additions & 0 deletions primitives/src/governance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This file is part of Astar.

// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

use crate::AccountId;
use frame_support::traits::EitherOfDiverse;
use frame_system::EnsureRoot;

pub type OracleMembershipInst = pallet_membership::Instance1;
pub type CouncilMembershipInst = pallet_membership::Instance2;
pub type TechnicalCommitteeMembershipInst = pallet_membership::Instance3;
pub type DappStakingCommitteeMembershipInst = pallet_membership::Instance4;

// Leaving instance 1 for potentially having an oracle membership collective instance
pub type CouncilCollectiveInst = pallet_collective::Instance2;
pub type TechnicalCommitteeCollectiveInst = pallet_collective::Instance3;
pub type DappStakingCommitteeCollectiveInst = pallet_collective::Instance4;

// Council
pub type EnsureRootOrAllCouncil = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollectiveInst, 1, 1>,
>;

pub type EnsureRootOrTwoThirdsCouncil = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollectiveInst, 2, 3>,
>;

// Technical Committee
pub type EnsureRootOrAllTechnicalCommittee = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, TechnicalCommitteeCollectiveInst, 1, 1>,
>;

pub type EnsureRootOrTwoThirdsTechnicalCommittee = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, TechnicalCommitteeCollectiveInst, 2, 3>,
>;

// Dapp Staking Committee
pub type EnsureRootOrAllDappStakingCommittee = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, DappStakingCommitteeCollectiveInst, 1, 1>,
>;

pub type EnsureRootOrTwoThirdsDappStakingCommittee = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, DappStakingCommitteeCollectiveInst, 2, 3>,
>;
3 changes: 3 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub mod testing;
/// Oracle & price primitives.
pub mod oracle;

/// Governance primitives.
pub mod governance;

/// Benchmark primitives
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks;
Expand Down
3 changes: 0 additions & 3 deletions primitives/src/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ use sp_arithmetic::fixed_point::FixedU128;
use sp_std::vec::Vec;

/// Interface for fetching price of the native token.
///
/// **NOTE:** This is just a temporary interface, and will be replaced with a proper oracle which will average
/// the price over a certain period of time.
pub trait PriceProvider {
/// Get the price of the native token.
fn average_price() -> CurrencyAmount;
Expand Down
1 change: 1 addition & 0 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ impl pallet_dapp_staking_v3::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type Currency = Balances;
type SmartContract = SmartContract<AccountId>;
type ContractRegistryOrigin = frame_system::EnsureRoot<AccountId>;
type ManagerOrigin = frame_system::EnsureRoot<AccountId>;
type NativePriceProvider = PriceAggregator;
type StakingRewardHandler = Inflation;
Expand Down
Loading

0 comments on commit c218080

Please sign in to comment.