Skip to content

Commit

Permalink
fix: try to fix relay protocol hardfork issue
Browse files Browse the repository at this point in the history
  • Loading branch information
quake committed Nov 12, 2024
1 parent 4a036d2 commit 74aed05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/protocols/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ 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};
use std::borrow::Borrow;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

use crate::protocols::{Peers, BAD_MESSAGE_BAN_TIME};
use crate::storage::Storage;

const CHECK_PENDING_TXS_TOKEN: u64 = 0;

Expand All @@ -22,6 +24,7 @@ pub(crate) struct RelayProtocol {
// Pending transactions which are waiting for relay
pending_txs: Arc<RwLock<PendingTxs>>,
consensus: Consensus,
storage: Storage,
v3: bool,
}

Expand Down Expand Up @@ -86,13 +89,15 @@ impl RelayProtocol {
pending_txs: Arc<RwLock<PendingTxs>>,
connected_peers: Arc<Peers>,
consensus: Consensus,
storage: Storage,
v3: bool,
) -> Self {
Self {
opened_peers: HashMap::new(),
pending_txs,
connected_peers,
consensus,
storage,
v3,
}
}
Expand Down Expand Up @@ -122,13 +127,20 @@ impl CKBProtocolHandler for RelayProtocol {
});

let epoch = match prove_state_epoch {
Some(epoch) => epoch.number(),
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 => {
debug!("RelayProtocol.connected peer={} failed to get epoch, ignore and close the protocol", peer);
let _ = nc
.p2p_control()
.expect("p2p_control should be exist")
.close_protocol(peer, nc.protocol_id());
trace!("RelayProtocol.connected peer={} failed to get epoch, ignore and close the protocol", peer);
close_protocol(nc.as_ref(), peer);
return;
}
};
Expand Down Expand Up @@ -308,10 +320,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 @@ -323,3 +332,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());
}
2 changes: 2 additions & 0 deletions src/subcmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ impl RunConfig {
pending_txs.clone(),
Arc::clone(&peers),
consensus.clone(),
storage.clone(),
false,
);
let relay_protocol_v3 = RelayProtocol::new(
pending_txs.clone(),
Arc::clone(&peers),
consensus.clone(),
storage.clone(),
true,
);
let light_client: Box<dyn CKBProtocolHandler> = Box::new(LightClientProtocol::new(
Expand Down

0 comments on commit 74aed05

Please sign in to comment.