wip: remove Request for object and call functions like buy_xmr directly

This commit is contained in:
binarybaron 2024-08-09 01:30:45 +02:00
parent 4f336e98a1
commit 693f4297c5
No known key found for this signature in database
GPG Key ID: 99B75D3E1476A26E
6 changed files with 95 additions and 203 deletions

1
Cargo.lock generated
View File

@ -7586,7 +7586,6 @@ dependencies = [
"swap",
"tauri",
"tauri-build",
"uuid",
]
[[package]]

View File

@ -4,7 +4,7 @@ import { rpcSetBalance, rpcSetSwapInfo } from "store/features/rpcSlice";
export async function checkBitcoinBalance() {
// TODO: use tauri-bindgen here
const response = (await invoke("balance")) as {
const response = (await invoke("get_balance")) as {
balance: number;
};
@ -12,7 +12,7 @@ export async function checkBitcoinBalance() {
}
export async function getRawSwapInfos() {
const response = await invoke("swap_infos_all");
const response = await invoke("get_swap_infos_all");
(response as any[]).forEach((info) => store.dispatch(rpcSetSwapInfo(info)));
}

View File

@ -21,4 +21,3 @@ serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
swap = { path = "../swap" }
tauri = { version = "2.0.0-rc.1", features = [ "config-json5" ] }
uuid = "1.10.0"

View File

@ -146,111 +146,8 @@ pub enum Method {
GetRawStates,
}
// TODO: Simplify this using instrument macro
impl Method {
fn get_tracing_span(&self, log_reference_id: Option<String>) -> Span {
let span = match self {
Method::Balance { .. } => {
debug_span!(
"method",
method_name = "Balance",
log_reference_id = field::Empty
)
}
Method::BuyXmr(BuyXmrArgs { swap_id, .. }) => {
debug_span!("method", method_name="BuyXmr", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::CancelAndRefund(CancelAndRefundArgs { swap_id }) => {
debug_span!("method", method_name="CancelAndRefund", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::Resume(ResumeArgs { swap_id }) => {
debug_span!("method", method_name="Resume", swap_id=%swap_id, log_reference_id=field::Empty)
}
Method::Config => {
debug_span!(
"method",
method_name = "Config",
log_reference_id = field::Empty
)
}
Method::ExportBitcoinWallet => {
debug_span!(
"method",
method_name = "ExportBitcoinWallet",
log_reference_id = field::Empty
)
}
Method::GetCurrentSwap => {
debug_span!(
"method",
method_name = "GetCurrentSwap",
log_reference_id = field::Empty
)
}
Method::GetSwapInfo { .. } => {
debug_span!(
"method",
method_name = "GetSwapInfo",
log_reference_id = field::Empty
)
}
Method::History => {
debug_span!(
"method",
method_name = "History",
log_reference_id = field::Empty
)
}
Method::ListSellers { .. } => {
debug_span!(
"method",
method_name = "ListSellers",
log_reference_id = field::Empty
)
}
Method::MoneroRecovery { .. } => {
debug_span!(
"method",
method_name = "MoneroRecovery",
log_reference_id = field::Empty
)
}
Method::GetRawStates => debug_span!(
"method",
method_name = "RawHistory",
log_reference_id = field::Empty
),
Method::StartDaemon { .. } => {
debug_span!(
"method",
method_name = "StartDaemon",
log_reference_id = field::Empty
)
}
Method::SuspendCurrentSwap => {
debug_span!(
"method",
method_name = "SuspendCurrentSwap",
log_reference_id = field::Empty
)
}
Method::WithdrawBtc { .. } => {
debug_span!(
"method",
method_name = "WithdrawBtc",
log_reference_id = field::Empty
)
}
};
if let Some(log_reference_id) = log_reference_id {
span.record("log_reference_id", log_reference_id.as_str());
}
span
}
}
// #[tracing::instrument(fields(method = "suspend_current_swap"), skip(context))]
async fn suspend_current_swap(context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "suspend_current_swap"), skip(context))]
pub async fn suspend_current_swap(context: Arc<Context>) -> Result<serde_json::Value> {
let swap_id = context.swap_lock.get_current_swap_id().await;
if let Some(id_value) = swap_id {
@ -262,6 +159,7 @@ async fn suspend_current_swap(context: Arc<Context>) -> Result<serde_json::Value
}
}
#[tracing::instrument(fields(method = "get_swap_infos_all"), skip(context))]
pub async fn get_swap_infos_all(context: Arc<Context>) -> Result<Vec<GetSwapInfoResponse>> {
let swap_ids = context.db.all().await?;
let mut swap_infos = Vec::new();
@ -274,7 +172,7 @@ pub async fn get_swap_infos_all(context: Arc<Context>) -> Result<Vec<GetSwapInfo
Ok(swap_infos)
}
// #[tracing::instrument(fields(method="get_swap_info", swap_id = args.swap_id), skip(context))]
#[tracing::instrument(fields(method = "get_swap_info"), skip(context))]
pub async fn get_swap_info(
args: GetSwapInfoArgs,
context: Arc<Context>,
@ -391,7 +289,8 @@ pub async fn get_swap_info(
})
}
async fn buy_xmr(buy_xmr: BuyXmrArgs, context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "buy_xmr"), skip(context))]
pub async fn buy_xmr(buy_xmr: BuyXmrArgs, context: Arc<Context>) -> Result<serde_json::Value> {
let BuyXmrArgs {
seller,
bitcoin_change_address,
@ -564,7 +463,8 @@ async fn buy_xmr(buy_xmr: BuyXmrArgs, context: Arc<Context>) -> Result<serde_jso
}))
}
async fn resume_swap(resume: ResumeArgs, context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "resume_swap"), skip(context))]
pub async fn resume_swap(resume: ResumeArgs, context: Arc<Context>) -> Result<serde_json::Value> {
let ResumeArgs { swap_id } = resume;
context.swap_lock.acquire_swap_lock(swap_id).await?;
@ -671,7 +571,8 @@ async fn resume_swap(resume: ResumeArgs, context: Arc<Context>) -> Result<serde_
}))
}
async fn cancel_and_refund(
#[tracing::instrument(fields(method = "cancel_and_refund"), skip(context))]
pub async fn cancel_and_refund(
cancel_and_refund: CancelAndRefundArgs,
context: Arc<Context>,
) -> Result<serde_json::Value> {
@ -699,7 +600,8 @@ async fn cancel_and_refund(
})
}
async fn get_history(context: Arc<Context>) -> Result<GetHistoryResponse> {
#[tracing::instrument(fields(method = "get_history"), skip(context))]
pub async fn get_history(context: Arc<Context>) -> Result<GetHistoryResponse> {
let swaps = context.db.all().await?;
let mut vec: Vec<(Uuid, String)> = Vec::new();
for (swap_id, state) in swaps {
@ -710,13 +612,15 @@ async fn get_history(context: Arc<Context>) -> Result<GetHistoryResponse> {
Ok(GetHistoryResponse { swaps: vec })
}
async fn get_raw_states(context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "get_raw_states"), skip(context))]
pub async fn get_raw_states(context: Arc<Context>) -> Result<serde_json::Value> {
let raw_history = context.db.raw_all().await?;
Ok(json!({ "raw_states": raw_history }))
}
async fn get_config(context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "get_config"), skip(context))]
pub async fn get_config(context: Arc<Context>) -> Result<serde_json::Value> {
let data_dir_display = context.config.data_dir.display();
tracing::info!(path=%data_dir_display, "Data directory");
tracing::info!(path=%format!("{}/logs", data_dir_display), "Log files directory");
@ -734,7 +638,8 @@ async fn get_config(context: Arc<Context>) -> Result<serde_json::Value> {
}))
}
async fn withdraw_btc(
#[tracing::instrument(fields(method = "withdraw_btc"), skip(context))]
pub async fn withdraw_btc(
withdraw_btc: WithdrawBtcArgs,
context: Arc<Context>,
) -> Result<serde_json::Value> {
@ -768,7 +673,8 @@ async fn withdraw_btc(
}))
}
async fn start_daemon(
#[tracing::instrument(fields(method = "start_daemon"), skip(context))]
pub async fn start_daemon(
start_daemon: StartDaemonArgs,
context: Arc<Context>,
) -> Result<serde_json::Value> {
@ -776,7 +682,7 @@ async fn start_daemon(
// Default to 127.0.0.1:1234
let server_address = server_address.unwrap_or("127.0.0.1:1234".parse()?);
let (addr, server_handle) = rpc::run_server(server_address, Arc::clone(&context)).await?;
let (addr, server_handle) = rpc::run_server(server_address, context).await?;
tracing::info!(%addr, "Started RPC server");
@ -787,7 +693,7 @@ async fn start_daemon(
Ok(json!({}))
}
// #[instrument(fields(method = "get_balance"))]
#[tracing::instrument(fields(method = "get_balance"), skip(context))]
pub async fn get_balance(balance: BalanceArgs, context: Arc<Context>) -> Result<BalanceResponse> {
let BalanceArgs { force_refresh } = balance;
let bitcoin_wallet = context
@ -818,7 +724,8 @@ pub async fn get_balance(balance: BalanceArgs, context: Arc<Context>) -> Result<
})
}
async fn list_sellers(
#[tracing::instrument(fields(method = "list_sellers"), skip(context))]
pub async fn list_sellers(
list_sellers: ListSellersArgs,
context: Arc<Context>,
) -> Result<serde_json::Value> {
@ -868,7 +775,8 @@ async fn list_sellers(
Ok(json!({ "sellers": sellers }))
}
async fn export_bitcoin_wallet(context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "export_bitcoin_wallet"), skip(context))]
pub async fn export_bitcoin_wallet(context: Arc<Context>) -> Result<serde_json::Value> {
let bitcoin_wallet = context
.bitcoin_wallet
.as_ref()
@ -881,7 +789,8 @@ async fn export_bitcoin_wallet(context: Arc<Context>) -> Result<serde_json::Valu
}))
}
async fn monero_recovery(
#[tracing::instrument(fields(method = "monero_recovery"), skip(context))]
pub async fn monero_recovery(
monero_recovery: MoneroRecoveryArgs,
context: Arc<Context>,
) -> Result<serde_json::Value> {
@ -914,7 +823,8 @@ async fn monero_recovery(
}
}
async fn get_current_swap(context: Arc<Context>) -> Result<serde_json::Value> {
#[tracing::instrument(fields(method = "get_current_swap"), skip(context))]
pub async fn get_current_swap(context: Arc<Context>) -> Result<serde_json::Value> {
Ok(json!({
"swap_id": context.swap_lock.get_current_swap_id().await
}))
@ -946,19 +856,7 @@ impl Request {
}
pub async fn call(self, context: Arc<Context>) -> Result<JsonValue> {
let method_span = self.cmd.get_tracing_span(self.log_reference.clone());
self.handle_cmd(context)
.instrument(method_span.clone())
.await
.map_err(|err| {
method_span.in_scope(|| {
// The {:?} formatter is used to print the entire error chain
tracing::debug!(err = format!("{:?}", err), "API call resulted in an error");
});
err
})
.map(|result| json!(result))
unreachable!("This function should never be called")
}
}

View File

@ -28,10 +28,11 @@ pub async fn run_server(
.set_middleware(middleware)
.build(server_address)
.await?;
let mut modules = RpcModule::new(());
{
modules
.merge(methods::register_modules(Arc::clone(&context))?)
.merge(methods::register_modules(context)?)
.expect("Could not register RPC modules")
}

View File

@ -1,6 +1,8 @@
use crate::api::request::{
buy_xmr, cancel_and_refund, get_balance, get_current_swap, get_history, get_raw_states,
get_swap_info, list_sellers, monero_recovery, resume_swap, suspend_current_swap, withdraw_btc,
BalanceArgs, BuyXmrArgs, CancelAndRefundArgs, GetSwapInfoArgs, ListSellersArgs, Method,
MoneroRecoveryArgs, Request, ResumeArgs, WithdrawBtcArgs,
MoneroRecoveryArgs, ResumeArgs, WithdrawBtcArgs,
};
use crate::api::Context;
use crate::bitcoin::bitcoin_address;
@ -8,18 +10,29 @@ use crate::monero::monero_address;
use crate::{bitcoin, monero};
use anyhow::Result;
use jsonrpsee::server::RpcModule;
use jsonrpsee::types::Params;
use libp2p::core::Multiaddr;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use uuid::Uuid;
pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>> {
let mut module = RpcModule::new(context);
trait ConvertToJsonRpseeError<T> {
fn to_jsonrpsee_result(self) -> Result<T, jsonrpsee_core::Error>;
}
impl<T> ConvertToJsonRpseeError<T> for Result<T, anyhow::Error> {
fn to_jsonrpsee_result(self) -> Result<T, jsonrpsee_core::Error> {
self.map_err(|e| jsonrpsee_core::Error::Custom(e.to_string()))
}
}
pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Context>>> {
let mut module = RpcModule::new(outer_context);
module.register_async_method("suspend_current_swap", |params, context| async move {
execute_request(params, Method::SuspendCurrentSwap, &context).await
suspend_current_swap(Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("get_swap_info", |params_raw, context| async move {
@ -32,12 +45,9 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
let swap_id = as_uuid(swap_id)
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
execute_request(
params_raw,
Method::GetSwapInfo(GetSwapInfoArgs { swap_id }),
&context,
)
.await
get_swap_info(GetSwapInfoArgs { swap_id }, Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("get_bitcoin_balance", |params_raw, context| async move {
@ -53,20 +63,21 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string())
})?;
execute_request(
params_raw,
Method::Balance(BalanceArgs { force_refresh }),
&context,
)
.await
get_balance(BalanceArgs { force_refresh }, Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("get_history", |params, context| async move {
execute_request(params, Method::History, &context).await
get_history(Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("get_raw_states", |params, context| async move {
execute_request(params, Method::GetRawStates, &context).await
get_raw_states(Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("resume_swap", |params_raw, context| async move {
@ -79,7 +90,9 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
let swap_id = as_uuid(swap_id)
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
execute_request(params_raw, Method::Resume(ResumeArgs { swap_id }), &context).await
resume_swap(ResumeArgs { swap_id }, Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("cancel_refund_swap", |params_raw, context| async move {
@ -92,12 +105,12 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
let swap_id = as_uuid(swap_id)
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
execute_request(
params_raw,
Method::CancelAndRefund(CancelAndRefundArgs { swap_id }),
&context,
cancel_and_refund(
CancelAndRefundArgs { swap_id },
Arc::unwrap_or_clone(context),
)
.await
.to_jsonrpsee_result()
})?;
module.register_async_method(
@ -113,12 +126,12 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string())
})?;
execute_request(
params_raw,
Method::MoneroRecovery(MoneroRecoveryArgs { swap_id }),
&context,
monero_recovery(
MoneroRecoveryArgs { swap_id },
Arc::unwrap_or_clone(context),
)
.await
.to_jsonrpsee_result()
},
)?;
@ -144,15 +157,15 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
let withdraw_address =
bitcoin_address::validate(withdraw_address, context.config.env_config.bitcoin_network)?;
execute_request(
params_raw,
Method::WithdrawBtc(WithdrawBtcArgs {
withdraw_btc(
WithdrawBtcArgs {
amount,
address: withdraw_address,
}),
&context,
},
Arc::unwrap_or_clone(context),
)
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("buy_xmr", |params_raw, context| async move {
@ -186,17 +199,17 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
})?)
.map_err(|err| jsonrpsee_core::Error::Custom(err.to_string()))?;
execute_request(
params_raw,
Method::BuyXmr(BuyXmrArgs {
buy_xmr(
BuyXmrArgs {
seller,
bitcoin_change_address,
monero_receive_address,
seller,
swap_id: Uuid::new_v4(),
}),
&context,
},
Arc::unwrap_or_clone(context),
)
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("list_sellers", |params_raw, context| async move {
@ -213,18 +226,20 @@ pub fn register_modules(context: Arc<Context>) -> Result<RpcModule<Arc<Context>>
jsonrpsee_core::Error::Custom("Could not parse valid multiaddr".to_string())
})?;
execute_request(
params_raw,
Method::ListSellers(ListSellersArgs {
list_sellers(
ListSellersArgs {
rendezvous_point: rendezvous_point.clone(),
}),
&context,
},
Arc::unwrap_or_clone(context),
)
.await
.to_jsonrpsee_result()
})?;
module.register_async_method("get_current_swap", |params, context| async move {
execute_request(params, Method::GetCurrentSwap, &context).await
get_current_swap(Arc::unwrap_or_clone(context))
.await
.to_jsonrpsee_result()
})?;
Ok(module)
@ -237,23 +252,3 @@ fn as_uuid(json_value: &serde_json::Value) -> Option<Uuid> {
None
}
}
async fn execute_request(
params: Params<'static>,
cmd: Method,
context: &Arc<Context>,
) -> Result<serde_json::Value, jsonrpsee_core::Error> {
// If we fail to parse the params as a String HashMap, it's most likely because its an empty object
// In that case, we want to make sure not to fail the request, so we set the log_reference_id to None
// and swallow the error
let reference_id = params
.parse::<HashMap<String, serde_json::Value>>()
.ok()
.and_then(|params_parsed| params_parsed.get("log_reference_id").cloned());
let request = Request::with_id(cmd, reference_id.map(|log_ref| log_ref.to_string()));
request
.call(Arc::clone(context))
.await
.map_err(|err| jsonrpsee_core::Error::Custom(format!("{:#}", err)))
}