-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathv2_get_instances_stats.rs
115 lines (112 loc) · 4.13 KB
/
v2_get_instances_stats.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
//! Print the list of all instances with a little bit of information about them.
//! Namely, address, version of the contract, size of the state, account that
//! created it, owned amount of CCD, and name of the contract.
//! For V1 instances an additional column containing the size of the serialized
//! state is added. Sizes are in bytes.
use anyhow::Context;
use clap::AppSettings;
use concordium_rust_sdk::{
smart_contracts::{engine, types::InstanceInfo},
types::hashes::BlockHash,
v2,
};
use futures::StreamExt;
use structopt::StructOpt;
#[derive(StructOpt)]
struct App {
#[structopt(
long = "node",
help = "GRPC interface of the node.",
default_value = "http://localhost:20000"
)]
endpoint: v2::Endpoint,
#[structopt(
long = "block",
help = "Hash of the block in which to query. Defaults to the last finalized block."
)]
block: Option<BlockHash>,
}
#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
let app = {
let app = App::clap().global_setting(AppSettings::ColoredHelp);
let matches = app.get_matches();
App::from_clap(&matches)
};
let mut client = v2::Client::new(app.endpoint)
.await
.context("Cannot connect.")?;
let block = app
.block
.map_or(v2::BlockIdentifier::LastFinal, v2::BlockIdentifier::Given);
let mut instances = client.get_instance_list(&block).await?;
let block = instances.block_hash;
println!("Using block {}", instances.block_hash);
let mut total_state_size: usize = 0;
while let Some(ia) = instances.response.next().await {
let ia = ia?;
let ii = client.get_instance_info(ia, &block).await?;
match ii.response {
InstanceInfo::V0 {
model,
owner,
amount,
name,
..
} => {
total_state_size += model.len();
println!(
"{}, V0, {}, {}, {}, {}",
ia,
model.len(),
owner,
amount,
String::from(name)
);
}
InstanceInfo::V1 {
owner,
amount,
name,
..
} => {
let al = client.get_instance_state(ia, &block).await?;
// reconstruct the state from the key-value pairs.
let mut state =
engine::v1::trie::PersistentState::try_from_stream(al.response).await?;
// serialize the state to a vector. This is useful if we just want to load the
// state later using the `PersistentState::deserialize` method.
let serialized_size = {
// since the state is entirely in memory any loader will do, no data needs to be
// accessed from the backing store.
let mut loader = engine::v1::trie::Loader { inner: Vec::new() };
let mut out = Vec::new();
state.serialize(&mut loader, &mut out)?;
out.len()
};
// Store the state into a buffer that allows partial loading.
// This is how the state is stored in the node so that partial updates are
// efficient.
let stored_size = {
let mut storer = engine::v1::trie::Storer {
inner: std::io::Cursor::new(Vec::new()),
};
let _root_ref = state.store_update(&mut storer)?;
storer.inner.into_inner().len()
};
total_state_size += stored_size;
println!(
"{}, V1, {}, {}, {}, {}, ({})",
ia,
stored_size,
owner,
amount,
String::from(name),
serialized_size,
)
}
}
}
println!("Total state size of all contracts is {}B", total_state_size);
Ok(())
}