Skip to content

Commit

Permalink
Return options for get_tx functions
Browse files Browse the repository at this point in the history
  • Loading branch information
djordon committed Sep 25, 2024
1 parent 7f9cdea commit 4c00a19
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion signer/src/bitcoin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl BitcoinInteract for ApiFallbackClient<BitcoinCoreClient> {
.await
}

fn get_tx(&self, txid: &Txid) -> Result<GetTxResponse, Error> {
fn get_tx(&self, txid: &Txid) -> Result<Option<GetTxResponse>, Error> {
self.get_client().get_tx(txid)
}

Expand Down
2 changes: 1 addition & 1 deletion signer/src/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait BitcoinInteract {
) -> impl Future<Output = Result<Option<bitcoin::Block>, Error>> + Send;

/// get tx
fn get_tx(&self, txid: &Txid) -> Result<GetTxResponse, Error>;
fn get_tx(&self, txid: &Txid) -> Result<Option<GetTxResponse>, Error>;

/// get tx info
fn get_tx_info(
Expand Down
24 changes: 14 additions & 10 deletions signer/src/bitcoin/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl BitcoinCoreClient {
/// mempool. If -txindex is enabled on bitcoin-core and no blockhash
/// argument is passed, it will return the transaction if it is in the
/// mempool or any block.
pub fn get_tx(&self, txid: &Txid) -> Result<GetTxResponse, Error> {
pub fn get_tx(&self, txid: &Txid) -> Result<Option<GetTxResponse>, Error> {
let args = [
serde_json::to_value(txid).map_err(Error::JsonSerialize)?,
// This is the verbosity level. The acceptable values are 0, 1,
Expand All @@ -224,9 +224,15 @@ impl BitcoinCoreClient {
serde_json::Value::Null,
];

self.inner
.call("getrawtransaction", &args)
.map_err(|err| Error::GetTransactionBitcoinCore(err, *txid))
match self.inner.call::<GetTxResponse>("getrawtransaction", &args) {
Ok(tx_info) => Ok(Some(tx_info)),
// If the transaction is not found in an
// actual block then the message is "No such transaction found
// in the provided block. Use gettransaction for wallet
// transactions." In both cases the code is the same.
Err(BtcRpcError::JsonRpc(JsonRpcError::Rpc(RpcError { code: -5, .. }))) => Ok(None),
Err(err) => Err(Error::GetTransactionBitcoinCore(err, *txid)),
}
}

/// Fetch and decode raw transaction from bitcoin-core using the
Expand Down Expand Up @@ -308,14 +314,12 @@ impl BitcoinInteract for BitcoinCoreClient {
unimplemented!()
}

#[doc = " get tx"]
fn get_tx(&self, _: &Txid) -> Result<GetTxResponse, Error> {
todo!()
fn get_tx(&self, txid: &Txid) -> Result<Option<GetTxResponse>, Error> {
self.get_tx(txid)
}

#[doc = " get tx info"]
fn get_tx_info(&self, _: &Txid, _: &BlockHash) -> Result<Option<BitcoinTxInfo>, Error> {
todo!()
fn get_tx_info(&self, txid: &Txid, block_hash: &BlockHash) -> Result<Option<BitcoinTxInfo>, Error> {
self.get_tx_info(txid, block_hash)
}

#[doc = " Estimate fee rate"]
Expand Down
8 changes: 5 additions & 3 deletions signer/src/block_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ impl DepositRequestValidator for CreateDepositRequest {
C: BitcoinInteract,
{
// Fetch the transaction from either a block or from the mempool
let response = client.get_tx(&self.outpoint.txid)?;
let Some(response) = client.get_tx(&self.outpoint.txid)? else {
return Err(Error::BitcoinTxMissing(self.outpoint.txid));
};

Ok(Deposit {
info: self.validate_tx(&response.tx)?,
Expand Down Expand Up @@ -772,8 +774,8 @@ mod tests {
}

impl BitcoinInteract for TestHarness {
fn get_tx(&self, txid: &bitcoin::Txid) -> Result<GetTxResponse, Error> {
self.deposits.get(txid).cloned().ok_or(Error::Encryption)
fn get_tx(&self, txid: &bitcoin::Txid) -> Result<Option<GetTxResponse>, Error> {
Ok(self.deposits.get(txid).cloned())
}

fn get_tx_info(&self, _: &Txid, _: &BlockHash) -> Result<Option<BitcoinTxInfo>, Error> {
Expand Down
6 changes: 6 additions & 0 deletions signer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub enum Error {
#[error("could not create RPC client to {1}: {0}")]
BitcoinCoreRpcClient(#[source] bitcoincore_rpc::Error, String),

/// The bitcoin tranaction was not found in the mempool or on the
/// bitcoin blockchain. This is thrown when we expect the transaction
/// to exist in bitcoin core but it does not.
#[error("Transaction is missing from mempool")]
BitcoinTxMissing(bitcoin::Txid),

/// Returned when we could not decode the hex into a
/// bitcoin::Transaction.
#[error("failed to decode the provided hex into a transaction. txid: {1}. {0}")]
Expand Down
2 changes: 1 addition & 1 deletion signer/src/testing/api_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl TryFrom<&[Url]> for NoopApiClient {

/// Noop implementation of the BitcoinInteract trait.
impl BitcoinInteract for NoopApiClient {
fn get_tx(&self, _: &bitcoin::Txid) -> Result<GetTxResponse, Error> {
fn get_tx(&self, _: &bitcoin::Txid) -> Result<Option<GetTxResponse>, Error> {
unimplemented!()
}
fn get_tx_info(
Expand Down
6 changes: 3 additions & 3 deletions signer/tests/integration/bitcoin_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn btc_client_getstransaction() {
let outpoint = faucet.send_to(500_000, &signer.address);
let vout = outpoint.vout as usize;

let response = client.get_tx(&outpoint.txid).unwrap();
let response = client.get_tx(&outpoint.txid).unwrap().unwrap();
// Let's make sure we got the right transaction
assert_eq!(response.tx.compute_txid(), outpoint.txid);
assert_eq!(response.tx.output[vout].value.to_sat(), 500_000);
Expand All @@ -45,7 +45,7 @@ fn btc_client_getstransaction() {
// Now let's confirm it and try again
faucet.generate_blocks(1);

let response = client.get_tx(&outpoint.txid).unwrap();
let response = client.get_tx(&outpoint.txid).unwrap().unwrap();
// Let's make sure we got the right transaction
assert_eq!(response.tx.compute_txid(), outpoint.txid);
assert_eq!(response.tx.output[vout].value.to_sat(), 500_000);
Expand Down Expand Up @@ -77,7 +77,7 @@ fn btc_client_gets_transaction_info() {
let outpoint = faucet.send_to(500_000, &signer.address);
let vout = outpoint.vout as usize;

let response = client.get_tx(&outpoint.txid).unwrap();
let response = client.get_tx(&outpoint.txid).unwrap().unwrap();
// Let's make sure we got the right transaction
assert_eq!(response.tx.compute_txid(), outpoint.txid);
assert_eq!(response.tx.output[vout].value.to_sat(), 500_000);
Expand Down

0 comments on commit 4c00a19

Please sign in to comment.