From 4629e88a28067c6d8df2069887e5db1a7dc0a2fa Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 15 Oct 2022 19:51:59 -0400 Subject: [PATCH] Add is_confirmed with a TODO: Remove Closes https://github.com/serai-dex/serai/issues/129. --- coins/monero/src/rpc.rs | 40 ++++++++++++++++++++++++------------ processor/src/coin/mod.rs | 3 +++ processor/src/coin/monero.rs | 6 ++++++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/coins/monero/src/rpc.rs b/coins/monero/src/rpc.rs index 21c8488d..0967d7cb 100644 --- a/coins/monero/src/rpc.rs +++ b/coins/monero/src/rpc.rs @@ -23,6 +23,20 @@ pub struct JsonRpcResponse { result: T, } +#[derive(Deserialize, Debug)] +struct TransactionResponse { + tx_hash: String, + block_height: usize, + as_hex: String, + pruned_as_hex: String, +} +#[derive(Deserialize, Debug)] +struct TransactionsResponse { + #[serde(default)] + missed_tx: Vec, + txs: Vec, +} + #[derive(Clone, Error, Debug)] pub enum RpcError { #[error("internal error ({0})")] @@ -149,19 +163,6 @@ impl Rpc { return Ok(vec![]); } - #[derive(Deserialize, Debug)] - struct TransactionResponse { - tx_hash: String, - as_hex: String, - pruned_as_hex: String, - } - #[derive(Deserialize, Debug)] - struct TransactionsResponse { - #[serde(default)] - missed_tx: Vec, - txs: Vec, - } - let txs: TransactionsResponse = self .rpc_call( "get_transactions", @@ -205,6 +206,19 @@ impl Rpc { self.get_transactions(&[tx]).await.map(|mut txs| txs.swap_remove(0)) } + pub async fn get_transaction_height(&self, tx: &[u8]) -> Result { + let txs: TransactionsResponse = + self.rpc_call("get_transactions", Some(json!({ "txs_hashes": [hex::encode(tx)] }))).await?; + + if !txs.missed_tx.is_empty() { + Err(RpcError::TransactionsNotFound( + txs.missed_tx.iter().map(|hash| hex::decode(hash).unwrap().try_into().unwrap()).collect(), + ))?; + } + + Ok(txs.txs[0].block_height) + } + pub async fn get_block(&self, height: usize) -> Result { #[derive(Deserialize, Debug)] struct BlockResponse { diff --git a/processor/src/coin/mod.rs b/processor/src/coin/mod.rs index 88d9d98c..98471d34 100644 --- a/processor/src/coin/mod.rs +++ b/processor/src/coin/mod.rs @@ -55,6 +55,9 @@ pub trait Coin { key: ::G, ) -> Result, CoinError>; + // TODO: Remove + async fn is_confirmed(&self, tx: &[u8], height: usize) -> Result; + async fn prepare_send( &self, keys: FrostKeys, diff --git a/processor/src/coin/monero.rs b/processor/src/coin/monero.rs index 436caa12..b25d42e6 100644 --- a/processor/src/coin/monero.rs +++ b/processor/src/coin/monero.rs @@ -147,6 +147,12 @@ impl Coin for Monero { ) } + async fn is_confirmed(&self, tx: &[u8], height: usize) -> Result { + let tx_height = + self.rpc.get_transaction_height(tx).await.map_err(|_| CoinError::ConnectionError)?; + Ok((height.saturating_sub(tx_height) + 1) >= 10) + } + async fn prepare_send( &self, keys: FrostKeys,