7

What's the main difference (besides the code) between using the decl_storage! macro rule and using the [pallet::storage] macro?

I've seen pretty much everyone using macro rules, but on the latest versions of the substrate node, I can't import macro rules like decl_event! or decl_error!, only decl_storage!

1
  • 1
    It's best to avoid greetings and thanks in questions, just describing the issue should be enough :-)
    – Dcompoze
    Apr 5, 2022 at 8:01

2 Answers 2

7

The decl_* macros were deprecated in favour of the newer procedural macros, such as [pallet::storage].

The problem with old macros is that they invented new syntax, instead of allowing users to write idiomatic Rust code. This caused several problems, including difficulties in diagnosing syntax errors, and causing rustfmt to not work well.

It is not recommended to use the decl_* macros anymore.

A more detailed rationale is discussed here: https://github.com/paritytech/substrate/discussions/7788#discussioncomment-252559

4

Declarative macros (specifically, macro_rules!) are used in FRAME V1. Most of FRAME pallets written prior to January 2021 use these. FRAME V2 on the other hand, officially introduced in January 2021 uses attribute macros and today are what all FRAME pallets use. There still exists projects which use FRAME V1 however developers are encouraged to upgrade their pallets. Although V2 is backwards compatible with V1, V1 is no longer being improved for new features.

The major improvements from V1 and V2 is that V2 provides a much better developer experience.

Main differences:

  • V1 is closer to writing DSL (domain-specific language) code, as in it requires runtime engineers to write more code to declare traits and types, because everything is custom to the macro requirements in the background. This also makes understanding compilation errors more difficult.
  • V2 is more heavily based on Rust idioms to define types and uses FRAME system and support crates to write code within the constraints of the macro being used. This means that if all macros were removed, the pallet would still compile as all macro inputs are correct Rust syntax, making it easier to understand where errors are coming from.

You can read more about the FRAME V1 macros here.

1
  • 1
    essentially the macro is used to generate impl of storage trait defined here for crud operations to on-chain storage
    – Ron
    Apr 5, 2022 at 15:07

Your Answer

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

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