1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright 2021 Axiom-Team
//
// This file is part of Duniter-v2S.
//
// Duniter-v2S is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// Duniter-v2S is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.

use anyhow::{bail, Context, Result};
use codec::Decode;
use core::hash::Hash;
use scale_info::form::PortableForm;
use serde::Serialize;
use std::{
    collections::HashMap,
    fs::File,
    io::{Read, Write},
    path::Path,
    process::Command,
};
use tera::Tera;
use weightanalyzer::{analyze_weight, MaxBlockWeight, WeightInfo};

fn rename_key<K, V>(h: &mut HashMap<K, V>, old_key: &K, new_key: K)
where
    K: Eq + Hash,
{
    if let Some(v) = h.remove(old_key) {
        h.insert(new_key, v);
    }
}

// consts

const CALLS_DOC_FILEPATH: &str = "docs/api/runtime-calls.md";
const EVENTS_DOC_FILEPATH: &str = "docs/api/runtime-events.md";
const ERRORS_DOC_FILEPATH: &str = "docs/api/runtime-errors.md";
const ERRORS_PO_FILEPATH: &str = "docs/api/runtime-errors.po";
const TEMPLATES_GLOB: &str = "xtask/res/templates/*.{md,po}";
const WEIGHT_FILEPATH: &str = "runtime/gdev/src/weights/";

// define structs and implementations

type RuntimePallets = Vec<Pallet>;

#[derive(Clone, Serialize)]
struct Pallet {
    index: u8,
    name: String,
    type_name: String,
    calls: Vec<Call>,
    events: Vec<Event>,
    errors: Vec<ErroR>,
}
#[derive(Clone, Serialize)]
struct Call {
    documentation: String,
    index: u8,
    name: String,
    params: Vec<CallParam>,
    weight: f64,
}
#[derive(Clone, Serialize)]
struct CallParam {
    name: String,
    type_name: String,
}
#[derive(Clone, Serialize)]
struct Event {
    documentation: String,
    index: u8,
    name: String,
    params: Vec<EventParam>,
}
#[derive(Clone, Serialize)]
struct EventParam {
    name: String,
    type_name: String,
}
#[derive(Clone, Serialize)]
struct ErroR {
    documentation: String,
    index: u8,
    name: String,
}

impl Pallet {
    fn new(
        index: u8,
        name: String,
        type_name: String,
        call_scale_type_def: &Option<scale_info::TypeDef<PortableForm>>,
        event_scale_type_def: &Option<scale_info::TypeDef<PortableForm>>,
        error_scale_type_def: &Option<scale_info::TypeDef<PortableForm>>,
    ) -> Result<Self> {
        let calls = if let Some(call_scale_type_def) = call_scale_type_def {
            if let scale_info::TypeDef::Variant(calls_enum) = call_scale_type_def {
                calls_enum.variants.iter().map(Into::into).collect()
            } else {
                bail!("Invalid metadata")
            }
        } else {
            vec![]
        };
        let events = if let Some(event_scale_type_def) = event_scale_type_def {
            if let scale_info::TypeDef::Variant(events_enum) = event_scale_type_def {
                events_enum.variants.iter().map(Into::into).collect()
            } else {
                bail!("Invalid metadata")
            }
        } else {
            vec![]
        };
        let errors = if let Some(error_scale_type_def) = error_scale_type_def {
            if let scale_info::TypeDef::Variant(errors_enum) = error_scale_type_def {
                errors_enum.variants.iter().map(Into::into).collect()
            } else {
                bail!("Invalid metadata")
            }
        } else {
            vec![]
        };
        Ok(Self {
            index,
            name,
            type_name,
            calls,
            events,
            errors,
        })
    }
}

impl From<&scale_info::Variant<PortableForm>> for Call {
    fn from(variant: &scale_info::Variant<PortableForm>) -> Self {
        Self {
            documentation: variant
                .docs
                .iter()
                .take_while(|line| !line.starts_with("# <weight>"))
                .cloned()
                .collect::<Vec<_>>()
                .join("\n"),
            index: variant.index,
            name: variant.name.to_owned(),
            params: variant.fields.iter().map(Into::into).collect(),
            weight: Default::default(),
        }
    }
}
impl From<&scale_info::Field<PortableForm>> for CallParam {
    fn from(field: &scale_info::Field<PortableForm>) -> Self {
        Self {
            name: field.clone().name.unwrap_or_default().to_string(),
            type_name: field.clone().type_name.unwrap_or_default().to_string(),
        }
    }
}

