Skip to content

Commit

Permalink
Merge branch 'main' into A0-4306
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin-Radecki authored May 17, 2024
2 parents 39aa19a + 23ae8d0 commit 886bcde
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/sync-from-snapshot-testnet-partidb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ on:
schedule:
- cron: '0 3 * * 3,6'
workflow_dispatch:
# testing, remove before merge
push:
branches:
- A0-4265

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
Expand Down
114 changes: 114 additions & 0 deletions finality-aleph/src/sync/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ where
mod tests {
use std::collections::HashSet;

use futures::FutureExt;

use super::{DatabaseIO, Error, HandleStateAction, HandleStateAction::*, Handler};
use crate::{
block::{
Expand Down Expand Up @@ -1115,6 +1117,44 @@ mod tests {
};
}

#[tokio::test]
async fn stops_handling_response_upon_block_with_incorrect_header() {
let (mut handler, _backend, mut notifier, genesis) = setup();
let branch = grow_light_branch(&mut handler, &genesis, 10, 4);
let mut response = branch_response(
branch.clone(),
BranchResponseContent {
headers: false,
blocks: true,
justifications: false,
},
);

// put invalid child of block at position 7 in the response at position 8
let mut invalid_header = MockHeader::random_child(&branch[7]);
invalid_header.invalidate();
let invalid_block = MockBlock::new(invalid_header.clone(), true);
response.insert(8, ResponseItem::Block(invalid_block));

let (new_justified, eq_proofs, maybe_err) = handler.handle_request_response(response, 7);
assert!(!new_justified);
assert!(eq_proofs.is_empty());
assert!(matches!(maybe_err, Some(Error::HeaderVerifier(_))));

// first 8 blocks should be imported
for header in branch.iter().take(8) {
match notifier.next().await {
Ok(BlockImported(h)) => {
assert_eq!(h, *header);
}
_ => panic!("should notify about imported block"),
};
}

// no other block should be imported
assert!(notifier.next().now_or_never().is_none());
}

#[tokio::test]
async fn detects_equivocated_response() {
let (mut handler, _backend, mut notifier, genesis) = setup();
Expand Down Expand Up @@ -2752,4 +2792,78 @@ mod tests {
}
assert_eq!(h2.state().unwrap().favourite_block(), branch[1]);
}

#[tokio::test]
async fn headers_above_favourite_trigger_chain_extension_request() {
let (mut h1, mut backend, _notifier, genesis) = setup();
let (mut h2, _backend, _notifier, _genesis) = setup();
let branch = import_branch(&mut backend, 10);
for header in branch {
h1.block_imported(header)
.expect("block imported should succeed");
}

let branch1 = grow_light_branch(&mut h2, &genesis, 11, 3);
let branch2 = grow_light_branch(&mut h2, &genesis, 30, 3);
let header1 = branch1.last().unwrap(); // 11
let header2 = branch2.last().unwrap(); // 30

let state1 = State::new(
MockJustification::for_header(MockHeader::genesis()),
header1.clone(),
);
let state2 = State::new(
MockJustification::for_header(MockHeader::genesis()),
header2.clone(),
);

let (action1, eq_proofs1) = h1
.handle_state(state1, 1)
.expect("handle state should succeed");
let (action2, eq_proofs2) = h1
.handle_state(state2, 2)
.expect("handle state should succeed");

assert!(eq_proofs1.is_none());
assert!(eq_proofs2.is_none());
assert!(matches!(action1, HandleStateAction::ExtendChain));
assert!(matches!(action2, HandleStateAction::ExtendChain));
}

#[tokio::test]
async fn headers_below_favourite_dont_trigger_chain_extension_request() {
let (mut h1, mut backend, _notifier, genesis) = setup();
let (mut h2, _backend, _notifier, _genesis) = setup();
let branch = import_branch(&mut backend, 10);
for header in branch {
h1.block_imported(header)
.expect("block imported should succeed");
}

let branch1 = grow_light_branch(&mut h2, &genesis, 10, 3);
let branch2 = grow_light_branch(&mut h2, &genesis, 10, 3);
let header1 = branch1.last().unwrap(); // 10
let header2 = branch2.first().unwrap(); // 1

let state1 = State::new(
MockJustification::for_header(MockHeader::genesis()),
header1.clone(),
);
let state2 = State::new(
MockJustification::for_header(MockHeader::genesis()),
header2.clone(),
);

let (action1, eq_proofs1) = h1
.handle_state(state1, 1)
.expect("handle state should succeed");
let (action2, eq_proofs2) = h1
.handle_state(state2, 2)
.expect("handle state should succeed");

assert!(eq_proofs1.is_none());
assert!(eq_proofs2.is_none());
assert!(matches!(action1, HandleStateAction::Noop));
assert!(matches!(action2, HandleStateAction::Noop));
}
}

0 comments on commit 886bcde

Please sign in to comment.