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 299 300 301 302 303 304 305 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 336 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
// 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 core::iter::FromIterator;
use core::ops::AddAssign;
use std::fmt::{self, Display, Formatter};
use bp::dbc::anchor;
use bp::{seals, Txid};
use strict_types::SemId;
use crate::contract::Opout;
use crate::schema::{self, SchemaId};
use crate::{
    AssignmentType, BundleId, OccurrencesMismatch, OpFullType, OpId, SecretSeal, StateType,
};
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Display)]
#[repr(u8)]
pub enum Validity {
    #[display("is valid")]
    Valid,
    #[display("has non-mined terminal(s)")]
    UnminedTerminals,
    #[display("contains unknown witness transactions")]
    UnresolvedTransactions,
    #[display("is NOT valid")]
    Invalid,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Status {
    pub unresolved_txids: Vec<Txid>,
    pub unmined_terminals: Vec<Txid>,
    pub failures: Vec<Failure>,
    pub warnings: Vec<Warning>,
    pub info: Vec<Info>,
}
impl Display for Status {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        writeln!(f, "Consignment {}", self.validity())?;
        if !self.unresolved_txids.is_empty() {
            f.write_str("Unknown witness transactions:\n")?;
            for txid in &self.unresolved_txids {
                writeln!(f, "- {txid}")?;
            }
        }
        if !self.unmined_terminals.is_empty() {
            f.write_str("Non-mined terminals:\n")?;
            for txid in &self.unmined_terminals {
                writeln!(f, "- {txid}")?;
            }
        }
        if !self.failures.is_empty() {
            f.write_str("Validation failures:\n")?;
            for fail in &self.failures {
                writeln!(f, "- {fail}")?;
            }
        }
        if !self.warnings.is_empty() {
            f.write_str("Validation warnings:\n")?;
            for warn in &self.warnings {
                writeln!(f, "- {warn}")?;
            }
        }
        if !self.info.is_empty() {
            f.write_str("Validation info:\n")?;
            for info in &self.info {
                writeln!(f, "- {info}")?;
            }
        }
        Ok(())
    }
}
impl AddAssign for Status {
    fn add_assign(&mut self, rhs: Self) {
        self.unresolved_txids.extend(rhs.unresolved_txids);
        self.unmined_terminals.extend(rhs.unmined_terminals);
        self.failures.extend(rhs.failures);
        self.warnings.extend(rhs.warnings);
        self.info.extend(rhs.info);
    }
}
impl Status {
    pub fn from_error(v: Failure) -> Self {
        Status {
            unresolved_txids: vec![],
            unmined_terminals: vec![],
            failures: vec![v],
            warnings: vec![],
            info: vec![],
        }
    }
}
impl FromIterator<Failure> for Status {
    fn from_iter<T: IntoIterator<Item = Failure>>(iter: T) -> Self {
        Self {
            failures: iter.into_iter().collect(),
            ..Self::default()
        }
    }
}
impl Status {
    pub fn new() -> Self { Self::default() }
    pub fn with_failure(failure: impl Into<Failure>) -> Self {
        Self {
            failures: vec![failure.into()],
            ..Self::default()
        }
    }
    pub fn add_failure(&mut self, failure: impl Into<Failure>) -> &Self {
        self.failures.push(failure.into());
        self
    }
    pub fn add_warning(&mut self, warning: impl Into<Warning>) -> &Self {
        self.warnings.push(warning.into());
        self
    }
    pub fn add_info(&mut self, info: impl Into<Info>) -> &Self {
        self.info.push(info.into());
        self
    }
    pub fn validity(&self) -> Validity {
        if self.failures.is_empty() {
            if self.unmined_terminals.is_empty() {
                Validity::Valid
            } else {
                Validity::UnminedTerminals
            }
        } else if self.unresolved_txids.is_empty() {
            Validity::Invalid
        } else {
            Validity::UnresolvedTransactions
        }
    }
}
#[derive(Clone, PartialEq, Eq, Debug, Display, From)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[display(doc_comments)]
pub enum Failure {
    /// schema {actual} provided for the consignment validation doesn't match
    /// schema {expected} used by the contract. This means that the consignment
    /// is invalid.
    SchemaMismatch {
        /// Expected schema id required by the contracts genesis.
        expected: SchemaId,
        /// Actual schema id provided by the consignment.
        actual: SchemaId,
    },
    /// schema uses reserved type for the blank state transition.
    SchemaBlankTransitionRedefined,
    /// schema global state #{0} uses semantic data type absent in type library
    /// ({1}).
    SchemaGlobalSemIdUnknown(schema::GlobalStateType, SemId),
    /// schema owned state #{0} uses semantic data type absent in type library
    /// ({1}).
    SchemaOwnedSemIdUnknown(schema::AssignmentType, SemId),
    /// schema metadata in {0} uses semantic data type absent in type library
    /// ({1}).
    SchemaOpMetaSemIdUnknown(OpFullType, SemId),
    /// schema for {0} has zero inputs.
    SchemaOpEmptyInputs(OpFullType),
    /// schema for {0} references undeclared global state type {1}.
    SchemaOpGlobalTypeUnknown(OpFullType, schema::GlobalStateType),
    /// schema for {0} references undeclared owned state type {1}.
    SchemaOpAssignmentTypeUnknown(OpFullType, schema::AssignmentType),
    /// schema for {0} references undeclared valency type {1}.
    SchemaOpValencyTypeUnknown(OpFullType, schema::ValencyType),
    /// invalid schema - no match with root schema requirements for global state
    /// type #{0}.
    SubschemaGlobalStateMismatch(schema::GlobalStateType),
    /// invalid schema - no match with root schema requirements for assignment
    /// type #{0}.
    SubschemaAssignmentTypeMismatch(schema::AssignmentType),
    /// invalid schema - no match with root schema requirements for valency
    /// type #{0}.
    SubschemaValencyTypeMismatch(schema::ValencyType),
    /// invalid schema - no match with root schema requirements for transition
    /// type #{0}.
    SubschemaTransitionTypeMismatch(schema::TransitionType),
    /// invalid schema - no match with root schema requirements for extension
    /// type #{0}.
    SubschemaExtensionTypeMismatch(schema::ExtensionType),
    /// invalid schema - no match with root schema requirements for metadata
    /// type (required {expected}, found {actual}).
    SubschemaOpMetaMismatch {
        op_type: OpFullType,
        expected: SemId,
        actual: SemId,
    },
    /// invalid schema - no match with root schema requirements for global state
    /// type #{1} used in {0}.
    SubschemaOpGlobalStateMismatch(OpFullType, schema::GlobalStateType),
    /// invalid schema - no match with root schema requirements for input
    /// type #{1} used in {0}.
    SubschemaOpInputMismatch(OpFullType, schema::AssignmentType),
    /// invalid schema - no match with root schema requirements for redeem
    /// type #{1} used in {0}.
    SubschemaOpRedeemMismatch(OpFullType, schema::ValencyType),
    /// invalid schema - no match with root schema requirements for assignment
    /// type #{1} used in {0}.
    SubschemaOpAssignmentsMismatch(OpFullType, schema::AssignmentType),
    /// invalid schema - no match with root schema requirements for valency
    /// type #{1} used in {0}.
    SubschemaOpValencyMismatch(OpFullType, schema::ValencyType),
    /// operation {0} uses invalid state extension type {1}.
    SchemaUnknownExtensionType(OpId, schema::ExtensionType),
    /// operation {0} uses invalid state transition type {1}.
    SchemaUnknownTransitionType(OpId, schema::TransitionType),
    /// operation {0} uses invalid global state type {1}.
    SchemaUnknownGlobalStateType(OpId, schema::GlobalStateType),
    /// operation {0} uses invalid assignment type {1}.
    SchemaUnknownAssignmentType(OpId, schema::AssignmentType),
    /// operation {0} uses invalid valency type {1}.
    SchemaUnknownValencyType(OpId, schema::ValencyType),
    /// invalid number of global state entries of type {1} in operation {0} -
    /// {2}
    SchemaGlobalStateOccurrences(OpId, schema::GlobalStateType, OccurrencesMismatch),
    /// number of global state entries of type {1} in operation {0} exceeds
    /// schema-defined maximum for that global state type ({2} vs {3}).
    SchemaGlobalStateLimit(OpId, schema::GlobalStateType, u16, u16),
    /// invalid metadata in operation {0} not matching semantic type id {1}.
    SchemaInvalidMetadata(OpId, SemId),
    /// invalid global state value in operation {0}, state type #{1} which does
    /// not match semantic type id {2}.
    SchemaInvalidGlobalValue(OpId, schema::GlobalStateType, SemId),
    /// invalid owned state value in operation {0}, state type #{1} which does
    /// not match semantic type id {2}.
    SchemaInvalidOwnedValue(OpId, schema::AssignmentType, SemId),
    /// invalid number of input entries of type {1} in operation {0} - {2}  
    SchemaInputOccurrences(OpId, schema::AssignmentType, OccurrencesMismatch),
    /// invalid number of assignment entries of type {1} in operation {0} - {2}
    SchemaAssignmentOccurrences(OpId, schema::AssignmentType, OccurrencesMismatch),
    // Consignment consistency errors
    /// operation {0} is absent from the consignment.
    OperationAbsent(OpId),
    /// state transition {0} is absent from the consignment.
    TransitionAbsent(OpId),
    /// bundle with id {0} is invalid.
    BundleInvalid(BundleId),
    // Errors checking seal closing
    /// transition {0} is not anchored.
    NotAnchored(OpId),
    /// anchor for transition {0} doesn't commit to the actual transition data.
    NotInAnchor(OpId, Txid),
    /// transition {opid} references state type {state_type} absent in the
    /// outputs of previous state transition {prev_id}.
    NoPrevState {
        opid: OpId,
        prev_id: OpId,
        state_type: schema::AssignmentType,
    },
    /// transition {0} references non-existing previous output {1}.
    NoPrevOut(OpId, Opout),
    /// seal defined in the history as a part of operation output {0} is
    /// confidential and can't be validated.
    ConfidentialSeal(Opout),
    /// transition {0} is not a part of multi-protocol commitment for witness
    /// {1}; anchor is invalid.
    MpcInvalid(OpId, Txid),
    /// witness transaction {0} is not known to the transaction resolver.
    SealNoWitnessTx(Txid),
    /// transition {0} doesn't close seal with the witness transaction {1}.
    /// Details: {2}
    SealInvalid(OpId, Txid, seals::txout::VerifyError),
    /// transition {0} is not properly anchored to the witness transaction {1}.
    /// Details: {2}
    AnchorInvalid(OpId, Txid, anchor::VerifyError),
    // State extensions errors
    /// valency {valency} redeemed by state extension {opid} references
    /// non-existing operation {prev_id}
    ValencyNoParent {
        opid: OpId,
        prev_id: OpId,
        valency: schema::ValencyType,
    },
    /// state extension {opid} references valency {valency} absent in the parent
    /// {prev_id}.
    NoPrevValency {
        opid: OpId,
        prev_id: OpId,
        valency: schema::ValencyType,
    },
    // Data check errors
    /// state in {opid}/{state_type} is of {found} type, while schema requires
    /// it to be {expected}.
    StateTypeMismatch {
        opid: OpId,
        state_type: schema::AssignmentType,
        expected: StateType,
        found: StateType,
    },
    /// state in {opid}/{state_type} is of {found} type, while schema requires
    /// it to be {expected}.
    MediaTypeMismatch {
        opid: OpId,
        state_type: schema::AssignmentType,
        expected: schema::MediaType,
        found: schema::MediaType,
    },
    /// state in {opid}/{state_type} is of {found} type, while schema requires
    /// it to be {expected}.
    FungibleTypeMismatch {
        opid: OpId,
        state_type: schema::AssignmentType,
        expected: schema::FungibleType,
        found: schema::FungibleType,
    },
    /// invalid bulletproofs in {0}:{1}: {2}
    BulletproofsInvalid(OpId, u16, String),
    /// operation {0} is invalid: {1}
    ScriptFailure(OpId, String),
    /// Custom error by external services on top of RGB Core.
    #[display(inner)]
    Custom(String),
}
#[derive(Clone, PartialEq, Eq, Debug, Display, From)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[display(doc_comments)]
pub enum Warning {
    /// terminal seal {1} referencing operation {0} is not present in operation
    /// assignments.
    TerminalSealAbsent(OpId, SecretSeal),
    /// operation {0} present in the consignment is excessive and not a part of
    /// the validated contract history.
    ExcessiveOperation(OpId),
    /// terminal witness transaction {0} is not yet mined.
    TerminalWitnessNotMined(Txid),
    /// Custom warning by external services on top of RGB Core.
    #[display(inner)]
    Custom(String),
}
#[derive(Clone, PartialEq, Eq, Debug, Display, From)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[display(doc_comments)]
pub enum Info {
    /// operation {0} contains state in assignment {1} which is confidential and
    /// thus was not validated.
    UncheckableConfidentialState(OpId, AssignmentType),
    /// Custom info by external services on top of RGB Core.
    #[display(inner)]
    Custom(String),
}