impl From<&scale_info::Variant<PortableForm>> for Event {
    fn from(variant: &scale_info::Variant<PortableForm>) -> Self {
        Self {
            documentation: variant.docs.to_vec().join("\n"),
            index: variant.index,
            name: variant.name.to_owned(),
            params: variant.fields.iter().map(Into::into).collect(),
        }
    }
}
impl From<&scale_info::Field<PortableForm>> for EventParam {
    fn from(field: &scale_info::Field<PortableForm>) -> Self {
        Self {
            name: field.clone().name.unwrap_or_default().to_string(),
            type_name: field.clone().type_name.unwrap_or_default().to_string(),
        }
    }
}

impl From<&scale_info::Variant<PortableForm>> for ErroR {
    fn from(variant: &scale_info::Variant<PortableForm>) -> Self {
        Self {
            documentation: variant.docs.to_vec().join("\n"),
            index: variant.index,
            name: variant.name.to_owned(),
        }
    }
}

enum CallCategory {
    Disabled,
    Inherent,
    OtherOrigin,
    Root,
    Sudo,
    User,
}

impl CallCategory {
    fn is(pallet_name: &str, call_name: &str) -> Self {
        match (pallet_name, call_name) {
            ("System", "remark" | "remark_with_event") => Self::Disabled,
            ("System", _) => Self::Root,
            ("Babe", "report_equivocation_unsigned") => Self::Inherent,
            ("Babe", "plan_config_change") => Self::Root,
            ("Timestamp", _) => Self::Inherent,
            ("Balances", "set_balance" | "force_transfer" | "force_unreserve") => Self::Root,
            ("AuthorityMembers", "prune_account_id_of" | "remove_member") => Self::Root,
            ("Authorship", _) => Self::Inherent,
            ("Session", _) => Self::Disabled,
            ("Grandpa", "report_equivocation_unsigned") => Self::Inherent,
            ("Grandpa", "note_stalled") => Self::Root,
            ("UpgradeOrigin", "dispatch_as_root") => Self::OtherOrigin,
            ("ImOnline", _) => Self::Inherent,
            ("Sudo", _) => Self::Sudo,
            (
                "Identity",
                "remove_identity" | "prune_item_identities_names" | "prune_item_identity_index_of",
            ) => Self::Root,
            ("Cert", "del_cert" | "remove_all_certs_received_by") => Self::Root,
            ("SmithCert", "del_cert" | "remove_all_certs_received_by") => Self::Root,
            ("TechnicalCommittee", "set_members" | "disapprove_proposal") => Self::Root,
            ("Utility", "dispatch_as") => Self::Root,
            ("Treasury", "approve_proposal" | "reject_proposal") => Self::OtherOrigin,
            _ => Self::User,
        }
    }

    fn is_root(pallet_name: &str, call_name: &str) -> bool {
        matches!(Self::is(pallet_name, call_name), Self::Root)
    }

    fn is_user(pallet_name: &str, call_name: &str) -> bool {
        matches!(Self::is(pallet_name, call_name), Self::User)
    }

    fn is_disabled(pallet_name: &str, call_name: &str) -> bool {
        matches!(Self::is(pallet_name, call_name), Self::Disabled)
    }
}

