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
// Copyright (C) Use Ink (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::layout::{
Layout,
MetadataError,
StructLayout,
};
use ink_prelude::collections::HashMap;
use ink_primitives::Key;
use scale_info::form::MetaForm;
/// It validates that the storage layout doesn't have conflicting storage keys.
/// Otherwise an error with a description of the conflict is returned.
pub struct ValidateLayout {
first_entry: HashMap<Key, String>,
name_stack: Vec<String>,
}
impl ValidateLayout {
/// Validates the storage layout.
pub fn validate(layout: &Layout<MetaForm>) -> Result<(), MetadataError> {
let mut validator = Self {
first_entry: Default::default(),
name_stack: Default::default(),
};
validator.recursive_validate(layout)
}
fn recursive_validate(
&mut self,
layout: &Layout<MetaForm>,
) -> Result<(), MetadataError> {
match layout {
Layout::Root(root) => {
self.check_key(root.root_key.key())?;
self.recursive_validate(root.layout())
}
Layout::Hash(hash) => self.recursive_validate(hash.layout()),
Layout::Array(array) => self.recursive_validate(array.layout()),
Layout::Struct(st) => self.check_struct_layout(st),
Layout::Enum(en) => {
// After `Enum::` we will have the struct -> `Enum::Struct`
self.name_stack.push(format!("{}::", en.name()));
for variant in en.variants().values() {
self.check_struct_layout(variant)?;
}
self.name_stack.pop().expect("stack is not empty; qed");
Ok(())
}
_ => Ok(()),
}
}
fn check_struct_layout(&mut self, st: &StructLayout) -> Result<(), MetadataError> {
self.name_stack.push(st.name().to_string());
for layout in st.fields() {
let name = layout.name();
// After `Struct` we always have fields -> `Struct.field`
// After field we have `Struct` or `Enum` -> `Struct.field:Struct`
self.name_stack.push(format!(".{name}:"));
self.recursive_validate(layout.layout())?;
self.name_stack.pop().unwrap();
}
self.name_stack.pop().unwrap();
Ok(())
}
fn check_key(&mut self, key: &Key) -> Result<(), MetadataError> {
let path = self.name_stack.join("");
if let Some(prev_path) = self.first_entry.get(key) {
Err(MetadataError::Collision(prev_path.clone(), path))
} else {
self.first_entry.insert(*key, path);
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use crate::layout::{
EnumLayout,
FieldLayout,
Layout,
LeafLayout,
MetadataError,
RootLayout,
StructLayout,
ValidateLayout,
};
use ink_primitives::Key;
use std::collections::BTreeSet;
#[test]
fn valid_layout_tree_only_roots() {
// Root(0) -> Root(1) -> Root(2) -> u32
let layout = RootLayout::new_empty(
0.into(),
RootLayout::new_empty(
1.into(),
RootLayout::new_empty(2.into(), LeafLayout::from_key::<u32>(2.into())),
),
);
assert!(ValidateLayout::validate(&Layout::Root(layout)).is_ok())
}
// If any of the root key are equal it should cause an error
fn valid_big_layout_tree(
key_for_root_0: Key,
key_for_root_1: Key,
key_for_root_2: Key,
key_for_root_3: Key,
key_for_root_4: Key,
) -> Result<(), MetadataError> {
let root_0 = key_for_root_0.into();
let root_1 = key_for_root_1.into();
let root_2 = key_for_root_2.into();
let root_3 = key_for_root_3.into();
let root_4 = key_for_root_4.into();
// Below the description of the layout tree. Inside `(...)` the expected storage
// key. Root(0)
// |
// Contract(0)
// / | \
// a:Root(1) b:u32(0) c:Struct0(0)
// | / \
// Vec<u8>(1) d:u128(0) f:Root(2)
// |
// Enum(2)
// / | \
// First Second Third
// 0.Struct1 0.u8(2) 0.Root(3)
// | |
// Root(4) String
// |
// g:BTreeSet<u64>(4)
let layout = RootLayout::new_empty(
root_0,
StructLayout::new(
"Contract",
vec![
FieldLayout::new(
"a",
StructLayout::new(
"Struct0",
vec![
FieldLayout::new("d", LeafLayout::from_key::<u128>(root_0)),
FieldLayout::new(
"f",
RootLayout::new_empty(
root_2,
EnumLayout::new(
"Enum",
root_2,
vec![
(
0.into(),
StructLayout::new(
"First",
vec![FieldLayout::new(
"0",
StructLayout::new(
"Struct1",
vec![FieldLayout::new(
"g",
RootLayout::new_empty(
root_4,
LeafLayout::from_key::<
BTreeSet<u64>,
>(
root_4
),
),
)],
),
)],
),
),
(
1.into(),
StructLayout::new(
"Second",
vec![FieldLayout::new(
"0",
LeafLayout::from_key::<u8>(root_2),
)],
),
),
(
2.into(),
StructLayout::new(
"Third",
vec![FieldLayout::new(
"0",
RootLayout::new_empty(
root_3,
LeafLayout::from_key::<String>(
root_3,
),
),
)],
),
),
],
),
),
),
],
),
),
FieldLayout::new("b", LeafLayout::from_key::<u32>(root_0)),
FieldLayout::new(
"c",
RootLayout::new_empty(root_1, LeafLayout::from_key::<Vec<u8>>(root_1)),
),
],
),
);
ValidateLayout::validate(&Layout::Root(layout))
}
#[test]
fn tree_is_valid() {
assert_eq!(Ok(()), valid_big_layout_tree(0, 1, 2, 3, 4));
assert_eq!(Ok(()), valid_big_layout_tree(4, 3, 2, 1, 0));
}
#[test]
fn conflict_0_and_1() {
assert_eq!(
Err(MetadataError::Collision(
"".to_string(),
"Contract.c:".to_string()
)),
valid_big_layout_tree(0, 0, 2, 3, 4)
)
}
#[test]
fn conflict_0_and_2() {
assert_eq!(
Err(MetadataError::Collision(
"".to_string(),
"Contract.a:Struct0.f:".to_string()
)),
valid_big_layout_tree(0, 1, 0, 3, 4)
)
}
#[test]
fn conflict_0_and_3() {
assert_eq!(
Err(MetadataError::Collision(
"".to_string(),
"Contract.a:Struct0.f:Enum::Third.0:".to_string()
)),
valid_big_layout_tree(0, 1, 2, 0, 4)
)
}
#[test]
fn conflict_0_and_4() {
assert_eq!(
Err(MetadataError::Collision(
"".to_string(),
"Contract.a:Struct0.f:Enum::First.0:Struct1.g:".to_string()
)),
valid_big_layout_tree(0, 1, 2, 3, 0)
)
}
#[test]
fn conflict_3_and_4() {
assert_eq!(
Err(MetadataError::Collision(
"Contract.a:Struct0.f:Enum::First.0:Struct1.g:".to_string(),
"Contract.a:Struct0.f:Enum::Third.0:".to_string()
)),
valid_big_layout_tree(0, 1, 2, 3, 3)
)
}
}