Skip to content

Commit b0e4e44

Browse files
committed
add new entrypoint for testing event serialization
1 parent 18c5ec7 commit b0e4e44

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

bindings/src/ffi_test_utils.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
use crate::error::FFIResult;
2+
use lightning::util::events::Event;
3+
use lightning::chain::transaction::OutPoint;
4+
use bitcoin::hash_types::Txid;
5+
use hex;
6+
use crate::adaptors::primitives::FFIEvents;
7+
use crate::utils::into_fixed_buffer;
8+
use crate::Out;
9+
use lightning::ln::channelmanager::{PaymentHash, PaymentSecret, PaymentPreimage};
10+
use bitcoin_hashes::core::time::Duration;
11+
use lightning::chain::keysinterface::SpendableOutputDescriptor;
12+
use bitcoin::TxOut;
213

3-
// These tests should be used for asserting that the wrapper code can see the expected
4-
// error messages when it fails (or succeeds).
14+
// These tests should be used for asserting that the wrapper can receive expected items from rust.
515
ffi! {
616
fn ffi_test_error() -> FFIResult {
717
use std::io;
@@ -12,5 +22,48 @@ ffi! {
1222
fn ffi_test_ok() -> FFIResult {
1323
FFIResult::ok()
1424
}
15-
}
1625

26+
fn test_event_serialization(buf_out: Out<u8>, buf_len: usize, actual_len: Out<usize>) -> FFIResult {
27+
let mut events = Vec::with_capacity(5);
28+
29+
let txid = bitcoin::consensus::deserialize(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap();
30+
let funding_txo= OutPoint::new(txid, 0);
31+
let user_channel_id = 1111;
32+
events.push(Event::FundingBroadcastSafe {funding_txo, user_channel_id} );
33+
34+
let payment_hash = PaymentHash([2;32]);
35+
let payment_secret = Some(PaymentSecret([3; 32]));
36+
let amt = 50000;
37+
events.push(Event::PaymentReceived {payment_secret, payment_hash, amt});
38+
39+
40+
let payment_preimage = PaymentPreimage([4;32]);
41+
events.push(Event::PaymentSent {payment_preimage});
42+
43+
let payment_hash = PaymentHash([5;32]);
44+
let rejected_by_dest = true;
45+
events.push(Event::PaymentFailed {payment_hash, rejected_by_dest});
46+
47+
let time_forwardable = Duration::from_millis(100);
48+
events.push(Event::PendingHTLCsForwardable {time_forwardable});
49+
50+
let outpoint = bitcoin::blockdata::transaction::OutPoint::new(txid, 0);
51+
let output = TxOut {value: 255, script_pubkey: bitcoin::blockdata::script::Script::new() };
52+
let static_output = SpendableOutputDescriptor::StaticOutput {outpoint, output: output.clone()};
53+
54+
let key = bitcoin::secp256k1::key::SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).unwrap();
55+
let dynamic_output_p2wsh = SpendableOutputDescriptor::DynamicOutputP2WSH {
56+
outpoint,
57+
key,
58+
witness_script: bitcoin::blockdata::script::Script::new(),
59+
to_self_delay: 144,
60+
output
61+
};
62+
let outputs = vec![static_output, dynamic_output_p2wsh];
63+
events.push(Event::SpendableOutputs {outputs});
64+
65+
let mut e = FFIEvents{ events };
66+
let buf = unsafe_block!("The buffer lives as long as this function, the length is within the buffer and the buffer won't be read before initialization" => buf_out.as_uninit_bytes_mut(buf_len));
67+
into_fixed_buffer(&mut e, buf, &mut actual_len)
68+
}
69+
}

bindings/src/test_utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
macro_rules! assert_match {
2+
($bind:pat = $bind_from:expr) => {
3+
assert_match!($bind = $bind_from => ())
4+
};
5+
($bind:pat = $bind_from:expr => $with:expr) => {
6+
match $bind_from {
7+
$bind => $with,
8+
_ => panic!("assertion failed: unexpected value `{:?}`", $bind_from),
9+
}
10+
};
11+
}
12+
113
pub mod static_assert {
214
use std::panic::UnwindSafe;
315

0 commit comments

Comments
 (0)