/// generate runtime calls documentation
pub(super) fn gen_doc() -> Result<()> {
    // Read metadata
    let mut file = std::fs::File::open("resources/metadata.scale")
        .with_context(|| "Failed to open metadata file")?;

    let mut bytes = Vec::new();
    file.read_to_end(&mut bytes)
        .with_context(|| "Failed to read metadata file")?;

    let metadata = frame_metadata::RuntimeMetadataPrefixed::decode(&mut &bytes[..])
        .with_context(|| "Failed to decode metadata")?;

    println!("Metadata successfully loaded!");

    let (mut runtime, max_weight) =
        if let frame_metadata::RuntimeMetadata::V15(ref metadata_v15) = metadata.1 {
            (
                get_from_metadata_v15(metadata_v15.clone())?,
                get_max_weight_from_metadata_v15(metadata_v15.clone())?,
            )
        } else {
            bail!("unsuported metadata version")
        };

    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(
        &mut weights,
        &"Collective".into(),
        "TechnicalCommittee".into(),
    );

    // 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("AtomicSwap".to_string(), Default::default()); // No weight

    // Insert weights for each call of each pallet.
    // If no weight is available, the weight is set to -1.
    // 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| {
                    (weight.relative_weight * 10000.).round() / 10000.
                })
        })
    });

    let (call_doc, event_doc, error_doc, error_po) = print_runtime(runtime);

    // Generate docs from rust code
    Command::new("cargo")
        .args([
            "doc",
            "--package=pallet-*",
            "--package=*-runtime",
            "--package=*distance*",
            "--package=*membership*",
            "--no-deps",
            "--document-private-items",
            "--features=runtime-benchmarks",
            "--package=pallet-atomic-swap",
            "--package=pallet-authority-discovery",
            "--package=pallet-balances",
            "--package=pallet-collective",
            "--package=pallet-im-online",
            "--package=pallet-preimage",
            "--package=pallet-proxy",
            "--package=pallet-scheduler",
            "--package=pallet-session",
            "--package=pallet-sudo",
            "--package=pallet-timestamp",
            "--package=pallet-treasury",
            "--package=pallet-utility",
        ])
        .status()
        .expect("cargo doc failed to execute");

    let mut file = File::create(CALLS_DOC_FILEPATH)
        .with_context(|| format!("Failed to create file '{}'", CALLS_DOC_FILEPATH))?;
    file.write_all(call_doc.as_bytes())
        .with_context(|| format!("Failed to write to file '{}'", CALLS_DOC_FILEPATH))?;
    let mut file = File::create(EVENTS_DOC_FILEPATH)
        .with_context(|| format!("Failed to create file '{}'", EVENTS_DOC_FILEPATH))?;
    file.write_all(event_doc.as_bytes())
        .with_context(|| format!("Failed to write to file '{}'", EVENTS_DOC_FILEPATH))?;
    let mut file = File::create(ERRORS_DOC_FILEPATH)
        .with_context(|| format!("Failed to create file '{}'", ERRORS_DOC_FILEPATH))?;
    file.write_all(error_doc.as_bytes())
        .with_context(|| format!("Failed to write to file '{}'", ERRORS_DOC_FILEPATH))?;
    let mut file = File::create(ERRORS_PO_FILEPATH)
        .with_context(|| format!("Failed to create file '{}'", ERRORS_PO_FILEPATH))?;
    file.write_all(error_po.as_bytes())
        .with_context(|| format!("Failed to write to file '{}'", ERRORS_PO_FILEPATH))?;

    Ok(())
}

fn get_max_weight_from_metadata_v15(
    metadata_v15: frame_metadata::v15::RuntimeMetadataV15,
) -> Result<u128> {
    // Extract the maximal weight available in one block
    // from the metadata.
    let block_weights = metadata_v15
        .pallets
        .iter()
        .find(|pallet| pallet.name == "System")
        .expect("Can't find System pallet metadata")
        .constants
        .iter()
        .find(|constant| constant.name == "BlockWeights")
        .expect("Can't find BlockWeights");

    let block_weights = scale_value::scale::decode_as_type(
        &mut &*block_weights.value,
        &block_weights.ty.id,
        &metadata_v15.types,
    )
    .expect("Can't decode max_weight")
    .value;

    if let scale_value::ValueDef::Composite(scale_value::Composite::Named(i)) = block_weights
        && let scale_value::ValueDef::Composite(scale_value::Composite::Named(j)) =
            &i.iter().find(|name| name.0 == "max_block").unwrap().1.value
        && let scale_value::ValueDef::Primitive(scale_value::Primitive::U128(k)) =
            &j.iter().find(|name| name.0 == "ref_time").unwrap().1.value
    {
        Ok(*k)
    } else {
        bail!("Invalid max_weight")
    }
}

