Skip to content

Commit

Permalink
validation: validate anchors depending on layer1 chain
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 30, 2023
1 parent 8eeae44 commit b6d754d
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 282 deletions.
5 changes: 3 additions & 2 deletions src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ pub use state::{ConfidentialState, ExposedState, StateCommitment, StateData, Sta
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[repr(u8)]
#[non_exhaustive]
pub enum AltLayer1 {
#[strict_type(dumb)]
Liquid = 1,
Abraxas = 0x10,
Prime = 0x11,
// Abraxas = 0x10,
// Prime = 0x11,
}

#[derive(Wrapper, Clone, PartialEq, Eq, Hash, Debug, From)]

Check warning on line 82 in src/contract/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/contract/mod.rs#L82

Added line #L82 was not covered by tests
Expand Down
9 changes: 8 additions & 1 deletion src/contract/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use bp::{Outpoint, Txid};
use commit_verify::{strategies, CommitEncode, Conceal};
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::LIB_NAME_RGB;
use crate::{Layer1, LIB_NAME_RGB};

pub trait ExposedSeal:
Debug
Expand Down Expand Up @@ -110,6 +110,13 @@ impl SealDefinition<GenesisSeal> {
}

impl<U: ExposedSeal> SealDefinition<U> {
pub fn layer1(self) -> Layer1 {
match self {
SealDefinition::Bitcoin(_) => Layer1::Bitcoin,
SealDefinition::Liquid(_) => Layer1::Liquid,

Check warning on line 116 in src/contract/seal.rs

View check run for this annotation

Codecov / codecov/patch

src/contract/seal.rs#L113-L116

Added lines #L113 - L116 were not covered by tests
}
}

Check warning on line 118 in src/contract/seal.rs

View check run for this annotation

Codecov / codecov/patch

src/contract/seal.rs#L118

Added line #L118 was not covered by tests

#[inline]
pub fn outpoint(self) -> Option<Outpoint> {
match self {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ pub mod vm;
pub mod stl;

pub mod prelude {
pub use bp::dbc::{Anchor, AnchorId};
pub use bp::dbc::AnchorId;
pub use contract::*;
pub use schema::*;
pub use validation::AnchoredBundle;
pub use validation::{Anchor, AnchoredBundle, Layer1};

use super::*;
pub use super::{schema, vm};
Expand Down
8 changes: 5 additions & 3 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,29 @@
pub use aluvm::stl::aluvm_stl;
pub use bp::bc::stl::bp_tx_stl;
pub use bp::stl::bp_core_stl;
use commit_verify::stl::commit_verify_stl;
use strict_types::stl::{std_stl, strict_types_stl};
use strict_types::typelib::LibBuilder;
use strict_types::{CompileError, TypeLib};

use crate::{Extension, Genesis, SubSchema, TransitionBundle, LIB_NAME_RGB};
use crate::{AnchoredBundle, Extension, Genesis, SubSchema, LIB_NAME_RGB};

/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB: &str =
"urn:ubideco:stl:28ZcP9sj9SC5CC52qLWM9YxxRRhXAJjwRqD98nZLmx2y#radar-inca-valid";
"urn:ubideco:stl:DmN8aZj1dPvAvx5JYfz22azHs5RRbECxmxWoo5LK7E5f#house-herman-poem";

fn _rgb_core_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB), tiny_bset! {
std_stl().to_dependency(),
strict_types_stl().to_dependency(),
commit_verify_stl().to_dependency(),
bp_tx_stl().to_dependency(),
bp_core_stl().to_dependency(),
aluvm_stl().to_dependency()
})
.transpile::<SubSchema>()
.transpile::<Genesis>()
.transpile::<TransitionBundle>()
.transpile::<AnchoredBundle>()
.transpile::<Extension>()
.compile()
}
Expand Down
97 changes: 97 additions & 0 deletions src/validation/anchor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 bp::dbc;
use commit_verify::mpc;
use commit_verify::mpc::{Message, ProtocolId};

use crate::{TransitionBundle, LIB_NAME_RGB};

#[derive(Clone, Eq, PartialEq, Debug)]

Check warning on line 29 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L29

Added line #L29 was not covered by tests
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 34 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L34

Added line #L34 was not covered by tests
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct AnchoredBundle {
pub anchor: Anchor,
pub bundle: TransitionBundle,
}

#[derive(Clone, Eq, PartialEq, Debug)]

Check warning on line 42 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L42

Added line #L42 was not covered by tests
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom, dumb = Self::Bitcoin(strict_dumb!()))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 47 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L47

Added line #L47 was not covered by tests
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[non_exhaustive]
pub enum Anchor {
#[strict_type(tag = 0x00)]
Bitcoin(dbc::Anchor<mpc::MerkleProof>),

#[strict_type(tag = 0x01)]
Liquid(dbc::Anchor<mpc::MerkleProof>),
}

impl Anchor {
pub fn layer1(&self) -> Layer1 {
match self {
Anchor::Bitcoin(_) => Layer1::Bitcoin,
Anchor::Liquid(_) => Layer1::Liquid,

Check warning on line 63 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L60-L63

Added lines #L60 - L63 were not covered by tests
}
}

Check warning on line 65 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L65

Added line #L65 was not covered by tests

/// Verifies that the anchor commits to the given message under the given
/// protocol.
pub fn convolve(
&self,
protocol_id: impl Into<ProtocolId>,
message: Message,
) -> Result<mpc::Commitment, mpc::InvalidProof> {
match self {
Anchor::Bitcoin(anchor) | Anchor::Liquid(anchor) => {
anchor.mpc_proof.convolve(protocol_id.into(), message)
}
}
}

Check warning on line 79 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L69-L79

Added lines #L69 - L79 were not covered by tests
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]

Check warning on line 82 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L82

Added line #L82 was not covered by tests
#[display(lowercase)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]

Check warning on line 84 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L84

Added line #L84 was not covered by tests
#[strict_type(lib = LIB_NAME_RGB, tags = repr, into_u8, try_from_u8)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 88 in src/validation/anchor.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/anchor.rs#L88

Added line #L88 was not covered by tests
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[repr(u8)]
#[non_exhaustive]
pub enum Layer1 {
#[strict_type(dumb)]
Bitcoin = 0,
Liquid = 1,
}
19 changes: 2 additions & 17 deletions src/validation/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,11 @@
use std::collections::BTreeSet;

use commit_verify::mpc;

use crate::{
Anchor, BundleId, Extension, Genesis, OpId, OpRef, SecretSeal, SubSchema, Transition,
TransitionBundle, LIB_NAME_RGB,
AnchoredBundle, BundleId, Extension, Genesis, OpId, OpRef, SecretSeal, SubSchema, Transition,
TransitionBundle,
};

#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct AnchoredBundle {
pub anchor: Anchor<mpc::MerkleProof>,
pub bundle: TransitionBundle,
}

