Skip to content

Commit

Permalink
fix: genesis import and reduce esplora calls
Browse files Browse the repository at this point in the history
  • Loading branch information
crisdut committed Aug 2, 2023
1 parent a16b40b commit 1dec8cf
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 44 deletions.
83 changes: 68 additions & 15 deletions src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ use crate::{

use self::{
carbonado::{retrieve_wallets, store_wallets},
constants::{CARBONADO_UNAVAILABLE, RGB_DEFAULT_NAME, STOCK_UNAVAILABLE},
constants::{
BITCOIN_DEFAULT_FETCH_LIMIT, CARBONADO_UNAVAILABLE, RGB_DEFAULT_FETCH_LIMIT,
RGB_DEFAULT_NAME, STOCK_UNAVAILABLE,
},
contract::{export_contract, ExportContractError},
import::{import_contract, ImportContractError},
prefetch::{
Expand Down Expand Up @@ -145,8 +148,13 @@ pub async fn issue_contract(sk: &str, request: IssueRequest) -> Result<IssueResp
Some(wallet) => {
let mut fetch_wallet = wallet.to_owned();
for contract_type in [AssetType::RGB20, AssetType::RGB21] {
prefetch_resolver_utxos(contract_type as u32, &mut fetch_wallet, &mut resolver)
.await;
prefetch_resolver_utxos(
contract_type as u32,
&mut fetch_wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
}

Some(fetch_wallet)
Expand Down Expand Up @@ -332,8 +340,13 @@ pub async fn reissue_contract(
Some(wallet) => {
let mut fetch_wallet = wallet.to_owned();
for contract_type in [AssetType::RGB20, AssetType::RGB21] {
prefetch_resolver_utxos(contract_type as u32, &mut fetch_wallet, &mut resolver)
.await;
prefetch_resolver_utxos(
contract_type as u32,
&mut fetch_wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
}

Some(fetch_wallet)
Expand Down Expand Up @@ -816,7 +829,13 @@ pub async fn full_transfer_asset(
let mut wallet = wallet.unwrap();
let mut all_unspents = vec![];
for bitcoin_index in bitcoin_indexes {
prefetch_resolver_utxos(bitcoin_index, &mut wallet, &mut resolver).await;
prefetch_resolver_utxos(
bitcoin_index,
&mut wallet,
&mut resolver,
Some(BITCOIN_DEFAULT_FETCH_LIMIT),
)
.await;
prefetch_resolver_utxo_status(bitcoin_index, &mut wallet, &mut resolver).await;

sync_wallet(bitcoin_index, &mut wallet, &mut resolver);
Expand Down Expand Up @@ -940,8 +959,13 @@ pub async fn get_contract(sk: &str, contract_id: &str) -> Result<ContractRespons
Some(wallet) => {
let mut fetch_wallet = wallet.to_owned();
for contract_type in [AssetType::RGB20, AssetType::RGB21] {
prefetch_resolver_utxos(contract_type as u32, &mut fetch_wallet, &mut resolver)
.await;
prefetch_resolver_utxos(
contract_type as u32,
&mut fetch_wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
}

Some(fetch_wallet)
Expand Down Expand Up @@ -977,8 +1001,13 @@ pub async fn list_contracts(sk: &str) -> Result<ContractsResponse> {
Some(wallet) => {
let mut fetch_wallet = wallet.to_owned();
for contract_type in [AssetType::RGB20, AssetType::RGB21] {
prefetch_resolver_utxos(contract_type as u32, &mut fetch_wallet, &mut resolver)
.await;
prefetch_resolver_utxos(
contract_type as u32,
&mut fetch_wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
}
Some(fetch_wallet)
}
Expand Down Expand Up @@ -1086,7 +1115,13 @@ pub async fn import(sk: &str, request: ImportRequest) -> Result<ContractResponse
let mut wallet = match wallet {
Some(wallet) => {
let mut fetch_wallet = wallet.to_owned();
prefetch_resolver_utxos(import.clone() as u32, &mut fetch_wallet, &mut resolver).await;
prefetch_resolver_utxos(
import.clone() as u32,
&mut fetch_wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
Some(fetch_wallet)
}
_ => None,
Expand Down Expand Up @@ -1206,7 +1241,13 @@ pub async fn watcher_details(sk: &str, name: &str) -> Result<WatcherDetailRespon
let mut allocations = vec![];
for contract_type in [AssetType::RGB20, AssetType::RGB21] {
let iface_index = contract_type as u32;
prefetch_resolver_utxos(iface_index, &mut wallet, &mut resolver).await;
prefetch_resolver_utxos(
iface_index,
&mut wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
prefetch_resolver_utxo_status(iface_index, &mut wallet, &mut resolver).await;
let mut result = list_allocations(&mut wallet, &mut stock, iface_index, &mut resolver)?;
allocations.append(&mut result);
Expand Down Expand Up @@ -1273,7 +1314,7 @@ pub async fn watcher_utxo(sk: &str, name: &str, utxo: &str) -> Result<WatcherUtx
asset_indexes,
&mut wallet,
&mut resolver,
Some(20),
Some(RGB_DEFAULT_FETCH_LIMIT),
)?
.into_iter()
.map(|utxo| utxo.outpoint.to_string())
Expand Down Expand Up @@ -1336,7 +1377,13 @@ pub async fn watcher_next_utxo(sk: &str, name: &str, iface: &str) -> Result<Next
..Default::default()
};

prefetch_resolver_utxos(iface_index, &mut wallet, &mut resolver).await;
prefetch_resolver_utxos(
iface_index,
&mut wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
prefetch_resolver_utxo_status(iface_index, &mut wallet, &mut resolver).await;

sync_wallet(iface_index, &mut wallet, &mut resolver);
Expand Down Expand Up @@ -1377,7 +1424,13 @@ pub async fn watcher_unspent_utxos(sk: &str, name: &str, iface: &str) -> Result<
..Default::default()
};

prefetch_resolver_utxos(iface_index, &mut wallet, &mut resolver).await;
prefetch_resolver_utxos(
iface_index,
&mut wallet,
&mut resolver,
Some(RGB_DEFAULT_FETCH_LIMIT),
)
.await;
prefetch_resolver_utxo_status(iface_index, &mut wallet, &mut resolver).await;

sync_wallet(iface_index, &mut wallet, &mut resolver);
Expand Down
2 changes: 2 additions & 0 deletions src/rgb/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub const RGB_PSBT_TAPRET: &str = "TAPRET";
pub const RGB_DEFAULT_NAME: &str = "default";
pub const RGB_OLDEST_VERSION: [u8; 8] = [0; 8];
pub const RGB_STRICT_TYPE_VERSION: [u8; 8] = *b"rgbst161";
pub const RGB_DEFAULT_FETCH_LIMIT: u32 = 10;
pub const BITCOIN_DEFAULT_FETCH_LIMIT: u32 = 20;

// General Errors
#[cfg(target_arch = "wasm32")]
Expand Down
29 changes: 16 additions & 13 deletions src/rgb/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use amplify::{
hex::FromHex,
};
use bech32::{decode, FromBase32};
use rgb_schemata::{nia_schema, uda_schema};
use rgb_schemata::{nia_rgb20, nia_schema, uda_rgb21, uda_schema};
use rgbstd::{
containers::Contract,
contract::Genesis,
interface::{rgb20, rgb21},
interface::{rgb20, rgb21, IfacePair},
persistence::{Inventory, Stash, Stock},
resolvers::ResolveHeight,
validation::ResolveTx,
Expand Down Expand Up @@ -70,19 +70,22 @@ pub fn contract_from_genesis(
asset_type: AssetType,
stock: Option<&mut Stock>,
) -> Contract {
let schema = match asset_type {
AssetType::RGB20 => nia_schema(),
AssetType::RGB21 => uda_schema(),
_ => nia_schema(),
let (schema, iface, iimpl) = match asset_type {
AssetType::RGB20 => (nia_schema(), rgb20(), nia_rgb20()),
AssetType::RGB21 => (uda_schema(), rgb21(), uda_rgb21()),
_ => (nia_schema(), rgb20(), nia_rgb20()),
};

if let Some(stock) = stock {
match asset_type {
AssetType::RGB20 => stock.import_iface(rgb20()),
AssetType::RGB21 => stock.import_iface(rgb21()),
_ => stock.import_iface(rgb20()),
}
.expect("import iface failed");
stock
.import_iface(iface.clone())
.expect("import iface failed");
}
Contract::new(schema, genesis)
let mut contract = Contract::new(schema, genesis);
contract
.ifaces
.insert(iface.iface_id(), IfacePair::with(iface, iimpl))
.expect("import iface pair failed");

contract
}
10 changes: 8 additions & 2 deletions src/rgb/prefetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub async fn prefetch_resolver_utxos(
iface_index: u32,
wallet: &mut RgbWallet,
explorer: &mut ExplorerResolver,
limit: Option<u32>,
) {
}

Expand Down Expand Up @@ -290,14 +291,19 @@ pub async fn prefetch_resolver_utxos(
iface_index: u32,
wallet: &mut RgbWallet,
explorer: &mut ExplorerResolver,
limit: Option<u32>,
) {
use std::collections::HashSet;

let esplora_client: EsploraBlockchain =
EsploraBlockchain::new(&explorer.explorer_url, 1).with_concurrency(6);

let step = 100;
let index = 0;
let mut step = 100;
if let Some(limit) = limit {
step = limit;
}

let mut utxos = bset![];

let scripts = wallet.descr.derive(iface_index, index..step);
Expand Down Expand Up @@ -395,11 +401,11 @@ pub async fn prefetch_resolver_waddress(
let esplora_client: EsploraBlockchain =
EsploraBlockchain::new(&explorer.explorer_url, 1).with_concurrency(6);

let index = 0;
let mut step = 100;
if let Some(limit) = limit {
step = limit;
}
let index = 0;

let sc = AddressCompat::from_str(address).expect("invalid address");
let script = ScriptBuf::from_hex(&sc.script_pubkey().to_hex()).expect("invalid script");
Expand Down
29 changes: 24 additions & 5 deletions tests/rgb/integration/import.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
#![cfg(not(target_arch = "wasm32"))]
use crate::rgb::integration::utils::ISSUER_MNEMONIC;
use crate::rgb::integration::utils::{issuer_issue_contract, ISSUER_MNEMONIC};
use bitmask_core::{
bitcoin::save_mnemonic,
bitcoin::{new_mnemonic, save_mnemonic},
rgb::import,
structs::{AssetType, ImportRequest, SecretString},
};

#[tokio::test]
async fn allow_import_fungibles_from_genesis() -> anyhow::Result<()> {
let issuer_resp = issuer_issue_contract("RGB20", 5, false, true, None).await;
assert!(issuer_resp.is_ok());

let another_vault = new_mnemonic(&SecretString("".to_string())).await?;

let sk = &another_vault.private.nostr_prv;
let contract_import = ImportRequest {
import: AssetType::RGB20,
data: issuer_resp?.genesis.strict,
};

let import_resp = import(sk, contract_import).await;
assert!(import_resp.is_ok());

Ok(())
}

#[tokio::test]
async fn allow_import_fungibles_from_genesis_data() -> anyhow::Result<()> {
let issuer_keys = save_mnemonic(
&SecretString(ISSUER_MNEMONIC.to_string()),
&SecretString("".to_string()),
Expand All @@ -26,7 +45,7 @@ async fn allow_import_fungibles_from_genesis() -> anyhow::Result<()> {
}

#[tokio::test]
async fn allow_import_uda_from_genesis() -> anyhow::Result<()> {
async fn allow_import_uda_from_genesis_data() -> anyhow::Result<()> {
let issuer_keys = save_mnemonic(
&SecretString(ISSUER_MNEMONIC.to_string()),
&SecretString("".to_string()),
Expand All @@ -45,7 +64,7 @@ async fn allow_import_uda_from_genesis() -> anyhow::Result<()> {
}

#[tokio::test]
async fn allow_import_fungible_contract() -> anyhow::Result<()> {
async fn allow_import_fungible_contract_data() -> anyhow::Result<()> {
let issuer_keys = save_mnemonic(
&SecretString(ISSUER_MNEMONIC.to_string()),
&SecretString("".to_string()),
Expand All @@ -64,7 +83,7 @@ async fn allow_import_fungible_contract() -> anyhow::Result<()> {
}

#[tokio::test]
async fn allow_import_uda_contract() -> anyhow::Result<()> {
async fn allow_import_uda_contract_data() -> anyhow::Result<()> {
let issuer_keys = save_mnemonic(
&SecretString(ISSUER_MNEMONIC.to_string()),
&SecretString("".to_string()),
Expand Down
Loading

0 comments on commit 1dec8cf

Please sign in to comment.