Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ tikv-jemalloc-ctl = { version = "0.6", features = ["stats"] }
serial_test = "3.1.1"
tracing-test = { version = "0.2.5", features = ["no-env-filter"] }
insta = { version = "1.40", features = ["json"] }
filetime = "0.2"

[package.metadata.precommit]
fmt = "cargo fmt --check --quiet"
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl Config {

pub fn get_validator_set_config(&self, shard_id: u32) -> Vec<ValidatorSetConfig> {
if let Some(sets) = &self.validator_sets {
assert!(sets.len() > 0);
assert!(!sets.is_empty());
return sets.to_vec();
}

if let Some(addresses) = &self.validator_addresses {
assert!(addresses.len() > 0);
assert!(!addresses.is_empty());
return vec![ValidatorSetConfig {
effective_at: 0,
validator_public_keys: addresses.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/malachite/read_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl State {
return false;
}

if self.sync.peers.len() == 0 {
if self.sync.peers.is_empty() {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ impl informalsystems_malachitebft_core_types::Context for SnapchainValidatorCont
height: Self::Height,
round: Round,
) -> &'a Self::Validator {
assert!(validator_set.validators.len() > 0);
assert!(!validator_set.validators.is_empty());
assert!(round != Round::Nil && round.as_i64() >= 0);

let proposer_index = {
Expand Down
37 changes: 20 additions & 17 deletions src/core/validations/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use alloy_primitives::Address;
use ed25519_dalek::{Signature, VerifyingKey};
use fancy_regex::Regex;
use prost::Message;
use std::sync::LazyLock;

const MAX_DATA_BYTES: usize = 2048;
const MAX_DATA_BYTES_FOR_10K_CAST: usize = 16_384;
Expand All @@ -23,6 +24,14 @@ const EMBEDS_V1_CUTOFF: u32 = 73612800;
const TWITTER_USERNAME_REGEX: &str = "^[a-z0-9_]{0,15}$";
const FNAME_REGEX: &str = "^[a-z0-9][a-z0-9-]{0,15}$";
const GITHUB_USERNAME_REGEX: &str = "^[a-zA-Z\\d](?:[a-zA-Z\\d]|-(?!-)){0,38}$";

static FNAME_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(FNAME_REGEX).unwrap());
static TWITTER_USERNAME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(TWITTER_USERNAME_REGEX).unwrap());
static GITHUB_USERNAME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(GITHUB_USERNAME_REGEX).unwrap());
static GEO_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^geo:(-?\d{1,2}\.\d{2}),(-?\d{1,3}\.\d{2})$").unwrap());
/** Number of seconds (10 minutes) that is appropriate for clock skew */
const ALLOWED_CLOCK_SKEW_SECONDS: u64 = 10 * 60;

Expand All @@ -36,7 +45,7 @@ fn validate_bytes_as_string(
max_length: u64,
required: bool,
) -> Result<(), ValidationError> {
if required && byte_array.len() == 0 {
if required && byte_array.is_empty() {
return Err(ValidationError::MissingString);
}
if byte_array.len() as u64 > max_length {
Expand Down Expand Up @@ -95,7 +104,7 @@ pub fn validate_message(
let message_data;
if message.data_bytes.is_some() {
data_bytes = message.data_bytes.as_ref().unwrap().clone();
if data_bytes.len() == 0 {
if data_bytes.is_empty() {
return Err(ValidationError::MissingData);
}
match MessageData::decode(message.data_bytes.as_ref().unwrap().as_slice()) {
Expand Down Expand Up @@ -255,7 +264,7 @@ fn validate_signature(
return Err(ValidationError::InvalidSignatureScheme);
}

if signature.len() == 0 {
if signature.is_empty() {
return Err(ValidationError::MissingSignature);
}

Expand All @@ -279,7 +288,7 @@ pub fn validate_message_hash(
return Err(ValidationError::InvalidHashScheme);
}

if data_bytes.len() == 0 {
if data_bytes.is_empty() {
return Err(ValidationError::MissingData);
}

Expand All @@ -291,7 +300,7 @@ pub fn validate_message_hash(
}

pub fn validate_fname(input: &String) -> Result<(), ValidationError> {
if input.len() == 0 {
if input.is_empty() {
return Err(ValidationError::FnameIsMissing);
}

Expand All @@ -300,8 +309,7 @@ pub fn validate_fname(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::FnameExceedsLength(input.clone()));
}

if !Regex::new(FNAME_REGEX)
.unwrap()
if !FNAME_RE
.is_match(&input)
.map_err(|_| ValidationError::InvalidData)?
{
Expand Down Expand Up @@ -336,8 +344,7 @@ pub fn validate_ens_name(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::EnsNameExceedsLength(input.clone()));
}

if !Regex::new(FNAME_REGEX)
.unwrap()
if !FNAME_RE
.is_match(name_parts[0])
.map_err(|_| ValidationError::InvalidData)?
{
Expand Down Expand Up @@ -368,8 +375,7 @@ pub fn validate_base_name(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::EnsNameExceedsLength(input.clone()));
}

if !Regex::new(FNAME_REGEX)
.unwrap()
if !FNAME_RE
.is_match(&name_parts[0])
.map_err(|_| ValidationError::InvalidData)?
{
Expand All @@ -387,8 +393,7 @@ pub fn validate_twitter_username(input: &String) -> Result<(), ValidationError>
return Err(ValidationError::UsernameExceedsLength(input.clone(), 15));
}

if !Regex::new(TWITTER_USERNAME_REGEX)
.unwrap()
if !TWITTER_USERNAME_RE
.is_match(&input)
.map_err(|_| ValidationError::InvalidData)?
{
Expand All @@ -406,8 +411,7 @@ pub fn validate_github_username(input: &String) -> Result<(), ValidationError> {
return Err(ValidationError::UsernameExceedsLength(input.clone(), 38));
}

if !Regex::new(GITHUB_USERNAME_REGEX)
.unwrap()
if !GITHUB_USERNAME_RE
.is_match(&input)
.map_err(|_| ValidationError::InvalidData)?
{
Expand Down Expand Up @@ -535,8 +539,7 @@ pub fn validate_user_location(location: &str) -> Result<(), ValidationError> {
return Ok(());
}

let captures = Regex::new(r"^geo:(-?\d{1,2}\.\d{2}),(-?\d{1,3}\.\d{2})$")
.unwrap()
let captures = GEO_RE
.captures(location)
.map_err(|_| ValidationError::InvalidLocationString)?;

Expand Down
8 changes: 4 additions & 4 deletions src/core/validations/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn validate_fname_transfer(
}

pub fn validate_eth_address(address: &Vec<u8>) -> Result<&Vec<u8>, ValidationError> {
if address.len() == 0 {
if address.is_empty() {
return Err(ValidationError::EthAddressMissing);
}

Expand All @@ -183,7 +183,7 @@ pub fn validate_eth_address(address: &Vec<u8>) -> Result<&Vec<u8>, ValidationErr
}

fn validate_eth_block_hash(block_hash: &Vec<u8>) -> Result<&Vec<u8>, ValidationError> {
if block_hash.len() == 0 {
if block_hash.is_empty() {
return Err(ValidationError::BlockHashMissing);
}

Expand All @@ -195,7 +195,7 @@ fn validate_eth_block_hash(block_hash: &Vec<u8>) -> Result<&Vec<u8>, ValidationE
}

pub fn validate_sol_address(address: &Vec<u8>) -> Result<&Vec<u8>, ValidationError> {
if address.len() == 0 {
if address.is_empty() {
return Err(ValidationError::SolAddressMissing);
}

Expand All @@ -207,7 +207,7 @@ pub fn validate_sol_address(address: &Vec<u8>) -> Result<&Vec<u8>, ValidationErr
}

fn validate_sol_block_hash(block_hash: &Vec<u8>) -> Result<&Vec<u8>, ValidationError> {
if block_hash.len() == 0 {
if block_hash.is_empty() {
return Err(ValidationError::BlockHashMissing);
}

Expand Down
66 changes: 66 additions & 0 deletions src/jobs/block_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,72 @@ use tracing::{error, info};

const THROTTLE: Duration = Duration::from_millis(100);

#[cfg(test)]
mod tests {
use super::*;
use crate::proto::FarcasterNetwork;
use crate::storage::db::RocksDB;
use crate::storage::trie::merkle_trie::MerkleTrie;
use std::sync::Arc;

fn make_block_stores(dir: &std::path::Path) -> BlockStores {
let db = Arc::new(RocksDB::new(dir.to_str().unwrap()));
db.open().unwrap();
BlockStores::new(db, MerkleTrie::new().unwrap(), FarcasterNetwork::Devnet)
}

#[test]
fn test_job_creation_with_sync_not_complete() {
let tmpdir = tempfile::TempDir::new().unwrap();
let block_stores = make_block_stores(&tmpdir.path().join("db"));
let (_tx, rx) = watch::channel(false);
let result = block_pruning_job(
"0/1 * * * * *",
Duration::from_secs(86400 * 30),
block_stores,
HashMap::new(),
rx,
);
assert!(
result.is_ok(),
"expected job creation to succeed: {:?}",
result.err()
);
}

#[test]
fn test_job_creation_with_sync_complete() {
let tmpdir = tempfile::TempDir::new().unwrap();
let block_stores = make_block_stores(&tmpdir.path().join("db"));
let (_tx, rx) = watch::channel(true);
let result = block_pruning_job(
"0/1 * * * * *",
Duration::from_secs(86400 * 30),
block_stores,
HashMap::new(),
rx,
);
assert!(
result.is_ok(),
"expected job creation to succeed: {:?}",
result.err()
);
}

#[test]
fn test_sync_gate_skips_pruning_when_not_synced() {
let (_tx, rx) = watch::channel(false);
assert!(!*rx.borrow(), "receiver should reflect false");
}

#[test]
fn test_sync_gate_allows_pruning_when_synced() {
let (tx, rx) = watch::channel(false);
tx.send(true).unwrap();
assert!(*rx.borrow(), "receiver should reflect true after send");
}
}

pub fn block_pruning_job(
schedule: &str,
block_retention: Duration,
Expand Down
41 changes: 41 additions & 0 deletions src/jobs/event_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,47 @@ use tracing::error;

const THROTTLE: Duration = Duration::from_millis(200);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_job_creation_with_empty_shard_map() {
let result = event_pruning_job("0/1 * * * * *", Duration::from_secs(86400), HashMap::new());
assert!(
result.is_ok(),
"expected job creation to succeed: {:?}",
result.err()
);
}

#[test]
fn test_cutoff_timestamp_precedes_now_by_retention() {
let retention = Duration::from_secs(3600);
let now = get_farcaster_time().unwrap();
let cutoff = now - retention.as_secs() as u64;
assert!(cutoff < now, "cutoff should be before now");
assert_eq!(
now - cutoff,
retention.as_secs() as u64,
"difference should equal retention in farcaster seconds"
);
}

#[test]
fn test_longer_retention_produces_lower_cutoff() {
let now = get_farcaster_time().unwrap();
let short = Duration::from_secs(3600);
let long = Duration::from_secs(7 * 24 * 3600);
let cutoff_short = now - short.as_secs() as u64;
let cutoff_long = now - long.as_secs() as u64;
assert!(
cutoff_long < cutoff_short,
"longer retention should prune further back in time"
);
}
}

pub fn event_pruning_job(
schedule: &str,
event_retention: Duration,
Expand Down
Loading
Loading