-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2024 MaidSafe.net limited. | ||
// | ||
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3. | ||
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed | ||
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. Please review the Licences for the specific language governing | ||
// permissions and limitations relating to use of the SAFE Network Software. | ||
|
||
use crate::Result; | ||
|
||
use libp2p::PeerId; | ||
|
||
// Threshold to determine if there is an attack using Kullback-Liebler (KL) divergence | ||
// between model peer ids distribution vs. actual distribution around any point in the address space. | ||
const KL_DIVERGENCE_THRESHOLD: usize = 10; | ||
|
||
pub(super) async fn check_for_sybil_attack(peers: &[PeerId]) -> Result<bool> { | ||
let q = num_peers_per_cpl(peers)? / 20; | ||
let n = get_net_size_estimate()?; | ||
let p = compute_model_distribution(n)?; | ||
let kl_divergence = compute_kl_divergence(p, q)?; | ||
|
||
let is_attack = kl_divergence > KL_DIVERGENCE_THRESHOLD; | ||
Ok(is_attack) | ||
} | ||
|
||
fn num_peers_per_cpl(peers: &[PeerId]) -> Result<usize> { | ||
// TODO! | ||
Check notice Code scanning / devskim A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note
Suspicious comment
|
||
Ok(0usize) | ||
} | ||
|
||
fn get_net_size_estimate() -> Result<usize> { | ||
Ok(0usize) | ||
// TODO! | ||
Check notice Code scanning / devskim A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note
Suspicious comment
|
||
} | ||
|
||
fn compute_model_distribution(net_size: usize) -> Result<usize> { | ||
// TODO! | ||
Check notice Code scanning / devskim A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note
Suspicious comment
|
||
let model_dist = net_size; | ||
Ok(model_dist) | ||
} | ||
|
||
fn compute_kl_divergence(model_dist: usize, peers_per_cpl: usize) -> Result<usize> { | ||
// TODO! | ||
Check notice Code scanning / devskim A "TODO" or similar was left in source code, possibly indicating incomplete functionality Note
Suspicious comment
|
||
let kl = model_dist * peers_per_cpl; | ||
Ok(kl) | ||
} |