|
| 1 | +use crate::types::{PayloadAttributesError, RollkitPayloadAttributes}; |
| 2 | +use alloy_primitives::{Address, B256}; |
| 3 | + |
| 4 | +#[cfg(test)] |
| 5 | +mod tests { |
| 6 | + use super::*; |
| 7 | + |
| 8 | + /// Test payload attributes creation and basic field assignment |
| 9 | + #[test] |
| 10 | + fn test_payload_attributes_creation() { |
| 11 | + let transactions = vec![]; |
| 12 | + let gas_limit = Some(1000000u64); |
| 13 | + let timestamp = 1234567890u64; |
| 14 | + let prev_randao = B256::random(); |
| 15 | + let suggested_fee_recipient = Address::random(); |
| 16 | + let parent_hash = B256::random(); |
| 17 | + let block_number = 1u64; |
| 18 | + |
| 19 | + let attrs = RollkitPayloadAttributes::new( |
| 20 | + transactions.clone(), |
| 21 | + gas_limit, |
| 22 | + timestamp, |
| 23 | + prev_randao, |
| 24 | + suggested_fee_recipient, |
| 25 | + parent_hash, |
| 26 | + block_number, |
| 27 | + ); |
| 28 | + |
| 29 | + assert_eq!(attrs.transactions, transactions); |
| 30 | + assert_eq!(attrs.gas_limit, gas_limit); |
| 31 | + assert_eq!(attrs.timestamp, timestamp); |
| 32 | + assert_eq!(attrs.prev_randao, prev_randao); |
| 33 | + assert_eq!(attrs.suggested_fee_recipient, suggested_fee_recipient); |
| 34 | + assert_eq!(attrs.parent_hash, parent_hash); |
| 35 | + assert_eq!(attrs.block_number, block_number); |
| 36 | + } |
| 37 | + |
| 38 | + /// Test comprehensive payload attributes validation including gas limits |
| 39 | + #[test] |
| 40 | + fn test_payload_attributes_validation() { |
| 41 | + // Test valid attributes with empty transactions (allowed for rollkit) |
| 42 | + let attrs = RollkitPayloadAttributes::new( |
| 43 | + vec![], |
| 44 | + Some(1000000), |
| 45 | + 1234567890, |
| 46 | + B256::random(), |
| 47 | + Address::random(), |
| 48 | + B256::random(), |
| 49 | + 1, |
| 50 | + ); |
| 51 | + assert!( |
| 52 | + attrs.validate().is_ok(), |
| 53 | + "Valid attributes should pass validation" |
| 54 | + ); |
| 55 | + |
| 56 | + // Test valid attributes with no gas limit (should use default) |
| 57 | + let attrs = RollkitPayloadAttributes::new( |
| 58 | + vec![], |
| 59 | + None, |
| 60 | + 1234567890, |
| 61 | + B256::random(), |
| 62 | + Address::random(), |
| 63 | + B256::random(), |
| 64 | + 1, |
| 65 | + ); |
| 66 | + assert!( |
| 67 | + attrs.validate().is_ok(), |
| 68 | + "No gas limit should be valid (use default)" |
| 69 | + ); |
| 70 | + |
| 71 | + // Test invalid attributes with zero gas limit |
| 72 | + let attrs = RollkitPayloadAttributes::new( |
| 73 | + vec![], |
| 74 | + Some(0), |
| 75 | + 1234567890, |
| 76 | + B256::random(), |
| 77 | + Address::random(), |
| 78 | + B256::random(), |
| 79 | + 1, |
| 80 | + ); |
| 81 | + assert!( |
| 82 | + attrs.validate().is_err(), |
| 83 | + "Zero gas limit should be invalid" |
| 84 | + ); |
| 85 | + assert!(matches!( |
| 86 | + attrs.validate().unwrap_err(), |
| 87 | + PayloadAttributesError::InvalidGasLimit |
| 88 | + )); |
| 89 | + } |
| 90 | + |
| 91 | + /// Test gas limit validation edge cases |
| 92 | + #[test] |
| 93 | + fn test_gas_limit_validation() { |
| 94 | + // Test minimum valid gas limit (1) |
| 95 | + let attrs = RollkitPayloadAttributes::new( |
| 96 | + vec![], |
| 97 | + Some(1), |
| 98 | + 1234567890, |
| 99 | + B256::random(), |
| 100 | + Address::random(), |
| 101 | + B256::random(), |
| 102 | + 1, |
| 103 | + ); |
| 104 | + assert!( |
| 105 | + attrs.validate().is_ok(), |
| 106 | + "Minimum gas limit (1) should be valid" |
| 107 | + ); |
| 108 | + |
| 109 | + // Test maximum gas limit |
| 110 | + let attrs = RollkitPayloadAttributes::new( |
| 111 | + vec![], |
| 112 | + Some(u64::MAX), |
| 113 | + 1234567890, |
| 114 | + B256::random(), |
| 115 | + Address::random(), |
| 116 | + B256::random(), |
| 117 | + 1, |
| 118 | + ); |
| 119 | + assert!( |
| 120 | + attrs.validate().is_ok(), |
| 121 | + "Maximum gas limit should be valid" |
| 122 | + ); |
| 123 | + |
| 124 | + // Test typical gas limits |
| 125 | + for gas_limit in [21_000, 1_000_000, 30_000_000, 100_000_000] { |
| 126 | + let attrs = RollkitPayloadAttributes::new( |
| 127 | + vec![], |
| 128 | + Some(gas_limit), |
| 129 | + 1234567890, |
| 130 | + B256::random(), |
| 131 | + Address::random(), |
| 132 | + B256::random(), |
| 133 | + 1, |
| 134 | + ); |
| 135 | + assert!( |
| 136 | + attrs.validate().is_ok(), |
| 137 | + "Typical gas limit {} should be valid", |
| 138 | + gas_limit |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /// Test payload attributes serialization and deserialization |
| 144 | + #[test] |
| 145 | + fn test_payload_attributes_serialization() { |
| 146 | + let attrs = RollkitPayloadAttributes::new( |
| 147 | + vec![], |
| 148 | + Some(1000000), |
| 149 | + 1234567890, |
| 150 | + B256::random(), |
| 151 | + Address::random(), |
| 152 | + B256::random(), |
| 153 | + 1, |
| 154 | + ); |
| 155 | + |
| 156 | + let serialized = serde_json::to_string(&attrs).unwrap(); |
| 157 | + let deserialized: RollkitPayloadAttributes = serde_json::from_str(&serialized).unwrap(); |
| 158 | + |
| 159 | + assert_eq!(attrs.transactions.len(), deserialized.transactions.len()); |
| 160 | + assert_eq!(attrs.gas_limit, deserialized.gas_limit); |
| 161 | + assert_eq!(attrs.timestamp, deserialized.timestamp); |
| 162 | + assert_eq!(attrs.prev_randao, deserialized.prev_randao); |
| 163 | + assert_eq!( |
| 164 | + attrs.suggested_fee_recipient, |
| 165 | + deserialized.suggested_fee_recipient |
| 166 | + ); |
| 167 | + assert_eq!(attrs.parent_hash, deserialized.parent_hash); |
| 168 | + assert_eq!(attrs.block_number, deserialized.block_number); |
| 169 | + } |
| 170 | + |
| 171 | + /// Test all error types and their string representations |
| 172 | + #[test] |
| 173 | + fn test_payload_attributes_errors() { |
| 174 | + // Test that error types can be created and formatted correctly |
| 175 | + let error = PayloadAttributesError::EmptyTransactions; |
| 176 | + assert_eq!(error.to_string(), "No transactions provided"); |
| 177 | + |
| 178 | + let error = PayloadAttributesError::InvalidGasLimit; |
| 179 | + assert_eq!(error.to_string(), "Invalid gas limit"); |
| 180 | + |
| 181 | + let error = PayloadAttributesError::TransactionValidation("test error".to_string()); |
| 182 | + assert_eq!( |
| 183 | + error.to_string(), |
| 184 | + "Transaction validation failed: test error" |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + /// Test payload attributes with edge case values |
| 189 | + #[test] |
| 190 | + fn test_payload_attributes_edge_cases() { |
| 191 | + // Test with maximum timestamp |
| 192 | + let attrs = RollkitPayloadAttributes::new( |
| 193 | + vec![], |
| 194 | + Some(u64::MAX), |
| 195 | + u64::MAX, |
| 196 | + B256::repeat_byte(0xFF), |
| 197 | + Address::repeat_byte(0xFF), |
| 198 | + B256::repeat_byte(0xFF), |
| 199 | + u64::MAX, |
| 200 | + ); |
| 201 | + assert!(attrs.validate().is_ok(), "Maximum values should be valid"); |
| 202 | + |
| 203 | + // Test with zero values (except gas limit which must be > 0) |
| 204 | + let attrs = RollkitPayloadAttributes::new( |
| 205 | + vec![], |
| 206 | + Some(1), // Minimum valid gas limit |
| 207 | + 0, |
| 208 | + B256::ZERO, |
| 209 | + Address::ZERO, |
| 210 | + B256::ZERO, |
| 211 | + 0, |
| 212 | + ); |
| 213 | + assert!( |
| 214 | + attrs.validate().is_ok(), |
| 215 | + "Zero values (except gas limit) should be valid" |
| 216 | + ); |
| 217 | + |
| 218 | + // Test with empty byte values |
| 219 | + let attrs = RollkitPayloadAttributes::new( |
| 220 | + vec![], |
| 221 | + None, // No gas limit specified |
| 222 | + 1234567890, |
| 223 | + B256::ZERO, |
| 224 | + Address::ZERO, |
| 225 | + B256::ZERO, |
| 226 | + 1, |
| 227 | + ); |
| 228 | + assert!(attrs.validate().is_ok(), "Zero byte values should be valid"); |
| 229 | + } |
| 230 | + |
| 231 | + /// Test validation consistency across different scenarios |
| 232 | + #[test] |
| 233 | + fn test_validation_consistency() { |
| 234 | + // Test that validation is consistent regardless of other field values |
| 235 | + let base_attrs = |gas_limit| { |
| 236 | + RollkitPayloadAttributes::new( |
| 237 | + vec![], |
| 238 | + gas_limit, |
| 239 | + 1234567890, |
| 240 | + B256::random(), |
| 241 | + Address::random(), |
| 242 | + B256::random(), |
| 243 | + 1, |
| 244 | + ) |
| 245 | + }; |
| 246 | + |
| 247 | + // Valid gas limits should always pass |
| 248 | + assert!(base_attrs(Some(1)).validate().is_ok()); |
| 249 | + assert!(base_attrs(Some(21_000)).validate().is_ok()); |
| 250 | + assert!(base_attrs(Some(30_000_000)).validate().is_ok()); |
| 251 | + assert!(base_attrs(None).validate().is_ok()); |
| 252 | + |
| 253 | + // Invalid gas limits should always fail |
| 254 | + assert!(base_attrs(Some(0)).validate().is_err()); |
| 255 | + } |
| 256 | +} |
0 commit comments