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
// RGB Core Library: consensus layer for RGB smart contracts.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2023 by
//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
// Copyright (C) 2019-2023 Dr Maxim Orlovsky. All rights reserved.
//
// 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::validation::Status;
use crate::{
    validation, OpFullType, OpSchema, Schema, StateSchema, SubSchema, BLANK_TRANSITION_ID,
};

impl SubSchema {
    pub fn verify(&self) -> validation::Status {
        let mut status = validation::Status::new();

        if let Some(ref root) = self.subset_of {
            status += self.verify_subschema(root);
        }

        // Validate internal schema consistency
        status += self.verify_consistency();

        status
    }

    fn verify_consistency(&self) -> validation::Status {
        let mut status = validation::Status::new();

        status += self.verify_operation(OpFullType::Genesis, &self.genesis);
        for (type_id, schema) in &self.transitions {
            status += self.verify_operation(OpFullType::StateTransition(*type_id), schema);
        }
        for (type_id, schema) in &self.extensions {
            status += self.verify_operation(OpFullType::StateExtension(*type_id), schema);
        }
        // Check that the schema doesn't contain reserved type ids
        if self.transitions.contains_key(&BLANK_TRANSITION_ID) {
            status.add_failure(validation::Failure::SchemaBlankTransitionRedefined);
        }

        for (type_id, schema) in &self.global_types {
            if !self.type_system.contains_key(&schema.sem_id) {
                status.add_failure(validation::Failure::SchemaGlobalSemIdUnknown(
                    *type_id,
                    schema.sem_id,
                ));
            }
        }

        for (type_id, schema) in &self.owned_types {
            if let StateSchema::Structured(sem_id) = schema {
                if !self.type_system.contains_key(sem_id) {
                    status.add_failure(validation::Failure::SchemaOwnedSemIdUnknown(
                        *type_id, *sem_id,
                    ));
                }
            }
        }

        status
    }

    fn verify_operation(&self, op_type: OpFullType, schema: &impl OpSchema) -> Status {
        let mut status = validation::Status::new();

        if !self.type_system.contains_key(&schema.metadata()) {
            status.add_failure(validation::Failure::SchemaOpMetaSemIdUnknown(
                op_type,
                schema.metadata(),
            ));
        }
        if matches!(schema.inputs(), Some(inputs) if inputs.is_empty()) {
            status.add_failure(validation::Failure::SchemaOpEmptyInputs(op_type));
        }
        if matches!(schema.redeems(), Some(inputs) if inputs.is_empty()) {
            status.add_failure(validation::Failure::SchemaOpEmptyInputs(op_type));
        }
        for type_id in schema.globals().keys() {
            if !self.global_types.contains_key(type_id) {
                status
                    .add_failure(validation::Failure::SchemaOpGlobalTypeUnknown(op_type, *type_id));
            }
        }
        for type_id in schema.assignments().keys() {
            if !self.owned_types.contains_key(type_id) {
                status.add_failure(validation::Failure::SchemaOpAssignmentTypeUnknown(
                    op_type, *type_id,
                ));
            }
        }
        for type_id in schema.valencies() {
            if !self.valency_types.contains(type_id) {
                status.add_failure(validation::Failure::SchemaOpValencyTypeUnknown(
                    op_type, *type_id,
                ));
            }
        }

        status
    }

