|
5 | 5 |
|
6 | 6 | use crate::e2e::{setup_test_boilerplate, test_signer}; |
7 | 7 | use alloy_consensus::BlockHeader; |
8 | | -use alloy_eips::eip2718::Encodable2718; |
9 | | -use alloy_primitives::{Address, B256, Bloom, Bytes, U256}; |
| 8 | +use alloy_eips::{BlockId, eip2718::Encodable2718}; |
| 9 | +use alloy_primitives::{Address, B256, Bloom, Bytes, TxKind, U256}; |
10 | 10 | use alloy_provider::Provider; |
| 11 | +use alloy_rpc_types_eth::TransactionRequest; |
11 | 12 | use bera_reth::{ |
12 | 13 | engine::validator::BerachainEngineValidatorBuilder, |
13 | 14 | flashblocks::{ |
@@ -233,6 +234,123 @@ async fn test_rpc_returns_flashblock_pending_receipt() -> eyre::Result<()> { |
233 | 234 | Ok(()) |
234 | 235 | } |
235 | 236 |
|
| 237 | +/// Tests that state-reading RPC methods return flashblock pending state. |
| 238 | +/// |
| 239 | +/// This test verifies that when a flashblock contains a transfer transaction: |
| 240 | +/// - `eth_getBalance` returns the recipient's updated balance |
| 241 | +/// - `eth_getTransactionCount` returns the sender's incremented nonce |
| 242 | +#[tokio::test] |
| 243 | +async fn test_rpc_returns_flashblock_pending_state() -> eyre::Result<()> { |
| 244 | + let (tasks, chain_spec) = setup_test_boilerplate().await?; |
| 245 | + let executor = tasks.executor(); |
| 246 | + |
| 247 | + let (pending_tx, pending_rx) = watch::channel(None); |
| 248 | + let (in_progress_tx, in_progress_rx) = watch::channel(None); |
| 249 | + let (unused_sequence_tx, _) = |
| 250 | + broadcast::channel::<FlashBlockCompleteSequence<BerachainFlashblockPayload>>(1); |
| 251 | + let (unused_received_tx, _) = broadcast::channel::<Arc<BerachainFlashblockPayload>>(1); |
| 252 | + |
| 253 | + let listeners: FlashblocksListeners<BerachainPrimitives, BerachainFlashblockPayload> = |
| 254 | + FlashblocksListeners::new( |
| 255 | + pending_rx, |
| 256 | + unused_sequence_tx, |
| 257 | + in_progress_rx, |
| 258 | + unused_received_tx, |
| 259 | + ); |
| 260 | + |
| 261 | + let eth_api_builder = BerachainEthApiBuilder::default().with_flashblocks_listeners(listeners); |
| 262 | + let add_ons = BerachainAddOns::<_, _, BerachainEngineValidatorBuilder>::new(eth_api_builder); |
| 263 | + |
| 264 | + let node_config = NodeConfig::new(chain_spec.clone()) |
| 265 | + .with_unused_ports() |
| 266 | + .with_rpc(RpcServerArgs::default().with_unused_ports().with_http()); |
| 267 | + |
| 268 | + let NodeHandle { node, node_exit_future: _ } = NodeBuilder::new(node_config) |
| 269 | + .testing_node(executor.clone()) |
| 270 | + .with_types::<BerachainNode>() |
| 271 | + .with_components(BerachainNode::default().components_builder()) |
| 272 | + .with_add_ons(add_ons) |
| 273 | + .launch() |
| 274 | + .await?; |
| 275 | + |
| 276 | + let (fb_tx, fb_rx) = tokio::sync::mpsc::channel::<BerachainFlashblockPayload>(128); |
| 277 | + let stream = MockFlashblockStream { rx: fb_rx }; |
| 278 | + |
| 279 | + let service = FlashBlockService::new( |
| 280 | + stream, |
| 281 | + node.evm_config.clone(), |
| 282 | + node.provider().clone(), |
| 283 | + executor.clone(), |
| 284 | + false, |
| 285 | + ); |
| 286 | + |
| 287 | + let mut service_in_progress_rx = service.subscribe_in_progress(); |
| 288 | + |
| 289 | + executor.spawn_critical( |
| 290 | + "flashblock-service", |
| 291 | + Box::pin(async move { |
| 292 | + service.run(pending_tx).await; |
| 293 | + }), |
| 294 | + ); |
| 295 | + |
| 296 | + let latest = node.provider().latest_header()?.expect("should have genesis"); |
| 297 | + let latest_hash = latest.hash(); |
| 298 | + let next_block = latest.number() + 1; |
| 299 | + let next_timestamp = latest.timestamp() + 2; |
| 300 | + |
| 301 | + // Create a transfer transaction to a fresh recipient address |
| 302 | + let recipient = Address::random(); |
| 303 | + let transfer_value = U256::from(100); |
| 304 | + |
| 305 | + let signer = test_signer()?; |
| 306 | + let sender = signer.address(); |
| 307 | + let chain_id = chain_spec.chain_id(); |
| 308 | + |
| 309 | + // Build a transfer transaction with a specific recipient |
| 310 | + let tx_request = TransactionRequest { |
| 311 | + nonce: Some(0), |
| 312 | + value: Some(transfer_value), |
| 313 | + to: Some(TxKind::Call(recipient)), |
| 314 | + gas: Some(21000), |
| 315 | + max_fee_per_gas: Some(20e9 as u128), |
| 316 | + max_priority_fee_per_gas: Some(20e9 as u128), |
| 317 | + chain_id: Some(chain_id), |
| 318 | + ..Default::default() |
| 319 | + }; |
| 320 | + let tx = TransactionTestContext::sign_tx(signer, tx_request).await; |
| 321 | + let tx_bytes = Bytes::from(tx.encoded_2718()); |
| 322 | + |
| 323 | + let payload_id = PayloadId::new([2u8; 8]); |
| 324 | + let mut fb0 = create_test_flashblock(0, next_block, payload_id, latest_hash, next_timestamp); |
| 325 | + fb0.diff.transactions = vec![tx_bytes]; |
| 326 | + fb0.diff.gas_used = 21000; |
| 327 | + |
| 328 | + // Inject the flashblock |
| 329 | + fb_tx.send(fb0).await?; |
| 330 | + |
| 331 | + // Wait for service to signal it's building |
| 332 | + tokio::time::timeout(Duration::from_millis(500), service_in_progress_rx.changed()).await??; |
| 333 | + in_progress_tx.send(*service_in_progress_rx.borrow())?; |
| 334 | + |
| 335 | + // Give the service time to build and publish the pending block |
| 336 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 337 | + |
| 338 | + let rpc_url = |
| 339 | + format!("http://127.0.0.1:{}", node.rpc_server_handle().http_local_addr().unwrap().port()); |
| 340 | + let client = alloy_provider::ProviderBuilder::new().connect(&rpc_url).await?; |
| 341 | + |
| 342 | + // Check recipient balance with "pending" block tag - should reflect the pending transfer |
| 343 | + let balance = client.get_balance(recipient).block_id(BlockId::pending()).await?; |
| 344 | + assert_eq!(balance, transfer_value); |
| 345 | + |
| 346 | + // Verify that the nonce is 1 more in flashblock state |
| 347 | + let nonce_latest = client.get_transaction_count(sender).block_id(BlockId::latest()).await?; |
| 348 | + let nonce_pending = client.get_transaction_count(sender).block_id(BlockId::pending()).await?; |
| 349 | + assert_eq!(nonce_pending, nonce_latest + 1); |
| 350 | + |
| 351 | + Ok(()) |
| 352 | +} |
| 353 | + |
236 | 354 | /// Tests that flashblocks with invalid parent hashes are rejected. |
237 | 355 | #[tokio::test] |
238 | 356 | async fn test_flashblock_rejects_invalid_parent_hash() -> eyre::Result<()> { |
|
0 commit comments