|
1 | 1 | use crate::{ |
2 | | - alloy::primitives::TxHash, |
3 | | - prelude::*, |
4 | | - reth::primitives::Recovered, |
| 2 | + alloy::primitives::TxHash, prelude::*, reth::primitives::Recovered, |
5 | 3 | }; |
6 | 4 |
|
7 | 5 | /// Quality of Life extensions for the `Span` type. |
@@ -106,3 +104,121 @@ impl<P: Platform> SpanExt<P> for Span<P> { |
106 | 104 | } |
107 | 105 | } |
108 | 106 | } |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod span_ext_tests { |
| 110 | + use crate::{ |
| 111 | + payload::{Span, SpanExt}, |
| 112 | + prelude::{BlockContext, Checkpoint, Ethereum}, |
| 113 | + test_utils::BlockContextMocked, |
| 114 | + }; |
| 115 | + |
| 116 | + use { |
| 117 | + alloy::{ |
| 118 | + consensus::{EthereumTxEnvelope, TxEip4844}, |
| 119 | + network::{TransactionBuilder, TxSignerSync}, |
| 120 | + primitives::{Address, U256}, |
| 121 | + signers::local::PrivateKeySigner, |
| 122 | + }, |
| 123 | + rblib::{alloy, reth, test_utils::FundedAccounts}, |
| 124 | + reth::{ |
| 125 | + ethereum::{TransactionSigned, primitives::SignedTransaction}, |
| 126 | + primitives::Recovered, |
| 127 | + rpc::types::TransactionRequest, |
| 128 | + }, |
| 129 | + }; |
| 130 | + |
| 131 | + fn transfer_tx( |
| 132 | + signer: &PrivateKeySigner, |
| 133 | + nonce: u64, |
| 134 | + value: U256, |
| 135 | + ) -> Recovered<EthereumTxEnvelope<TxEip4844>> { |
| 136 | + let mut tx = TransactionRequest::default() |
| 137 | + .with_nonce(nonce) |
| 138 | + .with_to(Address::random()) |
| 139 | + .value(value) |
| 140 | + .with_gas_price(1_000_000_000) |
| 141 | + .with_gas_limit(21_000) |
| 142 | + .with_max_priority_fee_per_gas(1_000_000) |
| 143 | + .with_max_fee_per_gas(2_000_000) |
| 144 | + .build_unsigned() |
| 145 | + .expect("valid transaction request"); |
| 146 | + |
| 147 | + let sig = signer |
| 148 | + .sign_transaction_sync(&mut tx) |
| 149 | + .expect("signing should succeed"); |
| 150 | + |
| 151 | + TransactionSigned::new_unhashed(tx.into(), sig) |
| 152 | + .with_signer(signer.address()) |
| 153 | + } |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn gas_and_blob_gas_used_sum_correctly() { |
| 157 | + let (block, _) = BlockContext::<Ethereum>::mocked(); |
| 158 | + |
| 159 | + let root = block.start(); |
| 160 | + let c1: Checkpoint<Ethereum> = root.barrier(); |
| 161 | + let c2: Checkpoint<Ethereum> = c1.barrier(); |
| 162 | + |
| 163 | + let span = Span::<Ethereum>::between(&root, &c2).unwrap(); |
| 164 | + |
| 165 | + assert_eq!(span.gas_used(), 0); |
| 166 | + assert_eq!(span.blob_gas_used(), 0); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn contains_transaction_hash_works() { |
| 171 | + let (block, _) = BlockContext::<Ethereum>::mocked(); |
| 172 | + |
| 173 | + let root = block.start(); |
| 174 | + let c1: Checkpoint<Ethereum> = root.barrier(); |
| 175 | + |
| 176 | + let tx1 = transfer_tx(&FundedAccounts::signer(0), 0, U256::from(50_000u64)); |
| 177 | + |
| 178 | + let c2 = c1.apply(tx1.clone()).unwrap(); |
| 179 | + |
| 180 | + let span = Span::<Ethereum>::between(&root, &c2).unwrap(); |
| 181 | + |
| 182 | + assert!(span.contains(*tx1.hash())); |
| 183 | + } |
| 184 | + |
| 185 | + #[test] |
| 186 | + fn split_at_produces_valid_halves() { |
| 187 | + let (block, _) = BlockContext::<Ethereum>::mocked(); |
| 188 | + |
| 189 | + let root = block.start(); |
| 190 | + let c1: Checkpoint<Ethereum> = root.barrier(); |
| 191 | + let c2: Checkpoint<Ethereum> = c1.barrier(); |
| 192 | + |
| 193 | + let span = Span::<Ethereum>::between(&root, &c2).unwrap(); |
| 194 | + |
| 195 | + let (left, right) = span.split_at(1); |
| 196 | + assert_eq!(left.len(), 1); |
| 197 | + assert_eq!(right.len(), 2); |
| 198 | + |
| 199 | + let (left_all, right_empty) = span.split_at(10); |
| 200 | + assert_eq!(left_all.len(), span.len()); |
| 201 | + assert!(right_empty.is_empty()); |
| 202 | + } |
| 203 | + |
| 204 | + #[test] |
| 205 | + fn take_and_skip_are_consistent_with_split_at() { |
| 206 | + let (block, _) = BlockContext::<Ethereum>::mocked(); |
| 207 | + |
| 208 | + let root = block.start(); |
| 209 | + let c1: Checkpoint<Ethereum> = root.barrier(); |
| 210 | + let c2: Checkpoint<Ethereum> = c1.barrier(); |
| 211 | + |
| 212 | + let span = Span::<Ethereum>::between(&root, &c2).unwrap(); |
| 213 | + |
| 214 | + let take_1 = span.take(1); |
| 215 | + let skip_1 = span.skip(1); |
| 216 | + |
| 217 | + assert_eq!(take_1.len(), 1); |
| 218 | + assert_eq!(skip_1.len(), 2); |
| 219 | + |
| 220 | + let (left, right) = span.split_at(1); |
| 221 | + assert_eq!(take_1.len(), left.len()); |
| 222 | + assert_eq!(skip_1.len(), right.len()); |
| 223 | + } |
| 224 | +} |
0 commit comments