Skip to content

Commit

Permalink
vm: move witness-related types from operations module
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 30, 2024
1 parent 8508441 commit e956247
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub use operations::{
};
pub use seal::{
ExposedSeal, GenesisSeal, GraphSeal, OutputSeal, SecretSeal, TxoSeal, XGenesisSeal, XGraphSeal,
XOutputSeal, XWitnessId, XWitnessTx,
XOutputSeal,
};
pub use state::{ConcealedState, ConfidentialState, ExposedState, RevealedState, StateType};
pub use xchain::{
Expand Down
102 changes: 3 additions & 99 deletions src/operation/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@ use std::hash::Hash;
use bp::dbc::Method;
pub use bp::seals::txout::blind::{ChainBlindSeal, ParseError, SingleBlindSeal};
pub use bp::seals::txout::TxoSeal;
use bp::seals::txout::{BlindSeal, CloseMethod, ExplicitSeal, SealTxid, VerifyError, Witness};
use bp::seals::txout::{BlindSeal, CloseMethod, ExplicitSeal, SealTxid};
pub use bp::seals::SecretSeal;
use bp::{dbc, Outpoint, Tx, Txid, Vout};
use commit_verify::{mpc, Conceal};
use single_use_seals::SealWitness;
use bp::{Outpoint, Txid, Vout};
use commit_verify::Conceal;
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::operation::xchain::Impossible;
use crate::{XChain, XOutpoint};

pub type GenesisSeal = SingleBlindSeal<Method>;
pub type GraphSeal = ChainBlindSeal<Method>;

pub type OutputSeal = ExplicitSeal<Txid, Method>;

// TODO: Move to vm::Contract
pub type XWitnessId = XChain<Txid>;

pub type XGenesisSeal = XChain<GenesisSeal>;
pub type XGraphSeal = XChain<GraphSeal>;
pub type XOutputSeal = XChain<OutputSeal>;
Expand Down Expand Up @@ -135,97 +130,6 @@ impl XChain<GenesisSeal> {
pub fn to_outpoint(&self) -> XOutpoint { self.map_ref(GenesisSeal::to_outpoint).into() }
}

impl<U: ExposedSeal> XChain<U> {
pub fn method(self) -> CloseMethod
where U: TxoSeal {
match self {
XChain::Bitcoin(seal) => seal.method(),
XChain::Liquid(seal) => seal.method(),
XChain::Other(_) => unreachable!(),
}
}

#[inline]
pub fn to_output_seal(self) -> Option<XOutputSeal>
where U: TxoSeal {
Some(match self {
XChain::Bitcoin(seal) => {
let outpoint = seal.outpoint()?;
XChain::Bitcoin(ExplicitSeal::new(seal.method(), outpoint))
}
XChain::Liquid(seal) => {
let outpoint = seal.outpoint()?;
XChain::Liquid(ExplicitSeal::new(seal.method(), outpoint))
}
XChain::Other(_) => unreachable!(),
})
}

pub fn try_to_output_seal(self, witness_id: XWitnessId) -> Result<XOutputSeal, Self>
where U: TxoSeal {
self.to_output_seal()
.or(match (self, witness_id) {
(XChain::Bitcoin(seal), XWitnessId::Bitcoin(txid)) => {
Some(XChain::Bitcoin(ExplicitSeal::new(seal.method(), seal.outpoint_or(txid))))
}
(XChain::Liquid(seal), XWitnessId::Liquid(txid)) => {
Some(XChain::Liquid(ExplicitSeal::new(seal.method(), seal.outpoint_or(txid))))
}
_ => None,
})
.ok_or(self)
}
}

// TODO: Move to vm::Contract
pub type XWitnessTx<X = Impossible> = XChain<Tx, X>;

impl XWitnessTx {
pub fn witness_id(&self) -> XWitnessId {
match self {
Self::Bitcoin(tx) => XWitnessId::Bitcoin(tx.txid()),
Self::Liquid(tx) => XWitnessId::Liquid(tx.txid()),
Self::Other(_) => unreachable!(),
}
}
}

impl<Dbc: dbc::Proof> XChain<Witness<Dbc>> {
pub fn witness_id(&self) -> XWitnessId {
match self {
Self::Bitcoin(w) => XWitnessId::Bitcoin(w.txid),
Self::Liquid(w) => XWitnessId::Liquid(w.txid),
Self::Other(_) => unreachable!(),
}
}
}

impl<Dbc: dbc::Proof, Seal: TxoSeal> SealWitness<Seal> for XChain<Witness<Dbc>> {
type Message = mpc::Commitment;
type Error = VerifyError<Dbc::Error>;

fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error> {
match self {
Self::Bitcoin(witness) | Self::Liquid(witness) => witness.verify_seal(seal, msg),
Self::Other(_) => unreachable!(),
}
}

fn verify_many_seals<'seal>(
&self,
seals: impl IntoIterator<Item = &'seal Seal>,
msg: &Self::Message,
) -> Result<(), Self::Error>
where
Seal: 'seal,
{
match self {
Self::Bitcoin(witness) | Self::Liquid(witness) => witness.verify_many_seals(seals, msg),
Self::Other(_) => unreachable!(),
}
}
}

