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: try to fix relay protocol hardfork issue #203

Merged
merged 3 commits into from
Nov 13, 2024
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ckb-light-client"
version = "0.4.0"
version = "0.4.1"
authors = ["Nervos Core Dev <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
42 changes: 31 additions & 11 deletions src/protocols/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ckb_network::{
async_trait, bytes::Bytes, extract_peer_id, CKBProtocolContext, CKBProtocolHandler, PeerId,
PeerIndex,
};
use ckb_types::core::{Cycle, TransactionView};
use ckb_types::core::{Cycle, EpochNumberWithFraction, TransactionView};
use ckb_types::{packed, prelude::*};
use linked_hash_map::LinkedHashMap;
use log::{debug, trace, warn};
Expand Down Expand Up @@ -116,17 +116,33 @@ impl CKBProtocolHandler for RelayProtocol {
peer: PeerIndex,
version: &str,
) {
let epoch = self
let prove_state_epoch = self
.connected_peers
.get_state(&peer)
.map(|peer_state| {
.and_then(|peer_state| {
peer_state
.get_prove_state()
.map(|s| s.get_last_header().header().epoch())
.unwrap_or_else(|| self.storage.get_last_state().1.raw().epoch().unpack())
.number()
})
.unwrap_or_default();
});

let epoch = match prove_state_epoch {
Some(proved) => {
let stored: EpochNumberWithFraction =
self.storage.get_last_state().1.raw().epoch().unpack();
if stored > proved {
trace!("RelayProtocol.connected peer={} got a stale epoch, ignore and close the protocol", peer);
close_protocol(nc.as_ref(), peer);
return;
} else {
proved.number()
}
}
None => {
trace!("RelayProtocol.connected peer={} failed to get epoch, ignore and close the protocol", peer);
close_protocol(nc.as_ref(), peer);
return;
}
};

let ckb2023 = self
.consensus
Expand Down Expand Up @@ -303,10 +319,7 @@ impl CKBProtocolHandler for RelayProtocol {
"RelayProtocol.notify peer={} is inactive, close the protocol",
peer
);
let _ = nc
.p2p_control()
.expect("p2p_control should be exist")
.close_protocol(peer, nc.protocol_id());
close_protocol(nc.as_ref(), peer);
}
}
}
Expand All @@ -318,3 +331,10 @@ impl CKBProtocolHandler for RelayProtocol {
}
}
}

fn close_protocol(nc: &dyn CKBProtocolContext, peer: PeerIndex) {
let _ = nc
.p2p_control()
.expect("p2p_control should be exist")
.close_protocol(peer, nc.protocol_id());
}
Loading