Skip to content

Commit

Permalink
contract: rename StateData and StateCommitment types
Browse files Browse the repository at this point in the history
remove their strict encoding
  • Loading branch information
dr-orlovsky committed Feb 11, 2024
1 parent a7077ab commit 2308c17
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 71 deletions.
6 changes: 3 additions & 3 deletions src/contract/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use commit_verify::{CommitVerify, Conceal, StrictEncodedProtocol};
use strict_encoding::StrictEncode;

use super::{ConfidentialState, ExposedState};
use crate::{MediaType, StateCommitment, StateData, StateType, LIB_NAME_RGB};
use crate::{ConcealedState, MediaType, RevealedState, StateType, LIB_NAME_RGB};

/// Unique data attachment identifier
#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
Expand Down Expand Up @@ -110,7 +110,7 @@ impl RevealedAttach {
impl ExposedState for RevealedAttach {
type Confidential = ConcealedAttach;
fn state_type(&self) -> StateType { StateType::Attachment }
fn state_data(&self) -> StateData { StateData::Attachment(self.clone()) }
fn state_data(&self) -> RevealedState { RevealedState::Attachment(self.clone()) }
}

impl Conceal for RevealedAttach {
Expand Down Expand Up @@ -139,7 +139,7 @@ pub struct ConcealedAttach(

impl ConfidentialState for ConcealedAttach {
fn state_type(&self) -> StateType { StateType::Attachment }
fn state_commitment(&self) -> StateCommitment { StateCommitment::Attachment(*self) }
fn state_commitment(&self) -> ConcealedState { ConcealedState::Attachment(*self) }
}

impl CommitVerify<RevealedAttach, StrictEncodedProtocol> for ConcealedAttach {
Expand Down
10 changes: 5 additions & 5 deletions src/contract/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use commit_verify::{CommitVerify, Conceal, StrictEncodedProtocol};
use strict_encoding::{StrictSerialize, StrictType};

use super::{ConfidentialState, ExposedState};
use crate::{StateCommitment, StateData, StateType, LIB_NAME_RGB};
use crate::{ConcealedState, RevealedState, StateType, LIB_NAME_RGB};

/// Struct using for storing Void (i.e. absent) state
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Display, Default)]
Expand All @@ -43,13 +43,13 @@ pub struct VoidState(());

impl ConfidentialState for VoidState {
fn state_type(&self) -> StateType { StateType::Void }
fn state_commitment(&self) -> StateCommitment { StateCommitment::Void }
fn state_commitment(&self) -> ConcealedState { ConcealedState::Void }
}

impl ExposedState for VoidState {
type Confidential = VoidState;
fn state_type(&self) -> StateType { StateType::Void }
fn state_data(&self) -> StateData { StateData::Void }
fn state_data(&self) -> RevealedState { RevealedState::Void }
}

impl Conceal for VoidState {
Expand Down Expand Up @@ -102,7 +102,7 @@ impl RevealedData {
impl ExposedState for RevealedData {
type Confidential = ConcealedData;
fn state_type(&self) -> StateType { StateType::Structured }
fn state_data(&self) -> StateData { StateData::Structured(self.clone()) }
fn state_data(&self) -> RevealedState { RevealedState::Structured(self.clone()) }
}

impl Conceal for RevealedData {
Expand Down Expand Up @@ -155,7 +155,7 @@ pub struct ConcealedData(

impl ConfidentialState for ConcealedData {
fn state_type(&self) -> StateType { StateType::Structured }
fn state_commitment(&self) -> StateCommitment { StateCommitment::Structured(*self) }
fn state_commitment(&self) -> ConcealedState { ConcealedState::Structured(*self) }
}

impl CommitVerify<RevealedData, StrictEncodedProtocol> for ConcealedData {
Expand Down
6 changes: 3 additions & 3 deletions src/contract/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use strict_encoding::{
};

use super::{ConfidentialState, ExposedState};
use crate::{schema, AssignmentType, StateCommitment, StateData, StateType, LIB_NAME_RGB};
use crate::{schema, AssignmentType, ConcealedState, RevealedState, StateType, LIB_NAME_RGB};

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
#[wrapper(Deref, BorrowSlice, Hex, Index, RangeOps)]
Expand Down Expand Up @@ -327,7 +327,7 @@ impl RevealedValue {
impl ExposedState for RevealedValue {
type Confidential = ConcealedValue;
fn state_type(&self) -> StateType { StateType::Fungible }
fn state_data(&self) -> StateData { StateData::Fungible(*self) }
fn state_data(&self) -> RevealedState { RevealedState::Fungible(*self) }
}

impl Conceal for RevealedValue {
Expand Down Expand Up @@ -470,7 +470,7 @@ pub struct ConcealedValue {

impl ConfidentialState for ConcealedValue {
fn state_type(&self) -> StateType { StateType::Fungible }
fn state_commitment(&self) -> StateCommitment { StateCommitment::Fungible(*self) }
fn state_commitment(&self) -> ConcealedState { ConcealedState::Fungible(*self) }
}

impl CommitVerify<RevealedValue, PedersenProtocol> for ConcealedValue {
Expand Down
2 changes: 1 addition & 1 deletion src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub use seal::{
ExposedSeal, GenesisSeal, GraphSeal, OutputSeal, SecretSeal, TxoSeal, WitnessId, WitnessOrd,
WitnessPos, XGenesisSeal, XGraphSeal, XOutputSeal, XPubWitness, XWitness,
};
pub use state::{ConfidentialState, ExposedState, StateCommitment, StateData, StateType};
pub use state::{ConcealedState, ConfidentialState, ExposedState, RevealedState, StateType};
pub use xchain::{
AltLayer1, AltLayer1Set, XChain, XChainParseError, XOutpoint, XCHAIN_BITCOIN_PREFIX,
XCHAIN_LIQUID_PREFIX,
Expand Down
65 changes: 18 additions & 47 deletions src/contract/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::{
ConcealedAttach, ConcealedData, ConcealedValue, RevealedAttach, RevealedData, RevealedValue,
LIB_NAME_RGB,
};

/// Marker trait for types of state which are just a commitment to the actual
/// state data.
pub trait ConfidentialState:
Debug + Hash + StrictDumb + StrictEncode + StrictDecode + Eq + Copy
{
pub trait ConfidentialState: Debug + Hash + Eq + Copy {
fn state_type(&self) -> StateType;
fn state_commitment(&self) -> StateCommitment;
fn state_commitment(&self) -> ConcealedState;
}

/// Marker trait for types of state holding explicit state data.
Expand All @@ -51,9 +48,9 @@ pub trait ExposedState:
+ Ord
+ Clone
{
type Confidential: ConfidentialState;
type Confidential: ConfidentialState + StrictEncode + StrictDecode + StrictDumb;
fn state_type(&self) -> StateType;
fn state_data(&self) -> StateData;
fn state_data(&self) -> RevealedState;
}

/// Categories of the state
Expand Down Expand Up @@ -81,77 +78,51 @@ pub enum StateType {

/// Categories of the state
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum StateData {
#[strict_type(tag = 0x00, dumb)]
pub enum RevealedState {
Void,
#[strict_type(tag = 0x01)]
Fungible(RevealedValue),
#[strict_type(tag = 0x02)]
Structured(RevealedData),
#[strict_type(tag = 0xFF)]
Attachment(RevealedAttach),
}

impl ExposedState for StateData {
type Confidential = StateCommitment;
fn state_type(&self) -> StateType {
match self {
StateData::Void => StateType::Void,
StateData::Fungible(_) => StateType::Fungible,
StateData::Structured(_) => StateType::Structured,
StateData::Attachment(_) => StateType::Attachment,
}
}
fn state_data(&self) -> StateData { self.clone() }
}

impl Conceal for StateData {
type Concealed = StateCommitment;
fn conceal(&self) -> Self::Concealed {
impl RevealedState {
pub fn state_type(&self) -> StateType {
match self {
StateData::Void => StateCommitment::Void,
StateData::Fungible(value) => StateCommitment::Fungible(value.conceal()),
StateData::Structured(data) => StateCommitment::Structured(data.conceal()),
StateData::Attachment(attach) => StateCommitment::Attachment(attach.conceal()),
RevealedState::Void => StateType::Void,
RevealedState::Fungible(_) => StateType::Fungible,
RevealedState::Structured(_) => StateType::Structured,
RevealedState::Attachment(_) => StateType::Attachment,
}
}
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[allow(clippy::large_enum_variant)]
pub enum StateCommitment {
#[strict_type(tag = 0x00, dumb)]
pub enum ConcealedState {
Void,
#[strict_type(tag = 0x01)]
Fungible(ConcealedValue),
#[strict_type(tag = 0x02)]
Structured(ConcealedData),
#[strict_type(tag = 0xFF)]
Attachment(ConcealedAttach),
}

impl ConfidentialState for StateCommitment {
impl ConfidentialState for ConcealedState {
fn state_type(&self) -> StateType {
match self {
StateCommitment::Void => StateType::Void,
StateCommitment::Fungible(_) => StateType::Fungible,
StateCommitment::Structured(_) => StateType::Structured,
StateCommitment::Attachment(_) => StateType::Attachment,
ConcealedState::Void => StateType::Void,
ConcealedState::Fungible(_) => StateType::Fungible,
ConcealedState::Structured(_) => StateType::Structured,
ConcealedState::Attachment(_) => StateType::Attachment,
}
}
fn state_commitment(&self) -> StateCommitment { *self }
fn state_commitment(&self) -> ConcealedState { *self }
}
2 changes: 1 addition & 1 deletion src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{AnchoredBundle, ContractState, Extension, Genesis, SubSchema, LIB_NA

/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB: &str =
"urn:ubideco:stl:ENn5cEDW7QUStLzY6HBXjZFARAbrGHv3tXmdQ3QGzoU3#option-data-mimosa";
"urn:ubideco:stl:2YfZKdnGf9Gibvh15WcNSqe3xN7WH1143j1gCbDgecvG#axiom-detail-panther";

fn _rgb_core_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB), tiny_bset! {
Expand Down
22 changes: 11 additions & 11 deletions src/validation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use strict_types::TypeSystem;

use crate::schema::AssignmentType;
use crate::{
validation, Assign, ConfidentialState, ExposedSeal, ExposedState, OpId, StateCommitment,
StateData, StateSchema,
validation, Assign, ConcealedState, ConfidentialState, ExposedSeal, ExposedState, OpId,
RevealedState, StateSchema,
};

impl StateSchema {
Expand All @@ -40,8 +40,8 @@ impl StateSchema {
match data {
Assign::Confidential { state, .. } | Assign::ConfidentialState { state, .. } => {
match (self, state.state_commitment()) {
(StateSchema::Declarative, StateCommitment::Void) => {}
(StateSchema::Fungible(_), StateCommitment::Fungible(value)) => {
(StateSchema::Declarative, ConcealedState::Void) => {}
(StateSchema::Fungible(_), ConcealedState::Fungible(value)) => {
// [SECURITY-CRITICAL]: Bulletproofs validation
if let Err(err) = value.verify_range_proof() {
status.add_failure(validation::Failure::BulletproofsInvalid(
Expand All @@ -51,12 +51,12 @@ impl StateSchema {
));
}
}
(StateSchema::Structured(_), StateCommitment::Structured(_)) => {
(StateSchema::Structured(_), ConcealedState::Structured(_)) => {
status.add_info(validation::Info::UncheckableConfidentialState(
*opid, state_type,
));
}
(StateSchema::Attachment(_), StateCommitment::Attachment(_)) => {
(StateSchema::Attachment(_), ConcealedState::Attachment(_)) => {
status.add_info(validation::Info::UncheckableConfidentialState(
*opid, state_type,
));
Expand All @@ -74,8 +74,8 @@ impl StateSchema {
}
Assign::Revealed { state, .. } | Assign::ConfidentialSeal { state, .. } => {
match (self, state.state_data()) {
(StateSchema::Declarative, StateData::Void) => {}
(StateSchema::Attachment(media_type), StateData::Attachment(attach))
(StateSchema::Declarative, RevealedState::Void) => {}
(StateSchema::Attachment(media_type), RevealedState::Attachment(attach))
if !attach.media_type.conforms(media_type) =>
{
status.add_failure(validation::Failure::MediaTypeMismatch {
Expand All @@ -85,7 +85,7 @@ impl StateSchema {
found: attach.media_type,
});
}
(StateSchema::Fungible(schema), StateData::Fungible(v))
(StateSchema::Fungible(schema), RevealedState::Fungible(v))
if v.value.fungible_type() != *schema =>
{
status.add_failure(validation::Failure::FungibleTypeMismatch {
Expand All @@ -95,8 +95,8 @@ impl StateSchema {
found: v.value.fungible_type(),
});
}
(StateSchema::Fungible(_), StateData::Fungible(_)) => {}
(StateSchema::Structured(sem_id), StateData::Structured(data)) => {
(StateSchema::Fungible(_), RevealedState::Fungible(_)) => {}
(StateSchema::Structured(sem_id), RevealedState::Structured(data)) => {
if type_system
.strict_deserialize_type(*sem_id, data.value.as_ref())
.is_err()
Expand Down

0 comments on commit 2308c17

Please sign in to comment.