This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split spammer into multiple files (#270)
- Loading branch information
Showing
4 changed files
with
102 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#[derive(Debug)] | ||
pub struct CertificateSpammerConfig { | ||
pub target_nodes: Option<Vec<String>>, | ||
pub target_nodes_path: Option<String>, | ||
pub local_key_seed: u64, | ||
pub cert_per_batch: u64, | ||
pub nb_subnets: u8, | ||
pub nb_batches: Option<u64>, | ||
pub batch_interval: u64, | ||
pub target_subnets: Option<Vec<String>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("target nodes are not specified")] | ||
TargetNodesNotSpecified, | ||
#[error("error reading target nodes json file:{0}")] | ||
ReadingTargetNodesJsonFile(String), | ||
#[error("error parsing target nodes json file:{0}")] | ||
InvalidTargetNodesJsonFile(String), | ||
#[error("invalid subnet id error: {0}")] | ||
InvalidSubnetId(String), | ||
#[error("hex conversion error {0}")] | ||
HexConversion(hex::FromHexError), | ||
#[error("invalid signing key: {0}")] | ||
InvalidSigningKey(String), | ||
#[error("Tce node connection error {0}")] | ||
TCENodeConnection(topos_tce_proxy::Error), | ||
#[error("Certificate signing error: {0}")] | ||
CertificateSigning(topos_core::uci::Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use topos_core::uci::{Certificate, SubnetId}; | ||
|
||
use crate::{error::Error, SourceSubnet}; | ||
|
||
pub fn generate_random_32b_array() -> [u8; 32] { | ||
(0..32) | ||
.map(|_| rand::random::<u8>()) | ||
.collect::<Vec<u8>>() | ||
.try_into() | ||
.expect("Valid 32 byte array") | ||
} | ||
|
||
/// Generate test certificate | ||
pub fn generate_test_certificate( | ||
source_subnet: &mut SourceSubnet, | ||
target_subnet_ids: &[SubnetId], | ||
) -> Result<Certificate, Box<dyn std::error::Error>> { | ||
let mut new_cert = Certificate::new( | ||
source_subnet.last_certificate_id, | ||
source_subnet.source_subnet_id, | ||
generate_random_32b_array(), | ||
generate_random_32b_array(), | ||
target_subnet_ids, | ||
0, | ||
Vec::new(), | ||
)?; | ||
new_cert | ||
.update_signature(&source_subnet.signing_key) | ||
.map_err(Error::CertificateSigning)?; | ||
|
||
source_subnet.last_certificate_id = new_cert.id; | ||
Ok(new_cert) | ||
} | ||
|
||
pub fn generate_source_subnets( | ||
local_key_seed: u64, | ||
number_of_subnets: u8, | ||
) -> Result<Vec<SourceSubnet>, Error> { | ||
let mut subnets = Vec::new(); | ||
|
||
let mut signing_key = [0u8; 32]; | ||
let (_, right) = signing_key.split_at_mut(24); | ||
right.copy_from_slice(local_key_seed.to_be_bytes().as_slice()); | ||
for _ in 0..number_of_subnets { | ||
signing_key = tiny_keccak::keccak256(&signing_key); | ||
|
||
// Subnet id of the source subnet which will be used for every generated certificate | ||
let source_subnet_id: SubnetId = topos_crypto::keys::derive_public_key(&signing_key) | ||
.map_err(|e| Error::InvalidSigningKey(e.to_string()))? | ||
.as_slice()[1..33] | ||
.try_into() | ||
.map_err(|_| Error::InvalidSubnetId("Unable to parse subnet id".to_string()))?; | ||
|
||
subnets.push(SourceSubnet { | ||
signing_key, | ||
source_subnet_id, | ||
last_certificate_id: Default::default(), | ||
}); | ||
} | ||
|
||
Ok(subnets) | ||
} |