Skip to content

Commit

Permalink
Merge pull request #198 from RGB-WG/develop
Browse files Browse the repository at this point in the history
Runtime refactoring and cli improvements
  • Loading branch information
dr-orlovsky committed May 6, 2024
2 parents 0f65b19 + 7c91b28 commit 5faad89
Show file tree
Hide file tree
Showing 25 changed files with 1,369 additions and 1,208 deletions.
282 changes: 135 additions & 147 deletions Cargo.lock

Large diffs are not rendered by default.

57 changes: 30 additions & 27 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@ license = "Apache-2.0"

[workspace.dependencies]
amplify = "4.6.0"
baid58 = "0.4.4"
baid64 = "0.1.0"
commit_verify = "0.11.0-beta.5"
strict_encoding = "2.7.0-beta.3"
strict_types = "2.7.0-beta.3"
bp-core = "0.11.0-beta.5"
bp-seals = "0.11.0-beta.5"
bp-std = "0.11.0-beta.5"
bp-wallet = "0.11.0-beta.5"
bp-util = "0.11.0-beta.5"
bp-electrum = "0.11.0-beta.5"
bp-esplora = "0.11.0-beta.5"
descriptors = "0.11.0-beta.5"
psbt = { version = "0.11.0-beta.5", features = ["client-side-validation"] }
bp-wallet = { version = "0.11.0-beta.5" }
rgb-std = { version = "0.11.0-beta.5", features = ["fs"] }
rgb-psbt = { version = "0.11.0-beta.5", path = "psbt" }
rgb-interfaces = "0.11.0-beta.5"
indexmap = "2.0.2"
chrono = "0.4.31"
serde_crate = { package = "serde", version = "1", features = ["derive"] }
serde_with = "3.4.0"
serde_yaml = "0.9.19"
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }

Expand All @@ -59,52 +58,56 @@ edition.workspace = true
license.workspace = true

[lib]
name = "rgb_rt"
name = "rgb"
crate-type = ["cdylib", "rlib"]

[dependencies]
amplify = { workspace = true }
baid58 = { workspace = true }
baid64 = { workspace = true }
bp-electrum = { workspace = true, optional = true }
commit_verify = { workspace = true }
strict_types = { workspace = true }
bp-core = { workspace = true }
bp-std = { workspace = true }
bp-wallet = { workspace = true, features = ["fs"] }
bp-esplora = { workspace = true, optional = true }
descriptors = { workspace = true }
bp-wallet = { workspace = true }
rgb-std = { workspace = true }
rgb-psbt = { workspace = true }
indexmap = { workspace = true }
chrono = { workspace = true }
serde_crate = { workspace = true, optional = true }
serde_yaml = { workspace = true, optional = true }
serde_with = { workspace = true, optional = true }
log = { workspace = true, optional = true }

[features]
default = ["esplora_blocking"]
all = ["esplora_blocking", "electrum", "serde", "log"]
esplora_blocking = ["bp-esplora", "bp-wallet/esplora"]
electrum = ["bp-electrum", "bp-wallet/electrum"]
serde = ["serde_crate", "serde_with", "serde_yaml", "bp-std/serde", "bp-wallet/serde", "descriptors/serde", "rgb-psbt/serde"]
all = ["esplora_blocking", "electrum_blocking", "serde", "log", "fs"]
fs = ["serde", "bp-wallet/fs"]
esplora_blocking = ["bp-esplora"]
electrum_blocking = ["bp-electrum"]
serde = ["serde_crate", "serde_yaml", "bp-std/serde", "descriptors/serde", "rgb-psbt/serde"]

[package.metadata.docs.rs]
features = ["all"]

[patch.crates-io]
commit_verify = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.11" }
single_use_seals = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.11" }
bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-seals = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-core = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" }
bp-invoice = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" }
bp-std = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" }
bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "v0.11" }
psbt = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" }
descriptors = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" }
aluvm = { git = "https://github.com/AluVM/rust-aluvm", branch = "v0.11" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "v0.11" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "v0.11" }
rgb-invoice = { git = "https://github.com/RGB-WG/rgb-std", branch = "v0.11" }
strict_encoding = { git = "https://github.com/strict-types/strict-encoding", branch = "develop" }
strict_types = { git = "https://github.com/strict-types/strict-types", branch = "develop" }
commit_verify = { git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" }
single_use_seals = { git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" }
bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "develop" }
bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "develop" }
bp-seals = { git = "https://github.com/BP-WG/bp-core", branch = "develop" }
bp-core = { git = "https://github.com/BP-WG/bp-core", branch = "develop" }
bp-invoice = { git = "https://github.com/BP-WG/bp-std", branch = "develop" }
bp-derive = { git = "https://github.com/BP-WG/bp-std", branch = "develop" }
bp-std = { git = "https://github.com/BP-WG/bp-std", branch = "develop" }
bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "develop" }
psbt = { git = "https://github.com/BP-WG/bp-std", branch = "develop" }
descriptors = { git = "https://github.com/BP-WG/bp-std", branch = "develop" }
aluvm = { git = "https://github.com/AluVM/rust-aluvm", branch = "develop" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "develop" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "develop" }
rgb-invoice = { git = "https://github.com/RGB-WG/rgb-std", branch = "develop" }
rgb-interfaces = { git = "https://github.com/RGB-WG/rgb-interfaces", branch = "develop" }
8 changes: 4 additions & 4 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ path = "src/main.rs"

