From 947c93907cd906d0ee5ffac799ba4c2eb8a04b25 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 21 Feb 2024 18:40:04 +0100 Subject: [PATCH] common: fix the proxy design Signed-off-by: Vincenzo Palazzo --- Makefile | 1 + flake.nix | 2 + rgb-common/src/proxy.rs | 73 ++++++++++++++++++++--------------- rgb-common/src/rgb_manager.rs | 21 +++++++--- rgb-testing/src/lib.rs | 6 ++- rust-toolchain | 2 +- 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 68a0e16..2e339ec 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ fmt: $(CC) fmt --all check: + $(CC) build RUST_LOG=debug $(CC) test --all -- --show-output example: diff --git a/flake.nix b/flake.nix index 61df307..908491a 100644 --- a/flake.nix +++ b/flake.nix @@ -55,6 +55,8 @@ export HOST_CC=gcc export PWD="$(pwd)" export RUST_LOG=debug + + make ''; }; } diff --git a/rgb-common/src/proxy.rs b/rgb-common/src/proxy.rs index 3b22a55..8066fd8 100644 --- a/rgb-common/src/proxy.rs +++ b/rgb-common/src/proxy.rs @@ -1,17 +1,55 @@ //! A module for operating an RGB HTTP JSON-RPC proxy +use core::str::FromStr; +use core::time::Duration; use amplify::s; +use bitcoin::Network; use reqwest::header::CONTENT_TYPE; use serde::{Deserialize, Serialize}; -use tokio::task; - -use core::time::Duration; use crate::BlockingClient; const JSON: &str = "application/json"; const PROXY_TIMEOUT: u8 = 90; +#[derive(Debug, Clone)] +pub struct Client { + inner: BlockingClient, + network: Network, +} + +impl Client { + pub fn new(network: &str) -> anyhow::Result { + let network = Network::from_str(network)?; + let inner = BlockingClient::builder() + .timeout(Duration::from_secs(PROXY_TIMEOUT as u64)) + .build()?; + Ok(Self { inner, network }) + } + + pub fn get_consignment(&self, consignment_id: &str) -> anyhow::Result> { + let body = JsonRpcRequest { + method: s!("consignment.get"), + jsonrpc: s!("2.0"), + id: None, + params: Some(BlindedUtxoParam { + blinded_utxo: consignment_id.to_owned(), + }), + }; + + // FIXME: add a URL for this + let url = ""; + let resp = self + .inner + .post(format!("{url}")) + .header(CONTENT_TYPE, JSON) + .json(&body) + .send()? + .json::>()?; + Ok(resp) + } +} + /// JSON-RPC Error #[derive(Debug, Deserialize, Serialize, Clone)] pub struct JsonRpcError { @@ -41,32 +79,3 @@ pub struct JsonRpcResponse { pub struct BlindedUtxoParam { blinded_utxo: String, } - -pub(crate) fn get_blocking_client() -> BlockingClient { - BlockingClient::builder() - .timeout(Duration::from_secs(PROXY_TIMEOUT as u64)) - .build() - .expect("valid proxy") -} - -pub(crate) fn get_consignment( - url: &str, - consignment_id: String, -) -> Result, reqwest::Error> { - task::block_in_place(|| { - let body = JsonRpcRequest { - method: s!("consignment.get"), - jsonrpc: s!("2.0"), - id: None, - params: Some(BlindedUtxoParam { - blinded_utxo: consignment_id, - }), - }; - get_blocking_client() - .post(url) - .header(CONTENT_TYPE, JSON) - .json(&body) - .send()? - .json::>() - }) -} diff --git a/rgb-common/src/rgb_manager.rs b/rgb-common/src/rgb_manager.rs index c91cb0b..d2ec538 100644 --- a/rgb-common/src/rgb_manager.rs +++ b/rgb-common/src/rgb_manager.rs @@ -3,12 +3,14 @@ use std::str::FromStr; use std::sync::Arc; use std::sync::Mutex; +use bitcoin::Network; + use crate::lib::wallet::{DatabaseType, Wallet, WalletData}; use crate::lib::BitcoinNetwork; use crate::proxy; pub struct RGBManager { - proxy_client: Arc, + proxy_client: Arc, wallet: Arc>, } @@ -20,8 +22,8 @@ impl std::fmt::Debug for RGBManager { impl RGBManager { pub fn init(root_dir: &str, pubkey: &str, network: &str) -> anyhow::Result { - let client = proxy::get_blocking_client(); - let wallet = Wallet::new(WalletData { + let client = proxy::Client::new(network)?; + let mut wallet = Wallet::new(WalletData { data_dir: root_dir.to_owned(), bitcoin_network: BitcoinNetwork::from_str(network)?, database_type: DatabaseType::Sqlite, @@ -30,7 +32,16 @@ impl RGBManager { mnemonic: None, vanilla_keychain: None, })?; - // FIXME: go online + let network = Network::from_str(network)?; + let url = match network { + Network::Bitcoin => "https://mempool.space/api", + Network::Testnet => "https://mempool.space/testnet/api", + Network::Signet => "https://mempool.space/signet/api", + Network::Regtest => "", + }; + if !url.is_empty() { + let _ = wallet.go_online(false, url.to_owned())?; + } // FIXME: setting up the correct proxy client URL Ok(Self { proxy_client: Arc::new(client), @@ -42,7 +53,7 @@ impl RGBManager { self.wallet.clone() } - pub fn proxy_client(&self) -> Arc { + pub fn proxy_client(&self) -> Arc { self.proxy_client.clone() } } diff --git a/rgb-testing/src/lib.rs b/rgb-testing/src/lib.rs index 0ab7e48..cfdbbe7 100644 --- a/rgb-testing/src/lib.rs +++ b/rgb-testing/src/lib.rs @@ -33,9 +33,11 @@ mod tests { "regtest", ) .await?; - let result = cln.rpc().call::("getinfo", json::json!({})); + let result = cln + .rpc() + .call::("getinfo", json::json!({})); + log::info!(target: "test_init_plugin", "{:?}", result); assert!(result.is_ok(), "{:?}", result); Ok(()) - } } diff --git a/rust-toolchain b/rust-toolchain index 07cde98..2bf5ad0 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.75 +stable