|
| 1 | +use { |
| 2 | + crate::scenarios::RunConfig, |
| 3 | + anyhow::{bail, ensure, Context, Result}, |
| 4 | + arc_swap::ArcSwap, |
| 5 | + futures::SinkExt, |
| 6 | + std::{ |
| 7 | + collections::HashMap, |
| 8 | + sync::{Arc, Mutex}, |
| 9 | + time::Duration, |
| 10 | + }, |
| 11 | + tokio_stream::StreamExt, |
| 12 | + yellowstone_block_machine::dragonsmouth::{ |
| 13 | + stream::{BlockMachineOutput, BlockStream}, |
| 14 | + wrapper::RESERVED_FILTER_NAME, |
| 15 | + }, |
| 16 | + yellowstone_grpc_client::{ |
| 17 | + test_tools::{Unstable, UnstableConnector}, |
| 18 | + AutoReconnect, Backoff, DedupState, DedupStream, InterceptorXToken, ReconnectConfig, |
| 19 | + ReplayPolicy, TonicGrpcConnector, |
| 20 | + }, |
| 21 | + yellowstone_grpc_e2e_macros::test_helper, |
| 22 | + yellowstone_grpc_geyser::plugin::message::CommitmentLevel, |
| 23 | + yellowstone_grpc_proto::{ |
| 24 | + geyser::{geyser_client::GeyserClient, SubscribeRequest, SubscribeRequestFilterSlots}, |
| 25 | + tonic::{transport::Endpoint, Request}, |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +/// Verifies auto-reconnect recovers from simulated stream drops and |
| 30 | +/// block reconstruction produces gapless frozen blocks across reconnects. |
| 31 | +#[test_helper(name = "reconnect-blocks", tags = ["reconnect"])] |
| 32 | +pub async fn it_should_reconstruct_blocks_across_reconnects(config: &RunConfig) -> Result<()> { |
| 33 | + let drop_interval = Duration::from_secs(15); |
| 34 | + let target_blocks = 30u64; |
| 35 | + |
| 36 | + let endpoint = Endpoint::from_shared(config.endpoint.clone()).context("valid endpoint")?; |
| 37 | + let channel = endpoint.connect().await.context("channel connect")?; |
| 38 | + |
| 39 | + let x_token = config |
| 40 | + .x_token |
| 41 | + .as_ref() |
| 42 | + .map(|t| t.parse()) |
| 43 | + .transpose() |
| 44 | + .context("valid x-token")?; |
| 45 | + |
| 46 | + let interceptor = InterceptorXToken { |
| 47 | + x_token: x_token.clone(), |
| 48 | + x_request_snapshot: false, |
| 49 | + }; |
| 50 | + |
| 51 | + let mut geyser = GeyserClient::with_interceptor(channel, interceptor) |
| 52 | + .max_decoding_message_size(16 * 1024 * 10224); |
| 53 | + |
| 54 | + let request = SubscribeRequest { |
| 55 | + slots: HashMap::from([( |
| 56 | + RESERVED_FILTER_NAME.to_string(), |
| 57 | + SubscribeRequestFilterSlots { |
| 58 | + interslot_updates: Some(true), |
| 59 | + ..Default::default() |
| 60 | + }, |
| 61 | + )]), |
| 62 | + blocks: HashMap::from([("test".to_string(), Default::default())]), |
| 63 | + blocks_meta: HashMap::from([("test".to_string(), Default::default())]), |
| 64 | + accounts: HashMap::from([("test".to_string(), Default::default())]), |
| 65 | + transactions: HashMap::from([("test".to_string(), Default::default())]), |
| 66 | + entry: HashMap::from([("test".to_string(), Default::default())]), |
| 67 | + commitment: Some(CommitmentLevel::Confirmed as i32), |
| 68 | + ..Default::default() |
| 69 | + }; |
| 70 | + |
| 71 | + let (mut subscribe_tx, subscribe_rx) = futures::channel::mpsc::channel(1000); |
| 72 | + subscribe_tx |
| 73 | + .send(request.clone()) |
| 74 | + .await |
| 75 | + .context("initial send")?; |
| 76 | + |
| 77 | + let response = geyser |
| 78 | + .subscribe(Request::new(subscribe_rx)) |
| 79 | + .await |
| 80 | + .context("initial subscribe")?; |
| 81 | + let raw_stream = response.into_inner(); |
| 82 | + |
| 83 | + let request_sink = Arc::new(Mutex::new(subscribe_tx)); |
| 84 | + let shared_request = Arc::new(ArcSwap::from_pointee(request)); |
| 85 | + |
| 86 | + let reconnect_config = ReconnectConfig { |
| 87 | + backoff: Backoff::new(Duration::from_millis(500), 2.0, 5), |
| 88 | + replay_policy: ReplayPolicy::default(), |
| 89 | + slot_retention: 250, |
| 90 | + }; |
| 91 | + |
| 92 | + let tonic_connector = TonicGrpcConnector::new( |
| 93 | + endpoint, |
| 94 | + reconnect_config.backoff.clone(), |
| 95 | + x_token, |
| 96 | + Default::default(), |
| 97 | + Arc::clone(&request_sink), |
| 98 | + ); |
| 99 | + let connector = UnstableConnector::new(tonic_connector, drop_interval); |
| 100 | + |
| 101 | + let unstable_stream = Unstable::new(raw_stream, drop_interval); |
| 102 | + |
| 103 | + let dedup = DedupStream::new( |
| 104 | + unstable_stream, |
| 105 | + DedupState::with_slot_retention(reconnect_config.slot_retention), |
| 106 | + ); |
| 107 | + |
| 108 | + let auto_reconnect = AutoReconnect::new( |
| 109 | + dedup, |
| 110 | + connector, |
| 111 | + Arc::clone(&shared_request), |
| 112 | + reconnect_config, |
| 113 | + ); |
| 114 | + |
| 115 | + let mut block_stream = BlockStream::new( |
| 116 | + auto_reconnect, |
| 117 | + solana_commitment_config::CommitmentLevel::Processed, |
| 118 | + ); |
| 119 | + |
| 120 | + let mut block_count = 0u64; |
| 121 | + let mut last_slot = 0u64; |
| 122 | + let mut gaps = 0u64; |
| 123 | + |
| 124 | + let result = tokio::time::timeout(Duration::from_secs(50), async { |
| 125 | + while let Some(item) = block_stream.next().await { |
| 126 | + if block_count >= target_blocks { |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + match item { |
| 131 | + Ok(BlockMachineOutput::FrozenBlock(block)) => { |
| 132 | + block_count += 1; |
| 133 | + |
| 134 | + if last_slot > 0 && block.slot != last_slot + 1 { |
| 135 | + log::warn!( |
| 136 | + "gap: expected slot {} but got {} (missed {})", |
| 137 | + last_slot + 1, |
| 138 | + block.slot, |
| 139 | + block.slot - last_slot - 1, |
| 140 | + ); |
| 141 | + gaps += 1; |
| 142 | + } |
| 143 | + |
| 144 | + log::info!( |
| 145 | + "block slot={} txns={} accounts={} entries={} total={block_count}/{target_blocks}", |
| 146 | + block.slot, |
| 147 | + block.txn_len(), |
| 148 | + block.account_len(), |
| 149 | + block.entry_len(), |
| 150 | + ); |
| 151 | + |
| 152 | + last_slot = block.slot; |
| 153 | + } |
| 154 | + Ok(BlockMachineOutput::SlotCommitmentUpdate(u)) => { |
| 155 | + log::debug!("commitment slot={} level={:?}", u.slot, u.commitment); |
| 156 | + } |
| 157 | + Ok(BlockMachineOutput::ForkDetected(f)) => { |
| 158 | + log::warn!("fork slot={}", f.slot); |
| 159 | + } |
| 160 | + Ok(BlockMachineOutput::DeadBlockDetect(d)) => { |
| 161 | + log::warn!("dead slot={}", d.slot); |
| 162 | + } |
| 163 | + Err(e) => { |
| 164 | + bail!("block stream error: {e}"); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + Ok(()) |
| 169 | + }) |
| 170 | + .await; |
| 171 | + |
| 172 | + match result { |
| 173 | + Ok(inner) => inner?, |
| 174 | + Err(_) => bail!("test timed out after 50s with {block_count}/{target_blocks} blocks"), |
| 175 | + } |
| 176 | + |
| 177 | + ensure!( |
| 178 | + gaps <= 2, |
| 179 | + "block reconstruction should have minimal gaps across reconnects, got {gaps} gaps" |
| 180 | + ); |
| 181 | + if gaps > 0 { |
| 182 | + log::warn!("reconnect-blocks: {gaps} gaps detected, known block-machine interop issue"); |
| 183 | + } |
| 184 | + |
| 185 | + Ok(()) |
| 186 | +} |
0 commit comments