Skip to content

Commit

Permalink
Merge pull request #203 from nervosnetwork/quake/fix-relay-protocol
Browse files Browse the repository at this point in the history
fix: try to fix relay protocol hardfork issue
  • Loading branch information
quake authored Nov 13, 2024
2 parents 1b3c037 + cab3d4c commit 067c9a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
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());
}

0 comments on commit 067c9a1

Please sign in to comment.