Skip to content

Commit

Permalink
test: reproduce the bug that peers will be banned when do reorg after…
Browse files Browse the repository at this point in the history
… client restart
  • Loading branch information
yangby-cryptape committed Dec 20, 2023
1 parent e0a060a commit bece275
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/protocols/light_client/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,13 @@ impl Peers {
self.inner.get(index).map(|peer| peer.clone())
}

#[cfg(test)]
pub(crate) fn mock_initialized(&self, index: PeerIndex) {
if let Some(mut peer) = self.inner.get_mut(&index) {
_ = peer.state.take();
}
}

#[cfg(test)]
pub(crate) fn mock_prove_request(
&self,
Expand Down
20 changes: 20 additions & 0 deletions src/tests/protocols/light_client/send_last_state_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,19 @@ async fn reorg_rollback_5_blocks() {
test_with_reorg_blocks(param).await;
}

#[tokio::test(flavor = "multi_thread")]
async fn reorg_rollback_after_restart_and_last_n_blocks_is_not_enough() {
let param = ReorgTestParameter {
last_number: 30,
rollback_blocks_count: 3,
last_n_blocks: 20,
result: StatusCode::InvalidReorgHeaders,
restart: true,
..Default::default()
};
test_with_reorg_blocks(param).await;
}

#[tokio::test(flavor = "multi_thread")]
async fn reorg_detect_long_fork_turn_1() {
let param = ReorgTestParameter {
Expand Down Expand Up @@ -1682,6 +1695,8 @@ struct ReorgTestParameter {
long_fork_detected: bool,
expected_last_headers_count_opt: Option<BlockNumber>,
result: StatusCode,
// Mock "restart" state: after restart, the first received "last state" is on a forked chain.
restart: bool,
}

async fn test_with_reorg_blocks(param: ReorgTestParameter) {
Expand Down Expand Up @@ -1834,6 +1849,11 @@ async fn test_with_reorg_blocks(param: ReorgTestParameter) {
assert_eq!(chain.shared().snapshot().tip_number(), last_number);
}

if param.restart {
protocol.peers().mock_initialized(peer_index);
protocol.peers().request_last_state(peer_index).unwrap();
}

// Run the test.
{
let mut prove_request = chain.build_prove_request(
Expand Down

0 comments on commit bece275

Please sign in to comment.