tests.rs 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Copyright 2018-2020 Parity Technologies (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 super::*;
use ink_primitives::KeyPtr;

#[test]
fn layout_key_works() {
20
    let layout_key = LayoutKey::from(Key::from([0x01; 32]));
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    let json = serde_json::to_string(&layout_key).unwrap();
    assert_eq!(
        json,
        "\"0x0101010101010101010101010101010101010101010101010101010101010101\"",
    );
}

fn named_fields_struct_layout(key_ptr: &mut KeyPtr) -> Layout {
    StructLayout::new(vec![
        FieldLayout::new(
            "a",
            CellLayout::new::<i32>(LayoutKey::from(key_ptr.advance_by(1))),
        ),
        FieldLayout::new(
            "b",
            CellLayout::new::<i64>(LayoutKey::from(key_ptr.advance_by(1))),
        ),
    ])
    .into()
}

#[test]
fn named_fields_work() {
44
    let layout = named_fields_struct_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    let mut registry = Registry::new();
    let compacted = layout.into_compact(&mut registry);
    let json = serde_json::to_value(&compacted).unwrap();
    let expected = serde_json::json! {
        {
            "Struct": {
                "fields": [
                    {
                        "layout": {
                            "Cell": {
                                "key": "0x\
                                    0000000000000000\
                                    0000000000000000\
                                    0000000000000000\
                                    0000000000000000",
                                "ty": 1,
                            }
                        },
                        "name": 1,
                    },
                    {
                        "layout": {
                            "Cell": {
                                "key": "0x\
69
                                    0100000000000000\
70
71
                                    0000000000000000\
                                    0000000000000000\
72
                                    0000000000000000",
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
                                "ty": 2,
                            }
                        },
                        "name": 2,
                    }
                ]
            }
        }
    };
    assert_eq!(json, expected);
}

fn tuple_struct_layout(key_ptr: &mut KeyPtr) -> Layout {
    StructLayout::new(vec![
        FieldLayout::new(
            None,
            CellLayout::new::<i32>(LayoutKey::from(key_ptr.advance_by(1))),
        ),
        FieldLayout::new(
            None,
            CellLayout::new::<i64>(LayoutKey::from(key_ptr.advance_by(1))),
        ),
    ])
    .into()
}

#[test]
fn tuple_struct_work() {
101
    let layout = tuple_struct_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    let mut registry = Registry::new();
    let compacted = layout.into_compact(&mut registry);
    let json = serde_json::to_value(&compacted).unwrap();
    let expected = serde_json::json! {
        {
            "Struct": {
                "fields": [
                    {
                        "layout": {
                            "Cell": {
                                "key": "0x\
                                    0000000000000000\
                                    0000000000000000\
                                    0000000000000000\
                                    0000000000000000",
                                "ty": 1,
                            }
                        },
                        "name": null,
                    },
                    {
                        "layout": {
                            "Cell": {
                                "key": "0x\
126
                                    0100000000000000\
127
128
                                    0000000000000000\
                                    0000000000000000\
129
                                    0000000000000000",
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
                                "ty": 2,
                            }
                        },
                        "name": null,
                    }
                ]
            }
        }
    };
    assert_eq!(json, expected);
}

fn clike_enum_layout(key_ptr: &mut KeyPtr) -> Layout {
    EnumLayout::new(
        key_ptr.advance_by(1),
        vec![
            (Discriminant(0), StructLayout::new(vec![])),
            (Discriminant(1), StructLayout::new(vec![])),
            (Discriminant(2), StructLayout::new(vec![])),
        ],
    )
    .into()
}

#[test]
fn clike_enum_work() {
156
    let layout = clike_enum_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
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
    let mut registry = Registry::new();
    let compacted = layout.into_compact(&mut registry);
    let json = serde_json::to_value(&compacted).unwrap();
    let expected = serde_json::json! {
        {
            "Enum": {
                "dispatch_key": "0x\
                    0000000000000000\
                    0000000000000000\
                    0000000000000000\
                    0000000000000000",
                "variants": {
                    "0": {
                        "fields": [],
                    },
                    "1": {
                        "fields": [],
                    },
                    "2": {
                        "fields": [],
                    },
                }
            }
        }
    };
    assert_eq!(json, expected);
}

fn mixed_enum_layout(key_ptr: &mut KeyPtr) -> Layout {
    EnumLayout::new(
187
        *key_ptr.advance_by(1),
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
        vec![
            (Discriminant(0), StructLayout::new(vec![])),
            {
                let mut variant_key_ptr = key_ptr.clone();
                (
                    Discriminant(1),
                    StructLayout::new(vec![
                        FieldLayout::new(
                            None,
                            CellLayout::new::<i32>(LayoutKey::from(
                                variant_key_ptr.advance_by(1),
                            )),
                        ),
                        FieldLayout::new(
                            None,
                            CellLayout::new::<i64>(LayoutKey::from(
                                variant_key_ptr.advance_by(1),
                            )),
                        ),
                    ]),
                )
            },
            {
                let mut variant_key_ptr = key_ptr.clone();
                (
                    Discriminant(2),
                    StructLayout::new(vec![
                        FieldLayout::new(
                            "a",
                            CellLayout::new::<i32>(LayoutKey::from(
                                variant_key_ptr.advance_by(1),
                            )),
                        ),
                        FieldLayout::new(
                            "b",
                            CellLayout::new::<i64>(LayoutKey::from(
                                variant_key_ptr.advance_by(1),
                            )),
                        ),
                    ]),
                )
            },
        ],
    )
    .into()
}

#[test]
fn mixed_enum_work() {
237
    let layout = mixed_enum_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
    let mut registry = Registry::new();
    let compacted = layout.into_compact(&mut registry);
    let json = serde_json::to_value(&compacted).unwrap();
    let expected = serde_json::json! {
        {
            "Enum": {
                "dispatch_key": "0x\
                    0000000000000000\
                    0000000000000000\
                    0000000000000000\
                    0000000000000000",
                "variants": {
                    "0": {
                        "fields": [],
                    },
                    "1": {
                        "fields": [
                            {
                                "layout": {
                                    "Cell": {
                                        "key": "0x\
259
                                            0100000000000000\
260
261
                                            0000000000000000\
                                            0000000000000000\
262
                                            0000000000000000",
263
264
265
266
267
268
269
270
271
                                        "ty": 1,
                                    }
                                },
                                "name": null,
                            },
                            {
                                "layout": {
                                    "Cell": {
                                        "key": "0x\
272
                                            0200000000000000\
273
274
                                            0000000000000000\
                                            0000000000000000\
275
                                            0000000000000000",
276
277
278
279
280
281
282
283
284
285
286
287
288
                                        "ty": 2,
                                    }
                                },
                                "name": null,
                            }
                        ],
                    },
                    "2": {
                        "fields": [
                            {
                                "layout": {
                                    "Cell": {
                                        "key": "0x\
289
                                            0100000000000000\
290
291
                                            0000000000000000\
                                            0000000000000000\
292
                                            0000000000000000",
293
294
295
296
297
298
299
300
301
                                        "ty": 1,
                                    }
                                },
                                "name": 1,
                            },
                            {
                                "layout": {
                                    "Cell": {
                                        "key": "0x\
302
                                            0200000000000000\
303
304
                                            0000000000000000\
                                            0000000000000000\
305
                                            0000000000000000",
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
                                        "ty": 2,
                                    }
                                },
                                "name": 2,
                            }
                        ],
                    },
                }
            }
        }
    };
    assert_eq!(json, expected);
}

fn unbounded_hashing_layout(key_ptr: &mut KeyPtr) -> Layout {
    let root_key = key_ptr.advance_by(1);
    HashLayout::new(
        root_key,
        HashingStrategy::new(
            CryptoHasher::Blake2x256,
            b"ink storage hashmap".to_vec(),
            Vec::new(),
        ),
        CellLayout::new::<(i32, bool)>(LayoutKey::from(root_key)),
    )
    .into()
}

#[test]
fn unbounded_layout_works() {
336
    let layout = unbounded_hashing_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
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
    let mut registry = Registry::new();
    let compacted = layout.into_compact(&mut registry);
    let json = serde_json::to_value(&compacted).unwrap();
    let expected = serde_json::json! {
        {
            "Hash": {
                "layout": {
                    "Cell": {
                        "key": "0x\
                            0000000000000000\
                            0000000000000000\
                            0000000000000000\
                            0000000000000000",
                        "ty": 1
                    }
                },
                "offset": "0x\
                    0000000000000000\
                    0000000000000000\
                    0000000000000000\
                    0000000000000000",
                "strategy": {
                        "hasher": "Blake2x256",
                        "prefix": "0x696e6b2073746f7261676520686173686d6170",
                        "postfix": "",
                }
            }
        }
    };
    assert_eq!(json, expected);
}