Skip to content

Commit

Permalink
test: reproduce the bug the GetLastState wasn't sent sometimes when…
Browse files Browse the repository at this point in the history
… refresh peers
  • Loading branch information
yangby-cryptape committed Jan 8, 2024
1 parent 0a8c34a commit 7837fa2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ckb-shared = "0.111.0"
ckb-chain = "0.111.0"
ckb-tx-pool = "0.111.0"
ckb-store = "0.111.0"
ckb-systemtime = { version = "0.111.0", features = ["enable_faketime"] }
tempfile = "3.0"
rand = "0.6"
serde_json = "1.0"
Expand Down
87 changes: 85 additions & 2 deletions src/tests/protocols/light_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ckb_network::{bytes::Bytes, CKBProtocolHandler, PeerIndex, SupportProtocols};
use ckb_systemtime::{faketime, unix_time_as_millis};
use ckb_types::{
core::{BlockNumber, EpochNumberWithFraction, HeaderBuilder},
packed,
Expand All @@ -8,10 +9,15 @@ use ckb_types::{
};

use crate::{
protocols::{light_client::constant::GET_IDLE_BLOCKS_TOKEN, PeerState, BAD_MESSAGE_BAN_TIME},
protocols::{
light_client::constant::{
GET_IDLE_BLOCKS_TOKEN, REFRESH_PEERS_DURATION, REFRESH_PEERS_TOKEN,
},
PeerState, BAD_MESSAGE_BAN_TIME,
},
tests::{
prelude::*,
utils::{MockChain, MockNetworkContext},
utils::{setup, MockChain, MockNetworkContext},
},
};

Expand Down Expand Up @@ -225,3 +231,80 @@ async fn test_light_client_get_idle_matched_blocks() {
]
);
}

#[tokio::test(flavor = "multi_thread")]
async fn refresh_all_peers() {
setup();

let chain = MockChain::new_with_dummy_pow("test-light-client").start();
let nc = MockNetworkContext::new(SupportProtocols::LightClient);

let peer_index = PeerIndex::new(1);
let peers = {
let peers = chain.create_peers();
peers.add_peer(peer_index);
peers.request_last_state(peer_index).unwrap();
peers
};
let mut protocol = chain.create_light_client_protocol(peers);
let storage = chain.client_storage();

let mut num = 20;
chain.mine_to(num);

// Setup the storage.
{
let snapshot = chain.shared().snapshot();
let header = snapshot.get_header_by_number(num).expect("block stored");
let last_total_difficulty = U256::from(500u64);
storage.update_last_state(&last_total_difficulty, &header.data(), &[]);
}

num -= 5;

// A node, whose tip number is small than client, connect to the client.
{
let snapshot = chain.shared().snapshot();
let last_header = snapshot
.get_verifiable_header_by_number(num)
.expect("block stored");
let data = {
let content = packed::SendLastState::new_builder()
.last_header(last_header)
.build();
packed::LightClientMessage::new_builder()
.set(content)
.build()
.as_bytes()
};

let peer_state = protocol
.get_peer_state(&peer_index)
.expect("has peer state");
assert!(peer_state.get_last_state().is_none());
assert!(nc.sent_messages().borrow().is_empty());

protocol.received(nc.context(), peer_index, data).await;

assert!(nc.not_banned(peer_index));

let peer_state = protocol
.get_peer_state(&peer_index)
.expect("has peer state");
assert!(peer_state.get_last_state().is_some());
assert!(nc.sent_messages().borrow().is_empty());
}

// Referesh all peers.
{
let start_ts = unix_time_as_millis();
let timeout_ts = start_ts + REFRESH_PEERS_DURATION.as_millis() as u64 + 1;
let faketime_guard = faketime();
faketime_guard.set_faketime(timeout_ts);

protocol.notify(nc.context(), REFRESH_PEERS_TOKEN).await;

// TODO FIXME A request `GetLastState` should be sent.
assert!(nc.sent_messages().borrow().is_empty());
}
}

0 comments on commit 7837fa2

Please sign in to comment.