Skip to content

Commit

Permalink
Optimize code based on responses
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 committed May 25, 2024
1 parent c216516 commit cdc1c2d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
4 changes: 0 additions & 4 deletions network/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
rand = "0.7"
ipnetwork = "0.18"

[dependencies.ckb-systemtime]
path = "../../util/systemtime"

[dependencies.ckb-network]
path = ".."
features = ["fuzz"]
Expand Down
26 changes: 4 additions & 22 deletions network/fuzz/fuzz_targets/fuzz_peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ckb_network::{
multiaddr::MultiAddr, peer_store::types::BannedAddr, peer_store::PeerStore, Flags, PeerId,
};
use ckb_network_fuzz::BufManager;
use rand::{thread_rng, RngCore};

fn new_multi_addr(data: &mut BufManager) -> (MultiAddr, Flags) {
let flags = data.get();
Expand All @@ -30,8 +29,6 @@ fn new_multi_addr(data: &mut BufManager) -> (MultiAddr, Flags) {
}

fn add_ban_addr(data: &mut BufManager, peer_store: &mut PeerStore) {
let now_ms = ckb_systemtime::unix_time_as_millis();

let num = data.get::<u8>() as usize;
for _ in 0..num {
let flags = data.get::<u8>();
Expand All @@ -44,8 +41,8 @@ fn add_ban_addr(data: &mut BufManager, peer_store: &mut PeerStore) {

let ban_addr = BannedAddr {
address: network,
ban_until: now_ms + (data.get::<u32>() as u64),
created_at: now_ms,
ban_until: data.get(),
created_at: data.get(),
ban_reason: String::new(),
};
peer_store.mut_ban_list().ban(ban_addr);
Expand All @@ -64,15 +61,7 @@ fn add_basic_addr(data: &mut BufManager, peer_store: &mut PeerStore) {

let basic_num = data.get::<u32>();

let last_connected_time_range = if flags >> 2 & 0b1 == 1 {
60_000 // 1 minute
} else {
1000 * 60 * 60 * 24 * 365
};

let num = basic_num % 16 + (16384) - 8; // ±8
let mut rng = thread_rng();
let now_ms = ckb_systemtime::unix_time_as_millis();

for i in 0..num {
let addr = format!(
Expand All @@ -82,20 +71,13 @@ fn add_basic_addr(data: &mut BufManager, peer_store: &mut PeerStore) {
)
.parse()
.unwrap();
let _ = peer_store.add_addr_fuzz(
addr,
Flags::all(),
now_ms - ((rng.next_u32() as u64) % last_connected_time_range),
rng.next_u32(),
);
let _ = peer_store.add_addr_fuzz(addr, Flags::all(), data.get(), data.get());
}
}

fuzz_target!(|data: &[u8]| {
let mut data = BufManager::new(data);

let now_ms = ckb_systemtime::unix_time_as_millis();

let mut peer_store: PeerStore = Default::default();

// basic addr:
Expand All @@ -106,7 +88,7 @@ fuzz_target!(|data: &[u8]| {

while !data.is_end() {
let (addr, flag) = new_multi_addr(&mut data);
let last_connected_time = now_ms + data.get::<u16>() as u64;
let last_connected_time = data.get();
let attempts_count = data.get::<u32>();
let _res = peer_store.add_addr_fuzz(addr, flag, last_connected_time, attempts_count);
// _res.expect("msg");
Expand Down
2 changes: 1 addition & 1 deletion network/fuzz/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2024-05-14
nightly

0 comments on commit cdc1c2d

Please sign in to comment.