Skip to content

Commit

Permalink
add immediate_update for ChannelUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jan 2, 2025
1 parent ba2d2bc commit 37070e2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
14 changes: 13 additions & 1 deletion src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ where
};

let channel_update = if error_code.is_update() {
state.try_create_channel_update_message(&self.network).await
state.generate_immediate_channel_update(&self.network).await
} else {
None
};
Expand Down Expand Up @@ -3478,6 +3478,18 @@ impl ChannelActorState {
Some(self.generate_channel_update(network).await)
}

async fn generate_immediate_channel_update(
&mut self,
network: &ActorRef<NetworkActorMessage>,
) -> Option<ChannelUpdate> {
self.try_create_channel_update_message(network)
.await
.map(move |mut channel_update| {
channel_update.immediate_update = true;
channel_update
})
}

pub fn get_unsigned_channel_update_message(&self) -> Option<ChannelUpdate> {
let local_is_node1 = self.local_is_node1();
let message_flags = if local_is_node1 { 0 } else { 1 };
Expand Down
37 changes: 26 additions & 11 deletions src/fiber/gen/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4274,6 +4274,7 @@ impl ::core::fmt::Display for ChannelUpdate {
"tlc_fee_proportional_millionths",
self.tlc_fee_proportional_millionths()
)?;
write!(f, ", {}: {}", "immediate_update", self.immediate_update())?;
write!(f, " }}")
}
}
Expand All @@ -4284,18 +4285,18 @@ impl ::core::default::Default for ChannelUpdate {
}
}
impl ChannelUpdate {
const DEFAULT_VALUE: [u8; 204] = [
const DEFAULT_VALUE: [u8; 205] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const TOTAL_SIZE: usize = 204;
pub const FIELD_SIZES: [usize; 10] = [64, 32, 36, 8, 4, 4, 8, 16, 16, 16];
pub const FIELD_COUNT: usize = 10;
pub const TOTAL_SIZE: usize = 205;
pub const FIELD_SIZES: [usize; 11] = [64, 32, 36, 8, 4, 4, 8, 16, 16, 16, 1];
pub const FIELD_COUNT: usize = 11;
pub fn signature(&self) -> EcdsaSignature {
EcdsaSignature::new_unchecked(self.0.slice(0..64))
}
Expand Down Expand Up @@ -4326,6 +4327,9 @@ impl ChannelUpdate {
pub fn tlc_fee_proportional_millionths(&self) -> Uint128 {
Uint128::new_unchecked(self.0.slice(188..204))
}
pub fn immediate_update(&self) -> Byte {
Byte::new_unchecked(self.0.slice(204..205))
}
pub fn as_reader<'r>(&'r self) -> ChannelUpdateReader<'r> {
ChannelUpdateReader::new_unchecked(self.as_slice())
}
Expand Down Expand Up @@ -4363,6 +4367,7 @@ impl molecule::prelude::Entity for ChannelUpdate {
.tlc_minimum_value(self.tlc_minimum_value())
.tlc_maximum_value(self.tlc_maximum_value())
.tlc_fee_proportional_millionths(self.tlc_fee_proportional_millionths())
.immediate_update(self.immediate_update())
}
}
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -4399,13 +4404,14 @@ impl<'r> ::core::fmt::Display for ChannelUpdateReader<'r> {
"tlc_fee_proportional_millionths",
self.tlc_fee_proportional_millionths()
)?;
write!(f, ", {}: {}", "immediate_update", self.immediate_update())?;
write!(f, " }}")
}
}
impl<'r> ChannelUpdateReader<'r> {
pub const TOTAL_SIZE: usize = 204;
pub const FIELD_SIZES: [usize; 10] = [64, 32, 36, 8, 4, 4, 8, 16, 16, 16];
pub const FIELD_COUNT: usize = 10;
pub const TOTAL_SIZE: usize = 205;
pub const FIELD_SIZES: [usize; 11] = [64, 32, 36, 8, 4, 4, 8, 16, 16, 16, 1];
pub const FIELD_COUNT: usize = 11;
pub fn signature(&self) -> EcdsaSignatureReader<'r> {
EcdsaSignatureReader::new_unchecked(&self.as_slice()[0..64])
}
Expand Down Expand Up @@ -4436,6 +4442,9 @@ impl<'r> ChannelUpdateReader<'r> {
pub fn tlc_fee_proportional_millionths(&self) -> Uint128Reader<'r> {
Uint128Reader::new_unchecked(&self.as_slice()[188..204])
}
pub fn immediate_update(&self) -> ByteReader<'r> {
ByteReader::new_unchecked(&self.as_slice()[204..205])
}
}
impl<'r> molecule::prelude::Reader<'r> for ChannelUpdateReader<'r> {
type Entity = ChannelUpdate;
Expand Down Expand Up @@ -4470,11 +4479,12 @@ pub struct ChannelUpdateBuilder {
pub(crate) tlc_minimum_value: Uint128,
pub(crate) tlc_maximum_value: Uint128,
pub(crate) tlc_fee_proportional_millionths: Uint128,
pub(crate) immediate_update: Byte,
}
impl ChannelUpdateBuilder {
pub const TOTAL_SIZE: usize = 204;
pub const FIELD_SIZES: [usize; 10] = [64, 32, 36, 8, 4, 4, 8, 16, 16, 16];
pub const FIELD_COUNT: usize = 10;
pub const TOTAL_SIZE: usize = 205;
pub const FIELD_SIZES: [usize; 11] = [64, 32, 36, 8, 4, 4, 8, 16, 16, 16, 1];
pub const FIELD_COUNT: usize = 11;
pub fn signature(mut self, v: EcdsaSignature) -> Self {
self.signature = v;
self
Expand Down Expand Up @@ -4515,6 +4525,10 @@ impl ChannelUpdateBuilder {
self.tlc_fee_proportional_millionths = v;
self
}
pub fn immediate_update(mut self, v: Byte) -> Self {
self.immediate_update = v;
self
}
}
impl molecule::prelude::Builder for ChannelUpdateBuilder {
type Entity = ChannelUpdate;
Expand All @@ -4533,6 +4547,7 @@ impl molecule::prelude::Builder for ChannelUpdateBuilder {
writer.write_all(self.tlc_minimum_value.as_slice())?;
writer.write_all(self.tlc_maximum_value.as_slice())?;
writer.write_all(self.tlc_fee_proportional_millionths.as_slice())?;
writer.write_all(self.immediate_update.as_slice())?;
Ok(())
}
fn build(&self) -> Self::Entity {
Expand Down
14 changes: 13 additions & 1 deletion src/fiber/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,8 +1650,20 @@ where
let _ = self
.store
.actor
.send_message(ExtendedGossipMessageStoreMessage::SaveMessage(message))
.send_message(ExtendedGossipMessageStoreMessage::SaveMessage(
message.clone(),
))
.expect("store actor alive");

if let BroadcastMessage::ChannelUpdate(channel_update) = message {
if channel_update.immediate_update {
let _ = self
.store
.actor
.send_message(ExtendedGossipMessageStoreMessage::Tick)
.expect("store actor alive");
}
}
}

async fn send_message_to_session(
Expand Down
3 changes: 2 additions & 1 deletion src/fiber/schema/gossip.mol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct BroadcastMessageQuery {
vector BroadcastMessageQueries <BroadcastMessageQuery>;

// A NodeAnnouncement is a broadcast message to the network for the node information.
// An update to the node information can be broadcasted with a NodeAnnouncement with larger timestamp.
// An update to the node information can be broadcasted with a NodeAnnouncement with larger timestamp.
table NodeAnnouncement {
// Signature to this message.
signature: EcdsaSignature,
Expand Down Expand Up @@ -92,6 +92,7 @@ struct ChannelUpdate {
tlc_minimum_value: Uint128,
tlc_maximum_value: Uint128,
tlc_fee_proportional_millionths: Uint128,
immediate_update: byte,
}

// All the broadcast messages.
Expand Down
5 changes: 5 additions & 0 deletions src/fiber/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,7 @@ pub struct ChannelUpdate {
pub tlc_expiry_delta: u64,
pub tlc_minimum_value: u128,
pub tlc_fee_proportional_millionths: u128,
pub immediate_update: bool,
}

impl ChannelUpdate {
Expand Down Expand Up @@ -1982,6 +1983,7 @@ impl ChannelUpdate {
tlc_expiry_delta,
tlc_minimum_value,
tlc_fee_proportional_millionths,
immediate_update: false,
}
}

Expand All @@ -1996,6 +1998,7 @@ impl ChannelUpdate {
tlc_expiry_delta: self.tlc_expiry_delta,
tlc_minimum_value: self.tlc_minimum_value,
tlc_fee_proportional_millionths: self.tlc_fee_proportional_millionths,
immediate_update: self.immediate_update,
};
deterministically_hash(&molecule_gossip::ChannelUpdate::from(unsigned_update))
}
Expand Down Expand Up @@ -2030,6 +2033,7 @@ impl From<ChannelUpdate> for molecule_gossip::ChannelUpdate {
.channel_flags(channel_update.channel_flags.pack())
.tlc_expiry_delta(channel_update.tlc_expiry_delta.pack())
.tlc_minimum_value(channel_update.tlc_minimum_value.pack())
.immediate_update((channel_update.immediate_update as u8).into())
.tlc_fee_proportional_millionths(channel_update.tlc_fee_proportional_millionths.pack());

let builder = if let Some(signature) = channel_update.signature {
Expand Down Expand Up @@ -2058,6 +2062,7 @@ impl TryFrom<molecule_gossip::ChannelUpdate> for ChannelUpdate {
tlc_fee_proportional_millionths: channel_update
.tlc_fee_proportional_millionths()
.unpack(),
immediate_update: u8::from(channel_update.immediate_update()) != 0,
})
}
}
Expand Down

0 comments on commit 37070e2

Please sign in to comment.