Skip to content

Commit

Permalink
add new entrypoint for testing event serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joemphilips committed Jun 24, 2020
1 parent 18c5ec7 commit b0e4e44
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
59 changes: 56 additions & 3 deletions bindings/src/ffi_test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use crate::error::FFIResult;
use lightning::util::events::Event;
use lightning::chain::transaction::OutPoint;
use bitcoin::hash_types::Txid;
use hex;
use crate::adaptors::primitives::FFIEvents;
use crate::utils::into_fixed_buffer;
use crate::Out;
use lightning::ln::channelmanager::{PaymentHash, PaymentSecret, PaymentPreimage};
use bitcoin_hashes::core::time::Duration;
use lightning::chain::keysinterface::SpendableOutputDescriptor;
use bitcoin::TxOut;

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

fn test_event_serialization(buf_out: Out<u8>, buf_len: usize, actual_len: Out<usize>) -> FFIResult {
let mut events = Vec::with_capacity(5);

let txid = bitcoin::consensus::deserialize(&hex::decode("4141414141414141414141414141414141414141414141414141414141414141").unwrap()).unwrap();
let funding_txo= OutPoint::new(txid, 0);
let user_channel_id = 1111;
events.push(Event::FundingBroadcastSafe {funding_txo, user_channel_id} );

let payment_hash = PaymentHash([2;32]);
let payment_secret = Some(PaymentSecret([3; 32]));
let amt = 50000;
events.push(Event::PaymentReceived {payment_secret, payment_hash, amt});


let payment_preimage = PaymentPreimage([4;32]);
events.push(Event::PaymentSent {payment_preimage});

let payment_hash = PaymentHash([5;32]);
let rejected_by_dest = true;
events.push(Event::PaymentFailed {payment_hash, rejected_by_dest});

let time_forwardable = Duration::from_millis(100);
events.push(Event::PendingHTLCsForwardable {time_forwardable});

let outpoint = bitcoin::blockdata::transaction::OutPoint::new(txid, 0);
let output = TxOut {value: 255, script_pubkey: bitcoin::blockdata::script::Script::new() };
let static_output = SpendableOutputDescriptor::StaticOutput {outpoint, output: output.clone()};

let key = bitcoin::secp256k1::key::SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).unwrap();
let dynamic_output_p2wsh = SpendableOutputDescriptor::DynamicOutputP2WSH {
outpoint,
key,
witness_script: bitcoin::blockdata::script::Script::new(),
to_self_delay: 144,
output
};
let outputs = vec![static_output, dynamic_output_p2wsh];
events.push(Event::SpendableOutputs {outputs});

let mut e = FFIEvents{ events };
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));
into_fixed_buffer(&mut e, buf, &mut actual_len)
}
}
12 changes: 12 additions & 0 deletions bindings/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
macro_rules! assert_match {
($bind:pat = $bind_from:expr) => {
assert_match!($bind = $bind_from => ())
};
($bind:pat = $bind_from:expr => $with:expr) => {
match $bind_from {
$bind => $with,
_ => panic!("assertion failed: unexpected value `{:?}`", $bind_from),
}
};
}

pub mod static_assert {
use std::panic::UnwindSafe;

Expand Down

0 comments on commit b0e4e44

Please sign in to comment.