-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
505 lines (431 loc) · 15.7 KB
/
main.rs
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
//! Investigate storage size of Substrate chains.
//!
//! ## Example
//!
//! First acquire a state snapshot. We are going to use the People Rococo chain, since it is rather
//! small. You will need the
//! [try-runtime-cli](https://paritytech.github.io/try-runtime-cli/try_runtime/) for this and an
//! archive node to download the state from:
//!
//! ```sh
//! try-runtime create-snapshot --uri wss://rococo-people-rpc.polkadot.io:443 rococo-people.snap
//! ```
//!
//! Then run the analysis:
//!
//! ```sh
//! cargo run --release -- --network rococo-people
//! ```
//!
//! The results will be a bit boring for such a small network, but for a larger one - eg Kusama - it
//! could look like this. You can download [this snapshot](https://tasty.limo/kusama.snap) to try it.
//!
//! ![Kusama storage analysis](./.images/ksm-overview.png)
//!
//! You can also zoom in on a specific pallet:
//!
//! ```sh
//! cargo run --release -- --network rococo-people --pallet Balances
//! ```
//!
//! Again for Kusama:
//!
//! ![Kusama Balances pallet](./.images/ksm-zoom.png)
//!
//! ## License
//!
//! GPLv3 ONLY, see [LICENSE](./LICENSE) file for details.
use anyhow::{anyhow, Result};
use clap::Parser;
use indicatif::{ProgressBar, ProgressStyle};
use itertools::Itertools;
use parity_scale_codec::{Compact, Decode, Encode};
use sp_crypto_hashing::twox_128;
use std::{collections::BTreeMap as Map, fs::File, io::prelude::*};
use subxt::Metadata;
use subxt_metadata::StorageEntryMetadata;
use termtree::Tree;
use tokio::sync::mpsc::{channel, Receiver};
use std::sync::Mutex;
use std::sync::Arc;
use tokio::task;
use std::time::Duration;
use tokio::task::JoinHandle;
use subxt_metadata::PalletMetadata;
/// PDU - Polkadot runtime storage analyzer.
#[derive(Parser)]
struct Args {
/// Name of the network to analyze.
#[clap(short, long)]
network: String,
/// URI of an Archive node endpoint.
#[clap(long, alias = "url")]
uri: Option<String>,
/// Focus only on this pallet.
#[clap(short, long)]
pallet: Option<String>,
/// Print verbose information.
#[clap(long)]
verbose: bool,
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();
let url = args
.uri
.clone()
.unwrap_or(format!("wss://{}-rpc.polkadot.io:443", args.network));
let snap_path = format!("{}.snap", args.network);
let meta_path = format!("{}.meta", args.network);
let verbose = args.verbose || args.pallet.is_some();
let (num_keys, rx) = load_snapshot(&snap_path)?;
let bar = setup_bar(num_keys);
let meta = get_metadata(&meta_path, &url).await?;
let pallets = meta.pallets().sorted_by(|a, b| a.name().cmp(b.name())).collect::<Vec<_>>();
let prefix_lookup = build_prefix_lookup(&pallets);
log::info!("Indexed {} known prefixes", prefix_lookup.len());
log::info!("Starting to categorize {} keys", num_keys);
let rx = Arc::new(Mutex::new(rx));
let prefix_lookup = Arc::new(prefix_lookup);
let num_threads = num_cpus::get();
let chunk_size = num_keys / num_threads + 1;
let mut handles = vec![];
for _ in 0..num_threads {
let rx_clone = Arc::clone(&rx);
let prefix_lookup_clone = Arc::clone(&prefix_lookup);
let bar_clone = bar.clone();
let handle = task::spawn(async move {
process_snapshot_chunk(rx_clone, prefix_lookup_clone, chunk_size, bar_clone).await
});
handles.push(handle);
}
let found_by_pallet = merge_partial_results(handles).await?;
bar.finish();
println!();
print_results(&found_by_pallet, verbose, &args);
Ok(())
}
fn setup_bar(num_keys: usize) -> ProgressBar {
let bar = ProgressBar::new(num_keys as u64);
bar.set_style(ProgressStyle::default_bar().template("[{elapsed}] {bar:60.cyan/blue} {percent}% {per_sec:1}").unwrap());
bar.enable_steady_tick(Duration::from_millis(100));
bar
}
fn build_prefix_lookup(pallets: &[PalletMetadata]) -> PrefixMap {
let mut prefix_lookup = PrefixMap::new();
for pallet in pallets {
let pallet_hash = twox_128(pallet.name().as_bytes());
prefix_lookup.insert(pallet_hash.into(), (pallet.name().into(), None));
if let Some(storage) = pallet.storage() {
for entry in storage.entries() {
let entry_hash = twox_128(entry.name().as_bytes());
let full_hash = [pallet_hash, entry_hash].concat();
prefix_lookup.insert(full_hash.into(), (pallet.name().into(), Some(entry.clone())));
}
}
}
prefix_lookup
}
async fn process_snapshot_chunk(
rx: Arc<Mutex<Receiver<(Vec<u8>, (Vec<u8>, i32))>>>,
prefix_lookup: Arc<PrefixMap>,
chunk_size: usize,
bar: ProgressBar,
) -> Map<String, PalletInfo> {
let mut found_by_pallet = Map::<String, PalletInfo>::new();
let unknown = ansi_term::Color::Yellow.paint("Unknown").to_string();
let mut processed = 0;
while processed < chunk_size {
let item = {
let mut rx_guard = rx.lock().unwrap();
rx_guard.try_recv()
};
match item {
Ok((key, (value, _ref_count))) => {
let cat = categorize_prefix(&key, &prefix_lookup);
match cat {
CategorizedKey::Item(pallet, item) => {
let pallet_info = found_by_pallet.entry(pallet.clone()).or_insert(PalletInfo {
name: pallet.clone(),
size: 0,
items: Map::new(),
});
let item_info = pallet_info.items.entry(item.name().to_string()).or_insert(ItemInfo {
name: item.name().to_string(),
key_len: 0,
value_len: 0,
num_entries: 0,
});
item_info.key_len += key.len();
item_info.value_len += value.len();
item_info.num_entries += 1;
pallet_info.size += key.len() + value.len();
},
CategorizedKey::Pallet(pallet) => {
let pallet_info = found_by_pallet.entry(pallet.clone()).or_insert(PalletInfo {
name: pallet.clone(),
size: 0,
items: Map::new(),
});
let item_info = pallet_info.items.entry(unknown.to_string()).or_insert(ItemInfo {
name: unknown.to_string(),
key_len: 0,
value_len: 0,
num_entries: 0,
});
item_info.key_len += key.len();
item_info.value_len += value.len();
item_info.num_entries += 1;
pallet_info.size += key.len() + value.len();
},
CategorizedKey::Unknown => {
let pallet_info = found_by_pallet.entry(unknown.to_string()).or_insert(PalletInfo {
name: unknown.to_string(),
size: 0,
items: Map::new(),
});
let item_info = pallet_info.items.entry(unknown.to_string()).or_insert(ItemInfo {
name: unknown.to_string(),
key_len: 0,
value_len: 0,
num_entries: 0,
});
item_info.key_len += key.len();
item_info.value_len += value.len();
item_info.num_entries += 1;
pallet_info.size += key.len() + value.len();
},
}
processed += 1;
bar.inc(1);
},
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => {
tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
},
Err(tokio::sync::mpsc::error::TryRecvError::Disconnected) => break,
}
}
found_by_pallet
}
async fn merge_partial_results(handles: Vec<JoinHandle<Map<String, PalletInfo>>>) -> Result<Map<String, PalletInfo>> {
let mut found_by_pallet = Map::<String, PalletInfo>::new();
for handle in handles {
let partial_result = handle.await?;
for (pallet, mut pallet_info) in partial_result {
found_by_pallet
.entry(pallet)
.and_modify(|existing| {
existing.size += pallet_info.size;
for (item_name, item_info) in pallet_info.items.iter_mut() {
existing.items
.entry(item_name.clone())
.and_modify(|existing_item| {
existing_item.key_len += item_info.key_len;
existing_item.value_len += item_info.value_len;
existing_item.num_entries += item_info.num_entries;
})
.or_insert_with(|| item_info.clone());
}
})
.or_insert(pallet_info);
}
}
Ok(found_by_pallet)
}
type PrefixMap = Map<Vec<u8>, (String, Option<StorageEntryMetadata>)>;
#[derive(Default)]
struct NetworkInfo {
size: usize,
num_keys: usize,
key_size: usize,
num_values: usize,
value_size: usize,
}
/// Storage size information of a pallet.
struct PalletInfo {
/// Name of the pallet.
name: String,
size: usize,
/// The storage items of the pallet.
items: Map<String, ItemInfo>,
}
/// Storage size information of a storage item inside a pallet.
#[derive(Clone)]
struct ItemInfo {
name: String,
key_len: usize,
value_len: usize,
num_entries: usize,
}
enum CategorizedKey {
/// A key that belongs to a storage item inside a pallet.
Item(String, StorageEntryMetadata),
/// A key that belongs to a pallet but an unknown storage item.
Pallet(String),
/// A key that does not belong to any known pallet.
Unknown,
}
impl From<(String, Option<StorageEntryMetadata>)> for CategorizedKey {
fn from((pallet, storage): (String, Option<StorageEntryMetadata>)) -> Self {
if let Some(storage) = storage {
CategorizedKey::Item(pallet, storage)
} else {
CategorizedKey::Pallet(pallet)
}
}
}
fn print_results(found_by_pallet: &Map<String, PalletInfo>, verbose: bool, args: &Args) {
let pallet_infos = found_by_pallet
.values()
.sorted_by(|a, b| b.size.cmp(&a.size))
.collect::<Vec<_>>();
let network_info = pallet_infos.iter().fold(NetworkInfo::default(), |acc, p| {
let key_size = p.items.values().map(|i| i.key_len).sum::<usize>();
let value_size = p.items.values().map(|i| i.value_len).sum::<usize>();
let num_keys = p.items.values().map(|i| i.num_entries).sum::<usize>();
NetworkInfo {
size: acc.size + p.size,
num_keys: acc.num_keys + num_keys,
key_size: acc.key_size + key_size,
num_values: acc.num_values + num_keys,
value_size: acc.value_size + value_size,
}
});
let suffix = if verbose {
format!(
" ({} keys, key: {}, value: {})",
network_info.num_keys,
fmt_bytes(network_info.key_size, false),
fmt_bytes(network_info.value_size, false)
)
} else {
"".into()
};
let mut pretty_tree = Tree::new(format!("{} {}{suffix}", fmt_bytes(network_info.size, true), args.network));
// Print stats about how many keys per pallet and item
for (_p, pallet) in pallet_infos.iter().enumerate() {
if args
.pallet
.as_ref()
.map_or(false, |p| p.to_lowercase() != pallet.name.to_lowercase())
{
continue;
}
let suffix = if verbose {
let total_keys = pallet.items.values().map(|i| i.num_entries).sum::<usize>();
let key_size = pallet.items.values().map(|i| i.key_len).sum::<usize>();
let value_size = pallet.items.values().map(|i| i.value_len).sum::<usize>();
format!(
" ({} keys, key: {}, value: {})",
total_keys,
fmt_bytes(key_size, false),
fmt_bytes(value_size, false)
)
} else {
"".into()
};
let mut pallet_node =
Tree::new(format!("{} {}{}", fmt_bytes(pallet.size, true), pallet.name, suffix));
for (_e, (_, item)) in pallet
.items
.iter()
.sorted_by_key(|(_, i)| i.key_len + i.value_len)
.rev()
.enumerate()
{
let suffix = if verbose {
format!(
" ({} keys, key: {}, value: {})",
item.num_entries,
fmt_bytes(item.key_len, false),
fmt_bytes(item.value_len, false)
)
} else {
"".into()
};
let item_node = format!("{} {}{}", fmt_bytes(item.value_len + item.key_len, true), item.name, suffix);
pallet_node.push(item_node);
}
pretty_tree.push(pallet_node);
}
println!("{}", pretty_tree);
}
fn fmt_bytes(number: usize, pad_left: bool) -> String {
let (scaled, suffix) = match number {
n if n >= 1_000_000_000 => (number as f64 / 1_000_000_000.0, "G"),
n if n >= 1_000_000 => (number as f64 / 1_000_000.0, "M"),
n if n >= 1_000 => (number as f64 / 1_000.0, "K"),
_ => (number as f64, ""),
};
let formatted = if scaled < 10.0 {
format!("{:.1} {}", scaled, suffix)
} else {
format!("{:.0} {}", scaled, suffix)
};
if pad_left {
format!("{:>3}", formatted)
} else {
formatted
}
}
fn categorize_prefix(key: &[u8], lookup: &PrefixMap) -> CategorizedKey {
if key.len() >= 32 {
let prefix = &key[0..32];
if let Some((pallet, storage)) = lookup.get(prefix) {
return (pallet.clone(), storage.clone()).into();
}
}
if key.len() >= 16 {
let prefix = &key[0..16];
if let Some((pallet, storage)) = lookup.get(prefix) {
return (pallet.clone(), storage.clone()).into();
}
}
CategorizedKey::Unknown
}
async fn get_metadata(path: &str, url: &str) -> Result<Metadata> {
// Check if metadata.json exists
if let Ok(file) = File::open(path) {
let bytes = file.bytes().map(|b| b.unwrap()).collect::<Vec<u8>>();
let meta = Metadata::decode(&mut bytes.as_slice())?;
log::info!("Metadata loaded from file");
return Ok(meta);
}
let cl = subxt::OnlineClient::<subxt::SubstrateConfig>::from_url(url).await?;
let meta = cl.metadata();
// Write meta to file
let mut file = File::create(path)?;
file.write_all(&meta.encode())?;
log::info!("Metadata written to file");
Ok(meta)
}
/// Load a try-runtime-cli snapshot from a path.
///
/// Returns the total number of keys in the snapshot and a channel that can be used to read exactly
/// that many Key-Value pairs.
fn load_snapshot(path: &str) -> Result<(usize, Receiver<(Vec<u8>, (Vec<u8>, i32))>)> {
log::info!("Loading snapshot from file");
let file = File::open(path)
.map_err(|e| anyhow!("Failed to load snapshot file from {}: {}", path, e))?;
let mut input = parity_scale_codec::IoReader(file);
let snapshot_version = Compact::<u16>::decode(&mut input)?;
if snapshot_version.0 != 4 {
log::warn!("Snapshot version is not 4 but {}", snapshot_version.0);
}
let state_version: u8 = u8::decode(&mut input)?;
if state_version != 1 {
log::warn!("State version is not 1 but {}", state_version);
}
let num_keys = Compact::<u32>::decode(&mut input).map(|l| l.0)?;
let (tx, rx) = channel(1024*100);
tokio::spawn(async move {
for _ in 0..num_keys {
let key = Vec::<u8>::decode(&mut input).unwrap();
let value = Vec::<u8>::decode(&mut input).unwrap();
let ref_count = i32::decode(&mut input).unwrap();
tx.send((key, (value, ref_count))).await.unwrap();
}
});
Ok((num_keys as usize, rx))
}