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

Commit

Permalink
special-case genesis when caching sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed Feb 9, 2021
1 parent e702f9f commit f013355
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ async fn cache_session_info_for_head(
) -> SubsystemResult<Result<(), SessionsUnavailable>> {
let session_index = {
let (s_tx, s_rx) = oneshot::channel();

// The genesis is guaranteed to be at the beginning of the session and its parent state
// is non-existent. Therefore if we're at the genesis, we request using its state and
// not the parent.
ctx.send_message(RuntimeApiMessage::Request(
block_header.parent_hash,
if block_header.number == 0 { block_hash } else { block_header.parent_hash },
RuntimeApiRequest::SessionIndexForChild(s_tx),
).into()).await;

Expand Down Expand Up @@ -512,6 +516,7 @@ pub(crate) async fn handle_new_head(
finalized_number: &Option<BlockNumber>,
) -> SubsystemResult<Vec<BlockImportedCandidates>> {
// Update session info based on most recent head.

let header = {
let (h_tx, h_rx) = oneshot::channel();
ctx.send_message(ChainApiMessage::BlockHeader(head, h_tx).into()).await;
Expand Down Expand Up @@ -1676,4 +1681,69 @@ mod tests {

futures::executor::block_on(futures::future::select(test_fut, aux_fut));
}

#[test]
fn request_session_info_for_genesis() {
let session: SessionIndex = 0;

let header = Header {
digest: Digest::default(),
extrinsics_root: Default::default(),
number: 0,
state_root: Default::default(),
parent_hash: Default::default(),
};

let pool = TaskExecutor::new();
let (mut ctx, mut handle) = make_subsystem_context::<(), _>(pool.clone());

let mut window = RollingSessionWindow::default();
let hash = header.hash();

let test_fut = {
let header = header.clone();
Box::pin(async move {
cache_session_info_for_head(
&mut ctx,
&mut window,
hash,
&header,
).await.unwrap().unwrap();

assert_eq!(window.earliest_session, Some(session));
assert_eq!(
window.session_info,
vec![dummy_session_info(session)],
);
})
};

let aux_fut = Box::pin(async move {
assert_matches!(
handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
h,
RuntimeApiRequest::SessionIndexForChild(s_tx),
)) => {
assert_eq!(h, hash);
let _ = s_tx.send(Ok(session));
}
);

assert_matches!(
handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
h,
RuntimeApiRequest::SessionInfo(s, s_tx),
)) => {
assert_eq!(h, hash);
assert_eq!(s, session);

let _ = s_tx.send(Ok(Some(dummy_session_info(s))));
}
);
});

futures::executor::block_on(futures::future::select(test_fut, aux_fut));
}
}

0 comments on commit f013355

Please sign in to comment.