frame_remote_externalities/
config.rs1use codec::{Compact, Decode, Encode};
21use sp_runtime::{traits::Block as BlockT, StateVersion};
22use std::{
23 fs,
24 path::{Path, PathBuf},
25};
26
27use crate::Result;
28
29pub(crate) const DEFAULT_WS_ENDPOINT: &str = "wss://try-runtime.polkadot.io:443";
30pub(crate) type SnapshotVersion = Compact<u16>;
31pub(crate) const SNAPSHOT_VERSION: SnapshotVersion = Compact(4);
32
33#[derive(Clone)]
35pub enum Mode<H> {
36 Online(OnlineConfig<H>),
38 Offline(OfflineConfig),
40 OfflineOrElseOnline(OfflineConfig, OnlineConfig<H>),
42}
43
44impl<H> Default for Mode<H> {
45 fn default() -> Self {
46 Mode::Online(OnlineConfig::default())
47 }
48}
49
50#[derive(Clone)]
54pub struct OfflineConfig {
55 pub state_snapshot: SnapshotConfig,
57}
58
59#[derive(Clone)]
63pub struct OnlineConfig<H> {
64 pub at: Option<H>,
67 pub state_snapshot: Option<SnapshotConfig>,
69 pub pallets: Vec<String>,
71 pub transport_uris: Vec<String>,
73 pub child_trie: bool,
75 pub hashed_prefixes: Vec<Vec<u8>>,
78 pub hashed_keys: Vec<Vec<u8>>,
80 pub disable_root_check: bool,
85}
86
87impl<H: Clone> OnlineConfig<H> {
88 pub(crate) fn at_expected(&self) -> H {
89 self.at.clone().expect("block at must be initialized; qed")
90 }
91}
92
93impl<H> Default for OnlineConfig<H> {
94 fn default() -> Self {
95 Self {
96 transport_uris: vec![DEFAULT_WS_ENDPOINT.to_owned()],
97 child_trie: true,
98 at: None,
99 state_snapshot: None,
100 pallets: Default::default(),
101 hashed_keys: Default::default(),
102 hashed_prefixes: Default::default(),
103 disable_root_check: false,
104 }
105 }
106}
107
108impl<H> From<String> for OnlineConfig<H> {
109 fn from(uri: String) -> Self {
110 Self { transport_uris: vec![uri], ..Default::default() }
111 }
112}
113
114#[derive(Clone)]
116pub struct SnapshotConfig {
117 pub path: PathBuf,
119}
120
121impl SnapshotConfig {
122 pub fn new<P: Into<PathBuf>>(path: P) -> Self {
123 Self { path: path.into() }
124 }
125}
126
127impl From<String> for SnapshotConfig {
128 fn from(s: String) -> Self {
129 Self::new(s)
130 }
131}
132
133impl Default for SnapshotConfig {
134 fn default() -> Self {
135 Self { path: Path::new("SNAPSHOT").into() }
136 }
137}
138
139#[derive(Decode, Encode)]
141pub(crate) struct Snapshot<B: BlockT> {
142 snapshot_version: SnapshotVersion,
143 pub(crate) state_version: StateVersion,
144 pub(crate) raw_storage: Vec<(Vec<u8>, (Vec<u8>, i32))>,
145 pub(crate) storage_root: B::Hash,
146 pub(crate) header: B::Header,
147}
148
149impl<B: BlockT> Snapshot<B> {
150 pub(crate) fn new(
151 state_version: StateVersion,
152 raw_storage: Vec<(Vec<u8>, (Vec<u8>, i32))>,
153 storage_root: B::Hash,
154 header: B::Header,
155 ) -> Self {
156 Self {
157 snapshot_version: SNAPSHOT_VERSION,
158 state_version,
159 raw_storage,
160 storage_root,
161 header,
162 }
163 }
164
165 pub(crate) fn load(path: &PathBuf) -> Result<Snapshot<B>> {
166 let bytes = fs::read(path).map_err(|_| "fs::read failed.")?;
167 let snapshot_version = SnapshotVersion::decode(&mut &*bytes)
170 .map_err(|_| "Failed to decode snapshot version")?;
171
172 if snapshot_version != SNAPSHOT_VERSION {
173 return Err("Unsupported snapshot version detected. Please create a new snapshot.");
174 }
175
176 Decode::decode(&mut &*bytes).map_err(|_| "Decode failed")
177 }
178}