Skip to content

Commit 797943d

Browse files
authored
Merge pull request #8 from benthecarman/cashu-tests
2 parents c2dd527 + 3dae83d commit 797943d

10 files changed

Lines changed: 340 additions & 96 deletions

File tree

.github/workflows/tests.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: Tests
22

33
on:
44
pull_request:
5+
push:
56
workflow_dispatch:
67

78
jobs:
@@ -17,6 +18,12 @@ jobs:
1718
override: true
1819
profile: minimal
1920

21+
- name: Install Protoc
22+
uses: arduino/setup-protoc@v3
23+
with:
24+
version: "30.2"
25+
repo-token: ${{ secrets.GITHUB_TOKEN }}
26+
2027
- uses: actions/cache@v3
2128
with:
2229
path: |
@@ -36,3 +43,6 @@ jobs:
3643

3744
- name: Run tests
3845
run: cargo test --features _test-utils
46+
47+
- name: Run cashu tests
48+
run: cargo test --features _cashu-tests

ci/check-lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -x
44

55
RUSTC_MINOR_VERSION=$(rustc --version | awk '{ split($2,a,"."); print a[2] }')
66

7-
cargo clippy -- -D warnings \
7+
cargo clippy --features _test-utils -- -D warnings \
88
`# We use this for sat groupings` \
99
-A clippy::inconsistent-digit-grouping \
1010
`# Some stuff we do sometimes when its reasonable` \

justfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ default:
22
@just --list
33

44
test *args:
5-
cargo test {{args}} --features _test-utils -p orange-sdk
5+
cargo test {{ args }} --features _test-utils -p orange-sdk
6+
7+
test-cashu *args:
8+
cargo test {{ args }} --features _cashu-tests -p orange-sdk
69

710
cli:
811
cd examples/cli && cargo run

orange-sdk/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ default = ["spark"]
88
spark = ["spark-wallet", "uuid"]
99
cashu = ["cdk", "serde_json"]
1010
_test-utils = ["corepc-node", "cashu", "uuid/v7", "rand"]
11+
_cashu-tests = ["_test-utils", "cdk-ldk-node", "cdk/mint", "cdk-sqlite", "cdk-axum", "axum"]
1112

1213
[dependencies]
1314
graduated-rebalancer = { path = "../graduated-rebalancer", version = "0.1.0" }
@@ -25,3 +26,7 @@ serde_json = { version = "1.0", optional = true }
2526
async-trait = "0.1"
2627

2728
corepc-node = { version = "0.8.0", features = ["29_0", "download"], optional = true }
29+
cdk-ldk-node = { version = "0.12.0", optional = true }
30+
cdk-sqlite = { version = "0.12.0", optional = true }
31+
cdk-axum = { version = "0.12.0", optional = true }
32+
axum = { version = "0.8.1", optional = true }

orange-sdk/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,14 @@ impl Wallet {
527527
)),
528528
#[cfg(feature = "_test-utils")]
529529
ExtraConfig::Dummy(cfg) => Arc::new(Box::new(
530-
DummyTrustedWallet::new(cfg.uuid, &cfg.lsp, &cfg.bitcoind, cfg.rt.clone()).await,
530+
DummyTrustedWallet::new(
531+
cfg.uuid,
532+
&cfg.lsp,
533+
&cfg.bitcoind,
534+
Arc::clone(&event_queue),
535+
Arc::clone(&runtime),
536+
)
537+
.await,
531538
)),
532539
};
533540

orange-sdk/src/trusted_wallet/cashu/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,21 @@ impl TrustedWalletInterface for Cashu {
244244
},
245245
};
246246

247-
// Execute the melt
248-
let _melt_response = self.cashu_wallet.melt(&quote.id).await.map_err(|e| {
249-
TrustedError::WalletOperationFailed(format!("Failed to execute melt: {e}"))
250-
})?;
247+
// Execute the melt in separate thread, do not block on it being successful/failed
248+
let cashu_wallet = Arc::clone(&self.cashu_wallet);
249+
let logger = Arc::clone(&self.logger);
250+
let quote_id = quote.id.clone();
251+
tokio::spawn(async move {
252+
// todo react with events too
253+
match cashu_wallet.melt(&quote_id).await {
254+
Ok(_) => {
255+
log_info!(logger, "Successfully sent for quote: {quote_id}");
256+
},
257+
Err(e) => {
258+
log_error!(logger, "Failed to melt quote {quote_id}: {e}",);
259+
},
260+
}
261+
});
251262

