Skip to content

Commit

Permalink
Merge pull request #298 from RGB-WG/wtxoseal
Browse files Browse the repository at this point in the history
Differentiate witness txo-based seals from existing utxo-based seals
  • Loading branch information
dr-orlovsky authored Jan 30, 2025
2 parents a4f0883 + 02ab71c commit 0b3f910
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 184 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ ultrasonic = { git = "https://github.com/AluVM/ultrasonic", branch = "master" }
hypersonic = { git = "https://github.com/AluVM/sonic", branch = "master" }
sonic-api = { git = "https://github.com/AluVM/sonic", branch = "master" }
sonic-callreq = { git = "https://github.com/AluVM/sonic", branch = "master" }
bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "v0.12" }
bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "v0.12" }
bp-seals = { git = "https://github.com/BP-WG/bp-core", branch = "v0.12" }
bp-core = { git = "https://github.com/BP-WG/bp-core", branch = "v0.12" }
bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "wtxoseal" }
bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "wtxoseal" }
bp-seals = { git = "https://github.com/BP-WG/bp-core", branch = "wtxoseal" }
bp-core = { git = "https://github.com/BP-WG/bp-core", branch = "wtxoseal" }
bp-invoice = { git = "https://github.com/BP-WG/bp-std", branch = "v0.12" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "v0.12" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "wtxoseal" }
38 changes: 21 additions & 17 deletions cli/src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ use commit_verify::ReservedBytes;
use hypersonic::aora::Aora;
use hypersonic::{Articles, ContractId, FileSupply, Operation};
use rgb::{
FilePile, Index, MoundConsumeError, Pile, PublishedWitness, RgbSeal, SealWitness, Stockpile,
MAGIC_BYTES_CONSIGNMENT,
FilePile, Index, MoundConsumeError, Pile, PublishedWitness, RgbSealDef, SealWitness,
SingleUseSeal, Stockpile, MAGIC_BYTES_CONSIGNMENT,
};
use serde::{Deserialize, Serialize};
use strict_encoding::{DecodeError, StreamReader, StrictDecode, StrictEncode, StrictReader};

pub fn dump_stockpile<Seal>(src: &Path, dst: impl AsRef<Path>) -> anyhow::Result<()>
pub fn dump_stockpile<SealDef>(src: &Path, dst: impl AsRef<Path>) -> anyhow::Result<()>
where
Seal: RgbSeal + Serialize + for<'de> Deserialize<'de>,
Seal::CliWitness: Serialize + StrictEncode + StrictDecode,
Seal::PubWitness: Serialize + StrictEncode + StrictDecode,
<Seal::PubWitness as PublishedWitness<Seal>>::PubId:
SealDef: RgbSealDef + Serialize + for<'de> Deserialize<'de>,
SealDef::Src: Serialize + for<'de> Deserialize<'de>,
<SealDef::Src as SingleUseSeal>::CliWitness: Serialize + StrictEncode + StrictDecode,
<SealDef::Src as SingleUseSeal>::PubWitness: Serialize + StrictEncode + StrictDecode,
<<SealDef::Src as SingleUseSeal>::PubWitness as PublishedWitness<SealDef::Src>>::PubId:
Ord + From<[u8; 32]> + Into<[u8; 32]> + Serialize,
{
let dst = dst.as_ref();
fs::create_dir_all(dst)?;

print!("Reading contract stockpile from '{}' ... ", src.display());
let mut stockpile = Stockpile::<FileSupply, FilePile<Seal>>::load(src);
let mut stockpile = Stockpile::<FileSupply, FilePile<SealDef>>::load(src);
println!("success reading {}", stockpile.contract_id());

print!("Processing contract articles ... ");
Expand Down Expand Up @@ -129,12 +130,15 @@ where
Ok(())
}