fn get_from_metadata_v15(
    metadata_v15: frame_metadata::v15::RuntimeMetadataV15,
) -> Result<RuntimePallets> {
    println!("Number of pallets: {}", metadata_v15.pallets.len());
    let mut pallets = Vec::new();
    for pallet in metadata_v15.pallets {
        let mut type_name: String = Default::default();
        let calls_type_def = if let Some(calls) = pallet.calls {
            let Some(calls_type) = metadata_v15.types.resolve(calls.ty.id) else {
                bail!("Invalid metadata")
            };
            type_name = calls_type
                .path
                .segments
                .first()
                .expect("cannot decode pallet type")
                .to_string();
            Some(calls_type.type_def.clone())
        } else {
            println!("{}: {} (0 calls)", pallet.index, pallet.name);
            None
        };
        let events_type_def = if let Some(events) = pallet.event {
            let Some(events_type) = metadata_v15.types.resolve(events.ty.id) else {
                bail!("Invalid metadata")
            };
            Some(events_type.type_def.clone())
        } else {
            println!("{}: {} (0 events)", pallet.index, pallet.name);
            None
        };
        let errors_type_def = if let Some(errors) = pallet.error {
            let Some(errors_type) = metadata_v15.types.resolve(errors.ty.id) else {
                bail!("Invalid metadata")
            };
            Some(errors_type.type_def.clone())
        } else {
            println!("{}: {} (0 errors)", pallet.index, pallet.name);
            None
        };

        let pallet = Pallet::new(
            pallet.index,
            pallet.name.clone(),
            type_name,
            &calls_type_def,
            &events_type_def,
            &errors_type_def,
        )?;

        println!(
            "{}: {} ({} calls)",
            pallet.index,
            pallet.name,
            pallet.calls.len()
        );
        println!(
            "{}: {} ({} events)",
            pallet.index,
            pallet.name,
            pallet.events.len()
        );
        println!(
            "{}: {} ({} errors)",
            pallet.index,
            pallet.name,
            pallet.errors.len()
        );
        pallets.push(pallet);
    }
    Ok(pallets)
}

fn get_weights(max_weight: u128) -> Result<HashMap<String, HashMap<String, WeightInfo>>> {
    analyze_weight(
        Path::new(WEIGHT_FILEPATH),
        &MaxBlockWeight::new(max_weight as f64),
    )
    .map_err(|e| anyhow::anyhow!(e))
}

/// use template to render markdown file with runtime calls documentation
fn print_runtime(pallets: RuntimePallets) -> (String, String, String, String) {
    // init variables
    let mut user_calls_counter = 0;
    let user_calls_pallets: RuntimePallets = pallets
        .iter()
        .cloned()
        .filter_map(|mut pallet| {
            let pallet_name = pallet.name.clone();
            pallet
                .calls
                .retain(|call| CallCategory::is_user(&pallet_name, &call.name));
            if pallet.calls.is_empty() {
                None
            } else {
                user_calls_counter += pallet.calls.len();
                Some(pallet)
            }
        })
        .collect();
    let mut root_calls_counter = 0;
    let root_calls_pallets: RuntimePallets = pallets
        .iter()
        .cloned()
        .filter_map(|mut pallet| {
            let pallet_name = pallet.name.clone();
            pallet
                .calls
                .retain(|call| CallCategory::is_root(&pallet_name, &call.name));
            if pallet.calls.is_empty() {
                None
            } else {
                root_calls_counter += pallet.calls.len();
                Some(pallet)
            }
        })
        .collect();
    let mut disabled_calls_counter = 0;
    let disabled_calls_pallets: RuntimePallets = pallets
        .iter()
        .cloned()
        .filter_map(|mut pallet| {
            let pallet_name = pallet.name.clone();
            pallet
                .calls
                .retain(|call| CallCategory::is_disabled(&pallet_name, &call.name));
            if pallet.calls.is_empty() {
                None
            } else {
                disabled_calls_counter += pallet.calls.len();
                Some(pallet)
            }
        })
        .collect();

    let mut event_counter = 0;
    pallets
        .iter()
        .for_each(|pallet| event_counter += pallet.events.len());

    let mut error_counter = 0;
    pallets
        .iter()
        .for_each(|pallet| error_counter += pallet.errors.len());

    // compile template
    let tera = match Tera::new(TEMPLATES_GLOB) {
        Ok(t) => t,
        Err(e) => {
            println!("Parsing error(s): {}", e);
            ::std::process::exit(1);
        }
    };

    // fills tera context for rendering calls
    let mut context = tera::Context::new();
    context.insert("user_calls_counter", &user_calls_counter);
    context.insert("user_calls_pallets", &user_calls_pallets);
    context.insert("root_calls_counter", &root_calls_counter);
    context.insert("root_calls_pallets", &root_calls_pallets);
    context.insert("disabled_calls_counter", &disabled_calls_counter);
    context.insert("disabled_calls_pallets", &disabled_calls_pallets);

    let call_doc = tera
        .render("runtime-calls.md", &context)
        .expect("template error");

    // render events
    context.insert("pallets", &pallets);
    context.insert("event_counter", &event_counter);
    let event_doc = tera
        .render("runtime-events.md", &context)
        .expect("template error");

    // render errors
    context.insert("error_counter", &error_counter);
    let error_doc = tera
        .render("runtime-errors.md", &context)
        .expect("template error");

    let error_po = tera
        .render("runtime-errors.po", &context)
        .expect("template error");

    (call_doc, event_doc, error_doc, error_po)
}