252263
// Convert quote ID to a 32-byte array for consistency
253264
// We'll use the quote ID as the payment identifier

orange-sdk/src/trusted_wallet/dummy.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! A dummy implementation of `TrustedWalletInterface` for testing purposes.
22
3+
use crate::EventQueue;
34
use crate::bitcoin::hashes::Hash;
4-
use crate::store::TxStatus;
5+
use crate::store::{PaymentId, TxStatus};
56
use crate::trusted_wallet::{Payment, TrustedError, TrustedWalletInterface};
67
use bitcoin_payment_instructions::PaymentMethod;
78
use bitcoin_payment_instructions::amount::Amount;
@@ -44,7 +45,9 @@ pub struct DummyTrustedWalletExtraConfig {
4445

4546
impl DummyTrustedWallet {
4647
/// Creates a new `DummyTrustedWallet` instance.
47-
pub(crate) async fn new(uuid: Uuid, lsp: &Node, bitcoind: &Bitcoind, rt: Arc<Runtime>) -> Self {
48+
pub(crate) async fn new(
49+
uuid: Uuid, lsp: &Node, bitcoind: &Bitcoind, event_queue: Arc<EventQueue>, rt: Arc<Runtime>,
50+
) -> Self {
4851
let mut builder = ldk_node::Builder::new();
4952
builder.set_network(Network::Regtest);
5053
let mut seed: [u8; 64] = [0; 64];
@@ -105,7 +108,7 @@ impl DummyTrustedWallet {
105108
bal.fetch_sub(payment.amount.milli_sats(), Ordering::SeqCst);
106109
}
107110
},
108-
Event::PaymentReceived { payment_id, amount_msat, .. } => {
111+
Event::PaymentReceived { payment_id, amount_msat, payment_hash, .. } => {
109112
// convert id
110113
let id = payment_id.unwrap().0;
111114

@@ -128,6 +131,17 @@ impl DummyTrustedWallet {
128131
};
129132
payments.push(new);
130133
bal.fetch_add(amount_msat, Ordering::SeqCst);
134+
135+
// Send a PaymentReceived event
136+
event_queue
137+
.add_event(crate::Event::PaymentReceived {
138+
payment_id: PaymentId::Trusted(id),
139+
payment_hash,
140+
amount_msat,
141+
custom_records: vec![],
142+
lsp_fee_msats: None,
143+
})
144+
.unwrap();
131145
},
132146
Event::PaymentForwarded { .. } => {},
133147
Event::PaymentClaimable { .. } => {},
@@ -142,6 +156,14 @@ impl DummyTrustedWallet {
142156
}
143157
});
144158

159+
// wait for ldk to be ready
160+
for _ in 0..10 {
161+
if ldk_node.status().is_listening {
162+
break;
163+
}
164+
tokio::time::sleep(Duration::from_secs(1)).await;
165+
}
166+
145167
// have LSP open channel to node
146168
lsp.open_channel(ldk_node.node_id(), socket_addr, 1_000_000, Some(500_000_000), None)
147169
.unwrap();

orange-sdk/src/trusted_wallet/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ pub trait TrustedWalletInterface: Send + Sync + private::Sealed {
7070
&self, method: PaymentMethod, amount: Amount,
7171
) -> Pin<Box<dyn Future<Output = Result<Amount, TrustedError>> + Send + '_>>;
7272

73-
/// Pays to the given payment method with the specified amount.
73+
/// Pays to the given payment method with the specified amount. This should not await
74+
/// the result of the payment, it should only initiate it and return the payment ID.
75+
///
76+
/// This should later emit a [`PaymentSuccessful`] or [`PaymentFailed`] event
77+
/// when the payment is completed or failed.
78+
///
79+
/// [`PaymentSuccessful`]: `crate::event::Event::PaymentSuccessful`
80+
/// [`PaymentFailed`]: `crate::event::Event::PaymentFailed`
7481
fn pay(
7582
&self, method: PaymentMethod, amount: Amount,
7683
) -> Pin<Box<dyn Future<Output = Result<[u8; 32], TrustedError>> + Send + '_>>;

0 commit comments

Comments
 (0)