impl<Id: SealTxid> XChain<BlindSeal<Id>> {
/// Converts revealed seal into concealed.
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use strict_types::typelib::LibBuilder;
use strict_types::{CompileError, TypeLib};

use crate::validation::DbcProof;
use crate::vm::GlobalOrd;
use crate::vm::{GlobalOrd, XWitnessId};
use crate::{
Extension, Genesis, OpCommitment, Schema, TransitionBundle, XWitnessId, LIB_NAME_RGB_COMMIT,
Extension, Genesis, OpCommitment, Schema, TransitionBundle, LIB_NAME_RGB_COMMIT,
LIB_NAME_RGB_LOGIC,
};

Expand Down
4 changes: 2 additions & 2 deletions src/validation/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use amplify::confinement::Confined;
use strict_types::TypeSystem;

use super::EAnchor;
use crate::vm::OpRef;
use crate::{BundleId, Genesis, OpId, Operation, Schema, TransitionBundle, XWitnessId};
use crate::vm::{OpRef, XWitnessId};
use crate::{BundleId, Genesis, OpId, Operation, Schema, TransitionBundle};

pub const CONSIGNMENT_MAX_LIBS: usize = 1024;

Expand Down
6 changes: 3 additions & 3 deletions src/validation/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ use amplify::num::u24;
use commit_verify::mpc::InvalidProof;
use strict_types::SemId;

use crate::operation::Opout;
use crate::schema::{self, SchemaId};
use crate::validation::WitnessResolverError;
use crate::vm::XWitnessId;
use crate::{
BundleId, ContractId, Layer1, OccurrencesMismatch, OpFullType, OpId, StateType, Vin,
XGraphSeal, XOutputSeal, XWitnessId,
BundleId, ContractId, Layer1, OccurrencesMismatch, OpFullType, OpId, Opout, StateType, Vin,
XGraphSeal, XOutputSeal,
};

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Display)]
Expand Down
7 changes: 5 additions & 2 deletions src/validation/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ use single_use_seals::SealWitness;

use super::status::Failure;
use super::{CheckedConsignment, ConsignmentApi, DbcProof, EAnchor, Status, Validity};
use crate::vm::{ContractStateAccess, ContractStateEvolve, OpOrd, OpRef, TxOrd, WitnessOrd};
use crate::vm::{
ContractStateAccess, ContractStateEvolve, OpOrd, OpRef, TxOrd, WitnessOrd, XWitnessId,
XWitnessTx,
};
use crate::{
validation, AltLayer1, BundleId, ContractId, Layer1, OpId, OpType, Operation, Opout, Schema,
SchemaId, TransitionBundle, XChain, XOutpoint, XOutputSeal, XWitnessId, XWitnessTx,
SchemaId, TransitionBundle, XChain, XOutpoint, XOutputSeal,
};

#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
Expand Down
103 changes: 100 additions & 3 deletions src/vm/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,112 @@ use std::rc::Rc;

use amplify::num::u24;
use amplify::Bytes32;
use bp::seals::txout::{CloseMethod, ExplicitSeal, VerifyError, Witness};
use bp::{dbc, Tx, Txid};
use commit_verify::mpc;
use single_use_seals::SealWitness;
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::{
AssetTags, AssignmentType, Assignments, AssignmentsRef, AttachState, ContractId, DataState,
Extension, ExtensionType, FungibleState, Genesis, GlobalState, GlobalStateType, GraphSeal,
Inputs, Metadata, OpFullType, OpId, OpType, Operation, Transition, TransitionType,
TypedAssigns, Valencies, XOutpoint, XWitnessId, LIB_NAME_RGB_LOGIC,
ExposedSeal, Extension, ExtensionType, FungibleState, Genesis, GlobalState, GlobalStateType,
GraphSeal, Impossible, Inputs, Metadata, OpFullType, OpId, OpType, Operation, Transition,
TransitionType, TxoSeal, TypedAssigns, Valencies, XChain, XOutpoint, XOutputSeal,
LIB_NAME_RGB_LOGIC,
};

