Skip to content

fix: Treasury Pallet - Review #431 - #434

Merged
czareko merged 3 commits into
mainfrom
fix/treasury_ai
Mar 15, 2026
Merged

czareko merged 3 commits into
mainfrom
fix/treasury_ai

Conversation

@czareko

@czareko czareko commented Mar 15, 2026 •

Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea13f5c268

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

#[pallet::storage]
#[pallet::getter(fn treasury_portion)]
pub type TreasuryPortion<T: Config> = StorageValue<_, u8, ValueQuery>;
pub type TreasuryPortion<T: Config> = StorageValue<_, Permill, OptionQuery>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add storage migration for TreasuryPortion type change

TreasuryPortion was changed from StorageValue<_, u8, ValueQuery> to StorageValue<_, Permill, OptionQuery> without a corresponding runtime migration, so upgraded chains will still have old u8-encoded bytes under the same key. After upgrade, portion() now calls expect(...), and pallets/mining-rewards::on_finalize reads this every block, so a decode mismatch can trap block execution immediately after runtime upgrade. Please add an on_runtime_upgrade migration (or equivalent translation) before switching the storage encoding/query type.

Useful? React with 👍 / 👎.

@czareko
czareko requested review from ethan-crypto, illuzen and n13 March 15, 2026 06:07
Comment thread pallets/treasury/src/lib.rs Outdated
Comment on lines +55 to +58
/// Placeholder for tests/chain-spec defaults. Production must set treasury_account
/// explicitly.
fn default() -> Self {
Self { treasury_account: [0u8; 32].into(), treasury_portion: 50 }
Self { treasury_account: [1u8; 32].into(), treasury_portion: Permill::from_percent(50) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the safest approach here would be to make missing treasury config fail fast during genesis instead of silently misconfiguring the chain with a magic nonzero treasury address. So that would mean either reverting back to the zero address or updating the type to Option<AccountId>,

@n13

n13 commented Mar 15, 2026

Copy link
Copy Markdown
Collaborator

Here's my review of PR #434: Treasury Pallet - Review #431.


Overview

This PR changes TreasuryPortion storage from u8 (0-100) to Permill, switches it to OptionQuery to enforce genesis configuration, adds zero-address rejection for the treasury account, and cleans up all downstream consumers. Good improvements overall -- a few things worth discussing.


Issues

1. Missing storage migration (blocking for live chains)

[deleted, non issue]

2. Default GenesisConfig uses [1u8; 32] -- Ethan's concern is valid

Self { treasury_account: [1u8; 32].into(), treasury_portion: Permill::from_percent(50) }

This is a magic address nobody controls. If someone stands up a chain using the default config without overriding treasury_account, funds silently go to an unspendable address. The old zero-address default was bad too, but at least it would now be caught by the zero-address assertion.

Consider making GenesisConfig::treasury_account an Option<T::AccountId> so genesis explicitly fails if unconfigured, rather than silently using a dummy address.

3. Empty integrity_test is dead code

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
    #[cfg(test)]
    fn integrity_test() {
        // ...comment explaining why it can't validate...
    }
}

An empty test-only hook that does nothing should just be removed. If it can't validate anything, it doesn't need to exist.

4. From<[u8; 32]> + PartialEq bounds spread everywhere

The zero-address check requires T::AccountId: From<[u8; 32]> + PartialEq, which now appears on:

  • BuildGenesisConfig
  • Pallet<T> call impl
  • Pallet<T> inherent impl
  • TreasuryAccountGetter
  • TreasuryPortionGetter (this one doesn't even touch AccountId!)
  • TreasuryProvider impl

This leaks implementation details across every consumer. A cleaner approach might be adding a ZeroAccount associated type or constant to Config, or doing the zero-check only in set_treasury_account and build() rather than in every account_id() call. At minimum, TreasuryPortionGetter should not need AccountId bounds just to return a Permill.

5. Double-panic in account_id() is aggressive

pub fn account_id() -> T::AccountId {
    let account = TreasuryAccount::<T>::get()
        .expect("Treasury account must be set in genesis; chain is misconfigured");
    let zero: T::AccountId = [0u8; 32].into();
    assert!(account != zero, "...");
    account
}

Genesis build() and set_treasury_account already prevent zero addresses, so the only way this assert fires in production is if someone directly writes zero to storage (e.g., a bad migration). A panic in a getter that's called every block is risky -- if it ever triggers, it halts the chain irrecoverably. The genesis/extrinsic guards are sufficient; this runtime assertion could be a debug_assert! instead.


Minor Nits

  • ensure!(portion <= Permill::one(), Error::<T>::InvalidPortion) -- this is technically reachable (someone could submit a hand-crafted extrinsic with Permill::from_parts(2_000_000)), so keeping it is fine, but worth a note that from_percent() and from_rational() already clamp.

  • The genesis assert!(self.treasury_portion <= Permill::one(), ...) is also always true for any Permill constructed through normal APIs -- the only way to violate it is manual from_parts() in test code.


What's Good

  • u8 to Permill is the right call -- enables fractional percentages (e.g., 33.3%) and eliminates the Permill::from_percent(u32::from(...)) boilerplate scattered through mining-rewards.
  • OptionQuery for both storage items enforces that genesis must configure these values. No more silent zero-account fallback.
  • Zero-address rejection on set_treasury_account prevents locking funds.
  • Test coverage is substantially improved -- boundary tests (0%, 100%), panic-when-unconfigured tests, and TreasuryProvider trait conformance.
  • Benchmarks re-run with correct proof sizes for the new Permill (4 bytes vs 1 byte).
  • Mining-rewards cleanup is much cleaner: Treasury::portion().mul_floor(total_reward) reads nicely.

Summary

The core type change (u8 -> Permill) and the OptionQuery switch are solid improvements. The main concerns are: (1) confirm no migration is needed or add one, (2) reconsider the default genesis address, (3) tighten up the trait bounds so they don't bleed into unrelated getters, and (4) consider softening the runtime panics in account_id() to avoid irrecoverable chain halts.

@czareko
czareko requested a review from ethan-crypto March 15, 2026 12:08

@ethan-crypto ethan-crypto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@czareko
czareko merged commit 99a35ec into main Mar 15, 2026
4 checks passed
@n13 n13 mentioned this pull request Apr 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants