Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion smite/src/bolt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod accept_channel;
mod accept_channel2;
mod attribution_data;
mod channel_ready;
mod channel_update;
mod commitment;
mod error;
mod funding;
Expand Down Expand Up @@ -38,6 +39,7 @@ pub use accept_channel::{AcceptChannel, AcceptChannelTlvs};
pub use accept_channel2::{AcceptChannel2, AcceptChannel2Tlvs};
pub use attribution_data::{AttributionData, TruncatedHmac};
pub use channel_ready::{ChannelReady, ChannelReadyTlvs};
pub use channel_update::ChannelUpdate;
pub use commitment::{
ChannelConfig, ChannelPartyConfig, CommitmentError, CommitmentPartyState, CommitmentState,
HolderIdentity, Side,
Expand All @@ -63,7 +65,7 @@ pub use tx_remove_input::TxRemoveInput;
pub use tx_remove_output::TxRemoveOutput;
pub use types::{
BigSize, CHANNEL_ID_SIZE, COMPACT_SIGNATURE_SIZE, ChannelId, MAX_MESSAGE_SIZE, PUBLIC_KEY_SIZE,
SHA256_HASH_SIZE, TXID_SIZE,
SHA256_HASH_SIZE, SHORT_CHANNEL_ID_SIZE, ShortChannelId, TXID_SIZE,
};
pub use update_fail_htlc::{UpdateFailHtlc, UpdateFailHtlcTlvs};
pub use update_fail_malformed_htlc::UpdateFailMalformedHtlc;
Expand Down Expand Up @@ -156,6 +158,8 @@ pub mod msg_type {
pub const UPDATE_FAIL_HTLC: u16 = 131;
/// `update_fail_malformed_htlc` message (BOLT 2).
pub const UPDATE_FAIL_MALFORMED_HTLC: u16 = 135;
/// `channel_update` message (BOLT 7).
pub const CHANNEL_UPDATE: u16 = 258;
/// Gossip timestamp filter message (BOLT 7).
pub const GOSSIP_TIMESTAMP_FILTER: u16 = 265;
}
Expand Down Expand Up @@ -210,6 +214,8 @@ pub enum Message {
UpdateFailHtlc(UpdateFailHtlc),
/// `update_fail_malformed_htlc` message (type 135).
UpdateFailMalformedHtlc(UpdateFailMalformedHtlc),
/// `channel_update` message (type 258).
ChannelUpdate(ChannelUpdate),
/// Gossip timestamp filter message (type 265).
GossipTimestampFilter(GossipTimestampFilter),
/// Unknown message type.
Expand Down Expand Up @@ -252,6 +258,7 @@ impl Message {
Self::UpdateFulfillHtlc(_) => msg_type::UPDATE_FULFILL_HTLC,
Self::UpdateFailHtlc(_) => msg_type::UPDATE_FAIL_HTLC,
Self::UpdateFailMalformedHtlc(_) => msg_type::UPDATE_FAIL_MALFORMED_HTLC,
Self::ChannelUpdate(_) => msg_type::CHANNEL_UPDATE,
Self::GossipTimestampFilter(_) => msg_type::GOSSIP_TIMESTAMP_FILTER,
Self::Unknown { msg_type, .. } => *msg_type,
}
Expand Down Expand Up @@ -286,6 +293,7 @@ impl Message {
Self::UpdateFulfillHtlc(m) => out.extend(m.encode()),
Self::UpdateFailHtlc(m) => out.extend(m.encode()),
Self::UpdateFailMalformedHtlc(m) => out.extend(m.encode()),
Self::ChannelUpdate(m) => out.extend(m.encode()),
Self::GossipTimestampFilter(m) => out.extend(m.encode()),
Self::Unknown { payload, .. } => out.extend(payload),
}
Expand Down Expand Up @@ -331,6 +339,7 @@ impl Message {
msg_type::UPDATE_FAIL_MALFORMED_HTLC => Ok(Self::UpdateFailMalformedHtlc(
UpdateFailMalformedHtlc::decode(cursor)?,
)),
msg_type::CHANNEL_UPDATE => Ok(Self::ChannelUpdate(ChannelUpdate::decode(cursor)?)),
msg_type::GOSSIP_TIMESTAMP_FILTER => Ok(Self::GossipTimestampFilter(
GossipTimestampFilter::decode(cursor)?,
)),
Expand Down Expand Up @@ -777,6 +786,40 @@ mod tests {
assert_eq!(decoded, Message::UpdateFailMalformedHtlc(msg));
}

fn sample_channel_update() -> ChannelUpdate {
let sk = SecretKey::from_slice(&[0x22; 32]).expect("valid secret");

let mut cu = ChannelUpdate {
// Placeholder; overwritten by `sign` below.
signature: bitcoin::secp256k1::ecdsa::Signature::from_compact(
&[0; COMPACT_SIGNATURE_SIZE],
)
.expect("zero signature parses"),
chain_hash: [0x6f; CHAIN_HASH_SIZE],
short_channel_id: ShortChannelId::from_u64(0x0000_0824_0000_0035),
timestamp: 1_715_000_000,
message_flags: 1, // must_be_one
channel_flags: 0,
cltv_expiry_delta: 144,
htlc_minimum_msat: 1_000,
fee_base_msat: 1_000,
fee_proportional_millionths: 100,
htlc_maximum_msat: 99_000_000,
extra: Vec::new(),
};
cu.sign(&sk);
cu
}

#[test]
fn message_channel_update_roundtrip() {
let cu = sample_channel_update();
let msg = Message::ChannelUpdate(cu.clone());
let encoded = msg.encode();
let decoded = Message::decode(&encoded).unwrap();
assert_eq!(decoded, Message::ChannelUpdate(cu));
}

#[test]
fn message_gossip_timestamp_filter_roundtrip() {
let chain_hash = [0x6f; 32];
Expand Down Expand Up @@ -923,6 +966,10 @@ mod tests {
.msg_type(),
msg_type::UPDATE_FAIL_MALFORMED_HTLC
);
assert_eq!(
Message::ChannelUpdate(sample_channel_update()).msg_type(),
msg_type::CHANNEL_UPDATE
);
assert_eq!(
Message::GossipTimestampFilter(GossipTimestampFilter::no_gossip([0u8; 32])).msg_type(),
msg_type::GOSSIP_TIMESTAMP_FILTER
Expand Down
Loading