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

Integrate Approval Voting into Overseer / Service / GRANDPA #2412

Merged
merged 14 commits into from
Feb 16, 2021
Merged
2 changes: 2 additions & 0 deletions Cargo.lock

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

32 changes: 23 additions & 9 deletions node/core/approval-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ const LOG_TARGET: &str = "approval_voting";

/// The approval voting subsystem.
pub struct ApprovalVotingSubsystem<T> {
keystore: LocalKeystore,
keystore: Arc<LocalKeystore>,
slot_duration_millis: u64,
db: Arc<T>,
}

impl<T> ApprovalVotingSubsystem<T> {
/// Create a new approval voting subsystem with the given keystore, slot duration,
/// and underlying DB.
pub fn new(keystore: Arc<LocalKeystore>, slot_duration_millis: u64, db: Arc<T>) -> Self {
ApprovalVotingSubsystem {
keystore,
slot_duration_millis,
db,
}
}
}

impl<T, C> Subsystem<C> for ApprovalVotingSubsystem<T>
where T: AuxStore + Send + Sync + 'static, C: SubsystemContext<Message = ApprovalVotingMessage> {
fn start(self, ctx: C) -> SpawnedSubsystem {
Expand Down Expand Up @@ -229,7 +241,7 @@ use approval_db_v1_reader::ApprovalDBV1Reader;

struct State<T> {
session_window: import::RollingSessionWindow,
keystore: LocalKeystore,
keystore: Arc<LocalKeystore>,
slot_duration_millis: u64,
db: T,
clock: Box<dyn Clock + Send + Sync>,
Expand Down Expand Up @@ -529,10 +541,10 @@ async fn handle_approved_ancestor(
db: &impl DBReader,
target: Hash,
lower_bound: BlockNumber,
) -> SubsystemResult<Option<Hash>> {
) -> SubsystemResult<Option<(Hash, BlockNumber)>> {
let mut all_approved_max = None;

let block_number = {
let target_number = {
let (tx, rx) = oneshot::channel();

ctx.send_message(ChainApiMessage::BlockNumber(target, tx).into()).await;
Expand All @@ -544,17 +556,17 @@ async fn handle_approved_ancestor(
}
};

if block_number <= lower_bound { return Ok(None) }
if target_number <= lower_bound { return Ok(None) }

// request ancestors up to but not including the lower bound,
// as a vote on the lower bound is implied if we cannot find
// anything else.
let ancestry = if block_number > lower_bound + 1 {
let ancestry = if target_number > lower_bound + 1 {
let (tx, rx) = oneshot::channel();

ctx.send_message(ChainApiMessage::Ancestors {
hash: target,
k: (block_number - (lower_bound + 1)) as usize,
k: (target_number - (lower_bound + 1)) as usize,
response_channel: tx,
}.into()).await;

Expand All @@ -566,7 +578,7 @@ async fn handle_approved_ancestor(
Vec::new()
};

for block_hash in std::iter::once(target).chain(ancestry) {
for (i, block_hash) in std::iter::once(target).chain(ancestry).enumerate() {
// Block entries should be present as the assumption is that
// nothing here is finalized. If we encounter any missing block
// entries we can fail.
Expand All @@ -577,7 +589,9 @@ async fn handle_approved_ancestor(

if entry.is_fully_approved() {
if all_approved_max.is_none() {
all_approved_max = Some(block_hash);
// First iteration of the loop is target, i = 0. After that,
// ancestry is moving backwards.
all_approved_max = Some((block_hash, target_number - i as BlockNumber));
}
} else {
all_approved_max = None;
Expand Down
6 changes: 3 additions & 3 deletions node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl DBReader for TestStore {
fn blank_state() -> State<TestStore> {
State {
session_window: import::RollingSessionWindow::default(),
keystore: LocalKeystore::in_memory(),
keystore: Arc::new(LocalKeystore::in_memory()),
slot_duration_millis: SLOT_DURATION_MILLIS,
db: TestStore::default(),
clock: Box::new(MockClock::default()),
Expand Down Expand Up @@ -1490,7 +1490,7 @@ fn approved_ancestor_all_approved() {
let test_fut = Box::pin(async move {
assert_eq!(
handle_approved_ancestor(&mut ctx, &state.db, block_hash_4, 0).await.unwrap(),
Some(block_hash_4),
Some((block_hash_4, 4)),
)
});

Expand Down Expand Up @@ -1572,7 +1572,7 @@ fn approved_ancestor_missing_approval() {
let test_fut = Box::pin(async move {
assert_eq!(
handle_approved_ancestor(&mut ctx, &state.db, block_hash_4, 0).await.unwrap(),
Some(block_hash_2),
Some((block_hash_2, 2)),
)
});

Expand Down
Loading