Skip to content

Commit

Permalink
feat(governance): add resolver id to application
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Jan 7, 2025
1 parent c6325ef commit 8659c74
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 139 deletions.
69 changes: 0 additions & 69 deletions node/specs/main.json

This file was deleted.

5 changes: 4 additions & 1 deletion node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ impl SubstrateCli for Cli {
Ok(match id {
"dev" => Box::new(chain_spec::development_config()?),
"main" => Box::new(chain_spec::ChainSpec::from_json_bytes(
include_bytes!("../specs/main.json").as_ref(),
include_bytes!("../../data/mainnet/spec.json").as_ref(),
)?),
"test" => Box::new(chain_spec::ChainSpec::from_json_bytes(
include_bytes!("../../data/testnet/spec.json").as_ref(),
)?),
path => Box::new(chain_spec::ChainSpec::from_json_file(
std::path::PathBuf::from(path),
Expand Down
42 changes: 28 additions & 14 deletions pallets/governance/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::frame::traits::ExistenceRequirement;
use crate::{whitelist, AccountIdOf, AgentApplications, BalanceOf, Block};
use crate::{whitelist, AccountIdOf, AgentApplications, BalanceOf};
use codec::{Decode, Encode, MaxEncodedLen};
use polkadot_sdk::frame_election_provider_support::Get;
use polkadot_sdk::frame_support::dispatch::DispatchResult;
use polkadot_sdk::frame_support::traits::Currency;
use polkadot_sdk::frame_support::traits::WithdrawReasons;
use polkadot_sdk::frame_support::DebugNoBound;
use polkadot_sdk::polkadot_sdk_frame::prelude::BlockNumberFor;
use polkadot_sdk::sp_runtime::BoundedVec;
use polkadot_sdk::sp_std::vec::Vec;
use scale_info::TypeInfo;
Expand All @@ -18,9 +19,9 @@ pub struct AgentApplication<T: crate::Config> {
pub agent_key: AccountIdOf<T>,
pub data: BoundedVec<u8, T::MaxApplicationDataLength>,
pub cost: BalanceOf<T>,
pub expires_at: Block,
pub expires_at: BlockNumberFor<T>,
pub action: ApplicationAction,
pub status: ApplicationStatus,
pub status: ApplicationStatus<T>,
}

#[derive(DebugNoBound, Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq)]
Expand All @@ -30,9 +31,13 @@ pub enum ApplicationAction {
}

#[derive(DebugNoBound, Decode, Encode, TypeInfo, MaxEncodedLen, PartialEq, Eq)]
pub enum ApplicationStatus {
#[scale_info(skip_type_params(T))]
pub enum ApplicationStatus<T: crate::Config> {
Open,
Resolved { accepted: bool },
Resolved {
accepted: bool,
resolver: AccountIdOf<T>,
},
Expired,
}

Expand Down Expand Up @@ -79,10 +84,7 @@ pub fn submit_application<T: crate::Config>(
return Err(crate::Error::<T>::InvalidApplicationDataLength.into());
}

let current_block: u64 =
TryInto::try_into(<polkadot_sdk::frame_system::Pallet<T>>::block_number())
.ok()
.expect("blockchain will not exceed 2^64 blocks; QED.");
let current_block = <polkadot_sdk::frame_system::Pallet<T>>::block_number();

let expires_at = current_block + config.agent_application_expiration;

Expand All @@ -108,7 +110,10 @@ pub fn submit_application<T: crate::Config>(
Ok(())
}

pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResult {
pub fn accept_application<T: crate::Config>(
resolver: T::AccountId,
application_id: u32,
) -> DispatchResult {
let application = crate::AgentApplications::<T>::get(application_id)
.ok_or(crate::Error::<T>::ApplicationNotFound)?;

Expand All @@ -129,7 +134,10 @@ pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResu

crate::AgentApplications::<T>::mutate(application_id, |application| {
if let Some(app) = application {
app.status = ApplicationStatus::Resolved { accepted: true };
app.status = ApplicationStatus::Resolved {
accepted: true,
resolver,
};
}
});

Expand All @@ -142,13 +150,19 @@ pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResu
Ok(())
}

pub fn deny_application<T: crate::Config>(application_id: u32) -> DispatchResult {
pub fn deny_application<T: crate::Config>(
resolver: T::AccountId,
application_id: u32,
) -> DispatchResult {
let application = crate::AgentApplications::<T>::get(application_id)
.ok_or(crate::Error::<T>::ApplicationNotFound)?;

crate::AgentApplications::<T>::mutate(application_id, |application| {
if let Some(app) = application {
app.status = ApplicationStatus::Resolved { accepted: false };
app.status = ApplicationStatus::Resolved {
accepted: false,
resolver,
};
}
});

Expand All @@ -157,7 +171,7 @@ pub fn deny_application<T: crate::Config>(application_id: u32) -> DispatchResult
Ok(())
}

pub(crate) fn resolve_expired_applications<T: crate::Config>(current_block: Block) {
pub(crate) fn resolve_expired_applications<T: crate::Config>(current_block: BlockNumberFor<T>) {
for application in crate::AgentApplications::<T>::iter_values() {
if current_block < application.expires_at {
continue;
Expand Down
9 changes: 5 additions & 4 deletions pallets/governance/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use codec::{Decode, Encode, MaxEncodedLen};
use polkadot_sdk::frame_election_provider_support::Get;
use polkadot_sdk::polkadot_sdk_frame::prelude::BlockNumberFor;
use polkadot_sdk::{frame_support::DebugNoBound, sp_runtime::Percent};
use scale_info::TypeInfo;

use crate::{BalanceOf, BlockAmount};
use crate::BalanceOf;

#[derive(Clone, TypeInfo, Decode, Encode, PartialEq, Eq, DebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct GovernanceConfiguration<T: crate::Config> {
pub proposal_cost: BalanceOf<T>,
pub proposal_expiration: BlockAmount,
pub proposal_expiration: BlockNumberFor<T>,
pub agent_application_cost: BalanceOf<T>,
pub agent_application_expiration: BlockAmount,
pub agent_application_expiration: BlockNumberFor<T>,
pub proposal_reward_treasury_allocation: Percent,
pub max_proposal_reward_treasury_allocation: BalanceOf<T>,
pub proposal_reward_interval: BlockAmount,
pub proposal_reward_interval: BlockNumberFor<T>,
}

impl<T: crate::Config> Default for GovernanceConfiguration<T> {
Expand Down
4 changes: 0 additions & 4 deletions pallets/governance/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ pub(super) type BalanceOf<T> = <<T as crate::Config>::Currency as Currency<
>>::Balance;

pub(super) type AccountIdOf<T> = <T as polkadot_sdk::frame_system::Config>::AccountId;

pub(super) type Block = u64;

pub(super) type BlockAmount = u64;
31 changes: 17 additions & 14 deletions pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod application;
pub mod config;
pub mod ext;
pub mod migrations;
pub mod proposal;
pub mod roles;
pub mod voting;
Expand Down Expand Up @@ -95,7 +96,8 @@ pub mod pallet {
type MaxApplicationDataLength: Get<u32>;

#[pallet::constant]
type ApplicationExpiration: Get<BlockAmount>;
#[pallet::no_default_bounds]
type ApplicationExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
type MaxPenaltyPercentage: Get<Percent>;
Expand All @@ -108,14 +110,16 @@ pub mod pallet {
type DefaultProposalCost: Get<BalanceOf<Self>>;

#[pallet::constant]
type DefaultProposalExpiration: Get<BlockAmount>;
#[pallet::no_default_bounds]
type DefaultProposalExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
#[pallet::no_default_bounds]
type DefaultAgentApplicationCost: Get<BalanceOf<Self>>;

#[pallet::constant]
type DefaultAgentApplicationExpiration: Get<BlockAmount>;
#[pallet::no_default_bounds]
type DefaultAgentApplicationExpiration: Get<BlockNumberFor<Self>>;

#[pallet::constant]
type DefaultProposalRewardTreasuryAllocation: Get<Percent>;
Expand All @@ -125,7 +129,8 @@ pub mod pallet {
type DefaultMaxProposalRewardTreasuryAllocation: Get<BalanceOf<Self>>;

#[pallet::constant]
type DefaultProposalRewardInterval: Get<BlockAmount>;
#[pallet::no_default_bounds]
type DefaultProposalRewardInterval: Get<BlockNumberFor<Self>>;

#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>>
Expand All @@ -136,17 +141,15 @@ pub mod pallet {
type WeightInfo: WeightInfo;
}

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(block_number: BlockNumberFor<T>) -> Weight {
let current_block: u64 = block_number
.try_into()
.ok()
.expect("blockchain won't pass 2 ^ 64 blocks");

fn on_initialize(current_block: BlockNumberFor<T>) -> Weight {
application::resolve_expired_applications::<T>(current_block);
proposal::tick_proposals::<T>(current_block);
proposal::tick_proposal_rewards::<T>(current_block);
Expand Down Expand Up @@ -202,15 +205,15 @@ pub mod pallet {
#[pallet::call_index(6)]
#[pallet::weight((<T as Config>::WeightInfo::accept_application(), DispatchClass::Normal, Pays::Yes))]
pub fn accept_application(origin: OriginFor<T>, application_id: u32) -> DispatchResult {
roles::ensure_curator::<T>(origin)?;
application::accept_application::<T>(application_id)
let key = roles::ensure_curator::<T>(origin)?;
application::accept_application::<T>(key, application_id)
}

#[pallet::call_index(7)]
#[pallet::weight((<T as Config>::WeightInfo::deny_application(), DispatchClass::Normal, Pays::Yes))]
pub fn deny_application(origin: OriginFor<T>, application_id: u32) -> DispatchResult {
roles::ensure_curator::<T>(origin)?;
application::deny_application::<T>(application_id)
let key = roles::ensure_curator::<T>(origin)?;
application::deny_application::<T>(key, application_id)
}

#[pallet::call_index(8)]
Expand Down
Loading

0 comments on commit 8659c74

Please sign in to comment.