Skip to main content

jam_primitives/
lib.rs

1pub mod block;
2pub mod crypto;
3pub mod header;
4pub mod transaction;
5pub mod types;
6pub mod utils;
7
8// Re-export commonly used types and traits
9pub use types::*;
10pub use utils::codec::{Decode, Encode};
11
12/// Common result type for primitive operations
13pub type PrimitiveResult<T> = Result<T, PrimitiveError>;
14
15/// Error types for primitive operations
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum PrimitiveError {
18    /// Invalid data format
19    InvalidFormat,
20    /// Validation failed
21    ValidationFailed(String),
22    /// Encoding/decoding error
23    CodecError(String),
24    /// Cryptographic operation failed
25    CryptoError(String),
26    /// Invalid block structure or content
27    InvalidBlock(String),
28    /// Size limit exceeded
29    SizeLimit(String),
30    /// Invalid transaction
31    InvalidTransaction(String),
32    /// Invalid header
33    InvalidHeader(String),
34}
35
36impl std::fmt::Display for PrimitiveError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            PrimitiveError::InvalidFormat => write!(f, "Invalid data format"),
40            PrimitiveError::ValidationFailed(msg) => write!(f, "Validation failed: {}", msg),
41            PrimitiveError::CodecError(msg) => write!(f, "Codec error: {}", msg),
42            PrimitiveError::CryptoError(msg) => write!(f, "Crypto error: {}", msg),
43            PrimitiveError::InvalidBlock(msg) => write!(f, "Invalid block: {}", msg),
44            PrimitiveError::SizeLimit(msg) => write!(f, "Size limit exceeded: {}", msg),
45            PrimitiveError::InvalidTransaction(msg) => write!(f, "Invalid transaction: {}", msg),
46            PrimitiveError::InvalidHeader(msg) => write!(f, "Invalid header: {}", msg),
47        }
48    }
49}
50
51impl std::error::Error for PrimitiveError {}
52
53/// Trait for types that can be hashed
54pub trait Hashable {
55    fn hash(&self) -> Hash;
56}
57
58/// Trait for types that can be validated
59pub trait Validate {
60    fn validate(&self) -> PrimitiveResult<()>;
61}
62
63/// Constants for the JAM protocol
64pub mod constants {
65    /// Maximum block size in bytes
66    pub const MAX_BLOCK_SIZE: u32 = 4 * 1024 * 1024; // 4MB
67
68    /// Maximum number of cores
69    pub const MAX_CORES: u32 = 1023;
70
71    /// Maximum accumulation size
72    pub const MAX_ACCUMULATION_SIZE: u32 = 1024 * 1024; // 1MB
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_primitive_error_display() {
81        let err = PrimitiveError::ValidationFailed("test error".to_string());
82        assert_eq!(format!("{}", err), "Validation failed: test error");
83    }
84}