Skip to content

Commit

Permalink
tests: refactoring tests
Browse files Browse the repository at this point in the history
Link: #103
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Mar 6, 2024
1 parent 53985dc commit 9e36ca8
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 190 deletions.
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";

naersk.url = "github:nix-community/naersk";
};

Expand All @@ -17,13 +16,11 @@
version = "master-62dd";
src = pkgs.fetchgit {
url = "https://github.com/ElementsProject/lightning";
rev = "62ddf84b4f15a460ed5a8f72b313998a83eefe19";
sha256 = "sha256-Xl7mrV9dSATfYIFvhK9qPyRQM2gZGPV27I/5ic5avpM=";
rev = "d1101f416f1ec959981a8ebe5639a6b267885bfd";
sha256 = "sha256-OFACPl6cSH+JwlcWDHUw+A2g2gp5RwTc1JZAhYs+2WI=";
fetchSubmodules = true;
};
configureFlags = [ "--disable-rust" "--disable-valgrind" ];
} // pkgs.lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
NIX_CFLAGS_COMPILE = "-Wno-stringop-truncation -w";
});
# Our integration tests required the cln and bitcoind
# so in this variable we declare everthin that we need
Expand Down
4 changes: 3 additions & 1 deletion lampo-bitcoind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Backend for BitcoinCore {
// FIXME: check the result.
let result: bitcoincore_rpc::Result<json::Value> = self.inner.call(
"sendrawtransaction",
&[lampo_common::bitcoin::consensus::serialize(&tx).into()],
&[lampo_common::bitcoin::consensus::encode::serialize_hex(&tx).into()],
);
log::info!(target: "bitcoind", "broadcast transaction return {:?}", result);
if result.is_ok() {
Expand All @@ -168,6 +168,8 @@ impl Backend for BitcoinCore {
return;
};
handler.emit(Event::OnChain(OnChainEvent::SendRawTransaction(tx.clone())));
} else {
log::error!(target: "bitcoind", "broadcast transaction return {:?}", result);
}
}

Expand Down
1 change: 1 addition & 0 deletions lampo-common/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod request {
pub use crate::model::invoice::request::*;
pub use crate::model::keysend::request::*;
pub use crate::model::new_addr::request::*;
#[allow(unused_imports)]
pub use crate::model::on_chain::request::*;
pub use crate::model::open_channel::request::*;
}
Expand Down
7 changes: 3 additions & 4 deletions lampo-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ use lampod::LampoDeamon;
macro_rules! wait {
($callback:expr, $timeout:expr) => {{
let mut success = false;
for wait in 0..$timeout {
for _ in 0..4 {
let result = $callback();
if let Err(_) = result {
std::thread::sleep(std::time::Duration::from_millis(wait));
std::thread::sleep(std::time::Duration::from_secs($timeout));
continue;
}
log::info!("callback completed in {wait} milliseconds");
success = true;
break;
}
assert!(success, "callback got a timeout");
}};
($callback:expr) => {
$crate::wait!($callback, 100);
$crate::wait!($callback, 5);
};
}

Expand Down
31 changes: 15 additions & 16 deletions lampod/src/jsonrpc/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn json_decode_invoice(ctx: &LampoDeamon, request: &json::Value) -> Result<j
pub fn json_pay(ctx: &LampoDeamon, request: &json::Value) -> Result<json::Value, Error> {
log::info!("call for `pay` with request `{:?}`", request);
let request: DecodeInvoice = json::from_value(request.clone())?;
let events = ctx.handler().events();
ctx.offchain_manager()
.pay_invoice(&request.invoice_str, None)
.map_err(|err| {
Expand All @@ -77,25 +78,25 @@ pub fn json_pay(ctx: &LampoDeamon, request: &json::Value) -> Result<json::Value,
data: None,
})
})?;
let events = ctx.handler().events();

// FIXME: this will loop when the Payment event is not generated
while let Event::Lightning(event) = events
.recv_timeout(Duration::from_secs(30))
// FIXME: this should be avoided, the `?` should be used here
.map_err(|err| {
Error::Rpc(RpcError {
code: -1,
message: format!("{err}"),
data: None,
})
})?
{
if let LightningEvent::PaymentEvent {
loop {
let event = events
.recv_timeout(Duration::from_secs(30))
// FIXME: this should be avoided, the `?` should be used here
.map_err(|err| {
Error::Rpc(RpcError {
code: -1,
message: format!("{err}"),
data: None,
})
})?;

if let Event::Lightning(LightningEvent::PaymentEvent {
payment_hash,
path,
state,
} = event
}) = event
{
return Ok(json::json!({
"state": state,
Expand All @@ -104,8 +105,6 @@ pub fn json_pay(ctx: &LampoDeamon, request: &json::Value) -> Result<json::Value,
}));
}
}
// FIXME the code should be removed
unreachable!()
}

pub fn json_keysend(ctx: &LampoDeamon, request: &json::Value) -> Result<json::Value, Error> {
Expand Down
19 changes: 15 additions & 4 deletions lampod/src/ln/offchain_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,29 @@ impl OffchainManager {

pub fn pay_invoice(&self, invoice_str: &str, amount_msat: Option<u64>) -> error::Result<()> {
let invoice = self.decode_invoice(invoice_str)?;
if invoice.amount_milli_satoshis().is_none() {
let payment_id = PaymentId((*invoice.payment_hash()).to_byte_array());
let (payment_hash, onion, route) = if invoice.amount_milli_satoshis().is_none() {
ldk::invoice::payment::payment_parameters_from_zero_amount_invoice(
&invoice,
amount_msat.ok_or(error::anyhow!(
"invoice with no amount, and amount must be specified"
))?,
)
.map_err(|err| error::anyhow!("{:?}", err))?;
.map_err(|err| error::anyhow!("{:?}", err))?
} else {
ldk::invoice::payment::payment_parameters_from_invoice(&invoice)
.map_err(|err| error::anyhow!("{:?}", err))?;
}
.map_err(|err| error::anyhow!("{:?}", err))?
};
self.channel_manager
.manager()
.send_payment(
payment_hash,
onion,
payment_id,
route,
Retry::Timeout(Duration::from_secs(10)),
)
.map_err(|err| error::anyhow!("{:?}", err))?;
Ok(())
}

Expand Down
Loading

0 comments on commit 9e36ca8

Please sign in to comment.