* move things around, add filter methods * validator keys, modify availability bitfields according to disputes * simplify, keep the filter -> sanitize generic for both usecases * minor * assure tests still work, reduce changeset * integration * start entropy passing * fixins * compile, 1 failing test * filter with coverage * fixins * Update runtime/parachains/src/paras_inherent.rs Co-authored-by: Robert Habermeier <rphmeier@gmail.com> * slip of the pen * improve test cases * misc * fix * fixins * test avoid extra into() calls in assert_noop! * chores * ff * test fixup superfluous into call * chore: pfmt * improve apply_block_weight_limit to try to maximize the number of sufficiently backed blocks and add extra bitfields in a round-robin fashion * new code treats the lack of backed candidates as ok * Use vrf based entropy * fixup vrf random * add warn * slip of the pen * fixup * assure ordering * rethink apply_weights * mock * use a closure as predicate check * extract and use DisputedBitfield * chore: simplify * remove stray dbg * chore: fmt * address feedback * fix test, halfway there * stage1 * dbg stuff * make group selection align * fix session index * fix wrongly returned candidates * cleanup * chore fmt * fix ensure check * make good case test work * more tests for bitfields * create sanitize_backed_candidates * fixup tests * update guide * add check referenced in the guide * improve weights code * fmt * fixins * Update roadmap/implementers-guide/src/runtime/inclusion.md Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> * compiling + address review * add comments * fix weight calc * address review comments and test failure * fix * fix: condition * Fix random_sel function * Fix overlength block check * Zeke + Ladi commit for disputes filtering + integration test builder + runtime benchmarks + integration tests * Add benchmarks for code upgrades * Code upgrade bench; Feature gate TestWeightInfo * Try and make CI happier * Feature gate enter test to not(benchmarks) * Make sure no unused imports/fn * refactor, re-use, the beginning * Fix issue with frame benchmarking dep compilation * More precise feature gating for some derives * integrate piece-wise * foo * fixins * chore fmt * fixins * rename const generic * Update runtime/parachains/src/paras_inherent.rs Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * Fix compilation * limit to test * remove unused spam slots * spellcheck * remove a tick, fix a typo * Add Code upgrade weights * comment improvements + >= Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * remove another tick * Update runtime/parachains/src/paras_inherent/benchmarking.rs Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * saturating fixins + some spaces * fix * benchmarking - preliminary results * Add training wheels * Refactor some early exit logic for enter * Gracefully handle filtering bitfields & candidates (#4280) This updates the logic for sanitize_bitfields and sanitize_backed_candidates to never error when there is an issue, but instead to simply skip the problematic items. * Refactor inherent data weight limiting logic (#4287) * Apply suggestions from code review * Update runtime/parachains/src/builder.rs Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * Update runtime/parachains/src/builder.rs * Update runtime/parachains/src/paras_inherent.rs * final pass * Run cargo +nightly-2021-10-29 fmt * Update implementors guide with `sanitize_*` & `enter` (#4294) * Make spell check happier * Make wasm runtimes compile with benchmarks enabled (#4303) * comment stuff out, use old toml * Seems to be working? * Remove feature gating from builder * Remove commented out stuff * Remove generic from digest * Update weight files for runtime Co-authored-by: Robert Habermeier <rphmeier@gmail.com> Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com> Co-authored-by: Lldenaurois <Ljdenaurois@gmail.com> Co-authored-by: Zeke Mostov <z.mostov@gmail.com> Co-authored-by: Bastian Köcher <info@kchr.de>
8.6 KiB
ParaInherent
This module is responsible for providing all data given to the runtime by the block author to the various parachains modules. The entry-point is mandatory, in that it must be invoked exactly once within every block, and it is also "inherent", in that it is provided with no origin by the block author. The data within it carries its own authentication; i.e. the data takes the form of signed statements by validators. If any of the steps within fails, the entry-point is considered as having failed and the block will be invalid.
This module does not have the same initialization/finalization concerns as the others, as it only requires that entry points be triggered after all modules have initialized and that finalization happens after entry points are triggered. Both of these are assumptions we have already made about the runtime's order of operations, so this module doesn't need to be initialized or finalized by the Initializer.
There are a couple of important notes to the operations in this inherent as they relate to disputes.
- We don't accept bitfields or backed candidates if in "governance-only" mode from having a local dispute conclude on this fork.
- When disputes are initiated, we remove the block from pending availability. This allows us to roll back chains to the block before blocks are included as opposed to backing. It's important to do this before processing bitfields.
Inclusion::collect_disputedis kind of expensive so it's important to gate this on whether there are actually any new disputes. Which should be never.- And we don't accept parablocks that have open disputes or disputes that have concluded against the candidate. It's important to import dispute statements before backing, but this is already the case as disputes are imported before processing bitfields.
Storage
/// Whether the para inherent was included or not.
Included: Option<()>,
/// Scraped on chain votes to be used in disputes off-chain.
OnChainVotes: Option<ScrapedOnChainVotes>,
Finalization
- Take (get and clear) the value of
Included. If it is notSome, throw an unrecoverable error.
Entry Points
-
enter: This entry-point accepts one parameter:ParaInherentData.- Ensure the origin is none.
- Ensure
Includedis set asNone. - Set
IncludedasSome. - Unpack
ParachainsInherentDataintosigned_bitfields,backed_candidates,parent_header, anddisputes. - Hash the parent header and make sure that it corresponds to the block hash of the parent (tracked by the
frame_systemFRAME module). - Calculate the
candidate_weight,bitfields_weight, anddisputes_weight. - If the sum of
candidate_weight,bitfields_weight, anddisputes_weightis greater than the max block weight we do the following with the goal of prioritizing the inclusion of disputes without making it game-able by block authors: 1. clearbitfieldsand setbitfields_weightequal to 0. 1. clearbacked_candidatesand setcandidate_weightequal to 0. 1. invokelimit_disputeson thedisputeswith the max block weight iff the disputes weight is greater than the max block weight. - Invoke
Disputes::provide_multi_dispute_data. - If
Disputes::is_frozen, return. - If there are any concluded disputes from the current session, invoke
Inclusion::collect_disputedwith the disputed candidates. Annotate each returned core withFreedReason::Concluded, sort them, and invokeScheduler::free_coreswith them. - The
Bitfieldsare first forwarded to theInclusion::process_bitfieldsroutine, returning a set included candidates and the respective freed cores. Provide the number of availability cores (Scheduler::availability_cores().len()) as the expected number of bits and aScheduler::core_paraas a core-lookup to theprocess_bitfieldsroutine. Annotate each of these freed cores withFreedReason::Concluded. - For each freed candidate from the
Inclusion::process_bitfieldscall, invokeDisputes::note_included(current_session, candidate). - If
Scheduler::availability_timeout_predicateisSome, invokeInclusion::collect_pendingusing it and annotate each of those freed cores withFreedReason::TimedOut. - Combine and sort the the bitfield-freed cores and the timed-out cores.
- Invoke
Scheduler::clear - Invoke
Scheduler::schedule(freed_cores, System::current_block()) - Extract
parent_storage_rootfrom the parent header, - If
Disputes::concluded_invalid(current_session, candidate)is true for any of thebacked_candidates, fail. - Invoke the
Inclusion::process_candidatesroutine with the parameters(parent_storage_root, backed_candidates, Scheduler::scheduled(), Scheduler::group_validators). - Deconstruct the returned
ProcessedCandidatesvalue intooccupiedcore indices, and backing validators by candidatebacking_validators_per_candidaterepresented byVec<(CandidateReceipt, Vec<(ValidatorIndex, ValidityAttestation)>)>. - Set
OnChainVotestoScrapedOnChainVotes, based on thecurrent_session, concludeddisputes, andbacking_validators_per_candidate. - Call
Scheduler::occupiedusing theoccupiedcore indices of the returned above, first sorting the list of assigned core indices. - Call the
Ump::process_pending_upward_messagesroutine to execute all messages in upward dispatch queues. - If all of the above succeeds, set
IncludedtoSome(()).
-
create_inherent: This entry-point accepts one parameter:InherentData.- Invoke
create_inherent_inner(InherentData), the unit testable logic for filtering and sanitzing the inherent data used when invokingenter. Save the result asinherent_data. - If the
inherent_datais anErrvariant, return theentercall signature with all inherent data cleared else return theentercall signature withinherent_datapassed in as thedataparam.
- Invoke
Routines
create_inherent_inner(data: &InherentData) -> Option<ParachainsInherentData<T::Header>>- Unpack
InherentDatainto its parts,bitfields,backed_candidates,disputesand theparent_header. If data cannot be unpacked returnNone. - Hash the
parent_headerand make sure that it corresponds to the block hash of the parent (tracked by theframe_systemFRAME module). - Invoke
Disputes::filter_multi_dispute_datato remove duplicates et al fromdisputes. - Run the following within a
with_transactionclosure to avoid side effects (we are essentially replicating the logic that would otherwise happen withinenterso we can get the filtered bitfields and theconcluded_invalid_disputes+scheduledto use in filtering thebacked_candidates.): - Invoke
Disputes::provide_multi_dispute_data. - Collect
current_concluded_invalid_disputes, the disputed candidate hashes from the current session that have concluded invalid. - Collect
concluded_invalid_disputes, the disputed candidate hashes from the givenbacked_candidates. - Invoke
Inclusion::collect_disputedwith the newly disputed candidates. Annotate each returned core withFreedReason::Concluded, sort them, and invokeScheduler::free_coreswith them. - Collect filtered
bitfieldsby invokingsanitize_bitfields<false>. - Collect
freed_concludedby invokingupdate_pending_availability_and_get_freed_coreson the filtered bitfields. - Collect all
freedcores by invokingcollect_all_freed_coresonfreed_concluding. - Invoke
scheduler::Pallet<T>>::clear(). - Invoke
scheduler::Pallet<T>>::schedulewithfreedand the current block number to create the same schedule of the cores thatenterwill create. - Read the new
<scheduler::Pallet<T>>::scheduled()intoschedule. - From the
with_transactionclosure returnconcluded_invalid_disputes,bitfields, andscheduled. - Invoke
sanitize_backed_candidatesusing thescheduledreturn from thewith_transactionand pass the closure|candidate_hash: CandidateHash| -> bool { DisputesHandler::concluded_invalid(current_session, candidate_hash) }for the paramcandidate_has_concluded_invalid_dispute. - create a
rngfromrand_chacha::ChaChaRng::from_seed(compute_entropy::<T>(parent_hash)). - Invoke
limit_disputeswith the max block weight andrng, storing the returned weigh inremaining_weight. - Fill up the remaining of the block weight with backed candidates and bitfields by invoking
apply_weight_limitwithremaining_weighandrng. - Return
Some(ParachainsInherentData { bitfields, backed_candidates, disputes, parent_header }.
- Unpack