pub fn dump_consignment<Seal>(src: &Path, dst: impl AsRef<Path>) -> anyhow::Result<()>
pub fn dump_consignment<SealDef>(src: &Path, dst: impl AsRef<Path>) -> anyhow::Result<()>
where
Seal: RgbSeal + Serialize,
Seal::CliWitness: Serialize + for<'de> Deserialize<'de> + StrictEncode + StrictDecode,
Seal::PubWitness: Serialize + for<'de> Deserialize<'de> + StrictEncode + StrictDecode,
<Seal::PubWitness as PublishedWitness<Seal>>::PubId:
SealDef: RgbSealDef + Serialize,
SealDef::Src: Serialize,
<SealDef::Src as SingleUseSeal>::CliWitness:
Serialize + for<'de> Deserialize<'de> + StrictEncode + StrictDecode,
<SealDef::Src as SingleUseSeal>::PubWitness:
Serialize + for<'de> Deserialize<'de> + StrictEncode + StrictDecode,
<<SealDef::Src as SingleUseSeal>::PubWitness as PublishedWitness<SealDef::Src>>::PubId:
Ord + From<[u8; 32]> + Into<[u8; 32]> + Serialize,
{
let dst = dst.as_ref();
Expand All @@ -146,7 +150,7 @@ where
let magic_bytes = Bytes16::strict_decode(&mut stream)?;
if magic_bytes.to_byte_array() != MAGIC_BYTES_CONSIGNMENT {
return Err(anyhow!(
MoundConsumeError::<Seal>::UnrecognizedMagic(magic_bytes.to_hex()).to_string()
MoundConsumeError::<SealDef>::UnrecognizedMagic(magic_bytes.to_hex()).to_string()
));
}
// Version
Expand All @@ -173,7 +177,7 @@ where

let out = File::create_new(dst.join("0000-seals.yml"))?;
let defined_seals =
SmallVec::<Seal>::strict_decode(&mut stream).expect("Failed to read consignment stream");
SmallVec::<SealDef>::strict_decode(&mut stream).expect("Failed to read consignment stream");
serde_yaml::to_writer(&out, &defined_seals)?;
seal_count += defined_seals.len();

Expand All @@ -196,7 +200,7 @@ where
serde_yaml::to_writer(&out, &operation)?;

let out = File::create_new(dst.join(format!("{op_count:04}-seals.yml")))?;
let defined_seals = SmallVec::<Seal>::strict_decode(&mut stream)
let defined_seals = SmallVec::<SealDef>::strict_decode(&mut stream)
.expect("Failed to read consignment stream");
serde_yaml::to_writer(&out, &defined_seals)?;
seal_count += defined_seals.len();
Expand All @@ -206,7 +210,7 @@ where
let out = File::create_new(
dst.join(format!("{op_count:04}-witness-{:02}.yaml", no + 1)),
)?;
let witness = SealWitness::<Seal>::strict_decode(&mut stream)?;
let witness = SealWitness::<SealDef::Src>::strict_decode(&mut stream)?;
serde_yaml::to_writer(&out, &witness)?;
}

Expand Down
6 changes: 3 additions & 3 deletions cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use std::fs;
use std::io::stdout;

