Skip to content

Commit 0afb048

Browse files
committed
Move ChannelManager-inner structs to TLV storage
Note that enums are left alone as we can use the type byte already present for future compatibility.
1 parent 2f872c8 commit 0afb048

File tree

1 file changed

+60
-43
lines changed

1 file changed

+60
-43
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4316,9 +4316,9 @@ impl PersistenceNotifier {
43164316
const SERIALIZATION_VERSION: u8 = 1;
43174317
const MIN_SERIALIZATION_VERSION: u8 = 1;
43184318

4319-
impl Writeable for PendingHTLCInfo {
4319+
impl Writeable for PendingHTLCRouting {
43204320
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
4321-
match &self.routing {
4321+
match &self {
43224322
&PendingHTLCRouting::Forward { ref onion_packet, ref short_channel_id } => {
43234323
0u8.write(writer)?;
43244324
onion_packet.write(writer)?;
@@ -4331,39 +4331,37 @@ impl Writeable for PendingHTLCInfo {
43314331
incoming_cltv_expiry.write(writer)?;
43324332
},
43334333
}
4334-
self.incoming_shared_secret.write(writer)?;
4335-
self.payment_hash.write(writer)?;
4336-
self.amt_to_forward.write(writer)?;
4337-
self.outgoing_cltv_value.write(writer)?;
43384334
Ok(())
43394335
}
43404336
}
43414337

4342-
impl Readable for PendingHTLCInfo {
4343-
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<PendingHTLCInfo, DecodeError> {
4344-
Ok(PendingHTLCInfo {
4345-
routing: match Readable::read(reader)? {
4346-
0u8 => PendingHTLCRouting::Forward {
4347-
onion_packet: Readable::read(reader)?,
4348-
short_channel_id: Readable::read(reader)?,
4349-
},
4350-
1u8 => PendingHTLCRouting::Receive {
4351-
payment_data: msgs::FinalOnionHopData {
4352-
payment_secret: Readable::read(reader)?,
4353-
total_msat: Readable::read(reader)?,
4354-
},
4355-
incoming_cltv_expiry: Readable::read(reader)?,
4338+
impl Readable for PendingHTLCRouting {
4339+
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<PendingHTLCRouting, DecodeError> {
4340+
match Readable::read(reader)? {
4341+
0u8 => Ok(PendingHTLCRouting::Forward {
4342+
onion_packet: Readable::read(reader)?,
4343+
short_channel_id: Readable::read(reader)?,
4344+
}),
4345+
1u8 => Ok(PendingHTLCRouting::Receive {
4346+
payment_data: msgs::FinalOnionHopData {
4347+
payment_secret: Readable::read(reader)?,
4348+
total_msat: Readable::read(reader)?,
43564349
},
4357-
_ => return Err(DecodeError::InvalidValue),
4358-
},
4359-
incoming_shared_secret: Readable::read(reader)?,
4360-
payment_hash: Readable::read(reader)?,
4361-
amt_to_forward: Readable::read(reader)?,
4362-
outgoing_cltv_value: Readable::read(reader)?,
4363-
})
4350+
incoming_cltv_expiry: Readable::read(reader)?,
4351+
}),
4352+
_ => Err(DecodeError::InvalidValue),
4353+
}
43644354
}
43654355
}
43664356

4357+
impl_writeable_tlv_based!(PendingHTLCInfo, {
4358+
(0, routing, PendingHTLCRouting::Receive { payment_data: msgs::FinalOnionHopData { payment_secret: PaymentSecret([0; 32]), total_msat: 0 }, incoming_cltv_expiry: 0 }),
4359+
(2, incoming_shared_secret, [0; 32]),
4360+
(4, payment_hash, PaymentHash([0; 32])),
4361+
(6, amt_to_forward, 0),
4362+
(8, outgoing_cltv_value, 0)
4363+
}, {}, {});
4364+
43674365
impl Writeable for HTLCFailureMsg {
43684366
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
43694367
match self {
@@ -4416,33 +4414,52 @@ impl Readable for PendingHTLCStatus {
44164414
}
44174415
}
44184416

4419-
impl_writeable!(HTLCPreviousHopData, 0, {
4420-
short_channel_id,
4421-
outpoint,
4422-
htlc_id,
4423-
incoming_packet_shared_secret
4424-
});
4417+
impl_writeable_tlv_based!(HTLCPreviousHopData, {
4418+
(0, short_channel_id, 0),
4419+
(2, outpoint, OutPoint::null()),
4420+
(4, htlc_id, 0),
4421+
(6, incoming_packet_shared_secret, [0; 32])
4422+
}, {}, {});
44254423

44264424
impl Writeable for ClaimableHTLC {
44274425
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
4428-
self.prev_hop.write(writer)?;
4429-
self.value.write(writer)?;
4430-
self.payment_data.payment_secret.write(writer)?;
4431-
self.payment_data.total_msat.write(writer)?;
4432-
self.cltv_expiry.write(writer)
4426+
write_tlv_fields!(writer, {
4427+
(0, self.prev_hop),
4428+
(2, self.value),
4429+
(4, self.payment_data.payment_secret),
4430+
(6, self.payment_data.total_msat),
4431+
(8, self.cltv_expiry)
4432+
}, {});
4433+
Ok(())
44334434
}
44344435
}
44354436

44364437
impl Readable for ClaimableHTLC {
44374438
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
4439+
let mut prev_hop = HTLCPreviousHopData {
4440+
short_channel_id: 0, htlc_id: 0,
4441+
incoming_packet_shared_secret: [0; 32],
4442+
outpoint: OutPoint::null(),
4443+
};
4444+
let mut value = 0;
4445+
let mut payment_secret = PaymentSecret([0; 32]);
4446+
let mut total_msat = 0;
4447+
let mut cltv_expiry = 0;
4448+
read_tlv_fields!(reader, {
4449+
(0, prev_hop),
4450+
(2, value),
4451+
(4, payment_secret),
4452+
(6, total_msat),
4453+
(8, cltv_expiry)
4454+
}, {});
44384455
Ok(ClaimableHTLC {
4439-
prev_hop: Readable::read(reader)?,
4440-
value: Readable::read(reader)?,
4456+
prev_hop,
4457+
value,
44414458
payment_data: msgs::FinalOnionHopData {
4442-
payment_secret: Readable::read(reader)?,
4443-
total_msat: Readable::read(reader)?,
4459+
payment_secret,
4460+
total_msat,
44444461
},
4445-
cltv_expiry: Readable::read(reader)?,
4462+
cltv_expiry,
44464463
})
44474464
}
44484465
}

0 commit comments

Comments
 (0)