Skip to content

Commit e4fe683

Browse files
committed
f respond to comments
1 parent da7845f commit e4fe683

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
6060
) -> Result<u32, GraphSyncError> {
6161
log_trace!(self.logger, "Processing RGS data...");
6262
let mut protocol_prefix = [0u8; 3];
63-
let mut version_prefix = [0u8; 1];
6463

6564
read_cursor.read_exact(&mut protocol_prefix)?;
6665
if protocol_prefix != GOSSIP_PREFIX {
6766
return Err(DecodeError::UnknownVersion.into());
6867
}
6968

70-
read_cursor.read_exact(&mut version_prefix)?;
71-
let version = version_prefix[0];
69+
let version: u8 = Readable::read(&mut read_cursor)?;
7270
if version != 1 && version != 2 {
7371
return Err(DecodeError::UnknownVersion.into());
7472
}
@@ -120,13 +118,28 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
120118
for _ in 0..node_id_count {
121119
let mut pubkey_bytes = [0u8; 33];
122120
read_cursor.read_exact(&mut pubkey_bytes)?;
123-
let node_detail_flag = pubkey_bytes[0];
121+
122+
/*
123+
We encode additional information in the pubkey parity byte with the following mapping:
124+
125+
7-6: expect extra data after the pubkey (01 => u8, 10 => u16, 11 => u32)
126+
5-3: index of new features among default (1-6). If index is 7 (all 3 bits are set, it's
127+
outside the present default range). 0 means no feature changes.
128+
2: addresses have changed
129+
130+
1: used for all keys
131+
0: used for odd keys
132+
*/
133+
let node_detail_flag = pubkey_bytes.first().ok_or(DecodeError::ShortRead)?;
134+
124135
let has_address_details = (node_detail_flag & (1 << 2)) > 0;
125136
let feature_detail_marker = (node_detail_flag & (0b111 << 3)) >> 3;
126-
// println!("feature detail marker: {}", feature_detail_marker);
127137
let additional_data_marker = (node_detail_flag & (0b11 << 6)) >> 6;
138+
139+
// extract the relevant bits for pubkey parity
128140
let key_parity = node_detail_flag & 0b_0000_0011;
129141
pubkey_bytes[0] = key_parity;
142+
130143
let current_pubkey = PublicKey::from_slice(&pubkey_bytes)?;
131144
let current_node_id = NodeId::from_pubkey(&current_pubkey);
132145
node_ids.push(current_pubkey);
@@ -155,7 +168,7 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
155168
if has_address_details {
156169
let address_count: u8 = Readable::read(read_cursor)?;
157170
let mut node_addresses: Vec<SocketAddress> = Vec::new();
158-
for _ in 0..address_count {
171+
for address_index in 0..address_count {
159172
let current_byte_count: u8 = Readable::read(read_cursor)?;
160173
let mut address_reader = FixedLengthReader::new(&mut read_cursor, current_byte_count as u64);
161174
let mut address_bytes = Vec::new();
@@ -164,6 +177,13 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
164177
let mut address_cursor = io::Cursor::new(&address_bytes);
165178
if let Ok(current_address) = Readable::read(&mut address_cursor) {
166179
node_addresses.push(current_address);
180+
} else {
181+
// Do not crash to allow future socket address forwards compatibility
182+
log_gossip!(
183+
self.logger,
184+
"Failure to parse address at index {} for node ID {}: {:?}",
185+
address_index, current_node_id, address_bytes
186+
);
167187
}
168188
}
169189
synthetic_node_announcement.addresses = node_addresses;

0 commit comments

Comments
 (0)