Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions roles/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions roles/mint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
shared_config = { path = "../roles-utils/config" }
2 changes: 1 addition & 1 deletion roles/pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
2 changes: 1 addition & 1 deletion roles/translator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 44 additions & 27 deletions roles/translator/src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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},
Expand Down Expand Up @@ -41,14 +44,14 @@ pub struct TranslatorSv2 {
config: ProxyConfig,
reconnect_wait_time: u64,
wallet: Arc<Wallet>,
mint_client: HttpClient,
}

fn create_wallet() -> Arc<Wallet> {
fn create_wallet(mint_url: &str) -> Arc<Wallet> {
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())
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -326,9 +333,7 @@ impl TranslatorSv2 {
}
};

for quote in &quotes {
Self::process_quote(&wallet, &mut conn, &rt, quote);
}
Self::process_quotes_batch(&wallet, &mut conn, &rt, &quotes, &mint_client);

thread::sleep(Duration::from_secs(60));
}
Expand All @@ -345,32 +350,44 @@ impl TranslatorSv2 {
}
}

fn lookup_uuid(conn: &mut Connection, redis_key: &str) -> Option<String> {
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<String, String> {
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::<HashMap<String, String>>()
},
Err(e) => {
tracing::error!("Failed to lookup batch UUIDs from mint: {}", e);
HashMap::new()
}
}
}

fn process_quote(wallet: &Arc<Wallet>, 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<Wallet>, 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, &quote.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<String> = quotes.iter().map(|q| q.id.clone()).collect();
let uuid_mapping = Self::lookup_uuids_batch(rt, mint_client, &quote_ids);

for quote in quotes {
if let Some(uuid) = uuid_mapping.get(&quote.id) {
// TODO get latest keyset
match rt.block_on(wallet.get_mining_share_proofs(uuid, &quote.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);
}
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions roles/translator/src/lib/proxy_config.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -73,6 +80,7 @@ impl ProxyConfig {
min_extranonce2_size,
downstream_difficulty_config: downstream.difficulty_config,
upstream_difficulty_config: upstream.difficulty_config,
mint_url,
}
}
}
Expand Down
Loading