use bp::seals::TxoSeal;
use bp::seals::WTxoSeal;
use rgb::popls::bp::PrefabBundle;
use rgb::Schema;
use strict_encoding::StrictDeserialize;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Args {
}
})?;
}
dump_consignment::<TxoSeal>(src, dst)?;
dump_consignment::<WTxoSeal>(src, dst)?;
}
Some(ext) if ext == "contract" => {
let dst = dst
Expand All @@ -111,7 +111,7 @@ impl Args {
}
})?;
}
dump_stockpile::<TxoSeal>(src, dst)?;
dump_stockpile::<WTxoSeal>(src, dst)?;
}
Some(_) => {
return Err(anyhow!(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]

extern crate alloc;

Expand Down
71 changes: 38 additions & 33 deletions src/mound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use amplify::hex::ToHex;
use amplify::Bytes16;
use commit_verify::ReservedBytes;
use hypersonic::{AuthToken, CellAddr, CodexId, ContractId, ContractName, Opid, Schema, Supply};
use rgb::RgbSeal;
use rgb::RgbSealDef;
use single_use_seals::{PublishedWitness, SingleUseSeal};
use strict_encoding::{
DecodeError, ReadRaw, StrictDecode, StrictDumb, StrictEncode, StrictReader, StrictWriter,
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<S: Supply, P: Pile, X: Excavate<S, P>> Mound<S, P, X> {

pub fn issue(
&mut self,
params: CreateParams<P::Seal>,
params: CreateParams<P::SealDef>,
supply: S,
pile: P,
) -> Result<ContractId, IssueError> {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<S: Supply, P: Pile, X: Excavate<S, P>> Mound<S, P, X> {

pub fn select<'seal>(
&self,
seal: &'seal P::Seal,
seal: &'seal P::SealDef,
) -> impl Iterator<Item = (ContractId, CellAddr)> + use<'_, 'seal, S, P, X> {
self.contracts
.iter()
Expand All @@ -185,8 +185,8 @@ impl<S: Supply, P: Pile, X: Excavate<S, P>> Mound<S, P, X> {
&mut self,
contract_id: ContractId,
opid: Opid,
pub_witness: &<P::Seal as SingleUseSeal>::PubWitness,
anchor: <P::Seal as SingleUseSeal>::CliWitness,
pub_witness: &<P::SealSrc as SingleUseSeal>::PubWitness,
anchor: <P::SealSrc as SingleUseSeal>::CliWitness,
) {
self.contract_mut(contract_id)
.include(opid, anchor, pub_witness)
Expand All @@ -199,9 +199,10 @@ impl<S: Supply, P: Pile, X: Excavate<S, P>> Mound<S, P, X> {
mut writer: StrictWriter<impl WriteRaw>,
) -> io::Result<()>
where
<P::Seal as SingleUseSeal>::CliWitness: StrictDumb + StrictEncode,
<P::Seal as SingleUseSeal>::PubWitness: StrictDumb + StrictEncode,
<<P::Seal as SingleUseSeal>::PubWitness as PublishedWitness<P::Seal>>::PubId: StrictEncode,
<P::SealSrc as SingleUseSeal>::CliWitness: StrictDumb + StrictEncode,
<P::SealSrc as SingleUseSeal>::PubWitness: StrictDumb + StrictEncode,
<<P::SealSrc as SingleUseSeal>::PubWitness as PublishedWitness<P::SealSrc>>::PubId:
StrictEncode,
{
writer = MAGIC_BYTES_CONSIGNMENT.strict_encode(writer)?;
// Version
Expand All @@ -213,12 +214,13 @@ impl<S: Supply, P: Pile, X: Excavate<S, P>> Mound<S, P, X> {
pub fn consume(
&mut self,
reader: &mut StrictReader<impl ReadRaw>,
seal_resolver: impl FnMut(&Operation) -> BTreeMap<u16, P::Seal>,
) -> Result<(), MoundConsumeError<P::Seal>>
seal_resolver: impl FnMut(&Operation) -> BTreeMap<u16, P::SealDef>,
) -> Result<(), MoundConsumeError<P::SealDef>>
where
<P::Seal as SingleUseSeal>::CliWitness: StrictDecode,
<P::Seal as SingleUseSeal>::PubWitness: StrictDecode,
<<P::Seal as SingleUseSeal>::PubWitness as PublishedWitness<P::Seal>>::PubId: StrictDecode,
<P::SealSrc as SingleUseSeal>::CliWitness: StrictDecode,
<P::SealSrc as SingleUseSeal>::PubWitness: StrictDecode,
<<P::SealSrc as SingleUseSeal>::PubWitness as PublishedWitness<P::SealSrc>>::PubId:
StrictDecode,
{
let magic_bytes = Bytes16::strict_decode(reader)?;
if magic_bytes.to_byte_array() != MAGIC_BYTES_CONSIGNMENT {
Expand Down Expand Up @@ -253,7 +255,7 @@ pub enum IssueError {

#[derive(Display, From)]
#[display(doc_comments)]
pub enum MoundConsumeError<Seal: RgbSeal> {
pub enum MoundConsumeError<Seal: RgbSealDef> {
/// unrecognized magic bytes in consignment stream ({0})
UnrecognizedMagic(String),

Expand All @@ -275,22 +277,22 @@ pub mod file {

use hypersonic::expect::Expect;
use hypersonic::FileSupply;
use rgb::RgbSeal;
use rgb::RgbSealDef;
use single_use_seals::PublishedWitness;
use strict_encoding::{StreamWriter, StrictDecode, StrictEncode};

use super::*;
use crate::FilePile;

pub struct DirExcavator<Seal: RgbSeal> {
pub struct DirExcavator<SealDef: RgbSealDef> {
dir: PathBuf,
consensus: Consensus,
testnet: bool,
no_prefix: bool,
_phantom: PhantomData<Seal>,
_phantom: PhantomData<SealDef>,
}

impl<Seal: RgbSeal> DirExcavator<Seal> {
impl<SealDef: RgbSealDef> DirExcavator<SealDef> {
pub fn new(consensus: Consensus, testnet: bool, dir: PathBuf, no_prefix: bool) -> Self {
Self { dir, consensus, testnet, no_prefix, _phantom: PhantomData }
}
Expand Down Expand Up @@ -318,11 +320,12 @@ pub mod file {
}
}

impl<Seal: RgbSeal> Excavate<FileSupply, FilePile<Seal>> for DirExcavator<Seal>
impl<SealDef: RgbSealDef> Excavate<FileSupply, FilePile<SealDef>> for DirExcavator<SealDef>
where
Seal::CliWitness: StrictEncode + StrictDecode,
Seal::PubWitness: StrictEncode + StrictDecode,
<Seal::PubWitness as PublishedWitness<Seal>>::PubId: Ord + From<[u8; 32]> + Into<[u8; 32]>,
<SealDef::Src as SingleUseSeal>::CliWitness: StrictEncode + StrictDecode,
<SealDef::Src as SingleUseSeal>::PubWitness: StrictEncode + StrictDecode,
<<SealDef::Src as SingleUseSeal>::PubWitness as PublishedWitness<SealDef::Src>>::PubId:
Ord + From<[u8; 32]> + Into<[u8; 32]>,
{
fn schemata(&mut self) -> impl Iterator<Item = (CodexId, Schema)> {
self.contents(true).filter_map(|(ty, path)| {
Expand All @@ -338,7 +341,7 @@ pub mod file {

fn contracts(
&mut self,
) -> impl Iterator<Item = (ContractId, Stockpile<FileSupply, FilePile<Seal>>)> {
) -> impl Iterator<Item = (ContractId, Stockpile<FileSupply, FilePile<SealDef>>)> {
self.contents(false).filter_map(|(ty, path)| {
if ty.is_dir() && path.extension().and_then(OsStr::to_str) == Some("contract") {
let contract = Stockpile::load(path);
Expand All @@ -352,13 +355,14 @@ pub mod file {
}
}

pub type DirMound<Seal> = Mound<FileSupply, FilePile<Seal>, DirExcavator<Seal>>;
pub type DirMound<SealDef> = Mound<FileSupply, FilePile<SealDef>, DirExcavator<SealDef>>;

impl<Seal: RgbSeal> DirMound<Seal>
impl<SealDef: RgbSealDef> DirMound<SealDef>
where
Seal::CliWitness: StrictEncode + StrictDecode,
Seal::PubWitness: StrictEncode + StrictDecode,
<Seal::PubWitness as PublishedWitness<Seal>>::PubId: Ord + From<[u8; 32]> + Into<[u8; 32]>,
<SealDef::Src as SingleUseSeal>::CliWitness: StrictEncode + StrictDecode,
<SealDef::Src as SingleUseSeal>::PubWitness: StrictEncode + StrictDecode,
<<SealDef::Src as SingleUseSeal>::PubWitness as PublishedWitness<SealDef::Src>>::PubId:
Ord + From<[u8; 32]> + Into<[u8; 32]>,
{
pub fn load_testnet(consensus: Consensus, path: impl AsRef<Path>, no_prefix: bool) -> Self {
let path = path.as_ref();
Expand All @@ -368,11 +372,11 @@ pub mod file {

pub fn issue_to_file(
&mut self,
params: CreateParams<Seal>,
params: CreateParams<SealDef>,
) -> Result<ContractId, IssueError> {
let dir = self.persistence.consensus_dir();
let supply = FileSupply::new(params.name.as_str(), &dir);
let pile = FilePile::<Seal>::new(params.name.as_str(), &dir);
let pile = FilePile::<SealDef>::new(params.name.as_str(), &dir);
self.issue(params, supply, pile)
}

Expand All @@ -385,9 +389,10 @@ pub mod file {
path: impl AsRef<Path>,
) -> io::Result<()>
where
Seal::CliWitness: StrictDumb,
Seal::PubWitness: StrictDumb,
<Seal::PubWitness as PublishedWitness<Seal>>::PubId: StrictEncode,
<SealDef::Src as SingleUseSeal>::CliWitness: StrictDumb,
<SealDef::Src as SingleUseSeal>::PubWitness: StrictDumb,
<<SealDef::Src as SingleUseSeal>::PubWitness as PublishedWitness<SealDef::Src>>::PubId:
StrictEncode,
{
let file = File::create_new(path)?;
let writer = StrictWriter::with(StreamWriter::new::<{ usize::MAX }>(file));
Expand Down
Loading

0 comments on commit 0b3f910

Please sign in to comment.