Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[developer_hub] Defensive Programming in Substrate Reference Document #2206

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c27d31e
initial draft
CrackTheCode016 Nov 6, 2023
7579a4e
minor changes + fix stuff
CrackTheCode016 Nov 7, 2023
57c7931
trim it down
CrackTheCode016 Nov 7, 2023
411a6c9
Merge branch 'kiz-developer-hub' into kiz-developer-hub
CrackTheCode016 Nov 7, 2023
dc50e7c
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 8, 2023
538a100
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 8, 2023
330c245
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 8, 2023
cccd9a6
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 8, 2023
0c81245
fmt
CrackTheCode016 Nov 8, 2023
7a1e59f
start addressing comments
CrackTheCode016 Nov 8, 2023
d921b7e
fmt + add fixed examples
CrackTheCode016 Nov 9, 2023
01ae609
add beginning of general info
CrackTheCode016 Nov 9, 2023
c8dbb45
add examples
CrackTheCode016 Nov 13, 2023
bd47256
def saturated add example
CrackTheCode016 Nov 13, 2023
0b653c6
Merge branch 'kiz-developer-hub' into kiz-developer-hub
CrackTheCode016 Nov 14, 2023
dbde974
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 20, 2023
413c05e
Update developer-hub/src/polkadot_sdk/frame_runtime.rs
CrackTheCode016 Nov 20, 2023
eccbf52
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 20, 2023
f589d62
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 20, 2023
5917fbe
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 20, 2023
9d96a98
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 20, 2023
25fb0f6
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
b77e5a2
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
eac36f4
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
9958011
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
9e42f75
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
b5e36fa
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
77cdda4
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
c6c399c
Update developer-hub/src/reference_docs/safe_defensive_programming.rs
CrackTheCode016 Nov 22, 2023
7ff41a8
Merge branch 'kiz-developer-hub' into kiz-developer-hub
CrackTheCode016 Nov 22, 2023
c2cd0d5
update name, make more concise
CrackTheCode016 Nov 22, 2023
0aa08c2
wording
CrackTheCode016 Nov 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions developer-hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ frame = { path = "../substrate/frame", features = ["runtime", "experimental"] }
pallet-examples = { path = "../substrate/frame/examples" }
pallet-default-config-example = { path = "../substrate/frame/examples/default-config" }

# Extra example pallets
pallet-referenda = { path = "../substrate/frame/referenda" }
pallet-broker = { path = "../substrate/frame/broker" }

# How we build docs in rust-docs
simple-mermaid = { git = "https://github.com/kianenigma/simple-mermaid.git", branch = "main" }
docify = "0.2.6"
Expand Down Expand Up @@ -57,6 +61,7 @@ sp-api = { path = "../substrate/primitives/api" }
sp-core = { path = "../substrate/primitives/core" }
sp-keyring = { path = "../substrate/primitives/keyring" }
sp-runtime = { path = "../substrate/primitives/runtime" }
sp-arithmetic = { path = "../substrate/primitives/arithmetic" }

[dev-dependencies]
parity-scale-codec = "3.6.5"
Expand Down
6 changes: 3 additions & 3 deletions developer-hub/src/guides/your_first_pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
//! This macro will call `.into()` under the hood.
#![doc = docify::embed!("./src/guides/your_first_pallet/mod.rs", transfer_better)]
//!
//! Moreover, you will learn in the [Safe Defensive Programming
//! section](crate::reference_docs::safe_defensive_programming) that it is always recommended to use
//! Moreover, you will learn in the [Defensive Programming
//! section](crate::reference_docs::defensive_programming) that it is always recommended to use
//! safe arithmetic operations in your runtime. By using [`frame::traits::CheckedSub`], we can not
//! only take a step in that direction, but also improve the error handing and make it slightly more
//! ergonomic.
Expand Down Expand Up @@ -288,7 +288,7 @@
//! The following topics where used in this guide, but not covered in depth. It is suggested to
//! study them subsequently:
//!
//! - [`crate::reference_docs::safe_defensive_programming`].
//! - [`crate::reference_docs::defensive_programming`].
//! - [`crate::reference_docs::frame_origin`].
//! - [`crate::reference_docs::frame_composite_enums`].
//! - The pallet we wrote in this guide was using `dev_mode`, learn more in
Expand Down
568 changes: 568 additions & 0 deletions developer-hub/src/reference_docs/defensive_programming.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion developer-hub/src/reference_docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub mod frame_origin;

/// Learn about how to write safe and defensive code in your FRAME runtime.
// TODO: @CrackTheCode016
pub mod safe_defensive_programming;
pub mod defensive_programming;

/// Learn about composite enums in FRAME-based runtimes, such as "RuntimeEvent" and "RuntimeCall".
pub mod frame_composite_enums;
Expand Down

This file was deleted.

7 changes: 7 additions & 0 deletions docs/mermaid/integer_operation_decision.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
flowchart LR
CH["Checked"]
ST["Saturated"]
CH-->NEED["The user needs to know that the operation failed - and why"]
CH-->DOUBT["Unsure whether this operation could fail/overflow"]
ST-->SILENT["Silently reaching upper/lower bound will not result in any damage"]
ST-->REASON["In all reasonable circumstances, the number will not overflow, but safety is still desired"]