Implement Approval Voting Subsystem (#2112)

* skeleton

* skeleton aux-schema module

* start approval types

* start aux schema with aux store

* doc

* finish basic types

* start approval types

* doc

* finish basic types

* write out schema types

* add debug and codec impls to approval types

* add debug and codec impls to approval types

also add some key computation

* add debug and codec impls to approval types

* getters for block and candidate entries

* grumbles

* remove unused AssignmentId

* load_decode utility

* implement DB clearing

* function for adding new block entry to aux store

* start `canonicalize` implementation

* more skeleton

* finish implementing canonicalize

* tag TODO

* implement a test AuxStore

* add allow(unused)

* basic loading and deleting test

* block_entry test function

* add a test for `add_block_entry`

* ensure range is exclusive at end

* test clear()

* test that add_block sets children

* add a test for canonicalize

* extract Pre-digest from header

* utilities for extracting RelayVRFStory from the header-chain

* add approval voting message types

* approval distribution message type

* subsystem skeleton

* state struct

* add futures-timer

* prepare service for babe slot duration

* more skeleton

* better integrate AuxStore

* RelayVRF -> RelayVRFStory

* canonicalize

* implement some tick functionality

* guide: tweaks

* check_approval

* more tweaks and helpers

* guide: add core index to candidate event

* primitives: add core index to candidate event

* runtime: add core index to candidate events

* head handling (session window)

* implement `determine_new_blocks`

* add TODO

* change error type on functions

* compute RelayVRFModulo assignments

* compute RelayVRFDelay assignments

* fix delay tranche calc

* assignment checking

* pluralize

* some dummy code for fetching assignments

* guide: add babe epoch runtime API

* implement a current_epoch() runtime API

* compute assignments

* candidate events get backing group

* import blocks and assignments into DB

* push block approval meta

* add message types, no overseer integration yet

* notify approval distribution of new blocks

* refactor import into separate functions

* impl tranches_to_approve

* guide: improve function signatures

* guide: remove Tick from ApprovalEntry

* trigger and broadcast assignment

* most of approval launching

* remove byteorder crate

* load blocks back to finality, except on startup

* check unchecked assignments

* add claimed core to approval voting message

* fix checks

* assign only to backing group

* remove import_checked_assignment from guide

* newline

* import assignments

* abstract out a bit

* check and import approvals

* check full approvals from assignment import too

* comment

* create a Transaction utility

* must_use

* use transaction in `check_full_approvals`

* wire up wakeups

* add Ord to CandidateHash

* wakeup refactoring

* return candidate info from add_block_entry

* schedule wakeups

* background task: do candidate validation

* forward candidate validation requests

* issue approval votes when requested

* clean up a couple TODOs

* fix up session caching

* clean up last unimplemented!() items

* fix remaining warnings

* remove TODO

* implement handle_approved_ancestor

* update Cargo.lock

* fix runtime API tests

* guide: cleanup assignment checking

* use claimed candidate index instead of core

* extract time to a trait

* tests module

* write a mock clock for testing

* allow swapping out the clock

* make abstract over assignment criteria

* add some skeleton tests and simplify params

* fix backing group check

* do backing group check inside check_assignment_cert

* write some empty test functions to implement

* add a test for non-backing

* test that produced checks pass

* some empty test ideas

* runtime/inclusion: remove outdated TODO

* fix compilation

* av-store: fix tests

* dummy cert

* criteria tests

* move `TestStore` to main tests file

* fix unused warning

* test harness beginnings

* resolve slots renaming fallout

* more compilation fixes

* wip: extract pure data into a separate module

* wip: extract pure data into a separate module

* move types completely to v1

* add persisted_entries

* add conversion trait impls

* clean up some warnings

* extract import logic to own module

* schedule wakeups

* experiment with Actions

* uncomment approval-checking

* separate module for approval checking utilities

* port more code to use actions

* get approval pipeline using actions

* all logic is uncommented

* main loop processes actions

* all loop logic uncommented

* separate function for handling actions

* remove last unimplemented item

* clean up warnings

* State gives read-only access to underlying DB

* tests for approval checking

* tests for approval criteria

* skeleton test module for import

* list of import tests to do

* some test glue code

* test reject bad assignment

* test slot too far in future

* test reject assignment with unknown candidate

* remove loads_blocks tests

* determine_new_blocks back to finalized & harness

* more coverage for determining new blocks

* make `imported_block_info` have less reliance on State

* candidate_info tests

* tests for session caching

* remove println

* extricate DB and main TestStores

* rewrite approval checking logic to counteract early delays

* move state out of function

* update approval-checking tests

* tweak wakeups & scheduling logic

* rename check_full_approvals

* test that assignment import updates candidate

* some approval import tests

* some tests for check_and_apply_approval

* add 'full' qualifier to avoid confusion

* extract should-trigger logic to separate function

* some tests for all triggering

* tests for when we trigger assignments

* test wakeups

* add block utilities for testing

* some more tests for approval updates

* approved_ancestor tests

* new action type for launch approval

* process-wakeup tests

* clean up some warnings

* fix in_future test

* approval checking tests

* tighten up too-far-in-future

* special-case genesis when caching sessions

* fix bitfield len

Co-authored-by: Andronik Ordian <write@reusable.software>
This commit is contained in:
Robert Habermeier
2021-02-11 10:21:47 -06:00
committed by GitHub
parent 09eadfc979
commit e48c687504
29 changed files with 7505 additions and 132 deletions
+32 -9
View File
@@ -51,8 +51,6 @@ pub struct AvailabilityBitfieldRecord<N> {
}
/// A backed candidate pending availability.
// TODO: split this type and change this to hold a plain `CandidateReceipt`.
// https://github.com/paritytech/polkadot/issues/1357
#[derive(Encode, Decode, PartialEq)]
#[cfg_attr(test, derive(Debug))]
pub struct CandidatePendingAvailability<H, N> {
@@ -70,6 +68,8 @@ pub struct CandidatePendingAvailability<H, N> {
relay_parent_number: N,
/// The block number of the relay-chain block this was backed in.
backed_in_number: N,
/// The group index backing this block.
backing_group: GroupIndex,
}
impl<H, N> CandidatePendingAvailability<H, N> {
@@ -197,11 +197,11 @@ decl_error! {
decl_event! {
pub enum Event<T> where <T as frame_system::Config>::Hash {
/// A candidate was backed. [candidate, head_data]
CandidateBacked(CandidateReceipt<Hash>, HeadData),
CandidateBacked(CandidateReceipt<Hash>, HeadData, CoreIndex, GroupIndex),
/// A candidate was included. [candidate, head_data]
CandidateIncluded(CandidateReceipt<Hash>, HeadData),
CandidateIncluded(CandidateReceipt<Hash>, HeadData, CoreIndex, GroupIndex),
/// A candidate timed out. [candidate, head_data]
CandidateTimedOut(CandidateReceipt<Hash>, HeadData),
CandidateTimedOut(CandidateReceipt<Hash>, HeadData, CoreIndex),
}
}
@@ -368,6 +368,8 @@ impl<T: Config> Module<T> {
receipt,
pending_availability.backers,
pending_availability.availability_votes,
pending_availability.core,
pending_availability.backing_group,
);
freed_cores.push(pending_availability.core);
@@ -555,7 +557,7 @@ impl<T: Config> Module<T> {
}
}
core_indices_and_backers.push((assignment.core, backers));
core_indices_and_backers.push((assignment.core, backers, assignment.group_idx));
continue 'a;
}
}
@@ -578,8 +580,8 @@ impl<T: Config> Module<T> {
};
// one more sweep for actually writing to storage.
let core_indices = core_indices_and_backers.iter().map(|&(ref c, _)| c.clone()).collect();
for (candidate, (core, backers)) in candidates.into_iter().zip(core_indices_and_backers) {
let core_indices = core_indices_and_backers.iter().map(|&(ref c, _, _)| c.clone()).collect();
for (candidate, (core, backers, group)) in candidates.into_iter().zip(core_indices_and_backers) {
let para_id = candidate.descriptor().para_id;
// initialize all availability votes to 0.
@@ -589,6 +591,8 @@ impl<T: Config> Module<T> {
Self::deposit_event(Event::<T>::CandidateBacked(
candidate.candidate.to_plain(),
candidate.candidate.commitments.head_data.clone(),
core,
group,
));
let candidate_hash = candidate.candidate.hash();
@@ -606,6 +610,7 @@ impl<T: Config> Module<T> {
relay_parent_number,
backers,
backed_in_number: check_cx.now,
backing_group: group,
});
<PendingAvailabilityCommitments>::insert(&para_id, commitments);
}
@@ -651,6 +656,8 @@ impl<T: Config> Module<T> {
receipt: CommittedCandidateReceipt<T::Hash>,
backers: BitVec<BitOrderLsb0, u8>,
availability_votes: BitVec<BitOrderLsb0, u8>,
core_index: CoreIndex,
backing_group: GroupIndex,
) -> Weight {
let plain = receipt.to_plain();
let commitments = receipt.commitments;
@@ -695,7 +702,7 @@ impl<T: Config> Module<T> {
);
Self::deposit_event(
Event::<T>::CandidateIncluded(plain, commitments.head_data.clone())
Event::<T>::CandidateIncluded(plain, commitments.head_data.clone(), core_index, backing_group)
);
weight + <paras::Module<T>>::note_new_head(
@@ -736,6 +743,7 @@ impl<T: Config> Module<T> {
Self::deposit_event(Event::<T>::CandidateTimedOut(
candidate,
commitments.head_data,
pending.core,
));
}
}
@@ -764,6 +772,8 @@ impl<T: Config> Module<T> {
candidate,
pending.backers,
pending.availability_votes,
pending.core,
pending.backing_group,
);
}
}
@@ -1154,6 +1164,7 @@ mod tests {
relay_parent_number: 0,
backed_in_number: 0,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(0),
});
PendingAvailabilityCommitments::insert(chain_a, default_candidate.commitments.clone());
@@ -1165,6 +1176,7 @@ mod tests {
relay_parent_number: 0,
backed_in_number: 0,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(1),
});
PendingAvailabilityCommitments::insert(chain_b, default_candidate.commitments);
@@ -1330,6 +1342,7 @@ mod tests {
relay_parent_number: 0,
backed_in_number: 0,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(0),
});
PendingAvailabilityCommitments::insert(chain_a, default_candidate.commitments);
@@ -1366,6 +1379,7 @@ mod tests {
relay_parent_number: 0,
backed_in_number: 0,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(0),
});
*bare_bitfield.0.get_mut(0).unwrap() = true;
@@ -1439,6 +1453,7 @@ mod tests {
relay_parent_number: 0,
backed_in_number: 0,
backers: backing_bitfield(&[3, 4]),
backing_group: GroupIndex::from(0),
});
PendingAvailabilityCommitments::insert(chain_a, candidate_a.commitments);
@@ -1456,6 +1471,7 @@ mod tests {
relay_parent_number: 0,
backed_in_number: 0,
backers: backing_bitfield(&[0, 2]),
backing_group: GroupIndex::from(1),
});
PendingAvailabilityCommitments::insert(chain_b, candidate_b.commitments);
@@ -1893,6 +1909,7 @@ mod tests {
relay_parent_number: 3,
backed_in_number: 4,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(0),
});
<PendingAvailabilityCommitments>::insert(&chain_a, candidate.commitments);
@@ -2187,6 +2204,7 @@ mod tests {
relay_parent_number: System::block_number() - 1,
backed_in_number: System::block_number(),
backers: backing_bitfield(&[0, 1]),
backing_group: GroupIndex::from(0),
})
);
assert_eq!(
@@ -2204,6 +2222,7 @@ mod tests {
relay_parent_number: System::block_number() - 1,
backed_in_number: System::block_number(),
backers: backing_bitfield(&[2, 3]),
backing_group: GroupIndex::from(1),
})
);
assert_eq!(
@@ -2221,6 +2240,7 @@ mod tests {
relay_parent_number: System::block_number() - 1,
backed_in_number: System::block_number(),
backers: backing_bitfield(&[4]),
backing_group: GroupIndex::from(2),
})
);
assert_eq!(
@@ -2318,6 +2338,7 @@ mod tests {
relay_parent_number: System::block_number() - 1,
backed_in_number: System::block_number(),
backers: backing_bitfield(&[0, 1, 2]),
backing_group: GroupIndex::from(0),
})
);
assert_eq!(
@@ -2394,6 +2415,7 @@ mod tests {
relay_parent_number: 5,
backed_in_number: 6,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(0),
});
<PendingAvailabilityCommitments>::insert(&chain_a, candidate.commitments.clone());
@@ -2405,6 +2427,7 @@ mod tests {
relay_parent_number: 6,
backed_in_number: 7,
backers: default_backing_bitfield(),
backing_group: GroupIndex::from(1),
});
<PendingAvailabilityCommitments>::insert(&chain_b, candidate.commitments);
@@ -271,9 +271,12 @@ where
<frame_system::Module<T>>::events().into_iter()
.filter_map(|record| extract_event(record.event))
.map(|event| match event {
RawEvent::<T>::CandidateBacked(c, h) => CandidateEvent::CandidateBacked(c, h),
RawEvent::<T>::CandidateIncluded(c, h) => CandidateEvent::CandidateIncluded(c, h),
RawEvent::<T>::CandidateTimedOut(c, h) => CandidateEvent::CandidateTimedOut(c, h),
RawEvent::<T>::CandidateBacked(c, h, core, group)
=> CandidateEvent::CandidateBacked(c, h, core, group),
RawEvent::<T>::CandidateIncluded(c, h, core, group)
=> CandidateEvent::CandidateIncluded(c, h, core, group),
RawEvent::<T>::CandidateTimedOut(c, h, core)
=> CandidateEvent::CandidateTimedOut(c, h, core),
})
.collect()
}