Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify fn prune_messages_to_be_saved #414

Merged

Conversation

quake
Copy link
Member

@quake quake commented Dec 21, 2024

The current implementation of prune_messages_to_be_saved is complex and unintuitive, it can be implemented in a much simpler way.

@quake quake requested a review from contrun December 21, 2024 10:42
@quake quake force-pushed the quake/refactor-prune_messages_to_be_saved branch from ab9c88a to 1a2ff8f Compare December 21, 2024 11:34
}
}
let mut sorted_messages = complete_messages.into_iter().collect::<Vec<_>>();
sorted_messages.sort_unstable();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to use the Ord implementation of BroadcastMessageWithTimestamp, which will sort messages according to their timestamp. It is possible that the timestamp of ChannelUpdate is smaller than that of Channel announcement (we didn't have code to specifically make the timestamp of ChannelUpdate later than the timestamp of the funding transaction block, we just use the system time).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will order by message_id first:

fiber/src/fiber/types.rs

Lines 2435 to 2441 in 1a2ff8f

impl Ord for BroadcastMessageWithTimestamp {
fn cmp(&self, other: &Self) -> Ordering {
self.message_id()
.cmp(&other.message_id())
.then(self.timestamp().cmp(&other.timestamp()))
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I didn't look carefully. In that case, this PR LGTM. I created a few tests for the dependency of channel gossip messages in #418 . Can you take a look and check if the code change here does not break the tests (I believe it doesn't. Still, it is better to
have some tests)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebased and pushed

@quake quake force-pushed the quake/refactor-prune_messages_to_be_saved branch from 1a2ff8f to c950ba1 Compare December 22, 2024 21:42
@quake quake merged commit 6042adc into nervosnetwork:develop Dec 25, 2024
23 checks passed
let messages_to_be_saved = take(&mut self.messages_to_be_saved);
let (complete_messages, uncomplete_messages) = messages_to_be_saved
.into_iter()
.partition(|m| self.has_dependencies_available(m));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a subtle bug here. Because we replaces messages_to_be_saved in line 1021, the line 1024 will always return false for ChannelUpdate messages that do not have corresponding ChannelAnnouncement in the store. Previously, the behavior is to also check corresponding ChannelAnnouncement in messages_to_be_saved. This bug is fixed in contrun@8ca34e0 . I will create a PR later.

BroadcastMessageID::NodeAnnouncement(pubkey2),
) => pubkey1.cmp(pubkey2),
(BroadcastMessageID::ChannelUpdate(_), _) => Ordering::Less,
(BroadcastMessageID::NodeAnnouncement(_), _) => Ordering::Greater,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to order messages by making NodeAnnouncement in the first place, ChannelAnnouncement the second place and ChannelUpdate the last place (this way the sorted messages in the list messages_to_be_saved can be processed sequentially)? If that is the case, I think the correct Ordering should be contrun@ea5c1fe. This problem is not discovered in the test because another bug above always excluding ChannelUpdate from being included in the complete messages when there is a corresponding ChannelAnnouncement in the messages_to_be_saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants