Skip to content

Commit

Permalink
fix: resolve ckb2023 relay protocol id error
Browse files Browse the repository at this point in the history
  • Loading branch information
quake committed Jan 11, 2024
1 parent c15c174 commit df1fa6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
33 changes: 18 additions & 15 deletions src/protocols/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ impl CKBProtocolHandler for RelayProtocol {
.tx_hashes(tx_hashes.pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();

if let Err(err) = nc.send_message(
SupportProtocols::RelayV2.protocol_id(),
peer,
message.as_bytes(),
) {
let protocol_id = if nc.ckb2023() {
SupportProtocols::RelayV3.protocol_id()
} else {
SupportProtocols::RelayV2.protocol_id()
};
if let Err(err) = nc.send_message(protocol_id, peer, message.as_bytes()) {
warn!(
"RelayProtocol failed to send RelayTransactionHashes message to peer={} since {:?}",
peer, err
Expand Down Expand Up @@ -207,6 +207,13 @@ impl CKBProtocolHandler for RelayProtocol {
CHECK_PENDING_TXS_TOKEN => {
// we check pending txs every 2 seconds, if the timestamp of the pending txs is updated in the last minute
// and connected relay protocol peers is empty, we try to open the protocol and broadcast the pending txs

let protocol_id = if nc.ckb2023() {
SupportProtocols::RelayV3.protocol_id()
} else {
SupportProtocols::RelayV2.protocol_id()
};

if self
.pending_txs
.read()
Expand All @@ -216,9 +223,7 @@ impl CKBProtocolHandler for RelayProtocol {
{
let p2p_control = nc.p2p_control().expect("p2p_control should be exist");
for peer in self.connected_peers.get_peers_index() {
if let Err(err) =
p2p_control.open_protocol(peer, SupportProtocols::RelayV2.protocol_id())
{
if let Err(err) = p2p_control.open_protocol(peer, protocol_id) {
warn!(
"RelayProtocol failed to open protocol to peer={} since {:?}",
peer, err
Expand All @@ -241,11 +246,9 @@ impl CKBProtocolHandler for RelayProtocol {
let message =
packed::RelayMessage::new_builder().set(content).build();

if let Err(err) = nc.send_message(
SupportProtocols::RelayV2.protocol_id(),
peer,
message.as_bytes(),
) {
if let Err(err) =
nc.send_message(protocol_id, peer, message.as_bytes())
{
warn!(
"RelayProtocol failed to send RelayTransactionHashes message to peer={} since {:?}",
peer, err
Expand All @@ -263,7 +266,7 @@ impl CKBProtocolHandler for RelayProtocol {
let _ = nc
.p2p_control()
.expect("p2p_control should be exist")
.close_protocol(peer, SupportProtocols::RelayV2.protocol_id());
.close_protocol(peer, protocol_id);
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/subcmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ impl RunConfig {
storage.get_last_check_point(),
));
let sync_protocol = SyncProtocol::new(storage.clone(), Arc::clone(&peers));
let relay_protocol = RelayProtocol::new(pending_txs.clone(), Arc::clone(&peers));
let relay_protocol_v2 = RelayProtocol::new(pending_txs.clone(), Arc::clone(&peers));
let relay_protocol_v3 = RelayProtocol::new(pending_txs.clone(), Arc::clone(&peers));
let light_client: Box<dyn CKBProtocolHandler> = Box::new(LightClientProtocol::new(
storage.clone(),
Arc::clone(&peers),
Expand All @@ -83,7 +84,12 @@ impl RunConfig {
),
CKBProtocol::new_with_support_protocol(
SupportProtocols::RelayV2,
Box::new(relay_protocol),
Box::new(relay_protocol_v2),
Arc::clone(&network_state),
),
CKBProtocol::new_with_support_protocol(
SupportProtocols::RelayV3,
Box::new(relay_protocol_v3),
Arc::clone(&network_state),
),
CKBProtocol::new_with_support_protocol(
Expand Down

0 comments on commit df1fa6b

Please sign in to comment.