Skip to content

Prevent storage item reads/writes at compile time by types that are not whitelisted #149

Open
@gpestana

Description

@gpestana

It would be useful to limit at compile time the types that can access (read and/or write) a pallet storage item as a way to ensure safety and encapsulation. This can probably be achieved by augmenting the current outer macro to statically check if the storage item is only accessed by the whitelisted structs.

For example:

#[pallet::storage(allow_only(Ticker))]
pub type Count<T: Config> = StorageValue<_, u32, ValueQuery>;

impl<T: Config> Pallet<T> {
 fn add_count() {
  Count::<T>::mutate(|c| *c + 1) // compile time error because `Count` is accessed outside of the `Ticker` impl.
 }
}

// snip..

pub struct Ticker<T> {
 // snip..
}

impl <T: Config> Ticker<T> {
 fn add_count() {
    // set locks, do checks/ something else that is hard to enforce outside this impl..

    // and then..
    Count::<T>::mutate(|c| *c + 1) // OK, whitelisted
  }
}

This would be very useful to ensure that there is a strict control on how and when a storage item is mutated and/or read. An example of this is paritytech/substrate#14582, where we want to encapsulate all the logic to update the staking ledger (locks, etc) in a struct impl.

The annotation syntax is open for discussion. I think it would be useful to have some degree of granularity and whitelist only reads, writes or reads and write through the annotation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    I10-unconfirmedIssue might be valid, but it's not yet known.I5-enhancementAn additional feature request.T1-FRAMEThis PR/Issue is related to core FRAME, the framework.

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions