Skip to content

Commit

Permalink
dbc: make Anchor consensus-only data. Remove all tx info outside of t…
Browse files Browse the repository at this point in the history
…he library
  • Loading branch information
dr-orlovsky committed Mar 30, 2024
1 parent 5b93351 commit 7aece00
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 260 deletions.
43 changes: 41 additions & 2 deletions consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use std::fmt::{Formatter, LowerHex};
use std::str::FromStr;

use amplify::hex::{FromHex, ToHex};
use amplify::{Bytes32StrRev, Wrapper};
use amplify::{ByteArray, Bytes32StrRev, Wrapper};

use crate::{BlockDataParseError, ConsensusDecode, ConsensusEncode, LIB_NAME_BITCOIN};
use crate::{BlockDataParseError, ConsensusDecode, ConsensusEncode, Txid, LIB_NAME_BITCOIN};

#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
Expand Down Expand Up @@ -58,6 +58,10 @@ pub struct TxMerkleNode(
Bytes32StrRev,
);

impl From<Txid> for TxMerkleNode {
fn from(txid: Txid) -> Self { Self::from_byte_array(txid.to_byte_array()) }

Check warning on line 62 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L62

Added line #L62 was not covered by tests
}

#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
Expand All @@ -73,6 +77,10 @@ pub struct BlockMerkleRoot(
Bytes32StrRev,
);

impl From<TxMerkleNode> for BlockMerkleRoot {
fn from(node: TxMerkleNode) -> Self { Self::from_byte_array(node.to_byte_array()) }

Check warning on line 81 in consensus/src/block.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/block.rs#L81

Added line #L81 was not covered by tests
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]
#[display(LowerHex)]
#[derive(StrictType, StrictEncode, StrictDecode, StrictDumb)]
Expand Down Expand Up @@ -112,6 +120,37 @@ impl FromStr for BlockHeader {
}
}

/*
#[derive(Wrapper, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
#[wrapper(BorrowSlice, Index, RangeOps, Debug)]
pub struct TxMerklePath(TinyVec<TxMerkleNode>);
impl TxMerklePath {
pub fn check(&self, txid: Txid, merkle_root: BlockMerkleRoot) -> Result<(), ()> {
// For a zero-length merkle path the txid is coinbase, and it must be equal to
// the merkle root.
let mut node = TxMerkleNode::from(txid);
for step in &self.0 {
node = TxMerkleNode::with(node, *step);
}
if node != merkle_root.into() {
Err(())
} else {
Ok(())
}
}
}
*/

#[cfg(test)]
mod test {
use super::*;
Expand Down
129 changes: 10 additions & 119 deletions dbc/src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ use bc::{Tx, TxMerkleNode, Txid};
use commit_verify::mpc::{self, Message, ProtocolId};
use strict_encoding::{StrictDumb, StrictEncode};

use crate::opret::OpretProof;
use crate::tapret::TapretProof;
use crate::{DbcMethod, Method, LIB_NAME_BPCORE};

mod dbc {
Expand Down Expand Up @@ -115,7 +113,7 @@ impl TxWitness {
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct EtxWitness<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod = Method> {
pub struct Anchor<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod = Method> {
/// Structured multi-protocol LNPBP-4 data the transaction commits to.
pub mpc_proof: L,

Expand All @@ -127,7 +125,7 @@ pub struct EtxWitness<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod
pub _method: PhantomData<M>,
}

impl<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod> EtxWitness<L, D, M> {
impl<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod> Anchor<L, D, M> {
/// Constructs extra-transaction witness for a given MPC and DBC
/// proofs.
pub fn new(mpc_proof: L, dbc_proof: D) -> Self {

Check warning on line 131 in dbc/src/anchor.rs

View check run for this annotation

Codecov / codecov/patch

dbc/src/anchor.rs#L131

Added line #L131 was not covered by tests
Expand Down Expand Up @@ -156,16 +154,16 @@ pub enum MergeError {
DbcMismatch,
}

impl<D: dbc::Proof<M>, M: DbcMethod> EtxWitness<mpc::MerkleProof, D, M> {
impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleProof, D, M> {
/// Reconstructs anchor containing merkle block
pub fn into_merkle_block(
self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
) -> Result<EtxWitness<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
) -> Result<Anchor<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
let lnpbp4_proof =
mpc::MerkleBlock::with(&self.mpc_proof, protocol_id.into(), message.into())?;
Ok(EtxWitness {
Ok(Anchor {
mpc_proof: lnpbp4_proof,
dbc_proof: self.dbc_proof,
_method: default!(),
Expand All @@ -177,7 +175,7 @@ impl<D: dbc::Proof<M>, M: DbcMethod> EtxWitness<mpc::MerkleProof, D, M> {
&self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
) -> Result<EtxWitness<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
) -> Result<Anchor<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
self.clone().into_merkle_block(protocol_id, message)
}

Expand Down Expand Up @@ -207,13 +205,13 @@ impl<D: dbc::Proof<M>, M: DbcMethod> EtxWitness<mpc::MerkleProof, D, M> {
}
}

impl<D: dbc::Proof<M>, M: DbcMethod> EtxWitness<mpc::MerkleBlock, D, M> {
impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleBlock, D, M> {
/// Conceals all LNPBP-4 data except specific protocol and produces merkle
/// proof anchor.
pub fn to_merkle_proof(
&self,
protocol: impl Into<ProtocolId>,
) -> Result<EtxWitness<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
) -> Result<Anchor<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
self.clone().into_merkle_proof(protocol)
}

Expand All @@ -222,9 +220,9 @@ impl<D: dbc::Proof<M>, M: DbcMethod> EtxWitness<mpc::MerkleBlock, D, M> {
pub fn into_merkle_proof(
self,
protocol: impl Into<ProtocolId>,
) -> Result<EtxWitness<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
) -> Result<Anchor<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
let lnpbp4_proof = self.mpc_proof.to_merkle_proof(protocol.into())?;
Ok(EtxWitness {
Ok(Anchor {
mpc_proof: lnpbp4_proof,
dbc_proof: self.dbc_proof,
_method: default!(),
Expand All @@ -248,110 +246,3 @@ impl<D: dbc::Proof<M>, M: DbcMethod> EtxWitness<mpc::MerkleBlock, D, M> {
Ok(self)
}
}

#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BPCORE, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase", untagged)
)]
pub enum Anchor<P: mpc::Proof + StrictDumb = mpc::MerkleProof> {
#[strict_type(tag = 0x01)]
Tapret {
tapret: EtxWitness<P, TapretProof>,
txw: TxWitness,
},
#[strict_type(tag = 0x02)]
Opret {
opret: EtxWitness<P, OpretProof>,
txw: TxWitness,
},
#[strict_type(tag = 0x03)]
Dual {
tapret: EtxWitness<P, TapretProof>,
opret: EtxWitness<P, OpretProof>,
txw: TxWitness,
},
}

impl<P: mpc::Proof + StrictDumb> StrictDumb for Anchor<P> {
fn strict_dumb() -> Self {
Self::Tapret {
tapret: strict_dumb!(),
txw: strict_dumb!(),
}
}
}

impl<P: mpc::Proof + StrictDumb> Anchor<P> {
pub fn txid(&self) -> Txid {
match self {
Anchor::Tapret { txw, .. } | Anchor::Opret { txw, .. } | Anchor::Dual { txw, .. } => {
txw.txid()
}
}
}

pub fn tx_witness(&self) -> &TxWitness {
match self {
Anchor::Tapret { txw, .. } | Anchor::Opret { txw, .. } | Anchor::Dual { txw, .. } => {
txw
}
}
}

pub fn mpc_proofs(&self) -> impl Iterator<Item = &P> {
let (t, o) = match self {
Anchor::Tapret { tapret, txw: _ } => (Some(tapret), None),
Anchor::Opret { opret, txw: _ } => (None, Some(opret)),
Anchor::Dual {
tapret,
opret,
txw: _,
} => (Some(tapret), Some(opret)),
};
t.map(|a| &a.mpc_proof)
.into_iter()
.chain(o.map(|a| &a.mpc_proof))
}

fn split(
self,
) -> (TxWitness, Option<EtxWitness<P, TapretProof>>, Option<EtxWitness<P, OpretProof>>) {
match self {
Anchor::Tapret { tapret, txw } => (txw, Some(tapret), None),
Anchor::Opret { opret, txw } => (txw, None, Some(opret)),
Anchor::Dual { tapret, opret, txw } => (txw, Some(tapret), Some(opret)),
}
}
}

impl Anchor<mpc::MerkleBlock> {
/// Merges two anchors keeping revealed data.
pub fn merge_reveal(self, other: Self) -> Result<Self, MergeError> {
if self == other {
return Ok(self);
}
let (txw1, tapret1, opret1) = self.split();
let (txw2, tapret2, opret2) = other.split();
let txw = txw1.merge_reveal(txw2)?;
let tapret = match (tapret1, tapret2) {
(None, None) => None,
(Some(tapret), None) | (None, Some(tapret)) => Some(tapret),
(Some(tapret1), Some(tapret2)) => Some(tapret1.merge_reveal(tapret2)?),
};
let opret = match (opret1, opret2) {
(None, None) => None,
(Some(opret), None) | (None, Some(opret)) => Some(opret),
(Some(opret1), Some(opret2)) => Some(opret1.merge_reveal(opret2)?),
};
Ok(match (tapret, opret) {
(None, None) => unreachable!(),
(Some(tapret), None) => Self::Tapret { tapret, txw },
(None, Some(opret)) => Self::Opret { opret, txw },
(Some(tapret), Some(opret)) => Self::Dual { tapret, opret, txw },
})
}
}
2 changes: 1 addition & 1 deletion dbc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ pub mod sigtweak;
pub mod tapret;
mod proof;

pub use anchor::Anchor;
pub use anchor::{Anchor, TxWitness};
pub use proof::{DbcMethod, Method, MethodParseError, Proof};
6 changes: 2 additions & 4 deletions seals/src/txout/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ use std::marker::PhantomData;

use bc::{Tx, Txid};
use commit_verify::mpc;
use dbc::anchor::EtxWitness;
use dbc::{DbcMethod, Method};
use single_use_seals::SealWitness;
use strict_encoding::StrictDumb;

use crate::txout::{TxoSeal, VerifyError};
use crate::SealCloseMethod;
Expand All @@ -53,11 +51,11 @@ pub struct Witness<D: dbc::Proof<M>, M: DbcMethod = Method> {
impl<D: dbc::Proof<M>, M: DbcMethod> Witness<D, M> {
/// Constructs witness from a witness transaction and extra-transaction
/// proof, taken from an anchor.
pub fn with<L: mpc::Proof + StrictDumb>(tx: Tx, etx: EtxWitness<L, D, M>) -> Witness<D, M> {
pub fn with(tx: Tx, dbc: D) -> Witness<D, M> {

Check warning on line 54 in seals/src/txout/witness.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout/witness.rs#L54

Added line #L54 was not covered by tests
Witness {
txid: tx.txid(),

Check warning on line 56 in seals/src/txout/witness.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout/witness.rs#L56

Added line #L56 was not covered by tests
tx,
proof: etx.dbc_proof,
proof: dbc,

Check warning on line 58 in seals/src/txout/witness.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout/witness.rs#L58

Added line #L58 was not covered by tests
_phantom: default!(),
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,29 @@

use bc::Txid;
use commit_verify::mpc;
use dbc::opret::OpretProof;
use dbc::tapret::TapretProof;
use dbc::{Method, LIB_NAME_BPCORE};
use seals::txout::TxPtr;
use strict_types::{CompileError, LibBuilder, TypeLib};

/// Strict types id for the library providing data types from [`dbc`] and
/// [`seals`] crates.
pub const LIB_ID_BPCORE: &str =
"urn:ubideco:stl:3uf6LCUN84pnw4JuwTuPscYa1vxtbYHHmA7EHUUgHdcX#escape-precise-andrea";
"urn:ubideco:stl:6skrch4mJzDzVaTYnCgCxFJLw23SSUxNhK7PKgPdSapR#roof-parent-reunion";

fn _bp_core_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_BPCORE), tiny_bset! {
strict_types::stl::std_stl().to_dependency(),
bc::stl::bp_tx_stl().to_dependency(),
commit_verify::stl::commit_verify_stl().to_dependency()
})
.transpile::<dbc::Anchor<mpc::MerkleTree>>()
.transpile::<dbc::Anchor<mpc::MerkleBlock>>()
.transpile::<dbc::Anchor<mpc::MerkleProof>>()
.transpile::<dbc::Anchor<mpc::MerkleTree, TapretProof>>()
.transpile::<dbc::Anchor<mpc::MerkleBlock, TapretProof>>()
.transpile::<dbc::Anchor<mpc::MerkleProof, TapretProof>>()
.transpile::<dbc::Anchor<mpc::MerkleTree, OpretProof>>()
.transpile::<dbc::Anchor<mpc::MerkleBlock, OpretProof>>()
.transpile::<dbc::Anchor<mpc::MerkleProof, OpretProof>>()
.transpile::<seals::txout::ExplicitSeal<TxPtr, Method>>()
.transpile::<seals::txout::ExplicitSeal<Txid, Method>>()
.transpile::<seals::SecretSeal>()
Expand Down
3 changes: 0 additions & 3 deletions stl/Anchor.MerkleBlock.Tapret.vesper
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
AnchorMerkleBlockTapretProof rec
reserved1 bytes len=1 aka=ReservedBytes1
txid bytes len=32 aka=Txid
reserved2 bytes len=1 aka=ReservedBytes1
mpcProof rec MerkleBlock
depth enum {
U5 _0=0 _1=1 _2=2 _3=3 _4=4 _5=5 _6=6 _7=7
Expand Down
3 changes: 0 additions & 3 deletions stl/Anchor.MerkleProof.Tapret.vesper
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
AnchorMerkleProofTapretProof rec
reserved1 bytes len=1 aka=ReservedBytes1
txid bytes len=32 aka=Txid
reserved2 bytes len=1 aka=ReservedBytes1
mpcProof rec MerkleProof
pos is U32
cofactor is U16
Expand Down
3 changes: 0 additions & 3 deletions stl/Anchor.MerkleTree.Opret.vesper
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
AnchorMerkleTreeOpretProof rec
reserved1 bytes len=1 aka=ReservedBytes1
txid bytes len=32 aka=Txid
reserved2 bytes len=1 aka=ReservedBytes1
mpcProof rec MerkleTree
depth enum {
U5 _0=0 _1=1 _2=2 _3=3 _4=4 _5=5 _6=6 _7=7
Expand Down
3 changes: 0 additions & 3 deletions stl/Anchor.MerkleTree.Tapret.vesper
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
AnchorMerkleTreeTapretProof rec
reserved1 bytes len=1 aka=ReservedBytes1
txid bytes len=32 aka=Txid
reserved2 bytes len=1 aka=ReservedBytes1
mpcProof rec MerkleTree
depth enum {
U5 _0=0 _1=1 _2=2 _3=3 _4=4 _5=5 _6=6 _7=7
Expand Down
Loading

0 comments on commit 7aece00

Please sign in to comment.