Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should mark fetching status as timeout when disconnect #159

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/protocols/light_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::collections::HashMap;
use std::sync::Arc;

use ckb_chain_spec::consensus::Consensus;
use ckb_constant::sync::INIT_BLOCKS_IN_TRANSIT_PER_PEER;
use ckb_constant::{
hardfork::{mainnet, testnet},
sync::INIT_BLOCKS_IN_TRANSIT_PER_PEER,
};
use ckb_network::{
async_trait, bytes::Bytes, CKBProtocolContext, CKBProtocolHandler, PeerIndex, SupportProtocols,
};
Expand Down Expand Up @@ -409,11 +412,11 @@ impl LightClientProtocol {

impl LightClientProtocol {
pub(crate) fn new(storage: Storage, peers: Arc<Peers>, consensus: Consensus) -> Self {
// TODO remove this hard code when mmr is activated on testnet
let mmr_activated_epoch = if consensus.is_public_chain() {
EpochNumber::MAX
} else {
0
// Ref: https://github.com/nervosnetwork/rfcs/blob/01f3bc64ef8f54c94c7b0dcf9d30c84b6c8418b0/rfcs/0044-ckb-light-client/0044-ckb-light-client.md#deployment
let mmr_activated_epoch = match consensus.id.as_str() {
mainnet::CHAIN_SPEC_NAME => 8648,
testnet::CHAIN_SPEC_NAME => 5676,
_ => 0,
};
Self {
storage,
Expand Down
2 changes: 2 additions & 0 deletions src/protocols/light_client/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,8 @@ impl Peers {
}

pub(crate) fn remove_peer(&self, index: PeerIndex) {
self.mark_fetching_headers_timeout(index);
self.mark_fetching_txs_timeout(index);
self.inner.remove(&index);
}

Expand Down
18 changes: 17 additions & 1 deletion src/tests/protocols/light_client/send_transactions_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use ckb_types::{
};

use crate::{
protocols::{light_client::constant::FETCH_HEADER_TX_TOKEN, FetchInfo, StatusCode},
protocols::{
light_client::constant::{FETCH_HEADER_TX_TOKEN, REFRESH_PEERS_TOKEN},
FetchInfo, StatusCode,
},
tests::{
prelude::*,
utils::{MockChain, MockNetworkContext},
Expand Down Expand Up @@ -487,6 +490,10 @@ async fn test_send_headers_txs_request() {
};

let mut protocol = chain.create_light_client_protocol(Arc::clone(&peers));

assert_eq!(peers.get_headers_to_fetch().len(), 1);
assert_eq!(peers.get_txs_to_fetch().len(), 1);

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

assert!(nc.not_banned(peer_index));
Expand All @@ -511,4 +518,13 @@ async fn test_send_headers_txs_request() {
let peer = peers.get_peer(&peer_index).unwrap();
assert!(peer.get_blocks_proof_request().is_some());
assert!(peer.get_txs_proof_request().is_some());

assert_eq!(peers.get_headers_to_fetch().len(), 0);
assert_eq!(peers.get_txs_to_fetch().len(), 0);

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

assert_eq!(peers.get_txs_to_fetch().len(), 1);
assert_eq!(peers.get_headers_to_fetch().len(), 1);
}
Loading