Skip to main content

kora_lib/validator/
bundle_validator.rs

1use crate::{
2    bundle::{constant::JITO_MAX_BUNDLE_SIZE, BundleError, JitoError},
3    KoraError,
4};
5
6pub struct BundleValidator {}
7
8impl BundleValidator {
9    pub fn validate_jito_bundle_size(transactions: &[String]) -> Result<(), KoraError> {
10        if transactions.is_empty() {
11            return Err(BundleError::Empty.into());
12        }
13        if transactions.len() > JITO_MAX_BUNDLE_SIZE {
14            return Err(BundleError::Jito(JitoError::BundleTooLarge(JITO_MAX_BUNDLE_SIZE)).into());
15        }
16        Ok(())
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn test_validate_jito_bundle_size_empty() {
26        let result = BundleValidator::validate_jito_bundle_size(&[]);
27        assert!(result.is_err());
28    }
29    #[test]
30    fn test_validate_jito_bundle_size_too_large() {
31        let result = BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 6]);
32        assert!(result.is_err());
33    }
34    #[test]
35    fn test_validate_jito_bundle_size_valid() {
36        let result = BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 5]);
37        assert!(result.is_ok());
38    }
39
40    #[test]
41    fn test_validate_jito_bundle_size_boundary() {
42        // Test boundary values: 1, 4, 5 should pass; 6, 7 should fail
43        assert!(BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 1]).is_ok());
44        assert!(BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 4]).is_ok());
45        assert!(BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 5]).is_ok());
46        assert!(BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 6]).is_err());
47        assert!(BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 7]).is_err());
48    }
49
50    #[test]
51    fn test_validate_jito_bundle_size_error_types() {
52        // Empty returns BundleError::Empty
53        let empty_err = BundleValidator::validate_jito_bundle_size(&[]).unwrap_err();
54        assert!(matches!(empty_err, KoraError::InvalidTransaction(_)));
55
56        // Too large returns JitoError::BundleTooLarge
57        let large_err =
58            BundleValidator::validate_jito_bundle_size(&vec!["tx".to_string(); 6]).unwrap_err();
59        assert!(matches!(large_err, KoraError::JitoError(_)));
60    }
61}