lightning_signer/policy/
mod.rs1#[macro_use]
3pub mod error;
4pub mod filter;
6#[cfg(feature = "test_utils")]
8pub mod null_validator;
9pub mod onchain_validator;
11pub mod simple_validator;
13pub mod validator;
15
16use crate::policy::error::temporary_policy_error;
17use crate::prelude::*;
18use crate::util::velocity::{VelocityControlIntervalType, VelocityControlSpec};
19use core::time::Duration;
20use error::{policy_error, ValidationError};
21use filter::{FilterResult, PolicyFilter};
22use log::warn;
23
24pub const DEFAULT_FEE_VELOCITY_CONTROL: VelocityControlSpec = VelocityControlSpec {
26 limit_msat: 1_000_000_000,
27 interval_type: VelocityControlIntervalType::Daily,
28};
29
30pub const MAX_CHANNELS: usize = 1000; pub const MAX_INVOICES: usize = 1000;
35
36pub const MAX_ONCHAIN_TX_SIZE: usize = 32 * 1024;
38
39pub const MIN_INVOICE_EXPIRY: Duration = Duration::from_secs(60);
41
42pub const MAX_CLOCK_SKEW: Duration = Duration::from_secs(60);
44
45pub trait Policy: Send + Sync {
47 fn policy_error(&self, _tag: String, msg: String) -> Result<(), error::ValidationError>;
50 fn temporary_policy_error(
53 &self,
54 _tag: String,
55 msg: String,
56 ) -> Result<(), error::ValidationError>;
57 fn policy_log(&self, _tag: String, msg: String);
59 fn global_velocity_control(&self) -> VelocityControlSpec;
61 fn max_channels(&self) -> usize {
63 MAX_CHANNELS
64 }
65 fn max_invoices(&self) -> usize {
67 MAX_INVOICES
68 }
69 fn fee_velocity_control(&self) -> VelocityControlSpec;
71}
72
73fn policy_error_with_filter(
74 tag: String,
75 msg: String,
76 filter: &PolicyFilter,
77) -> Result<(), ValidationError> {
78 warn!("policy failed: {} {}", tag, msg);
79
80 if filter.filter(tag.clone()) == FilterResult::Error {
81 Err(policy_error(msg))
82 } else {
83 #[cfg(feature = "use_backtrace")]
84 warn!("BACKTRACE:\n{:?}", backtrace::Backtrace::new());
85 Ok(())
86 }
87}
88
89fn temporary_policy_error_with_filter(
90 tag: String,
91 msg: String,
92 filter: &PolicyFilter,
93) -> Result<(), ValidationError> {
94 if filter.filter(tag.clone()) == FilterResult::Error {
95 Err(temporary_policy_error(msg))
96 } else {
97 warn!("policy temporarily failed: {} {}", tag, msg);
98 #[cfg(feature = "use_backtrace")]
99 warn!("BACKTRACE:\n{:?}", backtrace::Backtrace::new());
100 Ok(())
101 }
102}