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

approval-voting: Make importing of duplicate assignment idempotent #6971

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
78 changes: 50 additions & 28 deletions polkadot/node/core/approval-voting/src/approval_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,13 @@ mod tests {
}
.into();

approval_entry.import_assignment(0, ValidatorIndex(0), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, false);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick, false);

approval_entry.import_assignment(1, ValidatorIndex(2), block_tick + 1);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick + 1);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick + 1, false);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick + 1, false);

approval_entry.import_assignment(2, ValidatorIndex(4), block_tick + 2);
approval_entry.import_assignment(2, ValidatorIndex(4), block_tick + 2, false);

let approvals = bitvec![u8, BitOrderLsb0; 1; 5];

Expand Down Expand Up @@ -757,8 +757,10 @@ mod tests {
}
.into();

approval_entry.import_assignment(0, ValidatorIndex(0), block_tick);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, false);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, true);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick, false);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick, true);

let approvals = bitvec![u8, BitOrderLsb0; 0; 10];

Expand Down Expand Up @@ -798,10 +800,10 @@ mod tests {
}
.into();

approval_entry.import_assignment(0, ValidatorIndex(0), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, false);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick, false);

approval_entry.import_assignment(1, ValidatorIndex(2), block_tick);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick, false);

let mut approvals = bitvec![u8, BitOrderLsb0; 0; 10];
approvals.set(0, true);
Expand Down Expand Up @@ -844,11 +846,11 @@ mod tests {
}
.into();

approval_entry.import_assignment(0, ValidatorIndex(0), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, false);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick, false);

approval_entry.import_assignment(1, ValidatorIndex(2), block_tick);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick, false);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick, false);

let mut approvals = bitvec![u8, BitOrderLsb0; 0; n_validators];
approvals.set(0, true);
Expand Down Expand Up @@ -913,14 +915,24 @@ mod tests {
}
.into();

approval_entry.import_assignment(0, ValidatorIndex(0), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, false);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick, false);

approval_entry.import_assignment(1, ValidatorIndex(2), block_tick + 1);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick + 1);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick + 1, false);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick + 1, false);

approval_entry.import_assignment(2, ValidatorIndex(4), block_tick + no_show_duration + 2);
approval_entry.import_assignment(2, ValidatorIndex(5), block_tick + no_show_duration + 2);
approval_entry.import_assignment(
2,
ValidatorIndex(4),
block_tick + no_show_duration + 2,
false,
);
approval_entry.import_assignment(
2,
ValidatorIndex(5),
block_tick + no_show_duration + 2,
false,
);

let mut approvals = bitvec![u8, BitOrderLsb0; 0; n_validators];
approvals.set(0, true);
Expand Down Expand Up @@ -1007,14 +1019,24 @@ mod tests {
}
.into();

approval_entry.import_assignment(0, ValidatorIndex(0), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick);
approval_entry.import_assignment(0, ValidatorIndex(0), block_tick, false);
approval_entry.import_assignment(0, ValidatorIndex(1), block_tick, false);

approval_entry.import_assignment(1, ValidatorIndex(2), block_tick + 1);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick + 1);
approval_entry.import_assignment(1, ValidatorIndex(2), block_tick + 1, false);
approval_entry.import_assignment(1, ValidatorIndex(3), block_tick + 1, false);

approval_entry.import_assignment(2, ValidatorIndex(4), block_tick + no_show_duration + 2);
approval_entry.import_assignment(2, ValidatorIndex(5), block_tick + no_show_duration + 2);
approval_entry.import_assignment(
2,
ValidatorIndex(4),
block_tick + no_show_duration + 2,
false,
);
approval_entry.import_assignment(
2,
ValidatorIndex(5),
block_tick + no_show_duration + 2,
false,
);

let mut approvals = bitvec![u8, BitOrderLsb0; 0; n_validators];
approvals.set(0, true);
Expand Down Expand Up @@ -1066,7 +1088,7 @@ mod tests {
},
);

approval_entry.import_assignment(3, ValidatorIndex(6), block_tick);
approval_entry.import_assignment(3, ValidatorIndex(6), block_tick, false);
approvals.set(6, true);

let tranche_now = no_show_duration as DelayTranche + 3;
Expand Down Expand Up @@ -1176,7 +1198,7 @@ mod tests {
// Populate the requested tranches. The assignments aren't inspected in
// this test.
for &t in &test_tranche {
approval_entry.import_assignment(t, ValidatorIndex(0), 0)
approval_entry.import_assignment(t, ValidatorIndex(0), 0, false);
}

let filled_tranches = filled_tranche_iterator(approval_entry.tranches());
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/core/approval-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,7 @@ where
)),
};
is_duplicate &= approval_entry.is_assigned(assignment.validator);
approval_entry.import_assignment(tranche, assignment.validator, tick_now);
approval_entry.import_assignment(tranche, assignment.validator, tick_now, is_duplicate);

// We've imported a new assignment, so we need to schedule a wake-up for when that might
// no-show.
Expand Down
12 changes: 10 additions & 2 deletions polkadot/node/core/approval-voting/src/persisted_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl ApprovalEntry {
});

our.map(|a| {
self.import_assignment(a.tranche(), a.validator_index(), tick_now);
self.import_assignment(a.tranche(), a.validator_index(), tick_now, false);

(a.cert().clone(), a.validator_index(), a.tranche())
})
Expand All @@ -197,6 +197,7 @@ impl ApprovalEntry {
tranche: DelayTranche,
validator_index: ValidatorIndex,
tick_now: Tick,
is_duplicate: bool,
) {
// linear search probably faster than binary. not many tranches typically.
let idx = match self.tranches.iter().position(|t| t.tranche >= tranche) {
Expand All @@ -214,7 +215,14 @@ impl ApprovalEntry {
},
};

self.tranches[idx].assignments.push((validator_index, tick_now));
if !is_duplicate ||
Copy link
Member

Choose a reason for hiding this comment

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

I am not getting the !is_duplicate part. Can you give me your line of reasoning here, that would help I think. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We keep the assignments in two places in assigned_validators: Bitfield, and tranches: Vec<TrancheEntry>, so when we call this function we already know if we saw an assignment for this validator which is really rare case. Given that this is on the hot-path I want to prevent running this iteration if it is not truly needed which is where !is_duplicate helps.

Copy link
Member

Choose a reason for hiding this comment

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

I think this deserves a comment in the code as it's not obvious how is_duplicate is different from the iter check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed with @ordian this and we convinced ourselves that iterating on duplicates is actually superfluous, so I removed that part.

!self.tranches[idx]
alexggh marked this conversation as resolved.
Show resolved Hide resolved
.assignments
.iter()
.any(|(validator, _)| validator == &validator_index)
{
self.tranches[idx].assignments.push((validator_index, tick_now));
}
self.assigned_validators.set(validator_index.0 as _, true);
}

Expand Down
Loading