This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add pallet dev mode #12536
Merged
Merged
Add pallet dev mode #12536
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
8e171a1
stub for construct_dev_runtime!
sam0x17 ba45d0b
revert
sam0x17 e1883c9
stub for dev_mode proc macro
sam0x17 1f3eb5a
preliminary docs for pallet::dev_mode (attribute) proc macro
sam0x17 ef886e2
add dev_mode to pallet_macros module
sam0x17 a62437a
add docs item for dev_mode to frame_support
sam0x17 89337a6
parsing of #[pallet(dev_mode)]
sam0x17 4d4f235
strip out dev_mode stub since it will be an arg for pallet instead
sam0x17 76eb78b
make pallet Def struct aware of dev mode
sam0x17 7ac54de
WIP
sam0x17 2a5857e
revert changes to call.rs
sam0x17 fce8e41
pass dev_mode to pallet parsing code
sam0x17 011f1e0
auto-specify default weights when in dev mode if not specified
sam0x17 41f159c
add proof / expect for syn::parse in dev mode weight processing
sam0x17 a7c85b3
set all storages to unbounded when in dev mode
sam0x17 bb71c7f
just use 0
sam0x17 45f9932
add invalid pallet arg test
sam0x17 0470808
add passing dev mode pallet test
sam0x17 b0cfc6a
add test confirming that dev mode features only work in dev mode
sam0x17 b5a82cb
cargo fmt + clean up
sam0x17 450f09d
bump CI
sam0x17 e765c42
fix pallet ui test
sam0x17 755a62c
add docs for dev mode
sam0x17 69411ab
add warning about using dev mode in production circumstances
sam0x17 4f8b58f
remove comment about no other attributes being supported
sam0x17 da619d7
fix unneeded assignment
sam0x17 d350781
make warning more explicit
sam0x17 6ac7643
more explicit warning about using dev mode in production
sam0x17 dc038d4
simpler assignment for dev_mode boolean
sam0x17 555516d
add note about MEL requirement
sam0x17 386a8c1
add comment specifying why weights can be omitted in example
sam0x17 b2d988a
tweak wording of comments
sam0x17 0aea2c8
bump ci
sam0x17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,7 @@ impl CallDef { | |
attr_span: proc_macro2::Span, | ||
index: usize, | ||
item: &mut syn::Item, | ||
dev_mode: bool, | ||
) -> syn::Result<Self> { | ||
let item_impl = if let syn::Item::Impl(item) = item { | ||
item | ||
|
@@ -213,6 +214,14 @@ impl CallDef { | |
}, | ||
); | ||
|
||
if weight_attrs.is_empty() && dev_mode { | ||
// inject a default O(1) weight when dev mode is enabled and no weight has | ||
// been specified on the call | ||
let empty_weight: syn::Expr = syn::parse(quote::quote!(0).into()) | ||
.expect("we are parsing a quoted string; qed"); | ||
weight_attrs.push(FunctionAttr::Weight(empty_weight)); | ||
Comment on lines
+220
to
+222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Would be the same. |
||
} | ||
|
||
if weight_attrs.len() != 1 { | ||
let msg = if weight_attrs.is_empty() { | ||
"Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: Invalid pallet macro call: expected no attributes, e.g. macro call must be just `#[frame_support::pallet]` or `#[pallet]` | ||
--> $DIR/attr_non_empty.rs:1:26 | ||
error: Invalid pallet macro call: unexpected attribute. Macro call must be bare, such as `#[frame_support::pallet]` or `#[pallet]`, or must specify the `dev_mode` attribute, such as `#[frame_support::pallet(dev_mode)]` or #[pallet(dev_mode)]. | ||
--> tests/pallet_ui/attr_non_empty.rs:1:26 | ||
| | ||
1 | #[frame_support::pallet [foo]] | ||
| ^^^ |
33 changes: 33 additions & 0 deletions
33
frame/support/test/tests/pallet_ui/dev_mode_without_arg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet::*; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
// The struct on which we build all of our Pallet logic. | ||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
// Your Pallet's configuration trait, representing custom external types and interfaces. | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
#[pallet::storage] | ||
type MyStorage<T: Config> = StorageValue<_, Vec<u8>>; | ||
|
||
// Your Pallet's callable functions. | ||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
pub fn my_call(_origin: OriginFor<T>) -> DispatchResult { | ||
Ok(()) | ||
} | ||
} | ||
|
||
// Your Pallet's internal functions. | ||
impl<T: Config> Pallet<T> {} | ||
} | ||
|
||
fn main() {} |
11 changes: 11 additions & 0 deletions
11
frame/support/test/tests/pallet_ui/dev_mode_without_arg.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]` | ||
--> tests/pallet_ui/dev_mode_without_arg.rs:24:7 | ||
| | ||
24 | pub fn my_call(_origin: OriginFor<T>) -> DispatchResult { | ||
| ^^ | ||
|
||
error[E0432]: unresolved import `pallet` | ||
--> tests/pallet_ui/dev_mode_without_arg.rs:3:9 | ||
| | ||
3 | pub use pallet::*; | ||
| ^^^^^^ help: a similar path exists: `test_pallet::pallet` |
34 changes: 34 additions & 0 deletions
34
frame/support/test/tests/pallet_ui/dev_mode_without_arg_max_encoded_len.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet::*; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
// The struct on which we build all of our Pallet logic. | ||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
// Your Pallet's configuration trait, representing custom external types and interfaces. | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
#[pallet::storage] | ||
type MyStorage<T: Config> = StorageValue<_, Vec<u8>>; | ||
|
||
// Your Pallet's callable functions. | ||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
#[pallet::weight(0)] | ||
pub fn my_call(_origin: OriginFor<T>) -> DispatchResult { | ||
Ok(()) | ||
} | ||
} | ||
|
||
// Your Pallet's internal functions. | ||
impl<T: Config> Pallet<T> {} | ||
} | ||
|
||
fn main() {} |
17 changes: 17 additions & 0 deletions
17
frame/support/test/tests/pallet_ui/dev_mode_without_arg_max_encoded_len.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error[E0277]: the trait bound `Vec<u8>: MaxEncodedLen` is not satisfied | ||
--> tests/pallet_ui/dev_mode_without_arg_max_encoded_len.rs:11:12 | ||
| | ||
11 | #[pallet::pallet] | ||
| ^^^^^^ the trait `MaxEncodedLen` is not implemented for `Vec<u8>` | ||
| | ||
= help: the following other types implement trait `MaxEncodedLen`: | ||
() | ||
(TupleElement0, TupleElement1) | ||
(TupleElement0, TupleElement1, TupleElement2) | ||
(TupleElement0, TupleElement1, TupleElement2, TupleElement3) | ||
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4) | ||
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5) | ||
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6) | ||
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7) | ||
and 78 others | ||
= note: required because of the requirements on the impl of `StorageInfoTrait` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageMyStorage<T>, Vec<u8>>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[frame_support::pallet(foo)] | ||
pub mod pallet {} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: Invalid pallet macro call: unexpected attribute. Macro call must be bare, such as `#[frame_support::pallet]` or `#[pallet]`, or must specify the `dev_mode` attribute, such as `#[frame_support::pallet(dev_mode)]` or #[pallet(dev_mode)]. | ||
--> tests/pallet_ui/pallet_invalid_arg.rs:1:25 | ||
| | ||
1 | #[frame_support::pallet(foo)] | ||
| ^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub use pallet::*; | ||
|
||
#[frame_support::pallet(dev_mode)] | ||
pub mod pallet { | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
|
||
// The struct on which we build all of our Pallet logic. | ||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
// Your Pallet's configuration trait, representing custom external types and interfaces. | ||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
// The MEL requirement for bounded pallets is skipped by `dev_mode`. | ||
#[pallet::storage] | ||
sam0x17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type MyStorage<T: Config> = StorageValue<_, Vec<u8>>; | ||
|
||
// Your Pallet's callable functions. | ||
#[pallet::call] | ||
impl<T: Config> Pallet<T> { | ||
// No need to define a `weight` attribute here because of `dev_mode`. | ||
pub fn my_call(_origin: OriginFor<T>) -> DispatchResult { | ||
sam0x17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ok(()) | ||
} | ||
} | ||
|
||
// Your Pallet's internal functions. | ||
impl<T: Config> Pallet<T> {} | ||
} | ||
|
||
fn main() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it common to put HTML into the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now AFAIK there is no built-in way of doing a warning block unless something about the syntax of your code creates a doc warning (which isn't the case here), so the common workaround is to just use the same CSS classes rust docs use when there is a real warning and hard-code the HTML for now.
rust-lang/rust#73935 has tracked it since 2020
The actual HTML I use here was based on an SO answer that I am now having trouble locating, but yes it is using some of the built-in classes to achieve this warning look
TLDR: yes, for warnings