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
32 changes: 32 additions & 0 deletions smite/src/bolt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod types;
mod update_add_htlc;
mod update_fail_htlc;
mod update_fail_malformed_htlc;
mod update_fee;
mod update_fulfill_htlc;
mod warning;
mod wire;
Expand Down Expand Up @@ -96,6 +97,7 @@ pub use types::{
pub use update_add_htlc::{UpdateAddHtlc, UpdateAddHtlcTlvs};
pub use update_fail_htlc::{UpdateFailHtlc, UpdateFailHtlcTlvs};
pub use update_fail_malformed_htlc::UpdateFailMalformedHtlc;
pub use update_fee::UpdateFee;
pub use update_fulfill_htlc::{UpdateFulfillHtlc, UpdateFulfillHtlcTlvs};
pub use warning::Warning;
pub use wire::{EmptyTlv, WireFormat};
Expand Down Expand Up @@ -218,6 +220,8 @@ impl MessageType {
pub const COMMITMENT_SIGNED: MessageType = MessageType(132);
/// `revoke_and_ack` message (BOLT 2).
pub const REVOKE_AND_ACK: MessageType = MessageType(133);
/// `update_fee` message (BOLT 2).
pub const UPDATE_FEE: MessageType = MessageType(134);
/// `update_fail_malformed_htlc` message (BOLT 2).
pub const UPDATE_FAIL_MALFORMED_HTLC: MessageType = MessageType(135);
/// `channel_reestablish` message (BOLT 2).
Expand Down Expand Up @@ -284,6 +288,7 @@ impl MessageType {
Self::UPDATE_FAIL_HTLC => "update_fail_htlc",
Self::COMMITMENT_SIGNED => "commitment_signed",
Self::REVOKE_AND_ACK => "revoke_and_ack",
Self::UPDATE_FEE => "update_fee",
Self::UPDATE_FAIL_MALFORMED_HTLC => "update_fail_malformed_htlc",
Self::CHANNEL_REESTABLISH => "channel_reestablish",
Self::CHANNEL_ANNOUNCEMENT => "channel_announcement",
Expand Down Expand Up @@ -368,6 +373,8 @@ pub enum Message {
CommitmentSigned(CommitmentSigned),
/// `revoke_and_ack` message (type 133).
RevokeAndAck(RevokeAndAck),
/// `update_fee` message (type 134).
UpdateFee(UpdateFee),
/// `update_fail_malformed_htlc` message (type 135).
UpdateFailMalformedHtlc(UpdateFailMalformedHtlc),
/// `channel_reestablish` message (type 136).
Expand Down Expand Up @@ -439,6 +446,7 @@ impl Message {
Self::UpdateFailHtlc(_) => MessageType::UPDATE_FAIL_HTLC,
Self::CommitmentSigned(_) => MessageType::COMMITMENT_SIGNED,
Self::RevokeAndAck(_) => MessageType::REVOKE_AND_ACK,
Self::UpdateFee(_) => MessageType::UPDATE_FEE,
Self::UpdateFailMalformedHtlc(_) => MessageType::UPDATE_FAIL_MALFORMED_HTLC,
Self::ChannelReestablish(_) => MessageType::CHANNEL_REESTABLISH,
Self::ChannelAnnouncement(_) => MessageType::CHANNEL_ANNOUNCEMENT,
Expand Down Expand Up @@ -488,6 +496,7 @@ impl Message {
Self::UpdateFailHtlc(m) => out.extend(m.encode()),
Self::CommitmentSigned(m) => out.extend(m.encode()),
Self::RevokeAndAck(m) => out.extend(m.encode()),
Self::UpdateFee(m) => out.extend(m.encode()),
Self::UpdateFailMalformedHtlc(m) => out.extend(m.encode()),
Self::ChannelReestablish(m) => out.extend(m.encode()),
Self::ChannelAnnouncement(m) => out.extend(m.encode()),
Expand Down Expand Up @@ -558,6 +567,7 @@ impl Message {
Ok(Self::CommitmentSigned(CommitmentSigned::decode(cursor)?))
}
MessageType::REVOKE_AND_ACK => Ok(Self::RevokeAndAck(RevokeAndAck::decode(cursor)?)),
MessageType::UPDATE_FEE => Ok(Self::UpdateFee(UpdateFee::decode(cursor)?)),
MessageType::UPDATE_FAIL_MALFORMED_HTLC => Ok(Self::UpdateFailMalformedHtlc(
UpdateFailMalformedHtlc::decode(cursor)?,
)),
Expand Down Expand Up @@ -658,6 +668,7 @@ impl_from_message! {
UpdateFailHtlc => UPDATE_FAIL_HTLC,
CommitmentSigned => COMMITMENT_SIGNED,
RevokeAndAck => REVOKE_AND_ACK,
UpdateFee => UPDATE_FEE,
UpdateFailMalformedHtlc => UPDATE_FAIL_MALFORMED_HTLC,
ChannelReestablish => CHANNEL_REESTABLISH,
ChannelAnnouncement => CHANNEL_ANNOUNCEMENT,
Expand Down Expand Up @@ -1255,6 +1266,22 @@ mod tests {
assert_eq!(decoded, Message::RevokeAndAck(msg));
}

/// Valid `UpdateFee` message for testing.
fn sample_update_fee() -> UpdateFee {
UpdateFee {
channel_id: ChannelId::new([0x42; CHANNEL_ID_SIZE]),
feerate_per_kw: 2500,
}
}

#[test]
fn message_update_fee_roundtrip() {
let msg = sample_update_fee();
let encoded = Message::UpdateFee(msg.clone()).encode();
let decoded = Message::decode(&encoded).unwrap();
assert_eq!(decoded, Message::UpdateFee(msg));
}

#[test]
fn message_update_fail_malformed_htlc_roundtrip() {
let msg = UpdateFailMalformedHtlc {
Expand Down Expand Up @@ -1643,6 +1670,11 @@ mod tests {
"revoke_and_ack",
MessageType::REVOKE_AND_ACK,
),
(
Message::UpdateFee(sample_update_fee()),
"update_fee",
MessageType::UPDATE_FEE,
),
(
Message::UpdateFailMalformedHtlc(UpdateFailMalformedHtlc {
channel_id: ChannelId::new([0x42; CHANNEL_ID_SIZE]),
Expand Down
98 changes: 98 additions & 0 deletions smite/src/bolt/update_fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//! BOLT 2 `update_fee` message.

use super::BoltError;
use super::types::ChannelId;
use super::wire::WireFormat;

/// BOLT 2 `update_fee` message (type 134).
///
/// Sent by the node paying the Bitcoin fee to update the commitment feerate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateFee {
/// The channel ID.
pub channel_id: ChannelId,
/// The new commitment feerate in satoshis per 1000 weight.
pub feerate_per_kw: u32,
}

impl UpdateFee {
/// Encodes to wire format (without message type prefix).
#[must_use]
pub fn encode(&self) -> Vec<u8> {
let mut out = Vec::new();
self.channel_id.write(&mut out);
self.feerate_per_kw.write(&mut out);
out
}

/// Decodes from wire format (without message type prefix).
///
/// # Errors
///
/// Returns `Truncated` if the payload is too short.
pub fn decode(payload: &[u8]) -> Result<Self, BoltError> {
let mut cursor = payload;

let channel_id = WireFormat::read(&mut cursor)?;
let feerate_per_kw = WireFormat::read(&mut cursor)?;

Ok(Self {
channel_id,
feerate_per_kw,
})
}
}

#[cfg(test)]
mod tests {
use super::super::CHANNEL_ID_SIZE;
use super::*;

/// Valid `UpdateFee` message for testing.
fn sample_update_fee() -> UpdateFee {
UpdateFee {
channel_id: ChannelId::new([0x42; CHANNEL_ID_SIZE]),
feerate_per_kw: 2500,
}
}

#[test]
fn encode_fixed_field_size() {
let msg = sample_update_fee();
let encoded = msg.encode();
// channel_id(32) + feerate_per_kw(4) = 36
assert_eq!(encoded.len(), 36);
}

#[test]
fn roundtrip() {
let original = sample_update_fee();
let encoded = original.encode();
let decoded = UpdateFee::decode(&encoded).unwrap();
assert_eq!(original, decoded);
}

#[test]
fn decode_truncated_channel_id() {
assert_eq!(
UpdateFee::decode(&[0x00; 20]),
Err(BoltError::Truncated {
expected: CHANNEL_ID_SIZE,
actual: 20
})
);
}

#[test]
fn decode_truncated_feerate_per_kw() {
// channel_id(32) + 2 bytes into feerate_per_kw
let data = [0x00; 34];
assert_eq!(
UpdateFee::decode(&data),
Err(BoltError::Truncated {
expected: 4,
actual: 2
})
);
}
}
Loading