diff --git a/roles/Cargo.lock b/roles/Cargo.lock index 6f4bb7202..e38b58961 100644 --- a/roles/Cargo.lock +++ b/roles/Cargo.lock @@ -901,7 +901,7 @@ dependencies = [ [[package]] name = "cashu" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "bitcoin 0.32.5", "bitcoin_hashes 0.16.0", @@ -952,7 +952,7 @@ dependencies = [ [[package]] name = "cdk" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", @@ -981,15 +981,17 @@ dependencies = [ [[package]] name = "cdk-axum" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", "axum", "cdk", + "cdk-common", "futures", "moka", "paste", + "redis 0.23.3", "serde", "serde_json", "sha2 0.10.8", @@ -1001,7 +1003,7 @@ dependencies = [ [[package]] name = "cdk-cln" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "async-trait", "bitcoin 0.32.5", @@ -1018,7 +1020,7 @@ dependencies = [ [[package]] name = "cdk-common" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", @@ -1041,7 +1043,7 @@ dependencies = [ [[package]] name = "cdk-fake-wallet" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "async-trait", "bitcoin 0.32.5", @@ -1060,7 +1062,7 @@ dependencies = [ [[package]] name = "cdk-lnbits" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", @@ -1078,7 +1080,7 @@ dependencies = [ [[package]] name = "cdk-lnd" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", @@ -1094,7 +1096,7 @@ dependencies = [ [[package]] name = "cdk-mint-rpc" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "cdk", @@ -1114,7 +1116,7 @@ dependencies = [ [[package]] name = "cdk-mintd" version = "0.7.2" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "axum", @@ -1147,7 +1149,7 @@ dependencies = [ [[package]] name = "cdk-phoenixd" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", @@ -1166,7 +1168,7 @@ dependencies = [ [[package]] name = "cdk-redb" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "async-trait", "cdk-common", @@ -1182,7 +1184,7 @@ dependencies = [ [[package]] name = "cdk-sqlite" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "async-trait", "bitcoin 0.32.5", @@ -1199,7 +1201,7 @@ dependencies = [ [[package]] name = "cdk-strike" version = "0.7.1" -source = "git+https://github.com/vnprc/cdk?rev=773af52b#773af52b5977efa53fa4d094134425270497e422" +source = "git+https://github.com/vnprc/cdk?rev=64ec7f6f#64ec7f6f8681fc3707a0f5e023fcdb2fa0a60a5e" dependencies = [ "anyhow", "async-trait", @@ -3910,10 +3912,13 @@ dependencies = [ "itoa", "percent-encoding", "pin-project-lite", + "rustls 0.21.12", + "rustls-native-certs 0.6.3", "ryu", "sha1_smol", "socket2 0.4.10", "tokio", + "tokio-rustls 0.24.1", "tokio-util", "url", ] diff --git a/roles/mint/Cargo.toml b/roles/mint/Cargo.toml index 500515607..64bd2c3a0 100644 --- a/roles/mint/Cargo.toml +++ b/roles/mint/Cargo.toml @@ -17,11 +17,11 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" bitcoin = { version= "0.32.2" } -cdk = { git = "https://github.com/vnprc/cdk.git", package = "cdk", rev = "773af52b" } -cdk-axum = { git = "https://github.com/vnprc/cdk.git", package = "cdk-axum", rev = "773af52b" } -cdk-mintd = { git = "https://github.com/vnprc/cdk.git", package = "cdk-mintd", rev = "773af52b" } -cdk-sqlite = { git = "https://github.com/vnprc/cdk.git", package = "cdk-sqlite", rev = "773af52b" } -cdk-redb = { git = "https://github.com/vnprc/cdk.git", package = "cdk-redb", rev = "773af52b" } +cdk = { git = "https://github.com/vnprc/cdk.git", package = "cdk", rev = "64ec7f6f" } +cdk-axum = { git = "https://github.com/vnprc/cdk.git", package = "cdk-axum", rev = "64ec7f6f", features = ["redis"] } +cdk-mintd = { git = "https://github.com/vnprc/cdk.git", package = "cdk-mintd", rev = "64ec7f6f" } +cdk-sqlite = { git = "https://github.com/vnprc/cdk.git", package = "cdk-sqlite", rev = "64ec7f6f" } +cdk-redb = { git = "https://github.com/vnprc/cdk.git", package = "cdk-redb", rev = "64ec7f6f" } toml = "0.8.22" -shared_config = { path = "../roles-utils/config" } \ No newline at end of file +shared_config = { path = "../roles-utils/config" } diff --git a/roles/pool/Cargo.toml b/roles/pool/Cargo.toml index e9de61250..a2411f0ab 100644 --- a/roles/pool/Cargo.toml +++ b/roles/pool/Cargo.toml @@ -39,7 +39,7 @@ key-utils = { version = "^1.0.0", path = "../../utils/key-utils" } bip39 = { version = "2.0", features = ["rand"] } mining_sv2 = { version = "^1.0.0", path = "../../protocols/v2/subprotocols/mining" } bitcoin = { version= "0.32.2" } -cdk = { git = "https://github.com/vnprc/cdk", rev = "773af52b" } +cdk = { git = "https://github.com/vnprc/cdk", rev = "64ec7f6f" } bitcoin_hashes = { version = "0.16", features = ["serde"] } redis = { version = "0.25", features = ["tokio-comp"] } diff --git a/roles/translator/Cargo.toml b/roles/translator/Cargo.toml index 85649ff9c..a1be36c06 100644 --- a/roles/translator/Cargo.toml +++ b/roles/translator/Cargo.toml @@ -46,7 +46,7 @@ async-compat = "0.2.1" rand = "0.8.4" bitcoin = "0.30" mining_sv2 = { version = "^1.0.0", path = "../../protocols/v2/subprotocols/mining" } -cdk = { git = "https://github.com/vnprc/cdk", rev = "773af52b" } +cdk = { git = "https://github.com/vnprc/cdk", rev = "64ec7f6f" } uuid = { version = "1", features = ["v4"] } ureq = { version = "2", features = ["tls"] } # TODO delete after implementing the cdk HTTP API to retrieve quote id from header hash diff --git a/roles/translator/src/lib/mod.rs b/roles/translator/src/lib/mod.rs index ee84188ed..c4f24816f 100644 --- a/roles/translator/src/lib/mod.rs +++ b/roles/translator/src/lib/mod.rs @@ -1,5 +1,5 @@ use async_channel::{bounded, unbounded}; -use cdk::wallet::{Wallet, MintQuote}; +use cdk::wallet::{client::MintConnector, MintQuote, Wallet}; use futures::FutureExt; use rand::Rng; pub use roles_logic_sv2::utils::Mutex; @@ -8,8 +8,11 @@ use std::{ net::{IpAddr, SocketAddr}, str::FromStr, sync::Arc, + collections::HashMap, }; +use cdk::{HttpClient, mint_url::MintUrl}; + use tokio::{ sync::broadcast, task::{self, AbortHandle}, @@ -41,14 +44,14 @@ pub struct TranslatorSv2 { config: ProxyConfig, reconnect_wait_time: u64, wallet: Arc, + mint_client: HttpClient, } -fn create_wallet() -> Arc { +fn create_wallet(mint_url: &str) -> Arc { use cdk::cdk_database::WalletMemoryDatabase; use cdk::nuts::CurrencyUnit; let seed = rand::thread_rng().gen::<[u8; 32]>(); - let mint_url = "http://127.0.0.1:3338"; let localstore = WalletMemoryDatabase::default(); Arc::new(Wallet::new(mint_url, CurrencyUnit::Custom(HASH_CURRENCY_UNIT.to_string()), Arc::new(localstore), &seed, None).unwrap()) @@ -58,10 +61,12 @@ impl TranslatorSv2 { pub fn new(config: ProxyConfig) -> Self { let mut rng = rand::thread_rng(); let wait_time = rng.gen_range(0..=3000); + let mint_client = HttpClient::new(MintUrl::from_str(&config.mint_url).unwrap()); Self { + wallet: create_wallet(&config.mint_url), config, reconnect_wait_time: wait_time, - wallet: create_wallet(), + mint_client: mint_client, } } @@ -308,6 +313,8 @@ impl TranslatorSv2 { fn spawn_proof_sweeper(&self) { let wallet = self.wallet.clone(); + let mint_client = self.mint_client.clone(); + task::spawn_blocking(move || { let mut conn = match Self::connect_to_redis("redis://localhost:6379") { Some(c) => c, @@ -326,9 +333,7 @@ impl TranslatorSv2 { } }; - for quote in "es { - Self::process_quote(&wallet, &mut conn, &rt, quote); - } + Self::process_quotes_batch(&wallet, &mut conn, &rt, "es, &mint_client); thread::sleep(Duration::from_secs(60)); } @@ -345,32 +350,44 @@ impl TranslatorSv2 { } } - fn lookup_uuid(conn: &mut Connection, redis_key: &str) -> Option { - loop { - match conn.get(redis_key) { - Ok(val) => break Some(val), - Err(e) if e.kind() == redis::ErrorKind::TypeError => break None, - Err(e) => { - tracing::warn!("Retrying Redis lookup for key {}: {}", redis_key, e); - thread::sleep(Duration::from_secs(1)); - } + fn lookup_uuids_batch(rt: &Handle, mint_client: &HttpClient, share_hashes: &[String]) -> std::collections::HashMap { + if share_hashes.is_empty() { + return HashMap::new(); + } + + let quotes_shares_future = mint_client.get_quotes_shares(share_hashes.to_vec()); + + match rt.block_on(quotes_shares_future) { + Ok(response) => { + response.quote_ids.iter().map(|(id, uuid)| (id.clone(), uuid.clone())).collect::>() + }, + Err(e) => { + tracing::error!("Failed to lookup batch UUIDs from mint: {}", e); + HashMap::new() } } } - fn process_quote(wallet: &Arc, conn: &mut Connection, rt: &Handle, quote: &MintQuote) { - let redis_key = format!("mint:quotes:hash:{}", quote.id); - let Some(uuid) = Self::lookup_uuid(conn, &redis_key) else { + fn process_quotes_batch(wallet: &Arc, conn: &mut Connection, rt: &Handle, quotes: &[MintQuote], mint_client: &HttpClient) { + if quotes.is_empty() { return; - }; + } - // TODO get latest keyset - match rt.block_on(wallet.get_mining_share_proofs(&uuid, "e.id)) { - Ok(_proofs) => { - Self::log_success_and_cleanup(wallet, conn, rt, quote, &redis_key); - } - Err(e) => { - tracing::info!("Failed to mint ehash tokens for share {} error: {}", quote.id, e); + let quote_ids: Vec = quotes.iter().map(|q| q.id.clone()).collect(); + let uuid_mapping = Self::lookup_uuids_batch(rt, mint_client, "e_ids); + + for quote in quotes { + if let Some(uuid) = uuid_mapping.get("e.id) { + // TODO get latest keyset + match rt.block_on(wallet.get_mining_share_proofs(uuid, "e.id)) { + Ok(_proofs) => { + let redis_key = format!("mint:quotes:hash:{}", quote.id); + Self::log_success_and_cleanup(wallet, conn, rt, quote, &redis_key); + } + Err(e) => { + tracing::error!("Failed to mint ehash tokens for share {} error: {}", quote.id, e); + } + } } } } diff --git a/roles/translator/src/lib/proxy_config.rs b/roles/translator/src/lib/proxy_config.rs index ec3711c5d..fa0f63739 100644 --- a/roles/translator/src/lib/proxy_config.rs +++ b/roles/translator/src/lib/proxy_config.rs @@ -1,6 +1,10 @@ use key_utils::Secp256k1PublicKey; use serde::Deserialize; +fn default_mint_url() -> String { + "http://127.0.0.1:3338".to_string() +} + #[derive(Debug, Deserialize, Clone)] pub struct ProxyConfig { pub upstream_address: String, @@ -13,6 +17,8 @@ pub struct ProxyConfig { pub min_extranonce2_size: u16, pub downstream_difficulty_config: DownstreamDifficultyConfig, pub upstream_difficulty_config: UpstreamDifficultyConfig, + #[serde(default = "default_mint_url")] + pub mint_url: String, } pub struct UpstreamConfig { @@ -61,6 +67,7 @@ impl ProxyConfig { max_supported_version: u16, min_supported_version: u16, min_extranonce2_size: u16, + mint_url: String, ) -> Self { Self { upstream_address: upstream.address, @@ -73,6 +80,7 @@ impl ProxyConfig { min_extranonce2_size, downstream_difficulty_config: downstream.difficulty_config, upstream_difficulty_config: upstream.difficulty_config, + mint_url, } } }