frame_support/traits/safe_mode.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Types to put the runtime into safe mode.
19
20/// Can put the runtime into a safe mode.
21///
22/// When the runtime entered safe mode, transaction processing for most general transactions is
23/// paused.
24pub trait SafeMode {
25 /// Block number type.
26 type BlockNumber;
27
28 /// Whether safe mode is entered.
29 fn is_entered() -> bool {
30 Self::remaining().is_some()
31 }
32
33 /// How many more blocks safe mode will stay entered.
34 ///
35 /// If this returns `0`, then safe mode will exit in the next block.
36 fn remaining() -> Option<Self::BlockNumber>;
37
38 /// Enter safe mode for `duration` blocks.
39 ///
40 /// Should error when already entered with `AlreadyEntered`.
41 fn enter(duration: Self::BlockNumber) -> Result<(), SafeModeError>;
42
43 /// Extend safe mode for `duration` blocks.
44 ///
45 /// Should error when not entered with `AlreadyExited`.
46 fn extend(duration: Self::BlockNumber) -> Result<(), SafeModeError>;
47
48 /// Exit safe mode immediately.
49 ///
50 /// This takes effect already in the same block.
51 fn exit() -> Result<(), SafeModeError>;
52}
53
54/// The error type for [`SafeMode`].
55pub enum SafeModeError {
56 /// Safe mode is already entered.
57 AlreadyEntered,
58 /// Safe mode is already exited.
59 AlreadyExited,
60 /// Unknown error.
61 Unknown,
62}
63
64/// A trait to notify when the runtime enters or exits safe mode.
65pub trait SafeModeNotify {
66 /// Called when the runtime enters safe mode.
67 fn entered();
68
69 /// Called when the runtime exits safe mode.
70 fn exited();
71}
72
73impl SafeModeNotify for () {
74 fn entered() {}
75 fn exited() {}
76}