Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Issue 4804: Notify chain selection of concluded disputes directly #6512

Merged
merged 33 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
658f9de
Setting up new ChainSelectionMessage
BradleyOlson64 Dec 28, 2022
c66ea70
Partial first pass
BradleyOlson64 Jan 2, 2023
8ef1202
Got dispute conclusion data to provisioner
BradleyOlson64 Jan 3, 2023
e69a5b4
Finished first draft for 4804 code
BradleyOlson64 Jan 5, 2023
94e231a
Merge branch 'master' of https://github.com/paritytech/polkadot into …
BradleyOlson64 Jan 5, 2023
b370985
A bit of polish and code comments
BradleyOlson64 Jan 5, 2023
043f505
cargo fmt
BradleyOlson64 Jan 5, 2023
85db7c9
Implementers guide and code comments
BradleyOlson64 Jan 5, 2023
161122d
More formatting, and naming issues
BradleyOlson64 Jan 6, 2023
6a72033
Wrote test for ChainSelection side of change
BradleyOlson64 Jan 6, 2023
59e0453
Added dispute coordinator side test
BradleyOlson64 Jan 6, 2023
e12bfca
Merge branch 'master' of https://github.com/paritytech/polkadot into …
BradleyOlson64 Jan 6, 2023
682545d
FMT
BradleyOlson64 Jan 6, 2023
3b026ad
Merge branch 'master' of https://github.com/paritytech/polkadot into …
BradleyOlson64 Jan 9, 2023
87fc1f5
Addressing Marcin's comments
BradleyOlson64 Jan 9, 2023
013c4eb
fmt
BradleyOlson64 Jan 9, 2023
6442838
Addressing further Marcin comment
BradleyOlson64 Jan 9, 2023
bd6fe63
Removing unnecessary test line
BradleyOlson64 Jan 9, 2023
eff981b
Rough draft addressing Robert changes
BradleyOlson64 Jan 11, 2023
428e016
Clean up and test modification
BradleyOlson64 Jan 12, 2023
9f6f2ab
Majorly refactored scraper change
BradleyOlson64 Jan 13, 2023
5b89cf8
Minor fixes for ChainSelection
BradleyOlson64 Jan 13, 2023
d2454d8
Polish and fmt
BradleyOlson64 Jan 13, 2023
32b6f79
Condensing inclusions per candidate logic
BradleyOlson64 Jan 13, 2023
548e11b
Addressing Tsveto's comments
BradleyOlson64 Jan 16, 2023
bd283f0
Addressing Robert's Comments
BradleyOlson64 Jan 17, 2023
a257a1c
Altered inclusions struct to use nested BTreeMaps
BradleyOlson64 Jan 17, 2023
7c1149b
Naming fix
BradleyOlson64 Jan 17, 2023
a75cda3
Fixing inclusions struct comments
BradleyOlson64 Jan 17, 2023
992f240
Update node/core/dispute-coordinator/src/scraping/mod.rs
BradleyOlson64 Jan 17, 2023
492c7ed
Optimizing removal at block height for inclusions
BradleyOlson64 Jan 18, 2023
f6779cd
fmt
BradleyOlson64 Jan 18, 2023
f9b92fa
Using copy trait
BradleyOlson64 Jan 19, 2023
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
7 changes: 3 additions & 4 deletions node/core/dispute-coordinator/src/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,9 @@ impl Initialized {
// Notify ChainSelection if a dispute has concluded against a candidate. ChainSelection
// will need to mark the candidate's relay parent as reverted.
if import_result.is_freshly_concluded_against() {
if let Some(blocks_to_revert) =
self.scraper.get_blocks_including_candidate(&candidate_hash)
{
ctx.send_message(ChainSelectionMessage::RevertBlocks(blocks_to_revert.clone()))
let blocks_including = self.scraper.get_blocks_including_candidate(&candidate_hash);
if blocks_including.len() > 0 {
ctx.send_message(ChainSelectionMessage::RevertBlocks(blocks_including))
.await;
} else {
gum::debug!(
Expand Down
38 changes: 25 additions & 13 deletions node/core/dispute-coordinator/src/scraping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use std::{collections::HashMap, num::NonZeroUsize};
use std::{collections::BTreeMap, num::NonZeroUsize};

use futures::channel::oneshot;
use lru::LruCache;
Expand Down Expand Up @@ -75,12 +75,12 @@ impl ScrapedUpdates {
/// in each vector are ordered by decreasing parent block number to facilitate
/// minimal cost pruning.
pub struct Inclusions {
inclusions_inner: HashMap<CandidateHash, Vec<(BlockNumber, Hash)>>,
inclusions_inner: BTreeMap<CandidateHash, BTreeMap<BlockNumber, Vec<Hash>>>,
}

impl Inclusions {
pub fn new() -> Self {
Self { inclusions_inner: HashMap::new() }
Self { inclusions_inner: BTreeMap::new() }
}

// Insert parent block into vector for the candidate hash it is including,
BradleyOlson64 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -92,24 +92,36 @@ impl Inclusions {
block_hash: Hash,
) {
if let Some(blocks_including) = self.inclusions_inner.get_mut(&candidate_hash) {
let first_lower_entry = blocks_including.partition_point(|(number_at_idx, _)| *number_at_idx >= block_number);
blocks_including.insert(first_lower_entry, (block_number, block_hash));
if let Some(blocks_at_height) = blocks_including.get_mut(&block_number) {
blocks_at_height.push(block_hash);
} else {
blocks_including.insert(block_number, Vec::from([block_hash]));
}
} else {
let mut blocks_including: BTreeMap<BlockNumber, Vec<Hash>> = BTreeMap::new();
blocks_including.insert(block_number, Vec::from([block_hash]));
self.inclusions_inner
.insert(candidate_hash, Vec::from([(block_number, block_hash)]));
.insert(candidate_hash, blocks_including);
}
}

pub fn remove_up_to_height(&mut self, height: &BlockNumber) {
for including_blocks in self.inclusions_inner.values_mut() {
let first_lower_entry = including_blocks.partition_point(|(number_at_idx, _)| *number_at_idx >= *height);
including_blocks.drain(first_lower_entry..);
for blocks_including in self.inclusions_inner.values_mut() {
*blocks_including = blocks_including.split_off(height);
BradleyOlson64 marked this conversation as resolved.
Show resolved Hide resolved
}
self.inclusions_inner.retain(|_, including_blocks| including_blocks.len() > 0);
self.inclusions_inner.retain(|_, including_blocks| including_blocks.keys().len() > 0);
BradleyOlson64 marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn get(&mut self, candidate: &CandidateHash) -> Option<&Vec<(BlockNumber, Hash)>> {
self.inclusions_inner.get(candidate)
pub fn get(&mut self, candidate: &CandidateHash) -> Vec<(BlockNumber, Hash)> {
BradleyOlson64 marked this conversation as resolved.
Show resolved Hide resolved
let mut inclusions_as_vec: Vec<(BlockNumber, Hash)> = Vec::new();
if let Some(blocks_including) = self.inclusions_inner.get(candidate) {
for (height, blocks_at_height) in blocks_including.iter() {
for block in blocks_at_height {
inclusions_as_vec.push((*height, *block));
}
}
}
inclusions_as_vec
}
}

Expand Down Expand Up @@ -377,7 +389,7 @@ impl ChainScraper {
pub fn get_blocks_including_candidate(
&mut self,
candidate: &CandidateHash,
) -> Option<&Vec<(BlockNumber, Hash)>> {
) -> Vec<(BlockNumber, Hash)> {
self.inclusions.get(candidate)
}
}
Expand Down
14 changes: 7 additions & 7 deletions node/core/dispute-coordinator/src/scraping/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,10 @@ fn inclusions_per_candidate_properly_adds_and_prunes() {
// the candidate is included are recorded
assert_eq!(
scraper.get_blocks_including_candidate(&candidate.hash()),
Some(&Vec::from([
(TEST_TARGET_BLOCK_NUMBER_2, get_block_number_hash(TEST_TARGET_BLOCK_NUMBER_2)),
(TEST_TARGET_BLOCK_NUMBER, get_block_number_hash(TEST_TARGET_BLOCK_NUMBER))
]))
Vec::from([
(TEST_TARGET_BLOCK_NUMBER, get_block_number_hash(TEST_TARGET_BLOCK_NUMBER)),
(TEST_TARGET_BLOCK_NUMBER_2, get_block_number_hash(TEST_TARGET_BLOCK_NUMBER_2))
])
);

// After `DISPUTE_CANDIDATE_LIFETIME_AFTER_FINALIZATION` blocks the earlier inclusion should be removed
Expand All @@ -634,17 +634,17 @@ fn inclusions_per_candidate_properly_adds_and_prunes() {
// The later inclusion should still be present, as we haven't exceeded its lifetime
assert_eq!(
scraper.get_blocks_including_candidate(&candidate.hash()),
Some(&Vec::from([(
Vec::from([(
TEST_TARGET_BLOCK_NUMBER_2,
get_block_number_hash(TEST_TARGET_BLOCK_NUMBER_2)
)]))
)])
);

finalized_block_number =
TEST_TARGET_BLOCK_NUMBER_2 + DISPUTE_CANDIDATE_LIFETIME_AFTER_FINALIZATION;
process_finalized_block(&mut scraper, &finalized_block_number);

// Now both inclusions have exceeded their lifetimes after finalization and should be purged
assert_eq!(scraper.get_blocks_including_candidate(&candidate.hash()), None);
assert!(scraper.get_blocks_including_candidate(&candidate.hash()).len() == 0);
});
}