Skip to content

Commit

Permalink
Merge pull request #189 from quake/quake/fix-relay-v3-bug
Browse files Browse the repository at this point in the history
fix: resolve relay v3 bug
  • Loading branch information
quake authored Feb 8, 2024
2 parents e5eb012 + 49107e2 commit 086581d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
53 changes: 51 additions & 2 deletions src/protocols/relayer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ckb_chain_spec::consensus::Consensus;
use ckb_network::{
async_trait, bytes::Bytes, extract_peer_id, CKBProtocolContext, CKBProtocolHandler, PeerId,
PeerIndex,
Expand All @@ -11,6 +12,7 @@ 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 @@ -20,6 +22,9 @@ pub(crate) struct RelayProtocol {
opened_peers: HashMap<PeerIndex, Option<Instant>>,
// Pending transactions which are waiting for relay
pending_txs: Arc<RwLock<PendingTxs>>,
consensus: Consensus,
storage: Storage,
v3: bool,
}

// a simple struct to store the pending transactions in memory with size limit
Expand Down Expand Up @@ -79,11 +84,20 @@ impl PendingTxs {
}

impl RelayProtocol {
pub fn new(pending_txs: Arc<RwLock<PendingTxs>>, connected_peers: Arc<Peers>) -> Self {
pub fn new(
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 All @@ -102,7 +116,42 @@ impl CKBProtocolHandler for RelayProtocol {
peer: PeerIndex,
version: &str,
) {
debug!("RelayProtocol({}).connected peer={}", version, peer);
let epoch = self
.connected_peers
.get_state(&peer)
.map(|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 ckb2023 = self
.consensus
.hardfork_switch
.ckb2023
.is_vm_version_2_and_syscalls_3_enabled(epoch);

debug!(
"RelayProtocol V{}({}).connected peer={}, epoch={}",
if self.v3 { '3' } else { '2' },
version,
peer,
epoch
);

if self.v3 && !ckb2023 {
debug!("peer={} is not ckb2023 enabled, ignore", peer);
return;
}

if !self.v3 && ckb2023 {
debug!("peer={} is ckb2023 enabled, ignore", peer);
return;
}

if self
.pending_txs
.read()
Expand Down
16 changes: 14 additions & 2 deletions src/subcmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,20 @@ impl RunConfig {
storage.get_last_check_point(),
));
let sync_protocol = SyncProtocol::new(storage.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 relay_protocol_v2 = RelayProtocol::new(
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(
storage.clone(),
Arc::clone(&peers),
Expand Down

0 comments on commit 086581d

Please sign in to comment.