Skip to content

Commit

Permalink
Don't accept block by the same client who previously failed
Browse files Browse the repository at this point in the history
  • Loading branch information
inetic committed Oct 17, 2023
1 parent 27232cd commit 9554aad
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions lib/src/missing_parts/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl Tracker {
.entry(part_id)
.or_insert_with(|| MissingPartState {
offering_clients: HashSet::default(),
accepted_by: None,
currently_accepted_by: None,
previously_accepted_by: Default::default(),
required: false,
approved: false,
});
Expand Down Expand Up @@ -149,13 +150,18 @@ impl TrackerClient {
.entry(part_id)
.or_insert_with(|| MissingPartState {
offering_clients: HashSet::default(),
accepted_by: None,
currently_accepted_by: None,
previously_accepted_by: Default::default(),
required: false,
approved: false,
});

missing_part.offering_clients.insert(self.client_id);

// The peer could have previously lied about having the part (e.g. expired block). Now it's
// claiming to have it again so we need to reconsider loading from it again.
missing_part.previously_accepted_by.remove(&self.client_id);

match state {
OfferState::Approved => {
missing_part.approved = true;
Expand Down Expand Up @@ -184,6 +190,9 @@ impl Drop for TrackerClient {
notify = true;
}

// When the client reconnects, we allow it to accept the again.
missing_part.previously_accepted_by.remove(&self.client_id);

// TODO: if the block hasn't other offers and isn't required, remove it
}

Expand Down Expand Up @@ -233,9 +242,15 @@ impl PartPromiseAcceptor {
// unwrap is ok because of the invariant in `Inner`
let missing_part = inner.missing_parts.get_mut(part_id).unwrap();

if missing_part.required && missing_part.approved && missing_part.accepted_by.is_none()
if missing_part.required
&& missing_part.approved
&& missing_part.currently_accepted_by.is_none()
&& !missing_part
.previously_accepted_by
.contains(&self.client_id)
{
missing_part.accepted_by = Some(self.client_id);
missing_part.currently_accepted_by = Some(self.client_id);
missing_part.previously_accepted_by.insert(self.client_id);

return Some(PartPromise {
shared: self.shared.clone(),
Expand Down Expand Up @@ -354,16 +369,17 @@ struct Inner {
#[derive(Debug)]
struct MissingPartState {
offering_clients: HashSet<ClientId>,
accepted_by: Option<ClientId>,
currently_accepted_by: Option<ClientId>,
previously_accepted_by: HashSet<ClientId>,
required: bool,
approved: bool,
}

impl MissingPartState {
fn unaccept_by(&mut self, client_id: ClientId) -> bool {
if let Some(accepted_by) = &self.accepted_by {
if accepted_by == &client_id {
self.accepted_by = None;
if let Some(currently_accepted_by) = &self.currently_accepted_by {
if currently_accepted_by == &client_id {
self.currently_accepted_by = None;
return true;
}
}
Expand Down

0 comments on commit 9554aad

Please sign in to comment.