#[macro_use]
pub mod error;
pub mod filter;
#[cfg(feature = "test_utils")]
pub mod null_validator;
pub mod onchain_validator;
pub mod simple_validator;
pub mod validator;
use crate::policy::error::temporary_policy_error;
use crate::prelude::*;
use crate::util::velocity::{VelocityControlIntervalType, VelocityControlSpec};
use core::time::Duration;
use error::{policy_error, ValidationError};
use filter::{FilterResult, PolicyFilter};
use log::warn;
pub const DEFAULT_FEE_VELOCITY_CONTROL: VelocityControlSpec = VelocityControlSpec {
limit_msat: 1_000_000_000,
interval_type: VelocityControlIntervalType::Daily,
};
pub const MAX_CHANNELS: usize = 1000; pub const MAX_INVOICES: usize = 1000;
pub const MAX_ONCHAIN_TX_SIZE: usize = 32 * 1024;
pub const MIN_INVOICE_EXPIRY: Duration = Duration::from_secs(60);
pub const MAX_CLOCK_SKEW: Duration = Duration::from_secs(60);
pub trait Policy {
fn policy_error(&self, _tag: String, msg: String) -> Result<(), error::ValidationError>;
fn temporary_policy_error(
&self,
_tag: String,
msg: String,
) -> Result<(), error::ValidationError>;
fn policy_log(&self, _tag: String, msg: String);
fn global_velocity_control(&self) -> VelocityControlSpec;
fn max_channels(&self) -> usize {
MAX_CHANNELS
}
fn max_invoices(&self) -> usize {
MAX_INVOICES
}
fn fee_velocity_control(&self) -> VelocityControlSpec;
}
fn policy_error_with_filter(
tag: String,
msg: String,
filter: &PolicyFilter,
) -> Result<(), ValidationError> {
warn!("policy failed: {} {}", tag, msg);
if filter.filter(tag.clone()) == FilterResult::Error {
Err(policy_error(msg))
} else {
#[cfg(feature = "use_backtrace")]
warn!("BACKTRACE:\n{:?}", backtrace::Backtrace::new());
Ok(())
}
}
fn temporary_policy_error_with_filter(
tag: String,
msg: String,
filter: &PolicyFilter,
) -> Result<(), ValidationError> {
if filter.filter(tag.clone()) == FilterResult::Error {
Err(temporary_policy_error(msg))
} else {
warn!("policy temporarily failed: {} {}", tag, msg);
#[cfg(feature = "use_backtrace")]
warn!("BACKTRACE:\n{:?}", backtrace::Backtrace::new());
Ok(())
}
}