Skip to content

Commit

Permalink
XC-194: Add check_transaction_query method to Bitcoin checker
Browse files Browse the repository at this point in the history
  • Loading branch information
lpahlavi committed Jan 15, 2025
1 parent c4739e9 commit 6ca2ac8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions rs/bitcoin/checker/btc_checker_canister.did
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ service : (CheckArg) -> {
// Same as check_transaction, but taking the transaction id argument as a string.
check_transaction_str: (CheckTransactionStrArgs) -> (CheckTransactionResponse);

// Same as check_transaction, but query method, hence it does not perform any HTTP
// outcalls to fetch the required information.
// IMPORTANT: this endpoint is meant as a debugging tool and is not guaranteed to be backwards-compatible.
check_transaction_query: (CheckTransactionStrArgs) -> (CheckTransactionResponse) query;

// Return `Passed` if the given Bitcoin address passes the Bitcoin checker, or `Failed` otherwise.
// May throw error (trap) if the given address is malformed or not a mainnet address.
check_address: (CheckAddressArgs) -> (CheckAddressResponse) query;
Expand Down
21 changes: 19 additions & 2 deletions rs/bitcoin/checker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use ic_cdk::api::call::RejectionCode;
use ic_cdk::api::management_canister::http_request::{HttpResponse, TransformArgs};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::convert::Infallible;
use std::fmt;
use std::future::Future;
use std::str::FromStr;

mod dashboard;
Expand All @@ -23,6 +25,7 @@ mod logs;
mod providers;
mod state;

use crate::state::FetchedTx;
use fetch::{FetchEnv, FetchResult, TryFetchResult};
use logs::{Log, LogEntry, Priority, DEBUG, WARN};
use state::{get_config, set_config, Config, FetchGuardError, HttpGetTxError};
Expand Down Expand Up @@ -105,8 +108,7 @@ fn check_address(args: CheckAddressArgs) -> CheckAddressResponse {
/// together with a text description.
#[ic_cdk::update]
async fn check_transaction(args: CheckTransactionArgs) -> CheckTransactionResponse {
check_transaction_with(|| Txid::try_from(args.txid.as_ref()).map_err(|err| err.to_string()))
.await
check_transaction_with(|| check_transaction_txid(args)).await
}

#[ic_cdk::update]
Expand All @@ -116,6 +118,21 @@ async fn check_transaction_str(args: CheckTransactionStrArgs) -> CheckTransactio
.await
}

/// Performs the same checks as the `check_transaction` method, but since no cycles are available,
/// it only returns `Passed` if the transaction information is already available. If the transaction
/// inputs need to be fetched, it will return `NotEnoughCycles`.
#[ic_cdk::query]
async fn check_transaction_query(args: CheckTransactionArgs) -> CheckTransactionResponse {
match check_transaction_txid(args) {
Ok(txid) => check_transaction_inputs(txid).await,
Err(err) => CheckTransactionIrrecoverableError::InvalidTransactionId(err).into(),
}
}

fn check_transaction_txid(args: CheckTransactionArgs) -> Result<Txid, String> {
Txid::try_from(args.txid.as_ref()).map_err(|err| err.to_string())
}

async fn check_transaction_with<F: FnOnce() -> Result<Txid, String>>(
get_txid: F,
) -> CheckTransactionResponse {
Expand Down

0 comments on commit 6ca2ac8

Please sign in to comment.