Skip to content

Commit 1b97457

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

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

bindings/src/ffi_test_utils.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
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+
15+
// These tests should be used for asserting that the wrapper can receive expected items from rust.
516
ffi! {
617
fn ffi_test_error() -> FFIResult {
718
use std::io;
@@ -12,5 +23,51 @@ ffi! {
1223
fn ffi_test_ok() -> FFIResult {
1324
FFIResult::ok()
1425
}
15-
}
1626

27+
fn test_event_serialization(buf_out: Out<u8>, buf_len: usize, actual_len: Out<usize>) -> FFIResult {
28+
let mut events = Vec::with_capacity(5);
29+
30+
let txid = bitcoin::consensus::deserialize(&hex::decode("4141414141414141414141414141414141414141414141414141414141414142").unwrap()).unwrap();
31+
let funding_txo= OutPoint::new(txid, 1);
32+
let user_channel_id = 1111;
33+
events.push(Event::FundingBroadcastSafe {funding_txo, user_channel_id} );
34+
35+
let payment_hash = PaymentHash([2;32]);
36+
let payment_secret = Some(PaymentSecret([3; 32]));
37+
let amt = 50000;
38+
events.push(Event::PaymentReceived {payment_secret, payment_hash, amt});
39+
40+
41+
let payment_preimage = PaymentPreimage([4;32]);
42+
events.push(Event::PaymentSent {payment_preimage});
43+
44+
let payment_hash = PaymentHash([5;32]);
45+
let rejected_by_dest = true;
46+
events.push(Event::PaymentFailed {payment_hash, rejected_by_dest});
47+
48+
let time_forwardable = Duration::from_millis(100);
49+
events.push(Event::PendingHTLCsForwardable {time_forwardable});
50+
51+
// expected txid for this tx is "1e8a6ed582813120a85e1dfed1249f1a32f530ba4b3fbabf4047cfbc1faea28c"
52+
let tx: bitcoin::blockdata::transaction::Transaction = bitcoin::consensus::deserialize(&hex::decode("02000000000101b7ab83b98315c8e44e92aef50e2f43e3e21b1ca3a6299cbe72fa78caed5b49140000000000feffffff026c5f042a0100000016001438ce449f272f685f24c9d741444bc5224a62749ea086010000000000220020debbba2af4c4f581437a66e3e8d839e883f9c2ec8c7a12d002aba7317170284002473044022006f1e5f46202752b2ac41ce524f88c95f51d97adf39f5b120ae2576329b7bb1802202ecdad16893b28deeb5886db76dc52b8ccbfc02a185c6e159b30f4c86bf922a801210333218b9a0778cd13c3bc2d8eb73962cb4f4b4528ef359f120aa961c39a8bdb66d2000000").unwrap()).unwrap();
53+
let outpoint = bitcoin::blockdata::transaction::OutPoint::new(tx.txid(), 1);
54+
let output = TxOut {value: 255, script_pubkey: bitcoin::blockdata::script::Script::new() };
55+
let static_output = SpendableOutputDescriptor::StaticOutput {outpoint, output: output.clone()};
56+
57+
let key = bitcoin::secp256k1::key::SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).unwrap();
58+
let dynamic_output_p2wsh = SpendableOutputDescriptor::DynamicOutputP2WSH {
59+
outpoint,
60+
key,
61+
witness_script: bitcoin::blockdata::script::Script::new(),
62+
to_self_delay: 144,
63+
output
64+
};
65+
let outputs = vec![static_output, dynamic_output_p2wsh];
66+
events.push(Event::SpendableOutputs {outputs});
67+
68+
let mut e = FFIEvents{ events };
69+
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));
70+
into_fixed_buffer(&mut e, buf, &mut actual_len)
71+
}
72+
73+
}

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)