Skip to content

Commit 02a768a

Browse files
committed
more refactor on fiber-types
1 parent 030d400 commit 02a768a

File tree

7 files changed

+1070
-1050
lines changed

7 files changed

+1070
-1050
lines changed

crates/fiber-types/src/channel.rs

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//! Channel-related types: state flags, TLC status, channel state enum.
22
3+
use crate::invoice::HashAlgorithm;
4+
use crate::onion::PaymentOnionPacket;
5+
use crate::onion::TlcErrPacket;
6+
use crate::Hash256;
7+
use crate::Pubkey;
38
use bitflags::bitflags;
49
use serde::{Deserialize, Serialize};
510

@@ -408,8 +413,6 @@ pub enum ChannelOpeningStatus {
408413
// ChannelBasePublicKeys
409414
// ============================================================
410415

411-
use crate::Pubkey;
412-
413416
/// One counterparty's public keys which do not change over the life of a channel.
414417
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
415418
pub struct ChannelBasePublicKeys {
@@ -424,9 +427,6 @@ pub struct ChannelBasePublicKeys {
424427
// ============================================================
425428
// PrevTlcInfo
426429
// ============================================================
427-
428-
use crate::Hash256;
429-
430430
/// When we are forwarding a TLC, we need to know the previous TLC information.
431431
/// This struct keeps the information of the previous TLC.
432432
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
@@ -466,10 +466,6 @@ impl PrevTlcInfo {
466466
// ============================================================
467467
// TlcInfo
468468
// ============================================================
469-
470-
use crate::invoice::HashAlgorithm;
471-
use crate::payment::{PaymentOnionPacket, RemoveTlcReason};
472-
473469
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq)]
474470
pub struct TlcInfo {
475471
pub status: TlcStatus,
@@ -580,7 +576,6 @@ impl TlcInfo {
580576
}
581577

582578
pub fn is_fail_remove_confirmed(&self) -> bool {
583-
use crate::payment::RemoveTlcReason;
584579
matches!(self.removed_reason, Some(RemoveTlcReason::RemoveTlcFail(_)))
585580
&& matches!(
586581
self.status,
@@ -1341,3 +1336,107 @@ impl TryFrom<molecule_fiber::RevokeAndAck> for RevokeAndAck {
13411336
})
13421337
}
13431338
}
1339+
1340+
// ============================================================
1341+
// RemoveTlcFulfill
1342+
// ============================================================
1343+
/// The fulfillment of a TLC removal.
1344+
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
1345+
pub struct RemoveTlcFulfill {
1346+
pub payment_preimage: Hash256,
1347+
}
1348+
1349+
// ============================================================
1350+
// RemoveTlcReason
1351+
// ============================================================
1352+
1353+
use std::fmt::{Debug, Formatter};
1354+
1355+
/// The reason for removing a TLC.
1356+
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
1357+
pub enum RemoveTlcReason {
1358+
RemoveTlcFulfill(RemoveTlcFulfill),
1359+
RemoveTlcFail(TlcErrPacket),
1360+
}
1361+
1362+
impl Debug for RemoveTlcReason {
1363+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1364+
match self {
1365+
RemoveTlcReason::RemoveTlcFulfill(_fulfill) => {
1366+
write!(f, "RemoveTlcFulfill")
1367+
}
1368+
RemoveTlcReason::RemoveTlcFail(_fail) => {
1369+
write!(f, "RemoveTlcFail")
1370+
}
1371+
}
1372+
}
1373+
}
1374+
1375+
impl RemoveTlcReason {
1376+
/// Intermediate node backwards the error to the previous hop using the shared secret
1377+
/// used in forwarding the onion packet.
1378+
pub fn backward(self, shared_secret: &[u8; 32]) -> Self {
1379+
match self {
1380+
RemoveTlcReason::RemoveTlcFulfill(remove_tlc_fulfill) => {
1381+
RemoveTlcReason::RemoveTlcFulfill(remove_tlc_fulfill)
1382+
}
1383+
RemoveTlcReason::RemoveTlcFail(remove_tlc_fail) => {
1384+
RemoveTlcReason::RemoveTlcFail(remove_tlc_fail.backward(shared_secret))
1385+
}
1386+
}
1387+
}
1388+
}
1389+
1390+
impl From<RemoveTlcReason> for molecule_fiber::RemoveTlcReasonUnion {
1391+
fn from(remove_tlc_reason: RemoveTlcReason) -> Self {
1392+
match remove_tlc_reason {
1393+
RemoveTlcReason::RemoveTlcFulfill(remove_tlc_fulfill) => {
1394+
molecule_fiber::RemoveTlcReasonUnion::RemoveTlcFulfill(remove_tlc_fulfill.into())
1395+
}
1396+
RemoveTlcReason::RemoveTlcFail(remove_tlc_fail) => {
1397+
molecule_fiber::RemoveTlcReasonUnion::TlcErrPacket(remove_tlc_fail.into())
1398+
}
1399+
}
1400+
}
1401+
}
1402+
1403+
impl From<RemoveTlcReason> for molecule_fiber::RemoveTlcReason {
1404+
fn from(remove_tlc_reason: RemoveTlcReason) -> Self {
1405+
molecule_fiber::RemoveTlcReason::new_builder()
1406+
.set(remove_tlc_reason)
1407+
.build()
1408+
}
1409+
}
1410+
1411+
impl From<molecule_fiber::RemoveTlcReason> for RemoveTlcReason {
1412+
fn from(remove_tlc_reason: molecule_fiber::RemoveTlcReason) -> Self {
1413+
match remove_tlc_reason.to_enum() {
1414+
molecule_fiber::RemoveTlcReasonUnion::RemoveTlcFulfill(remove_tlc_fulfill) => {
1415+
RemoveTlcReason::RemoveTlcFulfill(remove_tlc_fulfill.into())
1416+
}
1417+
molecule_fiber::RemoveTlcReasonUnion::TlcErrPacket(remove_tlc_fail) => {
1418+
RemoveTlcReason::RemoveTlcFail(remove_tlc_fail.into())
1419+
}
1420+
}
1421+
}
1422+
}
1423+
1424+
// ============================================================
1425+
// Molecule conversions
1426+
// ============================================================
1427+
1428+
impl From<RemoveTlcFulfill> for molecule_fiber::RemoveTlcFulfill {
1429+
fn from(remove_tlc_fulfill: RemoveTlcFulfill) -> Self {
1430+
molecule_fiber::RemoveTlcFulfill::new_builder()
1431+
.payment_preimage(remove_tlc_fulfill.payment_preimage.into())
1432+
.build()
1433+
}
1434+
}
1435+
1436+
impl From<molecule_fiber::RemoveTlcFulfill> for RemoveTlcFulfill {
1437+
fn from(remove_tlc_fulfill: molecule_fiber::RemoveTlcFulfill) -> Self {
1438+
RemoveTlcFulfill {
1439+
payment_preimage: remove_tlc_fulfill.payment_preimage().into(),
1440+
}
1441+
}
1442+
}

0 commit comments

Comments
 (0)