Defensive Programming in Substrate Reference Document (#2615)

_This PR is being continued from
https://github.com/paritytech/polkadot-sdk/pull/2206, which was closed
when the developer_hub was merged._
closes https://github.com/paritytech/polkadot-sdk-docs/issues/44

---
# Description

This PR adds a reference document to the `developer-hub` crate (see
https://github.com/paritytech/polkadot-sdk/pull/2102). This specific
reference document covers defensive programming practices common within
the context of developing a runtime with Substrate.

In particular, this covers the following areas: 

- Default behavior of how Rust deals with numbers in general
- How to deal with floating point numbers in runtime / fixed point
arithmetic
- How to deal with Integer overflows
- General "safe math" / defensive programming practices for common
pallet development scenarios
- Defensive traits that exist within Substrate, i.e.,
`defensive_saturating_add `, `defensive_unwrap_or`
- More general defensive programming examples (keep it concise)
- Link to relevant examples where these practices are actually in
production / being used
- Unwrapping (or rather lack thereof) 101

todo
-- 
- [x] Apply feedback from previous PR
- [x] This may warrant a PR to append some of these docs to
`sp_arithmetic`

---------

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Francisco Aguirre <franciscoaguirreperez@gmail.com>
Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>
This commit is contained in:
bader y
2024-03-20 09:26:59 -04:00
committed by GitHub
parent 7241a8db7b
commit b686bfefba
10 changed files with 586 additions and 32 deletions
Generated
+6
View File
@@ -13376,7 +13376,9 @@ dependencies = [
"pallet-assets",
"pallet-aura",
"pallet-authorship",
"pallet-babe",
"pallet-balances",
"pallet-broker",
"pallet-collective",
"pallet-default-config-example",
"pallet-democracy",
@@ -13385,6 +13387,7 @@ dependencies = [
"pallet-examples",
"pallet-multisig",
"pallet-proxy",
"pallet-referenda",
"pallet-scheduler",
"pallet-timestamp",
"pallet-transaction-payment",
@@ -13404,6 +13407,7 @@ dependencies = [
"scale-info",
"simple-mermaid",
"sp-api",
"sp-arithmetic",
"sp-core",
"sp-io",
"sp-keyring",
@@ -18395,6 +18399,7 @@ name = "sp-arithmetic"
version = "23.0.0"
dependencies = [
"criterion 0.4.0",
"docify 0.2.7",
"integer-sqrt",
"num-traits",
"parity-scale-codec",
@@ -18403,6 +18408,7 @@ dependencies = [
"scale-info",
"serde",
"sp-crypto-hashing",
"sp-std 14.0.0",
"static_assertions",
]