Skip to content

Commit

Permalink
refactor: country IP ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 16, 2025
1 parent 7cd3b9b commit 0aa8240
Show file tree
Hide file tree
Showing 28 changed files with 885 additions and 359 deletions.
22 changes: 22 additions & 0 deletions boltzr/Cargo.lock

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

1 change: 1 addition & 0 deletions boltzr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fedimint-tonic-lnd = { version = "0.2.0", features = ["lightningrpc"], default-f
flate2 = "1.0.35"
pyroscope = { version = "0.5.8", optional = true }
pyroscope_pprofrs = { version = "0.2.8", optional = true }
csv = "1.3.1"

[build-dependencies]
built = { version = "0.7.5", features = ["git2"] }
Expand Down
10 changes: 10 additions & 0 deletions boltzr/protos/boltzr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ service BoltzR {
rpc DecodeInvoiceOrOffer (DecodeInvoiceOrOfferRequest) returns (DecodeInvoiceOrOfferResponse);
rpc FetchInvoice (FetchInvoiceRequest) returns (FetchInvoiceResponse);

rpc IsMarked (IsMarkedRequest) returns (IsMarkedResponse);

rpc ScanMempool (ScanMempoolRequest) returns (ScanMempoolResponse);
}

Expand Down Expand Up @@ -225,6 +227,14 @@ message FetchInvoiceResponse {
string invoice = 1;
}

message IsMarkedRequest {
string ip = 1;
}

message IsMarkedResponse {
bool is_marked = 1;
}

message ScanMempoolRequest {
repeated string symbols = 1;
}
Expand Down
3 changes: 3 additions & 0 deletions boltzr/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub struct GlobalConfig {
#[serde(rename = "mnemonicpathEvm")]
pub mnemonic_path_evm: Option<String>,

#[serde(rename = "marking")]
pub marking: Option<crate::service::MarkingsConfig>,

pub backup: Option<crate::backup::Config>,
pub notification: Option<crate::notifications::Config>,

Expand Down
8 changes: 8 additions & 0 deletions boltzr/src/grpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::grpc::service::BoltzService;
use crate::grpc::status_fetcher::StatusFetcher;
use crate::grpc::tls::load_certificates;
use crate::notifications::NotificationClient;
use crate::service::Service;
use crate::swap::manager::SwapManager;
use crate::tracing_setup::ReloadHandler;
use crate::webhook::caller::Caller;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub struct Server<M, W, N> {

log_reload_handler: ReloadHandler,

service: Arc<Service>,
manager: Arc<M>,

web_hook_helper: Box<W>,
Expand All @@ -61,6 +63,7 @@ where
cancellation_token: CancellationToken,
config: Config,
log_reload_handler: ReloadHandler,
service: Arc<Service>,
manager: Arc<M>,
swap_status_update_tx: tokio::sync::broadcast::Sender<Vec<SwapStatus>>,
web_hook_helper: Box<W>,
Expand All @@ -70,6 +73,7 @@ where
) -> Self {
Server {
config,
service,
manager,
refund_signer,
web_hook_helper,
Expand Down Expand Up @@ -98,6 +102,7 @@ where

let service = BoltzService::new(
self.log_reload_handler.clone(),
self.service.clone(),
self.manager.clone(),
self.status_fetcher.clone(),
self.swap_status_update_tx.clone(),
Expand Down Expand Up @@ -162,6 +167,7 @@ mod server_test {
use crate::grpc::service::boltzr::boltz_r_client::BoltzRClient;
use crate::grpc::service::boltzr::GetInfoRequest;
use crate::notifications::commands::Commands;
use crate::service::Service;
use crate::swap::manager::SwapManager;
use crate::tracing_setup::ReloadHandler;
use crate::webhook::caller;
Expand Down Expand Up @@ -243,6 +249,7 @@ mod server_test {
disable_ssl: Some(true),
},
ReloadHandler::new(),
Arc::new(Service::new(None)),
Arc::new(make_mock_manager()),
status_tx,
Box::new(make_mock_hook_helper()),
Expand Down Expand Up @@ -370,6 +377,7 @@ mod server_test {
disable_ssl: Some(false),
},
ReloadHandler::new(),
Arc::new(Service::new(None)),
Arc::new(make_mock_manager()),
status_tx,
Box::new(make_mock_hook_helper()),
Expand Down
36 changes: 30 additions & 6 deletions boltzr/src/grpc/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ use crate::grpc::service::boltzr::{
bolt11_invoice, bolt12_invoice, decode_invoice_or_offer_response, Bolt11Invoice, Bolt12Invoice,
Bolt12Offer, CreateWebHookRequest, CreateWebHookResponse, DecodeInvoiceOrOfferRequest,
DecodeInvoiceOrOfferResponse, Feature, FetchInvoiceRequest, FetchInvoiceResponse,
GetInfoRequest, GetInfoResponse, GetMessagesRequest, GetMessagesResponse, LogLevel,
ScanMempoolRequest, ScanMempoolResponse, SendMessageRequest, SendMessageResponse,
SendSwapUpdateRequest, SendSwapUpdateResponse, SendWebHookRequest, SendWebHookResponse,
SetLogLevelRequest, SetLogLevelResponse, SignEvmRefundRequest, SignEvmRefundResponse,
StartWebHookRetriesRequest, StartWebHookRetriesResponse, SwapUpdate, SwapUpdateRequest,
SwapUpdateResponse,
GetInfoRequest, GetInfoResponse, GetMessagesRequest, GetMessagesResponse, IsMarkedRequest,
IsMarkedResponse, LogLevel, ScanMempoolRequest, ScanMempoolResponse, SendMessageRequest,
SendMessageResponse, SendSwapUpdateRequest, SendSwapUpdateResponse, SendWebHookRequest,
SendWebHookResponse, SetLogLevelRequest, SetLogLevelResponse, SignEvmRefundRequest,
SignEvmRefundResponse, StartWebHookRetriesRequest, StartWebHookRetriesResponse, SwapUpdate,
SwapUpdateRequest, SwapUpdateResponse,
};
use crate::grpc::status_fetcher::StatusFetcher;
use crate::lightning::invoice::Invoice;
use crate::notifications::NotificationClient;
use crate::service::Service;
use crate::swap::manager::SwapManager;
use crate::tracing_setup::ReloadHandler;
use crate::webhook::caller::Caller;
Expand All @@ -31,6 +32,7 @@ use lightning::util::ser::Writeable;
use lightning_invoice::Bolt11InvoiceDescription;
use std::cell::Cell;
use std::collections::HashMap;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::time::UNIX_EPOCH;
Expand All @@ -48,6 +50,7 @@ pub struct BoltzService<M, T> {
log_reload_handler: ReloadHandler,
web_hook_retry_handle: Arc<Mutex<Cell<Option<tokio::task::JoinHandle<()>>>>>,

service: Arc<Service>,
manager: Arc<M>,

web_hook_helper: Arc<Box<dyn WebHookHelper + Sync + Send>>,
Expand All @@ -64,6 +67,7 @@ impl<M, T> BoltzService<M, T> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
log_reload_handler: ReloadHandler,
service: Arc<Service>,
manager: Arc<M>,
status_fetcher: StatusFetcher,
swap_status_update_tx: tokio::sync::broadcast::Sender<Vec<SwapStatus>>,
Expand All @@ -74,6 +78,7 @@ impl<M, T> BoltzService<M, T> {
) -> Self {
BoltzService {
manager,
service,
refund_signer,
status_fetcher,
web_hook_caller,
Expand Down Expand Up @@ -651,6 +656,23 @@ where
}
}

async fn is_marked(
&self,
request: Request<IsMarkedRequest>,
) -> Result<Response<IsMarkedResponse>, Status> {
extract_parent_context(&request);

match request.into_inner().ip.parse::<IpAddr>() {
Ok(ip) => Ok(Response::new(IsMarkedResponse {
is_marked: self.service.country_codes.is_relevant(&ip),
})),
Err(err) => Err(Status::new(
Code::InvalidArgument,
format!("could not parse ip: {}", err),
)),
}
}

#[instrument(name = "grpc::scan_mempool", skip_all)]
async fn scan_mempool(
&self,
Expand Down Expand Up @@ -719,6 +741,7 @@ mod test {
use crate::grpc::service::BoltzService;
use crate::grpc::status_fetcher::StatusFetcher;
use crate::notifications::commands::Commands;
use crate::service::Service;
use crate::swap::manager::SwapManager;
use crate::tracing_setup::ReloadHandler;
use crate::webhook::caller::{Caller, Config};
Expand Down Expand Up @@ -1075,6 +1098,7 @@ mod test {
token.clone(),
BoltzService::new(
ReloadHandler::new(),
Arc::new(Service::new(None)),
Arc::new(make_mock_manager()),
StatusFetcher::new(),
status_tx,
Expand Down
15 changes: 15 additions & 0 deletions boltzr/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::config::parse_config;
use crate::currencies::connect_nodes;
use crate::service::Service;
use crate::swap::manager::Manager;
use api::ws;
use clap::Parser;
Expand All @@ -18,6 +19,7 @@ mod evm;
mod grpc;
mod lightning;
mod notifications;
mod service;
mod swap;
mod tracing_setup;
mod utils;
Expand Down Expand Up @@ -112,6 +114,18 @@ async fn main() {

let cancellation_token = tokio_util::sync::CancellationToken::new();

let service = Arc::new(Service::new(config.marking));
{
let service = service.clone();
let cancellation_token = cancellation_token.clone();
tokio::spawn(async move {
if let Err(err) = service.start().await {
error!("Could not start service: {}", err);
cancellation_token.cancel();
}
});
}

#[cfg(feature = "metrics")]
let mut metrics_server =
metrics::server::Server::new(cancellation_token.clone(), config.sidecar.metrics);
Expand Down Expand Up @@ -211,6 +225,7 @@ async fn main() {
cancellation_token.clone(),
config.sidecar.grpc,
log_reload_handler,
service,
swap_manager.clone(),
swap_status_update_tx.clone(),
Box::new(db::helpers::web_hook::WebHookHelperDatabase::new(db_pool)),
Expand Down
Loading

0 comments on commit 0aa8240

Please sign in to comment.