1

Is this possible?

Say you have a function in a pallet outside of the lib.rs file that is a function like:

impl<T: Config> Pallet<T> {
  pub fn tester(block: u64) {
    StorageElement::<T>::insert(block);
  }
}

Any scenarios where a user can call this and update the storage?

I assume not although wanted to double check.

If a function is not using a call macro, is this possible?

2
  • Why do you want this?
    – aurexav
    Commented Apr 16 at 5:53
  • Lol i don't, that's why I'm asking. Because I noticed in testing I can call all of the functions I want even if they aren't using the call macro so i wanted to make sure this was only a testing env thing
    – Bob Linux
    Commented Apr 16 at 16:28

3 Answers 3

1

No. These functions are purely internal. It is safe to write internal logic in them - no user can call into it.

However, if you need something purely for testing you can still add a #[cfg(test)] or #[cfg(feature = "std")] to not compile the code into production runtimes.

1

I would also add that it is possible to call this function within runtime or from another pallet (with tight coupling) since it is a public function. For example, in pallet_a:

// Tight coupling with `pallet_b`
pub trait Config: frame_system::Config + pallet_b::Config {}

//..

// then, in the code
pallet_b::Pallet::<T>::tester();
1

Without the #[pallet::call] macro, these functions will not have any dispatch logic which would allow an external caller to trigger the function to execute.

You will note that dispatchable calls all have a certain format like requiring the first parameter to be OriginFor<T> and such.

Without the dispatch logic, there is no way for an incoming transaction to trigger these internal functions, unless you write your own logical paths yourselves.

In this context, the pub fn tester is public, but only in the same way that a rust crate can expose public functions to other rust crates. This level of "public" only lives at the developer level, where we assume maximum power to do anything anyway to the runtime.

As @dastansam mentions, yes other pallets could call this function, because other pallets are also just crates, and you have made a public function accessible by Rust crates, but again, within the context of Runtime development, you can already do anything.

For example, in any pallet I could make the function:

impl<T: Config> Pallet<T> {
  pub fn set_balance() {
    // Set storage directly, not with `#[frame::storage]` macros / APIs.
    sp_io::storage::set(key_for_account_balance, 1_000_000_000_000_000u128)
  }
}

So you can see how the runtime really is just a flat set of logic to access storage, and that Pallets are just a developer abstraction to help write code better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.