pub type XWitnessId = XChain<Txid>;

pub type XWitnessTx<X = Impossible> = XChain<Tx, X>;

impl XWitnessTx {
pub fn witness_id(&self) -> XWitnessId {
match self {
Self::Bitcoin(tx) => XWitnessId::Bitcoin(tx.txid()),
Self::Liquid(tx) => XWitnessId::Liquid(tx.txid()),
Self::Other(_) => unreachable!(),
}
}
}

impl<Dbc: dbc::Proof> XChain<Witness<Dbc>> {
pub fn witness_id(&self) -> XWitnessId {
match self {
Self::Bitcoin(w) => XWitnessId::Bitcoin(w.txid),
Self::Liquid(w) => XWitnessId::Liquid(w.txid),
Self::Other(_) => unreachable!(),
}
}
}

impl<Dbc: dbc::Proof, Seal: TxoSeal> SealWitness<Seal> for XChain<Witness<Dbc>> {
type Message = mpc::Commitment;
type Error = VerifyError<Dbc::Error>;

fn verify_seal(&self, seal: &Seal, msg: &Self::Message) -> Result<(), Self::Error> {
match self {
Self::Bitcoin(witness) | Self::Liquid(witness) => witness.verify_seal(seal, msg),
Self::Other(_) => unreachable!(),
}
}

fn verify_many_seals<'seal>(
&self,
seals: impl IntoIterator<Item = &'seal Seal>,
msg: &Self::Message,
) -> Result<(), Self::Error>
where
Seal: 'seal,
{
match self {
Self::Bitcoin(witness) | Self::Liquid(witness) => witness.verify_many_seals(seals, msg),
Self::Other(_) => unreachable!(),
}
}
}

impl<U: ExposedSeal> XChain<U> {
pub fn method(self) -> CloseMethod
where U: TxoSeal {
match self {
XChain::Bitcoin(seal) => seal.method(),
XChain::Liquid(seal) => seal.method(),
XChain::Other(_) => unreachable!(),
}
}

#[inline]
pub fn to_output_seal(self) -> Option<XOutputSeal>
where U: TxoSeal {
Some(match self {
XChain::Bitcoin(seal) => {
let outpoint = seal.outpoint()?;
XChain::Bitcoin(ExplicitSeal::new(seal.method(), outpoint))
}
XChain::Liquid(seal) => {
let outpoint = seal.outpoint()?;
XChain::Liquid(ExplicitSeal::new(seal.method(), outpoint))
}
XChain::Other(_) => unreachable!(),
})
}

pub fn try_to_output_seal(self, witness_id: XWitnessId) -> Result<XOutputSeal, Self>
where U: TxoSeal {
self.to_output_seal()
.or(match (self, witness_id) {
(XChain::Bitcoin(seal), XWitnessId::Bitcoin(txid)) => {
Some(XChain::Bitcoin(ExplicitSeal::new(seal.method(), seal.outpoint_or(txid))))
}
(XChain::Liquid(seal), XWitnessId::Liquid(txid)) => {
Some(XChain::Liquid(ExplicitSeal::new(seal.method(), seal.outpoint_or(txid))))
}
_ => None,
})
.ok_or(self)
}
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, From)]
pub enum OpRef<'op> {
#[from]
Expand Down
2 changes: 1 addition & 1 deletion src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod contract;
pub use aluvm::aluasm_isa;
pub use contract::{
ContractStateAccess, ContractStateEvolve, GlobalContractState, GlobalOrd, GlobalStateIter,
OpOrd, OpRef, TxOrd, TxPos, UnknownGlobalStateType, WitnessOrd,
OpOrd, OpRef, TxOrd, TxPos, UnknownGlobalStateType, WitnessOrd, XWitnessId, XWitnessTx,
};
pub(crate) use contract::{OpInfo, VmContext};
pub use isa::RgbIsa;
Expand Down

0 comments on commit e956247

Please sign in to comment.