Skip to content

Commit

Permalink
refactor fs workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 6, 2024
1 parent 08ec7a6 commit 5f1d047
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 835 deletions.
650 changes: 12 additions & 638 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ features = ["all"]

[patch.crates-io]
bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "runtime" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "master" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "runtime" }
30 changes: 15 additions & 15 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@

use std::fs;
use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;

use bpstd::{Wpkh, XpubDerivable};
use bpwallet::cli::{Args as BpArgs, Config, DescriptorOpts};
use bpwallet::Wallet;
use rgb::persistence::fs::{LoadFs, StoreFs};
use rgb::persistence::Stock;
use rgb::resolvers::AnyResolver;
use rgb::{RgbDescr, StoredStock, StoredWallet, TapretKey, WalletError};
use rgb::{RgbDescr, StoredWallet, TapretKey, WalletError};
use strict_types::encoding::{DecodeError, DeserializeError};

use crate::Command;
Expand Down Expand Up @@ -78,19 +77,22 @@ impl Default for RgbArgs {
}

impl RgbArgs {
pub(crate) fn load_stock(&self, stock_path: &Path) -> Result<Stock, WalletError> {
pub(crate) fn load_stock(
&self,
stock_path: impl ToOwned<Owned = PathBuf>,
) -> Result<Stock, WalletError> {
if self.verbose > 1 {
eprint!("Loading stock ... ");
}

Stock::load(stock_path).map_err(WalletError::from).or_else(|err| {
Stock::load(stock_path.to_owned()).map_err(WalletError::from).or_else(|err| {
if matches!(err, WalletError::Deserialize(DeserializeError::Decode(DecodeError::Io(ref err))) if err.kind() == ErrorKind::NotFound) {
if self.verbose > 1 {
eprint!("stock file is absent, creating a new one ... ");
}
let stock = Stock::default();
fs::create_dir_all(stock_path)?;
stock.store(stock_path)?;
fs::create_dir_all(stock_path.to_owned())?;
let stock = Stock::new(stock_path.to_owned());
stock.store()?;
if self.verbose > 1 {
eprintln!("success");
}
Expand All @@ -101,18 +103,18 @@ impl RgbArgs {
})
}

pub fn rgb_stock(&self) -> Result<StoredStock, WalletError> {
pub fn rgb_stock(&self) -> Result<Stock, WalletError> {
let stock_path = self.general.base_dir();
let stock = self.load_stock(&stock_path)?;
Ok(StoredStock::attach(stock_path, stock))
let stock = self.load_stock(stock_path)?;
Ok(stock)
}

pub fn rgb_wallet(
&self,
config: &Config,
) -> Result<StoredWallet<Wallet<XpubDerivable, RgbDescr>>, WalletError> {
let stock_path = self.general.base_dir();
let stock = self.load_stock(&stock_path)?;
let stock = self.load_stock(stock_path)?;
self.rgb_wallet_from_stock(config, stock)
}

Expand All @@ -121,10 +123,8 @@ impl RgbArgs {
config: &Config,
stock: Stock,
) -> Result<StoredWallet<Wallet<XpubDerivable, RgbDescr>>, WalletError> {
let stock_path = self.general.base_dir();
let wallet = self.inner.bp_wallet::<RgbDescr>(config)?;
let wallet_path = wallet.path().clone();
let wallet = StoredWallet::attach(stock_path, wallet_path, stock, wallet.detach());
let wallet = StoredWallet::new(stock, wallet);

Ok(wallet)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use rgb::containers::{
};
use rgb::interface::{AmountChange, IfaceId};
use rgb::invoice::{Beneficiary, Pay2Vout, RgbInvoice, RgbInvoiceBuilder, XChainNet};
use rgb::persistence::StashReadProvider;
use rgb::schema::SchemaId;
use rgb::validation::Validity;
use rgb::vm::RgbIsa;
Expand All @@ -47,6 +46,7 @@ use rgb::{
XOutpoint, XOutputSeal,
};
use rgbstd::interface::OutpointFilter;
use rgbstd::persistence::StashReadProvider;
use serde_crate::{Deserialize, Serialize};
use strict_types::encoding::{FieldName, TypeName};
use strict_types::StrictVal;
Expand Down Expand Up @@ -485,7 +485,7 @@ impl Exec for RgbArgs {
all,
} => {
let stock_path = self.general.base_dir();
let stock = self.load_stock(&stock_path)?;
let stock = self.load_stock(stock_path)?;

let contract = stock.contract_iface(*contract_id, tn!(iface.to_owned()))?;

Expand Down
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub enum WalletError {

#[cfg(feature = "fs")]
#[from]
#[from(bpwallet::LoadError)]
Bp(bpwallet::WalletError),
#[from(bpwallet::fs::LoadError)]
Bp(bpwallet::cli::ExecError),

/// resolver error: {0}
#[display(doc_comments)]
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ mod indexers;
mod wallet;
pub mod pay;
mod errors;
#[cfg(feature = "fs")]
mod store;

pub use descriptor::{DescriptorRgb, RgbDescr, RgbKeychain, TapTweakAlreadyAssigned, TapretKey};
Expand All @@ -44,6 +43,5 @@ pub mod resolvers {
pub use super::indexers::*;
pub use super::indexers::{AnyResolver, RgbResolver};
}
#[cfg(feature = "fs")]
pub use store::{StoredStock, StoredWallet};
pub use store::StoredWallet;
pub use wallet::{WalletStock, WalletWrapper};
16 changes: 9 additions & 7 deletions src/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

use std::collections::{BTreeMap, BTreeSet};
use std::marker::PhantomData;
use std::ops::DerefMut;

use bp::dbc::tapret::TapretProof;
use bp::seals::txout::ExplicitSeal;
use bp::{Outpoint, Sats, ScriptPubkey, Vout};
use bpstd::{psbt, Address};
use bpwallet::Wallet;
use bpwallet::{Save, Wallet, WalletDescr};
use psrgbt::{
Beneficiary as BpBeneficiary, Psbt, PsbtConstructor, PsbtMeta, RgbPsbt, TapretKeyError,
TxParams,
Expand Down Expand Up @@ -99,7 +98,7 @@ where Self::Descr: DescriptorRgb<K>
type Filter<'a>: Copy + WitnessFilter + OutpointFilter
where Self: 'a;
fn filter(&self) -> Self::Filter<'_>;
fn descriptor_mut(&mut self) -> &mut Self::Descr;
fn descriptor_mut<R>(&mut self, f: impl FnOnce(&mut WalletDescr<K, Self::Descr>) -> R) -> R;
fn outpoints(&self) -> impl Iterator<Item = Outpoint>;
fn txids(&self) -> impl Iterator<Item = Txid>;

Expand Down Expand Up @@ -291,8 +290,7 @@ where Self::Descr: DescriptorRgb<K>
.terminal_derivation()
.ok_or(CompletionError::InconclusiveDerivation)?;
let tapret_commitment = output.tapret_commitment()?;
self.descriptor_mut()
.add_tapret_tweak(terminal, tapret_commitment)?;
self.descriptor_mut(|descr| descr.add_tapret_tweak(terminal, tapret_commitment))?;
}

let witness_txid = psbt.txid();
Expand Down Expand Up @@ -322,10 +320,14 @@ where Self::Descr: DescriptorRgb<K>
}
}

impl<K, D: DescriptorRgb<K>> WalletProvider<K> for Wallet<K, D> {
impl<K, D: DescriptorRgb<K>> WalletProvider<K> for Wallet<K, D>
where Wallet<K, D>: Save
{
type Filter<'a> = WalletWrapper<'a, K, D> where Self: 'a;
fn filter(&self) -> Self::Filter<'_> { WalletWrapper(self) }
fn descriptor_mut(&mut self) -> &mut Self::Descr { self.deref_mut() }
fn descriptor_mut<R>(&mut self, f: impl FnOnce(&mut WalletDescr<K, D>) -> R) -> R {
self.descriptor_mut(f)
}
fn outpoints(&self) -> impl Iterator<Item = Outpoint> { self.coins().map(|coin| coin.outpoint) }
fn txids(&self) -> impl Iterator<Item = Txid> { self.transactions().keys().copied() }
}
Loading

0 comments on commit 5f1d047

Please sign in to comment.