Skip to content

Commit

Permalink
Merge pull request #5062 from ignazio-bovo/luxor/reduce-council-budget
Browse files Browse the repository at this point in the history
Luxor/reduce council budget
  • Loading branch information
mnaamani committed Mar 25, 2024
2 parents 9549942 + 61f0c66 commit fff9f89
Show file tree
Hide file tree
Showing 32 changed files with 460 additions and 61 deletions.
2 changes: 1 addition & 1 deletion chain-metadata.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions query-node/chain-metadata/2003.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions query-node/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ typegen:
- council.CouncilorRewardUpdated
- council.RequestFunded
- council.CouncilBudgetFunded
- council.CouncilBudgetDecreased
# Referendum
- referendum.ReferendumStarted
- referendum.ReferendumStartedForcefully
Expand Down Expand Up @@ -914,6 +915,8 @@ mappings:
handler: council_RequestFunded
- event: council.CouncilBudgetFunded
handler: council_CouncilBudgetFunded
- event: council.CouncilBudgetDecreased
handler: council_CouncilBudgetDecreased
# Referendum
- event: referendum.ReferendumStarted
handler: referendum_ReferendumStarted
Expand Down
17 changes: 17 additions & 0 deletions query-node/mappings/src/council.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
CandidacyWithdrawEvent,
Candidate,
CastVote,
CouncilBudgetDecreasedEvent,
CouncilBudgetFundedEvent,
CouncilMember,
CouncilStage,
Expand Down Expand Up @@ -68,6 +69,7 @@ import {
Council_CandidacyStakeReleaseEvent_V1001 as CandidacyStakeReleaseEvent_V1001,
Council_CandidacyWithdrawEvent_V1001 as CandidacyWithdrawEvent_V1001,
Council_CouncilBudgetFundedEvent_V1001 as CouncilBudgetFundedEvent_V1001,
Council_CouncilBudgetDecreasedEvent_V2003 as CouncilBudgetDecreasedEvent_V2003,
Council_RequestFundedEvent_V1001,
Council_CouncilorRewardUpdatedEvent_V1001 as CouncilorRewardUpdatedEvent_V1001,
Council_NewCandidateEvent_V1001 as NewCandidateEvent_V1001,
Expand Down Expand Up @@ -857,6 +859,21 @@ export async function council_CouncilBudgetFunded({ event, store }: EventContext
// no specific event processing
}

export async function council_CouncilBudgetDecreased({ event, store }: EventContext & StoreContext): Promise<void> {
// common event processing

const [amount] = new CouncilBudgetDecreasedEvent_V2003(event).params

const budgetDecreasedEvent = new CouncilBudgetDecreasedEvent({
...genericEventFields(event),
amount,
})

await store.save<CouncilBudgetDecreasedEvent>(budgetDecreasedEvent)

// no specific event processing
}

/// /////////////// Referendum events //////////////////////////////////////////

/*
Expand Down
31 changes: 21 additions & 10 deletions query-node/mappings/src/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
UpdateGlobalNftLimitProposalDetails,
UpdateWorkingGroupBudgetProposalDetails,
VetoProposalDetails,
DecreaseCouncilBudgetProposalDetails,
} from 'query-node/dist/model'
import {
INT32MAX,
Expand All @@ -75,20 +76,18 @@ import {
} from './common'
import {
ProposalsCodex_ProposalCreatedEvent_V1001 as ProposalCreatedEvent_V1001,
ProposalsCodex_ProposalCreatedEvent_V2002 as ProposalCreatedEvent_V2002,
ProposalsCodex_ProposalCreatedEvent_V2003 as ProposalCreatedEvent_V2003,
ProposalsEngine_ProposalCancelledEvent_V1001 as ProposalCancelledEvent_V1001,
ProposalsEngine_ProposalDecisionMadeEvent_V1001 as ProposalDecisionMadeEvent_V1001,
ProposalsEngine_ProposalExecutedEvent_V1001 as ProposalExecutedEvent_V1001,
ProposalsEngine_ProposalStatusUpdatedEvent_V1001 as ProposalStatusUpdatedEvent_V1001,
ProposalsEngine_VotedEvent_V1001 as ProposalVotedEvent_V1001,
} from '../generated/types'
import { PalletProposalsCodexProposalDetails as RuntimeProposalDetails_V1001 } from '../generated/types/1001/types-lookup'
import { PalletProposalsCodexProposalDetails as RuntimeProposalDetails_V2002 } from '../generated/types/2002/types-lookup'
import { PalletProposalsCodexProposalDetails as RuntimeProposalDetails_V2003 } from '../generated/types/2003/types-lookup'

import { createWorkingGroupOpeningMetadata } from './workingGroups'

type RuntimeProposalDetails = RuntimeProposalDetails_V1001 | RuntimeProposalDetails_V2002

async function getProposal(store: DatabaseManager, id: string) {
return getByIdOrFail(store, Proposal, id)
}
Expand All @@ -109,7 +108,7 @@ async function getOrCreateRuntimeWasmBytecode(store: DatabaseManager, bytecode:
async function parseProposalDetails(
event: SubstrateEvent,
store: DatabaseManager,
proposalDetails: RuntimeProposalDetails_V1001 | RuntimeProposalDetails_V2002
proposalDetails: RuntimeProposalDetails_V2003
): Promise<typeof ProposalDetails> {
const eventTime = new Date(event.blockTimestamp)

Expand Down Expand Up @@ -317,12 +316,20 @@ async function parseProposalDetails(
details.newWeeklyNftLimit = specificDetails[1].toNumber()
}
return details
} else if ((proposalDetails as RuntimeProposalDetails_V2002).isSetPalletFozenStatus) {
}
// RuntimeProposalDetails
else if (proposalDetails.isSetPalletFozenStatus) {
const details = new UpdatePalletFrozenStatusProposalDetails()
const [frozen, pallet] = (proposalDetails as RuntimeProposalDetails_V2002).asSetPalletFozenStatus
details.frozen = frozen.isTrue
details.pallet = pallet.toString()
return details
}
// DecreaseCouncilBudgetProposalDetails
else if (proposalDetails.isDecreaseCouncilBudget) {
const details = new DecreaseCouncilBudgetProposalDetails()
details.amount = new BN(proposalDetails.asDecreaseCouncilBudget.toString())
return details
} else {
unimplementedError(`Unsupported proposal details type: ${proposalDetails.type}`)
}
Expand Down Expand Up @@ -361,12 +368,16 @@ export async function proposalsCodex_ProposalCreated({
}: EventContext & StoreContext): Promise<void> {
const specVersion = block.runtimeVersion.specVersion
const [proposalId, generalProposalParameters, runtimeProposalDetails, proposalThreadId] =
parseInt(specVersion.toString()) >= 2002
? new ProposalCreatedEvent_V2002(event).params
: new ProposalCreatedEvent_V1001(event).params
Number(specVersion) < 2001
? new ProposalCreatedEvent_V1001(event).params
: new ProposalCreatedEvent_V2003(event).params

const eventTime = new Date(event.blockTimestamp)
const proposalDetails = await parseProposalDetails(event, store, runtimeProposalDetails)
const proposalDetails = await parseProposalDetails(
event,
store,
runtimeProposalDetails as RuntimeProposalDetails_V2003
)

const proposal = new Proposal({
id: proposalId.toString(),
Expand Down
24 changes: 24 additions & 0 deletions query-node/schemas/councilEvents.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,30 @@ type CouncilBudgetFundedEvent implements Event @entity {
rationale: String!
}

type CouncilBudgetDecreasedEvent implements Event @entity {
### GENERIC DATA ###

"(network}-{blockNumber}-{indexInBlock}"
id: ID!

"Hash of the extrinsic which caused the event to be emitted"
inExtrinsic: String

"Blocknumber of the block in which the event was emitted."
inBlock: Int!

"Network the block was produced in"
network: Network!

"Index of event in block from which it was emitted."
indexInBlock: Int!

### SPECIFIC DATA ###

"Funding amount."
amount: BigInt!
}

################### Referendum #################################################

type ReferendumStartedEvent implements Event @entity {
Expand Down
14 changes: 8 additions & 6 deletions query-node/schemas/proposals.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,9 @@ type UpdateChannelPayoutsProposalDetails @variant {
}

type UpdatePalletFrozenStatusProposalDetails @variant {
"""
New frozen status for pallet
"""
"New frozen status for pallet"
frozen: Boolean!
"""
The pallet to update frozen status on
"""
"The pallet to update frozen status on"
pallet: String!
}

Expand All @@ -351,6 +347,11 @@ type UpdateGlobalNftLimitProposalDetails @variant {
newWeeklyNftLimit: Int
}

type DecreaseCouncilBudgetProposalDetails @variant {
"Proposed amount of token to burn"
amount: BigInt!
}

union ProposalDetails =
SignalProposalDetails
| RuntimeUpgradeProposalDetails
Expand All @@ -376,3 +377,4 @@ union ProposalDetails =
| UpdateChannelPayoutsProposalDetails
| UpdatePalletFrozenStatusProposalDetails
| UpdateGlobalNftLimitProposalDetails
| DecreaseCouncilBudgetProposalDetails
22 changes: 22 additions & 0 deletions runtime-modules/council/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,20 @@ benchmarks! {
);
}

decrease_council_budget {
let reduction_amount: Balance<T> = 100u32.into();

Mutations::<T>::increase_budget(1000u32.into());

}: _ (RawOrigin::Root, reduction_amount)
verify {
assert_eq!(Council::<T>::budget(), 900u32.into(), "Budget not updated");
assert_last_event::<T>(
RawEvent::CouncilBudgetDecreased(reduction_amount).into()
);
}


candidate_remark {
let msg = b"test".to_vec();
let (account_id, member_id) = start_period_announce_candidacy::<T>(0);
Expand Down Expand Up @@ -863,6 +877,14 @@ mod tests {
})
}

#[test]
fn test_decrease_council_budget() {
let config = default_genesis_config();
build_test_externalities(config).execute_with(|| {
assert_ok!(Council::<Runtime>::test_benchmark_decrease_council_budget());
})
}

#[test]
fn test_councilor_remark() {
let config = default_genesis_config();
Expand Down
47 changes: 47 additions & 0 deletions runtime-modules/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ decl_event! {
/// Councilor reward has been updated.
CouncilorRewardUpdated(Balance),

/// Councilor budget has been decreased
/// Params:
/// - Reduction amount
CouncilBudgetDecreased(Balance),

/// Request has been funded
RequestFunded(AccountId, Balance),

Expand Down Expand Up @@ -518,6 +523,9 @@ decl_error! {

/// Cannot withdraw: insufficient budget balance.
InsufficientBalanceForTransfer,

/// Cannot reduce the budget by the given amount.
ReductionAmountTooLarge
}
}

Expand Down Expand Up @@ -843,6 +851,38 @@ decl_module! {
Ok(())
}

/// Decrease the council total budget
///
/// # <weight>
///
/// ## weight
/// `O (1)`
/// - db:
/// - `O(1)` doesn't depend on the state or parameters
/// # </weight>
#[weight = CouncilWeightInfo::<T>::decrease_council_budget()]
pub fn decrease_council_budget(origin, reduction_amount: Balance<T>) -> Result<(), Error<T>> {
// ensure action can be started
EnsureChecks::<T>::can_decrease_council_budget(origin)?;

// ensure reduction amount is not too large
ensure!(
reduction_amount <= Self::budget(),
Error::<T>::ReductionAmountTooLarge
);

//
// == MUTATION SAFE ==
//

// update state
Mutations::<T>::decrease_budget(reduction_amount);

// emit event
Self::deposit_event(RawEvent::CouncilBudgetDecreased(reduction_amount));

Ok(())
}

/// Transfers funds from council budget to account
///
Expand Down Expand Up @@ -1788,6 +1828,13 @@ impl<T: Config> EnsureChecks<T> {

Ok(())
}

// Ensures there is no problem in decreasing the council budget
fn can_decrease_council_budget(origin: T::RuntimeOrigin) -> Result<(), Error<T>> {
ensure_root(origin)?;

Ok(())
}
}

impl<T: Config + common::membership::MembershipTypes>
Expand Down
Loading

0 comments on commit fff9f89

Please sign in to comment.