-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
161 lines (138 loc) · 4.86 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
#![allow(dead_code)]
use std::fs::File;
use std::io::Write;
use anyhow::Result;
use chrono::prelude::*;
use codec::Decode;
use frame_support::pallet_prelude::StorageVersion;
use frame_support::storage::storage_prefix;
use serde::Serialize;
use sp_core::H256;
use subxt::rpc::types::{StorageChangeSet, StorageData};
use subxt::storage::StorageKey;
use subxt::{config::PolkadotConfig, OnlineClient};
/// Note, generate the file with subxt metadata -f bytes > metadata.scale
#[subxt::subxt(runtime_metadata_path = "./metadata.scale")]
pub mod polkadot {}
#[derive(Debug, Serialize)]
struct DBEntry {
key: StorageKey,
value: Option<StorageData>,
}
#[tokio::main]
async fn main() -> Result<()> {
let url = "ws://127.0.0.1:9944";
let client = OnlineClient::<PolkadotConfig>::from_url(url).await?;
let keys = get_keys(&client, None).await?;
let block_hash = get_blockhash(&client, 0).await?;
let change_sets = query_storage_value(&client, keys.clone(), block_hash).await?;
let json = serde_json::to_string_pretty(&change_sets).unwrap();
let mut file = File::create("change_sets.json").unwrap();
file.write_all(json.as_bytes()).unwrap();
let mut db_entries = Vec::new();
for key in keys {
let value = get_storage_value(&client, &key, None).await?;
db_entries.push(DBEntry { key, value });
}
let json = serde_json::to_string_pretty(&db_entries).unwrap();
let mut file = File::create("db.json").unwrap();
file.write_all(json.as_bytes()).unwrap();
Ok(())
}
async fn _print_block_migration(client: &OnlineClient<PolkadotConfig>) -> Result<()> {
let mut i = 1;
loop {
let block_number = get_blocknumber(&client).await? - i;
let block_hash = get_blockhash(&client, block_number).await?;
let time = get_timestamp(&client, block_hash).await?;
let version = get_contract_version(&client, Some(block_hash)).await?;
println!("{block_number} -> {time} -> {:?}", version);
if version == StorageVersion::new(8) {
break;
}
i += 1;
}
Ok(())
}
async fn get_blocknumber(client: &OnlineClient<PolkadotConfig>) -> Result<u32> {
let number_addr = polkadot::storage().system().number();
client
.storage()
.at_latest()
.await?
.fetch(&number_addr)
.await?
.ok_or_else(|| anyhow::format_err!("system::number failed"))
}
async fn get_contract_version(
client: &OnlineClient<PolkadotConfig>,
block_hash: Option<H256>,
) -> Result<StorageVersion> {
let key = storage_prefix(b"Contracts", b":__STORAGE_VERSION__:");
let StorageData(value) = get_storage_value(client, key, block_hash)
.await?
.ok_or_else(|| anyhow::format_err!("failed to get contracts storage version"))?;
StorageVersion::decode(&mut value.as_slice())
.ok()
.ok_or_else(|| anyhow::format_err!("failed to decode pallet_version"))
}
async fn get_blockhash(client: &OnlineClient<PolkadotConfig>, block_number: u32) -> Result<H256> {
let block_hash_addr = polkadot::storage().system().block_hash(block_number);
client
.storage()
.at_latest()
.await?
.fetch(&block_hash_addr)
.await?
.ok_or_else(|| anyhow::format_err!("system::block_hash failed"))
}
async fn get_timestamp(
client: &OnlineClient<PolkadotConfig>,
block_hash: H256,
) -> Result<DateTime<Local>> {
let now_addr = polkadot::storage().timestamp().now();
let now = client
.storage()
.at(block_hash)
.fetch(&now_addr)
.await?
.ok_or_else(|| anyhow::format_err!("timestamp::now failed"))?;
// format timestamp to human readable date
let now = NaiveDateTime::from_timestamp_millis(now as i64)
.and_then(|now| Local.from_local_datetime(&now).latest())
.ok_or_else(|| anyhow::format_err!("failed to convert timestamp to Date"))?;
Ok(now)
}
async fn get_keys(
client: &OnlineClient<PolkadotConfig>,
block_hash: Option<H256>,
) -> Result<Vec<StorageKey>> {
client
.rpc()
.storage_keys_paged(&[], 100, None, block_hash)
.await
.map_err(|reason| anyhow::format_err!("get_keys failed: {:?}", reason))
}
async fn get_storage_value<K: AsRef<[u8]>>(
client: &OnlineClient<PolkadotConfig>,
key: K,
block_hash: Option<H256>,
) -> Result<Option<StorageData>> {
client
.rpc()
.storage(key.as_ref(), block_hash)
.await
.map_err(|_| anyhow::format_err!("failed to get storage value"))
}
async fn query_storage_value(
client: &OnlineClient<PolkadotConfig>,
keys: Vec<StorageKey>,
block_hash: H256,
) -> Result<Vec<StorageChangeSet<H256>>> {
let keys = keys.iter().map(|k| &*k.0);
client
.rpc()
.query_storage(keys, block_hash, None)
.await
.map_err(|reason| anyhow::format_err!("get_keys failed: {:?}", reason))
}