Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zk2u committed Nov 13, 2024
1 parent dd244e9 commit 1dc8cc7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions faucet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ip_src = "ConnectInfo"
seed_file = "faucet.seed"
sqlite_file = "faucet.sqlite"
network = "signet"
esplora = "https://esploraapi.devnet-annapurna.stratabtc.org"
l2_http_endpoint = "https://stratareth.devnet-annapurna.stratabtc.org"
esplora = "https://esploraapi53d3659b.devnet-annapurna.stratabtc.org"
l2_http_endpoint = "https://stratareth3666f0713.devnet-annapurna.stratabtc.org"
sats_per_claim = 1_002_000_000
pow_difficulty = 19
16 changes: 14 additions & 2 deletions src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct BatcherNotAvailable(SendError);
pub struct BatcherConfig {
pub period: Duration,
pub max_per_tx: usize,
pub max_in_flight: usize,
}

impl Batcher {
Expand All @@ -65,6 +66,9 @@ impl Batcher {
select! {
biased;
instant = batch_interval.tick() => {
if l1_payout_queue.is_empty() {
continue
}
let span = info_span!("batch processing", batch = ?instant);
let _guard = span.enter();

Expand All @@ -78,7 +82,13 @@ impl Batcher {
psbt.add_recipient(req.address.script_pubkey(), req.amount);
total_sent += req.amount;
}
let mut psbt = psbt.finish().expect("valid tx");
let mut psbt = match psbt.finish() {
Ok(psbt) => psbt,
Err(e) => {
error!("failed finalizing tx: {e:?}");
continue;
}
};

let l1w = RwLockWriteGuard::downgrade(l1w);

Expand All @@ -105,7 +115,9 @@ impl Batcher {
}
req = rx.recv() => match req {
Ok(req) => match req {
PayoutRequest::L1(req) => l1_payout_queue.push_back(req)
PayoutRequest::L1(req) => if l1_payout_queue.len() < cfg.max_in_flight {
l1_payout_queue.push_back(req)
}
},
Err(e) => error!("error receiving PayoutRequest: {e:?}")
}
Expand Down
10 changes: 3 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::{
env,
net::{IpAddr, SocketAddr},
sync::{Arc, LazyLock},
time::Duration,
};

use alloy::{
Expand All @@ -30,7 +29,7 @@ use axum::{
Json, Router,
};
use axum_client_ip::SecureClientIp;
use batcher::{Batcher, BatcherConfig, L1PayoutRequest, PayoutRequest};
use batcher::{Batcher, L1PayoutRequest, PayoutRequest};
use bdk_wallet::{
bitcoin::{address::NetworkUnchecked, Address as L1Address},
KeychainKind,
Expand Down Expand Up @@ -82,12 +81,9 @@ async fn main() {

let l2_wallet = L2Wallet::new(&seed).expect("l2 wallet creation to succeed");
let l1_wallet = Arc::new(RwLock::new(l1_wallet));
let mut batcher = Batcher::new(BatcherConfig {
period: Duration::from_secs(30),
max_per_tx: 250,
});

let mut batcher = Batcher::new(SETTINGS.batcher.clone());
batcher.start(l1_wallet.clone());

L1Wallet::spawn_syncer(l1_wallet.clone());

let state = Arc::new(AppState {
Expand Down
14 changes: 12 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::{
path::PathBuf,
str::FromStr,
sync::LazyLock,
time::Duration,
};

use axum_client_ip::SecureClientIpSource;
use bdk_wallet::bitcoin::{Amount, Network};
use config::Config;
use serde::{Deserialize, Serialize};

use crate::CRATE_NAME;
use crate::{batcher::BatcherConfig, CRATE_NAME};

pub static SETTINGS: LazyLock<Settings> = LazyLock::new(|| {
let args = std::env::args().collect::<Vec<_>>();
Expand Down Expand Up @@ -49,9 +50,12 @@ pub struct InternalSettings {
pub l2_http_endpoint: String,
pub sats_per_claim: Amount,
pub pow_difficulty: u8,
pub batcher_period: Option<u64>,
pub batcher_max_per_batch: Option<usize>,
pub batcher_max_in_flight: Option<usize>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Debug)]
/// Settings struct filled with either config values or
/// opinionated defaults
pub struct Settings {
Expand All @@ -65,6 +69,7 @@ pub struct Settings {
pub l2_http_endpoint: String,
pub sats_per_claim: Amount,
pub pow_difficulty: u8,
pub batcher: BatcherConfig,
}

// on L2, we represent 1 btc as 1 "eth" on the rollup
Expand Down Expand Up @@ -93,6 +98,11 @@ impl TryFrom<InternalSettings> for Settings {
l2_http_endpoint: internal.l2_http_endpoint,
sats_per_claim: internal.sats_per_claim,
pow_difficulty: internal.pow_difficulty,
batcher: BatcherConfig {
period: Duration::from_secs(internal.batcher_period.unwrap_or(30)),
max_per_tx: internal.batcher_max_per_batch.unwrap_or(250),
max_in_flight: internal.batcher_max_in_flight.unwrap_or(2500),
},
})
}
}

0 comments on commit 1dc8cc7

Please sign in to comment.