Skip to content

Commit 99b65d4

Browse files
committed
Make current extension set empty, add support for doc-comments in TLV
1 parent b96b1b1 commit 99b65d4

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

gossip/src/contact_info.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,19 @@ struct SocketEntry {
118118
offset: u16, // Port offset with respect to the previous entry.
119119
}
120120

121-
// The Extension enum for optional features in gossip
122-
define_tlv_enum! (pub(crate) enum Extension {});
121+
define_tlv_enum!(
122+
/// TLV encoded Extensions in ContactInfo messages
123+
///
124+
/// On the wire each record is: [type: u8][len: varint][bytes]
125+
/// Extensions with unknown types are skipped by tlv::parse,
126+
/// so new types can be added without breaking legacy code,
127+
/// and support by all clients is not required.
128+
///
129+
/// Always add new TLV records to the end of this enum.
130+
/// Never reorder or reuse a type.
131+
/// Ensure new type collisions do not happen.
132+
pub(crate) enum Extension {}
133+
);
123134

124135
// As part of deserialization, self.addrs and self.sockets should be cross
125136
// verified and self.cache needs to be populated. This type serves as a
@@ -1186,14 +1197,4 @@ mod tests {
11861197
assert_eq!(other.overrides(&node), Some(true));
11871198
}
11881199
}
1189-
1190-
#[test]
1191-
fn test_extensions() {
1192-
let mut ci = ContactInfo::new_localhost(&Pubkey::new_unique(), 42);
1193-
ci.extensions.push(Extension::SupportSenderSignatures(42));
1194-
let bytes = bincode::serialize(&ci).unwrap();
1195-
let cil: ContactInfoLite = bincode::deserialize(&bytes).unwrap();
1196-
let ci2 = ContactInfo::try_from(cil).unwrap();
1197-
assert_eq!(ci, ci2);
1198-
}
11991200
}

gossip/src/tlv.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ pub(crate) struct TlvRecord {
1717
#[macro_export]
1818
macro_rules! define_tlv_enum {
1919
(
20+
$(#[$meta:meta])*
2021
$vis:vis enum $enum_name:ident {
2122
$($typ:literal => $variant:ident($inner:ty)),* $(,)?
2223
}
2324
) => {
25+
// add the doc-comment if present
26+
$(#[$meta])*
2427
// define the enum itself
2528
#[derive(Debug, Clone, Eq, PartialEq)]
2629
$vis enum $enum_name {
@@ -44,7 +47,7 @@ macro_rules! define_tlv_enum {
4447
impl TryFrom<&TlvRecord> for $enum_name {
4548
type Error = bincode::Error;
4649
fn try_from(value: &TlvRecord) -> Result<Self, Self::Error> {
47-
use serde::ser::Error;
50+
use serde::de::Error;
4851
match value.typ {
4952
$(
5053
$typ => Ok(Self::$variant(bincode::deserialize::<$inner>(&value.bytes)?)),
@@ -57,13 +60,16 @@ macro_rules! define_tlv_enum {
5760
impl TryFrom<&$enum_name> for TlvRecord {
5861
type Error = bincode::Error;
5962
fn try_from(value: &$enum_name) -> Result<Self, Self::Error> {
63+
use serde::ser::Error;
6064
match value {
6165
$(
6266
$enum_name::$variant(inner) => Ok(TlvRecord {
6367
typ: $typ,
6468
bytes: bincode::serialize(inner)?,
6569
}),
6670
)*
71+
#[allow(unreachable_patterns)]
72+
_ => Err(bincode::Error::custom("Unsupported enum variant")),
6773
}
6874
}
6975
}
@@ -114,7 +120,11 @@ mod tests {
114120
// Make sure legacy recover works correctly
115121
let legacy: Vec<ExtensionLegacy> = crate::tlv::parse(&tlv_vec);
116122
assert!(matches!(legacy[0], ExtensionLegacy::Test(42)));
117-
assert_eq!(legacy.len(), 1, "Legacy parser should only recover ")
123+
assert_eq!(
124+
legacy.len(),
125+
1,
126+
"Legacy parser should only recover 1 entry"
127+
)
118128
}
119129

120130
/// Test that TLV encoded data is forwards-compatible,

0 commit comments

Comments
 (0)