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: resolve relay v3 bug #189

Merged
merged 3 commits into from
Feb 8, 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
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
Loading