From d6fd10b60adaa912bae1e480e63ad37449bbdf3c Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Fri, 5 Jan 2024 16:04:20 +0100
Subject: [PATCH] refactor error handling

---
 docs/api/runtime-calls.md | 40 +++++++++++++++++++--------------------
 xtask/src/gen_doc.rs      | 29 +++++++++++++++++-----------
 2 files changed, 38 insertions(+), 31 deletions(-)

diff --git a/docs/api/runtime-calls.md b/docs/api/runtime-calls.md
index 88c28cd52..dd5b3eb1c 100644
--- a/docs/api/runtime-calls.md
+++ b/docs/api/runtime-calls.md
@@ -100,7 +100,7 @@ Cancel a named scheduled task.
 
 <details><summary><code>schedule_after(after, maybe_periodic, priority, call)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 after: T::BlockNumber
@@ -117,7 +117,7 @@ Anonymously schedule a task after a delay.
 
 <details><summary><code>schedule_named_after(id, after, maybe_periodic, priority, call)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 id: TaskName
@@ -137,7 +137,7 @@ Schedule a named task after a delay.
 
 <details><summary><code>report_equivocation(equivocation_proof, key_owner_proof)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 equivocation_proof: Box<EquivocationProof<T::Header>>
@@ -178,7 +178,7 @@ The dispatch origin for this call must be `Signed` by the transactor.
 
 <details><summary><code>set_balance_deprecated(who, new_free, old_reserved)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 who: AccountIdLookupOf<T>
@@ -248,7 +248,7 @@ The dispatch origin of this call must be Signed.
 
 <details><summary><code>transfer(dest, value)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 dest: AccountIdLookupOf<T>
@@ -265,7 +265,7 @@ WARNING: DEPRECATED! Will be released in approximately 3 months.
 
 <details><summary><code>force_set_balance(who, new_free)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 who: AccountIdLookupOf<T>
@@ -407,7 +407,7 @@ remove an identity from the blacklist
 
 <details><summary><code>report_equivocation(equivocation_proof, key_owner_proof)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 equivocation_proof: Box<EquivocationProof<T::Hash, T::BlockNumber>>
@@ -427,7 +427,7 @@ will be reported.
 
 <details><summary><code>dispatch_as_root_unchecked_weight(call, weight)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 call: Box<<T as Config>::Call>
@@ -542,7 +542,7 @@ Origin must be a member of the collective.
 
 <details><summary><code>propose(threshold, proposal, length_bound)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 threshold: MemberCount
@@ -595,7 +595,7 @@ fee.
 
 <details><summary><code>close(proposal_hash, index, proposal_weight_bound, length_bound)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 proposal_hash: T::Hash
@@ -991,7 +991,7 @@ The origin must be allow to certify.
 
 <details><summary><code>create_swap(target, hashed_proof, action, duration)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 target: T::AccountId
@@ -1019,7 +1019,7 @@ The dispatch origin for this call must be _Signed_.
 
 <details><summary><code>claim_swap(proof, action)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 proof: Vec<u8>
@@ -1040,7 +1040,7 @@ The dispatch origin for this call must be _Signed_.
 
 <details><summary><code>cancel_swap(target, hashed_proof)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 target: T::AccountId
@@ -1088,7 +1088,7 @@ O(Z + C) where Z is the length of the call and C its execution weight.
 
 <details><summary><code>as_multi(threshold, other_signatories, maybe_timepoint, call, max_weight)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 threshold: u16
@@ -1144,7 +1144,7 @@ may be found in the deposited `MultisigExecuted` event.
 
 <details><summary><code>approve_as_multi(threshold, other_signatories, maybe_timepoint, call_hash, max_weight)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 threshold: u16
@@ -1617,7 +1617,7 @@ includes bypassing `frame_system::Config::BaseCallFilter`).
 
 <details><summary><code>with_weight(call, weight)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 call: Box<<T as Config>::RuntimeCall>
@@ -1729,7 +1729,7 @@ Set the number of pages in the WebAssembly environment's heap.
 
 <details><summary><code>set_code(code)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 code: Vec<u8>
