|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use anyhow::bail; |
| 4 | +use async_trait::async_trait; |
| 5 | +use bitcoincore_rpc::{json::IndexStatus, RpcApi}; |
| 6 | +use citrea_e2e::{ |
| 7 | + config::{BitcoinConfig, TestCaseConfig}, |
| 8 | + framework::TestFramework, |
| 9 | + test_case::{TestCase, TestCaseRunner}, |
| 10 | + traits::Restart, |
| 11 | + Result, |
| 12 | +}; |
| 13 | + |
| 14 | +struct BasicSyncTest; |
| 15 | + |
| 16 | +#[async_trait] |
| 17 | +impl TestCase for BasicSyncTest { |
| 18 | + fn test_config() -> TestCaseConfig { |
| 19 | + TestCaseConfig { |
| 20 | + with_sequencer: false, |
| 21 | + n_nodes: 2, |
| 22 | + timeout: Duration::from_secs(60), |
| 23 | + ..Default::default() |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + async fn run_test(&mut self, f: &mut TestFramework) -> Result<()> { |
| 28 | + let (Some(da0), Some(da1)) = (f.bitcoin_nodes.get(0), f.bitcoin_nodes.get(1)) else { |
| 29 | + bail!("bitcoind not running. Test should run with two da nodes") |
| 30 | + }; |
| 31 | + let initial_height = f.initial_da_height; |
| 32 | + |
| 33 | + // Generate some blocks on node0 |
| 34 | + da0.generate(5, None).await?; |
| 35 | + |
| 36 | + let height0 = da0.get_block_count().await?; |
| 37 | + let height1 = da1.get_block_count().await?; |
| 38 | + |
| 39 | + // Nodes are now out of sync |
| 40 | + assert_eq!(height0, initial_height + 5); |
| 41 | + assert_eq!(height1, 0); |
| 42 | + |
| 43 | + // Sync both nodes |
| 44 | + f.bitcoin_nodes |
| 45 | + .wait_for_sync(Some(Duration::from_secs(30))) |
| 46 | + .await?; |
| 47 | + |
| 48 | + let height0 = da0.get_block_count().await?; |
| 49 | + let height1 = da1.get_block_count().await?; |
| 50 | + |
| 51 | + // Assert that nodes are in sync |
| 52 | + assert_eq!(height0, height1, "Block heights don't match"); |
| 53 | + |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[tokio::test] |
| 59 | +async fn test_basic_sync() -> Result<()> { |
| 60 | + TestCaseRunner::new(BasicSyncTest).run().await |
| 61 | +} |
| 62 | + |
| 63 | +struct RestartBitcoinTest; |
| 64 | + |
| 65 | +#[async_trait] |
| 66 | +impl TestCase for RestartBitcoinTest { |
| 67 | + fn test_config() -> TestCaseConfig { |
| 68 | + TestCaseConfig { |
| 69 | + with_sequencer: false, |
| 70 | + ..Default::default() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn bitcoin_config() -> BitcoinConfig { |
| 75 | + BitcoinConfig { |
| 76 | + extra_args: vec!["-txindex=0"], |
| 77 | + ..Default::default() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async fn run_test(&mut self, f: &mut TestFramework) -> Result<()> { |
| 82 | + let da = f.bitcoin_nodes.get_mut(0).unwrap(); |
| 83 | + // Add txindex flag to check that restart takes into account the extra args |
| 84 | + let new_conf = BitcoinConfig { |
| 85 | + extra_args: vec!["-txindex=1"], |
| 86 | + ..da.config.clone() |
| 87 | + }; |
| 88 | + |
| 89 | + let block_before = da.get_block_count().await?; |
| 90 | + let info = da.get_index_info().await?; |
| 91 | + |
| 92 | + assert_eq!(info.txindex, None); |
| 93 | + |
| 94 | + // Restart node with txindex |
| 95 | + da.restart(Some(new_conf)).await?; |
| 96 | + |
| 97 | + let block_after = da.get_block_count().await?; |
| 98 | + let info = da.get_index_info().await?; |
| 99 | + |
| 100 | + assert!(matches!( |
| 101 | + info.txindex, |
| 102 | + Some(IndexStatus { synced: true, .. }) |
| 103 | + )); |
| 104 | + // Assert that state is kept between restarts |
| 105 | + assert_eq!(block_before, block_after); |
| 106 | + |
| 107 | + Ok(()) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[tokio::test] |
| 112 | +async fn test_restart_bitcoin() -> Result<()> { |
| 113 | + TestCaseRunner::new(RestartBitcoinTest).run().await |
| 114 | +} |
0 commit comments