Skip to content

Commit

Permalink
Merge pull request #521 from dleutenegger/main
Browse files Browse the repository at this point in the history
Extend payment type filter to include `ClosedChannels` type
  • Loading branch information
roeierez authored Oct 25, 2023
2 parents 7e5f4f0 + 970a13b commit 173b7fb
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 59 deletions.
4 changes: 2 additions & 2 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ dictionary CheckMessageResponse {
enum PaymentTypeFilter {
"Sent",
"Received",
"All",
"ClosedChannels",
};

enum PaymentStatus {
Expand All @@ -148,7 +148,7 @@ dictionary Payment {
};

dictionary ListPaymentsRequest {
PaymentTypeFilter filter;
sequence<PaymentTypeFilter>? filters = null;
i64? from_timestamp = null;
i64? to_timestamp = null;
boolean? include_failures = null;
Expand Down
9 changes: 6 additions & 3 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,7 @@ pub(crate) mod tests {

let all = breez_services
.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -2053,7 +2053,7 @@ pub(crate) mod tests {

let received = breez_services
.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::Received,
filters: Some(vec![PaymentTypeFilter::Received]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -2065,7 +2065,10 @@ pub(crate) mod tests {

let sent = breez_services
.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::Sent,
filters: Some(vec![
PaymentTypeFilter::Sent,
PaymentTypeFilter::ClosedChannels,
]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand Down
31 changes: 28 additions & 3 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ pub extern "C" fn new_box_autoadd_u64_0(value: u64) -> *mut u64 {
support::new_leak_box_ptr(value)
}

#[no_mangle]
pub extern "C" fn new_list_payment_type_filter_0(len: i32) -> *mut wire_list_payment_type_filter {
let wrap = wire_list_payment_type_filter {
ptr: support::new_leak_vec_ptr(Default::default(), len),
len,
};
support::new_leak_box_ptr(wrap)
}

#[no_mangle]
pub extern "C" fn new_uint_8_list_0(len: i32) -> *mut wire_uint_8_list {
let ans = wire_uint_8_list {
Expand Down Expand Up @@ -587,10 +596,19 @@ impl Wire2Api<GreenlightNodeConfig> for wire_GreenlightNodeConfig {
}
}

impl Wire2Api<Vec<PaymentTypeFilter>> for *mut wire_list_payment_type_filter {
fn wire2api(self) -> Vec<PaymentTypeFilter> {
let vec = unsafe {
let wrap = support::box_from_leak_ptr(self);
support::vec_from_leak_ptr(wrap.ptr, wrap.len)
};
vec.into_iter().map(Wire2Api::wire2api).collect()
}
}
impl Wire2Api<ListPaymentsRequest> for wire_ListPaymentsRequest {
fn wire2api(self) -> ListPaymentsRequest {
ListPaymentsRequest {
filter: self.filter.wire2api(),
filters: self.filters.wire2api(),
from_timestamp: self.from_timestamp.wire2api(),
to_timestamp: self.to_timestamp.wire2api(),
include_failures: self.include_failures.wire2api(),
Expand Down Expand Up @@ -826,10 +844,17 @@ pub struct wire_GreenlightNodeConfig {
invite_code: *mut wire_uint_8_list,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_list_payment_type_filter {
ptr: *mut i32,
len: i32,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_ListPaymentsRequest {
filter: i32,
filters: *mut wire_list_payment_type_filter,
from_timestamp: *mut i64,
to_timestamp: *mut i64,
include_failures: *mut bool,
Expand Down Expand Up @@ -1100,7 +1125,7 @@ impl Default for wire_GreenlightNodeConfig {
impl NewWithNullPtr for wire_ListPaymentsRequest {
fn new_with_null_ptr() -> Self {
Self {
filter: Default::default(),
filters: core::ptr::null_mut(),
from_timestamp: core::ptr::null_mut(),
to_timestamp: core::ptr::null_mut(),
include_failures: core::ptr::null_mut(),
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ impl Wire2Api<PaymentTypeFilter> for i32 {
match self {
0 => PaymentTypeFilter::Sent,
1 => PaymentTypeFilter::Received,
2 => PaymentTypeFilter::All,
2 => PaymentTypeFilter::ClosedChannels,
_ => unreachable!("Invalid variant for PaymentTypeFilter: {}", self),
}
}
Expand Down
7 changes: 4 additions & 3 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const SWAP_PAYMENT_FEE_EXPIRY_SECONDS: u32 = 60 * 60 * 24 * 2; // 2 days
pub const INVOICE_PAYMENT_FEE_EXPIRY_SECONDS: u32 = 60 * 60; // 60 minutes

/// Different types of supported payments
#[derive(Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize)]
#[derive(Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize, Hash)]
pub enum PaymentType {
Sent,
Received,
Expand Down Expand Up @@ -576,10 +576,11 @@ impl From<Network> for bitcoin::network::constants::Network {
}

/// Different types of supported filters which can be applied when retrieving the transaction list
#[derive(PartialEq)]
pub enum PaymentTypeFilter {
Sent,
Received,
All,
ClosedChannels,
}

/// Different types of supported feerates
Expand Down Expand Up @@ -656,7 +657,7 @@ pub struct Payment {

/// Represents a list payments request.
pub struct ListPaymentsRequest {
pub filter: PaymentTypeFilter,
pub filters: Option<Vec<PaymentTypeFilter>>,
pub from_timestamp: Option<i64>,
pub to_timestamp: Option<i64>,
pub include_failures: Option<bool>,
Expand Down
61 changes: 41 additions & 20 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{anyhow, Result};
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use rusqlite::Row;
use rusqlite::{params, OptionalExtension};
use std::collections::HashSet;

use std::str::FromStr;

Expand Down Expand Up @@ -133,7 +134,7 @@ impl SqliteStorage {
/// or [Self::get_completed_payment_by_hash]
pub fn list_payments(&self, req: ListPaymentsRequest) -> SdkResult<Vec<Payment>> {
let where_clause = filter_to_where_clause(
req.filter,
req.filters,
req.from_timestamp,
req.to_timestamp,
req.include_failures,
Expand Down Expand Up @@ -262,7 +263,7 @@ impl SqliteStorage {
}

fn filter_to_where_clause(
type_filter: PaymentTypeFilter,
type_filters: Option<Vec<PaymentTypeFilter>>,
from_timestamp: Option<i64>,
to_timestamp: Option<i64>,
include_failures: Option<bool>,
Expand All @@ -280,18 +281,32 @@ fn filter_to_where_clause(
where_clause.push(format!("status != {}", PaymentStatus::Failed as i64));
};

match type_filter {
PaymentTypeFilter::Sent => {
if let Some(filters) = type_filters {
if !filters.is_empty() {
let mut type_filter_clause: HashSet<PaymentType> = HashSet::new();
for type_filter in filters {
match type_filter {
PaymentTypeFilter::Sent => {
type_filter_clause.insert(PaymentType::Sent);
}
PaymentTypeFilter::Received => {
type_filter_clause.insert(PaymentType::Received);
}
PaymentTypeFilter::ClosedChannels => {
type_filter_clause.insert(PaymentType::ClosedChannel);
}
}
}

where_clause.push(format!(
"payment_type in ('{}','{}') ",
PaymentType::Sent,
PaymentType::ClosedChannel
"payment_type in ({})",
type_filter_clause
.iter()
.map(|t| format!("'{}'", t))
.collect::<Vec<_>>()
.join(", ")
));
}
PaymentTypeFilter::Received => {
where_clause.push(format!("payment_type = '{}' ", PaymentType::Received));
}
PaymentTypeFilter::All => (),
}

let mut where_clause_str = String::new();
Expand Down Expand Up @@ -460,7 +475,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// retrieve all
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -472,7 +487,10 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

//test only sent
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::Sent,
filters: Some(vec![
PaymentTypeFilter::Sent,
PaymentTypeFilter::ClosedChannels,
]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -490,7 +508,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

//test only received
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::Received,
filters: Some(vec![PaymentTypeFilter::Received]),
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -505,7 +523,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

storage.insert_or_update_payments(&txs)?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -517,7 +535,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

storage.insert_open_channel_payment_info("123", 150)?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: None,
Expand All @@ -528,7 +546,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test all with failures
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(true),
Expand All @@ -539,7 +557,10 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test sent with failures
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::Sent,
filters: Some(vec![
PaymentTypeFilter::Sent,
PaymentTypeFilter::ClosedChannels,
]),
from_timestamp: None,
to_timestamp: None,
include_failures: Some(true),
Expand All @@ -550,7 +571,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test limit
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(false),
Expand All @@ -561,7 +582,7 @@ fn test_ln_transactions() -> Result<(), Box<dyn std::error::Error>> {

// test offset
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filter: PaymentTypeFilter::All,
filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(false),
Expand Down
10 changes: 9 additions & 1 deletion libs/sdk-flutter/ios/Classes/bridge_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ typedef struct wire_StaticBackupRequest {
struct wire_uint_8_list *working_dir;
} wire_StaticBackupRequest;

typedef struct wire_list_payment_type_filter {
int32_t *ptr;
int32_t len;
} wire_list_payment_type_filter;

typedef struct wire_ListPaymentsRequest {
int32_t filter;
struct wire_list_payment_type_filter *filters;
int64_t *from_timestamp;
int64_t *to_timestamp;
bool *include_failures;
Expand Down Expand Up @@ -338,6 +343,8 @@ uint32_t *new_box_autoadd_u32_0(uint32_t value);

uint64_t *new_box_autoadd_u64_0(uint64_t value);

struct wire_list_payment_type_filter *new_list_payment_type_filter_0(int32_t len);

struct wire_uint_8_list *new_uint_8_list_0(int32_t len);

union NodeConfigKind *inflate_NodeConfig_Greenlight(void);
Expand Down Expand Up @@ -416,6 +423,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) new_box_autoadd_sweep_request_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_u32_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_u64_0);
dummy_var ^= ((int64_t) (void*) new_list_payment_type_filter_0);
dummy_var ^= ((int64_t) (void*) new_uint_8_list_0);
dummy_var ^= ((int64_t) (void*) inflate_NodeConfig_Greenlight);
dummy_var ^= ((int64_t) (void*) free_WireSyncReturn);
Expand Down
Loading

0 comments on commit 173b7fb

Please sign in to comment.