@@ -1746,7 +1746,7 @@ Set the new runtime code.
 
 <details><summary><code>set_code_without_checks(code)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 code: Vec<u8>
@@ -1811,7 +1811,7 @@ the prefix we are removing to accurately calculate the weight of this function.
 
 <details><summary><code>plan_config_change(config)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 config: NextConfigDescriptor
@@ -1882,7 +1882,7 @@ remove an identity from the set of authorities
 
 <details><summary><code>note_stalled(delay, best_finalized_block_number)</code></summary>
 
-Taking -1 % of a block.
+Taking 0 % of a block.
 
 ```rust
 delay: T::BlockNumber
diff --git a/xtask/src/gen_doc.rs b/xtask/src/gen_doc.rs
index 06188243c..0afee2bee 100644
--- a/xtask/src/gen_doc.rs
+++ b/xtask/src/gen_doc.rs
@@ -265,9 +265,9 @@ pub(super) fn gen_doc() -> Result<()> {
             bail!("unsuported metadata version")
         };
 
-    let mut weights = get_weights(max_weight);
-    // AdHoc names convertion
-    // TODO automatic, possible???
+    let mut weights = get_weights(max_weight)?;
+
+    // Ad hoc names conversion between pallet filename and instance name
     rename_key(&mut weights, &"FrameSystem".into(), "System".into());
     rename_key(&mut weights, &"DuniterAccount".into(), "Account".into());
     rename_key(
@@ -291,20 +291,28 @@ pub(super) fn gen_doc() -> Result<()> {
         &"CertificationSmithCert".into(),
         "SmithCert".into(),
     );
-    // Pallet with manual benchmark
+
+    // We enforce weight for each pallet.
+    // For pallets with manual or no weight, we define a default value.
     weights.insert("Babe".to_string(), Default::default()); // Manual
     weights.insert("Grandpa".to_string(), Default::default()); // Manual
     weights.insert("Sudo".to_string(), Default::default()); // Only > v1.0 has WeightInfo TODO at update
     weights.insert("AtomicSwap".to_string(), Default::default()); // No weight
 
-    // Insert weights
+    // Insert weights for each call of each pallet.
+    // If no weight is available, the weight is set to 0.
+    // We use the relative weight in percent computed as the extrinsic base +
+    // the extrinsic execution divided by the total weight available in
+    // one block. If the weight depends on a complexity parameter,
+    // we display the worst possible weight, taking the upper limit as
+    // defined during the benchmark.
     runtime.iter_mut().for_each(|pallet| {
         pallet.calls.iter_mut().for_each(|call| {
             call.weight = weights
                 .get(&pallet.name)
                 .expect(&("No weight for ".to_owned() + &pallet.name))
                 .get(&call.name)
-                .map_or(-1f64, |weight| {
+                .map_or(0f64, |weight| {
                     (weight.relative_weight * 10000.).round() / 10000.
                 })
         })
@@ -331,9 +339,8 @@ pub(super) fn gen_doc() -> Result<()> {
 fn get_max_weight_from_metadata_v14(
     metadata_v14: frame_metadata::v14::RuntimeMetadataV14,
 ) -> Result<u128> {
-    // Compute the weights from the weight files.
-    // Require the `pallet_xxx.rs` weights,
-    // the `paritydb_weights.rs` and the `extrinsic_weights.rs`
+    // Extract the maximal weight available in one block
+    // from the metadata.
     let block_weights = metadata_v14
         .pallets
         .iter()
@@ -427,12 +434,12 @@ fn get_from_metadata_v14(
     Ok(pallets)
 }
 
-fn get_weights(max_weight: u128) -> HashMap<String, HashMap<String, WeightInfo>> {
+fn get_weights(max_weight: u128) -> Result<HashMap<String, HashMap<String, WeightInfo>>> {
     analyze_weight(
         Path::new(WEIGHT_FILEPATH),
         &MaxBlockWeight::new(max_weight as f64),
     )
-    .expect("Weights unavailable")
+    .map_err(|e| anyhow::anyhow!(e))
 }
 
 /// use template to render markdown file with runtime calls documentation
-- 
GitLab