/// Trait defining common data access API for all storage-related RGB structures
///
/// # Verification
Expand Down
4 changes: 3 additions & 1 deletion src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ mod model;
mod state;
mod validator;
mod consignment;
mod anchor;
mod status;

pub use consignment::{AnchoredBundle, ConsignmentApi};
pub use anchor::{Anchor, AnchoredBundle, Layer1};
pub use consignment::ConsignmentApi;
pub(crate) use model::OpInfo;
pub use script::VirtualMachine;
pub use status::{Failure, Info, Status, Validity, Warning};
Expand Down
6 changes: 4 additions & 2 deletions src/validation/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use strict_types::SemId;
use crate::contract::Opout;
use crate::schema::{self, SchemaId};
use crate::{
AssignmentType, BundleId, OccurrencesMismatch, OpFullType, OpId, SecretSeal, StateType,
AssignmentType, BundleId, Layer1, OccurrencesMismatch, OpFullType, OpId, SecretSeal, StateType,
};

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Display)]
Expand Down Expand Up @@ -298,7 +298,7 @@ pub enum Failure {
/// transition {0} is not anchored.
NotAnchored(OpId),
/// anchor for transition {0} doesn't commit to the actual transition data.
NotInAnchor(OpId, Txid),
NotInAnchor(OpId),
/// transition {opid} references state type {state_type} absent in the
/// outputs of previous state transition {prev_id}.
NoPrevState {
Expand All @@ -316,6 +316,8 @@ pub enum Failure {
MpcInvalid(OpId, Txid),
/// witness transaction {0} is not known to the transaction resolver.
SealNoWitnessTx(Txid),
/// witness layer 1 {anchor} doesn't match seal definition {seal}.
SealWitnessLayer1Mismatch { seal: Layer1, anchor: Layer1 },
/// transition {0} doesn't close seal with the witness transaction {1}.
/// Details: {2}
SealInvalid(OpId, Txid, seals::txout::VerifyError),
Expand Down
Loading

0 comments on commit b6d754d

Please sign in to comment.