Skip to content

Commit 88c63e9

Browse files
authored
Merge pull request #2230 from freddiekrugerrand/2228-validatechainhash
Check chain hash for channel announcement and update
2 parents c3de782 + bd962fc commit 88c63e9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

lightning/src/routing/gossip.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,13 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15731573
return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
15741574
}
15751575

1576+
if msg.chain_hash != self.genesis_hash {
1577+
return Err(LightningError {
1578+
err: "Channel announcement chain hash does not match genesis hash".to_owned(),
1579+
action: ErrorAction::IgnoreAndLog(Level::Debug),
1580+
});
1581+
}
1582+
15761583
{
15771584
let channels = self.channels.read().unwrap();
15781585

@@ -1819,6 +1826,13 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18191826
fn update_channel_intern(&self, msg: &msgs::UnsignedChannelUpdate, full_msg: Option<&msgs::ChannelUpdate>, sig: Option<&secp256k1::ecdsa::Signature>) -> Result<(), LightningError> {
18201827
let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
18211828

1829+
if msg.chain_hash != self.genesis_hash {
1830+
return Err(LightningError {
1831+
err: "Channel update chain hash does not match genesis hash".to_owned(),
1832+
action: ErrorAction::IgnoreAndLog(Level::Debug),
1833+
});
1834+
}
1835+
18221836
#[cfg(all(feature = "std", not(test), not(feature = "_test_utils")))]
18231837
{
18241838
// Note that many tests rely on being able to set arbitrarily old timestamps, thus we
@@ -2311,6 +2325,16 @@ pub(crate) mod tests {
23112325
Ok(_) => panic!(),
23122326
Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
23132327
};
2328+
2329+
// Test that channel announcements with the wrong chain hash are ignored (network graph is testnet,
2330+
// announcement is mainnet).
2331+
let incorrect_chain_announcement = get_signed_channel_announcement(|unsigned_announcement| {
2332+
unsigned_announcement.chain_hash = genesis_block(Network::Bitcoin).header.block_hash();
2333+
}, node_1_privkey, node_2_privkey, &secp_ctx);
2334+
match gossip_sync.handle_channel_announcement(&incorrect_chain_announcement) {
2335+
Ok(_) => panic!(),
2336+
Err(e) => assert_eq!(e.err, "Channel announcement chain hash does not match genesis hash")
2337+
};
23142338
}
23152339

23162340
#[test]
@@ -2415,6 +2439,17 @@ pub(crate) mod tests {
24152439
Ok(_) => panic!(),
24162440
Err(e) => assert_eq!(e.err, "Invalid signature on channel_update message")
24172441
};
2442+
2443+
// Test that channel updates with the wrong chain hash are ignored (network graph is testnet, channel
2444+
// update is mainet).
2445+
let incorrect_chain_update = get_signed_channel_update(|unsigned_channel_update| {
2446+
unsigned_channel_update.chain_hash = genesis_block(Network::Bitcoin).header.block_hash();
2447+
}, node_1_privkey, &secp_ctx);
2448+
2449+
match gossip_sync.handle_channel_update(&incorrect_chain_update) {
2450+
Ok(_) => panic!(),
2451+
Err(e) => assert_eq!(e.err, "Channel update chain hash does not match genesis hash")
2452+
};
24182453
}
24192454

24202455
#[test]

0 commit comments

Comments
 (0)