Skip to content

Commit 32060fd

Browse files
Fix upgradable_required fields to actually be required in lower level macros
When using lower level macros such as read_tlv_stream, upgradable_required fields have been treated as regular options. This is incorrect, they should either be upgradable_options or treated as required fields.
1 parent 1d835d7 commit 32060fd

File tree

5 files changed

+17
-30
lines changed

5 files changed

+17
-30
lines changed

lightning/src/chain/channelmonitor.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,7 @@ impl MaybeReadable for OnchainEventEntry {
462462
(3, block_hash, option),
463463
(4, event, upgradable_required),
464464
});
465-
if let Some(ev) = event {
466-
Ok(Some(Self { txid, transaction, height, block_hash, event: ev }))
467-
} else {
468-
Ok(None)
469-
}
465+
Ok(Some(Self { txid, transaction, height, block_hash, event: _init_tlv_based_struct_field!(event, upgradable_required) }))
470466
}
471467
}
472468

lightning/src/chain/onchaintx.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ impl MaybeReadable for OnchainEventEntry {
113113
(2, height, required),
114114
(4, event, upgradable_required),
115115
});
116-
if let Some(ev) = event {
117-
Ok(Some(Self { txid, height, block_hash, event: ev }))
118-
} else {
119-
Ok(None)
120-
}
116+
Ok(Some(Self { txid, height, block_hash, event: _init_tlv_based_struct_field!(event, upgradable_required) }))
121117
}
122118
}
123119

lightning/src/routing/gossip.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -883,9 +883,9 @@ impl Readable for ChannelInfo {
883883
(0, features, required),
884884
(1, announcement_received_time, (default_value, 0)),
885885
(2, node_one, required),
886-
(4, one_to_two_wrap, upgradable_required),
886+
(4, one_to_two_wrap, upgradable_option),
887887
(6, node_two, required),
888-
(8, two_to_one_wrap, upgradable_required),
888+
(8, two_to_one_wrap, upgradable_option),
889889
(10, capacity_sats, required),
890890
(12, announcement_message, required),
891891
});
@@ -1161,7 +1161,7 @@ impl Readable for NodeInfo {
11611161

11621162
read_tlv_fields!(reader, {
11631163
(0, _lowest_inbound_channel_fees, option),
1164-
(2, announcement_info_wrap, upgradable_required),
1164+
(2, announcement_info_wrap, upgradable_option),
11651165
(4, channels, vec_type),
11661166
});
11671167

lightning/src/util/events.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ impl MaybeReadable for Event {
11991199
let mut payment_id = None;
12001200
read_tlv_fields!(reader, {
12011201
(0, payment_hash, required),
1202-
(1, network_update, upgradable_required),
1202+
(1, network_update, upgradable_option),
12031203
(2, payment_failed_permanently, required),
12041204
(5, path, vec_type),
12051205
(7, short_channel_id, option),
@@ -1285,15 +1285,14 @@ impl MaybeReadable for Event {
12851285
(2, reason, upgradable_required),
12861286
(3, user_channel_id_high_opt, option),
12871287
});
1288-
if reason.is_none() { return Ok(None); }
12891288

12901289
// `user_channel_id` used to be a single u64 value. In order to remain
12911290
// backwards compatible with versions prior to 0.0.113, the u128 is serialized
12921291
// as two separate u64 values.
12931292
let user_channel_id = (user_channel_id_low_opt.unwrap_or(0) as u128) +
12941293
((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);
12951294

1296-
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: reason.unwrap() }))
1295+
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required) }))
12971296
};
12981297
f()
12991298
},
@@ -1358,11 +1357,10 @@ impl MaybeReadable for Event {
13581357
(2, purpose, upgradable_required),
13591358
(4, amount_msat, required),
13601359
});
1361-
if purpose.is_none() { return Ok(None); }
13621360
Ok(Some(Event::PaymentClaimed {
13631361
receiver_node_id,
13641362
payment_hash,
1365-
purpose: purpose.unwrap(),
1363+
purpose: _init_tlv_based_struct_field!(purpose, upgradable_required),
13661364
amount_msat,
13671365
}))
13681366
};
@@ -1415,17 +1413,10 @@ impl MaybeReadable for Event {
14151413
(0, prev_channel_id, required),
14161414
(2, failed_next_destination_opt, upgradable_required),
14171415
});
1418-
if let Some(failed_next_destination) = failed_next_destination_opt {
1419-
Ok(Some(Event::HTLCHandlingFailed {
1420-
prev_channel_id,
1421-
failed_next_destination,
1422-
}))
1423-
} else {
1424-
// If we fail to read a `failed_next_destination` assume it's because
1425-
// `MaybeReadable::read` returned `Ok(None)`, though it's also possible we
1426-
// were simply missing the field.
1427-
Ok(None)
1428-
}
1416+
Ok(Some(Event::HTLCHandlingFailed {
1417+
prev_channel_id,
1418+
failed_next_destination: _init_tlv_based_struct_field!(failed_next_destination_opt, upgradable_required),
1419+
}))
14291420
};
14301421
f()
14311422
},

lightning/src/util/ser_macros.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ macro_rules! _check_decoded_tlv_order {
217217
// no-op
218218
}};
219219
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_required) => {{
220-
// no-op
220+
#[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always false
221+
let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
222+
if invalid_order {
223+
return Ok(None);
224+
}
221225
}};
222226
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_option) => {{
223227
// no-op

0 commit comments

Comments
 (0)