Skip to content

Commit

Permalink
add unit test for tlc operations
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Dec 22, 2024
1 parent e08d786 commit 9380f2e
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 73 deletions.
20 changes: 2 additions & 18 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,7 @@ impl TlcStateV2 {
self.received_tlcs.add_tlc(tlc);
}

pub fn handle_commitment_signed(&self, local: bool) -> Hash256 {
pub fn commitment_signed(&self, local: bool) -> Vec<AddTlcInfoV2> {
let mut active_tls = vec![];
for tlc in self.offered_tlcs.tlcs.iter() {
if let TlcKindV2::AddTlc(add_info) = tlc {
Expand Down Expand Up @@ -2615,23 +2615,7 @@ impl TlcStateV2 {
}
}
}

// serialize active_tls to ge a hash
let keyparts = active_tls
.iter()
.map(|tlc| (tlc.amount, tlc.payment_hash))
.collect::<Vec<_>>();

eprintln!("keyparts: {:?}", keyparts);
let serialized = serde_json::to_string(&keyparts).expect("Failed to serialize active_tls");

// Hash the serialized data using SHA-256
let mut hasher = new_blake2b();
hasher.update(serialized.to_string().as_bytes());
let mut result = [0u8; 32];
hasher.finalize(&mut result);

result.into()
return active_tls;
}
}

Expand Down
56 changes: 1 addition & 55 deletions src/fiber/tests/channel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::fiber::channel::{
AddTlcInfo, CommitmentNumbers, InboundTlctatus, RemoveTlcInfo, TLCId, TlcKind, TlcState,
TlcStateV2, TlcStatus, UpdateCommand,
AddTlcInfo, CommitmentNumbers, RemoveTlcInfo, TLCId, TlcKind, TlcState, UpdateCommand,
};
use crate::fiber::channel::{AddTlcInfoV2, TlcKindV2};
use crate::fiber::config::MAX_PAYMENT_TLC_EXPIRY_LIMIT;
use crate::fiber::graph::PaymentSessionStatus;
use crate::fiber::network::{DebugEvent, SendPaymentCommand};
Expand Down Expand Up @@ -149,58 +147,6 @@ fn test_pending_tlcs() {
assert_eq!(tlcs2.len(), 0);
}

#[test]
fn test_tlc_state_v2() {
let mut tlc_state = TlcStateV2::default();
let mut add_tlc1 = AddTlcInfoV2 {
amount: 10000,
status: TlcStatus::Outbound(crate::fiber::channel::OutboundTlcStatus::LocalAnnounced),
channel_id: gen_rand_sha256_hash(),
payment_hash: gen_rand_sha256_hash(),
expiry: now_timestamp_as_millis_u64() + 1000,
hash_algorithm: HashAlgorithm::Sha256,
onion_packet: None,
shared_secret: NO_SHARED_SECRET.clone(),
tlc_id: TLCId::Offered(0),
created_at: CommitmentNumbers::default(),
removed_at: None,
payment_preimage: None,
previous_tlc: None,
};
let mut add_tlc2 = AddTlcInfoV2 {
amount: 20000,
status: TlcStatus::Outbound(crate::fiber::channel::OutboundTlcStatus::LocalAnnounced),
channel_id: gen_rand_sha256_hash(),
payment_hash: gen_rand_sha256_hash(),
expiry: now_timestamp_as_millis_u64() + 2000,
hash_algorithm: HashAlgorithm::Sha256,
onion_packet: None,
shared_secret: NO_SHARED_SECRET.clone(),
tlc_id: TLCId::Offered(1),
created_at: CommitmentNumbers::default(),
removed_at: None,
payment_preimage: None,
previous_tlc: None,
};
tlc_state.add_offered_tlc(TlcKindV2::AddTlc(add_tlc1.clone()));
tlc_state.add_offered_tlc(TlcKindV2::AddTlc(add_tlc2.clone()));

let mut tlc_state_2 = TlcStateV2::default();
add_tlc1.flip_mut();
add_tlc2.flip_mut();
add_tlc1.status = TlcStatus::Inbound(InboundTlctatus::RemoteAnnounced);
add_tlc2.status = TlcStatus::Inbound(InboundTlctatus::RemoteAnnounced);
tlc_state_2.add_received_tlc(TlcKindV2::AddTlc(add_tlc1));
tlc_state_2.add_received_tlc(TlcKindV2::AddTlc(add_tlc2));

let hash1 = tlc_state.handle_commitment_signed(true);
eprintln!("hash1: {:?}", hash1);

let hash2 = tlc_state_2.handle_commitment_signed(false);
eprintln!("hash2: {:?}", hash2);
assert_eq!(hash1, hash2);
}

#[test]
fn test_pending_tlcs_duplicated_tlcs() {
let mut tlc_state = TlcState::default();
Expand Down
1 change: 1 addition & 0 deletions src/fiber/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ mod path;
mod payment;
mod serde_utils;
pub mod test_utils;
mod tlc_op;
mod types;
Loading

0 comments on commit 9380f2e

Please sign in to comment.