mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-20 20:18:18 -04:00
refactor: remover Arc for start_daemon
This commit is contained in:
parent
630f4c6f23
commit
92034a5be8
5 changed files with 34 additions and 54 deletions
|
@ -159,11 +159,12 @@ impl Default for SwapLock {
|
||||||
|
|
||||||
// workaround for warning over monero_rpc_process which we must own but not read
|
// workaround for warning over monero_rpc_process which we must own but not read
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub db: Arc<dyn Database + Send + Sync>,
|
pub db: Arc<dyn Database + Send + Sync>,
|
||||||
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
|
bitcoin_wallet: Option<Arc<bitcoin::Wallet>>,
|
||||||
monero_wallet: Option<Arc<monero::Wallet>>,
|
monero_wallet: Option<Arc<monero::Wallet>>,
|
||||||
monero_rpc_process: Option<monero::WalletRpcProcess>,
|
monero_rpc_process: Option<Arc<monero::WalletRpcProcess>>,
|
||||||
pub swap_lock: Arc<SwapLock>,
|
pub swap_lock: Arc<SwapLock>,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub tasks: Arc<PendingTaskList>,
|
pub tasks: Arc<PendingTaskList>,
|
||||||
|
@ -227,7 +228,7 @@ impl Context {
|
||||||
db: open_db(data_dir.join("sqlite")).await?,
|
db: open_db(data_dir.join("sqlite")).await?,
|
||||||
bitcoin_wallet,
|
bitcoin_wallet,
|
||||||
monero_wallet,
|
monero_wallet,
|
||||||
monero_rpc_process,
|
monero_rpc_process: monero_rpc_process.map(Arc::new),
|
||||||
config: Config {
|
config: Config {
|
||||||
tor_socks5_port,
|
tor_socks5_port,
|
||||||
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
|
namespace: XmrBtcNamespace::from_is_testnet(is_testnet),
|
||||||
|
|
|
@ -679,7 +679,7 @@ pub async fn withdraw_btc(
|
||||||
#[tracing::instrument(fields(method = "start_daemon"), skip(context))]
|
#[tracing::instrument(fields(method = "start_daemon"), skip(context))]
|
||||||
pub async fn start_daemon(
|
pub async fn start_daemon(
|
||||||
start_daemon: StartDaemonArgs,
|
start_daemon: StartDaemonArgs,
|
||||||
context: Arc<Context>,
|
context: Context,
|
||||||
) -> Result<serde_json::Value> {
|
) -> Result<serde_json::Value> {
|
||||||
let StartDaemonArgs { server_address } = start_daemon;
|
let StartDaemonArgs { server_address } = start_daemon;
|
||||||
// Default to 127.0.0.1:1234
|
// Default to 127.0.0.1:1234
|
||||||
|
|
|
@ -156,19 +156,17 @@ where
|
||||||
monero,
|
monero,
|
||||||
tor,
|
tor,
|
||||||
} => {
|
} => {
|
||||||
let context = Arc::new(
|
let context = Context::build(
|
||||||
Context::build(
|
Some(bitcoin),
|
||||||
Some(bitcoin),
|
Some(monero),
|
||||||
Some(monero),
|
Some(tor),
|
||||||
Some(tor),
|
data,
|
||||||
data,
|
is_testnet,
|
||||||
is_testnet,
|
debug,
|
||||||
debug,
|
json,
|
||||||
json,
|
server_address,
|
||||||
server_address,
|
)
|
||||||
)
|
.await?;
|
||||||
.await?,
|
|
||||||
);
|
|
||||||
|
|
||||||
start_daemon(StartDaemonArgs { server_address }, context).await?;
|
start_daemon(StartDaemonArgs { server_address }, context).await?;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub enum Error {
|
||||||
|
|
||||||
pub async fn run_server(
|
pub async fn run_server(
|
||||||
server_address: SocketAddr,
|
server_address: SocketAddr,
|
||||||
context: Arc<Context>,
|
context: Context,
|
||||||
) -> anyhow::Result<(SocketAddr, ServerHandle)> {
|
) -> anyhow::Result<(SocketAddr, ServerHandle)> {
|
||||||
let cors = CorsLayer::permissive();
|
let cors = CorsLayer::permissive();
|
||||||
let middleware = tower::ServiceBuilder::new().layer(cors);
|
let middleware = tower::ServiceBuilder::new().layer(cors);
|
||||||
|
@ -29,12 +29,7 @@ pub async fn run_server(
|
||||||
.build(server_address)
|
.build(server_address)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut modules = RpcModule::new(());
|
let modules = methods::register_modules(context)?;
|
||||||
{
|
|
||||||
modules
|
|
||||||
.merge(methods::register_modules(context)?)
|
|
||||||
.expect("Could not register RPC modules")
|
|
||||||
}
|
|
||||||
|
|
||||||
let addr = server.local_addr()?;
|
let addr = server.local_addr()?;
|
||||||
let server_handle = server.start(modules)?;
|
let server_handle = server.start(modules)?;
|
||||||
|
|
|
@ -26,13 +26,11 @@ impl<T> ConvertToJsonRpseeError<T> for Result<T, anyhow::Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Context>>> {
|
pub fn register_modules(outer_context: Context) -> Result<RpcModule<Context>> {
|
||||||
let mut module = RpcModule::new(outer_context);
|
let mut module = RpcModule::new(outer_context);
|
||||||
|
|
||||||
module.register_async_method("suspend_current_swap", |params, context| async move {
|
module.register_async_method("suspend_current_swap", |params, context| async move {
|
||||||
suspend_current_swap(Arc::unwrap_or_clone(context))
|
suspend_current_swap(context).await.to_jsonrpsee_result()
|
||||||
.await
|
|
||||||
.to_jsonrpsee_result()
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
module.register_async_method("get_swap_info", |params_raw, context| async move {
|
module.register_async_method("get_swap_info", |params_raw, context| async move {
|
||||||
|
@ -45,7 +43,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
let swap_id = as_uuid(swap_id)
|
let swap_id = as_uuid(swap_id)
|
||||||
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||||
|
|
||||||
get_swap_info(GetSwapInfoArgs { swap_id }, Arc::unwrap_or_clone(context))
|
get_swap_info(GetSwapInfoArgs { swap_id }, context)
|
||||||
.await
|
.await
|
||||||
.to_jsonrpsee_result()
|
.to_jsonrpsee_result()
|
||||||
})?;
|
})?;
|
||||||
|
@ -63,21 +61,17 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string())
|
jsonrpsee_core::Error::Custom("force_refesh is not a boolean".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
get_balance(BalanceArgs { force_refresh }, Arc::unwrap_or_clone(context))
|
get_balance(BalanceArgs { force_refresh }, context)
|
||||||
.await
|
.await
|
||||||
.to_jsonrpsee_result()
|
.to_jsonrpsee_result()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
module.register_async_method("get_history", |params, context| async move {
|
module.register_async_method("get_history", |params, context| async move {
|
||||||
get_history(Arc::unwrap_or_clone(context))
|
get_history(context).await.to_jsonrpsee_result()
|
||||||
.await
|
|
||||||
.to_jsonrpsee_result()
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
module.register_async_method("get_raw_states", |params, context| async move {
|
module.register_async_method("get_raw_states", |params, context| async move {
|
||||||
get_raw_states(Arc::unwrap_or_clone(context))
|
get_raw_states(context).await.to_jsonrpsee_result()
|
||||||
.await
|
|
||||||
.to_jsonrpsee_result()
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
module.register_async_method("resume_swap", |params_raw, context| async move {
|
module.register_async_method("resume_swap", |params_raw, context| async move {
|
||||||
|
@ -90,7 +84,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
let swap_id = as_uuid(swap_id)
|
let swap_id = as_uuid(swap_id)
|
||||||
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||||
|
|
||||||
resume_swap(ResumeArgs { swap_id }, Arc::unwrap_or_clone(context))
|
resume_swap(ResumeArgs { swap_id }, context)
|
||||||
.await
|
.await
|
||||||
.to_jsonrpsee_result()
|
.to_jsonrpsee_result()
|
||||||
})?;
|
})?;
|
||||||
|
@ -105,12 +99,9 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
let swap_id = as_uuid(swap_id)
|
let swap_id = as_uuid(swap_id)
|
||||||
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
.ok_or_else(|| jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string()))?;
|
||||||
|
|
||||||
cancel_and_refund(
|
cancel_and_refund(CancelAndRefundArgs { swap_id }, context)
|
||||||
CancelAndRefundArgs { swap_id },
|
.await
|
||||||
Arc::unwrap_or_clone(context),
|
.to_jsonrpsee_result()
|
||||||
)
|
|
||||||
.await
|
|
||||||
.to_jsonrpsee_result()
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
module.register_async_method(
|
module.register_async_method(
|
||||||
|
@ -126,12 +117,9 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string())
|
jsonrpsee_core::Error::Custom("Could not parse swap_id".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
monero_recovery(
|
monero_recovery(MoneroRecoveryArgs { swap_id }, context)
|
||||||
MoneroRecoveryArgs { swap_id },
|
.await
|
||||||
Arc::unwrap_or_clone(context),
|
.to_jsonrpsee_result()
|
||||||
)
|
|
||||||
.await
|
|
||||||
.to_jsonrpsee_result()
|
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
@ -162,7 +150,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
amount,
|
amount,
|
||||||
address: withdraw_address,
|
address: withdraw_address,
|
||||||
},
|
},
|
||||||
Arc::unwrap_or_clone(context),
|
context,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.to_jsonrpsee_result()
|
.to_jsonrpsee_result()
|
||||||
|
@ -206,7 +194,7 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
monero_receive_address,
|
monero_receive_address,
|
||||||
swap_id: Uuid::new_v4(),
|
swap_id: Uuid::new_v4(),
|
||||||
},
|
},
|
||||||
Arc::unwrap_or_clone(context),
|
context,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.to_jsonrpsee_result()
|
.to_jsonrpsee_result()
|
||||||
|
@ -230,16 +218,14 @@ pub fn register_modules(outer_context: Arc<Context>) -> Result<RpcModule<Arc<Con
|
||||||
ListSellersArgs {
|
ListSellersArgs {
|
||||||
rendezvous_point: rendezvous_point.clone(),
|
rendezvous_point: rendezvous_point.clone(),
|
||||||
},
|
},
|
||||||
Arc::unwrap_or_clone(context),
|
context,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.to_jsonrpsee_result()
|
.to_jsonrpsee_result()
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
module.register_async_method("get_current_swap", |params, context| async move {
|
module.register_async_method("get_current_swap", |params, context| async move {
|
||||||
get_current_swap(Arc::unwrap_or_clone(context))
|
get_current_swap(context).await.to_jsonrpsee_result()
|
||||||
.await
|
|
||||||
.to_jsonrpsee_result()
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(module)
|
Ok(module)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue