From 4a036d26c97acd3e984ffadf34ac1f4a5eee940c Mon Sep 17 00:00:00 2001 From: quake Date: Mon, 11 Nov 2024 10:54:34 +0900 Subject: [PATCH 1/3] fix: try to fix relay protocol hardfork issue --- src/protocols/relayer.rs | 25 +++++++++++++++---------- src/subcmds.rs | 2 -- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/protocols/relayer.rs b/src/protocols/relayer.rs index a93fa7b..088f5f1 100644 --- a/src/protocols/relayer.rs +++ b/src/protocols/relayer.rs @@ -12,7 +12,6 @@ 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; @@ -23,7 +22,6 @@ pub(crate) struct RelayProtocol { // Pending transactions which are waiting for relay pending_txs: Arc>, consensus: Consensus, - storage: Storage, v3: bool, } @@ -88,7 +86,6 @@ impl RelayProtocol { pending_txs: Arc>, connected_peers: Arc, consensus: Consensus, - storage: Storage, v3: bool, ) -> Self { Self { @@ -96,7 +93,6 @@ impl RelayProtocol { pending_txs, connected_peers, consensus, - storage, v3, } } @@ -116,17 +112,26 @@ 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(epoch) => epoch.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()); + return; + } + }; let ckb2023 = self .consensus diff --git a/src/subcmds.rs b/src/subcmds.rs index f30534e..b3b822d 100644 --- a/src/subcmds.rs +++ b/src/subcmds.rs @@ -72,14 +72,12 @@ 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 = Box::new(LightClientProtocol::new( From 74aed05bd5ee65c4913adc6ec5f46b6546f58aab Mon Sep 17 00:00:00 2001 From: quake Date: Tue, 12 Nov 2024 16:06:38 +0900 Subject: [PATCH 2/3] fix: try to fix relay protocol hardfork issue --- src/protocols/relayer.rs | 38 +++++++++++++++++++++++++++----------- src/subcmds.rs | 2 ++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/protocols/relayer.rs b/src/protocols/relayer.rs index 088f5f1..05305fa 100644 --- a/src/protocols/relayer.rs +++ b/src/protocols/relayer.rs @@ -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; @@ -22,6 +24,7 @@ pub(crate) struct RelayProtocol { // Pending transactions which are waiting for relay pending_txs: Arc>, consensus: Consensus, + storage: Storage, v3: bool, } @@ -86,6 +89,7 @@ impl RelayProtocol { pending_txs: Arc>, connected_peers: Arc, consensus: Consensus, + storage: Storage, v3: bool, ) -> Self { Self { @@ -93,6 +97,7 @@ impl RelayProtocol { pending_txs, connected_peers, consensus, + storage, v3, } } @@ -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; } }; @@ -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); } } } @@ -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()); +} diff --git a/src/subcmds.rs b/src/subcmds.rs index b3b822d..f30534e 100644 --- a/src/subcmds.rs +++ b/src/subcmds.rs @@ -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 = Box::new(LightClientProtocol::new( From cab3d4cfb80ba0293e957b204c0c9fae66492757 Mon Sep 17 00:00:00 2001 From: quake Date: Wed, 13 Nov 2024 10:08:51 +0900 Subject: [PATCH 3/3] chore: code cleanup and version up --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/protocols/relayer.rs | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c6b0a5..71ec360 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -676,7 +676,7 @@ dependencies = [ [[package]] name = "ckb-light-client" -version = "0.4.0" +version = "0.4.1" dependencies = [ "anyhow", "ckb-app-config", diff --git a/Cargo.toml b/Cargo.toml index abaeff7..fec9435 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ckb-light-client" -version = "0.4.0" +version = "0.4.1" authors = ["Nervos Core Dev "] edition = "2021" license = "MIT" diff --git a/src/protocols/relayer.rs b/src/protocols/relayer.rs index 05305fa..6b2fb1c 100644 --- a/src/protocols/relayer.rs +++ b/src/protocols/relayer.rs @@ -7,7 +7,6 @@ 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};