[dependencies]
amplify = { workspace = true }
baid58 = { workspace = true }
baid64 = { workspace = true }
strict_types = { workspace = true, features = ["serde"] }
commit_verify = { workspace = true }
bp-seals = { workspace = true }
bp-std = { workspace = true, features = ["serde"] }
bp-wallet = { workspace = true }
bp-util = { workspace = true }
bp-wallet = { workspace = true, features = ["cli"] }
psbt = { workspace = true }
rgb-std = { workspace = true, features = ["serde"] }
rgb-runtime = { version = "0.11.0-beta.5", path = "..", features = ["electrum", "esplora_blocking", "log", "serde"] }
rgb-interfaces = { workspace = true }
rgb-runtime = { version = "0.11.0-beta.5", path = "..", features = ["electrum_blocking", "esplora_blocking", "log", "serde", "fs"] }
log = { workspace = true }
env_logger = "0.10.1"
clap = { version = "4.4.8", features = ["derive", "env"] }
Expand Down
85 changes: 55 additions & 30 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::needless_update)] // Caused by the From derivation macro
#![allow(clippy::needless_update)] // Required by From derive macro

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

use bp_util::{Config, DescriptorOpts};
use bpstd::{Wpkh, XpubDerivable};
use rgb_rt::{
electrum, esplora_blocking, AnyResolver, AnyResolverError, RgbDescr, Runtime, RuntimeError,
TapretKey,
};
use bpwallet::cli::{Args as BpArgs, Config, DescriptorOpts};
use bpwallet::Wallet;
use rgb::{AnyResolver, RgbDescr, StoredStock, StoredWallet, TapretKey, WalletError};
use rgbstd::persistence::fs::{LoadFs, StoreFs};
use rgbstd::persistence::Stock;
use strict_types::encoding::{DecodeError, DeserializeError};

use crate::Command;

Expand Down Expand Up @@ -65,43 +69,64 @@ impl DescriptorOpts for DescrRgbOpts {
#[command(author, version, about)]
pub struct RgbArgs {
#[clap(flatten)]
pub inner: bp_util::Args<Command, DescrRgbOpts>,
pub inner: BpArgs<Command, DescrRgbOpts>,
}

impl Default for RgbArgs {
fn default() -> Self { unreachable!() }
}

impl RgbArgs {
pub fn rgb_stock(&self) -> Result<Stock, RuntimeError> {
eprint!("Loading stock ... ");
let runtime = Runtime::<RgbDescr>::load_walletless(&self.general.base_dir())?;
eprintln!("success");
fn load_stock(&self, stock_path: &Path) -> Result<Stock, WalletError> {
if self.verbose > 1 {
eprint!("Loading stock ... ");
}

Ok(runtime)
Stock::load(&stock_path).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)?;
if self.verbose > 1 {
eprintln!("success");
}
return Ok(stock)
}
eprintln!("stock file is damaged, failing");
Err(err)
})
}

pub fn rgb_runtime(&self, config: &Config) -> Result<Runtime, RuntimeError> {
let bprt = self.inner.bp_runtime::<RgbDescr>(config)?;
eprint!("Loading stock ... ");
let runtime = Runtime::<RgbDescr>::load_attach(self.general.base_dir(), bprt)?;
eprintln!("success");
pub fn rgb_stock(&self) -> Result<StoredStock, WalletError> {
let stock_path = self.general.base_dir();
let stock = self.load_stock(&stock_path)?;
Ok(StoredStock::attach(stock_path, stock))
}

Ok(runtime)
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 wallet = self.inner.bp_runtime::<RgbDescr>(config)?;
let wallet_path = wallet.path().clone();
let wallet = StoredWallet::attach(stock_path, wallet_path, stock, wallet.detach());

Ok(wallet)
}

#[allow(clippy::result_large_err)]
pub fn resolver(&self) -> Result<AnyResolver, AnyResolverError> {
if self.resolver.electrum != bp_util::DEFAULT_ELECTRUM {
match electrum::Resolver::new(&self.resolver.electrum) {
Ok(c) => Ok(AnyResolver::Electrum(Box::new(c))),
Err(e) => Err(AnyResolverError::Electrum(e)),
}
} else {
match esplora_blocking::Resolver::new(&self.resolver.esplora) {
Ok(c) => Ok(AnyResolver::Esplora(Box::new(c))),
Err(e) => Err(AnyResolverError::Esplora(e)),
}
pub fn resolver(&self) -> Result<AnyResolver, WalletError> {
let resolver = match (&self.resolver.esplora, &self.resolver.electrum) {
(None, Some(url)) => AnyResolver::electrum_blocking(url),
(Some(url), None) => AnyResolver::esplora_blocking(url),
_ => unreachable!("clap is broken"),
}
.map_err(WalletError::Resolver)?;
resolver.check(self.general.network)?;
Ok(resolver)
}
}
Loading

0 comments on commit 5faad89

Please sign in to comment.