    fn verify_subschema(&self, root: &Schema<()>) -> validation::Status {
        let mut status = validation::Status::new();

        if self.subset_of.as_ref() != Some(root) {
            panic!("SubSchema::schema_verify called with a root schema not matching subset_of");
        }

        for (global_type, data_format) in &self.global_types {
            match root.global_types.get(global_type) {
                None => status
                    .add_failure(validation::Failure::SubschemaGlobalStateMismatch(*global_type)),
                Some(root_data_format) if root_data_format != data_format => status
                    .add_failure(validation::Failure::SubschemaGlobalStateMismatch(*global_type)),
                _ => &status,
            };
        }

        for (assignments_type, state_schema) in &self.owned_types {
            match root.owned_types.get(assignments_type) {
                None => status.add_failure(validation::Failure::SubschemaAssignmentTypeMismatch(
                    *assignments_type,
                )),
                Some(root_state_schema) if root_state_schema != state_schema => status.add_failure(
                    validation::Failure::SubschemaAssignmentTypeMismatch(*assignments_type),
                ),
                _ => &status,
            };
        }

        for valencies_type in &self.valency_types {
            match root.valency_types.contains(valencies_type) {
                false => status.add_failure(validation::Failure::SubschemaValencyTypeMismatch(
                    *valencies_type,
                )),
                _ => &status,
            };
        }

        status += self
            .genesis
            .verify_subschema(OpFullType::Genesis, &root.genesis);

        for (type_id, transition_schema) in &self.transitions {
            if let Some(root_transition_schema) = root.transitions.get(type_id) {
                status += transition_schema.verify_subschema(
                    OpFullType::StateTransition(*type_id),
                    root_transition_schema,
                );
            } else {
                status.add_failure(validation::Failure::SubschemaTransitionTypeMismatch(*type_id));
            }
        }
        for (type_id, extension_schema) in &self.extensions {
            if let Some(root_extension_schema) = root.extensions.get(type_id) {
                status += extension_schema
                    .verify_subschema(OpFullType::StateExtension(*type_id), root_extension_schema);
            } else {
                status.add_failure(validation::Failure::SubschemaExtensionTypeMismatch(*type_id));
            }
        }

        status
    }
}

/// Trait used for internal schema validation against some root schema
pub(crate) trait SchemaVerify {
    type Root;
    fn verify_subschema(&self, op_type: OpFullType, root: &Self::Root) -> validation::Status;
}

impl<T> SchemaVerify for T
where T: OpSchema
{
    type Root = T;

    fn verify_subschema(&self, op_type: OpFullType, root: &Self) -> validation::Status {
        let mut status = validation::Status::new();

        if self.metadata() != root.metadata() {
            status.add_failure(validation::Failure::SubschemaOpMetaMismatch {
                op_type,
                expected: root.metadata(),
                actual: self.metadata(),
            });
        }

        for (type_id, occ) in self.globals() {
            match root.globals().get(type_id) {
                None => status.add_failure(validation::Failure::SubschemaOpGlobalStateMismatch(
                    op_type, *type_id,
                )),
                Some(root_occ) if occ != root_occ => status.add_failure(
                    validation::Failure::SubschemaOpGlobalStateMismatch(op_type, *type_id),
                ),
                _ => &status,
            };
        }

        if let Some(inputs) = self.inputs() {
            let root_inputs = root.inputs().expect("generic guarantees");
            for (type_id, occ) in inputs {
                match root_inputs.get(type_id) {
                    None => status.add_failure(validation::Failure::SubschemaOpInputMismatch(
                        op_type, *type_id,
                    )),
                    Some(root_occ) if occ != root_occ => status.add_failure(
                        validation::Failure::SubschemaOpInputMismatch(op_type, *type_id),
                    ),
                    _ => &status,
                };
            }
        }

        for (type_id, occ) in self.assignments() {
            match root.assignments().get(type_id) {
                None => status.add_failure(validation::Failure::SubschemaOpAssignmentsMismatch(
                    op_type, *type_id,
                )),
                Some(root_occ) if occ != root_occ => status.add_failure(
                    validation::Failure::SubschemaOpAssignmentsMismatch(op_type, *type_id),
                ),
                _ => &status,
            };
        }

        if let Some(redeems) = self.redeems() {
            let root_redeems = root.redeems().expect("generic guarantees");
            for type_id in redeems {
                if !root_redeems.contains(type_id) {
                    status.add_failure(validation::Failure::SubschemaOpRedeemMismatch(
                        op_type, *type_id,
                    ));
                }
            }
        }

        for type_id in self.valencies() {
            if !root.valencies().contains(type_id) {
                status.add_failure(validation::Failure::SubschemaOpValencyMismatch(
                    op_type, *type_id,
                ));
            }
        }

        status
    }
}