Skip to content

Commit 23bd2c7

Browse files
committed
optimize sync + add options to pub sync
1 parent 4478bfd commit 23bd2c7

38 files changed

Lines changed: 788 additions & 394 deletions

bindings/c-ffi/example.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ int main() {
163163
return EXIT_FAILURE;
164164
}
165165

166-
CResultString sync_res = rgblib_sync(wlt, online);
166+
CResultString sync_res = rgblib_sync(
167+
wlt, online, "{\"keychain\":\"Colored\",\"type\":\"FullSync\"}");
167168
if (sync_res.result == Ok) {
168169
printf("Synced\n");
169170
} else {

bindings/c-ffi/src/lib.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rgb_lib::{
1616
keys::WitnessVersion,
1717
utils::BitcoinNetwork,
1818
wallet::{
19-
Online, Recipient, RefreshFilter, RgbWalletOpsOffline, RgbWalletOpsOnline, SinglesigKeys,
20-
Wallet, WalletData,
19+
Online, OnlineOptions, Recipient, RefreshFilter, RgbWalletOpsOffline, RgbWalletOpsOnline,
20+
SinglesigKeys, SyncOptions, Wallet, WalletData,
2121
},
2222
};
2323

@@ -124,9 +124,8 @@ pub extern "C" fn rgblib_create_utxos_end(
124124
wallet: &COpaqueStruct,
125125
online: *const c_char,
126126
signed_psbt: *const c_char,
127-
skip_sync: bool,
128127
) -> CResultString {
129-
create_utxos_end(wallet, online, signed_psbt, skip_sync).into()
128+
create_utxos_end(wallet, online, signed_psbt).into()
130129
}
131130

132131
#[unsafe(no_mangle)]
@@ -214,10 +213,9 @@ pub extern "C" fn rgblib_get_fee_estimation(
214213
#[unsafe(no_mangle)]
215214
pub extern "C" fn rgblib_go_online(
216215
wallet: &COpaqueStruct,
217-
skip_consistency_check: bool,
218-
electrum_url: *const c_char,
216+
online_options: *const c_char,
219217
) -> CResultString {
220-
go_online(wallet, skip_consistency_check, electrum_url).into()
218+
go_online(wallet, online_options).into()
221219
}
222220

223221
#[unsafe(no_mangle)]
@@ -390,7 +388,6 @@ pub extern "C" fn rgblib_send(
390388
fee_rate: *const c_char,
391389
min_confirmations: *const c_char,
392390
expiration_timestamp_opt: *const c_char,
393-
skip_sync: bool,
394391
) -> CResultString {
395392
send(
396393
wallet,
@@ -400,7 +397,6 @@ pub extern "C" fn rgblib_send(
400397
fee_rate,
401398
min_confirmations,
402399
expiration_timestamp_opt,
403-
skip_sync,
404400
)
405401
.into()
406402
}
@@ -446,9 +442,8 @@ pub extern "C" fn rgblib_send_end(
446442
wallet: &COpaqueStruct,
447443
online: *const c_char,
448444
signed_psbt: *const c_char,
449-
skip_sync: bool,
450445
) -> CResultString {
451-
send_end(wallet, online, signed_psbt, skip_sync).into()
446+
send_end(wallet, online, signed_psbt).into()
452447
}
453448

454449
#[unsafe(no_mangle)]
@@ -460,8 +455,12 @@ pub extern "C" fn rgblib_sign_psbt(
460455
}
461456

462457
#[unsafe(no_mangle)]
463-
pub extern "C" fn rgblib_sync(wallet: &COpaqueStruct, online: *const c_char) -> CResultString {
464-
sync(wallet, online).into()
458+
pub extern "C" fn rgblib_sync(
459+
wallet: &COpaqueStruct,
460+
online: *const c_char,
461+
options: *const c_char,
462+
) -> CResultString {
463+
sync(wallet, online, options).into()
465464
}
466465

467466
#[unsafe(no_mangle)]

bindings/c-ffi/src/utils.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,11 @@ pub(crate) fn create_utxos_end(
252252
wallet: &COpaqueStruct,
253253
online: *const c_char,
254254
signed_psbt: *const c_char,
255-
skip_sync: bool,
256255
) -> Result<String, Error> {
257256
let wallet = Wallet::from_opaque(wallet)?;
258257
let online = convert_online(online)?;
259258
let signed_psbt = ptr_to_string(signed_psbt);
260-
let res = wallet.create_utxos_end(online, signed_psbt, skip_sync)?;
259+
let res = wallet.create_utxos_end(online, signed_psbt)?;
261260
Ok(serde_json::to_string(&res)?)
262261
}
263262

@@ -355,11 +354,11 @@ pub(crate) fn get_fee_estimation(
355354

356355
pub(crate) fn go_online(
357356
wallet: &COpaqueStruct,
358-
skip_consistency_check: bool,
359-
electrum_url: *const c_char,
357+
online_options: *const c_char,
360358
) -> Result<String, Error> {
361359
let wallet = Wallet::from_opaque(wallet)?;
362-
let res = wallet.go_online(skip_consistency_check, ptr_to_string(electrum_url))?;
360+
let online_options: OnlineOptions = serde_json::from_str(&ptr_to_string(online_options))?;
361+
let res = wallet.go_online(online_options)?;
363362
Ok(serde_json::to_string(&res)?)
364363
}
365364

@@ -581,7 +580,6 @@ pub(crate) fn send(
581580
fee_rate: *const c_char,
582581
min_confirmations: *const c_char,
583582
expiration_timestamp_opt: *const c_char,
584-
skip_sync: bool,
585583
) -> Result<String, Error> {
586584
let wallet = Wallet::from_opaque(wallet)?;
587585
let online = convert_online(online)?;
@@ -597,7 +595,6 @@ pub(crate) fn send(
597595
fee_rate,
598596
min_confirmations,
599597
expiration_timestamp,
600-
skip_sync,
601598
)?;
602599
Ok(serde_json::to_string(&res)?)
603600
}
@@ -653,12 +650,11 @@ pub(crate) fn send_end(
653650
wallet: &COpaqueStruct,
654651
online: *const c_char,
655652
signed_psbt: *const c_char,
656-
skip_sync: bool,
657653
) -> Result<String, Error> {
658654
let wallet = Wallet::from_opaque(wallet)?;
659655
let online = convert_online(online)?;
660656
let signed_psbt = ptr_to_string(signed_psbt);
661-
let res = wallet.send_end(online, signed_psbt, skip_sync)?;
657+
let res = wallet.send_end(online, signed_psbt)?;
662658
Ok(serde_json::to_string(&res)?)
663659
}
664660

@@ -671,10 +667,15 @@ pub(crate) fn sign_psbt(
671667
Ok(wallet.sign_psbt(unsigned_psbt, None)?)
672668
}
673669

674-
pub(crate) fn sync(wallet: &COpaqueStruct, online: *const c_char) -> Result<(), Error> {
670+
pub(crate) fn sync(
671+
wallet: &COpaqueStruct,
672+
online: *const c_char,
673+
options: *const c_char,
674+
) -> Result<(), Error> {
675675
let wallet = Wallet::from_opaque(wallet)?;
676676
let online = convert_online(online)?;
677-
wallet.sync(online)?;
677+
let options: SyncOptions = serde_json::from_str(&ptr_to_string(options))?;
678+
wallet.sync(online, options)?;
678679
Ok(())
679680
}
680681

bindings/uniffi/src/lib.rs

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,60 @@ use rgb_lib::{
1616
AssignmentsCollection, Balance, BlockTime, BtcBalance, Cosigner as CosignerData,
1717
DatabaseType, EmbeddedMedia, HubInfo, InflateBeginResult, InflateDetails,
1818
InitOperationResult, Invoice as RgbLibInvoice, InvoiceData as RgbLibInvoiceData, Media,
19-
Metadata, MultisigKeys, MultisigVotingStatus as RgbLibMultisigVotingStatus,
20-
MultisigWallet as RgbLibMultisigWallet, Online, Operation as RgbLibOperation,
21-
OperationInfo as RgbLibOperationInfo, OperationResult, Outpoint, ProofOfReserves,
22-
PsbtInputInfo, PsbtInspection, PsbtOutputInfo, ReceiveData, Recipient as RgbLibRecipient,
23-
RecipientInfo as RgbLibRecipientInfo, RecipientType, RefreshFilter, RefreshTransferStatus,
24-
RefreshedTransfer, RespondToOperation as RgbLibRespondToOperation,
25-
RgbAllocation as RgbLibRgbAllocation, RgbInputInfo as RgbLibRgbInputInfo,
26-
RgbInspection as RgbLibRgbInspection, RgbOperationInfo as RgbLibRgbOperationInfo,
27-
RgbOutputInfo as RgbLibRgbOutputInfo, RgbTransitionInfo as RgbLibRgbTransitionInfo,
28-
RgbWalletOpsOffline, RgbWalletOpsOnline, SendBeginResult, SendDetails, SinglesigKeys,
29-
Token, TokenLight, Transaction, TransactionType, Transfer as RgbLibTransfer, TransferKind,
30-
TransferTransportEndpoint, TransportEndpoint as RgbLibTransportEndpoint, TypeOfTransition,
31-
Unspent as RgbLibUnspent, UserRole, Utxo, Wallet as RgbLibWallet, WalletData,
32-
WalletDescriptors, WitnessData,
19+
Metadata, MultisigKeys, MultisigOnlineOptions,
20+
MultisigVotingStatus as RgbLibMultisigVotingStatus, MultisigWallet as RgbLibMultisigWallet,
21+
Online, OnlineOptions, Operation as RgbLibOperation, OperationInfo as RgbLibOperationInfo,
22+
OperationResult, Outpoint, ProofOfReserves, PsbtInputInfo, PsbtInspection, PsbtOutputInfo,
23+
ReceiveData, Recipient as RgbLibRecipient, RecipientInfo as RgbLibRecipientInfo,
24+
RecipientType, RefreshFilter, RefreshTransferStatus, RefreshedTransfer,
25+
RespondToOperation as RgbLibRespondToOperation, RgbAllocation as RgbLibRgbAllocation,
26+
RgbInputInfo as RgbLibRgbInputInfo, RgbInspection as RgbLibRgbInspection,
27+
RgbOperationInfo as RgbLibRgbOperationInfo, RgbOutputInfo as RgbLibRgbOutputInfo,
28+
RgbTransitionInfo as RgbLibRgbTransitionInfo, RgbWalletOpsOffline, RgbWalletOpsOnline,
29+
SendBeginResult, SendDetails, SinglesigKeys, SyncKeychain as RgbLibSyncKeychain,
30+
SyncOptions as RgbLibSyncOptions, SyncStrategy, Token, TokenLight, Transaction,
31+
TransactionType, Transfer as RgbLibTransfer, TransferKind, TransferTransportEndpoint,
32+
TransportEndpoint as RgbLibTransportEndpoint, TypeOfTransition, Unspent as RgbLibUnspent,
33+
UserRole, Utxo, Wallet as RgbLibWallet, WalletData, WalletDescriptors, WitnessData,
3334
},
3435
};
3536

3637
uniffi::include_scaffolding!("rgb-lib");
3738

39+
// temporary solution needed because the Enum attribute doesn't support the Remote one
40+
pub enum SyncKeychain {
41+
Colored,
42+
Vanilla { lookback: u32 },
43+
}
44+
impl From<RgbLibSyncKeychain> for SyncKeychain {
45+
fn from(orig: RgbLibSyncKeychain) -> Self {
46+
match orig {
47+
RgbLibSyncKeychain::Colored => SyncKeychain::Colored,
48+
RgbLibSyncKeychain::Vanilla { lookback } => SyncKeychain::Vanilla { lookback },
49+
}
50+
}
51+
}
52+
impl From<SyncKeychain> for RgbLibSyncKeychain {
53+
fn from(orig: SyncKeychain) -> Self {
54+
match orig {
55+
SyncKeychain::Colored => RgbLibSyncKeychain::Colored,
56+
SyncKeychain::Vanilla { lookback } => RgbLibSyncKeychain::Vanilla { lookback },
57+
}
58+
}
59+
}
60+
pub struct SyncOptions {
61+
pub keychain: SyncKeychain,
62+
pub strategy: SyncStrategy,
63+
}
64+
impl From<SyncOptions> for RgbLibSyncOptions {
65+
fn from(orig: SyncOptions) -> Self {
66+
Self {
67+
keychain: orig.keychain.into(),
68+
strategy: orig.strategy,
69+
}
70+
}
71+
}
72+
3873
// temporary solution needed because the Enum attribute doesn't support the Remote one
3974
pub enum Assignment {
4075
Fungible { amount: u64 },
@@ -938,14 +973,8 @@ impl Wallet {
938973
.create_utxos_begin(online, up_to, num, size, fee_rate, skip_sync, dry_run)
939974
}
940975

941-
fn create_utxos_end(
942-
&self,
943-
online: Online,
944-
signed_psbt: String,
945-
skip_sync: bool,
946-
) -> Result<u8, RgbLibError> {
947-
self._get_wallet()
948-
.create_utxos_end(online, signed_psbt, skip_sync)
976+
fn create_utxos_end(&self, online: Online, signed_psbt: String) -> Result<u8, RgbLibError> {
977+
self._get_wallet().create_utxos_end(online, signed_psbt)
949978
}
950979

951980
fn delete_transfers(
@@ -1016,13 +1045,8 @@ impl Wallet {
10161045
self._get_wallet().get_fee_estimation(online, blocks)
10171046
}
10181047

1019-
fn go_online(
1020-
&self,
1021-
skip_consistency_check: bool,
1022-
indexer_url: String,
1023-
) -> Result<Online, RgbLibError> {
1024-
self._get_wallet()
1025-
.go_online(skip_consistency_check, indexer_url)
1048+
fn go_online(&self, online_options: OnlineOptions) -> Result<Online, RgbLibError> {
1049+
self._get_wallet().go_online(online_options)
10261050
}
10271051

10281052
fn inflate(
@@ -1184,7 +1208,6 @@ impl Wallet {
11841208
fee_rate: u64,
11851209
min_confirmations: u8,
11861210
expiration_timestamp: Option<u64>,
1187-
skip_sync: bool,
11881211
) -> Result<OperationResult, RgbLibError> {
11891212
self._get_wallet().send(
11901213
online,
@@ -1193,7 +1216,6 @@ impl Wallet {
11931216
fee_rate,
11941217
min_confirmations,
11951218
expiration_timestamp,
1196-
skip_sync,
11971219
)
11981220
}
11991221

@@ -1222,9 +1244,8 @@ impl Wallet {
12221244
&self,
12231245
online: Online,
12241246
signed_psbt: String,
1225-
skip_sync: bool,
12261247
) -> Result<OperationResult, RgbLibError> {
1227-
self._get_wallet().send_end(online, signed_psbt, skip_sync)
1248+
self._get_wallet().send_end(online, signed_psbt)
12281249
}
12291250

12301251
fn send_btc(
@@ -1252,18 +1273,12 @@ impl Wallet {
12521273
.send_btc_begin(online, address, amount, fee_rate, skip_sync, dry_run)
12531274
}
12541275

1255-
fn send_btc_end(
1256-
&self,
1257-
online: Online,
1258-
signed_psbt: String,
1259-
skip_sync: bool,
1260-
) -> Result<String, RgbLibError> {
1261-
self._get_wallet()
1262-
.send_btc_end(online, signed_psbt, skip_sync)
1276+
fn send_btc_end(&self, online: Online, signed_psbt: String) -> Result<String, RgbLibError> {
1277+
self._get_wallet().send_btc_end(online, signed_psbt)
12631278
}
12641279

1265-
fn sync(&self, online: Online) -> Result<(), RgbLibError> {
1266-
self._get_wallet().sync(online)
1280+
fn sync(&self, online: Online, options: SyncOptions) -> Result<(), RgbLibError> {
1281+
self._get_wallet().sync(online, options.into())
12671282
}
12681283

12691284
fn inspect_psbt(&self, psbt: String) -> Result<PsbtInspection, RgbLibError> {
@@ -1432,13 +1447,11 @@ impl MultisigWallet {
14321447

14331448
fn go_online(
14341449
&self,
1435-
skip_consistency_check: bool,
1436-
indexer_url: String,
1437-
hub_url: String,
1438-
hub_token: String,
1450+
online_options: OnlineOptions,
1451+
multisig_online_options: MultisigOnlineOptions,
14391452
) -> Result<Online, RgbLibError> {
14401453
let mut wallet = self.wallet_mutex.lock().expect("wallet");
1441-
wallet.go_online(skip_consistency_check, indexer_url, hub_url, hub_token)
1454+
wallet.go_online(online_options, multisig_online_options)
14421455
}
14431456

14441457
fn hub_info(&self, online: Online) -> Result<HubInfo, RgbLibError> {
@@ -1606,9 +1619,9 @@ impl MultisigWallet {
16061619
.send_btc_init(online, address, amount, fee_rate, skip_sync)
16071620
}
16081621

1609-
fn sync(&self, online: Online) -> Result<(), RgbLibError> {
1622+
fn sync(&self, online: Online, options: SyncOptions) -> Result<(), RgbLibError> {
16101623
let mut wallet = self.wallet_mutex.lock().expect("wallet");
1611-
wallet.sync(online)
1624+
wallet.sync(online, options.into())
16121625
}
16131626

16141627
fn get_address(&self, online: Online) -> Result<String, RgbLibError> {

0 commit comments

Comments
 (0)