Skip to content

Commit

Permalink
Merge branch 'develop' into exec/add-log-for-network-state-public_add…
Browse files Browse the repository at this point in the history
…r-remove
  • Loading branch information
zhangsoledad authored Feb 19, 2025
2 parents 6af7fa7 + f37845b commit 97190aa
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 83 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ allow = [
"MPL-2.0",
"BSL-1.0",
"BSD-3-Clause",
"BSD-2-Clause",
"ISC",
"CC0-1.0",
"Unicode-DFS-2016",
Expand Down
49 changes: 5 additions & 44 deletions network/src/peer_store/addr_manager.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! Address manager
use crate::peer_store::types::AddrInfo;
use p2p::{
multiaddr::{Multiaddr, Protocol},
utils::multiaddr_to_socketaddr,
};
use crate::peer_store::{base_addr, types::AddrInfo};
use p2p::{multiaddr::Multiaddr, utils::multiaddr_to_socketaddr};
use rand::Rng;
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -95,19 +92,7 @@ impl AddrManager {

/// Remove an address by ip and port
pub fn remove(&mut self, addr: &Multiaddr) -> Option<AddrInfo> {
let base_addr = addr
.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_)
) {
None
} else {
Some(p)
}
})
.collect();
let base_addr = base_addr(addr);
self.addr_to_id.remove(&base_addr).and_then(|id| {
let random_id_pos = self.id_to_info.get(&id).expect("exists").random_id_pos;
// swap with last index, then remove the last index
Expand All @@ -119,39 +104,15 @@ impl AddrManager {

/// Get an address information by ip and port
pub fn get(&self, addr: &Multiaddr) -> Option<&AddrInfo> {
let base_addr = addr
.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_)
) {
None
} else {
Some(p)
}
})
.collect();
let base_addr = base_addr(addr);
self.addr_to_id
.get(&base_addr)
.and_then(|id| self.id_to_info.get(id))
}

/// Get a mutable address information by ip and port
pub fn get_mut(&mut self, addr: &Multiaddr) -> Option<&mut AddrInfo> {
let base_addr = addr
.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_)
) {
None
} else {
Some(p)
}
})
.collect();
let base_addr = base_addr(addr);
if let Some(id) = self.addr_to_id.get(&base_addr) {
self.id_to_info.get_mut(id)
} else {
Expand Down
22 changes: 21 additions & 1 deletion network/src/peer_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod types;

pub(crate) use crate::Behaviour;
pub use crate::SessionType;
use p2p::multiaddr::Multiaddr;
use p2p::multiaddr::{Multiaddr, Protocol};
pub(crate) use peer_store_impl::required_flags_filter;
pub use peer_store_impl::PeerStore;

Expand Down Expand Up @@ -86,3 +86,23 @@ impl ReportResult {
self == ReportResult::Ok
}
}

// Remove unnecessary protocols
pub(crate) fn base_addr(addr: &Multiaddr) -> Multiaddr {
addr.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws
| Protocol::Wss
| Protocol::Memory(_)
| Protocol::Tls(_)
| Protocol::Onion3(_)
) {
None
} else {
Some(p)
}
})
.collect()
}
16 changes: 2 additions & 14 deletions network/src/peer_store/peer_store_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::{
peer_store::{
addr_manager::AddrManager,
ban_list::BanList,
base_addr,
types::{ip_to_network, AddrInfo, BannedAddr, PeerInfo},
Behaviour, Multiaddr, PeerScoreConfig, ReportResult, Status, ADDR_COUNT_LIMIT,
ADDR_TIMEOUT_MS, ADDR_TRY_TIMEOUT_MS, DIAL_INTERVAL,
},
Flags, PeerId, SessionType,
};
use ipnetwork::IpNetwork;
use p2p::multiaddr::Protocol;
use rand::prelude::IteratorRandom;
use std::collections::{hash_map::Entry, HashMap};

Expand Down Expand Up @@ -111,19 +111,7 @@ impl PeerStore {
if self.ban_list.is_addr_banned(&addr) {
return;
}
let base_addr = addr
.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_)
) {
None
} else {
Some(p)
}
})
.collect();
let base_addr = base_addr(&addr);
if let Some(info) = self.addr_manager.get_mut(&base_addr) {
info.last_connected_at_ms = ckb_systemtime::unix_time_as_millis()
}
Expand Down
18 changes: 4 additions & 14 deletions network/src/peer_store/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Type used on peer store
use crate::{
peer_store::{Score, SessionType, ADDR_MAX_FAILURES, ADDR_MAX_RETRIES, ADDR_TIMEOUT_MS},
peer_store::{
base_addr, Score, SessionType, ADDR_MAX_FAILURES, ADDR_MAX_RETRIES, ADDR_TIMEOUT_MS,
},
Flags,
};
use ipnetwork::IpNetwork;
Expand Down Expand Up @@ -63,19 +65,7 @@ impl AddrInfo {
pub fn new(addr: Multiaddr, last_connected_at_ms: u64, score: Score, flags: u64) -> Self {
AddrInfo {
// only store tcp protocol
addr: addr
.iter()
.filter_map(|p| {
if matches!(
p,
Protocol::Ws | Protocol::Wss | Protocol::Memory(_) | Protocol::Tls(_)
) {
None
} else {
Some(p)
}
})
.collect(),
addr: base_addr(&addr),
score,
last_connected_at_ms,
last_tried_at_ms: 0,
Expand Down
4 changes: 2 additions & 2 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1694,8 +1694,8 @@ Response
{ "rfc": "0032", "epoch_number": "0x1526" },
{ "rfc": "0036", "epoch_number": "0x0" },
{ "rfc": "0038", "epoch_number": "0x0" },
{ "rfc": "0048", "epoch_number": null },
{ "rfc": "0049", "epoch_number": null }
{ "rfc": "0048", "epoch_number": "0x3005" },
{ "rfc": "0049", "epoch_number": "0x3005" }
],
"id": "main",
"initial_primary_epoch_reward": "0x71afd498d000",
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,8 +1359,8 @@ pub trait ChainRpc {
/// { "rfc": "0032", "epoch_number": "0x1526" },
/// { "rfc": "0036", "epoch_number": "0x0" },
/// { "rfc": "0038", "epoch_number": "0x0" },
/// { "rfc": "0048", "epoch_number": null },
/// { "rfc": "0049", "epoch_number": null }
/// { "rfc": "0048", "epoch_number": "0x3005" },
/// { "rfc": "0049", "epoch_number": "0x3005" }
/// ],
/// "id": "main",
/// "initial_primary_epoch_reward": "0x71afd498d000",
Expand Down
7 changes: 5 additions & 2 deletions util/constant/src/hardfork/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ pub const RFC0028_RFC0032_RFC0033_RFC0034_START_EPOCH: u64 = 5414;
// pub const CKB2021_START_EPOCH: u64 = 5414;
pub const CKB2021_START_EPOCH: u64 = 0;

/// hardcode ckb2023 epoch
pub const CKB2023_START_EPOCH: u64 = u64::MAX;
/// 2025-07-01 06:32:53 utc
/// | hash | number | timestamp | epoch |
/// | 0xf959f70e487bc3073374d148ef0df713e6060542b84d89a3318bf18edbacdf94 | 15,414,776 | 1739773973982 | 11,489 (1800/1800)|
/// 1739773973982 + 804 * (4 * 60 * 60 * 1000) = 1751351573982 2024-10-25 05:43 utc
pub const CKB2023_START_EPOCH: u64 = 12_293;

0 comments on commit 97190aa

Please sign in to comment.