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

feat: ban libp2p peers on genesis hash mismatch #2184

Merged
merged 61 commits into from
Nov 21, 2022

Conversation

hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Nov 14, 2022

Summary of changes
Changes introduced in this pull request:

  • ban libp2p peers on genesis hash mismatch
  • ban libp2p peers on Ping protocol not supported

Based on #2154 Should let #2154 go first

Looking at the calibnet log, epoch too large errors during tipset validation are completely gone

Reference issue to close (if applicable)

Closes #2189

Other information and links

Copy link
Member

@LesnyRumcajs LesnyRumcajs left a comment

Choose a reason for hiding this comment

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

In general, it looks great. Do we ban peers permanently or only for some time? Potentially some peers may "correct" their behaviour and we should perhaps bring them back. I could imagine storing those in some kind of TLRU cache. Not for this PR of course, but maybe as a potential improvement in the future.

@@ -158,6 +158,7 @@ pub struct Libp2pService<DB, P: StoreParams> {
network_receiver_out: flume::Receiver<NetworkEvent>,
network_sender_out: flume::Sender<NetworkEvent>,
network_name: String,
genesis_hash: Cid,
Copy link
Member

Choose a reason for hiding this comment

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

maybe let's use genesis_cid, Cid is not precisely a hash (it does contain it, though)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -402,9 +406,10 @@ async fn handle_network_message<P: StoreParams>(

#[allow(clippy::too_many_arguments)]
async fn handle_forest_behaviour_event<DB, P: StoreParams>(
bh_mut: &mut ForestBehaviour<P>,
st_mut: &mut Swarm<ForestBehaviour<P>>,
Copy link
Member

Choose a reason for hiding this comment

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

do we need those postfixes? For example just swarm, it's short enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. That's a better name

@@ -425,6 +430,7 @@ async fn handle_forest_behaviour_event<DB, P: StoreParams>(
) where
DB: Blockstore + Store + BitswapStore<Params = P> + Clone + Sync + Send + 'static,
{
let bh_mut = st_mut.behaviour_mut();
Copy link
Member

Choose a reason for hiding this comment

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

same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -644,7 +658,13 @@ async fn handle_forest_behaviour_event<DB, P: StoreParams>(
);
}
Err(err) => {
warn!("{err}:{}", ping_event.peer.to_base58());
let err = err.to_string();
Copy link
Member

Choose a reason for hiding this comment

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

is this line needed?

Copy link
Contributor Author

@hanabi1224 hanabi1224 Nov 18, 2022

Choose a reason for hiding this comment

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

I haven't confirmed but I guess without this line, warn! will make an extra allocation by calling err.to_string in Display(we still need err.to_string().contains below I guess`) . Not a big deal tho I can change it if you prefer fewer lines

Copy link
Member

Choose a reason for hiding this comment

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

No, it's okay; I didn't notice the 2nd usage.

@hanabi1224
Copy link
Contributor Author

@LesnyRumcajs

Do we ban peers permanently or only for some time?

Looking at the source code, it's banned for the entire lifetime of the swarm instance.

Potentially some peers may "correct" their behaviour and we should perhaps bring them back.

Maybe we could propose some backward-compatible API changes to the upstream, e.g. adding fn ban_peer_id_with_duration

@LesnyRumcajs
Copy link
Member

@LesnyRumcajs

Do we ban peers permanently or only for some time?

Looking at the source code, it's banned for the entire lifetime of the swarm instance.

Potentially some peers may "correct" their behaviour and we should perhaps bring them back.

Maybe we could propose some backward-compatible API changes to the upstream, e.g. adding fn ban_peer_id_with_duration

I guess we could start by asking if there's a need for this, seems to me it may be pretty niche and it can be handled by the client code anyway (though with some overhead of an another hashmap).

@hanabi1224
Copy link
Contributor Author

I guess we could start by asking if there's a need for this

@LesnyRumcajs Yes, we need to achieve this with our code. libp2p/rust-libp2p#3140

@hanabi1224 hanabi1224 merged commit f909e52 into ChainSafe:main Nov 21, 2022
@hanabi1224 hanabi1224 deleted the libp2p-ban-genesis-mismatch branch November 21, 2022 15:16
@LesnyRumcajs
Copy link
Member

@hanabi1224 thanks for looking into this!

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.

Detect and ban libp2p peers when something is foundamentally wrong with their network.
3 participants