Skip to content

Commit 7e13748

Browse files
authored
fix: check confirmations for utxo related methods (#213)
* fix: check confirmations for utxo related methods * chore: bumped versions * fix: set max confirmations for amount greater than max upper limit * fix: block comparison
1 parent db87982 commit 7e13748

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridge-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bridge-cli"
3-
version = "0.3.18"
3+
version = "0.3.19"
44
edition = "2021"
55
repository = "https://github.com/Near-One/bridge-sdk-rs"
66

bridge-sdk/bridge-clients/near-bridge-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "near-bridge-client"
3-
version = "0.2.6"
3+
version = "0.2.7"
44
edition = "2021"
55

66
[dependencies]

bridge-sdk/bridge-clients/near-bridge-client/src/btc.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ struct PartialConfig {
160160
max_active_utxo_management_output_number: u8,
161161
active_management_lower_limit: u32,
162162
active_management_upper_limit: u32,
163+
confirmations_strategy: HashMap<String, u8>,
164+
confirmations_delta: u8,
163165
}
164166

165167
#[serde_as]
@@ -695,6 +697,23 @@ impl NearBridgeClient {
695697
Ok(config.min_deposit_amount)
696698
}
697699

700+
pub async fn get_total_confirmations(&self, chain: ChainKind, amount: u128) -> Result<u8> {
701+
let config = self.get_config(chain).await?;
702+
703+
let mut total_confirmations = None;
704+
let mut max_confirmations = 0;
705+
for (upper_limit, confirmations) in config.confirmations_strategy {
706+
if amount < upper_limit.parse::<u128>().unwrap_or_default() {
707+
total_confirmations = Some(total_confirmations.unwrap_or(0).max(confirmations));
708+
}
709+
max_confirmations = max(max_confirmations, confirmations);
710+
}
711+
712+
Ok(total_confirmations
713+
.unwrap_or(max_confirmations)
714+
.saturating_add(config.confirmations_delta))
715+
}
716+
698717
async fn get_config(&self, chain: ChainKind) -> Result<PartialConfig> {
699718
let endpoint = self.endpoint()?;
700719
let btc_connector = self.utxo_chain_connector(chain)?;

bridge-sdk/connectors/omni-connector/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "omni-connector"
3-
version = "0.2.15"
3+
version = "0.2.16"
44
edition = "2021"
55

66
[dependencies]

bridge-sdk/connectors/omni-connector/src/omni_connector.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,18 +471,27 @@ impl OmniConnector {
471471
transaction_options: TransactionOptions,
472472
) -> Result<CryptoHash> {
473473
let utxo_bridge_client = self.utxo_bridge_client(chain)?;
474+
let near_bridge_client = self.near_bridge_client()?;
475+
474476
let proof_data = utxo_bridge_client.extract_btc_proof(&tx_hash).await?;
475477

476478
let light_client = self.light_client(chain)?;
477479
let light_client_last_block = light_client.get_last_block_number().await?;
478480

479-
if proof_data.block_height > light_client_last_block {
481+
let amount = near_bridge_client
482+
.get_btc_pending_info(chain, tx_hash.clone())
483+
.await?
484+
.transfer_amount;
485+
let confirmations = near_bridge_client
486+
.get_total_confirmations(chain, amount)
487+
.await?;
488+
489+
if proof_data.block_height + u64::from(confirmations) > light_client_last_block {
480490
return Err(BridgeSdkError::LightClientNotSynced(
481491
light_client_last_block,
482492
));
483493
}
484494

485-
let near_bridge_client = self.near_bridge_client()?;
486495
let deposit_msg = match deposit_args {
487496
BtcDepositArgs::DepositMsg { msg } => msg,
488497
BtcDepositArgs::OmniDepositArgs { recipient_id, fee } => {
@@ -511,18 +520,27 @@ impl OmniConnector {
511520
transaction_options: TransactionOptions,
512521
) -> Result<CryptoHash> {
513522
let utxo_bridge_client = self.utxo_bridge_client(chain)?;
523+
let near_bridge_client = self.near_bridge_client()?;
524+
514525
let proof_data = utxo_bridge_client.extract_btc_proof(&tx_hash).await?;
515526

516527
let light_client = self.light_client(chain)?;
517528
let light_client_last_block = light_client.get_last_block_number().await?;
518529

519-
if proof_data.block_height > light_client_last_block {
530+
let amount = near_bridge_client
531+
.get_btc_pending_info(chain, tx_hash.clone())
532+
.await?
533+
.transfer_amount;
534+
let confirmations = near_bridge_client
535+
.get_total_confirmations(chain, amount)
536+
.await?;
537+
538+
if proof_data.block_height + u64::from(confirmations) > light_client_last_block {
520539
return Err(BridgeSdkError::LightClientNotSynced(
521540
light_client_last_block,
522541
));
523542
}
524543

525-
let near_bridge_client = self.near_bridge_client()?;
526544
let args = BtcVerifyWithdrawArgs {
527545
tx_id: tx_hash,
528546
tx_block_blockhash: proof_data.tx_block_blockhash,
@@ -555,18 +573,27 @@ impl OmniConnector {
555573
transaction_options: TransactionOptions,
556574
) -> Result<CryptoHash> {
557575
let utxo_bridge_client = self.utxo_bridge_client(chain)?;
576+
let near_bridge_client = self.near_bridge_client()?;
577+
558578
let proof_data = utxo_bridge_client.extract_btc_proof(&tx_hash).await?;
559579

560580
let light_client = self.light_client(chain)?;
561581
let light_client_last_block = light_client.get_last_block_number().await?;
562582

563-
if proof_data.block_height > light_client_last_block {
583+
let amount = near_bridge_client
584+
.get_btc_pending_info(chain, tx_hash.clone())
585+
.await?
586+
.transfer_amount;
587+
let confirmations = near_bridge_client
588+
.get_total_confirmations(chain, amount)
589+
.await?;
590+
591+
if proof_data.block_height + u64::from(confirmations) > light_client_last_block {
564592
return Err(BridgeSdkError::LightClientNotSynced(
565593
light_client_last_block,
566594
));
567595
}
568596

569-
let near_bridge_client = self.near_bridge_client()?;
570597
let args = BtcVerifyWithdrawArgs {
571598
tx_id: tx_hash,
572599
tx_block_blockhash: proof_data.tx_block_blockhash,

0 commit comments

Comments
 (0)