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

Commit

Permalink
wait a fixed time before getting availability chunks; clone context t…
Browse files Browse the repository at this point in the history
…o each job
  • Loading branch information
coriolinus committed Jul 7, 2020
1 parent cc6102d commit 58a5e6f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/bitfield-signing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ futures = "0.3.5"
log = "0.4.8"
polkadot-primitives = { path = "../../primitives" }
polkadot-node-subsystem = { path = "../subsystem" }
wasm-timer = "0.2.4"
38 changes: 32 additions & 6 deletions node/bitfield-signing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,28 @@

//! The bitfield signing subsystem produces `SignedAvailabilityBitfield`s once per block.
use futures::{channel::oneshot, future::{abortable, AbortHandle}, Future};
use futures::{
channel::{mpsc, oneshot},
future::{abortable, AbortHandle},
prelude::*,
Future,
};
use polkadot_node_subsystem::{
messages::{AllMessages, BitfieldSigningMessage},
OverseerSignal, SubsystemResult,
};
use polkadot_node_subsystem::{FromOverseer, SpawnedSubsystem, Subsystem, SubsystemContext};
use polkadot_primitives::Hash;
use std::{collections::HashMap, pin::Pin};
use std::{
collections::HashMap,
pin::Pin,
time::{Duration, Instant},
};

/// Delay between starting a bitfield signing job and its attempting to create a bitfield.
const JOB_DELAY: Duration = Duration::from_millis(1500);

/// JobCanceler aborts all abort handles on drop.
#[derive(Debug, Default)]
struct JobCanceler(HashMap<Hash, AbortHandle>);

Expand All @@ -38,12 +51,13 @@ impl Drop for JobCanceler {
}
}

/// Bitfield signing subsystem.
struct BitfieldSigning;

impl BitfieldSigning {
async fn run<Context>(mut ctx: Context) -> SubsystemResult<()>
where
Context: SubsystemContext<Message = BitfieldSigningMessage>,
Context: SubsystemContext<Message = BitfieldSigningMessage> + Clone,
{
let mut active_jobs = JobCanceler::default();

Expand All @@ -55,7 +69,8 @@ impl BitfieldSigning {
unreachable!("BitfieldSigningMessage is uninstantiable; qed")
}
Ok(Signal(StartWork(hash))) => {
let (future, abort_handle) = abortable(bitfield_signing_job(hash.clone()));
let (future, abort_handle) =
abortable(bitfield_signing_job(hash.clone(), ctx.clone()));
// future currently returns a Result based on whether or not it was aborted;
// let's ignore all that and return () unconditionally, to fit the interface.
let future = async move {
Expand All @@ -82,7 +97,7 @@ impl BitfieldSigning {

impl<Context> Subsystem<Context> for BitfieldSigning
where
Context: SubsystemContext<Message = BitfieldSigningMessage>,
Context: SubsystemContext<Message = BitfieldSigningMessage> + Clone,
{
fn start(self, ctx: Context) -> SpawnedSubsystem {
SpawnedSubsystem(Box::pin(async move {
Expand All @@ -93,7 +108,18 @@ where
}
}

async fn bitfield_signing_job(hash: Hash) {
async fn bitfield_signing_job<Context>(hash: Hash, ctx: Context)
where
Context: SubsystemContext<Message = BitfieldSigningMessage>,
{
// first up, figure out when we need to wait until
let delay = wasm_timer::Delay::new_at(Instant::now() + JOB_DELAY);
// next, do some prerequisite work
todo!();
// now, wait for the delay to be complete
if let Err(_) = delay.await {
return;
}
// let (tx, _) = oneshot::channel();

// ctx.send_message(AllMessages::CandidateValidation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ Upon onset of a new relay-chain head with `StartWork`, launch bitfield signing j
Localized to a specific relay-parent `r`
If not running as a validator, do nothing.

- Start by waiting a fixed period of time so the availability store has the chance to make candidates available.
- Determine our validator index `i`, the set of backed candidates pending availability in `r`, and which bit of the bitfield each corresponds to.
- Start with an empty bitfield. For each bit in the bitfield, if there is a candidate pending availability, query the [Availability Store](../utility/availability-store.md) for whether we have the availability chunk for our validator index.
- For all chunks we have, set the corresponding bit in the bitfield.
- Sign the bitfield and dispatch a `BitfieldDistribution::DistributeBitfield` message.

Note that because the bitfield signing job is launched once per block and executes immediately, the signed availability statement for block `N` is best considered as a report of the candidates available at the end of block `N-1`.

0 comments on commit 58a5e6f

Please sign in to comment.