Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
d565382b
Commit
d565382b
authored
3 years ago
by
Qinxuan Chen
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Migrate the pallet macros of some tests and benches (#9853)
Signed-off-by:
koushiro
<
koushiro.cqx@gmail.com
>
parent
f8a22885
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
substrate/frame/executive/src/lib.rs
+101
-76
101 additions, 76 deletions
substrate/frame/executive/src/lib.rs
substrate/frame/system/benches/bench.rs
+20
-19
20 additions, 19 deletions
substrate/frame/system/benches/bench.rs
with
121 additions
and
95 deletions
substrate/frame/executive/src/lib.rs
+
101
−
76
View file @
d565382b
...
...
@@ -575,15 +575,9 @@ where
#[cfg(test)]
mod
tests
{
use
super
::
*
;
use
frame_support
::{
assert_err
,
parameter_types
,
traits
::{
Currency
,
LockIdentifier
,
LockableCurrency
,
WithdrawReasons
},
weights
::{
IdentityFee
,
RuntimeDbWeight
,
Weight
,
WeightToFeePolynomial
},
};
use
frame_system
::{
Call
as
SystemCall
,
ChainContext
,
LastRuntimeUpgradeInfo
};
use
hex_literal
::
hex
;
use
pallet_balances
::
Call
as
BalancesCall
;
use
pallet_transaction_payment
::
CurrencyAdapter
;
use
sp_core
::
H256
;
use
sp_runtime
::{
generic
::{
DigestItem
,
Era
},
...
...
@@ -594,95 +588,135 @@ mod tests {
},
DispatchError
,
};
use
frame_support
::{
assert_err
,
parameter_types
,
traits
::{
Currency
,
LockIdentifier
,
LockableCurrency
,
WithdrawReasons
},
weights
::{
IdentityFee
,
RuntimeDbWeight
,
Weight
,
WeightToFeePolynomial
},
};
use
frame_system
::{
Call
as
SystemCall
,
ChainContext
,
LastRuntimeUpgradeInfo
};
use
pallet_balances
::
Call
as
BalancesCall
;
use
pallet_transaction_payment
::
CurrencyAdapter
;
const
TEST_KEY
:
&
[
u8
]
=
&*
b":test:key:"
;
#[frame_support::pallet]
mod
custom
{
use
frame_support
::
weights
::{
DispatchClass
,
Weight
};
use
sp_runtime
::
transaction_validity
::{
TransactionSource
,
TransactionValidity
,
TransactionValidityError
,
UnknownTransaction
,
};
use
frame_support
::
pallet_prelude
::
*
;
use
frame_system
::
pallet_prelude
::
*
;
#[pallet::pallet]
#[pallet::generate_store(pub(super)
trait
Store)]
pub
struct
Pallet
<
T
>
(
_
);
#[pallet::config]
pub
trait
Config
:
frame_system
::
Config
{}
frame_support
::
decl_module!
{
pub
struct
Module
<
T
:
Config
>
for
enum
Call
where
origin
:
T
::
Origin
{
#[weight
=
100
]
fn
some_function
(
origin
)
{
// NOTE: does not make any different.
frame_system
::
ensure_signed
(
origin
)
?
;
}
#[weight
=
(
200
,
DispatchClass::Operational)]
fn
some_root_operation
(
origin
)
{
frame_system
::
ensure_root
(
origin
)
?
;
}
#[weight
=
0
]
fn
some_unsigned_message
(
origin
)
{
frame_system
::
ensure_none
(
origin
)
?
;
}
#[pallet::hooks]
impl
<
T
:
Config
>
Hooks
<
BlockNumberFor
<
T
>>
for
Pallet
<
T
>
{
// module hooks.
// one with block number arg and one without
fn
on_initialize
(
n
:
T
::
BlockNumber
)
->
Weight
{
println!
(
"on_initialize({})"
,
n
);
175
}
#[weight
=
0
]
fn
allowed_unsigned
(
origin
)
{
frame_system
::
ensure_root
(
origin
)
?
;
}
fn
on_idle
(
n
:
T
::
BlockNumber
,
remaining_weight
:
Weight
)
->
Weight
{
println!
(
"on_idle{}, {})"
,
n
,
remaining_weight
);
175
}
#[weight
=
0
]
fn
unallowed_unsigned
(
origin
)
{
frame_system
::
ensure_root
(
origin
)
?
;
}
fn
on_finalize
(
n
:
T
::
BlockNumber
)
{
println!
(
"on_finalize({})"
,
n
);
}
#[w
eight
=
0
]
fn
inherent_call
(
origin
)
{
let
_
=
frame_system
::
ensure_none
(
origin
)
?
;
}
fn
on_runtime_upgrade
()
->
W
eight
{
sp_io
::
storage
::
set
(
super
::
TEST_KEY
,
"module"
.as_bytes
());
200
}
// module hooks.
// one with block number arg and one without
fn
on_initialize
(
n
:
T
::
BlockNumber
)
->
Weight
{
println!
(
"on_initialize({})"
,
n
);
175
}
fn
offchain_worker
(
n
:
T
::
BlockNumber
)
{
assert_eq!
(
T
::
BlockNumber
::
from
(
1u32
),
n
);
}
}
fn
on_idle
(
n
:
T
::
BlockNumber
,
remaining_weight
:
Weight
)
->
Weight
{
println!
(
"on_idle{}, {})"
,
n
,
remaining_weight
);
175
}
#[pallet::call]
impl
<
T
:
Config
>
Pallet
<
T
>
{
#[pallet::weight(
100
)]
pub
fn
some_function
(
origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
// NOTE: does not make any different.
frame_system
::
ensure_signed
(
origin
)
?
;
Ok
(())
}
fn
on_finalize
()
{
println!
(
"on_finalize(?)"
);
}
#[pallet::weight((
200
,
DispatchClass::Operational))]
pub
fn
some_root_operation
(
origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
frame_system
::
ensure_root
(
origin
)
?
;
Ok
(())
}
fn
on_runtime_upgrade
()
->
Weight
{
sp_io
::
storage
::
set
(
super
::
TEST_KEY
,
"module"
.as_bytes
());
200
}
#[pallet::weight(
0
)]
pub
fn
some_unsigned_message
(
origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
frame_system
::
ensure_none
(
origin
)
?
;
Ok
(())
}
fn
offchain_worker
(
n
:
T
::
BlockNumber
)
{
assert_eq!
(
T
::
BlockNumber
::
from
(
1u32
),
n
);
}
#[pallet::weight(
0
)]
pub
fn
allowed_unsigned
(
origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
frame_system
::
ensure_root
(
origin
)
?
;
Ok
(())
}
#[weight
=
0
]
fn
calculate_storage_root
(
_origin
)
{
let
root
=
sp_io
::
storage
::
root
();
sp_io
::
storage
::
set
(
"storage_root"
.as_bytes
(),
&
root
);
}
#[pallet::weight(
0
)]
pub
fn
unallowed_unsigned
(
origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
frame_system
::
ensure_root
(
origin
)
?
;
Ok
(())
}
#[pallet::weight(
0
)]
pub
fn
inherent_call
(
origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
let
_
=
frame_system
::
ensure_none
(
origin
)
?
;
Ok
(())
}
#[pallet::weight(
0
)]
pub
fn
calculate_storage_root
(
_origin
:
OriginFor
<
T
>
)
->
DispatchResult
{
let
root
=
sp_io
::
storage
::
root
();
sp_io
::
storage
::
set
(
"storage_root"
.as_bytes
(),
&
root
);
Ok
(())
}
}
impl
<
T
:
Config
>
frame_support
::
inherent
::
ProvideInherent
for
Module
<
T
>
{
#[pallet::inherent]
impl
<
T
:
Config
>
ProvideInherent
for
Pallet
<
T
>
{
type
Call
=
Call
<
T
>
;
type
Error
=
sp_inherents
::
MakeFatalError
<
()
>
;
const
INHERENT_IDENTIFIER
:
[
u8
;
8
]
=
*
b"test1234"
;
fn
create_inherent
(
_data
:
&
sp_inherents
::
InherentData
)
->
Option
<
Self
::
Call
>
{
fn
create_inherent
(
_data
:
&
InherentData
)
->
Option
<
Self
::
Call
>
{
None
}
fn
is_inherent
(
call
:
&
Self
::
Call
)
->
bool
{
*
call
==
Call
::
<
T
>
::
inherent_call
{}
}
}
impl
<
T
:
Config
>
sp_runtime
::
traits
::
ValidateUnsigned
for
Module
<
T
>
{
#[pallet::validate_unsigned]
impl
<
T
:
Config
>
ValidateUnsigned
for
Pallet
<
T
>
{
type
Call
=
Call
<
T
>
;
// Inherent call is accepted for being dispatched
fn
pre_dispatch
(
call
:
&
Self
::
Call
)
->
Result
<
(),
TransactionValidityError
>
{
match
call
{
Call
::
allowed_unsigned
{
..
}
=>
Ok
(()),
Call
::
inherent_call
{
..
}
=>
Ok
(()),
_
=>
Err
(
UnknownTransaction
::
NoUnsignedValidator
.into
()),
}
}
// Inherent call is not validated as unsigned
fn
validate_unsigned
(
_source
:
TransactionSource
,
...
...
@@ -693,15 +727,6 @@ mod tests {
_
=>
UnknownTransaction
::
NoUnsignedValidator
.into
(),
}
}
// Inherent call is accepted for being dispatched
fn
pre_dispatch
(
call
:
&
Self
::
Call
)
->
Result
<
(),
TransactionValidityError
>
{
match
call
{
Call
::
allowed_unsigned
{
..
}
=>
Ok
(()),
Call
::
inherent_call
{
..
}
=>
Ok
(()),
_
=>
Err
(
UnknownTransaction
::
NoUnsignedValidator
.into
()),
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
substrate/frame/system/benches/bench.rs
+
20
−
19
View file @
d565382b
...
...
@@ -16,8 +16,6 @@
// limitations under the License.
use
criterion
::{
black_box
,
criterion_group
,
criterion_main
,
Criterion
};
use
frame_support
::{
decl_event
,
decl_module
};
use
frame_system
as
system
;
use
sp_core
::
H256
;
use
sp_runtime
::{
testing
::
Header
,
...
...
@@ -25,24 +23,24 @@ use sp_runtime::{
Perbill
,
};
#[frame_support::pallet]
mod
module
{
use
super
::
*
;
use
frame_support
::
pallet_prelude
::
*
;
pub
trait
Config
:
system
::
Config
{
type
Event
:
From
<
Event
>
+
Into
<<
Self
as
system
::
Config
>
::
Event
>
;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super)
trait
Store)]
pub
struct
Pallet
<
T
>
(
_
);
decl_module!
{
pub
struct
Module
<
T
:
Config
>
for
enum
Call
where
origin
:
T
::
Origin
{
pub
fn
deposit_event
()
=
default
;
}
#[pallet::config]
pub
trait
Config
:
frame_system
::
Config
{
type
Event
:
From
<
Event
>
+
IsType
<<
Self
as
frame_system
::
Config
>
::
Event
>
;
}
decl_
event
!
(
pub
enum
E
vent
{
Complex
(
Vec
<
u8
>
,
u32
,
u16
,
u128
),
}
);
#[pallet::
event
]
#[pallet::generate_deposit(pub(super)
fn
deposit_e
vent
)]
pub
enum
Event
{
Complex
(
Vec
<
u8
>
,
u32
,
u16
,
u128
),
}
}
type
UncheckedExtrinsic
=
frame_system
::
mocking
::
MockUncheckedExtrinsic
<
Runtime
>
;
...
...
@@ -55,7 +53,7 @@ frame_support::construct_runtime!(
UncheckedExtrinsic
=
UncheckedExtrinsic
,
{
System
:
frame_system
::{
Pallet
,
Call
,
Config
,
Storage
,
Event
<
T
>
},
Module
:
module
::{
Pallet
,
Call
,
Event
},
Module
:
module
::{
Pallet
,
Event
},
}
);
...
...
@@ -70,7 +68,7 @@ frame_support::parameter_types! {
4
*
1024
*
1024
,
Perbill
::
from_percent
(
75
),
);
}
impl
system
::
Config
for
Runtime
{
impl
frame_
system
::
Config
for
Runtime
{
type
BaseCallFilter
=
frame_support
::
traits
::
Everything
;
type
BlockWeights
=
();
type
BlockLength
=
BlockLength
;
...
...
@@ -101,14 +99,17 @@ impl module::Config for Runtime {
}
fn
new_test_ext
()
->
sp_io
::
TestExternalities
{
system
::
GenesisConfig
::
default
()
.build_storage
::
<
Runtime
>
()
.unwrap
()
.into
()
frame_system
::
GenesisConfig
::
default
()
.build_storage
::
<
Runtime
>
()
.unwrap
()
.into
()
}
fn
deposit_events
(
n
:
usize
)
{
let
mut
t
=
new_test_ext
();
t
.execute_with
(||
{
for
_
in
0
..
n
{
module
::
Modu
le
::
<
Runtime
>
::
deposit_event
(
module
::
Event
::
Complex
(
module
::
Pal
le
t
::
<
Runtime
>
::
deposit_event
(
module
::
Event
::
Complex
(
vec!
[
1
,
2
,
3
],
2
,
3
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment